11#include " myobject.h"
22
3- node::js::persistent constructor;
3+ node::js::persistent MyObject:: constructor;
44
55MyObject::MyObject (double value) : value_(value) {
66}
77
88MyObject::~MyObject () {
99}
1010
11+ void MyObject::Destructor (void * nativeObject) {
12+ ((MyObject*) nativeObject)->~MyObject ();
13+ }
14+
1115void MyObject::Init (node::js::value env, node::js::value exports) {
1216 node::js::value function = node::js::CreateConstructorForWrap (env, New);
1317 node::js::SetFunctionName (env, function, node::js::CreateString (env, " MyObject" ));
@@ -31,24 +35,8 @@ void MyObject::Init(node::js::value env, node::js::value exports) {
3135}
3236
3337void MyObject::New (node::js::value env, node::js::FunctionCallbackInfo info) {
34- /*
35- if (info.IsConstructCall()) {
36- // Invoked as constructor: `new MyObject(...)`
37- double value = info[0]->IsUndefined() ? 0 : info[0]->NumberValue();
38- MyObject* obj = new MyObject(value);
39- obj->Wrap(info.This());
40- info.GetReturnValue().Set(info.This());
41- } else {
42- // Invoked as plain function `MyObject(...)`, turn into construct call.
43- const int argc = 1;
44- v8::Local<v8::Value> argv[argc] = { info[0] };
45- v8::Local<v8::Function> cons = Nan::New<v8::Function>(constructor);
46- info.GetReturnValue().Set(cons->NewInstance(argc, argv));
47- }
48- */
49-
50- // assumes its a constructor call
5138 if (node::js::IsContructCall (env, info)) {
39+ // Invoked as constructor: `new MyObject(...)`
5240 node::js::value args[1 ];
5341 node::js::GetCallbackArgs (info, args, 1 );
5442 double value = 0 ;
@@ -57,9 +45,10 @@ void MyObject::New(node::js::value env, node::js::FunctionCallbackInfo info) {
5745 }
5846 MyObject* obj = new MyObject (value);
5947 node::js::value jsobj = node::js::GetCallbackObject (env, info);
60- node::js::Wrap (env, jsobj, (void *) obj, nullptr );
48+ node::js::Wrap (env, jsobj, (void *) obj, MyObject::Destructor );
6149 node::js::SetReturnValue (env, info, jsobj);
6250 } else {
51+ // Invoked as plain function `MyObject(...)`, turn into construct call.
6352 node::js::value args[1 ];
6453 node::js::GetCallbackArgs (info, args, 1 );
6554 const int argc = 1 ;
0 commit comments