node-addon-api 4.3.0 → 5.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +38 -14
- package/index.js +3 -3
- package/napi-inl.deprecated.h +121 -127
- package/napi-inl.h +1277 -1183
- package/napi.h +2799 -2668
- package/package.json +63 -6
- package/tools/check-napi.js +13 -14
- package/tools/clang-format.js +4 -1
- package/tools/conversion.js +161 -169
- package/tools/eslint-format.js +9 -1
package/napi-inl.h
CHANGED
|
@@ -18,6 +18,10 @@
|
|
|
18
18
|
|
|
19
19
|
namespace Napi {
|
|
20
20
|
|
|
21
|
+
#ifdef NAPI_CPP_CUSTOM_NAMESPACE
|
|
22
|
+
namespace NAPI_CPP_CUSTOM_NAMESPACE {
|
|
23
|
+
#endif
|
|
24
|
+
|
|
21
25
|
// Helpers to handle functions exposed from C++.
|
|
22
26
|
namespace details {
|
|
23
27
|
|
|
@@ -26,11 +30,11 @@ namespace details {
|
|
|
26
30
|
// TODO: Replace this code with `napi_add_finalizer()` whenever it becomes
|
|
27
31
|
// available on all supported versions of Node.js.
|
|
28
32
|
template <typename FreeType>
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
33
|
+
inline napi_status AttachData(napi_env env,
|
|
34
|
+
napi_value obj,
|
|
35
|
+
FreeType* data,
|
|
36
|
+
napi_finalize finalizer = nullptr,
|
|
37
|
+
void* hint = nullptr) {
|
|
34
38
|
napi_status status;
|
|
35
39
|
if (finalizer == nullptr) {
|
|
36
40
|
finalizer = [](napi_env /*env*/, void* data, void* /*hint*/) {
|
|
@@ -41,22 +45,16 @@ static inline napi_status AttachData(napi_env env,
|
|
|
41
45
|
napi_value symbol, external;
|
|
42
46
|
status = napi_create_symbol(env, nullptr, &symbol);
|
|
43
47
|
if (status == napi_ok) {
|
|
44
|
-
status = napi_create_external(env,
|
|
45
|
-
data,
|
|
46
|
-
finalizer,
|
|
47
|
-
hint,
|
|
48
|
-
&external);
|
|
48
|
+
status = napi_create_external(env, data, finalizer, hint, &external);
|
|
49
49
|
if (status == napi_ok) {
|
|
50
|
-
napi_property_descriptor desc = {
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
nullptr
|
|
59
|
-
};
|
|
50
|
+
napi_property_descriptor desc = {nullptr,
|
|
51
|
+
symbol,
|
|
52
|
+
nullptr,
|
|
53
|
+
nullptr,
|
|
54
|
+
nullptr,
|
|
55
|
+
external,
|
|
56
|
+
napi_default,
|
|
57
|
+
nullptr};
|
|
60
58
|
status = napi_define_properties(env, obj, 1, &desc);
|
|
61
59
|
}
|
|
62
60
|
}
|
|
@@ -77,16 +75,16 @@ inline napi_value WrapCallback(Callable callback) {
|
|
|
77
75
|
e.ThrowAsJavaScriptException();
|
|
78
76
|
return nullptr;
|
|
79
77
|
}
|
|
80
|
-
#else
|
|
78
|
+
#else // NAPI_CPP_EXCEPTIONS
|
|
81
79
|
// When C++ exceptions are disabled, errors are immediately thrown as JS
|
|
82
80
|
// exceptions, so there is no need to catch and rethrow them here.
|
|
83
81
|
return callback();
|
|
84
|
-
#endif
|
|
82
|
+
#endif // NAPI_CPP_EXCEPTIONS
|
|
85
83
|
}
|
|
86
84
|
|
|
87
85
|
// For use in JS to C++ void callback wrappers to catch any Napi::Error
|
|
88
|
-
// exceptions and rethrow them as JavaScript exceptions before returning from
|
|
89
|
-
// callback.
|
|
86
|
+
// exceptions and rethrow them as JavaScript exceptions before returning from
|
|
87
|
+
// the callback.
|
|
90
88
|
template <typename Callable>
|
|
91
89
|
inline void WrapVoidCallback(Callable callback) {
|
|
92
90
|
#ifdef NAPI_CPP_EXCEPTIONS
|
|
@@ -95,21 +93,20 @@ inline void WrapVoidCallback(Callable callback) {
|
|
|
95
93
|
} catch (const Error& e) {
|
|
96
94
|
e.ThrowAsJavaScriptException();
|
|
97
95
|
}
|
|
98
|
-
#else
|
|
96
|
+
#else // NAPI_CPP_EXCEPTIONS
|
|
99
97
|
// When C++ exceptions are disabled, errors are immediately thrown as JS
|
|
100
98
|
// exceptions, so there is no need to catch and rethrow them here.
|
|
101
99
|
callback();
|
|
102
|
-
#endif
|
|
100
|
+
#endif // NAPI_CPP_EXCEPTIONS
|
|
103
101
|
}
|
|
104
102
|
|
|
105
103
|
template <typename Callable, typename Return>
|
|
106
104
|
struct CallbackData {
|
|
107
|
-
static inline
|
|
108
|
-
napi_value Wrapper(napi_env env, napi_callback_info info) {
|
|
105
|
+
static inline napi_value Wrapper(napi_env env, napi_callback_info info) {
|
|
109
106
|
return details::WrapCallback([&] {
|
|
110
107
|
CallbackInfo callbackInfo(env, info);
|
|
111
108
|
CallbackData* callbackData =
|
|
112
|
-
|
|
109
|
+
static_cast<CallbackData*>(callbackInfo.Data());
|
|
113
110
|
callbackInfo.SetData(callbackData->data);
|
|
114
111
|
return callbackData->callback(callbackInfo);
|
|
115
112
|
});
|
|
@@ -121,12 +118,11 @@ struct CallbackData {
|
|
|
121
118
|
|
|
122
119
|
template <typename Callable>
|
|
123
120
|
struct CallbackData<Callable, void> {
|
|
124
|
-
static inline
|
|
125
|
-
napi_value Wrapper(napi_env env, napi_callback_info info) {
|
|
121
|
+
static inline napi_value Wrapper(napi_env env, napi_callback_info info) {
|
|
126
122
|
return details::WrapCallback([&] {
|
|
127
123
|
CallbackInfo callbackInfo(env, info);
|
|
128
124
|
CallbackData* callbackData =
|
|
129
|
-
|
|
125
|
+
static_cast<CallbackData*>(callbackInfo.Data());
|
|
130
126
|
callbackInfo.SetData(callbackData->data);
|
|
131
127
|
callbackData->callback(callbackInfo);
|
|
132
128
|
return nullptr;
|
|
@@ -138,8 +134,8 @@ struct CallbackData<Callable, void> {
|
|
|
138
134
|
};
|
|
139
135
|
|
|
140
136
|
template <void (*Callback)(const CallbackInfo& info)>
|
|
141
|
-
|
|
142
|
-
|
|
137
|
+
napi_value TemplatedVoidCallback(napi_env env,
|
|
138
|
+
napi_callback_info info) NAPI_NOEXCEPT {
|
|
143
139
|
return details::WrapCallback([&] {
|
|
144
140
|
CallbackInfo cbInfo(env, info);
|
|
145
141
|
Callback(cbInfo);
|
|
@@ -148,8 +144,8 @@ TemplatedVoidCallback(napi_env env, napi_callback_info info) NAPI_NOEXCEPT {
|
|
|
148
144
|
}
|
|
149
145
|
|
|
150
146
|
template <Napi::Value (*Callback)(const CallbackInfo& info)>
|
|
151
|
-
|
|
152
|
-
|
|
147
|
+
napi_value TemplatedCallback(napi_env env,
|
|
148
|
+
napi_callback_info info) NAPI_NOEXCEPT {
|
|
153
149
|
return details::WrapCallback([&] {
|
|
154
150
|
CallbackInfo cbInfo(env, info);
|
|
155
151
|
return Callback(cbInfo);
|
|
@@ -158,8 +154,8 @@ TemplatedCallback(napi_env env, napi_callback_info info) NAPI_NOEXCEPT {
|
|
|
158
154
|
|
|
159
155
|
template <typename T,
|
|
160
156
|
Napi::Value (T::*UnwrapCallback)(const CallbackInfo& info)>
|
|
161
|
-
|
|
162
|
-
|
|
157
|
+
napi_value TemplatedInstanceCallback(napi_env env,
|
|
158
|
+
napi_callback_info info) NAPI_NOEXCEPT {
|
|
163
159
|
return details::WrapCallback([&] {
|
|
164
160
|
CallbackInfo cbInfo(env, info);
|
|
165
161
|
T* instance = T::Unwrap(cbInfo.This().As<Object>());
|
|
@@ -168,9 +164,8 @@ TemplatedInstanceCallback(napi_env env, napi_callback_info info) NAPI_NOEXCEPT {
|
|
|
168
164
|
}
|
|
169
165
|
|
|
170
166
|
template <typename T, void (T::*UnwrapCallback)(const CallbackInfo& info)>
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
napi_callback_info info) NAPI_NOEXCEPT {
|
|
167
|
+
napi_value TemplatedInstanceVoidCallback(napi_env env, napi_callback_info info)
|
|
168
|
+
NAPI_NOEXCEPT {
|
|
174
169
|
return details::WrapCallback([&] {
|
|
175
170
|
CallbackInfo cbInfo(env, info);
|
|
176
171
|
T* instance = T::Unwrap(cbInfo.This().As<Object>());
|
|
@@ -196,7 +191,8 @@ struct FinalizeData {
|
|
|
196
191
|
void* finalizeHint) NAPI_NOEXCEPT {
|
|
197
192
|
WrapVoidCallback([&] {
|
|
198
193
|
FinalizeData* finalizeData = static_cast<FinalizeData*>(finalizeHint);
|
|
199
|
-
finalizeData->callback(
|
|
194
|
+
finalizeData->callback(
|
|
195
|
+
Env(env), static_cast<T*>(data), finalizeData->hint);
|
|
200
196
|
delete finalizeData;
|
|
201
197
|
});
|
|
202
198
|
}
|
|
@@ -206,14 +202,14 @@ struct FinalizeData {
|
|
|
206
202
|
};
|
|
207
203
|
|
|
208
204
|
#if (NAPI_VERSION > 3 && !defined(__wasm32__))
|
|
209
|
-
template <typename ContextType=void,
|
|
210
|
-
typename Finalizer=std::function<void(Env, void*, ContextType*)>,
|
|
211
|
-
typename FinalizerDataType=void>
|
|
205
|
+
template <typename ContextType = void,
|
|
206
|
+
typename Finalizer = std::function<void(Env, void*, ContextType*)>,
|
|
207
|
+
typename FinalizerDataType = void>
|
|
212
208
|
struct ThreadSafeFinalize {
|
|
213
|
-
static inline
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
209
|
+
static inline void Wrapper(napi_env env,
|
|
210
|
+
void* rawFinalizeData,
|
|
211
|
+
void* /* rawContext */) {
|
|
212
|
+
if (rawFinalizeData == nullptr) return;
|
|
217
213
|
|
|
218
214
|
ThreadSafeFinalize* finalizeData =
|
|
219
215
|
static_cast<ThreadSafeFinalize*>(rawFinalizeData);
|
|
@@ -221,12 +217,10 @@ struct ThreadSafeFinalize {
|
|
|
221
217
|
delete finalizeData;
|
|
222
218
|
}
|
|
223
219
|
|
|
224
|
-
static inline
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
if (rawFinalizeData == nullptr)
|
|
229
|
-
return;
|
|
220
|
+
static inline void FinalizeWrapperWithData(napi_env env,
|
|
221
|
+
void* rawFinalizeData,
|
|
222
|
+
void* /* rawContext */) {
|
|
223
|
+
if (rawFinalizeData == nullptr) return;
|
|
230
224
|
|
|
231
225
|
ThreadSafeFinalize* finalizeData =
|
|
232
226
|
static_cast<ThreadSafeFinalize*>(rawFinalizeData);
|
|
@@ -234,12 +228,10 @@ struct ThreadSafeFinalize {
|
|
|
234
228
|
delete finalizeData;
|
|
235
229
|
}
|
|
236
230
|
|
|
237
|
-
static inline
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
if (rawFinalizeData == nullptr)
|
|
242
|
-
return;
|
|
231
|
+
static inline void FinalizeWrapperWithContext(napi_env env,
|
|
232
|
+
void* rawFinalizeData,
|
|
233
|
+
void* rawContext) {
|
|
234
|
+
if (rawFinalizeData == nullptr) return;
|
|
243
235
|
|
|
244
236
|
ThreadSafeFinalize* finalizeData =
|
|
245
237
|
static_cast<ThreadSafeFinalize*>(rawFinalizeData);
|
|
@@ -247,17 +239,14 @@ struct ThreadSafeFinalize {
|
|
|
247
239
|
delete finalizeData;
|
|
248
240
|
}
|
|
249
241
|
|
|
250
|
-
static inline
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
void* rawContext) {
|
|
254
|
-
if (rawFinalizeData == nullptr)
|
|
255
|
-
return;
|
|
242
|
+
static inline void FinalizeFinalizeWrapperWithDataAndContext(
|
|
243
|
+
napi_env env, void* rawFinalizeData, void* rawContext) {
|
|
244
|
+
if (rawFinalizeData == nullptr) return;
|
|
256
245
|
|
|
257
246
|
ThreadSafeFinalize* finalizeData =
|
|
258
247
|
static_cast<ThreadSafeFinalize*>(rawFinalizeData);
|
|
259
|
-
finalizeData->callback(
|
|
260
|
-
static_cast<ContextType*>(rawContext));
|
|
248
|
+
finalizeData->callback(
|
|
249
|
+
Env(env), finalizeData->data, static_cast<ContextType*>(rawContext));
|
|
261
250
|
delete finalizeData;
|
|
262
251
|
}
|
|
263
252
|
|
|
@@ -266,8 +255,8 @@ struct ThreadSafeFinalize {
|
|
|
266
255
|
};
|
|
267
256
|
|
|
268
257
|
template <typename ContextType, typename DataType, typename CallJs, CallJs call>
|
|
269
|
-
typename std::enable_if<call != nullptr>::type
|
|
270
|
-
|
|
258
|
+
inline typename std::enable_if<call != static_cast<CallJs>(nullptr)>::type
|
|
259
|
+
CallJsWrapper(napi_env env, napi_value jsCallback, void* context, void* data) {
|
|
271
260
|
call(env,
|
|
272
261
|
Function(env, jsCallback),
|
|
273
262
|
static_cast<ContextType*>(context),
|
|
@@ -275,8 +264,11 @@ typename std::enable_if<call != nullptr>::type static inline CallJsWrapper(
|
|
|
275
264
|
}
|
|
276
265
|
|
|
277
266
|
template <typename ContextType, typename DataType, typename CallJs, CallJs call>
|
|
278
|
-
typename std::enable_if<call == nullptr>::type
|
|
279
|
-
|
|
267
|
+
inline typename std::enable_if<call == static_cast<CallJs>(nullptr)>::type
|
|
268
|
+
CallJsWrapper(napi_env env,
|
|
269
|
+
napi_value jsCallback,
|
|
270
|
+
void* /*context*/,
|
|
271
|
+
void* /*data*/) {
|
|
280
272
|
if (jsCallback != nullptr) {
|
|
281
273
|
Function(env, jsCallback).Call(0, nullptr);
|
|
282
274
|
}
|
|
@@ -307,23 +299,23 @@ napi_value DefaultCallbackWrapper(napi_env env, Napi::Function cb) {
|
|
|
307
299
|
|
|
308
300
|
template <typename Getter, typename Setter>
|
|
309
301
|
struct AccessorCallbackData {
|
|
310
|
-
static inline
|
|
311
|
-
|
|
302
|
+
static inline napi_value GetterWrapper(napi_env env,
|
|
303
|
+
napi_callback_info info) {
|
|
312
304
|
return details::WrapCallback([&] {
|
|
313
305
|
CallbackInfo callbackInfo(env, info);
|
|
314
306
|
AccessorCallbackData* callbackData =
|
|
315
|
-
|
|
307
|
+
static_cast<AccessorCallbackData*>(callbackInfo.Data());
|
|
316
308
|
callbackInfo.SetData(callbackData->data);
|
|
317
309
|
return callbackData->getterCallback(callbackInfo);
|
|
318
310
|
});
|
|
319
311
|
}
|
|
320
312
|
|
|
321
|
-
static inline
|
|
322
|
-
|
|
313
|
+
static inline napi_value SetterWrapper(napi_env env,
|
|
314
|
+
napi_callback_info info) {
|
|
323
315
|
return details::WrapCallback([&] {
|
|
324
316
|
CallbackInfo callbackInfo(env, info);
|
|
325
317
|
AccessorCallbackData* callbackData =
|
|
326
|
-
|
|
318
|
+
static_cast<AccessorCallbackData*>(callbackInfo.Data());
|
|
327
319
|
callbackInfo.SetData(callbackData->data);
|
|
328
320
|
callbackData->setterCallback(callbackInfo);
|
|
329
321
|
return nullptr;
|
|
@@ -338,8 +330,8 @@ struct AccessorCallbackData {
|
|
|
338
330
|
} // namespace details
|
|
339
331
|
|
|
340
332
|
#ifndef NODE_ADDON_API_DISABLE_DEPRECATED
|
|
341
|
-
#
|
|
342
|
-
#endif
|
|
333
|
+
#include "napi-inl.deprecated.h"
|
|
334
|
+
#endif // !NODE_ADDON_API_DISABLE_DEPRECATED
|
|
343
335
|
|
|
344
336
|
////////////////////////////////////////////////////////////////////////////////
|
|
345
337
|
// Module registration
|
|
@@ -354,16 +346,15 @@ struct AccessorCallbackData {
|
|
|
354
346
|
|
|
355
347
|
// Register an add-on based on a subclass of `Addon<T>` with a custom Node.js
|
|
356
348
|
// module name.
|
|
357
|
-
#define NODE_API_NAMED_ADDON(modname, classname)
|
|
358
|
-
static napi_value __napi_
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
NAPI_MODULE(modname, __napi_ ## classname)
|
|
349
|
+
#define NODE_API_NAMED_ADDON(modname, classname) \
|
|
350
|
+
static napi_value __napi_##classname(napi_env env, napi_value exports) { \
|
|
351
|
+
return Napi::RegisterModule(env, exports, &classname::Init); \
|
|
352
|
+
} \
|
|
353
|
+
NAPI_MODULE(modname, __napi_##classname)
|
|
363
354
|
|
|
364
355
|
// Register an add-on based on a subclass of `Addon<T>` with the Node.js module
|
|
365
356
|
// name given by node-gyp from the `target_name` in binding.gyp.
|
|
366
|
-
#define NODE_API_ADDON(classname)
|
|
357
|
+
#define NODE_API_ADDON(classname) \
|
|
367
358
|
NODE_API_NAMED_ADDON(NODE_GYP_MODULE_NAME, classname)
|
|
368
359
|
|
|
369
360
|
// Adapt the NAPI_MODULE registration function:
|
|
@@ -373,8 +364,8 @@ inline napi_value RegisterModule(napi_env env,
|
|
|
373
364
|
napi_value exports,
|
|
374
365
|
ModuleRegisterCallback registerCallback) {
|
|
375
366
|
return details::WrapCallback([&] {
|
|
376
|
-
return napi_value(
|
|
377
|
-
|
|
367
|
+
return napi_value(
|
|
368
|
+
registerCallback(Napi::Env(env), Napi::Object(env, exports)));
|
|
378
369
|
});
|
|
379
370
|
}
|
|
380
371
|
|
|
@@ -448,8 +439,7 @@ inline Maybe<T> Just(const T& t) {
|
|
|
448
439
|
// Env class
|
|
449
440
|
////////////////////////////////////////////////////////////////////////////////
|
|
450
441
|
|
|
451
|
-
inline Env::Env(napi_env env) : _env(env) {
|
|
452
|
-
}
|
|
442
|
+
inline Env::Env(napi_env env) : _env(env) {}
|
|
453
443
|
|
|
454
444
|
inline Env::operator napi_env() const {
|
|
455
445
|
return _env;
|
|
@@ -479,11 +469,12 @@ inline Value Env::Null() const {
|
|
|
479
469
|
inline bool Env::IsExceptionPending() const {
|
|
480
470
|
bool result;
|
|
481
471
|
napi_status status = napi_is_exception_pending(_env, &result);
|
|
482
|
-
if (status != napi_ok)
|
|
472
|
+
if (status != napi_ok)
|
|
473
|
+
result = false; // Checking for a pending exception shouldn't throw.
|
|
483
474
|
return result;
|
|
484
475
|
}
|
|
485
476
|
|
|
486
|
-
inline Error Env::GetAndClearPendingException() {
|
|
477
|
+
inline Error Env::GetAndClearPendingException() const {
|
|
487
478
|
napi_value value;
|
|
488
479
|
napi_status status = napi_get_and_clear_last_exception(_env, &value);
|
|
489
480
|
if (status != napi_ok) {
|
|
@@ -493,16 +484,16 @@ inline Error Env::GetAndClearPendingException() {
|
|
|
493
484
|
return Error(_env, value);
|
|
494
485
|
}
|
|
495
486
|
|
|
496
|
-
inline MaybeOrValue<Value> Env::RunScript(const char* utf8script) {
|
|
487
|
+
inline MaybeOrValue<Value> Env::RunScript(const char* utf8script) const {
|
|
497
488
|
String script = String::New(_env, utf8script);
|
|
498
489
|
return RunScript(script);
|
|
499
490
|
}
|
|
500
491
|
|
|
501
|
-
inline MaybeOrValue<Value> Env::RunScript(const std::string& utf8script) {
|
|
492
|
+
inline MaybeOrValue<Value> Env::RunScript(const std::string& utf8script) const {
|
|
502
493
|
return RunScript(utf8script.c_str());
|
|
503
494
|
}
|
|
504
495
|
|
|
505
|
-
inline MaybeOrValue<Value> Env::RunScript(String script) {
|
|
496
|
+
inline MaybeOrValue<Value> Env::RunScript(String script) const {
|
|
506
497
|
napi_value result;
|
|
507
498
|
napi_status status = napi_run_script(_env, script, &result);
|
|
508
499
|
NAPI_RETURN_OR_THROW_IF_FAILED(
|
|
@@ -531,28 +522,31 @@ void Env::CleanupHook<Hook, Arg>::WrapperWithArg(void* data) NAPI_NOEXCEPT {
|
|
|
531
522
|
|
|
532
523
|
#if NAPI_VERSION > 5
|
|
533
524
|
template <typename T, Env::Finalizer<T> fini>
|
|
534
|
-
inline void Env::SetInstanceData(T* data) {
|
|
535
|
-
napi_status status =
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
525
|
+
inline void Env::SetInstanceData(T* data) const {
|
|
526
|
+
napi_status status = napi_set_instance_data(
|
|
527
|
+
_env,
|
|
528
|
+
data,
|
|
529
|
+
[](napi_env env, void* data, void*) { fini(env, static_cast<T*>(data)); },
|
|
530
|
+
nullptr);
|
|
539
531
|
NAPI_THROW_IF_FAILED_VOID(_env, status);
|
|
540
532
|
}
|
|
541
533
|
|
|
542
534
|
template <typename DataType,
|
|
543
535
|
typename HintType,
|
|
544
536
|
Napi::Env::FinalizerWithHint<DataType, HintType> fini>
|
|
545
|
-
inline void Env::SetInstanceData(DataType* data, HintType* hint) {
|
|
546
|
-
napi_status status =
|
|
547
|
-
|
|
537
|
+
inline void Env::SetInstanceData(DataType* data, HintType* hint) const {
|
|
538
|
+
napi_status status = napi_set_instance_data(
|
|
539
|
+
_env,
|
|
540
|
+
data,
|
|
548
541
|
[](napi_env env, void* data, void* hint) {
|
|
549
542
|
fini(env, static_cast<DataType*>(data), static_cast<HintType*>(hint));
|
|
550
|
-
},
|
|
543
|
+
},
|
|
544
|
+
hint);
|
|
551
545
|
NAPI_THROW_IF_FAILED_VOID(_env, status);
|
|
552
546
|
}
|
|
553
547
|
|
|
554
548
|
template <typename T>
|
|
555
|
-
inline T* Env::GetInstanceData() {
|
|
549
|
+
inline T* Env::GetInstanceData() const {
|
|
556
550
|
void* data = nullptr;
|
|
557
551
|
|
|
558
552
|
napi_status status = napi_get_instance_data(_env, &data);
|
|
@@ -561,7 +555,8 @@ inline T* Env::GetInstanceData() {
|
|
|
561
555
|
return static_cast<T*>(data);
|
|
562
556
|
}
|
|
563
557
|
|
|
564
|
-
template <typename T>
|
|
558
|
+
template <typename T>
|
|
559
|
+
void Env::DefaultFini(Env, T* data) {
|
|
565
560
|
delete data;
|
|
566
561
|
}
|
|
567
562
|
|
|
@@ -575,22 +570,21 @@ void Env::DefaultFiniWithHint(Env, DataType* data, HintType*) {
|
|
|
575
570
|
// Value class
|
|
576
571
|
////////////////////////////////////////////////////////////////////////////////
|
|
577
572
|
|
|
578
|
-
inline Value::Value() : _env(nullptr), _value(nullptr) {
|
|
579
|
-
}
|
|
573
|
+
inline Value::Value() : _env(nullptr), _value(nullptr) {}
|
|
580
574
|
|
|
581
|
-
inline Value::Value(napi_env env, napi_value value)
|
|
582
|
-
}
|
|
575
|
+
inline Value::Value(napi_env env, napi_value value)
|
|
576
|
+
: _env(env), _value(value) {}
|
|
583
577
|
|
|
584
578
|
inline Value::operator napi_value() const {
|
|
585
579
|
return _value;
|
|
586
580
|
}
|
|
587
581
|
|
|
588
|
-
inline bool Value::operator
|
|
582
|
+
inline bool Value::operator==(const Value& other) const {
|
|
589
583
|
return StrictEquals(other);
|
|
590
584
|
}
|
|
591
585
|
|
|
592
|
-
inline bool Value::operator
|
|
593
|
-
return !this->operator
|
|
586
|
+
inline bool Value::operator!=(const Value& other) const {
|
|
587
|
+
return !this->operator==(other);
|
|
594
588
|
}
|
|
595
589
|
|
|
596
590
|
inline bool Value::StrictEquals(const Value& other) const {
|
|
@@ -784,11 +778,10 @@ inline Boolean Boolean::New(napi_env env, bool val) {
|
|
|
784
778
|
return Boolean(env, value);
|
|
785
779
|
}
|
|
786
780
|
|
|
787
|
-
inline Boolean::Boolean() : Napi::Value() {
|
|
788
|
-
}
|
|
781
|
+
inline Boolean::Boolean() : Napi::Value() {}
|
|
789
782
|
|
|
790
|
-
inline Boolean::Boolean(napi_env env, napi_value value)
|
|
791
|
-
}
|
|
783
|
+
inline Boolean::Boolean(napi_env env, napi_value value)
|
|
784
|
+
: Napi::Value(env, value) {}
|
|
792
785
|
|
|
793
786
|
inline Boolean::operator bool() const {
|
|
794
787
|
return Value();
|
|
@@ -812,11 +805,9 @@ inline Number Number::New(napi_env env, double val) {
|
|
|
812
805
|
return Number(env, value);
|
|
813
806
|
}
|
|
814
807
|
|
|
815
|
-
inline Number::Number() : Value() {
|
|
816
|
-
}
|
|
808
|
+
inline Number::Number() : Value() {}
|
|
817
809
|
|
|
818
|
-
inline Number::Number(napi_env env, napi_value value) : Value(env, value) {
|
|
819
|
-
}
|
|
810
|
+
inline Number::Number(napi_env env, napi_value value) : Value(env, value) {}
|
|
820
811
|
|
|
821
812
|
inline Number::operator int32_t() const {
|
|
822
813
|
return Int32Value();
|
|
@@ -889,46 +880,50 @@ inline BigInt BigInt::New(napi_env env, uint64_t val) {
|
|
|
889
880
|
return BigInt(env, value);
|
|
890
881
|
}
|
|
891
882
|
|
|
892
|
-
inline BigInt BigInt::New(napi_env env,
|
|
883
|
+
inline BigInt BigInt::New(napi_env env,
|
|
884
|
+
int sign_bit,
|
|
885
|
+
size_t word_count,
|
|
886
|
+
const uint64_t* words) {
|
|
893
887
|
napi_value value;
|
|
894
|
-
napi_status status =
|
|
888
|
+
napi_status status =
|
|
889
|
+
napi_create_bigint_words(env, sign_bit, word_count, words, &value);
|
|
895
890
|
NAPI_THROW_IF_FAILED(env, status, BigInt());
|
|
896
891
|
return BigInt(env, value);
|
|
897
892
|
}
|
|
898
893
|
|
|
899
|
-
inline BigInt::BigInt() : Value() {
|
|
900
|
-
}
|
|
894
|
+
inline BigInt::BigInt() : Value() {}
|
|
901
895
|
|
|
902
|
-
inline BigInt::BigInt(napi_env env, napi_value value) : Value(env, value) {
|
|
903
|
-
}
|
|
896
|
+
inline BigInt::BigInt(napi_env env, napi_value value) : Value(env, value) {}
|
|
904
897
|
|
|
905
898
|
inline int64_t BigInt::Int64Value(bool* lossless) const {
|
|
906
899
|
int64_t result;
|
|
907
|
-
napi_status status =
|
|
908
|
-
_env, _value, &result, lossless);
|
|
900
|
+
napi_status status =
|
|
901
|
+
napi_get_value_bigint_int64(_env, _value, &result, lossless);
|
|
909
902
|
NAPI_THROW_IF_FAILED(_env, status, 0);
|
|
910
903
|
return result;
|
|
911
904
|
}
|
|
912
905
|
|
|
913
906
|
inline uint64_t BigInt::Uint64Value(bool* lossless) const {
|
|
914
907
|
uint64_t result;
|
|
915
|
-
napi_status status =
|
|
916
|
-
_env, _value, &result, lossless);
|
|
908
|
+
napi_status status =
|
|
909
|
+
napi_get_value_bigint_uint64(_env, _value, &result, lossless);
|
|
917
910
|
NAPI_THROW_IF_FAILED(_env, status, 0);
|
|
918
911
|
return result;
|
|
919
912
|
}
|
|
920
913
|
|
|
921
914
|
inline size_t BigInt::WordCount() const {
|
|
922
915
|
size_t word_count;
|
|
923
|
-
napi_status status =
|
|
924
|
-
_env, _value, nullptr, &word_count, nullptr);
|
|
916
|
+
napi_status status =
|
|
917
|
+
napi_get_value_bigint_words(_env, _value, nullptr, &word_count, nullptr);
|
|
925
918
|
NAPI_THROW_IF_FAILED(_env, status, 0);
|
|
926
919
|
return word_count;
|
|
927
920
|
}
|
|
928
921
|
|
|
929
|
-
inline void BigInt::ToWords(int* sign_bit,
|
|
930
|
-
|
|
931
|
-
|
|
922
|
+
inline void BigInt::ToWords(int* sign_bit,
|
|
923
|
+
size_t* word_count,
|
|
924
|
+
uint64_t* words) {
|
|
925
|
+
napi_status status =
|
|
926
|
+
napi_get_value_bigint_words(_env, _value, sign_bit, word_count, words);
|
|
932
927
|
NAPI_THROW_IF_FAILED_VOID(_env, status);
|
|
933
928
|
}
|
|
934
929
|
#endif // NAPI_VERSION > 5
|
|
@@ -945,11 +940,9 @@ inline Date Date::New(napi_env env, double val) {
|
|
|
945
940
|
return Date(env, value);
|
|
946
941
|
}
|
|
947
942
|
|
|
948
|
-
inline Date::Date() : Value() {
|
|
949
|
-
}
|
|
943
|
+
inline Date::Date() : Value() {}
|
|
950
944
|
|
|
951
|
-
inline Date::Date(napi_env env, napi_value value) : Value(env, value) {
|
|
952
|
-
}
|
|
945
|
+
inline Date::Date(napi_env env, napi_value value) : Value(env, value) {}
|
|
953
946
|
|
|
954
947
|
inline Date::operator double() const {
|
|
955
948
|
return ValueOf();
|
|
@@ -957,8 +950,7 @@ inline Date::operator double() const {
|
|
|
957
950
|
|
|
958
951
|
inline double Date::ValueOf() const {
|
|
959
952
|
double result;
|
|
960
|
-
napi_status status = napi_get_date_value(
|
|
961
|
-
_env, _value, &result);
|
|
953
|
+
napi_status status = napi_get_date_value(_env, _value, &result);
|
|
962
954
|
NAPI_THROW_IF_FAILED(_env, status, 0);
|
|
963
955
|
return result;
|
|
964
956
|
}
|
|
@@ -968,11 +960,9 @@ inline double Date::ValueOf() const {
|
|
|
968
960
|
// Name class
|
|
969
961
|
////////////////////////////////////////////////////////////////////////////////
|
|
970
962
|
|
|
971
|
-
inline Name::Name() : Value() {
|
|
972
|
-
}
|
|
963
|
+
inline Name::Name() : Value() {}
|
|
973
964
|
|
|
974
|
-
inline Name::Name(napi_env env, napi_value value) : Value(env, value) {
|
|
975
|
-
}
|
|
965
|
+
inline Name::Name(napi_env env, napi_value value) : Value(env, value) {}
|
|
976
966
|
|
|
977
967
|
////////////////////////////////////////////////////////////////////////////////
|
|
978
968
|
// String class
|
|
@@ -994,7 +984,8 @@ inline String String::New(napi_env env, const char* val) {
|
|
|
994
984
|
NAPI_THROW_IF_FAILED(env, napi_invalid_arg, String());
|
|
995
985
|
}
|
|
996
986
|
napi_value value;
|
|
997
|
-
napi_status status =
|
|
987
|
+
napi_status status =
|
|
988
|
+
napi_create_string_utf8(env, val, std::strlen(val), &value);
|
|
998
989
|
NAPI_THROW_IF_FAILED(env, status, String());
|
|
999
990
|
return String(env, value);
|
|
1000
991
|
}
|
|
@@ -1007,7 +998,8 @@ inline String String::New(napi_env env, const char16_t* val) {
|
|
|
1007
998
|
// Throw an error that looks like it came from core.
|
|
1008
999
|
NAPI_THROW_IF_FAILED(env, napi_invalid_arg, String());
|
|
1009
1000
|
}
|
|
1010
|
-
napi_status status =
|
|
1001
|
+
napi_status status =
|
|
1002
|
+
napi_create_string_utf16(env, val, std::u16string(val).size(), &value);
|
|
1011
1003
|
NAPI_THROW_IF_FAILED(env, status, String());
|
|
1012
1004
|
return String(env, value);
|
|
1013
1005
|
}
|
|
@@ -1026,11 +1018,9 @@ inline String String::New(napi_env env, const char16_t* val, size_t length) {
|
|
|
1026
1018
|
return String(env, value);
|
|
1027
1019
|
}
|
|
1028
1020
|
|
|
1029
|
-
inline String::String() : Name() {
|
|
1030
|
-
}
|
|
1021
|
+
inline String::String() : Name() {}
|
|
1031
1022
|
|
|
1032
|
-
inline String::String(napi_env env, napi_value value) : Name(env, value) {
|
|
1033
|
-
}
|
|
1023
|
+
inline String::String(napi_env env, napi_value value) : Name(env, value) {}
|
|
1034
1024
|
|
|
1035
1025
|
inline String::operator std::string() const {
|
|
1036
1026
|
return Utf8Value();
|
|
@@ -1042,26 +1032,30 @@ inline String::operator std::u16string() const {
|
|
|
1042
1032
|
|
|
1043
1033
|
inline std::string String::Utf8Value() const {
|
|
1044
1034
|
size_t length;
|
|
1045
|
-
napi_status status =
|
|
1035
|
+
napi_status status =
|
|
1036
|
+
napi_get_value_string_utf8(_env, _value, nullptr, 0, &length);
|
|
1046
1037
|
NAPI_THROW_IF_FAILED(_env, status, "");
|
|
1047
1038
|
|
|
1048
1039
|
std::string value;
|
|
1049
1040
|
value.reserve(length + 1);
|
|
1050
1041
|
value.resize(length);
|
|
1051
|
-
status = napi_get_value_string_utf8(
|
|
1042
|
+
status = napi_get_value_string_utf8(
|
|
1043
|
+
_env, _value, &value[0], value.capacity(), nullptr);
|
|
1052
1044
|
NAPI_THROW_IF_FAILED(_env, status, "");
|
|
1053
1045
|
return value;
|
|
1054
1046
|
}
|
|
1055
1047
|
|
|
1056
1048
|
inline std::u16string String::Utf16Value() const {
|
|
1057
1049
|
size_t length;
|
|
1058
|
-
napi_status status =
|
|
1050
|
+
napi_status status =
|
|
1051
|
+
napi_get_value_string_utf16(_env, _value, nullptr, 0, &length);
|
|
1059
1052
|
NAPI_THROW_IF_FAILED(_env, status, NAPI_WIDE_TEXT(""));
|
|
1060
1053
|
|
|
1061
1054
|
std::u16string value;
|
|
1062
1055
|
value.reserve(length + 1);
|
|
1063
1056
|
value.resize(length);
|
|
1064
|
-
status = napi_get_value_string_utf16(
|
|
1057
|
+
status = napi_get_value_string_utf16(
|
|
1058
|
+
_env, _value, &value[0], value.capacity(), nullptr);
|
|
1065
1059
|
NAPI_THROW_IF_FAILED(_env, status, NAPI_WIDE_TEXT(""));
|
|
1066
1060
|
return value;
|
|
1067
1061
|
}
|
|
@@ -1071,8 +1065,9 @@ inline std::u16string String::Utf16Value() const {
|
|
|
1071
1065
|
////////////////////////////////////////////////////////////////////////////////
|
|
1072
1066
|
|
|
1073
1067
|
inline Symbol Symbol::New(napi_env env, const char* description) {
|
|
1074
|
-
napi_value descriptionValue = description != nullptr
|
|
1075
|
-
|
|
1068
|
+
napi_value descriptionValue = description != nullptr
|
|
1069
|
+
? String::New(env, description)
|
|
1070
|
+
: static_cast<napi_value>(nullptr);
|
|
1076
1071
|
return Symbol::New(env, descriptionValue);
|
|
1077
1072
|
}
|
|
1078
1073
|
|
|
@@ -1104,7 +1099,12 @@ inline MaybeOrValue<Symbol> Symbol::WellKnown(napi_env env,
|
|
|
1104
1099
|
}
|
|
1105
1100
|
return Nothing<Symbol>();
|
|
1106
1101
|
#else
|
|
1107
|
-
return Napi::Env(env)
|
|
1102
|
+
return Napi::Env(env)
|
|
1103
|
+
.Global()
|
|
1104
|
+
.Get("Symbol")
|
|
1105
|
+
.As<Object>()
|
|
1106
|
+
.Get(name)
|
|
1107
|
+
.As<Symbol>();
|
|
1108
1108
|
#endif
|
|
1109
1109
|
}
|
|
1110
1110
|
|
|
@@ -1145,11 +1145,9 @@ inline MaybeOrValue<Symbol> Symbol::For(napi_env env, napi_value description) {
|
|
|
1145
1145
|
#endif
|
|
1146
1146
|
}
|
|
1147
1147
|
|
|
1148
|
-
inline Symbol::Symbol() : Name() {
|
|
1149
|
-
}
|
|
1148
|
+
inline Symbol::Symbol() : Name() {}
|
|
1150
1149
|
|
|
1151
|
-
inline Symbol::Symbol(napi_env env, napi_value value) : Name(env, value) {
|
|
1152
|
-
}
|
|
1150
|
+
inline Symbol::Symbol(napi_env env, napi_value value) : Name(env, value) {}
|
|
1153
1151
|
|
|
1154
1152
|
////////////////////////////////////////////////////////////////////////////////
|
|
1155
1153
|
// Automagic value creation
|
|
@@ -1163,7 +1161,7 @@ struct vf_number {
|
|
|
1163
1161
|
}
|
|
1164
1162
|
};
|
|
1165
1163
|
|
|
1166
|
-
template<>
|
|
1164
|
+
template <>
|
|
1167
1165
|
struct vf_number<bool> {
|
|
1168
1166
|
static Boolean From(napi_env env, bool value) {
|
|
1169
1167
|
return Boolean::New(env, value);
|
|
@@ -1195,36 +1193,33 @@ struct vf_utf16_string {
|
|
|
1195
1193
|
|
|
1196
1194
|
template <typename T>
|
|
1197
1195
|
struct vf_fallback {
|
|
1198
|
-
static Value From(napi_env env, const T& value) {
|
|
1199
|
-
return Value(env, value);
|
|
1200
|
-
}
|
|
1196
|
+
static Value From(napi_env env, const T& value) { return Value(env, value); }
|
|
1201
1197
|
};
|
|
1202
1198
|
|
|
1203
|
-
template <typename...>
|
|
1204
|
-
|
|
1199
|
+
template <typename...>
|
|
1200
|
+
struct disjunction : std::false_type {};
|
|
1201
|
+
template <typename B>
|
|
1202
|
+
struct disjunction<B> : B {};
|
|
1205
1203
|
template <typename B, typename... Bs>
|
|
1206
1204
|
struct disjunction<B, Bs...>
|
|
1207
1205
|
: std::conditional<bool(B::value), B, disjunction<Bs...>>::type {};
|
|
1208
1206
|
|
|
1209
1207
|
template <typename T>
|
|
1210
1208
|
struct can_make_string
|
|
1211
|
-
: disjunction<typename std::is_convertible<T, const char
|
|
1212
|
-
typename std::is_convertible<T, const char16_t
|
|
1209
|
+
: disjunction<typename std::is_convertible<T, const char*>::type,
|
|
1210
|
+
typename std::is_convertible<T, const char16_t*>::type,
|
|
1213
1211
|
typename std::is_convertible<T, std::string>::type,
|
|
1214
1212
|
typename std::is_convertible<T, std::u16string>::type> {};
|
|
1215
|
-
}
|
|
1213
|
+
} // namespace details
|
|
1216
1214
|
|
|
1217
1215
|
template <typename T>
|
|
1218
1216
|
Value Value::From(napi_env env, const T& value) {
|
|
1219
1217
|
using Helper = typename std::conditional<
|
|
1220
|
-
|
|
1221
|
-
|
|
1222
|
-
|
|
1223
|
-
|
|
1224
|
-
|
|
1225
|
-
details::vf_fallback<T>
|
|
1226
|
-
>::type
|
|
1227
|
-
>::type;
|
|
1218
|
+
std::is_integral<T>::value || std::is_floating_point<T>::value,
|
|
1219
|
+
details::vf_number<T>,
|
|
1220
|
+
typename std::conditional<details::can_make_string<T>::value,
|
|
1221
|
+
String,
|
|
1222
|
+
details::vf_fallback<T>>::type>::type;
|
|
1228
1223
|
return Helper::From(env, value);
|
|
1229
1224
|
}
|
|
1230
1225
|
|
|
@@ -1232,22 +1227,18 @@ template <typename T>
|
|
|
1232
1227
|
String String::From(napi_env env, const T& value) {
|
|
1233
1228
|
struct Dummy {};
|
|
1234
1229
|
using Helper = typename std::conditional<
|
|
1235
|
-
|
|
1236
|
-
|
|
1237
|
-
typename std::conditional<
|
|
1238
|
-
std::is_convertible<T, const char16_t*>::value,
|
|
1239
|
-
details::vf_utf16_charp,
|
|
1230
|
+
std::is_convertible<T, const char*>::value,
|
|
1231
|
+
details::vf_utf8_charp,
|
|
1240
1232
|
typename std::conditional<
|
|
1241
|
-
|
|
1242
|
-
|
|
1243
|
-
|
|
1244
|
-
|
|
1245
|
-
|
|
1246
|
-
|
|
1247
|
-
|
|
1248
|
-
|
|
1249
|
-
|
|
1250
|
-
>::type;
|
|
1233
|
+
std::is_convertible<T, const char16_t*>::value,
|
|
1234
|
+
details::vf_utf16_charp,
|
|
1235
|
+
typename std::conditional<
|
|
1236
|
+
std::is_convertible<T, std::string>::value,
|
|
1237
|
+
details::vf_utf8_string,
|
|
1238
|
+
typename std::conditional<
|
|
1239
|
+
std::is_convertible<T, std::u16string>::value,
|
|
1240
|
+
details::vf_utf16_string,
|
|
1241
|
+
Dummy>::type>::type>::type>::type;
|
|
1251
1242
|
return Helper::From(env, value);
|
|
1252
1243
|
}
|
|
1253
1244
|
|
|
@@ -1265,8 +1256,10 @@ inline Object::PropertyLValue<Key>::operator Value() const {
|
|
|
1265
1256
|
#endif
|
|
1266
1257
|
}
|
|
1267
1258
|
|
|
1268
|
-
template <typename Key>
|
|
1269
|
-
|
|
1259
|
+
template <typename Key>
|
|
1260
|
+
template <typename ValueType>
|
|
1261
|
+
inline Object::PropertyLValue<Key>& Object::PropertyLValue<Key>::operator=(
|
|
1262
|
+
ValueType value) {
|
|
1270
1263
|
#ifdef NODE_ADDON_API_ENABLE_MAYBE
|
|
1271
1264
|
MaybeOrValue<bool> result =
|
|
1272
1265
|
#endif
|
|
@@ -1279,7 +1272,7 @@ inline Object::PropertyLValue<Key>& Object::PropertyLValue<Key>::operator =(Valu
|
|
|
1279
1272
|
|
|
1280
1273
|
template <typename Key>
|
|
1281
1274
|
inline Object::PropertyLValue<Key>::PropertyLValue(Object object, Key key)
|
|
1282
|
-
|
|
1275
|
+
: _env(object.Env()), _object(object), _key(key) {}
|
|
1283
1276
|
|
|
1284
1277
|
inline Object Object::New(napi_env env) {
|
|
1285
1278
|
napi_value value;
|
|
@@ -1288,28 +1281,24 @@ inline Object Object::New(napi_env env) {
|
|
|
1288
1281
|
return Object(env, value);
|
|
1289
1282
|
}
|
|
1290
1283
|
|
|
1291
|
-
inline Object::Object() : Value() {
|
|
1292
|
-
}
|
|
1284
|
+
inline Object::Object() : Value() {}
|
|
1293
1285
|
|
|
1294
|
-
inline Object::Object(napi_env env, napi_value value) : Value(env, value) {
|
|
1295
|
-
}
|
|
1286
|
+
inline Object::Object(napi_env env, napi_value value) : Value(env, value) {}
|
|
1296
1287
|
|
|
1297
|
-
inline Object::PropertyLValue<std::string> Object::operator
|
|
1288
|
+
inline Object::PropertyLValue<std::string> Object::operator[](
|
|
1289
|
+
const char* utf8name) {
|
|
1298
1290
|
return PropertyLValue<std::string>(*this, utf8name);
|
|
1299
1291
|
}
|
|
1300
1292
|
|
|
1301
|
-
inline Object::PropertyLValue<std::string> Object::operator
|
|
1293
|
+
inline Object::PropertyLValue<std::string> Object::operator[](
|
|
1294
|
+
const std::string& utf8name) {
|
|
1302
1295
|
return PropertyLValue<std::string>(*this, utf8name);
|
|
1303
1296
|
}
|
|
1304
1297
|
|
|
1305
|
-
inline Object::PropertyLValue<uint32_t> Object::operator
|
|
1298
|
+
inline Object::PropertyLValue<uint32_t> Object::operator[](uint32_t index) {
|
|
1306
1299
|
return PropertyLValue<uint32_t>(*this, index);
|
|
1307
1300
|
}
|
|
1308
1301
|
|
|
1309
|
-
inline Object::PropertyLValue<Value> Object::operator[](Value index) {
|
|
1310
|
-
return PropertyLValue<Value>(*this, index);
|
|
1311
|
-
}
|
|
1312
|
-
|
|
1313
1302
|
inline Object::PropertyLValue<Value> Object::operator[](Value index) const {
|
|
1314
1303
|
return PropertyLValue<Value>(*this, index);
|
|
1315
1304
|
}
|
|
@@ -1363,7 +1352,8 @@ inline MaybeOrValue<bool> Object::HasOwnProperty(Value key) const {
|
|
|
1363
1352
|
|
|
1364
1353
|
inline MaybeOrValue<bool> Object::HasOwnProperty(const char* utf8name) const {
|
|
1365
1354
|
napi_value key;
|
|
1366
|
-
napi_status status =
|
|
1355
|
+
napi_status status =
|
|
1356
|
+
napi_create_string_utf8(_env, utf8name, std::strlen(utf8name), &key);
|
|
1367
1357
|
NAPI_MAYBE_THROW_IF_FAILED(_env, status, bool);
|
|
1368
1358
|
return HasOwnProperty(key);
|
|
1369
1359
|
}
|
|
@@ -1396,14 +1386,15 @@ inline MaybeOrValue<Value> Object::Get(const std::string& utf8name) const {
|
|
|
1396
1386
|
}
|
|
1397
1387
|
|
|
1398
1388
|
template <typename ValueType>
|
|
1399
|
-
inline MaybeOrValue<bool> Object::Set(napi_value key,
|
|
1389
|
+
inline MaybeOrValue<bool> Object::Set(napi_value key,
|
|
1390
|
+
const ValueType& value) const {
|
|
1400
1391
|
napi_status status =
|
|
1401
1392
|
napi_set_property(_env, _value, key, Value::From(_env, value));
|
|
1402
1393
|
NAPI_RETURN_OR_THROW_IF_FAILED(_env, status, status == napi_ok, bool);
|
|
1403
1394
|
}
|
|
1404
1395
|
|
|
1405
1396
|
template <typename ValueType>
|
|
1406
|
-
inline MaybeOrValue<bool> Object::Set(Value key, const ValueType& value) {
|
|
1397
|
+
inline MaybeOrValue<bool> Object::Set(Value key, const ValueType& value) const {
|
|
1407
1398
|
napi_status status =
|
|
1408
1399
|
napi_set_property(_env, _value, key, Value::From(_env, value));
|
|
1409
1400
|
NAPI_RETURN_OR_THROW_IF_FAILED(_env, status, status == napi_ok, bool);
|
|
@@ -1411,7 +1402,7 @@ inline MaybeOrValue<bool> Object::Set(Value key, const ValueType& value) {
|
|
|
1411
1402
|
|
|
1412
1403
|
template <typename ValueType>
|
|
1413
1404
|
inline MaybeOrValue<bool> Object::Set(const char* utf8name,
|
|
1414
|
-
const ValueType& value) {
|
|
1405
|
+
const ValueType& value) const {
|
|
1415
1406
|
napi_status status =
|
|
1416
1407
|
napi_set_named_property(_env, _value, utf8name, Value::From(_env, value));
|
|
1417
1408
|
NAPI_RETURN_OR_THROW_IF_FAILED(_env, status, status == napi_ok, bool);
|
|
@@ -1419,27 +1410,27 @@ inline MaybeOrValue<bool> Object::Set(const char* utf8name,
|
|
|
1419
1410
|
|
|
1420
1411
|
template <typename ValueType>
|
|
1421
1412
|
inline MaybeOrValue<bool> Object::Set(const std::string& utf8name,
|
|
1422
|
-
const ValueType& value) {
|
|
1413
|
+
const ValueType& value) const {
|
|
1423
1414
|
return Set(utf8name.c_str(), value);
|
|
1424
1415
|
}
|
|
1425
1416
|
|
|
1426
|
-
inline MaybeOrValue<bool> Object::Delete(napi_value key) {
|
|
1417
|
+
inline MaybeOrValue<bool> Object::Delete(napi_value key) const {
|
|
1427
1418
|
bool result;
|
|
1428
1419
|
napi_status status = napi_delete_property(_env, _value, key, &result);
|
|
1429
1420
|
NAPI_RETURN_OR_THROW_IF_FAILED(_env, status, result, bool);
|
|
1430
1421
|
}
|
|
1431
1422
|
|
|
1432
|
-
inline MaybeOrValue<bool> Object::Delete(Value key) {
|
|
1423
|
+
inline MaybeOrValue<bool> Object::Delete(Value key) const {
|
|
1433
1424
|
bool result;
|
|
1434
1425
|
napi_status status = napi_delete_property(_env, _value, key, &result);
|
|
1435
1426
|
NAPI_RETURN_OR_THROW_IF_FAILED(_env, status, result, bool);
|
|
1436
1427
|
}
|
|
1437
1428
|
|
|
1438
|
-
inline MaybeOrValue<bool> Object::Delete(const char* utf8name) {
|
|
1429
|
+
inline MaybeOrValue<bool> Object::Delete(const char* utf8name) const {
|
|
1439
1430
|
return Delete(String::New(_env, utf8name));
|
|
1440
1431
|
}
|
|
1441
1432
|
|
|
1442
|
-
inline MaybeOrValue<bool> Object::Delete(const std::string& utf8name) {
|
|
1433
|
+
inline MaybeOrValue<bool> Object::Delete(const std::string& utf8name) const {
|
|
1443
1434
|
return Delete(String::New(_env, utf8name));
|
|
1444
1435
|
}
|
|
1445
1436
|
|
|
@@ -1456,13 +1447,14 @@ inline MaybeOrValue<Value> Object::Get(uint32_t index) const {
|
|
|
1456
1447
|
}
|
|
1457
1448
|
|
|
1458
1449
|
template <typename ValueType>
|
|
1459
|
-
inline MaybeOrValue<bool> Object::Set(uint32_t index,
|
|
1450
|
+
inline MaybeOrValue<bool> Object::Set(uint32_t index,
|
|
1451
|
+
const ValueType& value) const {
|
|
1460
1452
|
napi_status status =
|
|
1461
1453
|
napi_set_element(_env, _value, index, Value::From(_env, value));
|
|
1462
1454
|
NAPI_RETURN_OR_THROW_IF_FAILED(_env, status, status == napi_ok, bool);
|
|
1463
1455
|
}
|
|
1464
1456
|
|
|
1465
|
-
inline MaybeOrValue<bool> Object::Delete(uint32_t index) {
|
|
1457
|
+
inline MaybeOrValue<bool> Object::Delete(uint32_t index) const {
|
|
1466
1458
|
bool result;
|
|
1467
1459
|
napi_status status = napi_delete_element(_env, _value, index, &result);
|
|
1468
1460
|
NAPI_RETURN_OR_THROW_IF_FAILED(_env, status, result, bool);
|
|
@@ -1475,23 +1467,32 @@ inline MaybeOrValue<Array> Object::GetPropertyNames() const {
|
|
|
1475
1467
|
}
|
|
1476
1468
|
|
|
1477
1469
|
inline MaybeOrValue<bool> Object::DefineProperty(
|
|
1478
|
-
const PropertyDescriptor& property) {
|
|
1479
|
-
napi_status status = napi_define_properties(
|
|
1480
|
-
|
|
1470
|
+
const PropertyDescriptor& property) const {
|
|
1471
|
+
napi_status status = napi_define_properties(
|
|
1472
|
+
_env,
|
|
1473
|
+
_value,
|
|
1474
|
+
1,
|
|
1475
|
+
reinterpret_cast<const napi_property_descriptor*>(&property));
|
|
1481
1476
|
NAPI_RETURN_OR_THROW_IF_FAILED(_env, status, status == napi_ok, bool);
|
|
1482
1477
|
}
|
|
1483
1478
|
|
|
1484
1479
|
inline MaybeOrValue<bool> Object::DefineProperties(
|
|
1485
|
-
const std::initializer_list<PropertyDescriptor>& properties) {
|
|
1486
|
-
napi_status status = napi_define_properties(
|
|
1487
|
-
|
|
1480
|
+
const std::initializer_list<PropertyDescriptor>& properties) const {
|
|
1481
|
+
napi_status status = napi_define_properties(
|
|
1482
|
+
_env,
|
|
1483
|
+
_value,
|
|
1484
|
+
properties.size(),
|
|
1485
|
+
reinterpret_cast<const napi_property_descriptor*>(properties.begin()));
|
|
1488
1486
|
NAPI_RETURN_OR_THROW_IF_FAILED(_env, status, status == napi_ok, bool);
|
|
1489
1487
|
}
|
|
1490
1488
|
|
|
1491
1489
|
inline MaybeOrValue<bool> Object::DefineProperties(
|
|
1492
|
-
const std::vector<PropertyDescriptor>& properties) {
|
|
1493
|
-
napi_status status = napi_define_properties(
|
|
1494
|
-
|
|
1490
|
+
const std::vector<PropertyDescriptor>& properties) const {
|
|
1491
|
+
napi_status status = napi_define_properties(
|
|
1492
|
+
_env,
|
|
1493
|
+
_value,
|
|
1494
|
+
properties.size(),
|
|
1495
|
+
reinterpret_cast<const napi_property_descriptor*>(properties.data()));
|
|
1495
1496
|
NAPI_RETURN_OR_THROW_IF_FAILED(_env, status, status == napi_ok, bool);
|
|
1496
1497
|
}
|
|
1497
1498
|
|
|
@@ -1503,7 +1504,7 @@ inline MaybeOrValue<bool> Object::InstanceOf(
|
|
|
1503
1504
|
}
|
|
1504
1505
|
|
|
1505
1506
|
template <typename Finalizer, typename T>
|
|
1506
|
-
inline void Object::AddFinalizer(Finalizer finalizeCallback, T* data) {
|
|
1507
|
+
inline void Object::AddFinalizer(Finalizer finalizeCallback, T* data) const {
|
|
1507
1508
|
details::FinalizeData<T, Finalizer>* finalizeData =
|
|
1508
1509
|
new details::FinalizeData<T, Finalizer>(
|
|
1509
1510
|
{std::move(finalizeCallback), nullptr});
|
|
@@ -1522,16 +1523,16 @@ inline void Object::AddFinalizer(Finalizer finalizeCallback, T* data) {
|
|
|
1522
1523
|
template <typename Finalizer, typename T, typename Hint>
|
|
1523
1524
|
inline void Object::AddFinalizer(Finalizer finalizeCallback,
|
|
1524
1525
|
T* data,
|
|
1525
|
-
Hint* finalizeHint) {
|
|
1526
|
+
Hint* finalizeHint) const {
|
|
1526
1527
|
details::FinalizeData<T, Finalizer, Hint>* finalizeData =
|
|
1527
1528
|
new details::FinalizeData<T, Finalizer, Hint>(
|
|
1528
1529
|
{std::move(finalizeCallback), finalizeHint});
|
|
1529
|
-
napi_status status =
|
|
1530
|
-
|
|
1531
|
-
|
|
1532
|
-
|
|
1533
|
-
|
|
1534
|
-
|
|
1530
|
+
napi_status status = details::AttachData(
|
|
1531
|
+
_env,
|
|
1532
|
+
*this,
|
|
1533
|
+
data,
|
|
1534
|
+
details::FinalizeData<T, Finalizer, Hint>::WrapperWithHint,
|
|
1535
|
+
finalizeData);
|
|
1535
1536
|
if (status != napi_ok) {
|
|
1536
1537
|
delete finalizeData;
|
|
1537
1538
|
NAPI_THROW_IF_FAILED_VOID(_env, status);
|
|
@@ -1616,12 +1617,12 @@ Object::iterator::operator*() {
|
|
|
1616
1617
|
#endif // NAPI_CPP_EXCEPTIONS
|
|
1617
1618
|
|
|
1618
1619
|
#if NAPI_VERSION >= 8
|
|
1619
|
-
inline MaybeOrValue<bool> Object::Freeze() {
|
|
1620
|
+
inline MaybeOrValue<bool> Object::Freeze() const {
|
|
1620
1621
|
napi_status status = napi_object_freeze(_env, _value);
|
|
1621
1622
|
NAPI_RETURN_OR_THROW_IF_FAILED(_env, status, status == napi_ok, bool);
|
|
1622
1623
|
}
|
|
1623
1624
|
|
|
1624
|
-
inline MaybeOrValue<bool> Object::Seal() {
|
|
1625
|
+
inline MaybeOrValue<bool> Object::Seal() const {
|
|
1625
1626
|
napi_status status = napi_object_seal(_env, _value);
|
|
1626
1627
|
NAPI_RETURN_OR_THROW_IF_FAILED(_env, status, status == napi_ok, bool);
|
|
1627
1628
|
}
|
|
@@ -1634,7 +1635,8 @@ inline MaybeOrValue<bool> Object::Seal() {
|
|
|
1634
1635
|
template <typename T>
|
|
1635
1636
|
inline External<T> External<T>::New(napi_env env, T* data) {
|
|
1636
1637
|
napi_value value;
|
|
1637
|
-
napi_status status =
|
|
1638
|
+
napi_status status =
|
|
1639
|
+
napi_create_external(env, data, nullptr, nullptr, &value);
|
|
1638
1640
|
NAPI_THROW_IF_FAILED(env, status, External());
|
|
1639
1641
|
return External(env, value);
|
|
1640
1642
|
}
|
|
@@ -1648,12 +1650,12 @@ inline External<T> External<T>::New(napi_env env,
|
|
|
1648
1650
|
details::FinalizeData<T, Finalizer>* finalizeData =
|
|
1649
1651
|
new details::FinalizeData<T, Finalizer>(
|
|
1650
1652
|
{std::move(finalizeCallback), nullptr});
|
|
1651
|
-
napi_status status =
|
|
1652
|
-
|
|
1653
|
-
|
|
1654
|
-
|
|
1655
|
-
|
|
1656
|
-
|
|
1653
|
+
napi_status status =
|
|
1654
|
+
napi_create_external(env,
|
|
1655
|
+
data,
|
|
1656
|
+
details::FinalizeData<T, Finalizer>::Wrapper,
|
|
1657
|
+
finalizeData,
|
|
1658
|
+
&value);
|
|
1657
1659
|
if (status != napi_ok) {
|
|
1658
1660
|
delete finalizeData;
|
|
1659
1661
|
NAPI_THROW_IF_FAILED(env, status, External());
|
|
@@ -1672,11 +1674,11 @@ inline External<T> External<T>::New(napi_env env,
|
|
|
1672
1674
|
new details::FinalizeData<T, Finalizer, Hint>(
|
|
1673
1675
|
{std::move(finalizeCallback), finalizeHint});
|
|
1674
1676
|
napi_status status = napi_create_external(
|
|
1675
|
-
|
|
1676
|
-
|
|
1677
|
-
|
|
1678
|
-
|
|
1679
|
-
|
|
1677
|
+
env,
|
|
1678
|
+
data,
|
|
1679
|
+
details::FinalizeData<T, Finalizer, Hint>::WrapperWithHint,
|
|
1680
|
+
finalizeData,
|
|
1681
|
+
&value);
|
|
1680
1682
|
if (status != napi_ok) {
|
|
1681
1683
|
delete finalizeData;
|
|
1682
1684
|
NAPI_THROW_IF_FAILED(env, status, External());
|
|
@@ -1685,12 +1687,11 @@ inline External<T> External<T>::New(napi_env env,
|
|
|
1685
1687
|
}
|
|
1686
1688
|
|
|
1687
1689
|
template <typename T>
|
|
1688
|
-
inline External<T>::External() : Value() {
|
|
1689
|
-
}
|
|
1690
|
+
inline External<T>::External() : Value() {}
|
|
1690
1691
|
|
|
1691
1692
|
template <typename T>
|
|
1692
|
-
inline External<T>::External(napi_env env, napi_value value)
|
|
1693
|
-
}
|
|
1693
|
+
inline External<T>::External(napi_env env, napi_value value)
|
|
1694
|
+
: Value(env, value) {}
|
|
1694
1695
|
|
|
1695
1696
|
template <typename T>
|
|
1696
1697
|
inline T* External<T>::Data() const {
|
|
@@ -1718,11 +1719,9 @@ inline Array Array::New(napi_env env, size_t length) {
|
|
|
1718
1719
|
return Array(env, value);
|
|
1719
1720
|
}
|
|
1720
1721
|
|
|
1721
|
-
inline Array::Array() : Object() {
|
|
1722
|
-
}
|
|
1722
|
+
inline Array::Array() : Object() {}
|
|
1723
1723
|
|
|
1724
|
-
inline Array::Array(napi_env env, napi_value value) : Object(env, value) {
|
|
1725
|
-
}
|
|
1724
|
+
inline Array::Array(napi_env env, napi_value value) : Object(env, value) {}
|
|
1726
1725
|
|
|
1727
1726
|
inline uint32_t Array::Length() const {
|
|
1728
1727
|
uint32_t result;
|
|
@@ -1749,7 +1748,7 @@ inline ArrayBuffer ArrayBuffer::New(napi_env env,
|
|
|
1749
1748
|
size_t byteLength) {
|
|
1750
1749
|
napi_value value;
|
|
1751
1750
|
napi_status status = napi_create_external_arraybuffer(
|
|
1752
|
-
|
|
1751
|
+
env, externalData, byteLength, nullptr, nullptr, &value);
|
|
1753
1752
|
NAPI_THROW_IF_FAILED(env, status, ArrayBuffer());
|
|
1754
1753
|
|
|
1755
1754
|
return ArrayBuffer(env, value);
|
|
@@ -1765,12 +1764,12 @@ inline ArrayBuffer ArrayBuffer::New(napi_env env,
|
|
|
1765
1764
|
new details::FinalizeData<void, Finalizer>(
|
|
1766
1765
|
{std::move(finalizeCallback), nullptr});
|
|
1767
1766
|
napi_status status = napi_create_external_arraybuffer(
|
|
1768
|
-
|
|
1769
|
-
|
|
1770
|
-
|
|
1771
|
-
|
|
1772
|
-
|
|
1773
|
-
|
|
1767
|
+
env,
|
|
1768
|
+
externalData,
|
|
1769
|
+
byteLength,
|
|
1770
|
+
details::FinalizeData<void, Finalizer>::Wrapper,
|
|
1771
|
+
finalizeData,
|
|
1772
|
+
&value);
|
|
1774
1773
|
if (status != napi_ok) {
|
|
1775
1774
|
delete finalizeData;
|
|
1776
1775
|
NAPI_THROW_IF_FAILED(env, status, ArrayBuffer());
|
|
@@ -1790,12 +1789,12 @@ inline ArrayBuffer ArrayBuffer::New(napi_env env,
|
|
|
1790
1789
|
new details::FinalizeData<void, Finalizer, Hint>(
|
|
1791
1790
|
{std::move(finalizeCallback), finalizeHint});
|
|
1792
1791
|
napi_status status = napi_create_external_arraybuffer(
|
|
1793
|
-
|
|
1794
|
-
|
|
1795
|
-
|
|
1796
|
-
|
|
1797
|
-
|
|
1798
|
-
|
|
1792
|
+
env,
|
|
1793
|
+
externalData,
|
|
1794
|
+
byteLength,
|
|
1795
|
+
details::FinalizeData<void, Finalizer, Hint>::WrapperWithHint,
|
|
1796
|
+
finalizeData,
|
|
1797
|
+
&value);
|
|
1799
1798
|
if (status != napi_ok) {
|
|
1800
1799
|
delete finalizeData;
|
|
1801
1800
|
NAPI_THROW_IF_FAILED(env, status, ArrayBuffer());
|
|
@@ -1804,12 +1803,10 @@ inline ArrayBuffer ArrayBuffer::New(napi_env env,
|
|
|
1804
1803
|
return ArrayBuffer(env, value);
|
|
1805
1804
|
}
|
|
1806
1805
|
|
|
1807
|
-
inline ArrayBuffer::ArrayBuffer() : Object() {
|
|
1808
|
-
}
|
|
1806
|
+
inline ArrayBuffer::ArrayBuffer() : Object() {}
|
|
1809
1807
|
|
|
1810
1808
|
inline ArrayBuffer::ArrayBuffer(napi_env env, napi_value value)
|
|
1811
|
-
|
|
1812
|
-
}
|
|
1809
|
+
: Object(env, value) {}
|
|
1813
1810
|
|
|
1814
1811
|
inline void* ArrayBuffer::Data() {
|
|
1815
1812
|
void* data;
|
|
@@ -1820,7 +1817,8 @@ inline void* ArrayBuffer::Data() {
|
|
|
1820
1817
|
|
|
1821
1818
|
inline size_t ArrayBuffer::ByteLength() {
|
|
1822
1819
|
size_t length;
|
|
1823
|
-
napi_status status =
|
|
1820
|
+
napi_status status =
|
|
1821
|
+
napi_get_arraybuffer_info(_env, _value, nullptr, &length);
|
|
1824
1822
|
NAPI_THROW_IF_FAILED(_env, status, 0);
|
|
1825
1823
|
return length;
|
|
1826
1824
|
}
|
|
@@ -1842,8 +1840,7 @@ inline void ArrayBuffer::Detach() {
|
|
|
1842
1840
|
////////////////////////////////////////////////////////////////////////////////
|
|
1843
1841
|
// DataView class
|
|
1844
1842
|
////////////////////////////////////////////////////////////////////////////////
|
|
1845
|
-
inline DataView DataView::New(napi_env env,
|
|
1846
|
-
Napi::ArrayBuffer arrayBuffer) {
|
|
1843
|
+
inline DataView DataView::New(napi_env env, Napi::ArrayBuffer arrayBuffer) {
|
|
1847
1844
|
return New(env, arrayBuffer, 0, arrayBuffer.ByteLength());
|
|
1848
1845
|
}
|
|
1849
1846
|
|
|
@@ -1851,12 +1848,12 @@ inline DataView DataView::New(napi_env env,
|
|
|
1851
1848
|
Napi::ArrayBuffer arrayBuffer,
|
|
1852
1849
|
size_t byteOffset) {
|
|
1853
1850
|
if (byteOffset > arrayBuffer.ByteLength()) {
|
|
1854
|
-
NAPI_THROW(RangeError::New(
|
|
1855
|
-
|
|
1856
|
-
|
|
1851
|
+
NAPI_THROW(RangeError::New(
|
|
1852
|
+
env, "Start offset is outside the bounds of the buffer"),
|
|
1853
|
+
DataView());
|
|
1857
1854
|
}
|
|
1858
|
-
return New(
|
|
1859
|
-
arrayBuffer.ByteLength() - byteOffset);
|
|
1855
|
+
return New(
|
|
1856
|
+
env, arrayBuffer, byteOffset, arrayBuffer.ByteLength() - byteOffset);
|
|
1860
1857
|
}
|
|
1861
1858
|
|
|
1862
1859
|
inline DataView DataView::New(napi_env env,
|
|
@@ -1864,52 +1861,47 @@ inline DataView DataView::New(napi_env env,
|
|
|
1864
1861
|
size_t byteOffset,
|
|
1865
1862
|
size_t byteLength) {
|
|
1866
1863
|
if (byteOffset + byteLength > arrayBuffer.ByteLength()) {
|
|
1867
|
-
NAPI_THROW(RangeError::New(env, "Invalid DataView length"),
|
|
1868
|
-
DataView());
|
|
1864
|
+
NAPI_THROW(RangeError::New(env, "Invalid DataView length"), DataView());
|
|
1869
1865
|
}
|
|
1870
1866
|
napi_value value;
|
|
1871
|
-
napi_status status =
|
|
1872
|
-
|
|
1867
|
+
napi_status status =
|
|
1868
|
+
napi_create_dataview(env, byteLength, arrayBuffer, byteOffset, &value);
|
|
1873
1869
|
NAPI_THROW_IF_FAILED(env, status, DataView());
|
|
1874
1870
|
return DataView(env, value);
|
|
1875
1871
|
}
|
|
1876
1872
|
|
|
1877
|
-
inline DataView::DataView() : Object() {
|
|
1878
|
-
}
|
|
1873
|
+
inline DataView::DataView() : Object() {}
|
|
1879
1874
|
|
|
1880
1875
|
inline DataView::DataView(napi_env env, napi_value value) : Object(env, value) {
|
|
1881
|
-
napi_status status = napi_get_dataview_info(
|
|
1882
|
-
|
|
1883
|
-
|
|
1884
|
-
|
|
1885
|
-
|
|
1886
|
-
|
|
1887
|
-
nullptr /* byteOffset */);
|
|
1876
|
+
napi_status status = napi_get_dataview_info(_env,
|
|
1877
|
+
_value /* dataView */,
|
|
1878
|
+
&_length /* byteLength */,
|
|
1879
|
+
&_data /* data */,
|
|
1880
|
+
nullptr /* arrayBuffer */,
|
|
1881
|
+
nullptr /* byteOffset */);
|
|
1888
1882
|
NAPI_THROW_IF_FAILED_VOID(_env, status);
|
|
1889
1883
|
}
|
|
1890
1884
|
|
|
1891
1885
|
inline Napi::ArrayBuffer DataView::ArrayBuffer() const {
|
|
1892
1886
|
napi_value arrayBuffer;
|
|
1893
|
-
napi_status status = napi_get_dataview_info(
|
|
1894
|
-
|
|
1895
|
-
|
|
1896
|
-
|
|
1897
|
-
|
|
1898
|
-
|
|
1899
|
-
nullptr /* byteOffset */);
|
|
1887
|
+
napi_status status = napi_get_dataview_info(_env,
|
|
1888
|
+
_value /* dataView */,
|
|
1889
|
+
nullptr /* byteLength */,
|
|
1890
|
+
nullptr /* data */,
|
|
1891
|
+
&arrayBuffer /* arrayBuffer */,
|
|
1892
|
+
nullptr /* byteOffset */);
|
|
1900
1893
|
NAPI_THROW_IF_FAILED(_env, status, Napi::ArrayBuffer());
|
|
1901
1894
|
return Napi::ArrayBuffer(_env, arrayBuffer);
|
|
1902
1895
|
}
|
|
1903
1896
|
|
|
1904
1897
|
inline size_t DataView::ByteOffset() const {
|
|
1905
1898
|
size_t byteOffset;
|
|
1906
|
-
napi_status status = napi_get_dataview_info(
|
|
1907
|
-
|
|
1908
|
-
|
|
1909
|
-
|
|
1910
|
-
|
|
1911
|
-
|
|
1912
|
-
&byteOffset /* byteOffset */);
|
|
1899
|
+
napi_status status = napi_get_dataview_info(_env,
|
|
1900
|
+
_value /* dataView */,
|
|
1901
|
+
nullptr /* byteLength */,
|
|
1902
|
+
nullptr /* data */,
|
|
1903
|
+
nullptr /* arrayBuffer */,
|
|
1904
|
+
&byteOffset /* byteOffset */);
|
|
1913
1905
|
NAPI_THROW_IF_FAILED(_env, status, 0);
|
|
1914
1906
|
return byteOffset;
|
|
1915
1907
|
}
|
|
@@ -1990,8 +1982,9 @@ template <typename T>
|
|
|
1990
1982
|
inline T DataView::ReadData(size_t byteOffset) const {
|
|
1991
1983
|
if (byteOffset + sizeof(T) > _length ||
|
|
1992
1984
|
byteOffset + sizeof(T) < byteOffset) { // overflow
|
|
1993
|
-
NAPI_THROW(
|
|
1994
|
-
"Offset is outside the bounds of the DataView"),
|
|
1985
|
+
NAPI_THROW(
|
|
1986
|
+
RangeError::New(_env, "Offset is outside the bounds of the DataView"),
|
|
1987
|
+
0);
|
|
1995
1988
|
}
|
|
1996
1989
|
|
|
1997
1990
|
return *reinterpret_cast<T*>(static_cast<uint8_t*>(_data) + byteOffset);
|
|
@@ -2001,8 +1994,8 @@ template <typename T>
|
|
|
2001
1994
|
inline void DataView::WriteData(size_t byteOffset, T value) const {
|
|
2002
1995
|
if (byteOffset + sizeof(T) > _length ||
|
|
2003
1996
|
byteOffset + sizeof(T) < byteOffset) { // overflow
|
|
2004
|
-
NAPI_THROW_VOID(
|
|
2005
|
-
"Offset is outside the bounds of the DataView"));
|
|
1997
|
+
NAPI_THROW_VOID(
|
|
1998
|
+
RangeError::New(_env, "Offset is outside the bounds of the DataView"));
|
|
2006
1999
|
}
|
|
2007
2000
|
|
|
2008
2001
|
*reinterpret_cast<T*>(static_cast<uint8_t*>(_data) + byteOffset) = value;
|
|
@@ -2013,33 +2006,37 @@ inline void DataView::WriteData(size_t byteOffset, T value) const {
|
|
|
2013
2006
|
////////////////////////////////////////////////////////////////////////////////
|
|
2014
2007
|
|
|
2015
2008
|
inline TypedArray::TypedArray()
|
|
2016
|
-
|
|
2017
|
-
}
|
|
2009
|
+
: Object(), _type(napi_typedarray_type::napi_int8_array), _length(0) {}
|
|
2018
2010
|
|
|
2019
2011
|
inline TypedArray::TypedArray(napi_env env, napi_value value)
|
|
2020
|
-
|
|
2012
|
+
: Object(env, value),
|
|
2013
|
+
_type(napi_typedarray_type::napi_int8_array),
|
|
2014
|
+
_length(0) {
|
|
2015
|
+
if (value != nullptr) {
|
|
2016
|
+
napi_status status =
|
|
2017
|
+
napi_get_typedarray_info(_env,
|
|
2018
|
+
_value,
|
|
2019
|
+
&const_cast<TypedArray*>(this)->_type,
|
|
2020
|
+
&const_cast<TypedArray*>(this)->_length,
|
|
2021
|
+
nullptr,
|
|
2022
|
+
nullptr,
|
|
2023
|
+
nullptr);
|
|
2024
|
+
NAPI_THROW_IF_FAILED_VOID(_env, status);
|
|
2025
|
+
}
|
|
2021
2026
|
}
|
|
2022
2027
|
|
|
2023
2028
|
inline TypedArray::TypedArray(napi_env env,
|
|
2024
2029
|
napi_value value,
|
|
2025
2030
|
napi_typedarray_type type,
|
|
2026
2031
|
size_t length)
|
|
2027
|
-
|
|
2028
|
-
}
|
|
2032
|
+
: Object(env, value), _type(type), _length(length) {}
|
|
2029
2033
|
|
|
2030
2034
|
inline napi_typedarray_type TypedArray::TypedArrayType() const {
|
|
2031
|
-
if (_type == TypedArray::unknown_array_type) {
|
|
2032
|
-
napi_status status = napi_get_typedarray_info(_env, _value,
|
|
2033
|
-
&const_cast<TypedArray*>(this)->_type, &const_cast<TypedArray*>(this)->_length,
|
|
2034
|
-
nullptr, nullptr, nullptr);
|
|
2035
|
-
NAPI_THROW_IF_FAILED(_env, status, napi_int8_array);
|
|
2036
|
-
}
|
|
2037
|
-
|
|
2038
2035
|
return _type;
|
|
2039
2036
|
}
|
|
2040
2037
|
|
|
2041
2038
|
inline uint8_t TypedArray::ElementSize() const {
|
|
2042
|
-
switch (
|
|
2039
|
+
switch (_type) {
|
|
2043
2040
|
case napi_int8_array:
|
|
2044
2041
|
case napi_uint8_array:
|
|
2045
2042
|
case napi_uint8_clamped_array:
|
|
@@ -2063,20 +2060,13 @@ inline uint8_t TypedArray::ElementSize() const {
|
|
|
2063
2060
|
}
|
|
2064
2061
|
|
|
2065
2062
|
inline size_t TypedArray::ElementLength() const {
|
|
2066
|
-
if (_type == TypedArray::unknown_array_type) {
|
|
2067
|
-
napi_status status = napi_get_typedarray_info(_env, _value,
|
|
2068
|
-
&const_cast<TypedArray*>(this)->_type, &const_cast<TypedArray*>(this)->_length,
|
|
2069
|
-
nullptr, nullptr, nullptr);
|
|
2070
|
-
NAPI_THROW_IF_FAILED(_env, status, 0);
|
|
2071
|
-
}
|
|
2072
|
-
|
|
2073
2063
|
return _length;
|
|
2074
2064
|
}
|
|
2075
2065
|
|
|
2076
2066
|
inline size_t TypedArray::ByteOffset() const {
|
|
2077
2067
|
size_t byteOffset;
|
|
2078
2068
|
napi_status status = napi_get_typedarray_info(
|
|
2079
|
-
|
|
2069
|
+
_env, _value, nullptr, nullptr, nullptr, nullptr, &byteOffset);
|
|
2080
2070
|
NAPI_THROW_IF_FAILED(_env, status, 0);
|
|
2081
2071
|
return byteOffset;
|
|
2082
2072
|
}
|
|
@@ -2088,7 +2078,7 @@ inline size_t TypedArray::ByteLength() const {
|
|
|
2088
2078
|
inline Napi::ArrayBuffer TypedArray::ArrayBuffer() const {
|
|
2089
2079
|
napi_value arrayBuffer;
|
|
2090
2080
|
napi_status status = napi_get_typedarray_info(
|
|
2091
|
-
|
|
2081
|
+
_env, _value, nullptr, nullptr, nullptr, &arrayBuffer, nullptr);
|
|
2092
2082
|
NAPI_THROW_IF_FAILED(_env, status, Napi::ArrayBuffer());
|
|
2093
2083
|
return Napi::ArrayBuffer(_env, arrayBuffer);
|
|
2094
2084
|
}
|
|
@@ -2101,7 +2091,8 @@ template <typename T>
|
|
|
2101
2091
|
inline TypedArrayOf<T> TypedArrayOf<T>::New(napi_env env,
|
|
2102
2092
|
size_t elementLength,
|
|
2103
2093
|
napi_typedarray_type type) {
|
|
2104
|
-
Napi::ArrayBuffer arrayBuffer =
|
|
2094
|
+
Napi::ArrayBuffer arrayBuffer =
|
|
2095
|
+
Napi::ArrayBuffer::New(env, elementLength * sizeof(T));
|
|
2105
2096
|
return New(env, elementLength, arrayBuffer, 0, type);
|
|
2106
2097
|
}
|
|
2107
2098
|
|
|
@@ -2113,21 +2104,24 @@ inline TypedArrayOf<T> TypedArrayOf<T>::New(napi_env env,
|
|
|
2113
2104
|
napi_typedarray_type type) {
|
|
2114
2105
|
napi_value value;
|
|
2115
2106
|
napi_status status = napi_create_typedarray(
|
|
2116
|
-
|
|
2107
|
+
env, type, elementLength, arrayBuffer, bufferOffset, &value);
|
|
2117
2108
|
NAPI_THROW_IF_FAILED(env, status, TypedArrayOf<T>());
|
|
2118
2109
|
|
|
2119
2110
|
return TypedArrayOf<T>(
|
|
2120
|
-
|
|
2121
|
-
|
|
2111
|
+
env,
|
|
2112
|
+
value,
|
|
2113
|
+
type,
|
|
2114
|
+
elementLength,
|
|
2115
|
+
reinterpret_cast<T*>(reinterpret_cast<uint8_t*>(arrayBuffer.Data()) +
|
|
2116
|
+
bufferOffset));
|
|
2122
2117
|
}
|
|
2123
2118
|
|
|
2124
2119
|
template <typename T>
|
|
2125
|
-
inline TypedArrayOf<T>::TypedArrayOf() : TypedArray(), _data(nullptr) {
|
|
2126
|
-
}
|
|
2120
|
+
inline TypedArrayOf<T>::TypedArrayOf() : TypedArray(), _data(nullptr) {}
|
|
2127
2121
|
|
|
2128
2122
|
template <typename T>
|
|
2129
2123
|
inline TypedArrayOf<T>::TypedArrayOf(napi_env env, napi_value value)
|
|
2130
|
-
|
|
2124
|
+
: TypedArray(env, value), _data(nullptr) {
|
|
2131
2125
|
napi_status status = napi_ok;
|
|
2132
2126
|
if (value != nullptr) {
|
|
2133
2127
|
void* data = nullptr;
|
|
@@ -2147,21 +2141,24 @@ inline TypedArrayOf<T>::TypedArrayOf(napi_env env,
|
|
|
2147
2141
|
napi_typedarray_type type,
|
|
2148
2142
|
size_t length,
|
|
2149
2143
|
T* data)
|
|
2150
|
-
|
|
2144
|
+
: TypedArray(env, value, type, length), _data(data) {
|
|
2151
2145
|
if (!(type == TypedArrayTypeForPrimitiveType<T>() ||
|
|
2152
|
-
|
|
2153
|
-
|
|
2154
|
-
|
|
2146
|
+
(type == napi_uint8_clamped_array &&
|
|
2147
|
+
std::is_same<T, uint8_t>::value))) {
|
|
2148
|
+
NAPI_THROW_VOID(TypeError::New(
|
|
2149
|
+
env,
|
|
2150
|
+
"Array type must match the template parameter. "
|
|
2151
|
+
"(Uint8 arrays may optionally have the \"clamped\" array type.)"));
|
|
2155
2152
|
}
|
|
2156
2153
|
}
|
|
2157
2154
|
|
|
2158
2155
|
template <typename T>
|
|
2159
|
-
inline T& TypedArrayOf<T>::operator
|
|
2156
|
+
inline T& TypedArrayOf<T>::operator[](size_t index) {
|
|
2160
2157
|
return _data[index];
|
|
2161
2158
|
}
|
|
2162
2159
|
|
|
2163
2160
|
template <typename T>
|
|
2164
|
-
inline const T& TypedArrayOf<T>::operator
|
|
2161
|
+
inline const T& TypedArrayOf<T>::operator[](size_t index) const {
|
|
2165
2162
|
return _data[index];
|
|
2166
2163
|
}
|
|
2167
2164
|
|
|
@@ -2180,12 +2177,11 @@ inline const T* TypedArrayOf<T>::Data() const {
|
|
|
2180
2177
|
////////////////////////////////////////////////////////////////////////////////
|
|
2181
2178
|
|
|
2182
2179
|
template <typename CbData>
|
|
2183
|
-
|
|
2184
|
-
|
|
2185
|
-
|
|
2186
|
-
|
|
2187
|
-
|
|
2188
|
-
napi_value* result) {
|
|
2180
|
+
inline napi_status CreateFunction(napi_env env,
|
|
2181
|
+
const char* utf8name,
|
|
2182
|
+
napi_callback cb,
|
|
2183
|
+
CbData* data,
|
|
2184
|
+
napi_value* result) {
|
|
2189
2185
|
napi_status status =
|
|
2190
2186
|
napi_create_function(env, utf8name, NAPI_AUTO_LENGTH, cb, data, result);
|
|
2191
2187
|
if (status == napi_ok) {
|
|
@@ -2245,11 +2241,8 @@ inline Function Function::New(napi_env env,
|
|
|
2245
2241
|
auto callbackData = new CbData{std::move(cb), data};
|
|
2246
2242
|
|
|
2247
2243
|
napi_value value;
|
|
2248
|
-
napi_status status =
|
|
2249
|
-
|
|
2250
|
-
CbData::Wrapper,
|
|
2251
|
-
callbackData,
|
|
2252
|
-
&value);
|
|
2244
|
+
napi_status status =
|
|
2245
|
+
CreateFunction(env, utf8name, CbData::Wrapper, callbackData, &value);
|
|
2253
2246
|
if (status != napi_ok) {
|
|
2254
2247
|
delete callbackData;
|
|
2255
2248
|
NAPI_THROW_IF_FAILED(env, status, Function());
|
|
@@ -2266,11 +2259,10 @@ inline Function Function::New(napi_env env,
|
|
|
2266
2259
|
return New(env, cb, utf8name.c_str(), data);
|
|
2267
2260
|
}
|
|
2268
2261
|
|
|
2269
|
-
inline Function::Function() : Object() {
|
|
2270
|
-
}
|
|
2262
|
+
inline Function::Function() : Object() {}
|
|
2271
2263
|
|
|
2272
|
-
inline Function::Function(napi_env env, napi_value value)
|
|
2273
|
-
}
|
|
2264
|
+
inline Function::Function(napi_env env, napi_value value)
|
|
2265
|
+
: Object(env, value) {}
|
|
2274
2266
|
|
|
2275
2267
|
inline MaybeOrValue<Value> Function::operator()(
|
|
2276
2268
|
const std::initializer_list<napi_value>& args) const {
|
|
@@ -2287,6 +2279,11 @@ inline MaybeOrValue<Value> Function::Call(
|
|
|
2287
2279
|
return Call(Env().Undefined(), args);
|
|
2288
2280
|
}
|
|
2289
2281
|
|
|
2282
|
+
inline MaybeOrValue<Value> Function::Call(
|
|
2283
|
+
const std::vector<Value>& args) const {
|
|
2284
|
+
return Call(Env().Undefined(), args);
|
|
2285
|
+
}
|
|
2286
|
+
|
|
2290
2287
|
inline MaybeOrValue<Value> Function::Call(size_t argc,
|
|
2291
2288
|
const napi_value* args) const {
|
|
2292
2289
|
return Call(Env().Undefined(), argc, args);
|
|
@@ -2302,12 +2299,33 @@ inline MaybeOrValue<Value> Function::Call(
|
|
|
2302
2299
|
return Call(recv, args.size(), args.data());
|
|
2303
2300
|
}
|
|
2304
2301
|
|
|
2302
|
+
inline MaybeOrValue<Value> Function::Call(
|
|
2303
|
+
napi_value recv, const std::vector<Value>& args) const {
|
|
2304
|
+
const size_t argc = args.size();
|
|
2305
|
+
const size_t stackArgsCount = 6;
|
|
2306
|
+
napi_value stackArgs[stackArgsCount];
|
|
2307
|
+
std::vector<napi_value> heapArgs;
|
|
2308
|
+
napi_value* argv;
|
|
2309
|
+
if (argc <= stackArgsCount) {
|
|
2310
|
+
argv = stackArgs;
|
|
2311
|
+
} else {
|
|
2312
|
+
heapArgs.resize(argc);
|
|
2313
|
+
argv = heapArgs.data();
|
|
2314
|
+
}
|
|
2315
|
+
|
|
2316
|
+
for (size_t index = 0; index < argc; index++) {
|
|
2317
|
+
argv[index] = static_cast<napi_value>(args[index]);
|
|
2318
|
+
}
|
|
2319
|
+
|
|
2320
|
+
return Call(recv, argc, argv);
|
|
2321
|
+
}
|
|
2322
|
+
|
|
2305
2323
|
inline MaybeOrValue<Value> Function::Call(napi_value recv,
|
|
2306
2324
|
size_t argc,
|
|
2307
2325
|
const napi_value* args) const {
|
|
2308
2326
|
napi_value result;
|
|
2309
|
-
napi_status status =
|
|
2310
|
-
|
|
2327
|
+
napi_status status =
|
|
2328
|
+
napi_call_function(_env, recv, _value, argc, args, &result);
|
|
2311
2329
|
NAPI_RETURN_OR_THROW_IF_FAILED(
|
|
2312
2330
|
_env, status, Napi::Value(_env, result), Napi::Value);
|
|
2313
2331
|
}
|
|
@@ -2332,8 +2350,8 @@ inline MaybeOrValue<Value> Function::MakeCallback(
|
|
|
2332
2350
|
const napi_value* args,
|
|
2333
2351
|
napi_async_context context) const {
|
|
2334
2352
|
napi_value result;
|
|
2335
|
-
napi_status status =
|
|
2336
|
-
|
|
2353
|
+
napi_status status =
|
|
2354
|
+
napi_make_callback(_env, context, recv, _value, argc, args, &result);
|
|
2337
2355
|
NAPI_RETURN_OR_THROW_IF_FAILED(
|
|
2338
2356
|
_env, status, Napi::Value(_env, result), Napi::Value);
|
|
2339
2357
|
}
|
|
@@ -2351,8 +2369,7 @@ inline MaybeOrValue<Object> Function::New(
|
|
|
2351
2369
|
inline MaybeOrValue<Object> Function::New(size_t argc,
|
|
2352
2370
|
const napi_value* args) const {
|
|
2353
2371
|
napi_value result;
|
|
2354
|
-
napi_status status = napi_new_instance(
|
|
2355
|
-
_env, _value, argc, args, &result);
|
|
2372
|
+
napi_status status = napi_new_instance(_env, _value, argc, args, &result);
|
|
2356
2373
|
NAPI_RETURN_OR_THROW_IF_FAILED(
|
|
2357
2374
|
_env, status, Napi::Object(_env, result), Napi::Object);
|
|
2358
2375
|
}
|
|
@@ -2388,8 +2405,7 @@ inline void Promise::Deferred::Reject(napi_value value) const {
|
|
|
2388
2405
|
NAPI_THROW_IF_FAILED_VOID(_env, status);
|
|
2389
2406
|
}
|
|
2390
2407
|
|
|
2391
|
-
inline Promise::Promise(napi_env env, napi_value value) : Object(env, value) {
|
|
2392
|
-
}
|
|
2408
|
+
inline Promise::Promise(napi_env env, napi_value value) : Object(env, value) {}
|
|
2393
2409
|
|
|
2394
2410
|
////////////////////////////////////////////////////////////////////////////////
|
|
2395
2411
|
// Buffer<T> class
|
|
@@ -2399,7 +2415,8 @@ template <typename T>
|
|
|
2399
2415
|
inline Buffer<T> Buffer<T>::New(napi_env env, size_t length) {
|
|
2400
2416
|
napi_value value;
|
|
2401
2417
|
void* data;
|
|
2402
|
-
napi_status status =
|
|
2418
|
+
napi_status status =
|
|
2419
|
+
napi_create_buffer(env, length * sizeof(T), &data, &value);
|
|
2403
2420
|
NAPI_THROW_IF_FAILED(env, status, Buffer<T>());
|
|
2404
2421
|
return Buffer(env, value, length, static_cast<T*>(data));
|
|
2405
2422
|
}
|
|
@@ -2408,7 +2425,7 @@ template <typename T>
|
|
|
2408
2425
|
inline Buffer<T> Buffer<T>::New(napi_env env, T* data, size_t length) {
|
|
2409
2426
|
napi_value value;
|
|
2410
2427
|
napi_status status = napi_create_external_buffer(
|
|
2411
|
-
|
|
2428
|
+
env, length * sizeof(T), data, nullptr, nullptr, &value);
|
|
2412
2429
|
NAPI_THROW_IF_FAILED(env, status, Buffer<T>());
|
|
2413
2430
|
return Buffer(env, value, length, data);
|
|
2414
2431
|
}
|
|
@@ -2423,13 +2440,13 @@ inline Buffer<T> Buffer<T>::New(napi_env env,
|
|
|
2423
2440
|
details::FinalizeData<T, Finalizer>* finalizeData =
|
|
2424
2441
|
new details::FinalizeData<T, Finalizer>(
|
|
2425
2442
|
{std::move(finalizeCallback), nullptr});
|
|
2426
|
-
napi_status status =
|
|
2427
|
-
|
|
2428
|
-
|
|
2429
|
-
|
|
2430
|
-
|
|
2431
|
-
|
|
2432
|
-
|
|
2443
|
+
napi_status status =
|
|
2444
|
+
napi_create_external_buffer(env,
|
|
2445
|
+
length * sizeof(T),
|
|
2446
|
+
data,
|
|
2447
|
+
details::FinalizeData<T, Finalizer>::Wrapper,
|
|
2448
|
+
finalizeData,
|
|
2449
|
+
&value);
|
|
2433
2450
|
if (status != napi_ok) {
|
|
2434
2451
|
delete finalizeData;
|
|
2435
2452
|
NAPI_THROW_IF_FAILED(env, status, Buffer());
|
|
@@ -2449,12 +2466,12 @@ inline Buffer<T> Buffer<T>::New(napi_env env,
|
|
|
2449
2466
|
new details::FinalizeData<T, Finalizer, Hint>(
|
|
2450
2467
|
{std::move(finalizeCallback), finalizeHint});
|
|
2451
2468
|
napi_status status = napi_create_external_buffer(
|
|
2452
|
-
|
|
2453
|
-
|
|
2454
|
-
|
|
2455
|
-
|
|
2456
|
-
|
|
2457
|
-
|
|
2469
|
+
env,
|
|
2470
|
+
length * sizeof(T),
|
|
2471
|
+
data,
|
|
2472
|
+
details::FinalizeData<T, Finalizer, Hint>::WrapperWithHint,
|
|
2473
|
+
finalizeData,
|
|
2474
|
+
&value);
|
|
2458
2475
|
if (status != napi_ok) {
|
|
2459
2476
|
delete finalizeData;
|
|
2460
2477
|
NAPI_THROW_IF_FAILED(env, status, Buffer());
|
|
@@ -2465,25 +2482,22 @@ inline Buffer<T> Buffer<T>::New(napi_env env,
|
|
|
2465
2482
|
template <typename T>
|
|
2466
2483
|
inline Buffer<T> Buffer<T>::Copy(napi_env env, const T* data, size_t length) {
|
|
2467
2484
|
napi_value value;
|
|
2468
|
-
napi_status status =
|
|
2469
|
-
|
|
2485
|
+
napi_status status =
|
|
2486
|
+
napi_create_buffer_copy(env, length * sizeof(T), data, nullptr, &value);
|
|
2470
2487
|
NAPI_THROW_IF_FAILED(env, status, Buffer<T>());
|
|
2471
2488
|
return Buffer<T>(env, value);
|
|
2472
2489
|
}
|
|
2473
2490
|
|
|
2474
2491
|
template <typename T>
|
|
2475
|
-
inline Buffer<T>::Buffer() : Uint8Array(), _length(0), _data(nullptr) {
|
|
2476
|
-
}
|
|
2492
|
+
inline Buffer<T>::Buffer() : Uint8Array(), _length(0), _data(nullptr) {}
|
|
2477
2493
|
|
|
2478
2494
|
template <typename T>
|
|
2479
2495
|
inline Buffer<T>::Buffer(napi_env env, napi_value value)
|
|
2480
|
-
|
|
2481
|
-
}
|
|
2496
|
+
: Uint8Array(env, value), _length(0), _data(nullptr) {}
|
|
2482
2497
|
|
|
2483
2498
|
template <typename T>
|
|
2484
2499
|
inline Buffer<T>::Buffer(napi_env env, napi_value value, size_t length, T* data)
|
|
2485
|
-
|
|
2486
|
-
}
|
|
2500
|
+
: Uint8Array(env, value), _length(length), _data(data) {}
|
|
2487
2501
|
|
|
2488
2502
|
template <typename T>
|
|
2489
2503
|
inline size_t Buffer<T>::Length() const {
|
|
@@ -2505,9 +2519,10 @@ inline void Buffer<T>::EnsureInfo() const {
|
|
|
2505
2519
|
if (_data == nullptr) {
|
|
2506
2520
|
size_t byteLength;
|
|
2507
2521
|
void* voidData;
|
|
2508
|
-
napi_status status =
|
|
2522
|
+
napi_status status =
|
|
2523
|
+
napi_get_buffer_info(_env, _value, &voidData, &byteLength);
|
|
2509
2524
|
NAPI_THROW_IF_FAILED_VOID(_env, status);
|
|
2510
|
-
_length = byteLength / sizeof
|
|
2525
|
+
_length = byteLength / sizeof(T);
|
|
2511
2526
|
_data = static_cast<T*>(voidData);
|
|
2512
2527
|
}
|
|
2513
2528
|
}
|
|
@@ -2544,19 +2559,16 @@ inline Error Error::New(napi_env env) {
|
|
|
2544
2559
|
// A pending exception takes precedence over any internal error status.
|
|
2545
2560
|
if (is_exception_pending) {
|
|
2546
2561
|
status = napi_get_and_clear_last_exception(env, &error);
|
|
2547
|
-
NAPI_FATAL_IF_FAILED(
|
|
2548
|
-
|
|
2549
|
-
else {
|
|
2562
|
+
NAPI_FATAL_IF_FAILED(
|
|
2563
|
+
status, "Error::New", "napi_get_and_clear_last_exception");
|
|
2564
|
+
} else {
|
|
2550
2565
|
const char* error_message = last_error_info_copy.error_message != nullptr
|
|
2551
2566
|
? last_error_info_copy.error_message
|
|
2552
2567
|
: "Error in native callback";
|
|
2553
2568
|
|
|
2554
2569
|
napi_value message;
|
|
2555
2570
|
status = napi_create_string_utf8(
|
|
2556
|
-
|
|
2557
|
-
error_message,
|
|
2558
|
-
std::strlen(error_message),
|
|
2559
|
-
&message);
|
|
2571
|
+
env, error_message, std::strlen(error_message), &message);
|
|
2560
2572
|
NAPI_FATAL_IF_FAILED(status, "Error::New", "napi_create_string_utf8");
|
|
2561
2573
|
|
|
2562
2574
|
switch (last_error_info_copy.error_code) {
|
|
@@ -2577,21 +2589,24 @@ inline Error Error::New(napi_env env) {
|
|
|
2577
2589
|
}
|
|
2578
2590
|
|
|
2579
2591
|
inline Error Error::New(napi_env env, const char* message) {
|
|
2580
|
-
return Error::New<Error>(
|
|
2592
|
+
return Error::New<Error>(
|
|
2593
|
+
env, message, std::strlen(message), napi_create_error);
|
|
2581
2594
|
}
|
|
2582
2595
|
|
|
2583
2596
|
inline Error Error::New(napi_env env, const std::string& message) {
|
|
2584
|
-
return Error::New<Error>(
|
|
2597
|
+
return Error::New<Error>(
|
|
2598
|
+
env, message.c_str(), message.size(), napi_create_error);
|
|
2585
2599
|
}
|
|
2586
2600
|
|
|
2587
|
-
inline NAPI_NO_RETURN void Error::Fatal(const char* location,
|
|
2601
|
+
inline NAPI_NO_RETURN void Error::Fatal(const char* location,
|
|
2602
|
+
const char* message) {
|
|
2588
2603
|
napi_fatal_error(location, NAPI_AUTO_LENGTH, message, NAPI_AUTO_LENGTH);
|
|
2589
2604
|
}
|
|
2590
2605
|
|
|
2591
|
-
inline Error::Error() : ObjectReference() {
|
|
2592
|
-
}
|
|
2606
|
+
inline Error::Error() : ObjectReference() {}
|
|
2593
2607
|
|
|
2594
|
-
inline Error::Error(napi_env env, napi_value value)
|
|
2608
|
+
inline Error::Error(napi_env env, napi_value value)
|
|
2609
|
+
: ObjectReference(env, nullptr) {
|
|
2595
2610
|
if (value != nullptr) {
|
|
2596
2611
|
// Attempting to create a reference on the error object.
|
|
2597
2612
|
// If it's not a Object/Function/Symbol, this call will return an error
|
|
@@ -2607,8 +2622,8 @@ inline Error::Error(napi_env env, napi_value value) : ObjectReference(env, nullp
|
|
|
2607
2622
|
|
|
2608
2623
|
// property flag that we attach to show the error object is wrapped
|
|
2609
2624
|
napi_property_descriptor wrapObjFlag = {
|
|
2610
|
-
ERROR_WRAP_VALUE, // Unique GUID identifier since Symbol isn't a
|
|
2611
|
-
|
|
2625
|
+
ERROR_WRAP_VALUE(), // Unique GUID identifier since Symbol isn't a
|
|
2626
|
+
// viable option
|
|
2612
2627
|
nullptr,
|
|
2613
2628
|
nullptr,
|
|
2614
2629
|
nullptr,
|
|
@@ -2648,15 +2663,17 @@ inline Object Error::Value() const {
|
|
|
2648
2663
|
// We are checking if the object is wrapped
|
|
2649
2664
|
bool isWrappedObject = false;
|
|
2650
2665
|
|
|
2651
|
-
status = napi_has_property(
|
|
2652
|
-
|
|
2666
|
+
status = napi_has_property(_env,
|
|
2667
|
+
refValue,
|
|
2668
|
+
String::From(_env, ERROR_WRAP_VALUE()),
|
|
2669
|
+
&isWrappedObject);
|
|
2653
2670
|
|
|
2654
2671
|
// Don't care about status
|
|
2655
2672
|
if (isWrappedObject) {
|
|
2656
2673
|
napi_value unwrappedValue;
|
|
2657
2674
|
status = napi_get_property(_env,
|
|
2658
2675
|
refValue,
|
|
2659
|
-
String::From(_env, ERROR_WRAP_VALUE),
|
|
2676
|
+
String::From(_env, ERROR_WRAP_VALUE()),
|
|
2660
2677
|
&unwrappedValue);
|
|
2661
2678
|
NAPI_THROW_IF_FAILED(_env, status, Object());
|
|
2662
2679
|
|
|
@@ -2667,18 +2684,16 @@ inline Object Error::Value() const {
|
|
|
2667
2684
|
return Object(_env, refValue);
|
|
2668
2685
|
}
|
|
2669
2686
|
|
|
2670
|
-
inline Error::Error(Error&& other) : ObjectReference(std::move(other)) {
|
|
2671
|
-
}
|
|
2687
|
+
inline Error::Error(Error&& other) : ObjectReference(std::move(other)) {}
|
|
2672
2688
|
|
|
2673
|
-
inline Error& Error::operator
|
|
2689
|
+
inline Error& Error::operator=(Error&& other) {
|
|
2674
2690
|
static_cast<Reference<Object>*>(this)->operator=(std::move(other));
|
|
2675
2691
|
return *this;
|
|
2676
2692
|
}
|
|
2677
2693
|
|
|
2678
|
-
inline Error::Error(const Error& other) : ObjectReference(other) {
|
|
2679
|
-
}
|
|
2694
|
+
inline Error::Error(const Error& other) : ObjectReference(other) {}
|
|
2680
2695
|
|
|
2681
|
-
inline Error& Error::operator
|
|
2696
|
+
inline Error& Error::operator=(const Error& other) {
|
|
2682
2697
|
Reset();
|
|
2683
2698
|
|
|
2684
2699
|
_env = other.Env();
|
|
@@ -2698,12 +2713,11 @@ inline const std::string& Error::Message() const NAPI_NOEXCEPT {
|
|
|
2698
2713
|
#ifdef NAPI_CPP_EXCEPTIONS
|
|
2699
2714
|
try {
|
|
2700
2715
|
_message = Get("message").As<String>();
|
|
2701
|
-
}
|
|
2702
|
-
catch (...) {
|
|
2716
|
+
} catch (...) {
|
|
2703
2717
|
// Catch all errors here, to include e.g. a std::bad_alloc from
|
|
2704
2718
|
// the std::string::operator=, because this method may not throw.
|
|
2705
2719
|
}
|
|
2706
|
-
#else
|
|
2720
|
+
#else // NAPI_CPP_EXCEPTIONS
|
|
2707
2721
|
#if defined(NODE_ADDON_API_ENABLE_MAYBE)
|
|
2708
2722
|
Napi::Value message_val;
|
|
2709
2723
|
if (Get("message").UnwrapTo(&message_val)) {
|
|
@@ -2712,7 +2726,7 @@ inline const std::string& Error::Message() const NAPI_NOEXCEPT {
|
|
|
2712
2726
|
#else
|
|
2713
2727
|
_message = Get("message").As<String>();
|
|
2714
2728
|
#endif
|
|
2715
|
-
#endif
|
|
2729
|
+
#endif // NAPI_CPP_EXCEPTIONS
|
|
2716
2730
|
}
|
|
2717
2731
|
return _message;
|
|
2718
2732
|
}
|
|
@@ -2758,9 +2772,10 @@ inline void Error::ThrowAsJavaScriptException() const {
|
|
|
2758
2772
|
if (status != napi_ok) {
|
|
2759
2773
|
throw Error::New(_env);
|
|
2760
2774
|
}
|
|
2761
|
-
#else
|
|
2762
|
-
NAPI_FATAL_IF_FAILED(
|
|
2763
|
-
|
|
2775
|
+
#else // NAPI_CPP_EXCEPTIONS
|
|
2776
|
+
NAPI_FATAL_IF_FAILED(
|
|
2777
|
+
status, "Error::ThrowAsJavaScriptException", "napi_throw");
|
|
2778
|
+
#endif // NAPI_CPP_EXCEPTIONS
|
|
2764
2779
|
}
|
|
2765
2780
|
}
|
|
2766
2781
|
|
|
@@ -2770,7 +2785,11 @@ inline const char* Error::what() const NAPI_NOEXCEPT {
|
|
|
2770
2785
|
return Message().c_str();
|
|
2771
2786
|
}
|
|
2772
2787
|
|
|
2773
|
-
#endif
|
|
2788
|
+
#endif // NAPI_CPP_EXCEPTIONS
|
|
2789
|
+
|
|
2790
|
+
inline const char* Error::ERROR_WRAP_VALUE() NAPI_NOEXCEPT {
|
|
2791
|
+
return "4bda9e7e-4913-4dbc-95de-891cbf66598e-errorVal";
|
|
2792
|
+
}
|
|
2774
2793
|
|
|
2775
2794
|
template <typename TError>
|
|
2776
2795
|
inline TError Error::New(napi_env env,
|
|
@@ -2789,39 +2808,42 @@ inline TError Error::New(napi_env env,
|
|
|
2789
2808
|
}
|
|
2790
2809
|
|
|
2791
2810
|
inline TypeError TypeError::New(napi_env env, const char* message) {
|
|
2792
|
-
return Error::New<TypeError>(
|
|
2811
|
+
return Error::New<TypeError>(
|
|
2812
|
+
env, message, std::strlen(message), napi_create_type_error);
|
|
2793
2813
|
}
|
|
2794
2814
|
|
|
2795
2815
|
inline TypeError TypeError::New(napi_env env, const std::string& message) {
|
|
2796
|
-
return Error::New<TypeError>(
|
|
2816
|
+
return Error::New<TypeError>(
|
|
2817
|
+
env, message.c_str(), message.size(), napi_create_type_error);
|
|
2797
2818
|
}
|
|
2798
2819
|
|
|
2799
|
-
inline TypeError::TypeError() : Error() {
|
|
2800
|
-
}
|
|
2820
|
+
inline TypeError::TypeError() : Error() {}
|
|
2801
2821
|
|
|
2802
|
-
inline TypeError::TypeError(napi_env env, napi_value value)
|
|
2803
|
-
}
|
|
2822
|
+
inline TypeError::TypeError(napi_env env, napi_value value)
|
|
2823
|
+
: Error(env, value) {}
|
|
2804
2824
|
|
|
2805
2825
|
inline RangeError RangeError::New(napi_env env, const char* message) {
|
|
2806
|
-
return Error::New<RangeError>(
|
|
2826
|
+
return Error::New<RangeError>(
|
|
2827
|
+
env, message, std::strlen(message), napi_create_range_error);
|
|
2807
2828
|
}
|
|
2808
2829
|
|
|
2809
2830
|
inline RangeError RangeError::New(napi_env env, const std::string& message) {
|
|
2810
|
-
return Error::New<RangeError>(
|
|
2831
|
+
return Error::New<RangeError>(
|
|
2832
|
+
env, message.c_str(), message.size(), napi_create_range_error);
|
|
2811
2833
|
}
|
|
2812
2834
|
|
|
2813
|
-
inline RangeError::RangeError() : Error() {
|
|
2814
|
-
}
|
|
2835
|
+
inline RangeError::RangeError() : Error() {}
|
|
2815
2836
|
|
|
2816
|
-
inline RangeError::RangeError(napi_env env, napi_value value)
|
|
2817
|
-
}
|
|
2837
|
+
inline RangeError::RangeError(napi_env env, napi_value value)
|
|
2838
|
+
: Error(env, value) {}
|
|
2818
2839
|
|
|
2819
2840
|
////////////////////////////////////////////////////////////////////////////////
|
|
2820
2841
|
// Reference<T> class
|
|
2821
2842
|
////////////////////////////////////////////////////////////////////////////////
|
|
2822
2843
|
|
|
2823
2844
|
template <typename T>
|
|
2824
|
-
inline Reference<T> Reference<T>::New(const T& value,
|
|
2845
|
+
inline Reference<T> Reference<T>::New(const T& value,
|
|
2846
|
+
uint32_t initialRefcount) {
|
|
2825
2847
|
napi_env env = value.Env();
|
|
2826
2848
|
napi_value val = value;
|
|
2827
2849
|
|
|
@@ -2836,15 +2858,13 @@ inline Reference<T> Reference<T>::New(const T& value, uint32_t initialRefcount)
|
|
|
2836
2858
|
return Reference<T>(env, ref);
|
|
2837
2859
|
}
|
|
2838
2860
|
|
|
2839
|
-
|
|
2840
2861
|
template <typename T>
|
|
2841
|
-
inline Reference<T>::Reference()
|
|
2842
|
-
}
|
|
2862
|
+
inline Reference<T>::Reference()
|
|
2863
|
+
: _env(nullptr), _ref(nullptr), _suppressDestruct(false) {}
|
|
2843
2864
|
|
|
2844
2865
|
template <typename T>
|
|
2845
2866
|
inline Reference<T>::Reference(napi_env env, napi_ref ref)
|
|
2846
|
-
|
|
2847
|
-
}
|
|
2867
|
+
: _env(env), _ref(ref), _suppressDestruct(false) {}
|
|
2848
2868
|
|
|
2849
2869
|
template <typename T>
|
|
2850
2870
|
inline Reference<T>::~Reference() {
|
|
@@ -2859,14 +2879,16 @@ inline Reference<T>::~Reference() {
|
|
|
2859
2879
|
|
|
2860
2880
|
template <typename T>
|
|
2861
2881
|
inline Reference<T>::Reference(Reference<T>&& other)
|
|
2862
|
-
|
|
2882
|
+
: _env(other._env),
|
|
2883
|
+
_ref(other._ref),
|
|
2884
|
+
_suppressDestruct(other._suppressDestruct) {
|
|
2863
2885
|
other._env = nullptr;
|
|
2864
2886
|
other._ref = nullptr;
|
|
2865
2887
|
other._suppressDestruct = false;
|
|
2866
2888
|
}
|
|
2867
2889
|
|
|
2868
2890
|
template <typename T>
|
|
2869
|
-
inline Reference<T>& Reference<T>::operator
|
|
2891
|
+
inline Reference<T>& Reference<T>::operator=(Reference<T>&& other) {
|
|
2870
2892
|
Reset();
|
|
2871
2893
|
_env = other._env;
|
|
2872
2894
|
_ref = other._ref;
|
|
@@ -2879,15 +2901,17 @@ inline Reference<T>& Reference<T>::operator =(Reference<T>&& other) {
|
|
|
2879
2901
|
|
|
2880
2902
|
template <typename T>
|
|
2881
2903
|
inline Reference<T>::Reference(const Reference<T>& other)
|
|
2882
|
-
|
|
2904
|
+
: _env(other._env), _ref(nullptr), _suppressDestruct(false) {
|
|
2883
2905
|
HandleScope scope(_env);
|
|
2884
2906
|
|
|
2885
2907
|
napi_value value = other.Value();
|
|
2886
2908
|
if (value != nullptr) {
|
|
2887
|
-
// Copying is a limited scenario (currently only used for Error object) and
|
|
2888
|
-
// strong reference to the given value even if the incoming
|
|
2909
|
+
// Copying is a limited scenario (currently only used for Error object) and
|
|
2910
|
+
// always creates a strong reference to the given value even if the incoming
|
|
2911
|
+
// reference is weak.
|
|
2889
2912
|
napi_status status = napi_create_reference(_env, value, 1, &_ref);
|
|
2890
|
-
NAPI_FATAL_IF_FAILED(
|
|
2913
|
+
NAPI_FATAL_IF_FAILED(
|
|
2914
|
+
status, "Reference<T>::Reference", "napi_create_reference");
|
|
2891
2915
|
}
|
|
2892
2916
|
}
|
|
2893
2917
|
|
|
@@ -2897,14 +2921,14 @@ inline Reference<T>::operator napi_ref() const {
|
|
|
2897
2921
|
}
|
|
2898
2922
|
|
|
2899
2923
|
template <typename T>
|
|
2900
|
-
inline bool Reference<T>::operator
|
|
2924
|
+
inline bool Reference<T>::operator==(const Reference<T>& other) const {
|
|
2901
2925
|
HandleScope scope(_env);
|
|
2902
2926
|
return this->Value().StrictEquals(other.Value());
|
|
2903
2927
|
}
|
|
2904
2928
|
|
|
2905
2929
|
template <typename T>
|
|
2906
|
-
inline bool Reference<T>::operator
|
|
2907
|
-
return !this->operator
|
|
2930
|
+
inline bool Reference<T>::operator!=(const Reference<T>& other) const {
|
|
2931
|
+
return !this->operator==(other);
|
|
2908
2932
|
}
|
|
2909
2933
|
|
|
2910
2934
|
template <typename T>
|
|
@@ -2930,7 +2954,7 @@ inline T Reference<T>::Value() const {
|
|
|
2930
2954
|
}
|
|
2931
2955
|
|
|
2932
2956
|
template <typename T>
|
|
2933
|
-
inline uint32_t Reference<T>::Ref() {
|
|
2957
|
+
inline uint32_t Reference<T>::Ref() const {
|
|
2934
2958
|
uint32_t result;
|
|
2935
2959
|
napi_status status = napi_reference_ref(_env, _ref, &result);
|
|
2936
2960
|
NAPI_THROW_IF_FAILED(_env, status, 0);
|
|
@@ -2938,7 +2962,7 @@ inline uint32_t Reference<T>::Ref() {
|
|
|
2938
2962
|
}
|
|
2939
2963
|
|
|
2940
2964
|
template <typename T>
|
|
2941
|
-
inline uint32_t Reference<T>::Unref() {
|
|
2965
|
+
inline uint32_t Reference<T>::Unref() const {
|
|
2942
2966
|
uint32_t result;
|
|
2943
2967
|
napi_status status = napi_reference_unref(_env, _ref, &result);
|
|
2944
2968
|
NAPI_THROW_IF_FAILED(_env, status, 0);
|
|
@@ -3001,33 +3025,29 @@ inline FunctionReference Persistent(Function value) {
|
|
|
3001
3025
|
// ObjectReference class
|
|
3002
3026
|
////////////////////////////////////////////////////////////////////////////////
|
|
3003
3027
|
|
|
3004
|
-
inline ObjectReference::ObjectReference(): Reference<Object>() {
|
|
3005
|
-
}
|
|
3028
|
+
inline ObjectReference::ObjectReference() : Reference<Object>() {}
|
|
3006
3029
|
|
|
3007
|
-
inline ObjectReference::ObjectReference(napi_env env, napi_ref ref)
|
|
3008
|
-
}
|
|
3030
|
+
inline ObjectReference::ObjectReference(napi_env env, napi_ref ref)
|
|
3031
|
+
: Reference<Object>(env, ref) {}
|
|
3009
3032
|
|
|
3010
3033
|
inline ObjectReference::ObjectReference(Reference<Object>&& other)
|
|
3011
|
-
|
|
3012
|
-
}
|
|
3034
|
+
: Reference<Object>(std::move(other)) {}
|
|
3013
3035
|
|
|
3014
|
-
inline ObjectReference& ObjectReference::operator
|
|
3036
|
+
inline ObjectReference& ObjectReference::operator=(Reference<Object>&& other) {
|
|
3015
3037
|
static_cast<Reference<Object>*>(this)->operator=(std::move(other));
|
|
3016
3038
|
return *this;
|
|
3017
3039
|
}
|
|
3018
3040
|
|
|
3019
3041
|
inline ObjectReference::ObjectReference(ObjectReference&& other)
|
|
3020
|
-
|
|
3021
|
-
}
|
|
3042
|
+
: Reference<Object>(std::move(other)) {}
|
|
3022
3043
|
|
|
3023
|
-
inline ObjectReference& ObjectReference::operator
|
|
3044
|
+
inline ObjectReference& ObjectReference::operator=(ObjectReference&& other) {
|
|
3024
3045
|
static_cast<Reference<Object>*>(this)->operator=(std::move(other));
|
|
3025
3046
|
return *this;
|
|
3026
3047
|
}
|
|
3027
3048
|
|
|
3028
3049
|
inline ObjectReference::ObjectReference(const ObjectReference& other)
|
|
3029
|
-
|
|
3030
|
-
}
|
|
3050
|
+
: Reference<Object>(other) {}
|
|
3031
3051
|
|
|
3032
3052
|
inline MaybeOrValue<Napi::Value> ObjectReference::Get(
|
|
3033
3053
|
const char* utf8name) const {
|
|
@@ -3064,61 +3084,61 @@ inline MaybeOrValue<Napi::Value> ObjectReference::Get(
|
|
|
3064
3084
|
}
|
|
3065
3085
|
|
|
3066
3086
|
inline MaybeOrValue<bool> ObjectReference::Set(const char* utf8name,
|
|
3067
|
-
napi_value value) {
|
|
3087
|
+
napi_value value) const {
|
|
3068
3088
|
HandleScope scope(_env);
|
|
3069
3089
|
return Value().Set(utf8name, value);
|
|
3070
3090
|
}
|
|
3071
3091
|
|
|
3072
3092
|
inline MaybeOrValue<bool> ObjectReference::Set(const char* utf8name,
|
|
3073
|
-
Napi::Value value) {
|
|
3093
|
+
Napi::Value value) const {
|
|
3074
3094
|
HandleScope scope(_env);
|
|
3075
3095
|
return Value().Set(utf8name, value);
|
|
3076
3096
|
}
|
|
3077
3097
|
|
|
3078
3098
|
inline MaybeOrValue<bool> ObjectReference::Set(const char* utf8name,
|
|
3079
|
-
const char* utf8value) {
|
|
3099
|
+
const char* utf8value) const {
|
|
3080
3100
|
HandleScope scope(_env);
|
|
3081
3101
|
return Value().Set(utf8name, utf8value);
|
|
3082
3102
|
}
|
|
3083
3103
|
|
|
3084
3104
|
inline MaybeOrValue<bool> ObjectReference::Set(const char* utf8name,
|
|
3085
|
-
bool boolValue) {
|
|
3105
|
+
bool boolValue) const {
|
|
3086
3106
|
HandleScope scope(_env);
|
|
3087
3107
|
return Value().Set(utf8name, boolValue);
|
|
3088
3108
|
}
|
|
3089
3109
|
|
|
3090
3110
|
inline MaybeOrValue<bool> ObjectReference::Set(const char* utf8name,
|
|
3091
|
-
double numberValue) {
|
|
3111
|
+
double numberValue) const {
|
|
3092
3112
|
HandleScope scope(_env);
|
|
3093
3113
|
return Value().Set(utf8name, numberValue);
|
|
3094
3114
|
}
|
|
3095
3115
|
|
|
3096
3116
|
inline MaybeOrValue<bool> ObjectReference::Set(const std::string& utf8name,
|
|
3097
|
-
napi_value value) {
|
|
3117
|
+
napi_value value) const {
|
|
3098
3118
|
HandleScope scope(_env);
|
|
3099
3119
|
return Value().Set(utf8name, value);
|
|
3100
3120
|
}
|
|
3101
3121
|
|
|
3102
3122
|
inline MaybeOrValue<bool> ObjectReference::Set(const std::string& utf8name,
|
|
3103
|
-
Napi::Value value) {
|
|
3123
|
+
Napi::Value value) const {
|
|
3104
3124
|
HandleScope scope(_env);
|
|
3105
3125
|
return Value().Set(utf8name, value);
|
|
3106
3126
|
}
|
|
3107
3127
|
|
|
3108
3128
|
inline MaybeOrValue<bool> ObjectReference::Set(const std::string& utf8name,
|
|
3109
|
-
std::string& utf8value) {
|
|
3129
|
+
std::string& utf8value) const {
|
|
3110
3130
|
HandleScope scope(_env);
|
|
3111
3131
|
return Value().Set(utf8name, utf8value);
|
|
3112
3132
|
}
|
|
3113
3133
|
|
|
3114
3134
|
inline MaybeOrValue<bool> ObjectReference::Set(const std::string& utf8name,
|
|
3115
|
-
bool boolValue) {
|
|
3135
|
+
bool boolValue) const {
|
|
3116
3136
|
HandleScope scope(_env);
|
|
3117
3137
|
return Value().Set(utf8name, boolValue);
|
|
3118
3138
|
}
|
|
3119
3139
|
|
|
3120
3140
|
inline MaybeOrValue<bool> ObjectReference::Set(const std::string& utf8name,
|
|
3121
|
-
double numberValue) {
|
|
3141
|
+
double numberValue) const {
|
|
3122
3142
|
HandleScope scope(_env);
|
|
3123
3143
|
return Value().Set(utf8name, numberValue);
|
|
3124
3144
|
}
|
|
@@ -3140,36 +3160,37 @@ inline MaybeOrValue<Napi::Value> ObjectReference::Get(uint32_t index) const {
|
|
|
3140
3160
|
}
|
|
3141
3161
|
|
|
3142
3162
|
inline MaybeOrValue<bool> ObjectReference::Set(uint32_t index,
|
|
3143
|
-
napi_value value) {
|
|
3163
|
+
napi_value value) const {
|
|
3144
3164
|
HandleScope scope(_env);
|
|
3145
3165
|
return Value().Set(index, value);
|
|
3146
3166
|
}
|
|
3147
3167
|
|
|
3148
3168
|
inline MaybeOrValue<bool> ObjectReference::Set(uint32_t index,
|
|
3149
|
-
Napi::Value value) {
|
|
3169
|
+
Napi::Value value) const {
|
|
3150
3170
|
HandleScope scope(_env);
|
|
3151
3171
|
return Value().Set(index, value);
|
|
3152
3172
|
}
|
|
3153
3173
|
|
|
3154
3174
|
inline MaybeOrValue<bool> ObjectReference::Set(uint32_t index,
|
|
3155
|
-
const char* utf8value) {
|
|
3175
|
+
const char* utf8value) const {
|
|
3156
3176
|
HandleScope scope(_env);
|
|
3157
3177
|
return Value().Set(index, utf8value);
|
|
3158
3178
|
}
|
|
3159
3179
|
|
|
3160
|
-
inline MaybeOrValue<bool> ObjectReference::Set(
|
|
3161
|
-
|
|
3180
|
+
inline MaybeOrValue<bool> ObjectReference::Set(
|
|
3181
|
+
uint32_t index, const std::string& utf8value) const {
|
|
3162
3182
|
HandleScope scope(_env);
|
|
3163
3183
|
return Value().Set(index, utf8value);
|
|
3164
3184
|
}
|
|
3165
3185
|
|
|
3166
|
-
inline MaybeOrValue<bool> ObjectReference::Set(uint32_t index,
|
|
3186
|
+
inline MaybeOrValue<bool> ObjectReference::Set(uint32_t index,
|
|
3187
|
+
bool boolValue) const {
|
|
3167
3188
|
HandleScope scope(_env);
|
|
3168
3189
|
return Value().Set(index, boolValue);
|
|
3169
3190
|
}
|
|
3170
3191
|
|
|
3171
3192
|
inline MaybeOrValue<bool> ObjectReference::Set(uint32_t index,
|
|
3172
|
-
double numberValue) {
|
|
3193
|
+
double numberValue) const {
|
|
3173
3194
|
HandleScope scope(_env);
|
|
3174
3195
|
return Value().Set(index, numberValue);
|
|
3175
3196
|
}
|
|
@@ -3178,27 +3199,25 @@ inline MaybeOrValue<bool> ObjectReference::Set(uint32_t index,
|
|
|
3178
3199
|
// FunctionReference class
|
|
3179
3200
|
////////////////////////////////////////////////////////////////////////////////
|
|
3180
3201
|
|
|
3181
|
-
inline FunctionReference::FunctionReference(): Reference<Function>() {
|
|
3182
|
-
}
|
|
3202
|
+
inline FunctionReference::FunctionReference() : Reference<Function>() {}
|
|
3183
3203
|
|
|
3184
3204
|
inline FunctionReference::FunctionReference(napi_env env, napi_ref ref)
|
|
3185
|
-
|
|
3186
|
-
}
|
|
3205
|
+
: Reference<Function>(env, ref) {}
|
|
3187
3206
|
|
|
3188
3207
|
inline FunctionReference::FunctionReference(Reference<Function>&& other)
|
|
3189
|
-
|
|
3190
|
-
}
|
|
3208
|
+
: Reference<Function>(std::move(other)) {}
|
|
3191
3209
|
|
|
3192
|
-
inline FunctionReference& FunctionReference::operator
|
|
3210
|
+
inline FunctionReference& FunctionReference::operator=(
|
|
3211
|
+
Reference<Function>&& other) {
|
|
3193
3212
|
static_cast<Reference<Function>*>(this)->operator=(std::move(other));
|
|
3194
3213
|
return *this;
|
|
3195
3214
|
}
|
|
3196
3215
|
|
|
3197
3216
|
inline FunctionReference::FunctionReference(FunctionReference&& other)
|
|
3198
|
-
|
|
3199
|
-
}
|
|
3217
|
+
: Reference<Function>(std::move(other)) {}
|
|
3200
3218
|
|
|
3201
|
-
inline FunctionReference& FunctionReference::operator
|
|
3219
|
+
inline FunctionReference& FunctionReference::operator=(
|
|
3220
|
+
FunctionReference&& other) {
|
|
3202
3221
|
static_cast<Reference<Function>*>(this)->operator=(std::move(other));
|
|
3203
3222
|
return *this;
|
|
3204
3223
|
}
|
|
@@ -3404,10 +3423,15 @@ inline MaybeOrValue<Object> FunctionReference::New(
|
|
|
3404
3423
|
////////////////////////////////////////////////////////////////////////////////
|
|
3405
3424
|
|
|
3406
3425
|
inline CallbackInfo::CallbackInfo(napi_env env, napi_callback_info info)
|
|
3407
|
-
: _env(env),
|
|
3426
|
+
: _env(env),
|
|
3427
|
+
_info(info),
|
|
3428
|
+
_this(nullptr),
|
|
3429
|
+
_dynamicArgs(nullptr),
|
|
3430
|
+
_data(nullptr) {
|
|
3408
3431
|
_argc = _staticArgCount;
|
|
3409
3432
|
_argv = _staticArgs;
|
|
3410
|
-
napi_status status =
|
|
3433
|
+
napi_status status =
|
|
3434
|
+
napi_get_cb_info(env, info, &_argc, _argv, &_this, &_data);
|
|
3411
3435
|
NAPI_THROW_IF_FAILED_VOID(_env, status);
|
|
3412
3436
|
|
|
3413
3437
|
if (_argc > _staticArgCount) {
|
|
@@ -3427,6 +3451,10 @@ inline CallbackInfo::~CallbackInfo() {
|
|
|
3427
3451
|
}
|
|
3428
3452
|
}
|
|
3429
3453
|
|
|
3454
|
+
inline CallbackInfo::operator napi_callback_info() const {
|
|
3455
|
+
return _info;
|
|
3456
|
+
}
|
|
3457
|
+
|
|
3430
3458
|
inline Value CallbackInfo::NewTarget() const {
|
|
3431
3459
|
napi_value newTarget;
|
|
3432
3460
|
napi_status status = napi_get_new_target(_env, _info, &newTarget);
|
|
@@ -3446,7 +3474,7 @@ inline size_t CallbackInfo::Length() const {
|
|
|
3446
3474
|
return _argc;
|
|
3447
3475
|
}
|
|
3448
3476
|
|
|
3449
|
-
inline const Value CallbackInfo::operator
|
|
3477
|
+
inline const Value CallbackInfo::operator[](size_t index) const {
|
|
3450
3478
|
return index < _argc ? Value(_env, _argv[index]) : Env().Undefined();
|
|
3451
3479
|
}
|
|
3452
3480
|
|
|
@@ -3470,10 +3498,8 @@ inline void CallbackInfo::SetData(void* data) {
|
|
|
3470
3498
|
////////////////////////////////////////////////////////////////////////////////
|
|
3471
3499
|
|
|
3472
3500
|
template <typename PropertyDescriptor::GetterCallback Getter>
|
|
3473
|
-
PropertyDescriptor
|
|
3474
|
-
|
|
3475
|
-
napi_property_attributes attributes,
|
|
3476
|
-
void* data) {
|
|
3501
|
+
PropertyDescriptor PropertyDescriptor::Accessor(
|
|
3502
|
+
const char* utf8name, napi_property_attributes attributes, void* data) {
|
|
3477
3503
|
napi_property_descriptor desc = napi_property_descriptor();
|
|
3478
3504
|
|
|
3479
3505
|
desc.utf8name = utf8name;
|
|
@@ -3485,18 +3511,16 @@ PropertyDescriptor::Accessor(const char* utf8name,
|
|
|
3485
3511
|
}
|
|
3486
3512
|
|
|
3487
3513
|
template <typename PropertyDescriptor::GetterCallback Getter>
|
|
3488
|
-
PropertyDescriptor
|
|
3489
|
-
|
|
3490
|
-
|
|
3491
|
-
|
|
3514
|
+
PropertyDescriptor PropertyDescriptor::Accessor(
|
|
3515
|
+
const std::string& utf8name,
|
|
3516
|
+
napi_property_attributes attributes,
|
|
3517
|
+
void* data) {
|
|
3492
3518
|
return Accessor<Getter>(utf8name.c_str(), attributes, data);
|
|
3493
3519
|
}
|
|
3494
3520
|
|
|
3495
3521
|
template <typename PropertyDescriptor::GetterCallback Getter>
|
|
3496
|
-
PropertyDescriptor
|
|
3497
|
-
|
|
3498
|
-
napi_property_attributes attributes,
|
|
3499
|
-
void* data) {
|
|
3522
|
+
PropertyDescriptor PropertyDescriptor::Accessor(
|
|
3523
|
+
Name name, napi_property_attributes attributes, void* data) {
|
|
3500
3524
|
napi_property_descriptor desc = napi_property_descriptor();
|
|
3501
3525
|
|
|
3502
3526
|
desc.name = name;
|
|
@@ -3507,14 +3531,10 @@ PropertyDescriptor::Accessor(Name name,
|
|
|
3507
3531
|
return desc;
|
|
3508
3532
|
}
|
|
3509
3533
|
|
|
3510
|
-
template <
|
|
3511
|
-
typename PropertyDescriptor::
|
|
3512
|
-
|
|
3513
|
-
|
|
3514
|
-
PropertyDescriptor::Accessor(const char* utf8name,
|
|
3515
|
-
napi_property_attributes attributes,
|
|
3516
|
-
void* data) {
|
|
3517
|
-
|
|
3534
|
+
template <typename PropertyDescriptor::GetterCallback Getter,
|
|
3535
|
+
typename PropertyDescriptor::SetterCallback Setter>
|
|
3536
|
+
PropertyDescriptor PropertyDescriptor::Accessor(
|
|
3537
|
+
const char* utf8name, napi_property_attributes attributes, void* data) {
|
|
3518
3538
|
napi_property_descriptor desc = napi_property_descriptor();
|
|
3519
3539
|
|
|
3520
3540
|
desc.utf8name = utf8name;
|
|
@@ -3526,23 +3546,19 @@ PropertyDescriptor::Accessor(const char* utf8name,
|
|
|
3526
3546
|
return desc;
|
|
3527
3547
|
}
|
|
3528
3548
|
|
|
3529
|
-
template <
|
|
3530
|
-
typename PropertyDescriptor::
|
|
3531
|
-
|
|
3532
|
-
|
|
3533
|
-
|
|
3534
|
-
|
|
3535
|
-
void* data) {
|
|
3549
|
+
template <typename PropertyDescriptor::GetterCallback Getter,
|
|
3550
|
+
typename PropertyDescriptor::SetterCallback Setter>
|
|
3551
|
+
PropertyDescriptor PropertyDescriptor::Accessor(
|
|
3552
|
+
const std::string& utf8name,
|
|
3553
|
+
napi_property_attributes attributes,
|
|
3554
|
+
void* data) {
|
|
3536
3555
|
return Accessor<Getter, Setter>(utf8name.c_str(), attributes, data);
|
|
3537
3556
|
}
|
|
3538
3557
|
|
|
3539
|
-
template <
|
|
3540
|
-
typename PropertyDescriptor::
|
|
3541
|
-
|
|
3542
|
-
|
|
3543
|
-
PropertyDescriptor::Accessor(Name name,
|
|
3544
|
-
napi_property_attributes attributes,
|
|
3545
|
-
void* data) {
|
|
3558
|
+
template <typename PropertyDescriptor::GetterCallback Getter,
|
|
3559
|
+
typename PropertyDescriptor::SetterCallback Setter>
|
|
3560
|
+
PropertyDescriptor PropertyDescriptor::Accessor(
|
|
3561
|
+
Name name, napi_property_attributes attributes, void* data) {
|
|
3546
3562
|
napi_property_descriptor desc = napi_property_descriptor();
|
|
3547
3563
|
|
|
3548
3564
|
desc.name = name;
|
|
@@ -3555,15 +3571,15 @@ PropertyDescriptor::Accessor(Name name,
|
|
|
3555
3571
|
}
|
|
3556
3572
|
|
|
3557
3573
|
template <typename Getter>
|
|
3558
|
-
inline PropertyDescriptor
|
|
3559
|
-
|
|
3560
|
-
|
|
3561
|
-
|
|
3562
|
-
|
|
3563
|
-
|
|
3564
|
-
|
|
3574
|
+
inline PropertyDescriptor PropertyDescriptor::Accessor(
|
|
3575
|
+
Napi::Env env,
|
|
3576
|
+
Napi::Object object,
|
|
3577
|
+
const char* utf8name,
|
|
3578
|
+
Getter getter,
|
|
3579
|
+
napi_property_attributes attributes,
|
|
3580
|
+
void* data) {
|
|
3565
3581
|
using CbData = details::CallbackData<Getter, Napi::Value>;
|
|
3566
|
-
auto callbackData = new CbData({
|
|
3582
|
+
auto callbackData = new CbData({getter, data});
|
|
3567
3583
|
|
|
3568
3584
|
napi_status status = AttachData(env, object, callbackData);
|
|
3569
3585
|
if (status != napi_ok) {
|
|
@@ -3571,37 +3587,37 @@ PropertyDescriptor::Accessor(Napi::Env env,
|
|
|
3571
3587
|
NAPI_THROW_IF_FAILED(env, status, napi_property_descriptor());
|
|
3572
3588
|
}
|
|
3573
3589
|
|
|
3574
|
-
return PropertyDescriptor({
|
|
3575
|
-
|
|
3576
|
-
|
|
3577
|
-
|
|
3578
|
-
|
|
3579
|
-
|
|
3580
|
-
|
|
3581
|
-
|
|
3582
|
-
callbackData
|
|
3583
|
-
});
|
|
3590
|
+
return PropertyDescriptor({utf8name,
|
|
3591
|
+
nullptr,
|
|
3592
|
+
nullptr,
|
|
3593
|
+
CbData::Wrapper,
|
|
3594
|
+
nullptr,
|
|
3595
|
+
nullptr,
|
|
3596
|
+
attributes,
|
|
3597
|
+
callbackData});
|
|
3584
3598
|
}
|
|
3585
3599
|
|
|
3586
3600
|
template <typename Getter>
|
|
3587
|
-
inline PropertyDescriptor PropertyDescriptor::Accessor(
|
|
3588
|
-
|
|
3589
|
-
|
|
3590
|
-
|
|
3591
|
-
|
|
3592
|
-
|
|
3601
|
+
inline PropertyDescriptor PropertyDescriptor::Accessor(
|
|
3602
|
+
Napi::Env env,
|
|
3603
|
+
Napi::Object object,
|
|
3604
|
+
const std::string& utf8name,
|
|
3605
|
+
Getter getter,
|
|
3606
|
+
napi_property_attributes attributes,
|
|
3607
|
+
void* data) {
|
|
3593
3608
|
return Accessor(env, object, utf8name.c_str(), getter, attributes, data);
|
|
3594
3609
|
}
|
|
3595
3610
|
|
|
3596
3611
|
template <typename Getter>
|
|
3597
|
-
inline PropertyDescriptor PropertyDescriptor::Accessor(
|
|
3598
|
-
|
|
3599
|
-
|
|
3600
|
-
|
|
3601
|
-
|
|
3602
|
-
|
|
3612
|
+
inline PropertyDescriptor PropertyDescriptor::Accessor(
|
|
3613
|
+
Napi::Env env,
|
|
3614
|
+
Napi::Object object,
|
|
3615
|
+
Name name,
|
|
3616
|
+
Getter getter,
|
|
3617
|
+
napi_property_attributes attributes,
|
|
3618
|
+
void* data) {
|
|
3603
3619
|
using CbData = details::CallbackData<Getter, Napi::Value>;
|
|
3604
|
-
auto callbackData = new CbData({
|
|
3620
|
+
auto callbackData = new CbData({getter, data});
|
|
3605
3621
|
|
|
3606
3622
|
napi_status status = AttachData(env, object, callbackData);
|
|
3607
3623
|
if (status != napi_ok) {
|
|
@@ -3609,28 +3625,27 @@ inline PropertyDescriptor PropertyDescriptor::Accessor(Napi::Env env,
|
|
|
3609
3625
|
NAPI_THROW_IF_FAILED(env, status, napi_property_descriptor());
|
|
3610
3626
|
}
|
|
3611
3627
|
|
|
3612
|
-
return PropertyDescriptor({
|
|
3613
|
-
|
|
3614
|
-
|
|
3615
|
-
|
|
3616
|
-
|
|
3617
|
-
|
|
3618
|
-
|
|
3619
|
-
|
|
3620
|
-
callbackData
|
|
3621
|
-
});
|
|
3628
|
+
return PropertyDescriptor({nullptr,
|
|
3629
|
+
name,
|
|
3630
|
+
nullptr,
|
|
3631
|
+
CbData::Wrapper,
|
|
3632
|
+
nullptr,
|
|
3633
|
+
nullptr,
|
|
3634
|
+
attributes,
|
|
3635
|
+
callbackData});
|
|
3622
3636
|
}
|
|
3623
3637
|
|
|
3624
3638
|
template <typename Getter, typename Setter>
|
|
3625
|
-
inline PropertyDescriptor PropertyDescriptor::Accessor(
|
|
3626
|
-
|
|
3627
|
-
|
|
3628
|
-
|
|
3629
|
-
|
|
3630
|
-
|
|
3631
|
-
|
|
3639
|
+
inline PropertyDescriptor PropertyDescriptor::Accessor(
|
|
3640
|
+
Napi::Env env,
|
|
3641
|
+
Napi::Object object,
|
|
3642
|
+
const char* utf8name,
|
|
3643
|
+
Getter getter,
|
|
3644
|
+
Setter setter,
|
|
3645
|
+
napi_property_attributes attributes,
|
|
3646
|
+
void* data) {
|
|
3632
3647
|
using CbData = details::AccessorCallbackData<Getter, Setter>;
|
|
3633
|
-
auto callbackData = new CbData({
|
|
3648
|
+
auto callbackData = new CbData({getter, setter, data});
|
|
3634
3649
|
|
|
3635
3650
|
napi_status status = AttachData(env, object, callbackData);
|
|
3636
3651
|
if (status != napi_ok) {
|
|
@@ -3638,39 +3653,40 @@ inline PropertyDescriptor PropertyDescriptor::Accessor(Napi::Env env,
|
|
|
3638
3653
|
NAPI_THROW_IF_FAILED(env, status, napi_property_descriptor());
|
|
3639
3654
|
}
|
|
3640
3655
|
|
|
3641
|
-
return PropertyDescriptor({
|
|
3642
|
-
|
|
3643
|
-
|
|
3644
|
-
|
|
3645
|
-
|
|
3646
|
-
|
|
3647
|
-
|
|
3648
|
-
|
|
3649
|
-
callbackData
|
|
3650
|
-
});
|
|
3656
|
+
return PropertyDescriptor({utf8name,
|
|
3657
|
+
nullptr,
|
|
3658
|
+
nullptr,
|
|
3659
|
+
CbData::GetterWrapper,
|
|
3660
|
+
CbData::SetterWrapper,
|
|
3661
|
+
nullptr,
|
|
3662
|
+
attributes,
|
|
3663
|
+
callbackData});
|
|
3651
3664
|
}
|
|
3652
3665
|
|
|
3653
3666
|
template <typename Getter, typename Setter>
|
|
3654
|
-
inline PropertyDescriptor PropertyDescriptor::Accessor(
|
|
3655
|
-
|
|
3656
|
-
|
|
3657
|
-
|
|
3658
|
-
|
|
3659
|
-
|
|
3660
|
-
|
|
3661
|
-
|
|
3667
|
+
inline PropertyDescriptor PropertyDescriptor::Accessor(
|
|
3668
|
+
Napi::Env env,
|
|
3669
|
+
Napi::Object object,
|
|
3670
|
+
const std::string& utf8name,
|
|
3671
|
+
Getter getter,
|
|
3672
|
+
Setter setter,
|
|
3673
|
+
napi_property_attributes attributes,
|
|
3674
|
+
void* data) {
|
|
3675
|
+
return Accessor(
|
|
3676
|
+
env, object, utf8name.c_str(), getter, setter, attributes, data);
|
|
3662
3677
|
}
|
|
3663
3678
|
|
|
3664
3679
|
template <typename Getter, typename Setter>
|
|
3665
|
-
inline PropertyDescriptor PropertyDescriptor::Accessor(
|
|
3666
|
-
|
|
3667
|
-
|
|
3668
|
-
|
|
3669
|
-
|
|
3670
|
-
|
|
3671
|
-
|
|
3680
|
+
inline PropertyDescriptor PropertyDescriptor::Accessor(
|
|
3681
|
+
Napi::Env env,
|
|
3682
|
+
Napi::Object object,
|
|
3683
|
+
Name name,
|
|
3684
|
+
Getter getter,
|
|
3685
|
+
Setter setter,
|
|
3686
|
+
napi_property_attributes attributes,
|
|
3687
|
+
void* data) {
|
|
3672
3688
|
using CbData = details::AccessorCallbackData<Getter, Setter>;
|
|
3673
|
-
auto callbackData = new CbData({
|
|
3689
|
+
auto callbackData = new CbData({getter, setter, data});
|
|
3674
3690
|
|
|
3675
3691
|
napi_status status = AttachData(env, object, callbackData);
|
|
3676
3692
|
if (status != napi_ok) {
|
|
@@ -3678,99 +3694,99 @@ inline PropertyDescriptor PropertyDescriptor::Accessor(Napi::Env env,
|
|
|
3678
3694
|
NAPI_THROW_IF_FAILED(env, status, napi_property_descriptor());
|
|
3679
3695
|
}
|
|
3680
3696
|
|
|
3681
|
-
return PropertyDescriptor({
|
|
3682
|
-
|
|
3683
|
-
|
|
3684
|
-
|
|
3685
|
-
|
|
3686
|
-
|
|
3687
|
-
|
|
3688
|
-
|
|
3689
|
-
callbackData
|
|
3690
|
-
});
|
|
3697
|
+
return PropertyDescriptor({nullptr,
|
|
3698
|
+
name,
|
|
3699
|
+
nullptr,
|
|
3700
|
+
CbData::GetterWrapper,
|
|
3701
|
+
CbData::SetterWrapper,
|
|
3702
|
+
nullptr,
|
|
3703
|
+
attributes,
|
|
3704
|
+
callbackData});
|
|
3691
3705
|
}
|
|
3692
3706
|
|
|
3693
3707
|
template <typename Callable>
|
|
3694
|
-
inline PropertyDescriptor PropertyDescriptor::Function(
|
|
3695
|
-
|
|
3696
|
-
|
|
3697
|
-
|
|
3698
|
-
|
|
3699
|
-
|
|
3700
|
-
|
|
3701
|
-
|
|
3702
|
-
|
|
3703
|
-
|
|
3704
|
-
|
|
3705
|
-
|
|
3706
|
-
|
|
3707
|
-
|
|
3708
|
-
|
|
3709
|
-
});
|
|
3708
|
+
inline PropertyDescriptor PropertyDescriptor::Function(
|
|
3709
|
+
Napi::Env env,
|
|
3710
|
+
Napi::Object /*object*/,
|
|
3711
|
+
const char* utf8name,
|
|
3712
|
+
Callable cb,
|
|
3713
|
+
napi_property_attributes attributes,
|
|
3714
|
+
void* data) {
|
|
3715
|
+
return PropertyDescriptor({utf8name,
|
|
3716
|
+
nullptr,
|
|
3717
|
+
nullptr,
|
|
3718
|
+
nullptr,
|
|
3719
|
+
nullptr,
|
|
3720
|
+
Napi::Function::New(env, cb, utf8name, data),
|
|
3721
|
+
attributes,
|
|
3722
|
+
nullptr});
|
|
3710
3723
|
}
|
|
3711
3724
|
|
|
3712
3725
|
template <typename Callable>
|
|
3713
|
-
inline PropertyDescriptor PropertyDescriptor::Function(
|
|
3714
|
-
|
|
3715
|
-
|
|
3716
|
-
|
|
3717
|
-
|
|
3718
|
-
|
|
3726
|
+
inline PropertyDescriptor PropertyDescriptor::Function(
|
|
3727
|
+
Napi::Env env,
|
|
3728
|
+
Napi::Object object,
|
|
3729
|
+
const std::string& utf8name,
|
|
3730
|
+
Callable cb,
|
|
3731
|
+
napi_property_attributes attributes,
|
|
3732
|
+
void* data) {
|
|
3719
3733
|
return Function(env, object, utf8name.c_str(), cb, attributes, data);
|
|
3720
3734
|
}
|
|
3721
3735
|
|
|
3722
3736
|
template <typename Callable>
|
|
3723
|
-
inline PropertyDescriptor PropertyDescriptor::Function(
|
|
3724
|
-
|
|
3725
|
-
|
|
3726
|
-
|
|
3727
|
-
|
|
3728
|
-
|
|
3729
|
-
|
|
3730
|
-
|
|
3731
|
-
|
|
3732
|
-
|
|
3733
|
-
|
|
3734
|
-
|
|
3735
|
-
|
|
3736
|
-
|
|
3737
|
-
|
|
3738
|
-
});
|
|
3739
|
-
}
|
|
3740
|
-
|
|
3741
|
-
inline PropertyDescriptor PropertyDescriptor::Value(const char* utf8name,
|
|
3742
|
-
napi_value value,
|
|
3743
|
-
napi_property_attributes attributes) {
|
|
3744
|
-
return PropertyDescriptor({
|
|
3745
|
-
utf8name, nullptr, nullptr, nullptr, nullptr, value, attributes, nullptr
|
|
3746
|
-
});
|
|
3737
|
+
inline PropertyDescriptor PropertyDescriptor::Function(
|
|
3738
|
+
Napi::Env env,
|
|
3739
|
+
Napi::Object /*object*/,
|
|
3740
|
+
Name name,
|
|
3741
|
+
Callable cb,
|
|
3742
|
+
napi_property_attributes attributes,
|
|
3743
|
+
void* data) {
|
|
3744
|
+
return PropertyDescriptor({nullptr,
|
|
3745
|
+
name,
|
|
3746
|
+
nullptr,
|
|
3747
|
+
nullptr,
|
|
3748
|
+
nullptr,
|
|
3749
|
+
Napi::Function::New(env, cb, nullptr, data),
|
|
3750
|
+
attributes,
|
|
3751
|
+
nullptr});
|
|
3747
3752
|
}
|
|
3748
3753
|
|
|
3749
|
-
inline PropertyDescriptor PropertyDescriptor::Value(
|
|
3750
|
-
|
|
3751
|
-
|
|
3754
|
+
inline PropertyDescriptor PropertyDescriptor::Value(
|
|
3755
|
+
const char* utf8name,
|
|
3756
|
+
napi_value value,
|
|
3757
|
+
napi_property_attributes attributes) {
|
|
3758
|
+
return PropertyDescriptor({utf8name,
|
|
3759
|
+
nullptr,
|
|
3760
|
+
nullptr,
|
|
3761
|
+
nullptr,
|
|
3762
|
+
nullptr,
|
|
3763
|
+
value,
|
|
3764
|
+
attributes,
|
|
3765
|
+
nullptr});
|
|
3766
|
+
}
|
|
3767
|
+
|
|
3768
|
+
inline PropertyDescriptor PropertyDescriptor::Value(
|
|
3769
|
+
const std::string& utf8name,
|
|
3770
|
+
napi_value value,
|
|
3771
|
+
napi_property_attributes attributes) {
|
|
3752
3772
|
return Value(utf8name.c_str(), value, attributes);
|
|
3753
3773
|
}
|
|
3754
3774
|
|
|
3755
|
-
inline PropertyDescriptor PropertyDescriptor::Value(
|
|
3756
|
-
|
|
3757
|
-
|
|
3758
|
-
|
|
3759
|
-
nullptr, name, nullptr, nullptr, nullptr, value, attributes, nullptr
|
|
3760
|
-
});
|
|
3775
|
+
inline PropertyDescriptor PropertyDescriptor::Value(
|
|
3776
|
+
napi_value name, napi_value value, napi_property_attributes attributes) {
|
|
3777
|
+
return PropertyDescriptor(
|
|
3778
|
+
{nullptr, name, nullptr, nullptr, nullptr, value, attributes, nullptr});
|
|
3761
3779
|
}
|
|
3762
3780
|
|
|
3763
|
-
inline PropertyDescriptor PropertyDescriptor::Value(
|
|
3764
|
-
|
|
3765
|
-
napi_property_attributes attributes) {
|
|
3781
|
+
inline PropertyDescriptor PropertyDescriptor::Value(
|
|
3782
|
+
Name name, Napi::Value value, napi_property_attributes attributes) {
|
|
3766
3783
|
napi_value nameValue = name;
|
|
3767
3784
|
napi_value valueValue = value;
|
|
3768
3785
|
return PropertyDescriptor::Value(nameValue, valueValue, attributes);
|
|
3769
3786
|
}
|
|
3770
3787
|
|
|
3771
3788
|
inline PropertyDescriptor::PropertyDescriptor(napi_property_descriptor desc)
|
|
3772
|
-
|
|
3773
|
-
}
|
|
3789
|
+
: _desc(desc) {}
|
|
3774
3790
|
|
|
3775
3791
|
inline PropertyDescriptor::operator napi_property_descriptor&() {
|
|
3776
3792
|
return _desc;
|
|
@@ -3785,26 +3801,22 @@ inline PropertyDescriptor::operator const napi_property_descriptor&() const {
|
|
|
3785
3801
|
////////////////////////////////////////////////////////////////////////////////
|
|
3786
3802
|
|
|
3787
3803
|
template <typename T>
|
|
3788
|
-
inline void InstanceWrap<T>::AttachPropData(
|
|
3789
|
-
|
|
3790
|
-
const napi_property_descriptor* prop) {
|
|
3804
|
+
inline void InstanceWrap<T>::AttachPropData(
|
|
3805
|
+
napi_env env, napi_value value, const napi_property_descriptor* prop) {
|
|
3791
3806
|
napi_status status;
|
|
3792
3807
|
if (!(prop->attributes & napi_static)) {
|
|
3793
3808
|
if (prop->method == T::InstanceVoidMethodCallbackWrapper) {
|
|
3794
|
-
status = Napi::details::AttachData(
|
|
3795
|
-
|
|
3796
|
-
static_cast<InstanceVoidMethodCallbackData*>(prop->data));
|
|
3809
|
+
status = Napi::details::AttachData(
|
|
3810
|
+
env, value, static_cast<InstanceVoidMethodCallbackData*>(prop->data));
|
|
3797
3811
|
NAPI_THROW_IF_FAILED_VOID(env, status);
|
|
3798
3812
|
} else if (prop->method == T::InstanceMethodCallbackWrapper) {
|
|
3799
|
-
status = Napi::details::AttachData(
|
|
3800
|
-
|
|
3801
|
-
static_cast<InstanceMethodCallbackData*>(prop->data));
|
|
3813
|
+
status = Napi::details::AttachData(
|
|
3814
|
+
env, value, static_cast<InstanceMethodCallbackData*>(prop->data));
|
|
3802
3815
|
NAPI_THROW_IF_FAILED_VOID(env, status);
|
|
3803
3816
|
} else if (prop->getter == T::InstanceGetterCallbackWrapper ||
|
|
3804
|
-
|
|
3805
|
-
status = Napi::details::AttachData(
|
|
3806
|
-
|
|
3807
|
-
static_cast<InstanceAccessorCallbackData*>(prop->data));
|
|
3817
|
+
prop->setter == T::InstanceSetterCallbackWrapper) {
|
|
3818
|
+
status = Napi::details::AttachData(
|
|
3819
|
+
env, value, static_cast<InstanceAccessorCallbackData*>(prop->data));
|
|
3808
3820
|
NAPI_THROW_IF_FAILED_VOID(env, status);
|
|
3809
3821
|
}
|
|
3810
3822
|
}
|
|
@@ -3817,7 +3829,7 @@ inline ClassPropertyDescriptor<T> InstanceWrap<T>::InstanceMethod(
|
|
|
3817
3829
|
napi_property_attributes attributes,
|
|
3818
3830
|
void* data) {
|
|
3819
3831
|
InstanceVoidMethodCallbackData* callbackData =
|
|
3820
|
-
|
|
3832
|
+
new InstanceVoidMethodCallbackData({method, data});
|
|
3821
3833
|
|
|
3822
3834
|
napi_property_descriptor desc = napi_property_descriptor();
|
|
3823
3835
|
desc.utf8name = utf8name;
|
|
@@ -3833,7 +3845,8 @@ inline ClassPropertyDescriptor<T> InstanceWrap<T>::InstanceMethod(
|
|
|
3833
3845
|
InstanceMethodCallback method,
|
|
3834
3846
|
napi_property_attributes attributes,
|
|
3835
3847
|
void* data) {
|
|
3836
|
-
InstanceMethodCallbackData* callbackData =
|
|
3848
|
+
InstanceMethodCallbackData* callbackData =
|
|
3849
|
+
new InstanceMethodCallbackData({method, data});
|
|
3837
3850
|
|
|
3838
3851
|
napi_property_descriptor desc = napi_property_descriptor();
|
|
3839
3852
|
desc.utf8name = utf8name;
|
|
@@ -3850,7 +3863,7 @@ inline ClassPropertyDescriptor<T> InstanceWrap<T>::InstanceMethod(
|
|
|
3850
3863
|
napi_property_attributes attributes,
|
|
3851
3864
|
void* data) {
|
|
3852
3865
|
InstanceVoidMethodCallbackData* callbackData =
|
|
3853
|
-
|
|
3866
|
+
new InstanceVoidMethodCallbackData({method, data});
|
|
3854
3867
|
|
|
3855
3868
|
napi_property_descriptor desc = napi_property_descriptor();
|
|
3856
3869
|
desc.name = name;
|
|
@@ -3866,7 +3879,8 @@ inline ClassPropertyDescriptor<T> InstanceWrap<T>::InstanceMethod(
|
|
|
3866
3879
|
InstanceMethodCallback method,
|
|
3867
3880
|
napi_property_attributes attributes,
|
|
3868
3881
|
void* data) {
|
|
3869
|
-
InstanceMethodCallbackData* callbackData =
|
|
3882
|
+
InstanceMethodCallbackData* callbackData =
|
|
3883
|
+
new InstanceMethodCallbackData({method, data});
|
|
3870
3884
|
|
|
3871
3885
|
napi_property_descriptor desc = napi_property_descriptor();
|
|
3872
3886
|
desc.name = name;
|
|
@@ -3879,9 +3893,7 @@ inline ClassPropertyDescriptor<T> InstanceWrap<T>::InstanceMethod(
|
|
|
3879
3893
|
template <typename T>
|
|
3880
3894
|
template <typename InstanceWrap<T>::InstanceVoidMethodCallback method>
|
|
3881
3895
|
inline ClassPropertyDescriptor<T> InstanceWrap<T>::InstanceMethod(
|
|
3882
|
-
const char* utf8name,
|
|
3883
|
-
napi_property_attributes attributes,
|
|
3884
|
-
void* data) {
|
|
3896
|
+
const char* utf8name, napi_property_attributes attributes, void* data) {
|
|
3885
3897
|
napi_property_descriptor desc = napi_property_descriptor();
|
|
3886
3898
|
desc.utf8name = utf8name;
|
|
3887
3899
|
desc.method = details::TemplatedInstanceVoidCallback<T, method>;
|
|
@@ -3893,9 +3905,7 @@ inline ClassPropertyDescriptor<T> InstanceWrap<T>::InstanceMethod(
|
|
|
3893
3905
|
template <typename T>
|
|
3894
3906
|
template <typename InstanceWrap<T>::InstanceMethodCallback method>
|
|
3895
3907
|
inline ClassPropertyDescriptor<T> InstanceWrap<T>::InstanceMethod(
|
|
3896
|
-
const char* utf8name,
|
|
3897
|
-
napi_property_attributes attributes,
|
|
3898
|
-
void* data) {
|
|
3908
|
+
const char* utf8name, napi_property_attributes attributes, void* data) {
|
|
3899
3909
|
napi_property_descriptor desc = napi_property_descriptor();
|
|
3900
3910
|
desc.utf8name = utf8name;
|
|
3901
3911
|
desc.method = details::TemplatedInstanceCallback<T, method>;
|
|
@@ -3907,9 +3917,7 @@ inline ClassPropertyDescriptor<T> InstanceWrap<T>::InstanceMethod(
|
|
|
3907
3917
|
template <typename T>
|
|
3908
3918
|
template <typename InstanceWrap<T>::InstanceVoidMethodCallback method>
|
|
3909
3919
|
inline ClassPropertyDescriptor<T> InstanceWrap<T>::InstanceMethod(
|
|
3910
|
-
Symbol name,
|
|
3911
|
-
napi_property_attributes attributes,
|
|
3912
|
-
void* data) {
|
|
3920
|
+
Symbol name, napi_property_attributes attributes, void* data) {
|
|
3913
3921
|
napi_property_descriptor desc = napi_property_descriptor();
|
|
3914
3922
|
desc.name = name;
|
|
3915
3923
|
desc.method = details::TemplatedInstanceVoidCallback<T, method>;
|
|
@@ -3921,9 +3929,7 @@ inline ClassPropertyDescriptor<T> InstanceWrap<T>::InstanceMethod(
|
|
|
3921
3929
|
template <typename T>
|
|
3922
3930
|
template <typename InstanceWrap<T>::InstanceMethodCallback method>
|
|
3923
3931
|
inline ClassPropertyDescriptor<T> InstanceWrap<T>::InstanceMethod(
|
|
3924
|
-
Symbol name,
|
|
3925
|
-
napi_property_attributes attributes,
|
|
3926
|
-
void* data) {
|
|
3932
|
+
Symbol name, napi_property_attributes attributes, void* data) {
|
|
3927
3933
|
napi_property_descriptor desc = napi_property_descriptor();
|
|
3928
3934
|
desc.name = name;
|
|
3929
3935
|
desc.method = details::TemplatedInstanceCallback<T, method>;
|
|
@@ -3940,7 +3946,7 @@ inline ClassPropertyDescriptor<T> InstanceWrap<T>::InstanceAccessor(
|
|
|
3940
3946
|
napi_property_attributes attributes,
|
|
3941
3947
|
void* data) {
|
|
3942
3948
|
InstanceAccessorCallbackData* callbackData =
|
|
3943
|
-
|
|
3949
|
+
new InstanceAccessorCallbackData({getter, setter, data});
|
|
3944
3950
|
|
|
3945
3951
|
napi_property_descriptor desc = napi_property_descriptor();
|
|
3946
3952
|
desc.utf8name = utf8name;
|
|
@@ -3959,7 +3965,7 @@ inline ClassPropertyDescriptor<T> InstanceWrap<T>::InstanceAccessor(
|
|
|
3959
3965
|
napi_property_attributes attributes,
|
|
3960
3966
|
void* data) {
|
|
3961
3967
|
InstanceAccessorCallbackData* callbackData =
|
|
3962
|
-
|
|
3968
|
+
new InstanceAccessorCallbackData({getter, setter, data});
|
|
3963
3969
|
|
|
3964
3970
|
napi_property_descriptor desc = napi_property_descriptor();
|
|
3965
3971
|
desc.name = name;
|
|
@@ -3974,9 +3980,7 @@ template <typename T>
|
|
|
3974
3980
|
template <typename InstanceWrap<T>::InstanceGetterCallback getter,
|
|
3975
3981
|
typename InstanceWrap<T>::InstanceSetterCallback setter>
|
|
3976
3982
|
inline ClassPropertyDescriptor<T> InstanceWrap<T>::InstanceAccessor(
|
|
3977
|
-
const char* utf8name,
|
|
3978
|
-
napi_property_attributes attributes,
|
|
3979
|
-
void* data) {
|
|
3983
|
+
const char* utf8name, napi_property_attributes attributes, void* data) {
|
|
3980
3984
|
napi_property_descriptor desc = napi_property_descriptor();
|
|
3981
3985
|
desc.utf8name = utf8name;
|
|
3982
3986
|
desc.getter = details::TemplatedInstanceCallback<T, getter>;
|
|
@@ -3990,9 +3994,7 @@ template <typename T>
|
|
|
3990
3994
|
template <typename InstanceWrap<T>::InstanceGetterCallback getter,
|
|
3991
3995
|
typename InstanceWrap<T>::InstanceSetterCallback setter>
|
|
3992
3996
|
inline ClassPropertyDescriptor<T> InstanceWrap<T>::InstanceAccessor(
|
|
3993
|
-
Symbol name,
|
|
3994
|
-
napi_property_attributes attributes,
|
|
3995
|
-
void* data) {
|
|
3997
|
+
Symbol name, napi_property_attributes attributes, void* data) {
|
|
3996
3998
|
napi_property_descriptor desc = napi_property_descriptor();
|
|
3997
3999
|
desc.name = name;
|
|
3998
4000
|
desc.getter = details::TemplatedInstanceCallback<T, getter>;
|
|
@@ -4016,9 +4018,7 @@ inline ClassPropertyDescriptor<T> InstanceWrap<T>::InstanceValue(
|
|
|
4016
4018
|
|
|
4017
4019
|
template <typename T>
|
|
4018
4020
|
inline ClassPropertyDescriptor<T> InstanceWrap<T>::InstanceValue(
|
|
4019
|
-
Symbol name,
|
|
4020
|
-
Napi::Value value,
|
|
4021
|
-
napi_property_attributes attributes) {
|
|
4021
|
+
Symbol name, Napi::Value value, napi_property_attributes attributes) {
|
|
4022
4022
|
napi_property_descriptor desc = napi_property_descriptor();
|
|
4023
4023
|
desc.name = name;
|
|
4024
4024
|
desc.value = value;
|
|
@@ -4028,12 +4028,11 @@ inline ClassPropertyDescriptor<T> InstanceWrap<T>::InstanceValue(
|
|
|
4028
4028
|
|
|
4029
4029
|
template <typename T>
|
|
4030
4030
|
inline napi_value InstanceWrap<T>::InstanceVoidMethodCallbackWrapper(
|
|
4031
|
-
napi_env env,
|
|
4032
|
-
napi_callback_info info) {
|
|
4031
|
+
napi_env env, napi_callback_info info) {
|
|
4033
4032
|
return details::WrapCallback([&] {
|
|
4034
4033
|
CallbackInfo callbackInfo(env, info);
|
|
4035
4034
|
InstanceVoidMethodCallbackData* callbackData =
|
|
4036
|
-
|
|
4035
|
+
reinterpret_cast<InstanceVoidMethodCallbackData*>(callbackInfo.Data());
|
|
4037
4036
|
callbackInfo.SetData(callbackData->data);
|
|
4038
4037
|
T* instance = T::Unwrap(callbackInfo.This().As<Object>());
|
|
4039
4038
|
auto cb = callbackData->callback;
|
|
@@ -4044,12 +4043,11 @@ inline napi_value InstanceWrap<T>::InstanceVoidMethodCallbackWrapper(
|
|
|
4044
4043
|
|
|
4045
4044
|
template <typename T>
|
|
4046
4045
|
inline napi_value InstanceWrap<T>::InstanceMethodCallbackWrapper(
|
|
4047
|
-
napi_env env,
|
|
4048
|
-
napi_callback_info info) {
|
|
4046
|
+
napi_env env, napi_callback_info info) {
|
|
4049
4047
|
return details::WrapCallback([&] {
|
|
4050
4048
|
CallbackInfo callbackInfo(env, info);
|
|
4051
4049
|
InstanceMethodCallbackData* callbackData =
|
|
4052
|
-
|
|
4050
|
+
reinterpret_cast<InstanceMethodCallbackData*>(callbackInfo.Data());
|
|
4053
4051
|
callbackInfo.SetData(callbackData->data);
|
|
4054
4052
|
T* instance = T::Unwrap(callbackInfo.This().As<Object>());
|
|
4055
4053
|
auto cb = callbackData->callback;
|
|
@@ -4059,12 +4057,11 @@ inline napi_value InstanceWrap<T>::InstanceMethodCallbackWrapper(
|
|
|
4059
4057
|
|
|
4060
4058
|
template <typename T>
|
|
4061
4059
|
inline napi_value InstanceWrap<T>::InstanceGetterCallbackWrapper(
|
|
4062
|
-
napi_env env,
|
|
4063
|
-
napi_callback_info info) {
|
|
4060
|
+
napi_env env, napi_callback_info info) {
|
|
4064
4061
|
return details::WrapCallback([&] {
|
|
4065
4062
|
CallbackInfo callbackInfo(env, info);
|
|
4066
4063
|
InstanceAccessorCallbackData* callbackData =
|
|
4067
|
-
|
|
4064
|
+
reinterpret_cast<InstanceAccessorCallbackData*>(callbackInfo.Data());
|
|
4068
4065
|
callbackInfo.SetData(callbackData->data);
|
|
4069
4066
|
T* instance = T::Unwrap(callbackInfo.This().As<Object>());
|
|
4070
4067
|
auto cb = callbackData->getterCallback;
|
|
@@ -4074,12 +4071,11 @@ inline napi_value InstanceWrap<T>::InstanceGetterCallbackWrapper(
|
|
|
4074
4071
|
|
|
4075
4072
|
template <typename T>
|
|
4076
4073
|
inline napi_value InstanceWrap<T>::InstanceSetterCallbackWrapper(
|
|
4077
|
-
napi_env env,
|
|
4078
|
-
napi_callback_info info) {
|
|
4074
|
+
napi_env env, napi_callback_info info) {
|
|
4079
4075
|
return details::WrapCallback([&] {
|
|
4080
4076
|
CallbackInfo callbackInfo(env, info);
|
|
4081
4077
|
InstanceAccessorCallbackData* callbackData =
|
|
4082
|
-
|
|
4078
|
+
reinterpret_cast<InstanceAccessorCallbackData*>(callbackInfo.Data());
|
|
4083
4079
|
callbackInfo.SetData(callbackData->data);
|
|
4084
4080
|
T* instance = T::Unwrap(callbackInfo.This().As<Object>());
|
|
4085
4081
|
auto cb = callbackData->setterCallback;
|
|
@@ -4132,7 +4128,7 @@ inline ObjectWrap<T>::~ObjectWrap() {
|
|
|
4132
4128
|
}
|
|
4133
4129
|
}
|
|
4134
4130
|
|
|
4135
|
-
template<typename T>
|
|
4131
|
+
template <typename T>
|
|
4136
4132
|
inline T* ObjectWrap<T>::Unwrap(Object wrapper) {
|
|
4137
4133
|
void* unwrapped;
|
|
4138
4134
|
napi_status status = napi_unwrap(wrapper.Env(), wrapper, &unwrapped);
|
|
@@ -4141,12 +4137,12 @@ inline T* ObjectWrap<T>::Unwrap(Object wrapper) {
|
|
|
4141
4137
|
}
|
|
4142
4138
|
|
|
4143
4139
|
template <typename T>
|
|
4144
|
-
inline Function
|
|
4145
|
-
|
|
4146
|
-
|
|
4147
|
-
|
|
4148
|
-
|
|
4149
|
-
|
|
4140
|
+
inline Function ObjectWrap<T>::DefineClass(
|
|
4141
|
+
Napi::Env env,
|
|
4142
|
+
const char* utf8name,
|
|
4143
|
+
const size_t props_count,
|
|
4144
|
+
const napi_property_descriptor* descriptors,
|
|
4145
|
+
void* data) {
|
|
4150
4146
|
napi_status status;
|
|
4151
4147
|
std::vector<napi_property_descriptor> props(props_count);
|
|
4152
4148
|
|
|
@@ -4162,16 +4158,18 @@ ObjectWrap<T>::DefineClass(Napi::Env env,
|
|
|
4162
4158
|
props[index] = descriptors[index];
|
|
4163
4159
|
napi_property_descriptor* prop = &props[index];
|
|
4164
4160
|
if (prop->method == T::StaticMethodCallbackWrapper) {
|
|
4165
|
-
status =
|
|
4166
|
-
|
|
4167
|
-
|
|
4168
|
-
|
|
4169
|
-
|
|
4161
|
+
status =
|
|
4162
|
+
CreateFunction(env,
|
|
4163
|
+
utf8name,
|
|
4164
|
+
prop->method,
|
|
4165
|
+
static_cast<StaticMethodCallbackData*>(prop->data),
|
|
4166
|
+
&(prop->value));
|
|
4170
4167
|
NAPI_THROW_IF_FAILED(env, status, Function());
|
|
4171
4168
|
prop->method = nullptr;
|
|
4172
4169
|
prop->data = nullptr;
|
|
4173
4170
|
} else if (prop->method == T::StaticVoidMethodCallbackWrapper) {
|
|
4174
|
-
status =
|
|
4171
|
+
status =
|
|
4172
|
+
CreateFunction(env,
|
|
4175
4173
|
utf8name,
|
|
4176
4174
|
prop->method,
|
|
4177
4175
|
static_cast<StaticVoidMethodCallbackData*>(prop->data),
|
|
@@ -4201,9 +4199,8 @@ ObjectWrap<T>::DefineClass(Napi::Env env,
|
|
|
4201
4199
|
|
|
4202
4200
|
if (prop->getter == T::StaticGetterCallbackWrapper ||
|
|
4203
4201
|
prop->setter == T::StaticSetterCallbackWrapper) {
|
|
4204
|
-
status = Napi::details::AttachData(
|
|
4205
|
-
|
|
4206
|
-
static_cast<StaticAccessorCallbackData*>(prop->data));
|
|
4202
|
+
status = Napi::details::AttachData(
|
|
4203
|
+
env, value, static_cast<StaticAccessorCallbackData*>(prop->data));
|
|
4207
4204
|
NAPI_THROW_IF_FAILED(env, status, Function());
|
|
4208
4205
|
} else {
|
|
4209
4206
|
// InstanceWrap<T>::AttachPropData is responsible for attaching the data
|
|
@@ -4221,11 +4218,12 @@ inline Function ObjectWrap<T>::DefineClass(
|
|
|
4221
4218
|
const char* utf8name,
|
|
4222
4219
|
const std::initializer_list<ClassPropertyDescriptor<T>>& properties,
|
|
4223
4220
|
void* data) {
|
|
4224
|
-
return DefineClass(
|
|
4225
|
-
|
|
4226
|
-
|
|
4227
|
-
|
|
4228
|
-
|
|
4221
|
+
return DefineClass(
|
|
4222
|
+
env,
|
|
4223
|
+
utf8name,
|
|
4224
|
+
properties.size(),
|
|
4225
|
+
reinterpret_cast<const napi_property_descriptor*>(properties.begin()),
|
|
4226
|
+
data);
|
|
4229
4227
|
}
|
|
4230
4228
|
|
|
4231
4229
|
template <typename T>
|
|
@@ -4234,11 +4232,12 @@ inline Function ObjectWrap<T>::DefineClass(
|
|
|
4234
4232
|
const char* utf8name,
|
|
4235
4233
|
const std::vector<ClassPropertyDescriptor<T>>& properties,
|
|
4236
4234
|
void* data) {
|
|
4237
|
-
return DefineClass(
|
|
4238
|
-
|
|
4239
|
-
|
|
4240
|
-
|
|
4241
|
-
|
|
4235
|
+
return DefineClass(
|
|
4236
|
+
env,
|
|
4237
|
+
utf8name,
|
|
4238
|
+
properties.size(),
|
|
4239
|
+
reinterpret_cast<const napi_property_descriptor*>(properties.data()),
|
|
4240
|
+
data);
|
|
4242
4241
|
}
|
|
4243
4242
|
|
|
4244
4243
|
template <typename T>
|
|
@@ -4247,13 +4246,15 @@ inline ClassPropertyDescriptor<T> ObjectWrap<T>::StaticMethod(
|
|
|
4247
4246
|
StaticVoidMethodCallback method,
|
|
4248
4247
|
napi_property_attributes attributes,
|
|
4249
4248
|
void* data) {
|
|
4250
|
-
StaticVoidMethodCallbackData* callbackData =
|
|
4249
|
+
StaticVoidMethodCallbackData* callbackData =
|
|
4250
|
+
new StaticVoidMethodCallbackData({method, data});
|
|
4251
4251
|
|
|
4252
4252
|
napi_property_descriptor desc = napi_property_descriptor();
|
|
4253
4253
|
desc.utf8name = utf8name;
|
|
4254
4254
|
desc.method = T::StaticVoidMethodCallbackWrapper;
|
|
4255
4255
|
desc.data = callbackData;
|
|
4256
|
-
desc.attributes =
|
|
4256
|
+
desc.attributes =
|
|
4257
|
+
static_cast<napi_property_attributes>(attributes | napi_static);
|
|
4257
4258
|
return desc;
|
|
4258
4259
|
}
|
|
4259
4260
|
|
|
@@ -4263,13 +4264,15 @@ inline ClassPropertyDescriptor<T> ObjectWrap<T>::StaticMethod(
|
|
|
4263
4264
|
StaticMethodCallback method,
|
|
4264
4265
|
napi_property_attributes attributes,
|
|
4265
4266
|
void* data) {
|
|
4266
|
-
StaticMethodCallbackData* callbackData =
|
|
4267
|
+
StaticMethodCallbackData* callbackData =
|
|
4268
|
+
new StaticMethodCallbackData({method, data});
|
|
4267
4269
|
|
|
4268
4270
|
napi_property_descriptor desc = napi_property_descriptor();
|
|
4269
4271
|
desc.utf8name = utf8name;
|
|
4270
4272
|
desc.method = T::StaticMethodCallbackWrapper;
|
|
4271
4273
|
desc.data = callbackData;
|
|
4272
|
-
desc.attributes =
|
|
4274
|
+
desc.attributes =
|
|
4275
|
+
static_cast<napi_property_attributes>(attributes | napi_static);
|
|
4273
4276
|
return desc;
|
|
4274
4277
|
}
|
|
4275
4278
|
|
|
@@ -4279,13 +4282,15 @@ inline ClassPropertyDescriptor<T> ObjectWrap<T>::StaticMethod(
|
|
|
4279
4282
|
StaticVoidMethodCallback method,
|
|
4280
4283
|
napi_property_attributes attributes,
|
|
4281
4284
|
void* data) {
|
|
4282
|
-
StaticVoidMethodCallbackData* callbackData =
|
|
4285
|
+
StaticVoidMethodCallbackData* callbackData =
|
|
4286
|
+
new StaticVoidMethodCallbackData({method, data});
|
|
4283
4287
|
|
|
4284
4288
|
napi_property_descriptor desc = napi_property_descriptor();
|
|
4285
4289
|
desc.name = name;
|
|
4286
4290
|
desc.method = T::StaticVoidMethodCallbackWrapper;
|
|
4287
4291
|
desc.data = callbackData;
|
|
4288
|
-
desc.attributes =
|
|
4292
|
+
desc.attributes =
|
|
4293
|
+
static_cast<napi_property_attributes>(attributes | napi_static);
|
|
4289
4294
|
return desc;
|
|
4290
4295
|
}
|
|
4291
4296
|
|
|
@@ -4295,69 +4300,67 @@ inline ClassPropertyDescriptor<T> ObjectWrap<T>::StaticMethod(
|
|
|
4295
4300
|
StaticMethodCallback method,
|
|
4296
4301
|
napi_property_attributes attributes,
|
|
4297
4302
|
void* data) {
|
|
4298
|
-
StaticMethodCallbackData* callbackData =
|
|
4303
|
+
StaticMethodCallbackData* callbackData =
|
|
4304
|
+
new StaticMethodCallbackData({method, data});
|
|
4299
4305
|
|
|
4300
4306
|
napi_property_descriptor desc = napi_property_descriptor();
|
|
4301
4307
|
desc.name = name;
|
|
4302
4308
|
desc.method = T::StaticMethodCallbackWrapper;
|
|
4303
4309
|
desc.data = callbackData;
|
|
4304
|
-
desc.attributes =
|
|
4310
|
+
desc.attributes =
|
|
4311
|
+
static_cast<napi_property_attributes>(attributes | napi_static);
|
|
4305
4312
|
return desc;
|
|
4306
4313
|
}
|
|
4307
4314
|
|
|
4308
4315
|
template <typename T>
|
|
4309
4316
|
template <typename ObjectWrap<T>::StaticVoidMethodCallback method>
|
|
4310
4317
|
inline ClassPropertyDescriptor<T> ObjectWrap<T>::StaticMethod(
|
|
4311
|
-
const char* utf8name,
|
|
4312
|
-
napi_property_attributes attributes,
|
|
4313
|
-
void* data) {
|
|
4318
|
+
const char* utf8name, napi_property_attributes attributes, void* data) {
|
|
4314
4319
|
napi_property_descriptor desc = napi_property_descriptor();
|
|
4315
4320
|
desc.utf8name = utf8name;
|
|
4316
4321
|
desc.method = details::TemplatedVoidCallback<method>;
|
|
4317
4322
|
desc.data = data;
|
|
4318
|
-
desc.attributes =
|
|
4323
|
+
desc.attributes =
|
|
4324
|
+
static_cast<napi_property_attributes>(attributes | napi_static);
|
|
4319
4325
|
return desc;
|
|
4320
4326
|
}
|
|
4321
4327
|
|
|
4322
4328
|
template <typename T>
|
|
4323
4329
|
template <typename ObjectWrap<T>::StaticVoidMethodCallback method>
|
|
4324
4330
|
inline ClassPropertyDescriptor<T> ObjectWrap<T>::StaticMethod(
|
|
4325
|
-
Symbol name,
|
|
4326
|
-
napi_property_attributes attributes,
|
|
4327
|
-
void* data) {
|
|
4331
|
+
Symbol name, napi_property_attributes attributes, void* data) {
|
|
4328
4332
|
napi_property_descriptor desc = napi_property_descriptor();
|
|
4329
4333
|
desc.name = name;
|
|
4330
4334
|
desc.method = details::TemplatedVoidCallback<method>;
|
|
4331
4335
|
desc.data = data;
|
|
4332
|
-
desc.attributes =
|
|
4336
|
+
desc.attributes =
|
|
4337
|
+
static_cast<napi_property_attributes>(attributes | napi_static);
|
|
4333
4338
|
return desc;
|
|
4334
4339
|
}
|
|
4335
4340
|
|
|
4336
4341
|
template <typename T>
|
|
4337
4342
|
template <typename ObjectWrap<T>::StaticMethodCallback method>
|
|
4338
4343
|
inline ClassPropertyDescriptor<T> ObjectWrap<T>::StaticMethod(
|
|
4339
|
-
const char* utf8name,
|
|
4340
|
-
napi_property_attributes attributes,
|
|
4341
|
-
void* data) {
|
|
4344
|
+
const char* utf8name, napi_property_attributes attributes, void* data) {
|
|
4342
4345
|
napi_property_descriptor desc = napi_property_descriptor();
|
|
4343
4346
|
desc.utf8name = utf8name;
|
|
4344
4347
|
desc.method = details::TemplatedCallback<method>;
|
|
4345
4348
|
desc.data = data;
|
|
4346
|
-
desc.attributes =
|
|
4349
|
+
desc.attributes =
|
|
4350
|
+
static_cast<napi_property_attributes>(attributes | napi_static);
|
|
4347
4351
|
return desc;
|
|
4348
4352
|
}
|
|
4349
4353
|
|
|
4350
4354
|
template <typename T>
|
|
4351
4355
|
template <typename ObjectWrap<T>::StaticMethodCallback method>
|
|
4352
4356
|
inline ClassPropertyDescriptor<T> ObjectWrap<T>::StaticMethod(
|
|
4353
|
-
Symbol name,
|
|
4354
|
-
napi_property_attributes attributes,
|
|
4355
|
-
void* data) {
|
|
4357
|
+
Symbol name, napi_property_attributes attributes, void* data) {
|
|
4356
4358
|
napi_property_descriptor desc = napi_property_descriptor();
|
|
4357
4359
|
desc.name = name;
|
|
4358
4360
|
desc.method = details::TemplatedCallback<method>;
|
|
4359
4361
|
desc.data = data;
|
|
4360
|
-
desc.attributes =
|
|
4362
|
+
desc.attributes =
|
|
4363
|
+
static_cast<napi_property_attributes>(attributes | napi_static);
|
|
4361
4364
|
return desc;
|
|
4362
4365
|
}
|
|
4363
4366
|
|
|
@@ -4369,14 +4372,15 @@ inline ClassPropertyDescriptor<T> ObjectWrap<T>::StaticAccessor(
|
|
|
4369
4372
|
napi_property_attributes attributes,
|
|
4370
4373
|
void* data) {
|
|
4371
4374
|
StaticAccessorCallbackData* callbackData =
|
|
4372
|
-
|
|
4375
|
+
new StaticAccessorCallbackData({getter, setter, data});
|
|
4373
4376
|
|
|
4374
4377
|
napi_property_descriptor desc = napi_property_descriptor();
|
|
4375
4378
|
desc.utf8name = utf8name;
|
|
4376
4379
|
desc.getter = getter != nullptr ? T::StaticGetterCallbackWrapper : nullptr;
|
|
4377
4380
|
desc.setter = setter != nullptr ? T::StaticSetterCallbackWrapper : nullptr;
|
|
4378
4381
|
desc.data = callbackData;
|
|
4379
|
-
desc.attributes =
|
|
4382
|
+
desc.attributes =
|
|
4383
|
+
static_cast<napi_property_attributes>(attributes | napi_static);
|
|
4380
4384
|
return desc;
|
|
4381
4385
|
}
|
|
4382
4386
|
|
|
@@ -4388,14 +4392,15 @@ inline ClassPropertyDescriptor<T> ObjectWrap<T>::StaticAccessor(
|
|
|
4388
4392
|
napi_property_attributes attributes,
|
|
4389
4393
|
void* data) {
|
|
4390
4394
|
StaticAccessorCallbackData* callbackData =
|
|
4391
|
-
|
|
4395
|
+
new StaticAccessorCallbackData({getter, setter, data});
|
|
4392
4396
|
|
|
4393
4397
|
napi_property_descriptor desc = napi_property_descriptor();
|
|
4394
4398
|
desc.name = name;
|
|
4395
4399
|
desc.getter = getter != nullptr ? T::StaticGetterCallbackWrapper : nullptr;
|
|
4396
4400
|
desc.setter = setter != nullptr ? T::StaticSetterCallbackWrapper : nullptr;
|
|
4397
4401
|
desc.data = callbackData;
|
|
4398
|
-
desc.attributes =
|
|
4402
|
+
desc.attributes =
|
|
4403
|
+
static_cast<napi_property_attributes>(attributes | napi_static);
|
|
4399
4404
|
return desc;
|
|
4400
4405
|
}
|
|
4401
4406
|
|
|
@@ -4403,15 +4408,14 @@ template <typename T>
|
|
|
4403
4408
|
template <typename ObjectWrap<T>::StaticGetterCallback getter,
|
|
4404
4409
|
typename ObjectWrap<T>::StaticSetterCallback setter>
|
|
4405
4410
|
inline ClassPropertyDescriptor<T> ObjectWrap<T>::StaticAccessor(
|
|
4406
|
-
const char* utf8name,
|
|
4407
|
-
napi_property_attributes attributes,
|
|
4408
|
-
void* data) {
|
|
4411
|
+
const char* utf8name, napi_property_attributes attributes, void* data) {
|
|
4409
4412
|
napi_property_descriptor desc = napi_property_descriptor();
|
|
4410
4413
|
desc.utf8name = utf8name;
|
|
4411
4414
|
desc.getter = details::TemplatedCallback<getter>;
|
|
4412
4415
|
desc.setter = This::WrapStaticSetter(This::StaticSetterTag<setter>());
|
|
4413
4416
|
desc.data = data;
|
|
4414
|
-
desc.attributes =
|
|
4417
|
+
desc.attributes =
|
|
4418
|
+
static_cast<napi_property_attributes>(attributes | napi_static);
|
|
4415
4419
|
return desc;
|
|
4416
4420
|
}
|
|
4417
4421
|
|
|
@@ -4419,53 +4423,64 @@ template <typename T>
|
|
|
4419
4423
|
template <typename ObjectWrap<T>::StaticGetterCallback getter,
|
|
4420
4424
|
typename ObjectWrap<T>::StaticSetterCallback setter>
|
|
4421
4425
|
inline ClassPropertyDescriptor<T> ObjectWrap<T>::StaticAccessor(
|
|
4422
|
-
Symbol name,
|
|
4423
|
-
napi_property_attributes attributes,
|
|
4424
|
-
void* data) {
|
|
4426
|
+
Symbol name, napi_property_attributes attributes, void* data) {
|
|
4425
4427
|
napi_property_descriptor desc = napi_property_descriptor();
|
|
4426
4428
|
desc.name = name;
|
|
4427
4429
|
desc.getter = details::TemplatedCallback<getter>;
|
|
4428
4430
|
desc.setter = This::WrapStaticSetter(This::StaticSetterTag<setter>());
|
|
4429
4431
|
desc.data = data;
|
|
4430
|
-
desc.attributes =
|
|
4432
|
+
desc.attributes =
|
|
4433
|
+
static_cast<napi_property_attributes>(attributes | napi_static);
|
|
4431
4434
|
return desc;
|
|
4432
4435
|
}
|
|
4433
4436
|
|
|
4434
4437
|
template <typename T>
|
|
4435
|
-
inline ClassPropertyDescriptor<T> ObjectWrap<T>::StaticValue(
|
|
4436
|
-
|
|
4438
|
+
inline ClassPropertyDescriptor<T> ObjectWrap<T>::StaticValue(
|
|
4439
|
+
const char* utf8name,
|
|
4440
|
+
Napi::Value value,
|
|
4441
|
+
napi_property_attributes attributes) {
|
|
4437
4442
|
napi_property_descriptor desc = napi_property_descriptor();
|
|
4438
4443
|
desc.utf8name = utf8name;
|
|
4439
4444
|
desc.value = value;
|
|
4440
|
-
desc.attributes =
|
|
4445
|
+
desc.attributes =
|
|
4446
|
+
static_cast<napi_property_attributes>(attributes | napi_static);
|
|
4441
4447
|
return desc;
|
|
4442
4448
|
}
|
|
4443
4449
|
|
|
4444
4450
|
template <typename T>
|
|
4445
|
-
inline ClassPropertyDescriptor<T> ObjectWrap<T>::StaticValue(
|
|
4446
|
-
Napi::Value value, napi_property_attributes attributes) {
|
|
4451
|
+
inline ClassPropertyDescriptor<T> ObjectWrap<T>::StaticValue(
|
|
4452
|
+
Symbol name, Napi::Value value, napi_property_attributes attributes) {
|
|
4447
4453
|
napi_property_descriptor desc = napi_property_descriptor();
|
|
4448
4454
|
desc.name = name;
|
|
4449
4455
|
desc.value = value;
|
|
4450
|
-
desc.attributes =
|
|
4456
|
+
desc.attributes =
|
|
4457
|
+
static_cast<napi_property_attributes>(attributes | napi_static);
|
|
4451
4458
|
return desc;
|
|
4452
4459
|
}
|
|
4453
4460
|
|
|
4461
|
+
template <typename T>
|
|
4462
|
+
inline Value ObjectWrap<T>::OnCalledAsFunction(
|
|
4463
|
+
const Napi::CallbackInfo& callbackInfo) {
|
|
4464
|
+
NAPI_THROW(
|
|
4465
|
+
TypeError::New(callbackInfo.Env(),
|
|
4466
|
+
"Class constructors cannot be invoked without 'new'"),
|
|
4467
|
+
Napi::Value());
|
|
4468
|
+
}
|
|
4469
|
+
|
|
4454
4470
|
template <typename T>
|
|
4455
4471
|
inline void ObjectWrap<T>::Finalize(Napi::Env /*env*/) {}
|
|
4456
4472
|
|
|
4457
4473
|
template <typename T>
|
|
4458
4474
|
inline napi_value ObjectWrap<T>::ConstructorCallbackWrapper(
|
|
4459
|
-
napi_env env,
|
|
4460
|
-
napi_callback_info info) {
|
|
4475
|
+
napi_env env, napi_callback_info info) {
|
|
4461
4476
|
napi_value new_target;
|
|
4462
4477
|
napi_status status = napi_get_new_target(env, info, &new_target);
|
|
4463
4478
|
if (status != napi_ok) return nullptr;
|
|
4464
4479
|
|
|
4465
4480
|
bool isConstructCall = (new_target != nullptr);
|
|
4466
4481
|
if (!isConstructCall) {
|
|
4467
|
-
|
|
4468
|
-
|
|
4482
|
+
return details::WrapCallback(
|
|
4483
|
+
[&] { return T::OnCalledAsFunction(CallbackInfo(env, info)); });
|
|
4469
4484
|
}
|
|
4470
4485
|
|
|
4471
4486
|
napi_value wrapper = details::WrapCallback([&] {
|
|
@@ -4482,7 +4497,7 @@ inline napi_value ObjectWrap<T>::ConstructorCallbackWrapper(
|
|
|
4482
4497
|
} else {
|
|
4483
4498
|
instance->_construction_failed = false;
|
|
4484
4499
|
}
|
|
4485
|
-
#
|
|
4500
|
+
#endif // NAPI_CPP_EXCEPTIONS
|
|
4486
4501
|
return callbackInfo.This();
|
|
4487
4502
|
});
|
|
4488
4503
|
|
|
@@ -4491,12 +4506,11 @@ inline napi_value ObjectWrap<T>::ConstructorCallbackWrapper(
|
|
|
4491
4506
|
|
|
4492
4507
|
template <typename T>
|
|
4493
4508
|
inline napi_value ObjectWrap<T>::StaticVoidMethodCallbackWrapper(
|
|
4494
|
-
napi_env env,
|
|
4495
|
-
napi_callback_info info) {
|
|
4509
|
+
napi_env env, napi_callback_info info) {
|
|
4496
4510
|
return details::WrapCallback([&] {
|
|
4497
4511
|
CallbackInfo callbackInfo(env, info);
|
|
4498
4512
|
StaticVoidMethodCallbackData* callbackData =
|
|
4499
|
-
|
|
4513
|
+
reinterpret_cast<StaticVoidMethodCallbackData*>(callbackInfo.Data());
|
|
4500
4514
|
callbackInfo.SetData(callbackData->data);
|
|
4501
4515
|
callbackData->callback(callbackInfo);
|
|
4502
4516
|
return nullptr;
|
|
@@ -4505,12 +4519,11 @@ inline napi_value ObjectWrap<T>::StaticVoidMethodCallbackWrapper(
|
|
|
4505
4519
|
|
|
4506
4520
|
template <typename T>
|
|
4507
4521
|
inline napi_value ObjectWrap<T>::StaticMethodCallbackWrapper(
|
|
4508
|
-
napi_env env,
|
|
4509
|
-
napi_callback_info info) {
|
|
4522
|
+
napi_env env, napi_callback_info info) {
|
|
4510
4523
|
return details::WrapCallback([&] {
|
|
4511
4524
|
CallbackInfo callbackInfo(env, info);
|
|
4512
4525
|
StaticMethodCallbackData* callbackData =
|
|
4513
|
-
|
|
4526
|
+
reinterpret_cast<StaticMethodCallbackData*>(callbackInfo.Data());
|
|
4514
4527
|
callbackInfo.SetData(callbackData->data);
|
|
4515
4528
|
return callbackData->callback(callbackInfo);
|
|
4516
4529
|
});
|
|
@@ -4518,12 +4531,11 @@ inline napi_value ObjectWrap<T>::StaticMethodCallbackWrapper(
|
|
|
4518
4531
|
|
|
4519
4532
|
template <typename T>
|
|
4520
4533
|
inline napi_value ObjectWrap<T>::StaticGetterCallbackWrapper(
|
|
4521
|
-
napi_env env,
|
|
4522
|
-
napi_callback_info info) {
|
|
4534
|
+
napi_env env, napi_callback_info info) {
|
|
4523
4535
|
return details::WrapCallback([&] {
|
|
4524
4536
|
CallbackInfo callbackInfo(env, info);
|
|
4525
4537
|
StaticAccessorCallbackData* callbackData =
|
|
4526
|
-
|
|
4538
|
+
reinterpret_cast<StaticAccessorCallbackData*>(callbackInfo.Data());
|
|
4527
4539
|
callbackInfo.SetData(callbackData->data);
|
|
4528
4540
|
return callbackData->getterCallback(callbackInfo);
|
|
4529
4541
|
});
|
|
@@ -4531,12 +4543,11 @@ inline napi_value ObjectWrap<T>::StaticGetterCallbackWrapper(
|
|
|
4531
4543
|
|
|
4532
4544
|
template <typename T>
|
|
4533
4545
|
inline napi_value ObjectWrap<T>::StaticSetterCallbackWrapper(
|
|
4534
|
-
napi_env env,
|
|
4535
|
-
napi_callback_info info) {
|
|
4546
|
+
napi_env env, napi_callback_info info) {
|
|
4536
4547
|
return details::WrapCallback([&] {
|
|
4537
4548
|
CallbackInfo callbackInfo(env, info);
|
|
4538
4549
|
StaticAccessorCallbackData* callbackData =
|
|
4539
|
-
|
|
4550
|
+
reinterpret_cast<StaticAccessorCallbackData*>(callbackInfo.Data());
|
|
4540
4551
|
callbackInfo.SetData(callbackData->data);
|
|
4541
4552
|
callbackData->setterCallback(callbackInfo, callbackInfo[0]);
|
|
4542
4553
|
return nullptr;
|
|
@@ -4544,7 +4555,9 @@ inline napi_value ObjectWrap<T>::StaticSetterCallbackWrapper(
|
|
|
4544
4555
|
}
|
|
4545
4556
|
|
|
4546
4557
|
template <typename T>
|
|
4547
|
-
inline void ObjectWrap<T>::FinalizeCallback(napi_env env,
|
|
4558
|
+
inline void ObjectWrap<T>::FinalizeCallback(napi_env env,
|
|
4559
|
+
void* data,
|
|
4560
|
+
void* /*hint*/) {
|
|
4548
4561
|
HandleScope scope(env);
|
|
4549
4562
|
T* instance = static_cast<T*>(data);
|
|
4550
4563
|
instance->Finalize(Napi::Env(env));
|
|
@@ -4567,8 +4580,7 @@ inline napi_value ObjectWrap<T>::WrappedMethod(
|
|
|
4567
4580
|
////////////////////////////////////////////////////////////////////////////////
|
|
4568
4581
|
|
|
4569
4582
|
inline HandleScope::HandleScope(napi_env env, napi_handle_scope scope)
|
|
4570
|
-
: _env(env), _scope(scope) {
|
|
4571
|
-
}
|
|
4583
|
+
: _env(env), _scope(scope) {}
|
|
4572
4584
|
|
|
4573
4585
|
inline HandleScope::HandleScope(Napi::Env env) : _env(env) {
|
|
4574
4586
|
napi_status status = napi_open_handle_scope(_env, &_scope);
|
|
@@ -4577,9 +4589,8 @@ inline HandleScope::HandleScope(Napi::Env env) : _env(env) {
|
|
|
4577
4589
|
|
|
4578
4590
|
inline HandleScope::~HandleScope() {
|
|
4579
4591
|
napi_status status = napi_close_handle_scope(_env, _scope);
|
|
4580
|
-
NAPI_FATAL_IF_FAILED(
|
|
4581
|
-
|
|
4582
|
-
"napi_close_handle_scope");
|
|
4592
|
+
NAPI_FATAL_IF_FAILED(
|
|
4593
|
+
status, "HandleScope::~HandleScope", "napi_close_handle_scope");
|
|
4583
4594
|
}
|
|
4584
4595
|
|
|
4585
4596
|
inline HandleScope::operator napi_handle_scope() const {
|
|
@@ -4595,8 +4606,8 @@ inline Napi::Env HandleScope::Env() const {
|
|
|
4595
4606
|
////////////////////////////////////////////////////////////////////////////////
|
|
4596
4607
|
|
|
4597
4608
|
inline EscapableHandleScope::EscapableHandleScope(
|
|
4598
|
-
|
|
4599
|
-
}
|
|
4609
|
+
napi_env env, napi_escapable_handle_scope scope)
|
|
4610
|
+
: _env(env), _scope(scope) {}
|
|
4600
4611
|
|
|
4601
4612
|
inline EscapableHandleScope::EscapableHandleScope(Napi::Env env) : _env(env) {
|
|
4602
4613
|
napi_status status = napi_open_escapable_handle_scope(_env, &_scope);
|
|
@@ -4625,28 +4636,25 @@ inline Value EscapableHandleScope::Escape(napi_value escapee) {
|
|
|
4625
4636
|
return Value(_env, result);
|
|
4626
4637
|
}
|
|
4627
4638
|
|
|
4628
|
-
|
|
4629
4639
|
#if (NAPI_VERSION > 2)
|
|
4630
4640
|
////////////////////////////////////////////////////////////////////////////////
|
|
4631
4641
|
// CallbackScope class
|
|
4632
4642
|
////////////////////////////////////////////////////////////////////////////////
|
|
4633
4643
|
|
|
4634
|
-
inline CallbackScope::CallbackScope(
|
|
4635
|
-
|
|
4636
|
-
}
|
|
4644
|
+
inline CallbackScope::CallbackScope(napi_env env, napi_callback_scope scope)
|
|
4645
|
+
: _env(env), _scope(scope) {}
|
|
4637
4646
|
|
|
4638
4647
|
inline CallbackScope::CallbackScope(napi_env env, napi_async_context context)
|
|
4639
4648
|
: _env(env) {
|
|
4640
|
-
napi_status status =
|
|
4641
|
-
_env, Object::New(env), context, &_scope);
|
|
4649
|
+
napi_status status =
|
|
4650
|
+
napi_open_callback_scope(_env, Object::New(env), context, &_scope);
|
|
4642
4651
|
NAPI_THROW_IF_FAILED_VOID(_env, status);
|
|
4643
4652
|
}
|
|
4644
4653
|
|
|
4645
4654
|
inline CallbackScope::~CallbackScope() {
|
|
4646
4655
|
napi_status status = napi_close_callback_scope(_env, _scope);
|
|
4647
|
-
NAPI_FATAL_IF_FAILED(
|
|
4648
|
-
|
|
4649
|
-
"napi_close_callback_scope");
|
|
4656
|
+
NAPI_FATAL_IF_FAILED(
|
|
4657
|
+
status, "CallbackScope::~CallbackScope", "napi_close_callback_scope");
|
|
4650
4658
|
}
|
|
4651
4659
|
|
|
4652
4660
|
inline CallbackScope::operator napi_callback_scope() const {
|
|
@@ -4663,8 +4671,7 @@ inline Napi::Env CallbackScope::Env() const {
|
|
|
4663
4671
|
////////////////////////////////////////////////////////////////////////////////
|
|
4664
4672
|
|
|
4665
4673
|
inline AsyncContext::AsyncContext(napi_env env, const char* resource_name)
|
|
4666
|
-
|
|
4667
|
-
}
|
|
4674
|
+
: AsyncContext(env, resource_name, Object::New(env)) {}
|
|
4668
4675
|
|
|
4669
4676
|
inline AsyncContext::AsyncContext(napi_env env,
|
|
4670
4677
|
const char* resource_name,
|
|
@@ -4693,7 +4700,7 @@ inline AsyncContext::AsyncContext(AsyncContext&& other) {
|
|
|
4693
4700
|
other._context = nullptr;
|
|
4694
4701
|
}
|
|
4695
4702
|
|
|
4696
|
-
inline AsyncContext& AsyncContext::operator
|
|
4703
|
+
inline AsyncContext& AsyncContext::operator=(AsyncContext&& other) {
|
|
4697
4704
|
_env = other._env;
|
|
4698
4705
|
other._env = nullptr;
|
|
4699
4706
|
_context = other._context;
|
|
@@ -4714,78 +4721,72 @@ inline Napi::Env AsyncContext::Env() const {
|
|
|
4714
4721
|
////////////////////////////////////////////////////////////////////////////////
|
|
4715
4722
|
|
|
4716
4723
|
inline AsyncWorker::AsyncWorker(const Function& callback)
|
|
4717
|
-
|
|
4718
|
-
}
|
|
4724
|
+
: AsyncWorker(callback, "generic") {}
|
|
4719
4725
|
|
|
4720
4726
|
inline AsyncWorker::AsyncWorker(const Function& callback,
|
|
4721
4727
|
const char* resource_name)
|
|
4722
|
-
|
|
4723
|
-
}
|
|
4728
|
+
: AsyncWorker(callback, resource_name, Object::New(callback.Env())) {}
|
|
4724
4729
|
|
|
4725
4730
|
inline AsyncWorker::AsyncWorker(const Function& callback,
|
|
4726
4731
|
const char* resource_name,
|
|
4727
4732
|
const Object& resource)
|
|
4728
|
-
|
|
4729
|
-
|
|
4730
|
-
resource_name,
|
|
4731
|
-
resource) {
|
|
4732
|
-
}
|
|
4733
|
+
: AsyncWorker(
|
|
4734
|
+
Object::New(callback.Env()), callback, resource_name, resource) {}
|
|
4733
4735
|
|
|
4734
4736
|
inline AsyncWorker::AsyncWorker(const Object& receiver,
|
|
4735
4737
|
const Function& callback)
|
|
4736
|
-
|
|
4737
|
-
}
|
|
4738
|
+
: AsyncWorker(receiver, callback, "generic") {}
|
|
4738
4739
|
|
|
4739
4740
|
inline AsyncWorker::AsyncWorker(const Object& receiver,
|
|
4740
4741
|
const Function& callback,
|
|
4741
4742
|
const char* resource_name)
|
|
4742
|
-
|
|
4743
|
-
|
|
4744
|
-
resource_name,
|
|
4745
|
-
Object::New(callback.Env())) {
|
|
4746
|
-
}
|
|
4743
|
+
: AsyncWorker(
|
|
4744
|
+
receiver, callback, resource_name, Object::New(callback.Env())) {}
|
|
4747
4745
|
|
|
4748
4746
|
inline AsyncWorker::AsyncWorker(const Object& receiver,
|
|
4749
4747
|
const Function& callback,
|
|
4750
4748
|
const char* resource_name,
|
|
4751
4749
|
const Object& resource)
|
|
4752
|
-
|
|
4753
|
-
|
|
4754
|
-
|
|
4755
|
-
|
|
4750
|
+
: _env(callback.Env()),
|
|
4751
|
+
_receiver(Napi::Persistent(receiver)),
|
|
4752
|
+
_callback(Napi::Persistent(callback)),
|
|
4753
|
+
_suppress_destruct(false) {
|
|
4756
4754
|
napi_value resource_id;
|
|
4757
4755
|
napi_status status = napi_create_string_latin1(
|
|
4758
4756
|
_env, resource_name, NAPI_AUTO_LENGTH, &resource_id);
|
|
4759
4757
|
NAPI_THROW_IF_FAILED_VOID(_env, status);
|
|
4760
4758
|
|
|
4761
|
-
status = napi_create_async_work(_env,
|
|
4762
|
-
|
|
4759
|
+
status = napi_create_async_work(_env,
|
|
4760
|
+
resource,
|
|
4761
|
+
resource_id,
|
|
4762
|
+
OnAsyncWorkExecute,
|
|
4763
|
+
OnAsyncWorkComplete,
|
|
4764
|
+
this,
|
|
4765
|
+
&_work);
|
|
4763
4766
|
NAPI_THROW_IF_FAILED_VOID(_env, status);
|
|
4764
4767
|
}
|
|
4765
4768
|
|
|
4766
|
-
inline AsyncWorker::AsyncWorker(Napi::Env env)
|
|
4767
|
-
: AsyncWorker(env, "generic") {
|
|
4768
|
-
}
|
|
4769
|
+
inline AsyncWorker::AsyncWorker(Napi::Env env) : AsyncWorker(env, "generic") {}
|
|
4769
4770
|
|
|
4770
|
-
inline AsyncWorker::AsyncWorker(Napi::Env env,
|
|
4771
|
-
|
|
4772
|
-
: AsyncWorker(env, resource_name, Object::New(env)) {
|
|
4773
|
-
}
|
|
4771
|
+
inline AsyncWorker::AsyncWorker(Napi::Env env, const char* resource_name)
|
|
4772
|
+
: AsyncWorker(env, resource_name, Object::New(env)) {}
|
|
4774
4773
|
|
|
4775
4774
|
inline AsyncWorker::AsyncWorker(Napi::Env env,
|
|
4776
4775
|
const char* resource_name,
|
|
4777
4776
|
const Object& resource)
|
|
4778
|
-
|
|
4779
|
-
_receiver(),
|
|
4780
|
-
_callback(),
|
|
4781
|
-
_suppress_destruct(false) {
|
|
4777
|
+
: _env(env), _receiver(), _callback(), _suppress_destruct(false) {
|
|
4782
4778
|
napi_value resource_id;
|
|
4783
4779
|
napi_status status = napi_create_string_latin1(
|
|
4784
4780
|
_env, resource_name, NAPI_AUTO_LENGTH, &resource_id);
|
|
4785
4781
|
NAPI_THROW_IF_FAILED_VOID(_env, status);
|
|
4786
4782
|
|
|
4787
|
-
status = napi_create_async_work(_env,
|
|
4788
|
-
|
|
4783
|
+
status = napi_create_async_work(_env,
|
|
4784
|
+
resource,
|
|
4785
|
+
resource_id,
|
|
4786
|
+
OnAsyncWorkExecute,
|
|
4787
|
+
OnAsyncWorkComplete,
|
|
4788
|
+
this,
|
|
4789
|
+
&_work);
|
|
4789
4790
|
NAPI_THROW_IF_FAILED_VOID(_env, status);
|
|
4790
4791
|
}
|
|
4791
4792
|
|
|
@@ -4811,7 +4812,7 @@ inline AsyncWorker::AsyncWorker(AsyncWorker&& other) {
|
|
|
4811
4812
|
_suppress_destruct = other._suppress_destruct;
|
|
4812
4813
|
}
|
|
4813
4814
|
|
|
4814
|
-
inline AsyncWorker& AsyncWorker::operator
|
|
4815
|
+
inline AsyncWorker& AsyncWorker::operator=(AsyncWorker&& other) {
|
|
4815
4816
|
_env = other._env;
|
|
4816
4817
|
other._env = nullptr;
|
|
4817
4818
|
_work = other._work;
|
|
@@ -4861,7 +4862,8 @@ inline void AsyncWorker::OnOK() {
|
|
|
4861
4862
|
|
|
4862
4863
|
inline void AsyncWorker::OnError(const Error& e) {
|
|
4863
4864
|
if (!_callback.IsEmpty()) {
|
|
4864
|
-
_callback.Call(_receiver.Value(),
|
|
4865
|
+
_callback.Call(_receiver.Value(),
|
|
4866
|
+
std::initializer_list<napi_value>{e.Value()});
|
|
4865
4867
|
}
|
|
4866
4868
|
}
|
|
4867
4869
|
|
|
@@ -4891,9 +4893,9 @@ inline void AsyncWorker::OnExecute(Napi::Env /*DO_NOT_USE*/) {
|
|
|
4891
4893
|
} catch (const std::exception& e) {
|
|
4892
4894
|
SetError(e.what());
|
|
4893
4895
|
}
|
|
4894
|
-
#else
|
|
4896
|
+
#else // NAPI_CPP_EXCEPTIONS
|
|
4895
4897
|
Execute();
|
|
4896
|
-
#endif
|
|
4898
|
+
#endif // NAPI_CPP_EXCEPTIONS
|
|
4897
4899
|
}
|
|
4898
4900
|
|
|
4899
4901
|
inline void AsyncWorker::OnAsyncWorkComplete(napi_env env,
|
|
@@ -4908,8 +4910,7 @@ inline void AsyncWorker::OnWorkComplete(Napi::Env /*env*/, napi_status status) {
|
|
|
4908
4910
|
details::WrapCallback([&] {
|
|
4909
4911
|
if (_error.size() == 0) {
|
|
4910
4912
|
OnOK();
|
|
4911
|
-
}
|
|
4912
|
-
else {
|
|
4913
|
+
} else {
|
|
4913
4914
|
OnError(Error::New(_env, _error));
|
|
4914
4915
|
}
|
|
4915
4916
|
return nullptr;
|
|
@@ -5323,7 +5324,7 @@ template <typename ContextType,
|
|
|
5323
5324
|
typename DataType,
|
|
5324
5325
|
void (*CallJs)(Napi::Env, Napi::Function, ContextType*, DataType*)>
|
|
5325
5326
|
inline napi_status
|
|
5326
|
-
TypedThreadSafeFunction<ContextType, DataType, CallJs>::Release() {
|
|
5327
|
+
TypedThreadSafeFunction<ContextType, DataType, CallJs>::Release() const {
|
|
5327
5328
|
return napi_release_threadsafe_function(_tsfn, napi_tsfn_release);
|
|
5328
5329
|
}
|
|
5329
5330
|
|
|
@@ -5331,7 +5332,7 @@ template <typename ContextType,
|
|
|
5331
5332
|
typename DataType,
|
|
5332
5333
|
void (*CallJs)(Napi::Env, Napi::Function, ContextType*, DataType*)>
|
|
5333
5334
|
inline napi_status
|
|
5334
|
-
TypedThreadSafeFunction<ContextType, DataType, CallJs>::Abort() {
|
|
5335
|
+
TypedThreadSafeFunction<ContextType, DataType, CallJs>::Abort() const {
|
|
5335
5336
|
return napi_release_threadsafe_function(_tsfn, napi_tsfn_abort);
|
|
5336
5337
|
}
|
|
5337
5338
|
|
|
@@ -5412,68 +5413,93 @@ TypedThreadSafeFunction<ContextType, DataType, CallJs>::FunctionOrEmpty(
|
|
|
5412
5413
|
// static
|
|
5413
5414
|
template <typename ResourceString>
|
|
5414
5415
|
inline ThreadSafeFunction ThreadSafeFunction::New(napi_env env,
|
|
5415
|
-
|
|
5416
|
-
|
|
5417
|
-
|
|
5418
|
-
|
|
5419
|
-
return New(
|
|
5420
|
-
|
|
5416
|
+
const Function& callback,
|
|
5417
|
+
ResourceString resourceName,
|
|
5418
|
+
size_t maxQueueSize,
|
|
5419
|
+
size_t initialThreadCount) {
|
|
5420
|
+
return New(
|
|
5421
|
+
env, callback, Object(), resourceName, maxQueueSize, initialThreadCount);
|
|
5421
5422
|
}
|
|
5422
5423
|
|
|
5423
5424
|
// static
|
|
5424
5425
|
template <typename ResourceString, typename ContextType>
|
|
5425
5426
|
inline ThreadSafeFunction ThreadSafeFunction::New(napi_env env,
|
|
5426
|
-
|
|
5427
|
-
|
|
5428
|
-
|
|
5429
|
-
|
|
5430
|
-
|
|
5431
|
-
return New(env,
|
|
5432
|
-
|
|
5427
|
+
const Function& callback,
|
|
5428
|
+
ResourceString resourceName,
|
|
5429
|
+
size_t maxQueueSize,
|
|
5430
|
+
size_t initialThreadCount,
|
|
5431
|
+
ContextType* context) {
|
|
5432
|
+
return New(env,
|
|
5433
|
+
callback,
|
|
5434
|
+
Object(),
|
|
5435
|
+
resourceName,
|
|
5436
|
+
maxQueueSize,
|
|
5437
|
+
initialThreadCount,
|
|
5438
|
+
context);
|
|
5433
5439
|
}
|
|
5434
5440
|
|
|
5435
5441
|
// static
|
|
5436
5442
|
template <typename ResourceString, typename Finalizer>
|
|
5437
5443
|
inline ThreadSafeFunction ThreadSafeFunction::New(napi_env env,
|
|
5438
|
-
|
|
5439
|
-
|
|
5440
|
-
|
|
5441
|
-
|
|
5442
|
-
|
|
5443
|
-
return New(env,
|
|
5444
|
-
|
|
5444
|
+
const Function& callback,
|
|
5445
|
+
ResourceString resourceName,
|
|
5446
|
+
size_t maxQueueSize,
|
|
5447
|
+
size_t initialThreadCount,
|
|
5448
|
+
Finalizer finalizeCallback) {
|
|
5449
|
+
return New(env,
|
|
5450
|
+
callback,
|
|
5451
|
+
Object(),
|
|
5452
|
+
resourceName,
|
|
5453
|
+
maxQueueSize,
|
|
5454
|
+
initialThreadCount,
|
|
5455
|
+
finalizeCallback);
|
|
5445
5456
|
}
|
|
5446
5457
|
|
|
5447
5458
|
// static
|
|
5448
|
-
template <typename ResourceString,
|
|
5459
|
+
template <typename ResourceString,
|
|
5460
|
+
typename Finalizer,
|
|
5449
5461
|
typename FinalizerDataType>
|
|
5450
5462
|
inline ThreadSafeFunction ThreadSafeFunction::New(napi_env env,
|
|
5451
|
-
|
|
5452
|
-
|
|
5453
|
-
|
|
5454
|
-
|
|
5455
|
-
|
|
5456
|
-
|
|
5457
|
-
return New(env,
|
|
5458
|
-
|
|
5463
|
+
const Function& callback,
|
|
5464
|
+
ResourceString resourceName,
|
|
5465
|
+
size_t maxQueueSize,
|
|
5466
|
+
size_t initialThreadCount,
|
|
5467
|
+
Finalizer finalizeCallback,
|
|
5468
|
+
FinalizerDataType* data) {
|
|
5469
|
+
return New(env,
|
|
5470
|
+
callback,
|
|
5471
|
+
Object(),
|
|
5472
|
+
resourceName,
|
|
5473
|
+
maxQueueSize,
|
|
5474
|
+
initialThreadCount,
|
|
5475
|
+
finalizeCallback,
|
|
5476
|
+
data);
|
|
5459
5477
|
}
|
|
5460
5478
|
|
|
5461
5479
|
// static
|
|
5462
5480
|
template <typename ResourceString, typename ContextType, typename Finalizer>
|
|
5463
5481
|
inline ThreadSafeFunction ThreadSafeFunction::New(napi_env env,
|
|
5464
|
-
|
|
5465
|
-
|
|
5466
|
-
|
|
5467
|
-
|
|
5468
|
-
|
|
5469
|
-
|
|
5470
|
-
return New(env,
|
|
5471
|
-
|
|
5482
|
+
const Function& callback,
|
|
5483
|
+
ResourceString resourceName,
|
|
5484
|
+
size_t maxQueueSize,
|
|
5485
|
+
size_t initialThreadCount,
|
|
5486
|
+
ContextType* context,
|
|
5487
|
+
Finalizer finalizeCallback) {
|
|
5488
|
+
return New(env,
|
|
5489
|
+
callback,
|
|
5490
|
+
Object(),
|
|
5491
|
+
resourceName,
|
|
5492
|
+
maxQueueSize,
|
|
5493
|
+
initialThreadCount,
|
|
5494
|
+
context,
|
|
5495
|
+
finalizeCallback);
|
|
5472
5496
|
}
|
|
5473
5497
|
|
|
5474
5498
|
// static
|
|
5475
|
-
template <typename ResourceString,
|
|
5476
|
-
typename
|
|
5499
|
+
template <typename ResourceString,
|
|
5500
|
+
typename ContextType,
|
|
5501
|
+
typename Finalizer,
|
|
5502
|
+
typename FinalizerDataType>
|
|
5477
5503
|
inline ThreadSafeFunction ThreadSafeFunction::New(napi_env env,
|
|
5478
5504
|
const Function& callback,
|
|
5479
5505
|
ResourceString resourceName,
|
|
@@ -5482,89 +5508,128 @@ inline ThreadSafeFunction ThreadSafeFunction::New(napi_env env,
|
|
|
5482
5508
|
ContextType* context,
|
|
5483
5509
|
Finalizer finalizeCallback,
|
|
5484
5510
|
FinalizerDataType* data) {
|
|
5485
|
-
return New(env,
|
|
5486
|
-
|
|
5511
|
+
return New(env,
|
|
5512
|
+
callback,
|
|
5513
|
+
Object(),
|
|
5514
|
+
resourceName,
|
|
5515
|
+
maxQueueSize,
|
|
5516
|
+
initialThreadCount,
|
|
5517
|
+
context,
|
|
5518
|
+
finalizeCallback,
|
|
5519
|
+
data);
|
|
5487
5520
|
}
|
|
5488
5521
|
|
|
5489
5522
|
// static
|
|
5490
5523
|
template <typename ResourceString>
|
|
5491
5524
|
inline ThreadSafeFunction ThreadSafeFunction::New(napi_env env,
|
|
5492
|
-
|
|
5493
|
-
|
|
5494
|
-
|
|
5495
|
-
|
|
5496
|
-
|
|
5497
|
-
return New(env,
|
|
5498
|
-
|
|
5525
|
+
const Function& callback,
|
|
5526
|
+
const Object& resource,
|
|
5527
|
+
ResourceString resourceName,
|
|
5528
|
+
size_t maxQueueSize,
|
|
5529
|
+
size_t initialThreadCount) {
|
|
5530
|
+
return New(env,
|
|
5531
|
+
callback,
|
|
5532
|
+
resource,
|
|
5533
|
+
resourceName,
|
|
5534
|
+
maxQueueSize,
|
|
5535
|
+
initialThreadCount,
|
|
5536
|
+
static_cast<void*>(nullptr) /* context */);
|
|
5499
5537
|
}
|
|
5500
5538
|
|
|
5501
5539
|
// static
|
|
5502
5540
|
template <typename ResourceString, typename ContextType>
|
|
5503
5541
|
inline ThreadSafeFunction ThreadSafeFunction::New(napi_env env,
|
|
5504
|
-
|
|
5505
|
-
|
|
5506
|
-
|
|
5507
|
-
|
|
5508
|
-
|
|
5509
|
-
|
|
5510
|
-
return New(env,
|
|
5511
|
-
|
|
5542
|
+
const Function& callback,
|
|
5543
|
+
const Object& resource,
|
|
5544
|
+
ResourceString resourceName,
|
|
5545
|
+
size_t maxQueueSize,
|
|
5546
|
+
size_t initialThreadCount,
|
|
5547
|
+
ContextType* context) {
|
|
5548
|
+
return New(env,
|
|
5549
|
+
callback,
|
|
5550
|
+
resource,
|
|
5551
|
+
resourceName,
|
|
5552
|
+
maxQueueSize,
|
|
5553
|
+
initialThreadCount,
|
|
5554
|
+
context,
|
|
5512
5555
|
[](Env, ContextType*) {} /* empty finalizer */);
|
|
5513
5556
|
}
|
|
5514
5557
|
|
|
5515
5558
|
// static
|
|
5516
5559
|
template <typename ResourceString, typename Finalizer>
|
|
5517
5560
|
inline ThreadSafeFunction ThreadSafeFunction::New(napi_env env,
|
|
5518
|
-
|
|
5519
|
-
|
|
5520
|
-
|
|
5521
|
-
|
|
5522
|
-
|
|
5523
|
-
|
|
5524
|
-
return New(env,
|
|
5525
|
-
|
|
5526
|
-
|
|
5561
|
+
const Function& callback,
|
|
5562
|
+
const Object& resource,
|
|
5563
|
+
ResourceString resourceName,
|
|
5564
|
+
size_t maxQueueSize,
|
|
5565
|
+
size_t initialThreadCount,
|
|
5566
|
+
Finalizer finalizeCallback) {
|
|
5567
|
+
return New(env,
|
|
5568
|
+
callback,
|
|
5569
|
+
resource,
|
|
5570
|
+
resourceName,
|
|
5571
|
+
maxQueueSize,
|
|
5572
|
+
initialThreadCount,
|
|
5573
|
+
static_cast<void*>(nullptr) /* context */,
|
|
5574
|
+
finalizeCallback,
|
|
5575
|
+
static_cast<void*>(nullptr) /* data */,
|
|
5527
5576
|
details::ThreadSafeFinalize<void, Finalizer>::Wrapper);
|
|
5528
5577
|
}
|
|
5529
5578
|
|
|
5530
5579
|
// static
|
|
5531
|
-
template <typename ResourceString,
|
|
5580
|
+
template <typename ResourceString,
|
|
5581
|
+
typename Finalizer,
|
|
5532
5582
|
typename FinalizerDataType>
|
|
5533
5583
|
inline ThreadSafeFunction ThreadSafeFunction::New(napi_env env,
|
|
5534
|
-
|
|
5535
|
-
|
|
5536
|
-
|
|
5537
|
-
|
|
5538
|
-
|
|
5539
|
-
|
|
5540
|
-
|
|
5541
|
-
return New(env,
|
|
5542
|
-
|
|
5543
|
-
|
|
5544
|
-
|
|
5545
|
-
|
|
5584
|
+
const Function& callback,
|
|
5585
|
+
const Object& resource,
|
|
5586
|
+
ResourceString resourceName,
|
|
5587
|
+
size_t maxQueueSize,
|
|
5588
|
+
size_t initialThreadCount,
|
|
5589
|
+
Finalizer finalizeCallback,
|
|
5590
|
+
FinalizerDataType* data) {
|
|
5591
|
+
return New(env,
|
|
5592
|
+
callback,
|
|
5593
|
+
resource,
|
|
5594
|
+
resourceName,
|
|
5595
|
+
maxQueueSize,
|
|
5596
|
+
initialThreadCount,
|
|
5597
|
+
static_cast<void*>(nullptr) /* context */,
|
|
5598
|
+
finalizeCallback,
|
|
5599
|
+
data,
|
|
5600
|
+
details::ThreadSafeFinalize<void, Finalizer, FinalizerDataType>::
|
|
5601
|
+
FinalizeWrapperWithData);
|
|
5546
5602
|
}
|
|
5547
5603
|
|
|
5548
5604
|
// static
|
|
5549
5605
|
template <typename ResourceString, typename ContextType, typename Finalizer>
|
|
5550
5606
|
inline ThreadSafeFunction ThreadSafeFunction::New(napi_env env,
|
|
5551
|
-
|
|
5552
|
-
|
|
5553
|
-
|
|
5554
|
-
|
|
5555
|
-
|
|
5556
|
-
|
|
5557
|
-
|
|
5558
|
-
return New(
|
|
5559
|
-
|
|
5560
|
-
|
|
5561
|
-
|
|
5562
|
-
|
|
5607
|
+
const Function& callback,
|
|
5608
|
+
const Object& resource,
|
|
5609
|
+
ResourceString resourceName,
|
|
5610
|
+
size_t maxQueueSize,
|
|
5611
|
+
size_t initialThreadCount,
|
|
5612
|
+
ContextType* context,
|
|
5613
|
+
Finalizer finalizeCallback) {
|
|
5614
|
+
return New(
|
|
5615
|
+
env,
|
|
5616
|
+
callback,
|
|
5617
|
+
resource,
|
|
5618
|
+
resourceName,
|
|
5619
|
+
maxQueueSize,
|
|
5620
|
+
initialThreadCount,
|
|
5621
|
+
context,
|
|
5622
|
+
finalizeCallback,
|
|
5623
|
+
static_cast<void*>(nullptr) /* data */,
|
|
5624
|
+
details::ThreadSafeFinalize<ContextType,
|
|
5625
|
+
Finalizer>::FinalizeWrapperWithContext);
|
|
5563
5626
|
}
|
|
5564
5627
|
|
|
5565
5628
|
// static
|
|
5566
|
-
template <typename ResourceString,
|
|
5567
|
-
typename
|
|
5629
|
+
template <typename ResourceString,
|
|
5630
|
+
typename ContextType,
|
|
5631
|
+
typename Finalizer,
|
|
5632
|
+
typename FinalizerDataType>
|
|
5568
5633
|
inline ThreadSafeFunction ThreadSafeFunction::New(napi_env env,
|
|
5569
5634
|
const Function& callback,
|
|
5570
5635
|
const Object& resource,
|
|
@@ -5574,20 +5639,24 @@ inline ThreadSafeFunction ThreadSafeFunction::New(napi_env env,
|
|
|
5574
5639
|
ContextType* context,
|
|
5575
5640
|
Finalizer finalizeCallback,
|
|
5576
5641
|
FinalizerDataType* data) {
|
|
5577
|
-
return New(
|
|
5578
|
-
|
|
5579
|
-
|
|
5580
|
-
|
|
5642
|
+
return New(
|
|
5643
|
+
env,
|
|
5644
|
+
callback,
|
|
5645
|
+
resource,
|
|
5646
|
+
resourceName,
|
|
5647
|
+
maxQueueSize,
|
|
5648
|
+
initialThreadCount,
|
|
5649
|
+
context,
|
|
5650
|
+
finalizeCallback,
|
|
5651
|
+
data,
|
|
5652
|
+
details::ThreadSafeFinalize<ContextType, Finalizer, FinalizerDataType>::
|
|
5653
|
+
FinalizeFinalizeWrapperWithDataAndContext);
|
|
5581
5654
|
}
|
|
5582
5655
|
|
|
5583
|
-
inline ThreadSafeFunction::ThreadSafeFunction()
|
|
5584
|
-
: _tsfn() {
|
|
5585
|
-
}
|
|
5656
|
+
inline ThreadSafeFunction::ThreadSafeFunction() : _tsfn() {}
|
|
5586
5657
|
|
|
5587
|
-
inline ThreadSafeFunction::ThreadSafeFunction(
|
|
5588
|
-
|
|
5589
|
-
: _tsfn(tsfn) {
|
|
5590
|
-
}
|
|
5658
|
+
inline ThreadSafeFunction::ThreadSafeFunction(napi_threadsafe_function tsfn)
|
|
5659
|
+
: _tsfn(tsfn) {}
|
|
5591
5660
|
|
|
5592
5661
|
inline ThreadSafeFunction::operator napi_threadsafe_function() const {
|
|
5593
5662
|
return _tsfn;
|
|
@@ -5598,20 +5667,18 @@ inline napi_status ThreadSafeFunction::BlockingCall() const {
|
|
|
5598
5667
|
}
|
|
5599
5668
|
|
|
5600
5669
|
template <>
|
|
5601
|
-
inline napi_status ThreadSafeFunction::BlockingCall(
|
|
5602
|
-
void* data) const {
|
|
5670
|
+
inline napi_status ThreadSafeFunction::BlockingCall(void* data) const {
|
|
5603
5671
|
return napi_call_threadsafe_function(_tsfn, data, napi_tsfn_blocking);
|
|
5604
5672
|
}
|
|
5605
5673
|
|
|
5606
5674
|
template <typename Callback>
|
|
5607
|
-
inline napi_status ThreadSafeFunction::BlockingCall(
|
|
5608
|
-
Callback callback) const {
|
|
5675
|
+
inline napi_status ThreadSafeFunction::BlockingCall(Callback callback) const {
|
|
5609
5676
|
return CallInternal(new CallbackWrapper(callback), napi_tsfn_blocking);
|
|
5610
5677
|
}
|
|
5611
5678
|
|
|
5612
5679
|
template <typename DataType, typename Callback>
|
|
5613
|
-
inline napi_status ThreadSafeFunction::BlockingCall(
|
|
5614
|
-
|
|
5680
|
+
inline napi_status ThreadSafeFunction::BlockingCall(DataType* data,
|
|
5681
|
+
Callback callback) const {
|
|
5615
5682
|
auto wrapper = [data, callback](Env env, Function jsCallback) {
|
|
5616
5683
|
callback(env, jsCallback, data);
|
|
5617
5684
|
};
|
|
@@ -5623,8 +5690,7 @@ inline napi_status ThreadSafeFunction::NonBlockingCall() const {
|
|
|
5623
5690
|
}
|
|
5624
5691
|
|
|
5625
5692
|
template <>
|
|
5626
|
-
inline napi_status ThreadSafeFunction::NonBlockingCall(
|
|
5627
|
-
void* data) const {
|
|
5693
|
+
inline napi_status ThreadSafeFunction::NonBlockingCall(void* data) const {
|
|
5628
5694
|
return napi_call_threadsafe_function(_tsfn, data, napi_tsfn_nonblocking);
|
|
5629
5695
|
}
|
|
5630
5696
|
|
|
@@ -5661,25 +5727,29 @@ inline napi_status ThreadSafeFunction::Acquire() const {
|
|
|
5661
5727
|
return napi_acquire_threadsafe_function(_tsfn);
|
|
5662
5728
|
}
|
|
5663
5729
|
|
|
5664
|
-
inline napi_status ThreadSafeFunction::Release() {
|
|
5730
|
+
inline napi_status ThreadSafeFunction::Release() const {
|
|
5665
5731
|
return napi_release_threadsafe_function(_tsfn, napi_tsfn_release);
|
|
5666
5732
|
}
|
|
5667
5733
|
|
|
5668
|
-
inline napi_status ThreadSafeFunction::Abort() {
|
|
5734
|
+
inline napi_status ThreadSafeFunction::Abort() const {
|
|
5669
5735
|
return napi_release_threadsafe_function(_tsfn, napi_tsfn_abort);
|
|
5670
5736
|
}
|
|
5671
5737
|
|
|
5672
|
-
inline ThreadSafeFunction::ConvertibleContext
|
|
5673
|
-
|
|
5738
|
+
inline ThreadSafeFunction::ConvertibleContext ThreadSafeFunction::GetContext()
|
|
5739
|
+
const {
|
|
5674
5740
|
void* context;
|
|
5675
5741
|
napi_status status = napi_get_threadsafe_function_context(_tsfn, &context);
|
|
5676
|
-
NAPI_FATAL_IF_FAILED(status,
|
|
5677
|
-
|
|
5742
|
+
NAPI_FATAL_IF_FAILED(status,
|
|
5743
|
+
"ThreadSafeFunction::GetContext",
|
|
5744
|
+
"napi_get_threadsafe_function_context");
|
|
5745
|
+
return ConvertibleContext({context});
|
|
5678
5746
|
}
|
|
5679
5747
|
|
|
5680
5748
|
// static
|
|
5681
|
-
template <typename ResourceString,
|
|
5682
|
-
typename
|
|
5749
|
+
template <typename ResourceString,
|
|
5750
|
+
typename ContextType,
|
|
5751
|
+
typename Finalizer,
|
|
5752
|
+
typename FinalizerDataType>
|
|
5683
5753
|
inline ThreadSafeFunction ThreadSafeFunction::New(napi_env env,
|
|
5684
5754
|
const Function& callback,
|
|
5685
5755
|
const Object& resource,
|
|
@@ -5690,16 +5760,26 @@ inline ThreadSafeFunction ThreadSafeFunction::New(napi_env env,
|
|
|
5690
5760
|
Finalizer finalizeCallback,
|
|
5691
5761
|
FinalizerDataType* data,
|
|
5692
5762
|
napi_finalize wrapper) {
|
|
5693
|
-
static_assert(details::can_make_string<ResourceString>::value
|
|
5694
|
-
|
|
5695
|
-
|
|
5763
|
+
static_assert(details::can_make_string<ResourceString>::value ||
|
|
5764
|
+
std::is_convertible<ResourceString, napi_value>::value,
|
|
5765
|
+
"Resource name should be convertible to the string type");
|
|
5696
5766
|
|
|
5697
5767
|
ThreadSafeFunction tsfn;
|
|
5698
|
-
auto* finalizeData = new details::
|
|
5699
|
-
FinalizerDataType>(
|
|
5700
|
-
|
|
5701
|
-
|
|
5702
|
-
|
|
5768
|
+
auto* finalizeData = new details::
|
|
5769
|
+
ThreadSafeFinalize<ContextType, Finalizer, FinalizerDataType>(
|
|
5770
|
+
{data, finalizeCallback});
|
|
5771
|
+
napi_status status =
|
|
5772
|
+
napi_create_threadsafe_function(env,
|
|
5773
|
+
callback,
|
|
5774
|
+
resource,
|
|
5775
|
+
Value::From(env, resourceName),
|
|
5776
|
+
maxQueueSize,
|
|
5777
|
+
initialThreadCount,
|
|
5778
|
+
finalizeData,
|
|
5779
|
+
wrapper,
|
|
5780
|
+
context,
|
|
5781
|
+
CallJS,
|
|
5782
|
+
&tsfn._tsfn);
|
|
5703
5783
|
if (status != napi_ok) {
|
|
5704
5784
|
delete finalizeData;
|
|
5705
5785
|
NAPI_THROW_IF_FAILED(env, status, ThreadSafeFunction());
|
|
@@ -5711,8 +5791,8 @@ inline ThreadSafeFunction ThreadSafeFunction::New(napi_env env,
|
|
|
5711
5791
|
inline napi_status ThreadSafeFunction::CallInternal(
|
|
5712
5792
|
CallbackWrapper* callbackWrapper,
|
|
5713
5793
|
napi_threadsafe_function_call_mode mode) const {
|
|
5714
|
-
napi_status status =
|
|
5715
|
-
_tsfn, callbackWrapper, mode);
|
|
5794
|
+
napi_status status =
|
|
5795
|
+
napi_call_threadsafe_function(_tsfn, callbackWrapper, mode);
|
|
5716
5796
|
if (status != napi_ok && callbackWrapper != nullptr) {
|
|
5717
5797
|
delete callbackWrapper;
|
|
5718
5798
|
}
|
|
@@ -5742,13 +5822,15 @@ inline void ThreadSafeFunction::CallJS(napi_env env,
|
|
|
5742
5822
|
// Async Progress Worker Base class
|
|
5743
5823
|
////////////////////////////////////////////////////////////////////////////////
|
|
5744
5824
|
template <typename DataType>
|
|
5745
|
-
inline AsyncProgressWorkerBase<DataType>::AsyncProgressWorkerBase(
|
|
5746
|
-
|
|
5747
|
-
|
|
5748
|
-
|
|
5749
|
-
|
|
5750
|
-
|
|
5751
|
-
|
|
5825
|
+
inline AsyncProgressWorkerBase<DataType>::AsyncProgressWorkerBase(
|
|
5826
|
+
const Object& receiver,
|
|
5827
|
+
const Function& callback,
|
|
5828
|
+
const char* resource_name,
|
|
5829
|
+
const Object& resource,
|
|
5830
|
+
size_t queue_size)
|
|
5831
|
+
: AsyncWorker(receiver, callback, resource_name, resource) {
|
|
5832
|
+
// Fill all possible arguments to work around ambiguous
|
|
5833
|
+
// ThreadSafeFunction::New signatures.
|
|
5752
5834
|
_tsfn = ThreadSafeFunction::New(callback.Env(),
|
|
5753
5835
|
callback,
|
|
5754
5836
|
resource,
|
|
@@ -5762,15 +5844,18 @@ inline AsyncProgressWorkerBase<DataType>::AsyncProgressWorkerBase(const Object&
|
|
|
5762
5844
|
|
|
5763
5845
|
#if NAPI_VERSION > 4
|
|
5764
5846
|
template <typename DataType>
|
|
5765
|
-
inline AsyncProgressWorkerBase<DataType>::AsyncProgressWorkerBase(
|
|
5766
|
-
|
|
5767
|
-
|
|
5768
|
-
|
|
5769
|
-
|
|
5847
|
+
inline AsyncProgressWorkerBase<DataType>::AsyncProgressWorkerBase(
|
|
5848
|
+
Napi::Env env,
|
|
5849
|
+
const char* resource_name,
|
|
5850
|
+
const Object& resource,
|
|
5851
|
+
size_t queue_size)
|
|
5852
|
+
: AsyncWorker(env, resource_name, resource) {
|
|
5770
5853
|
// TODO: Once the changes to make the callback optional for threadsafe
|
|
5771
|
-
// functions are available on all versions we can remove the dummy Function
|
|
5854
|
+
// functions are available on all versions we can remove the dummy Function
|
|
5855
|
+
// here.
|
|
5772
5856
|
Function callback;
|
|
5773
|
-
// Fill all possible arguments to work around ambiguous
|
|
5857
|
+
// Fill all possible arguments to work around ambiguous
|
|
5858
|
+
// ThreadSafeFunction::New signatures.
|
|
5774
5859
|
_tsfn = ThreadSafeFunction::New(env,
|
|
5775
5860
|
callback,
|
|
5776
5861
|
resource,
|
|
@@ -5783,38 +5868,45 @@ inline AsyncProgressWorkerBase<DataType>::AsyncProgressWorkerBase(Napi::Env env,
|
|
|
5783
5868
|
}
|
|
5784
5869
|
#endif
|
|
5785
5870
|
|
|
5786
|
-
template<typename DataType>
|
|
5871
|
+
template <typename DataType>
|
|
5787
5872
|
inline AsyncProgressWorkerBase<DataType>::~AsyncProgressWorkerBase() {
|
|
5788
5873
|
// Abort pending tsfn call.
|
|
5789
5874
|
// Don't send progress events after we've already completed.
|
|
5790
|
-
// It's ok to call ThreadSafeFunction::Abort and ThreadSafeFunction::Release
|
|
5875
|
+
// It's ok to call ThreadSafeFunction::Abort and ThreadSafeFunction::Release
|
|
5876
|
+
// duplicated.
|
|
5791
5877
|
_tsfn.Abort();
|
|
5792
5878
|
}
|
|
5793
5879
|
|
|
5794
5880
|
template <typename DataType>
|
|
5795
|
-
inline void AsyncProgressWorkerBase<DataType>::OnAsyncWorkProgress(
|
|
5796
|
-
|
|
5797
|
-
void* data) {
|
|
5881
|
+
inline void AsyncProgressWorkerBase<DataType>::OnAsyncWorkProgress(
|
|
5882
|
+
Napi::Env /* env */, Napi::Function /* jsCallback */, void* data) {
|
|
5798
5883
|
ThreadSafeData* tsd = static_cast<ThreadSafeData*>(data);
|
|
5799
5884
|
tsd->asyncprogressworker()->OnWorkProgress(tsd->data());
|
|
5800
5885
|
delete tsd;
|
|
5801
5886
|
}
|
|
5802
5887
|
|
|
5803
5888
|
template <typename DataType>
|
|
5804
|
-
inline napi_status AsyncProgressWorkerBase<DataType>::NonBlockingCall(
|
|
5889
|
+
inline napi_status AsyncProgressWorkerBase<DataType>::NonBlockingCall(
|
|
5890
|
+
DataType* data) {
|
|
5805
5891
|
auto tsd = new AsyncProgressWorkerBase::ThreadSafeData(this, data);
|
|
5806
|
-
|
|
5892
|
+
auto ret = _tsfn.NonBlockingCall(tsd, OnAsyncWorkProgress);
|
|
5893
|
+
if (ret != napi_ok) {
|
|
5894
|
+
delete tsd;
|
|
5895
|
+
}
|
|
5896
|
+
return ret;
|
|
5807
5897
|
}
|
|
5808
5898
|
|
|
5809
5899
|
template <typename DataType>
|
|
5810
|
-
inline void AsyncProgressWorkerBase<DataType>::OnWorkComplete(
|
|
5900
|
+
inline void AsyncProgressWorkerBase<DataType>::OnWorkComplete(
|
|
5901
|
+
Napi::Env /* env */, napi_status status) {
|
|
5811
5902
|
_work_completed = true;
|
|
5812
5903
|
_complete_status = status;
|
|
5813
5904
|
_tsfn.Release();
|
|
5814
5905
|
}
|
|
5815
5906
|
|
|
5816
5907
|
template <typename DataType>
|
|
5817
|
-
inline void AsyncProgressWorkerBase<DataType>::OnThreadSafeFunctionFinalize(
|
|
5908
|
+
inline void AsyncProgressWorkerBase<DataType>::OnThreadSafeFunctionFinalize(
|
|
5909
|
+
Napi::Env env, void* /* data */, AsyncProgressWorkerBase* context) {
|
|
5818
5910
|
if (context->_work_completed) {
|
|
5819
5911
|
context->AsyncWorker::OnWorkComplete(env, context->_complete_status);
|
|
5820
5912
|
}
|
|
@@ -5823,76 +5915,65 @@ inline void AsyncProgressWorkerBase<DataType>::OnThreadSafeFunctionFinalize(Napi
|
|
|
5823
5915
|
////////////////////////////////////////////////////////////////////////////////
|
|
5824
5916
|
// Async Progress Worker class
|
|
5825
5917
|
////////////////////////////////////////////////////////////////////////////////
|
|
5826
|
-
template<class T>
|
|
5918
|
+
template <class T>
|
|
5827
5919
|
inline AsyncProgressWorker<T>::AsyncProgressWorker(const Function& callback)
|
|
5828
|
-
|
|
5829
|
-
}
|
|
5920
|
+
: AsyncProgressWorker(callback, "generic") {}
|
|
5830
5921
|
|
|
5831
|
-
template<class T>
|
|
5922
|
+
template <class T>
|
|
5832
5923
|
inline AsyncProgressWorker<T>::AsyncProgressWorker(const Function& callback,
|
|
5833
|
-
|
|
5834
|
-
|
|
5835
|
-
}
|
|
5924
|
+
const char* resource_name)
|
|
5925
|
+
: AsyncProgressWorker(
|
|
5926
|
+
callback, resource_name, Object::New(callback.Env())) {}
|
|
5836
5927
|
|
|
5837
|
-
template<class T>
|
|
5928
|
+
template <class T>
|
|
5838
5929
|
inline AsyncProgressWorker<T>::AsyncProgressWorker(const Function& callback,
|
|
5839
|
-
|
|
5840
|
-
|
|
5841
|
-
|
|
5842
|
-
|
|
5843
|
-
resource_name,
|
|
5844
|
-
resource) {
|
|
5845
|
-
}
|
|
5930
|
+
const char* resource_name,
|
|
5931
|
+
const Object& resource)
|
|
5932
|
+
: AsyncProgressWorker(
|
|
5933
|
+
Object::New(callback.Env()), callback, resource_name, resource) {}
|
|
5846
5934
|
|
|
5847
|
-
template<class T>
|
|
5935
|
+
template <class T>
|
|
5848
5936
|
inline AsyncProgressWorker<T>::AsyncProgressWorker(const Object& receiver,
|
|
5849
5937
|
const Function& callback)
|
|
5850
|
-
|
|
5851
|
-
}
|
|
5938
|
+
: AsyncProgressWorker(receiver, callback, "generic") {}
|
|
5852
5939
|
|
|
5853
|
-
template<class T>
|
|
5940
|
+
template <class T>
|
|
5854
5941
|
inline AsyncProgressWorker<T>::AsyncProgressWorker(const Object& receiver,
|
|
5855
5942
|
const Function& callback,
|
|
5856
5943
|
const char* resource_name)
|
|
5857
|
-
|
|
5858
|
-
|
|
5859
|
-
resource_name,
|
|
5860
|
-
Object::New(callback.Env())) {
|
|
5861
|
-
}
|
|
5944
|
+
: AsyncProgressWorker(
|
|
5945
|
+
receiver, callback, resource_name, Object::New(callback.Env())) {}
|
|
5862
5946
|
|
|
5863
|
-
template<class T>
|
|
5947
|
+
template <class T>
|
|
5864
5948
|
inline AsyncProgressWorker<T>::AsyncProgressWorker(const Object& receiver,
|
|
5865
5949
|
const Function& callback,
|
|
5866
5950
|
const char* resource_name,
|
|
5867
5951
|
const Object& resource)
|
|
5868
|
-
|
|
5869
|
-
|
|
5870
|
-
|
|
5871
|
-
}
|
|
5952
|
+
: AsyncProgressWorkerBase(receiver, callback, resource_name, resource),
|
|
5953
|
+
_asyncdata(nullptr),
|
|
5954
|
+
_asyncsize(0),
|
|
5955
|
+
_signaled(false) {}
|
|
5872
5956
|
|
|
5873
5957
|
#if NAPI_VERSION > 4
|
|
5874
|
-
template<class T>
|
|
5958
|
+
template <class T>
|
|
5875
5959
|
inline AsyncProgressWorker<T>::AsyncProgressWorker(Napi::Env env)
|
|
5876
|
-
|
|
5877
|
-
}
|
|
5960
|
+
: AsyncProgressWorker(env, "generic") {}
|
|
5878
5961
|
|
|
5879
|
-
template<class T>
|
|
5962
|
+
template <class T>
|
|
5880
5963
|
inline AsyncProgressWorker<T>::AsyncProgressWorker(Napi::Env env,
|
|
5881
5964
|
const char* resource_name)
|
|
5882
|
-
|
|
5883
|
-
}
|
|
5965
|
+
: AsyncProgressWorker(env, resource_name, Object::New(env)) {}
|
|
5884
5966
|
|
|
5885
|
-
template<class T>
|
|
5967
|
+
template <class T>
|
|
5886
5968
|
inline AsyncProgressWorker<T>::AsyncProgressWorker(Napi::Env env,
|
|
5887
5969
|
const char* resource_name,
|
|
5888
5970
|
const Object& resource)
|
|
5889
|
-
|
|
5890
|
-
|
|
5891
|
-
|
|
5892
|
-
}
|
|
5971
|
+
: AsyncProgressWorkerBase(env, resource_name, resource),
|
|
5972
|
+
_asyncdata(nullptr),
|
|
5973
|
+
_asyncsize(0) {}
|
|
5893
5974
|
#endif
|
|
5894
5975
|
|
|
5895
|
-
template<class T>
|
|
5976
|
+
template <class T>
|
|
5896
5977
|
inline AsyncProgressWorker<T>::~AsyncProgressWorker() {
|
|
5897
5978
|
{
|
|
5898
5979
|
std::lock_guard<std::mutex> lock(this->_mutex);
|
|
@@ -5901,22 +5982,25 @@ inline AsyncProgressWorker<T>::~AsyncProgressWorker() {
|
|
|
5901
5982
|
}
|
|
5902
5983
|
}
|
|
5903
5984
|
|
|
5904
|
-
template<class T>
|
|
5985
|
+
template <class T>
|
|
5905
5986
|
inline void AsyncProgressWorker<T>::Execute() {
|
|
5906
5987
|
ExecutionProgress progress(this);
|
|
5907
5988
|
Execute(progress);
|
|
5908
5989
|
}
|
|
5909
5990
|
|
|
5910
|
-
template<class T>
|
|
5991
|
+
template <class T>
|
|
5911
5992
|
inline void AsyncProgressWorker<T>::OnWorkProgress(void*) {
|
|
5912
5993
|
T* data;
|
|
5913
5994
|
size_t size;
|
|
5995
|
+
bool signaled;
|
|
5914
5996
|
{
|
|
5915
5997
|
std::lock_guard<std::mutex> lock(this->_mutex);
|
|
5916
5998
|
data = this->_asyncdata;
|
|
5917
5999
|
size = this->_asyncsize;
|
|
6000
|
+
signaled = this->_signaled;
|
|
5918
6001
|
this->_asyncdata = nullptr;
|
|
5919
6002
|
this->_asyncsize = 0;
|
|
6003
|
+
this->_signaled = false;
|
|
5920
6004
|
}
|
|
5921
6005
|
|
|
5922
6006
|
/**
|
|
@@ -5926,7 +6010,7 @@ inline void AsyncProgressWorker<T>::OnWorkProgress(void*) {
|
|
|
5926
6010
|
* the deferring the signal of uv_async_t is been sent again, i.e. potential
|
|
5927
6011
|
* not coalesced two calls of the TSFN callback.
|
|
5928
6012
|
*/
|
|
5929
|
-
if (data == nullptr) {
|
|
6013
|
+
if (data == nullptr && !signaled) {
|
|
5930
6014
|
return;
|
|
5931
6015
|
}
|
|
5932
6016
|
|
|
@@ -5934,119 +6018,119 @@ inline void AsyncProgressWorker<T>::OnWorkProgress(void*) {
|
|
|
5934
6018
|
delete[] data;
|
|
5935
6019
|
}
|
|
5936
6020
|
|
|
5937
|
-
template<class T>
|
|
6021
|
+
template <class T>
|
|
5938
6022
|
inline void AsyncProgressWorker<T>::SendProgress_(const T* data, size_t count) {
|
|
5939
|
-
|
|
5940
|
-
|
|
5941
|
-
|
|
5942
|
-
|
|
5943
|
-
|
|
5944
|
-
|
|
5945
|
-
|
|
5946
|
-
|
|
5947
|
-
|
|
5948
|
-
|
|
5949
|
-
|
|
6023
|
+
T* new_data = new T[count];
|
|
6024
|
+
std::copy(data, data + count, new_data);
|
|
6025
|
+
|
|
6026
|
+
T* old_data;
|
|
6027
|
+
{
|
|
6028
|
+
std::lock_guard<std::mutex> lock(this->_mutex);
|
|
6029
|
+
old_data = _asyncdata;
|
|
6030
|
+
_asyncdata = new_data;
|
|
6031
|
+
_asyncsize = count;
|
|
6032
|
+
_signaled = false;
|
|
6033
|
+
}
|
|
6034
|
+
this->NonBlockingCall(nullptr);
|
|
5950
6035
|
|
|
5951
|
-
|
|
6036
|
+
delete[] old_data;
|
|
5952
6037
|
}
|
|
5953
6038
|
|
|
5954
|
-
template<class T>
|
|
5955
|
-
inline void AsyncProgressWorker<T>::Signal()
|
|
6039
|
+
template <class T>
|
|
6040
|
+
inline void AsyncProgressWorker<T>::Signal() {
|
|
6041
|
+
{
|
|
6042
|
+
std::lock_guard<std::mutex> lock(this->_mutex);
|
|
6043
|
+
_signaled = true;
|
|
6044
|
+
}
|
|
5956
6045
|
this->NonBlockingCall(static_cast<T*>(nullptr));
|
|
5957
6046
|
}
|
|
5958
6047
|
|
|
5959
|
-
template<class T>
|
|
6048
|
+
template <class T>
|
|
5960
6049
|
inline void AsyncProgressWorker<T>::ExecutionProgress::Signal() const {
|
|
5961
|
-
_worker->Signal();
|
|
6050
|
+
this->_worker->Signal();
|
|
5962
6051
|
}
|
|
5963
6052
|
|
|
5964
|
-
template<class T>
|
|
5965
|
-
inline void AsyncProgressWorker<T>::ExecutionProgress::Send(
|
|
6053
|
+
template <class T>
|
|
6054
|
+
inline void AsyncProgressWorker<T>::ExecutionProgress::Send(
|
|
6055
|
+
const T* data, size_t count) const {
|
|
5966
6056
|
_worker->SendProgress_(data, count);
|
|
5967
6057
|
}
|
|
5968
6058
|
|
|
5969
6059
|
////////////////////////////////////////////////////////////////////////////////
|
|
5970
6060
|
// Async Progress Queue Worker class
|
|
5971
6061
|
////////////////////////////////////////////////////////////////////////////////
|
|
5972
|
-
template<class T>
|
|
5973
|
-
inline AsyncProgressQueueWorker<T>::AsyncProgressQueueWorker(
|
|
5974
|
-
|
|
5975
|
-
}
|
|
6062
|
+
template <class T>
|
|
6063
|
+
inline AsyncProgressQueueWorker<T>::AsyncProgressQueueWorker(
|
|
6064
|
+
const Function& callback)
|
|
6065
|
+
: AsyncProgressQueueWorker(callback, "generic") {}
|
|
5976
6066
|
|
|
5977
|
-
template<class T>
|
|
5978
|
-
inline AsyncProgressQueueWorker<T>::AsyncProgressQueueWorker(
|
|
5979
|
-
|
|
5980
|
-
|
|
5981
|
-
}
|
|
6067
|
+
template <class T>
|
|
6068
|
+
inline AsyncProgressQueueWorker<T>::AsyncProgressQueueWorker(
|
|
6069
|
+
const Function& callback, const char* resource_name)
|
|
6070
|
+
: AsyncProgressQueueWorker(
|
|
6071
|
+
callback, resource_name, Object::New(callback.Env())) {}
|
|
5982
6072
|
|
|
5983
|
-
template<class T>
|
|
5984
|
-
inline AsyncProgressQueueWorker<T>::AsyncProgressQueueWorker(
|
|
5985
|
-
|
|
5986
|
-
|
|
5987
|
-
|
|
5988
|
-
callback,
|
|
5989
|
-
resource_name,
|
|
5990
|
-
resource) {
|
|
5991
|
-
}
|
|
6073
|
+
template <class T>
|
|
6074
|
+
inline AsyncProgressQueueWorker<T>::AsyncProgressQueueWorker(
|
|
6075
|
+
const Function& callback, const char* resource_name, const Object& resource)
|
|
6076
|
+
: AsyncProgressQueueWorker(
|
|
6077
|
+
Object::New(callback.Env()), callback, resource_name, resource) {}
|
|
5992
6078
|
|
|
5993
|
-
template<class T>
|
|
5994
|
-
inline AsyncProgressQueueWorker<T>::AsyncProgressQueueWorker(
|
|
5995
|
-
|
|
5996
|
-
|
|
5997
|
-
}
|
|
6079
|
+
template <class T>
|
|
6080
|
+
inline AsyncProgressQueueWorker<T>::AsyncProgressQueueWorker(
|
|
6081
|
+
const Object& receiver, const Function& callback)
|
|
6082
|
+
: AsyncProgressQueueWorker(receiver, callback, "generic") {}
|
|
5998
6083
|
|
|
5999
|
-
template<class T>
|
|
6000
|
-
inline AsyncProgressQueueWorker<T>::AsyncProgressQueueWorker(
|
|
6001
|
-
|
|
6002
|
-
|
|
6003
|
-
|
|
6004
|
-
callback,
|
|
6005
|
-
resource_name,
|
|
6006
|
-
Object::New(callback.Env())) {
|
|
6007
|
-
}
|
|
6084
|
+
template <class T>
|
|
6085
|
+
inline AsyncProgressQueueWorker<T>::AsyncProgressQueueWorker(
|
|
6086
|
+
const Object& receiver, const Function& callback, const char* resource_name)
|
|
6087
|
+
: AsyncProgressQueueWorker(
|
|
6088
|
+
receiver, callback, resource_name, Object::New(callback.Env())) {}
|
|
6008
6089
|
|
|
6009
|
-
template<class T>
|
|
6010
|
-
inline AsyncProgressQueueWorker<T>::AsyncProgressQueueWorker(
|
|
6011
|
-
|
|
6012
|
-
|
|
6013
|
-
|
|
6014
|
-
|
|
6015
|
-
|
|
6090
|
+
template <class T>
|
|
6091
|
+
inline AsyncProgressQueueWorker<T>::AsyncProgressQueueWorker(
|
|
6092
|
+
const Object& receiver,
|
|
6093
|
+
const Function& callback,
|
|
6094
|
+
const char* resource_name,
|
|
6095
|
+
const Object& resource)
|
|
6096
|
+
: AsyncProgressWorkerBase<std::pair<T*, size_t>>(
|
|
6097
|
+
receiver,
|
|
6098
|
+
callback,
|
|
6099
|
+
resource_name,
|
|
6100
|
+
resource,
|
|
6101
|
+
/** unlimited queue size */ 0) {}
|
|
6016
6102
|
|
|
6017
6103
|
#if NAPI_VERSION > 4
|
|
6018
|
-
template<class T>
|
|
6104
|
+
template <class T>
|
|
6019
6105
|
inline AsyncProgressQueueWorker<T>::AsyncProgressQueueWorker(Napi::Env env)
|
|
6020
|
-
|
|
6021
|
-
}
|
|
6106
|
+
: AsyncProgressQueueWorker(env, "generic") {}
|
|
6022
6107
|
|
|
6023
|
-
template<class T>
|
|
6024
|
-
inline AsyncProgressQueueWorker<T>::AsyncProgressQueueWorker(
|
|
6025
|
-
|
|
6026
|
-
|
|
6027
|
-
}
|
|
6108
|
+
template <class T>
|
|
6109
|
+
inline AsyncProgressQueueWorker<T>::AsyncProgressQueueWorker(
|
|
6110
|
+
Napi::Env env, const char* resource_name)
|
|
6111
|
+
: AsyncProgressQueueWorker(env, resource_name, Object::New(env)) {}
|
|
6028
6112
|
|
|
6029
|
-
template<class T>
|
|
6030
|
-
inline AsyncProgressQueueWorker<T>::AsyncProgressQueueWorker(
|
|
6031
|
-
|
|
6032
|
-
|
|
6033
|
-
|
|
6034
|
-
}
|
|
6113
|
+
template <class T>
|
|
6114
|
+
inline AsyncProgressQueueWorker<T>::AsyncProgressQueueWorker(
|
|
6115
|
+
Napi::Env env, const char* resource_name, const Object& resource)
|
|
6116
|
+
: AsyncProgressWorkerBase<std::pair<T*, size_t>>(
|
|
6117
|
+
env, resource_name, resource, /** unlimited queue size */ 0) {}
|
|
6035
6118
|
#endif
|
|
6036
6119
|
|
|
6037
|
-
template<class T>
|
|
6120
|
+
template <class T>
|
|
6038
6121
|
inline void AsyncProgressQueueWorker<T>::Execute() {
|
|
6039
6122
|
ExecutionProgress progress(this);
|
|
6040
6123
|
Execute(progress);
|
|
6041
6124
|
}
|
|
6042
6125
|
|
|
6043
|
-
template<class T>
|
|
6044
|
-
inline void AsyncProgressQueueWorker<T>::OnWorkProgress(
|
|
6126
|
+
template <class T>
|
|
6127
|
+
inline void AsyncProgressQueueWorker<T>::OnWorkProgress(
|
|
6128
|
+
std::pair<T*, size_t>* datapair) {
|
|
6045
6129
|
if (datapair == nullptr) {
|
|
6046
6130
|
return;
|
|
6047
6131
|
}
|
|
6048
6132
|
|
|
6049
|
-
T
|
|
6133
|
+
T* data = datapair->first;
|
|
6050
6134
|
size_t size = datapair->second;
|
|
6051
6135
|
|
|
6052
6136
|
this->OnProgress(data, size);
|
|
@@ -6054,33 +6138,36 @@ inline void AsyncProgressQueueWorker<T>::OnWorkProgress(std::pair<T*, size_t>* d
|
|
|
6054
6138
|
delete[] data;
|
|
6055
6139
|
}
|
|
6056
6140
|
|
|
6057
|
-
template<class T>
|
|
6058
|
-
inline void AsyncProgressQueueWorker<T>::SendProgress_(const T* data,
|
|
6059
|
-
|
|
6060
|
-
|
|
6141
|
+
template <class T>
|
|
6142
|
+
inline void AsyncProgressQueueWorker<T>::SendProgress_(const T* data,
|
|
6143
|
+
size_t count) {
|
|
6144
|
+
T* new_data = new T[count];
|
|
6145
|
+
std::copy(data, data + count, new_data);
|
|
6061
6146
|
|
|
6062
|
-
|
|
6063
|
-
|
|
6147
|
+
auto pair = new std::pair<T*, size_t>(new_data, count);
|
|
6148
|
+
this->NonBlockingCall(pair);
|
|
6064
6149
|
}
|
|
6065
6150
|
|
|
6066
|
-
template<class T>
|
|
6151
|
+
template <class T>
|
|
6067
6152
|
inline void AsyncProgressQueueWorker<T>::Signal() const {
|
|
6068
|
-
this->
|
|
6153
|
+
this->SendProgress_(static_cast<T*>(nullptr), 0);
|
|
6069
6154
|
}
|
|
6070
6155
|
|
|
6071
|
-
template<class T>
|
|
6072
|
-
inline void AsyncProgressQueueWorker<T>::OnWorkComplete(Napi::Env env,
|
|
6156
|
+
template <class T>
|
|
6157
|
+
inline void AsyncProgressQueueWorker<T>::OnWorkComplete(Napi::Env env,
|
|
6158
|
+
napi_status status) {
|
|
6073
6159
|
// Draining queued items in TSFN.
|
|
6074
6160
|
AsyncProgressWorkerBase<std::pair<T*, size_t>>::OnWorkComplete(env, status);
|
|
6075
6161
|
}
|
|
6076
6162
|
|
|
6077
|
-
template<class T>
|
|
6163
|
+
template <class T>
|
|
6078
6164
|
inline void AsyncProgressQueueWorker<T>::ExecutionProgress::Signal() const {
|
|
6079
|
-
_worker->
|
|
6165
|
+
_worker->SendProgress_(static_cast<T*>(nullptr), 0);
|
|
6080
6166
|
}
|
|
6081
6167
|
|
|
6082
|
-
template<class T>
|
|
6083
|
-
inline void AsyncProgressQueueWorker<T>::ExecutionProgress::Send(
|
|
6168
|
+
template <class T>
|
|
6169
|
+
inline void AsyncProgressQueueWorker<T>::ExecutionProgress::Send(
|
|
6170
|
+
const T* data, size_t count) const {
|
|
6084
6171
|
_worker->SendProgress_(data, count);
|
|
6085
6172
|
}
|
|
6086
6173
|
#endif // NAPI_VERSION > 3 && !defined(__wasm32__)
|
|
@@ -6089,9 +6176,11 @@ inline void AsyncProgressQueueWorker<T>::ExecutionProgress::Send(const T* data,
|
|
|
6089
6176
|
// Memory Management class
|
|
6090
6177
|
////////////////////////////////////////////////////////////////////////////////
|
|
6091
6178
|
|
|
6092
|
-
inline int64_t MemoryManagement::AdjustExternalMemory(Env env,
|
|
6179
|
+
inline int64_t MemoryManagement::AdjustExternalMemory(Env env,
|
|
6180
|
+
int64_t change_in_bytes) {
|
|
6093
6181
|
int64_t result;
|
|
6094
|
-
napi_status status =
|
|
6182
|
+
napi_status status =
|
|
6183
|
+
napi_adjust_external_memory(env, change_in_bytes, &result);
|
|
6095
6184
|
NAPI_THROW_IF_FAILED(env, status, 0);
|
|
6096
6185
|
return result;
|
|
6097
6186
|
}
|
|
@@ -6132,24 +6221,20 @@ inline T* Addon<T>::Unwrap(Object wrapper) {
|
|
|
6132
6221
|
}
|
|
6133
6222
|
|
|
6134
6223
|
template <typename T>
|
|
6135
|
-
inline void
|
|
6136
|
-
|
|
6137
|
-
const std::initializer_list<AddonProp>& props) {
|
|
6224
|
+
inline void Addon<T>::DefineAddon(
|
|
6225
|
+
Object exports, const std::initializer_list<AddonProp>& props) {
|
|
6138
6226
|
DefineProperties(exports, props);
|
|
6139
6227
|
entry_point_ = exports;
|
|
6140
6228
|
}
|
|
6141
6229
|
|
|
6142
6230
|
template <typename T>
|
|
6143
|
-
inline Napi::Object
|
|
6144
|
-
|
|
6145
|
-
const std::initializer_list<AddonProp>& props) {
|
|
6231
|
+
inline Napi::Object Addon<T>::DefineProperties(
|
|
6232
|
+
Object object, const std::initializer_list<AddonProp>& props) {
|
|
6146
6233
|
const napi_property_descriptor* properties =
|
|
6147
|
-
|
|
6234
|
+
reinterpret_cast<const napi_property_descriptor*>(props.begin());
|
|
6148
6235
|
size_t size = props.size();
|
|
6149
|
-
napi_status status =
|
|
6150
|
-
|
|
6151
|
-
size,
|
|
6152
|
-
properties);
|
|
6236
|
+
napi_status status =
|
|
6237
|
+
napi_define_properties(object.Env(), object, size, properties);
|
|
6153
6238
|
NAPI_THROW_IF_FAILED(object.Env(), status, object);
|
|
6154
6239
|
for (size_t idx = 0; idx < size; idx++)
|
|
6155
6240
|
T::AttachPropData(object.Env(), object, &properties[idx]);
|
|
@@ -6168,6 +6253,11 @@ Env::CleanupHook<Hook> Env::AddCleanupHook(Hook hook) {
|
|
|
6168
6253
|
return CleanupHook<Hook>(*this, hook);
|
|
6169
6254
|
}
|
|
6170
6255
|
|
|
6256
|
+
template <typename Hook, typename Arg>
|
|
6257
|
+
Env::CleanupHook<Hook, Arg>::CleanupHook() {
|
|
6258
|
+
data = nullptr;
|
|
6259
|
+
}
|
|
6260
|
+
|
|
6171
6261
|
template <typename Hook, typename Arg>
|
|
6172
6262
|
Env::CleanupHook<Hook, Arg>::CleanupHook(Napi::Env env, Hook hook)
|
|
6173
6263
|
: wrapper(Env::CleanupHook<Hook, Arg>::Wrapper) {
|
|
@@ -6204,6 +6294,10 @@ bool Env::CleanupHook<Hook, Arg>::IsEmpty() const {
|
|
|
6204
6294
|
}
|
|
6205
6295
|
#endif // NAPI_VERSION > 2
|
|
6206
6296
|
|
|
6207
|
-
|
|
6297
|
+
#ifdef NAPI_CPP_CUSTOM_NAMESPACE
|
|
6298
|
+
} // namespace NAPI_CPP_CUSTOM_NAMESPACE
|
|
6299
|
+
#endif
|
|
6300
|
+
|
|
6301
|
+
} // namespace Napi
|
|
6208
6302
|
|
|
6209
|
-
#endif
|
|
6303
|
+
#endif // SRC_NAPI_INL_H_
|