Composition and Inheritance of Wrapped Objects Examples
Can examples of composition and inheritance of wrapped objects be added? I think one could potentially figure it out with the current examples, but I'm not able to figure it out.
class BasicClass { int IntValue; };
class BasicComposition
{
int foo;
BasicClass bc;
};
class BasicInheritance : BasicClass
{
double yValue;
};
class InheritanceAndComposition : BasicInheritance
{
std::string strValue;
BasicComposition bcValue;
};
// Export all classes to JS
One should be able to instantiate and use each C++ class in JavaScript (e.g. create an instance of BasicClass independent of instances of BasicComposition).
It seems to me that only the case of BasicClass has an example in 6_object_wrap. The other cases with inheritance and composition of classes extending ObjectWrap<T> do not appear to exist. Inheritance might be covered in inherits_from_event_emitter, but it is not clear to me what is going on in that example.
I'm hoping someone with more experience with node-addon-api can provide these types of examples. It would greatly assist me in defining more complex relationships in Napi.
@AeromXundes inheritance is an unaddressed problem in the V8 implementation of N-API. That is, V8 exposes the ability to properly inherit from a JavaScript class only in a V8-specific fashion (by maintaining access to the v8::FunctionTemplate), which is something we cannot use in N-API. Thus, we cannot easily provide an API for creating JavaScript class hierarchies which correspond to C++ class hierarchies.
The only workaround is to structure the C++ portion of the code as a flat list of functions or as distinct classes by deriving from Napi::ObjectWrap, and to establish the JavaScript class hierarchy in pure JavaScript (https://github.com/nodejs/node-addon-api/issues/229#issuecomment-423009095).
@gabrielschulhof Thanks for the quick response. Any thoughts on composition? Many class hierarchies can be reworked to be composition instead.