forked from nodejs/node-addon-examples
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmyobject.cc
More file actions
198 lines (151 loc) · 5.25 KB
/
myobject.cc
File metadata and controls
198 lines (151 loc) · 5.25 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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
#include "myobject.h"
#include <assert.h>
napi_ref MyObject::constructor;
MyObject::MyObject(double value)
: value_(value), env_(nullptr), wrapper_(nullptr) {}
MyObject::~MyObject() {
napi_delete_reference(env_, wrapper_);
}
void MyObject::Destructor(napi_env env,
void* nativeObject,
void* /*finalize_hint*/) {
reinterpret_cast<MyObject*>(nativeObject)->~MyObject();
}
#define DECLARE_NAPI_METHOD(name, func) \
{ name, 0, func, 0, 0, 0, napi_default, 0 }
napi_value MyObject::Init(napi_env env, napi_value exports) {
napi_status status;
napi_property_descriptor properties[] = {
{"value", 0, 0, GetValue, SetValue, 0, napi_default, 0},
DECLARE_NAPI_METHOD("plusOne", PlusOne),
DECLARE_NAPI_METHOD("multiply", Multiply),
};
napi_value cons;
status = napi_define_class(
env, "MyObject", NAPI_AUTO_LENGTH, New, nullptr, 3, properties, &cons);
assert(status == napi_ok);
status = napi_create_reference(env, cons, 1, &constructor);
assert(status == napi_ok);
status = napi_set_named_property(env, exports, "MyObject", cons);
assert(status == napi_ok);
return exports;
}
napi_value MyObject::New(napi_env env, napi_callback_info info) {
napi_status status;
napi_value target;
status = napi_get_new_target(env, info, &target);
assert(status == napi_ok);
bool is_constructor = target != nullptr;
if (is_constructor) {
// Invoked as constructor: `new MyObject(...)`
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);
double value = 0;
napi_valuetype valuetype;
status = napi_typeof(env, args[0], &valuetype);
assert(status == napi_ok);
if (valuetype != napi_undefined) {
status = napi_get_value_double(env, args[0], &value);
assert(status == napi_ok);
}
MyObject* obj = new MyObject(value);
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;
} else {
// Invoked as plain function `MyObject(...)`, turn into construct call.
size_t argc_ = 1;
napi_value args[1];
status = napi_get_cb_info(env, info, &argc_, args, nullptr, nullptr);
assert(status == napi_ok);
const size_t argc = 1;
napi_value argv[argc] = {args[0]};
napi_value cons;
status = napi_get_reference_value(env, constructor, &cons);
assert(status == napi_ok);
napi_value instance;
status = napi_new_instance(env, cons, argc, argv, &instance);
assert(status == napi_ok);
return instance;
}
}
napi_value MyObject::GetValue(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);
napi_value num;
status = napi_create_double(env, obj->value_, &num);
assert(status == napi_ok);
return num;
}
napi_value MyObject::SetValue(napi_env env, napi_callback_info info) {
napi_status status;
size_t argc = 1;
napi_value value;
napi_value jsthis;
status = napi_get_cb_info(env, info, &argc, &value, &jsthis, nullptr);
assert(status == napi_ok);
MyObject* obj;
status = napi_unwrap(env, jsthis, reinterpret_cast<void**>(&obj));
assert(status == napi_ok);
status = napi_get_value_double(env, value, &obj->value_);
assert(status == napi_ok);
return nullptr;
}
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->value_ += 1;
napi_value num;
status = napi_create_double(env, obj->value_, &num);
assert(status == napi_ok);
return num;
}
napi_value MyObject::Multiply(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);
double multiple = 1;
if (valuetype != napi_undefined) {
status = napi_get_value_double(env, args[0], &multiple);
assert(status == napi_ok);
}
MyObject* obj;
status = napi_unwrap(env, jsthis, reinterpret_cast<void**>(&obj));
assert(status == napi_ok);
napi_value cons;
status = napi_get_reference_value(env, constructor, &cons);
assert(status == napi_ok);
const int kArgCount = 1;
napi_value argv[kArgCount];
status = napi_create_double(env, obj->value_ * multiple, argv);
assert(status == napi_ok);
napi_value instance;
status = napi_new_instance(env, cons, kArgCount, argv, &instance);
assert(status == napi_ok);
return instance;
}