Skip to content

Instantly share code, notes, and snippets.

@vvuk
Created June 25, 2017 23:19
Show Gist options
  • Save vvuk/c44c4922d3fc4943502ce0c4f3145baa to your computer and use it in GitHub Desktop.
Save vvuk/c44c4922d3fc4943502ce0c4f3145baa to your computer and use it in GitHub Desktop.
#include <memory>
#include <emscripten.h>
#include <emscripten/bind.h>
class Base
{
public:
Base() {}
void Poke() {}
};
class Derived
: public Base
{
public:
Derived() {}
virtual void DoThing() = 0;
};
class Foo
{
public:
void DoSomething(std::shared_ptr<Base> aBase) {
aBase->Poke();
}
};
using namespace emscripten;
struct DerivedWrapper : public wrapper<Derived> {
EMSCRIPTEN_WRAPPER(DerivedWrapper);
void DoThing() {
return call<void>("doThing");
}
};
EMSCRIPTEN_BINDINGS(emb) {
class_<Base>("Base")
.smart_ptr_constructor("spBase", &std::make_shared<Base>)
.function("poke", &Base::Poke)
;
// This fails to compile, because
// error: field type 'Derived' is an abstract class
// if I comment out the _constructor line and use the .smart_ptr(),
// then it compiles, but then at runtime it fails with
// BindingError: Passing raw pointer to smart pointer is illegal,
class_<Derived, base<Base>>("Derived")
.smart_ptr_constructor("spDerived", &std::make_shared<Derived>)
//.smart_ptr<std::shared_ptr<Derived>>("spDerived")
.allow_subclass<DerivedWrapper>("DerivedWrapper")
.function("doThing", &Derived::DoThing, pure_virtual())
;
class_<Foo>("Foo")
.smart_ptr_constructor("spFoo", &std::make_shared<Foo>)
.function("doSomething", &Foo::DoSomething)
;
}
int main(int argc, char **arv)
{
EM_ASM({
var foo = new Module.Foo();
var bs = new Module.Base();
foo.doSomething(bs);
Module.print('yay');
});
}
@jymuk
Copy link

jymuk commented Jan 31, 2019

Hej,
Did you ever find a solution to BindingError: Passing raw pointer to smart pointer is illegal?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment