forked from nodejs/node-addon-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmyobject.cc
More file actions
137 lines (110 loc) · 4.04 KB
/
myobject.cc
File metadata and controls
137 lines (110 loc) · 4.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#include "myobject.h"
#include <assert.h>
MyObject::MyObject() : env_(nullptr), wrapper_(nullptr) {}
MyObject::~MyObject() {
napi_delete_reference(env_, wrapper_);
}
void MyObject::Destructor(napi_env env,
void* nativeObject,
void* /*finalize_hint*/) {
delete reinterpret_cast<MyObject*>(nativeObject);
}
#define DECLARE_NAPI_METHOD(name, func) \
{ name, 0, func, 0, 0, 0, napi_default, 0 }
napi_status MyObject::Init(napi_env env) {
napi_status status;
napi_property_descriptor properties[] = {
DECLARE_NAPI_METHOD("plusOne", PlusOne),
};
napi_value cons;
status = napi_define_class(
env, "MyObject", NAPI_AUTO_LENGTH, New, nullptr, 1, properties, &cons);
if (status != napi_ok) return status;
// We will need the constructor `cons` later during the life cycle of the
// addon, so we store a persistent reference to it as the instance data for
// our addon. This will enable us to use `napi_get_instance_data` at any
// point during the life cycle of our addon to retrieve it. We cannot simply
// store it as a global static variable, because that will render our addon
// unable to support Node.js worker threads and multiple contexts on a single
// thread.
//
// The finalizer we pass as a lambda will be called when our addon is unloaded
// and is responsible for releasing the persistent reference and freeing the
// heap memory where we stored the persistent reference.
napi_ref* constructor = new napi_ref;
status = napi_create_reference(env, cons, 1, constructor);
assert(status == napi_ok);
status = napi_set_instance_data(
env,
constructor,
[](napi_env env, void* data, void* hint) {
napi_ref* constructor = static_cast<napi_ref*>(data);
napi_status status = napi_delete_reference(env, *constructor);
assert(status == napi_ok);
delete constructor;
},
nullptr);
assert(status == napi_ok);
return napi_ok;
}
napi_value MyObject::New(napi_env env, napi_callback_info info) {
napi_status status;
size_t argc = 1;
napi_value args[1];
napi_value jsthis;
status = napi_get_cb_info(env, info, &argc, args, &jsthis, nullptr);
assert(status == napi_ok);
napi_valuetype valuetype;
status = napi_typeof(env, args[0], &valuetype);
assert(status == napi_ok);
MyObject* obj = new MyObject();
if (valuetype == napi_undefined) {
obj->counter_ = 0;
} else {
status = napi_get_value_double(env, args[0], &obj->counter_);
assert(status == napi_ok);
}
obj->env_ = env;
status = napi_wrap(env,
jsthis,
reinterpret_cast<void*>(obj),
MyObject::Destructor,
nullptr, /* finalize_hint */
&obj->wrapper_);
assert(status == napi_ok);
return jsthis;
}
napi_value MyObject::Constructor(napi_env env) {
void* instance_data = nullptr;
napi_status status = napi_get_instance_data(env, &instance_data);
assert(status == napi_ok);
napi_ref* constructor = static_cast<napi_ref*>(instance_data);
napi_value cons;
status = napi_get_reference_value(env, *constructor, &cons);
assert(status == napi_ok);
return cons;
}
napi_status MyObject::NewInstance(napi_env env,
napi_value arg,
napi_value* instance) {
napi_status status;
const int argc = 1;
napi_value argv[argc] = {arg};
status = napi_new_instance(env, Constructor(env), argc, argv, instance);
if (status != napi_ok) return status;
return napi_ok;
}
napi_value MyObject::PlusOne(napi_env env, napi_callback_info info) {
napi_status status;
napi_value jsthis;
status = napi_get_cb_info(env, info, nullptr, nullptr, &jsthis, nullptr);
assert(status == napi_ok);
MyObject* obj;
status = napi_unwrap(env, jsthis, reinterpret_cast<void**>(&obj));
assert(status == napi_ok);
obj->counter_ += 1;
napi_value num;
status = napi_create_double(env, obj->counter_, &num);
assert(status == napi_ok);
return num;
}