问题
I have two subclasses of Nan::ObjectWrap
class Zyre: public Nan::ObjectWrap {...}
class ZyreEvent: public Nan::ObjectWrap {...}
How can I return a ZyreEvent
javascript object from a method in Zyre
?
I have the following method, in which I create a ZyreEvent
:
NAN_METHOD (Zyre::_recv) {
Zyre *node = Nan::ObjectWrap::Unwrap <Zyre> (info.Holder ());
ZyreEvent *zyre_event = new ZyreEvent (node->self);
info.GetReturnValue().Set(zyre_event->Wrap(info.This()));
}
But I can't Wrap the zyre_event because Wrap
is a protected member.
回答1:
If I understand correctly, you want to return from (subclass of) Nan::ObjectWrap
's method instance of another (subclass of) Nan::ObjectWrap
.
Note: I'm not experienced so this may have faults or be wrong. I've put my sources in brackets where are examples how it's is done I guess.
- Create
static NewInstance
method in a first class which receives pointer of itself (NewInstance) - Use
v8::External
to wrap first class' C++ object and pass it as an argument forNew
withargc
andargv
to first class' constructor (using v8::External) (v8::External doc) - Edit first class'
New
method and handleinfo.Length() == 1 && info[0]->IsExternal()
case which is basically copy constructor in this case (copying passed pointer) - Use
...::NewInstance()
in second class to set return value
来源:https://stackoverflow.com/questions/38794789/return-a-nanobjectwrap-from-another-nanobjectwrap