Created
June 25, 2017 23:19
-
-
Save vvuk/c44c4922d3fc4943502ce0c4f3145baa to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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'); | |
}); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hej,
Did you ever find a solution to BindingError: Passing raw pointer to smart pointer is illegal?