Category : embind

Imagine that I have the following variadic class in C++: template<typename… Ts> class IFoo { virtual bar(Ts… values) = 0; }; I want to implement this class in JavaScript. To do so, I must create a wrapper. template<typename… Ts> struct FooWrapper : public emscripten::wrapper<IFoo<Ts…>> { EMSCRIPTEN_WRAPPER(FooWrapper); // Implement the class’ functions. } This results in ..

Read more

I have a function that takes a std::function as a parameter: class Foo { virtual void bar(std::function<void()> &&func) = 0; }; I want to create a JS wrapper that implements Foo. struct FooWrapper : public wrapper<Foo> { EMSCRIPTEN_WRAPPER(FooWrapper); void bar(std::function<void()> &&func) override { call<void>("bar", std::move(func)); } }; EMSCRIPTEN_BINDINGS(Foo) { class_<Foo>("Foo") .smart_ptr<Foo>("Foo") .allow_subclass<FooWrapper, std::shared_ptr<FooWrapper>>("FooWraper", "FooWraperSharedPtr"); } ..

Read more

Consider the following contrived example: class IBar { void qux() = 0; }; class IFoo { void execute(const IBar &bar) = 0; }; struct FooWrapper : public wrapper<IFoo> { EMSCRIPTEN_WRAPPER(FooWrapper); void execute(const IBar &bar) override { return call<void>("execute", bar); } }; EMSCRIPTEN_BINDINGS(IFoo) { class_<IFoo>("IFoo") .function("execute", &IFoo::execute, allow_raw_pointers(), pure_virtual()) .allow_subclass<FooWrapper>("FooWrapper"); } If I try to compile ..

Read more

Consider the following C++ code and corresponding Emscripten bindings. class IBar { void qux() = 0; }; struct BarWrapper : public wrapper<IBar> { void qux() override { return call<>("qux"); } } EMSCRIPTEN_BINDINGS(IBar) { class_<IBar>("IBar") .smart_ptr<std::shared_ptr<IBar>>("IBar") .function("qux", &IBar::qux) .allow_subclass<BarWrapper>("BarWrapper");; } class Foo { std::shared_ptr<IBar> getBar() const; void setBar(std::shared_ptr<IBar> bar); }; EMSCRIPTEN_BINDINGS(Foo) { class_<Options>("Foo") .constructor<>() .property("bar", &Foo::getBar, ..

Read more

I have two fairly simple C++ class definitions and their interfaces, uuid.{hpp,cpp} and uuid_util.{hpp,cpp}, and I have one more file uuid_bind.cpp with #include <emscripten/bind.h> to bind the C++ classes, function and static function definitions to JavaScript. The two classes are first built as a static library uuid_lib.a, which is then linked against the latter C++ ..

Read more

To pass typed array from emscripten’ed C++ to javascript I came up with this code #include <emscripten/bind.h> #include <emscripten/val.h> auto test(const emscripten::val &input) { const auto data = emscripten::convertJSArrayToNumberVector<float>(input); // copies data // generate output in some form std::vector<float> output = { 1, 2, 3 }; // make a typed array view of the output ..

Read more