How to return a C++ Class to NODE.JS
I think, as described in Napi::Object docs, you cannot instantiate an object with a custom class. Only primitive values. Therefore I would suggest creating an empty Napi::Object
and using it's Set
to map the values.
Napi::Object ret = Napi::Object::New(env);
ret.Set("my_float", Napi::Number::New(env, (float)tmp.my_float()));
Fill all the fields and return the object. Just like you did with the exports
You can fit a custom class inside Napi::Object, it is convenient for destruction:
class MyObject : public Napi::ObjectWrap<MyObject> {
void * inner_obj_;
}
And use reinterpret_cast to call it:
reinterpret_cast<MyClass *>(inner_obj_)->my_float();