A reimplementation of node::ObjectWrap that adds some API not present in older versions of Node. Should be preferred over node::ObjectWrap in all cases for consistency.
Definition:
classObjectWrap {public:ObjectWrap();virtual~ObjectWrap();template <classT>staticinlineT*Unwrap(v8::Local<v8::Object> handle);inline v8::Local<v8::Object> handle();inline Nan::Persistent<v8::Object>&persistent();protected:inlinevoidWrap(v8::Local<v8::Object> handle);inlinevoidMakeWeak(); /* Ref() marks the object as being attached to an event loop. * Refed objects will not be garbage collected, even if * all references are lost. */virtualvoidRef(); /* Unref() marks an object as detached from the event loop. This is its * default state. When an object with a "weak" reference changes from * attached to detached state it will be freed. Be careful not to access * the object after making this call as it might be gone! * (A "weak reference" means an object that only has a * persistant handle.) * * DO NOT CALL THIS FROM DESTRUCTOR */virtualvoidUnref();int refs_; // ro};
When calling Unwrap, it is important that the argument is indeed some JavaScript object which got wrapped by a Wrap call for this class or any derived class. The Signature installed by Nan::SetPrototypeMethod() does ensure that info.Holder() is just such an instance. In Node 0.12 and later, info.This() will also be of such a type, since otherwise the invocation will get rejected. However, in Node 0.10 and before it was possible to invoke a method on a JavaScript object which just had the extension type in its prototype chain. In such a situation, calling Unwrap on info.This() will likely lead to a failed assertion causing a crash, but could lead to even more serious corruption.
On the other hand, calling Unwrap in an accessor should not use Holder() if the accessor is defined on the prototype. So either define your accessors on the instance template, or use This() after verifying that it is indeed a valid object.
var wrappedobjectfactory =require('bindings')('wrappedobjectfactory');var obj =wrappedobjectfactory.newFactoryObjectInstance(10);console.log('Should be 10: '+obj.getValue());
Passing wrapped objects around
Use the MyFactoryObject class above along with the following: