Skip to content

Commit 67e70fa

Browse files
committed
Cleanup and add destructor handling
- misc cleanup - add passing in destructor so that C++ objects will be cleaned up properly when the js objects are collected
1 parent c84a42d commit 67e70fa

File tree

2 files changed

+10
-19
lines changed

2 files changed

+10
-19
lines changed

6_object_wrap/abi/myobject.cc

Lines changed: 8 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
#include "myobject.h"
22

3-
node::js::persistent constructor;
3+
node::js::persistent MyObject::constructor;
44

55
MyObject::MyObject(double value) : value_(value) {
66
}
77

88
MyObject::~MyObject() {
99
}
1010

11+
void MyObject::Destructor(void* nativeObject) {
12+
((MyObject*) nativeObject)->~MyObject();
13+
}
14+
1115
void 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

3337
void 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;

6_object_wrap/abi/myobject.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
class MyObject {
77
public:
88
static void Init(node::js::value env, node::js::value exports);
9+
static void Destructor(void* jsObject);
910

1011
private:
1112
explicit MyObject(double value = 0);
@@ -15,6 +16,7 @@ class MyObject {
1516
static void GetValue(node::js::value env, node::js::FunctionCallbackInfo info);
1617
static void PlusOne(node::js::value env, node::js::FunctionCallbackInfo info);
1718
static void Multiply(node::js::value env, node::js::FunctionCallbackInfo info);
19+
static node::js::persistent constructor;
1820
double value_;
1921
};
2022

0 commit comments

Comments
 (0)