-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathnode_api_helpers.h
466 lines (396 loc) · 12.5 KB
/
node_api_helpers.h
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
#ifndef SRC_NODE_API_HELPERS_H_
#define SRC_NODE_API_HELPERS_H_
////////////////////////////////////////////////////////////////////////////////
// Nan-like Helpers
////////////////////////////////////////////////////////////////////////////////
// Must not have symbol exports to achieve clean C opaque API/ABI
//
// TODO(ianhall): This should all move into its own header file and perhaps be
// optional since it contains a lot of inline code and brings in a lot of header
// dependencies.
////////////////////////////////////////////////////////////////////////////////
#include "node_jsvmapi.h"
#include "node_asyncapi.h"
#include <limits.h>
#include <string.h>
#include <assert.h>
#define NAPI_METHOD(name) \
void name(napi_env env, napi_callback_info info)
#define NAPI_GETTER(name) NAPI_METHOD(name)
#define NAPI_SETTER(name) NAPI_METHOD(name)
#define NAPI_MODULE_INIT(name) \
void name(napi_env env, napi_value exports, napi_value module)
// This is taken from NAN and is the C++11 version.
// TODO(ianhall): Support pre-C++11 compilation?
#define NAPI_DISALLOW_ASSIGN(CLASS) void operator=(const CLASS&) = delete;
#define NAPI_DISALLOW_COPY(CLASS) CLASS(const CLASS&) = delete;
#define NAPI_DISALLOW_MOVE(CLASS) \
CLASS(CLASS&) = delete; \
void operator=(CLASS&) = delete;
#define NAPI_DISALLOW_ASSIGN_COPY_MOVE(CLASS) \
NAPI_DISALLOW_ASSIGN(CLASS) \
NAPI_DISALLOW_COPY(CLASS) \
NAPI_DISALLOW_MOVE(CLASS)
namespace Napi {
// RAII HandleScope helpers
// Mirror Nan versions for easy conversion
// Ensure scopes are closed in the correct order on the stack
class HandleScope {
public:
HandleScope() {
napi_get_current_env(&env);
napi_open_handle_scope(env, &scope);
}
explicit HandleScope(napi_env e) : env(e) {
napi_open_handle_scope(env, &scope);
}
~HandleScope() {
napi_close_handle_scope(env, scope);
}
private:
napi_env env;
napi_handle_scope scope;
};
class EscapableHandleScope {
public:
EscapableHandleScope() {
napi_get_current_env(&env);
napi_open_escapable_handle_scope(env, &scope);
}
explicit EscapableHandleScope(napi_env e) : env(e) {
napi_open_escapable_handle_scope(e, &scope);
}
~EscapableHandleScope() {
napi_close_escapable_handle_scope(env, scope);
}
napi_value Escape(napi_value escapee) {
napi_value result;
napi_escape_handle(env, scope, escapee, &result);
return result;
}
private:
napi_env env;
napi_escapable_handle_scope scope;
};
class Utf8String {
public:
inline explicit Utf8String(napi_value from) :
length_(0), str_(str_st_) {
if (from != nullptr) {
napi_env env;
napi_get_current_env(&env);
napi_value string = nullptr;
napi_coerce_to_string(env, from, &string);
if (string != nullptr) {
napi_get_value_string_utf8_length(env, string, &length_);
int bufsize = length_ + 1;
if (static_cast<size_t>(bufsize) > sizeof(str_st_)) {
str_ = new char[bufsize];
assert(str_ != 0);
}
napi_get_value_string_utf8(env, string, str_, bufsize, nullptr);
}
}
}
inline size_t length() const {
return length_;
}
inline char* operator*() { return str_; }
inline const char* operator*() const { return str_; }
inline ~Utf8String() {
if (str_ != str_st_) {
delete [] str_;
}
}
private:
NAPI_DISALLOW_ASSIGN_COPY_MOVE(Utf8String)
int length_;
char *str_;
char str_st_[1024];
};
// TODO(ianhall): This class uses napi_get_current_env() extensively
// does that make sense? will this behave correctly when passed around
// workers? Is there a good reason to have napi_env?
// Perhaps the C++ layer shields the user from napi_env and behaves
// like Nan, always using the current one?
class Callback {
public:
Callback() {
napi_env env;
napi_get_current_env(&env);
HandleScope scope(env);
napi_value obj;
napi_create_object(env, &obj);
napi_create_reference(env, obj, 1, &handle);
}
explicit Callback(napi_value fn) {
napi_env env;
napi_get_current_env(&env);
HandleScope scope(env);
napi_value obj;
napi_create_object(env, &obj);
napi_create_reference(env, obj, 1, &handle);
SetFunction(fn);
}
~Callback() {
if (handle == NULL) {
return;
}
napi_env env;
napi_get_current_env(&env);
napi_delete_reference(env, handle);
}
bool operator==(const Callback &other) const {
HandleScope scope;
napi_env env;
napi_get_current_env(&env);
napi_value ha;
napi_get_reference_value(env, handle, &ha);
napi_value hb;
napi_get_reference_value(env, other.handle, &hb);
napi_value a;
napi_get_element(env, ha, kCallbackIndex, &a);
napi_value b;
napi_get_element(env, hb, kCallbackIndex, &b);
bool result;
napi_strict_equals(env, a, b, &result);
return result;
}
bool operator!=(const Callback &other) const {
return !this->operator==(other);
}
inline
napi_value operator*() const { return this->GetFunction(); }
inline napi_value operator()(
napi_value target,
int argc = 0,
napi_value argv[] = 0) const {
return this->Call(target, argc, argv);
}
inline napi_value operator()(
int argc = 0,
napi_value argv[] = 0) const {
return this->Call(argc, argv);
}
inline void SetFunction(napi_value fn) {
HandleScope scope;
napi_env env;
napi_get_current_env(&env);
napi_value h;
napi_get_reference_value(env, handle, &h);
napi_set_element(env, h, kCallbackIndex, fn);
}
inline napi_value GetFunction() const {
EscapableHandleScope scope;
napi_env env;
napi_get_current_env(&env);
napi_value h;
napi_get_reference_value(env, handle, &h);
napi_value fn;
napi_get_element(env, h, kCallbackIndex, &fn);
return scope.Escape(fn);
}
inline bool IsEmpty() const {
HandleScope scope;
napi_env env;
napi_get_current_env(&env);
napi_value h;
napi_get_reference_value(env, handle, &h);
napi_value fn;
napi_get_element(env, h, kCallbackIndex, &fn);
napi_valuetype valuetype;
napi_get_type_of_value(env, fn, &valuetype);
return napi_undefined == valuetype;
}
inline napi_value
Call(napi_value target,
int argc,
napi_value argv[]) const {
return Call_(target, argc, argv);
}
inline napi_value
Call(int argc, napi_value argv[]) const {
napi_env env;
napi_get_current_env(&env);
napi_value global;
napi_get_global(env, &global);
return Call_(global, argc, argv);
}
private:
NAPI_DISALLOW_ASSIGN_COPY_MOVE(Callback)
napi_ref handle;
static const uint32_t kCallbackIndex = 0;
napi_value Call_(napi_value target,
int argc,
napi_value argv[]) const {
EscapableHandleScope scope;
napi_env env;
napi_get_current_env(&env);
napi_value h;
napi_get_reference_value(env, handle, &h);
napi_value fn;
napi_get_element(env, h, kCallbackIndex, &fn);
napi_value cb;
napi_make_callback(
env,
target,
fn,
argc,
argv,
&cb);
return scope.Escape(cb);
}
};
// TODO(ianhall): This class uses napi_get_current_env() extensively
// See comment above on class Callback
/* abstract */ class AsyncWorker {
public:
explicit AsyncWorker(Callback *callback_)
: callback(callback_), errmsg_(NULL) {
request = napi_create_async_work();
napi_env env;
napi_get_current_env(&env);
HandleScope scope;
napi_value obj;
napi_create_object(env, &obj);
napi_create_reference(env, obj, 1, &persistentHandle);
}
virtual ~AsyncWorker() {
HandleScope scope;
if (persistentHandle != NULL) {
napi_env env;
napi_get_current_env(&env);
napi_delete_reference(env, persistentHandle);
persistentHandle = NULL;
}
delete callback;
delete[] errmsg_;
napi_delete_async_work(request);
}
virtual void WorkComplete() {
HandleScope scope;
if (errmsg_ == NULL)
HandleOKCallback();
else
HandleErrorCallback();
delete callback;
callback = NULL;
}
inline void SaveToPersistent(
const char *key, napi_value value) {
HandleScope scope;
napi_env env;
napi_get_current_env(&env);
napi_propertyname pnKey;
napi_property_name(env, key, &pnKey);
napi_value h;
napi_get_reference_value(env, persistentHandle, &h);
napi_set_property(env, h, pnKey, value);
}
inline void SaveToPersistent(
napi_propertyname key, napi_value value) {
HandleScope scope;
napi_env env;
napi_get_current_env(&env);
napi_value h;
napi_get_reference_value(env, persistentHandle, &h);
napi_set_property(env, h, key, value);
}
inline void SaveToPersistent(
uint32_t index, napi_value value) {
HandleScope scope;
napi_env env;
napi_get_current_env(&env);
napi_value h;
napi_get_reference_value(env, persistentHandle, &h);
napi_set_element(env, h, index, value);
}
inline napi_value GetFromPersistent(const char *key) const {
EscapableHandleScope scope;
napi_env env;
napi_get_current_env(&env);
napi_propertyname pnKey;
napi_property_name(env, key, &pnKey);
napi_value h;
napi_get_reference_value(env, persistentHandle, &h);
napi_value v;
napi_get_property(env, h, pnKey, &v);
return scope.Escape(v);
}
inline napi_value
GetFromPersistent(napi_propertyname key) const {
EscapableHandleScope scope;
napi_env env;
napi_get_current_env(&env);
napi_value h;
napi_get_reference_value(env, persistentHandle, &h);
napi_value v;
napi_get_property(env, h, key, &v);
return scope.Escape(v);
}
inline napi_value GetFromPersistent(uint32_t index) const {
EscapableHandleScope scope;
napi_env env;
napi_get_current_env(&env);
napi_value h;
napi_get_reference_value(env, persistentHandle, &h);
napi_value v;
napi_get_element(env, h, index, &v);
return scope.Escape(v);
}
virtual void Execute() = 0;
napi_work request;
virtual void Destroy() {
delete this;
}
static void CallExecute(void* this_pointer){
AsyncWorker* self = static_cast<AsyncWorker*>(this_pointer);
self->Execute();
}
static void CallWorkComplete(void* this_pointer) {
AsyncWorker* self = static_cast<AsyncWorker*>(this_pointer);
self->WorkComplete();
}
static void CallDestroy(void* this_pointer) {
AsyncWorker* self = static_cast<AsyncWorker*>(this_pointer);
self->Destroy();
}
protected:
napi_ref persistentHandle;
Callback *callback;
virtual void HandleOKCallback() {
callback->Call(0, NULL);
}
virtual void HandleErrorCallback() {
HandleScope scope;
napi_env env;
napi_get_current_env(&env);
napi_value s;
napi_create_string_utf8(env, ErrorMessage(), -1, &s);
napi_value argv[1];
napi_create_error(env, s, argv);
callback->Call(1, argv);
}
void SetErrorMessage(const char *msg) {
delete[] errmsg_;
size_t size = strlen(msg) + 1;
errmsg_ = new char[size];
memcpy(errmsg_, msg, size);
}
const char* ErrorMessage() const {
return errmsg_;
}
private:
NAPI_DISALLOW_ASSIGN_COPY_MOVE(AsyncWorker)
char *errmsg_;
};
inline void AsyncQueueWorker(AsyncWorker* worker) {
napi_work req = worker->request;
napi_async_set_data(req, static_cast<void*>(worker));
napi_async_set_execute(req, &AsyncWorker::CallExecute);
napi_async_set_complete(req, &AsyncWorker::CallWorkComplete);
napi_async_set_destroy(req, &AsyncWorker::CallDestroy);
napi_async_queue_worker(req);
}
} // namespace Napi
#endif // SRC_NODE_API_HELPERS_H_