Skip to content

Composition and Inheritance of Wrapped Objects Examples #72

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
AeromXundes opened this issue Nov 20, 2018 · 2 comments
Open

Composition and Inheritance of Wrapped Objects Examples #72

AeromXundes opened this issue Nov 20, 2018 · 2 comments

Comments

@AeromXundes
Copy link

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.

@gabrielschulhof
Copy link
Contributor

gabrielschulhof commented Nov 20, 2018

@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 (nodejs/node-addon-api#229 (comment)).

@AeromXundes
Copy link
Author

@gabrielschulhof Thanks for the quick response. Any thoughts on composition? Many class hierarchies can be reworked to be composition instead.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants