Return Nan :: ObjectWrap from another Nan :: ObjectWrap

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 zyre_event because Wrap is a protected member.

+3
c ++ node.js-addon node.js-nan
source share
1 answer

If I understand correctly, you want to return from the method instance (subclass) of Nan::ObjectWrap another (subclass) of Nan::ObjectWrap .

Note. I do not experience, so this may have flaws or be wrong. I put my sources in brackets for examples of how this is done. I think.

  • Create a static NewInstance method in the first class that gets a pointer to itself ( NewInstance )
  • Use v8::External to wrap the first class C ++ object and pass it as an argument to New with argc and argv to the first class constructor ( using v8 :: External ) ( v8 :: External document )
  • Change the method of the first class New and handle info.Length() == 1 && info[0]->IsExternal() case, which is basically the constructor of the copy in this case ( copying the passed pointer )
  • Use ...::NewInstance() in the second class to set the return value
+1
source share

All Articles