Hi,
I have a couple of structs objects, inheriting from a base struct, and
passed using smart pointers. I've tried to bind them using embind, but when
I look at the object in the debugger I see that it isn't a smart pointer
but a raw pointer, and when I try to pass the object to another object, I
get an error that the wrong type of object is passed.
c++ code:
struct RenderModel {};
EMSCRIPTEN_BINDINGS(RenderModelBinding) {
emscripten::class_<RenderModel>("RenderModel")
.constructor<>()
.smart_ptr<std::shared_ptr<RenderModel>>("RenderModel");
}
struct GroupRenderModel : RenderModel {
void addModel(std::shared_ptr<wre_model::RenderModel> model) {
models.push_back(model);
}
std::vector<std::shared_ptr<wre_model::RenderModel>> models;
};
EMSCRIPTEN_BINDINGS(GroupRenderModelBinding) {
emscripten::class_<GroupRenderModel>("GroupRenderModel")
.constructor<>()
.function("addModel", &GroupRenderModel::addModel)
.smart_ptr<std::shared_ptr<GroupRenderModel>>("GroupRenderModel");
}
struct TextureRenderModel : public RenderModel {};
EMSCRIPTEN_BINDINGS(TextureRenderModelBinding) {
emscripten::class_<TextureRenderModel>("TextureRenderModel")
.constructor<>()
.smart_ptr<std::shared_ptr<TextureRenderModel>>("TextureRenderModel");
}
JS code:
const groupLayer = new Module.GroupRenderModel();
const textureLayer = new Module.TextureRenderModel();
groupLayer.addModel(textureLayer);
When checking in the debugger, I see that groupLayer's pointer is:
1. ptrType: RegisteredPointer
1. destructorFunction: null
2. isConst: false
3. isReference: false
4. isSmartPointer: false
5. name: "GroupRenderModel*"
textureLayer's pointer is:
1. ptrType: RegisteredPointer
1. destructorFunction: null
2. isConst: false
3. isReference: false
4. isSmartPointer: false
5. name: "TextureRenderModel*"
And the error printed when calling setModel is:
1. BindingError {name: "BindingError", message: "Expected null or
instance of RenderModel, got an instance of TextureRenderModel", stack:
"BindingError:
Expected null or instance of
RenderM…ttp://localhost:51840/client.e31bb0bc.js:68387:15"}
2. message: "Expected null or instance of RenderModel, got an instance
of TextureRenderModel"
1. name: "BindingError"
2. stack: "BindingError: Expected null or instance of RenderModel,
got an instance of TextureRenderModel↵ at BindingError.<anonymous>
(http://localhost:51840/render/appWASM.js:5207:24)↵ at new BindingError
(eval at createNamedFunction
(http://localhost:51840/render/appWASM.js:1:1), <anonymous>:4:34)↵ at
throwBindingError (http://localhost:51840/render/appWASM.js:5225:13)↵ at
upcastPointer (http://localhost:51840/render/appWASM.js:5569:15)↵ at
RegisteredPointer.genericPointerToWireType [as toWireType]
(http://localhost:51840/render/appWASM.js:5622:13)↵ at
GroupRenderModel$addModel [as addModel] (eval at new_
(http://localhost:51840/render/appWASM.js:1:1), <anonymous>:9:26)↵ at
CompositionCanvas.updateRenderEngine
(http://localhost:51840/client.e31bb0bc.js:68401:18)↵ at
http://localhost:51840/client.e31bb0bc.js:68387:15"
3. __proto__: Error
Using smart_ptr_constructor changes the value in isSmartPointer, but the
same binding error is returned.
struct RenderModel {};
EMSCRIPTEN_BINDINGS(RenderModelBinding) {
emscripten::class_<RenderModel>("RenderModel")
.smart_ptr_constructor<std::shared_ptr<RenderModel>>("RenderModel",
&std::make_shared<RenderModel>);
}
struct GroupRenderModel : public RenderModel {
void addModel(std::shared_ptr<wre_model::RenderModel> model) {
models.push_back(model);
}
std::vector<std::shared_ptr<wre_model::RenderModel>> models;
};
EMSCRIPTEN_BINDINGS(GroupRenderModelBinding) {
emscripten::class_<GroupRenderModel>("GroupRenderModel")
.function("addModel", &GroupRenderModel::addModel)
.smart_ptr_constructor<std::shared_ptr<GroupRenderModel>>(
"GroupRenderModel", &std::make_shared<GroupRenderModel>);
}
struct TextureRenderModel : public RenderModel {};
EMSCRIPTEN_BINDINGS(TextureRenderModelBinding) {
emscripten::class_<TextureRenderModel>("TextureRenderModel")
.smart_ptr_constructor<std::shared_ptr<TextureRenderModel>>(
"TextureRenderModel", &std::make_shared<TextureRenderModel>);
}
Am I making some mistake with my binding declaration, or does embind not
support implicit casting?
1.
--
You received this message because you are subscribed to the Google Groups
"emscripten-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/emscripten-discuss/849ef787-772d-4a66-ac98-1d9d3e948b36%40googlegroups.com.