forked from nodejs/node-addon-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
napi-inl.h
5084 lines (4354 loc) · 163 KB
/
napi-inl.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#ifndef SRC_NAPI_INL_H_
#define SRC_NAPI_INL_H_
////////////////////////////////////////////////////////////////////////////////
// N-API C++ Wrapper Classes
//
// Inline header-only implementations for "N-API" ABI-stable C APIs for Node.js.
////////////////////////////////////////////////////////////////////////////////
// Note: Do not include this file directly! Include "napi.h" instead.
#include <algorithm>
#include <cstring>
#include <mutex>
#include <type_traits>
namespace Napi {
// Helpers to handle functions exposed from C++.
namespace details {
// Attach a data item to an object and delete it when the object gets
// garbage-collected.
// TODO: Replace this code with `napi_add_finalizer()` whenever it becomes
// available on all supported versions of Node.js.
template <typename FreeType>
static inline napi_status AttachData(napi_env env,
napi_value obj,
FreeType* data,
napi_finalize finalizer = nullptr,
void* hint = nullptr) {
napi_status status;
if (finalizer == nullptr) {
finalizer = [](napi_env /*env*/, void* data, void* /*hint*/) {
delete static_cast<FreeType*>(data);
};
}
#if (NAPI_VERSION < 5)
napi_value symbol, external;
status = napi_create_symbol(env, nullptr, &symbol);
if (status == napi_ok) {
status = napi_create_external(env,
data,
finalizer,
hint,
&external);
if (status == napi_ok) {
napi_property_descriptor desc = {
nullptr,
symbol,
nullptr,
nullptr,
nullptr,
external,
napi_default,
nullptr
};
status = napi_define_properties(env, obj, 1, &desc);
}
}
#else // NAPI_VERSION >= 5
status = napi_add_finalizer(env, obj, data, finalizer, hint, nullptr);
#endif
return status;
}
// For use in JS to C++ callback wrappers to catch any Napi::Error exceptions
// and rethrow them as JavaScript exceptions before returning from the callback.
template <typename Callable>
inline napi_value WrapCallback(Callable callback) {
#ifdef NAPI_CPP_EXCEPTIONS
try {
return callback();
} catch (const Error& e) {
e.ThrowAsJavaScriptException();
return nullptr;
}
#else // NAPI_CPP_EXCEPTIONS
// When C++ exceptions are disabled, errors are immediately thrown as JS
// exceptions, so there is no need to catch and rethrow them here.
return callback();
#endif // NAPI_CPP_EXCEPTIONS
}
// For use in JS to C++ void callback wrappers to catch any Napi::Error
// exceptions and rethrow them as JavaScript exceptions before returning from the
// callback.
template <typename Callable>
inline void WrapVoidCallback(Callable callback) {
#ifdef NAPI_CPP_EXCEPTIONS
try {
callback();
} catch (const Error& e) {
e.ThrowAsJavaScriptException();
}
#else // NAPI_CPP_EXCEPTIONS
// When C++ exceptions are disabled, errors are immediately thrown as JS
// exceptions, so there is no need to catch and rethrow them here.
callback();
#endif // NAPI_CPP_EXCEPTIONS
}
template <typename Callable, typename Return>
struct CallbackData {
static inline
napi_value Wrapper(napi_env env, napi_callback_info info) {
return details::WrapCallback([&] {
CallbackInfo callbackInfo(env, info);
CallbackData* callbackData =
static_cast<CallbackData*>(callbackInfo.Data());
callbackInfo.SetData(callbackData->data);
return callbackData->callback(callbackInfo);
});
}
Callable callback;
void* data;
};
template <typename Callable>
struct CallbackData<Callable, void> {
static inline
napi_value Wrapper(napi_env env, napi_callback_info info) {
return details::WrapCallback([&] {
CallbackInfo callbackInfo(env, info);
CallbackData* callbackData =
static_cast<CallbackData*>(callbackInfo.Data());
callbackInfo.SetData(callbackData->data);
callbackData->callback(callbackInfo);
return nullptr;
});
}
Callable callback;
void* data;
};
template <void (*Callback)(const CallbackInfo& info)>
static napi_value
TemplatedVoidCallback(napi_env env, napi_callback_info info) NAPI_NOEXCEPT {
return details::WrapCallback([&] {
CallbackInfo cbInfo(env, info);
Callback(cbInfo);
return nullptr;
});
}
template <Napi::Value (*Callback)(const CallbackInfo& info)>
static napi_value
TemplatedCallback(napi_env env, napi_callback_info info) NAPI_NOEXCEPT {
return details::WrapCallback([&] {
CallbackInfo cbInfo(env, info);
return Callback(cbInfo);
});
}
template <typename T,
Napi::Value (T::*UnwrapCallback)(const CallbackInfo& info)>
static napi_value
TemplatedInstanceCallback(napi_env env, napi_callback_info info) NAPI_NOEXCEPT {
return details::WrapCallback([&] {
CallbackInfo cbInfo(env, info);
T* instance = T::Unwrap(cbInfo.This().As<Object>());
return (instance->*UnwrapCallback)(cbInfo);
});
}
template <typename T, void (T::*UnwrapCallback)(const CallbackInfo& info)>
static napi_value
TemplatedInstanceVoidCallback(napi_env env,
napi_callback_info info) NAPI_NOEXCEPT {
return details::WrapCallback([&] {
CallbackInfo cbInfo(env, info);
T* instance = T::Unwrap(cbInfo.This().As<Object>());
(instance->*UnwrapCallback)(cbInfo);
return nullptr;
});
}
template <typename T, typename Finalizer, typename Hint = void>
struct FinalizeData {
static inline
void Wrapper(napi_env env, void* data, void* finalizeHint) noexcept {
WrapVoidCallback([&] {
FinalizeData* finalizeData = static_cast<FinalizeData*>(finalizeHint);
finalizeData->callback(Env(env), static_cast<T*>(data));
delete finalizeData;
});
}
static inline
void WrapperWithHint(napi_env env, void* data, void* finalizeHint) noexcept {
WrapVoidCallback([&] {
FinalizeData* finalizeData = static_cast<FinalizeData*>(finalizeHint);
finalizeData->callback(Env(env), static_cast<T*>(data), finalizeData->hint);
delete finalizeData;
});
}
Finalizer callback;
Hint* hint;
};
#if (NAPI_VERSION > 3 && !defined(__wasm32__))
template <typename ContextType=void,
typename Finalizer=std::function<void(Env, void*, ContextType*)>,
typename FinalizerDataType=void>
struct ThreadSafeFinalize {
static inline
void Wrapper(napi_env env, void* rawFinalizeData, void* /* rawContext */) {
if (rawFinalizeData == nullptr)
return;
ThreadSafeFinalize* finalizeData =
static_cast<ThreadSafeFinalize*>(rawFinalizeData);
finalizeData->callback(Env(env));
delete finalizeData;
}
static inline
void FinalizeWrapperWithData(napi_env env,
void* rawFinalizeData,
void* /* rawContext */) {
if (rawFinalizeData == nullptr)
return;
ThreadSafeFinalize* finalizeData =
static_cast<ThreadSafeFinalize*>(rawFinalizeData);
finalizeData->callback(Env(env), finalizeData->data);
delete finalizeData;
}
static inline
void FinalizeWrapperWithContext(napi_env env,
void* rawFinalizeData,
void* rawContext) {
if (rawFinalizeData == nullptr)
return;
ThreadSafeFinalize* finalizeData =
static_cast<ThreadSafeFinalize*>(rawFinalizeData);
finalizeData->callback(Env(env), static_cast<ContextType*>(rawContext));
delete finalizeData;
}
static inline
void FinalizeFinalizeWrapperWithDataAndContext(napi_env env,
void* rawFinalizeData,
void* rawContext) {
if (rawFinalizeData == nullptr)
return;
ThreadSafeFinalize* finalizeData =
static_cast<ThreadSafeFinalize*>(rawFinalizeData);
finalizeData->callback(Env(env), finalizeData->data,
static_cast<ContextType*>(rawContext));
delete finalizeData;
}
FinalizerDataType* data;
Finalizer callback;
};
#endif // NAPI_VERSION > 3 && !defined(__wasm32__)
template <typename Getter, typename Setter>
struct AccessorCallbackData {
static inline
napi_value GetterWrapper(napi_env env, napi_callback_info info) {
return details::WrapCallback([&] {
CallbackInfo callbackInfo(env, info);
AccessorCallbackData* callbackData =
static_cast<AccessorCallbackData*>(callbackInfo.Data());
callbackInfo.SetData(callbackData->data);
return callbackData->getterCallback(callbackInfo);
});
}
static inline
napi_value SetterWrapper(napi_env env, napi_callback_info info) {
return details::WrapCallback([&] {
CallbackInfo callbackInfo(env, info);
AccessorCallbackData* callbackData =
static_cast<AccessorCallbackData*>(callbackInfo.Data());
callbackInfo.SetData(callbackData->data);
callbackData->setterCallback(callbackInfo);
return nullptr;
});
}
Getter getterCallback;
Setter setterCallback;
void* data;
};
} // namespace details
#ifndef NODE_ADDON_API_DISABLE_DEPRECATED
# include "napi-inl.deprecated.h"
#endif // !NODE_ADDON_API_DISABLE_DEPRECATED
////////////////////////////////////////////////////////////////////////////////
// Module registration
////////////////////////////////////////////////////////////////////////////////
// Register an add-on based on an initializer function.
#define NODE_API_MODULE(modname, regfunc) \
napi_value __napi_ ## regfunc(napi_env env, \
napi_value exports) { \
return Napi::RegisterModule(env, exports, regfunc); \
} \
NAPI_MODULE(modname, __napi_ ## regfunc)
// Register an add-on based on a subclass of `Addon<T>` with a custom Node.js
// module name.
#define NODE_API_NAMED_ADDON(modname, classname) \
static napi_value __napi_ ## classname(napi_env env, \
napi_value exports) { \
return Napi::RegisterModule(env, exports, &classname::Init); \
} \
NAPI_MODULE(modname, __napi_ ## classname)
// Register an add-on based on a subclass of `Addon<T>` with the Node.js module
// name given by node-gyp from the `target_name` in binding.gyp.
#define NODE_API_ADDON(classname) \
NODE_API_NAMED_ADDON(NODE_GYP_MODULE_NAME, classname)
// Adapt the NAPI_MODULE registration function:
// - Wrap the arguments in NAPI wrappers.
// - Catch any NAPI errors and rethrow as JS exceptions.
inline napi_value RegisterModule(napi_env env,
napi_value exports,
ModuleRegisterCallback registerCallback) {
return details::WrapCallback([&] {
return napi_value(registerCallback(Napi::Env(env),
Napi::Object(env, exports)));
});
}
////////////////////////////////////////////////////////////////////////////////
// Env class
////////////////////////////////////////////////////////////////////////////////
inline Env::Env(napi_env env) : _env(env) {
}
inline Env::operator napi_env() const {
return _env;
}
inline Object Env::Global() const {
napi_value value;
napi_status status = napi_get_global(*this, &value);
NAPI_THROW_IF_FAILED(*this, status, Object());
return Object(*this, value);
}
inline Value Env::Undefined() const {
napi_value value;
napi_status status = napi_get_undefined(*this, &value);
NAPI_THROW_IF_FAILED(*this, status, Value());
return Value(*this, value);
}
inline Value Env::Null() const {
napi_value value;
napi_status status = napi_get_null(*this, &value);
NAPI_THROW_IF_FAILED(*this, status, Value());
return Value(*this, value);
}
inline bool Env::IsExceptionPending() const {
bool result;
napi_status status = napi_is_exception_pending(_env, &result);
if (status != napi_ok) result = false; // Checking for a pending exception shouldn't throw.
return result;
}
inline Error Env::GetAndClearPendingException() {
napi_value value;
napi_status status = napi_get_and_clear_last_exception(_env, &value);
if (status != napi_ok) {
// Don't throw another exception when failing to get the exception!
return Error();
}
return Error(_env, value);
}
inline Value Env::RunScript(const char* utf8script) {
String script = String::New(_env, utf8script);
return RunScript(script);
}
inline Value Env::RunScript(const std::string& utf8script) {
return RunScript(utf8script.c_str());
}
inline Value Env::RunScript(String script) {
napi_value result;
napi_status status = napi_run_script(_env, script, &result);
NAPI_THROW_IF_FAILED(_env, status, Undefined());
return Value(_env, result);
}
#if NAPI_VERSION > 5
template <typename T, Env::Finalizer<T> fini>
inline void Env::SetInstanceData(T* data) {
napi_status status =
napi_set_instance_data(_env, data, [](napi_env env, void* data, void*) {
fini(env, static_cast<T*>(data));
}, nullptr);
NAPI_THROW_IF_FAILED_VOID(_env, status);
}
template <typename DataType,
typename HintType,
Napi::Env::FinalizerWithHint<DataType, HintType> fini>
inline void Env::SetInstanceData(DataType* data, HintType* hint) {
napi_status status =
napi_set_instance_data(_env, data,
[](napi_env env, void* data, void* hint) {
fini(env, static_cast<DataType*>(data), static_cast<HintType*>(hint));
}, hint);
NAPI_THROW_IF_FAILED_VOID(_env, status);
}
template <typename T>
inline T* Env::GetInstanceData() {
void* data = nullptr;
napi_status status = napi_get_instance_data(_env, &data);
NAPI_THROW_IF_FAILED(_env, status, nullptr);
return static_cast<T*>(data);
}
template <typename T> void Env::DefaultFini(Env, T* data) {
delete data;
}
template <typename DataType, typename HintType>
void Env::DefaultFiniWithHint(Env, DataType* data, HintType*) {
delete data;
}
#endif // NAPI_VERSION > 5
////////////////////////////////////////////////////////////////////////////////
// Value class
////////////////////////////////////////////////////////////////////////////////
inline Value::Value() : _env(nullptr), _value(nullptr) {
}
inline Value::Value(napi_env env, napi_value value) : _env(env), _value(value) {
}
inline Value::operator napi_value() const {
return _value;
}
inline bool Value::operator ==(const Value& other) const {
return StrictEquals(other);
}
inline bool Value::operator !=(const Value& other) const {
return !this->operator ==(other);
}
inline bool Value::StrictEquals(const Value& other) const {
bool result;
napi_status status = napi_strict_equals(_env, *this, other, &result);
NAPI_THROW_IF_FAILED(_env, status, false);
return result;
}
inline Napi::Env Value::Env() const {
return Napi::Env(_env);
}
inline bool Value::IsEmpty() const {
return _value == nullptr;
}
inline napi_valuetype Value::Type() const {
if (IsEmpty()) {
return napi_undefined;
}
napi_valuetype type;
napi_status status = napi_typeof(_env, _value, &type);
NAPI_THROW_IF_FAILED(_env, status, napi_undefined);
return type;
}
inline bool Value::IsUndefined() const {
return Type() == napi_undefined;
}
inline bool Value::IsNull() const {
return Type() == napi_null;
}
inline bool Value::IsBoolean() const {
return Type() == napi_boolean;
}
inline bool Value::IsNumber() const {
return Type() == napi_number;
}
#if NAPI_VERSION > 5
inline bool Value::IsBigInt() const {
return Type() == napi_bigint;
}
#endif // NAPI_VERSION > 5
#if (NAPI_VERSION > 4)
inline bool Value::IsDate() const {
if (IsEmpty()) {
return false;
}
bool result;
napi_status status = napi_is_date(_env, _value, &result);
NAPI_THROW_IF_FAILED(_env, status, false);
return result;
}
#endif
inline bool Value::IsString() const {
return Type() == napi_string;
}
inline bool Value::IsSymbol() const {
return Type() == napi_symbol;
}
inline bool Value::IsArray() const {
if (IsEmpty()) {
return false;
}
bool result;
napi_status status = napi_is_array(_env, _value, &result);
NAPI_THROW_IF_FAILED(_env, status, false);
return result;
}
inline bool Value::IsArrayBuffer() const {
if (IsEmpty()) {
return false;
}
bool result;
napi_status status = napi_is_arraybuffer(_env, _value, &result);
NAPI_THROW_IF_FAILED(_env, status, false);
return result;
}
inline bool Value::IsTypedArray() const {
if (IsEmpty()) {
return false;
}
bool result;
napi_status status = napi_is_typedarray(_env, _value, &result);
NAPI_THROW_IF_FAILED(_env, status, false);
return result;
}
inline bool Value::IsObject() const {
return Type() == napi_object || IsFunction();
}
inline bool Value::IsFunction() const {
return Type() == napi_function;
}
inline bool Value::IsPromise() const {
if (IsEmpty()) {
return false;
}
bool result;
napi_status status = napi_is_promise(_env, _value, &result);
NAPI_THROW_IF_FAILED(_env, status, false);
return result;
}
inline bool Value::IsDataView() const {
if (IsEmpty()) {
return false;
}
bool result;
napi_status status = napi_is_dataview(_env, _value, &result);
NAPI_THROW_IF_FAILED(_env, status, false);
return result;
}
inline bool Value::IsBuffer() const {
if (IsEmpty()) {
return false;
}
bool result;
napi_status status = napi_is_buffer(_env, _value, &result);
NAPI_THROW_IF_FAILED(_env, status, false);
return result;
}
inline bool Value::IsExternal() const {
return Type() == napi_external;
}
template <typename T>
inline T Value::As() const {
return T(_env, _value);
}
inline Boolean Value::ToBoolean() const {
napi_value result;
napi_status status = napi_coerce_to_bool(_env, _value, &result);
NAPI_THROW_IF_FAILED(_env, status, Boolean());
return Boolean(_env, result);
}
inline Number Value::ToNumber() const {
napi_value result;
napi_status status = napi_coerce_to_number(_env, _value, &result);
NAPI_THROW_IF_FAILED(_env, status, Number());
return Number(_env, result);
}
inline String Value::ToString() const {
napi_value result;
napi_status status = napi_coerce_to_string(_env, _value, &result);
NAPI_THROW_IF_FAILED(_env, status, String());
return String(_env, result);
}
inline Object Value::ToObject() const {
napi_value result;
napi_status status = napi_coerce_to_object(_env, _value, &result);
NAPI_THROW_IF_FAILED(_env, status, Object());
return Object(_env, result);
}
////////////////////////////////////////////////////////////////////////////////
// Boolean class
////////////////////////////////////////////////////////////////////////////////
inline Boolean Boolean::New(napi_env env, bool val) {
napi_value value;
napi_status status = napi_get_boolean(env, val, &value);
NAPI_THROW_IF_FAILED(env, status, Boolean());
return Boolean(env, value);
}
inline Boolean::Boolean() : Napi::Value() {
}
inline Boolean::Boolean(napi_env env, napi_value value) : Napi::Value(env, value) {
}
inline Boolean::operator bool() const {
return Value();
}
inline bool Boolean::Value() const {
bool result;
napi_status status = napi_get_value_bool(_env, _value, &result);
NAPI_THROW_IF_FAILED(_env, status, false);
return result;
}
////////////////////////////////////////////////////////////////////////////////
// Number class
////////////////////////////////////////////////////////////////////////////////
inline Number Number::New(napi_env env, double val) {
napi_value value;
napi_status status = napi_create_double(env, val, &value);
NAPI_THROW_IF_FAILED(env, status, Number());
return Number(env, value);
}
inline Number::Number() : Value() {
}
inline Number::Number(napi_env env, napi_value value) : Value(env, value) {
}
inline Number::operator int32_t() const {
return Int32Value();
}
inline Number::operator uint32_t() const {
return Uint32Value();
}
inline Number::operator int64_t() const {
return Int64Value();
}
inline Number::operator float() const {
return FloatValue();
}
inline Number::operator double() const {
return DoubleValue();
}
inline int32_t Number::Int32Value() const {
int32_t result;
napi_status status = napi_get_value_int32(_env, _value, &result);
NAPI_THROW_IF_FAILED(_env, status, 0);
return result;
}
inline uint32_t Number::Uint32Value() const {
uint32_t result;
napi_status status = napi_get_value_uint32(_env, _value, &result);
NAPI_THROW_IF_FAILED(_env, status, 0);
return result;
}
inline int64_t Number::Int64Value() const {
int64_t result;
napi_status status = napi_get_value_int64(_env, _value, &result);
NAPI_THROW_IF_FAILED(_env, status, 0);
return result;
}
inline float Number::FloatValue() const {
return static_cast<float>(DoubleValue());
}
inline double Number::DoubleValue() const {
double result;
napi_status status = napi_get_value_double(_env, _value, &result);
NAPI_THROW_IF_FAILED(_env, status, 0);
return result;
}
#if NAPI_VERSION > 5
////////////////////////////////////////////////////////////////////////////////
// BigInt Class
////////////////////////////////////////////////////////////////////////////////
inline BigInt BigInt::New(napi_env env, int64_t val) {
napi_value value;
napi_status status = napi_create_bigint_int64(env, val, &value);
NAPI_THROW_IF_FAILED(env, status, BigInt());
return BigInt(env, value);
}
inline BigInt BigInt::New(napi_env env, uint64_t val) {
napi_value value;
napi_status status = napi_create_bigint_uint64(env, val, &value);
NAPI_THROW_IF_FAILED(env, status, BigInt());
return BigInt(env, value);
}
inline BigInt BigInt::New(napi_env env, int sign_bit, size_t word_count, const uint64_t* words) {
napi_value value;
napi_status status = napi_create_bigint_words(env, sign_bit, word_count, words, &value);
NAPI_THROW_IF_FAILED(env, status, BigInt());
return BigInt(env, value);
}
inline BigInt::BigInt() : Value() {
}
inline BigInt::BigInt(napi_env env, napi_value value) : Value(env, value) {
}
inline int64_t BigInt::Int64Value(bool* lossless) const {
int64_t result;
napi_status status = napi_get_value_bigint_int64(
_env, _value, &result, lossless);
NAPI_THROW_IF_FAILED(_env, status, 0);
return result;
}
inline uint64_t BigInt::Uint64Value(bool* lossless) const {
uint64_t result;
napi_status status = napi_get_value_bigint_uint64(
_env, _value, &result, lossless);
NAPI_THROW_IF_FAILED(_env, status, 0);
return result;
}
inline size_t BigInt::WordCount() const {
size_t word_count;
napi_status status = napi_get_value_bigint_words(
_env, _value, nullptr, &word_count, nullptr);
NAPI_THROW_IF_FAILED(_env, status, 0);
return word_count;
}
inline void BigInt::ToWords(int* sign_bit, size_t* word_count, uint64_t* words) {
napi_status status = napi_get_value_bigint_words(
_env, _value, sign_bit, word_count, words);
NAPI_THROW_IF_FAILED_VOID(_env, status);
}
#endif // NAPI_VERSION > 5
#if (NAPI_VERSION > 4)
////////////////////////////////////////////////////////////////////////////////
// Date Class
////////////////////////////////////////////////////////////////////////////////
inline Date Date::New(napi_env env, double val) {
napi_value value;
napi_status status = napi_create_date(env, val, &value);
NAPI_THROW_IF_FAILED(env, status, Date());
return Date(env, value);
}
inline Date::Date() : Value() {
}
inline Date::Date(napi_env env, napi_value value) : Value(env, value) {
}
inline Date::operator double() const {
return ValueOf();
}
inline double Date::ValueOf() const {
double result;
napi_status status = napi_get_date_value(
_env, _value, &result);
NAPI_THROW_IF_FAILED(_env, status, 0);
return result;
}
#endif
////////////////////////////////////////////////////////////////////////////////
// Name class
////////////////////////////////////////////////////////////////////////////////
inline Name::Name() : Value() {
}
inline Name::Name(napi_env env, napi_value value) : Value(env, value) {
}
////////////////////////////////////////////////////////////////////////////////
// String class
////////////////////////////////////////////////////////////////////////////////
inline String String::New(napi_env env, const std::string& val) {
return String::New(env, val.c_str(), val.size());
}
inline String String::New(napi_env env, const std::u16string& val) {
return String::New(env, val.c_str(), val.size());
}
inline String String::New(napi_env env, const char* val) {
napi_value value;
napi_status status = napi_create_string_utf8(env, val, std::strlen(val), &value);
NAPI_THROW_IF_FAILED(env, status, String());
return String(env, value);
}
inline String String::New(napi_env env, const char16_t* val) {
napi_value value;
napi_status status = napi_create_string_utf16(env, val, std::u16string(val).size(), &value);
NAPI_THROW_IF_FAILED(env, status, String());
return String(env, value);
}
inline String String::New(napi_env env, const char* val, size_t length) {
napi_value value;
napi_status status = napi_create_string_utf8(env, val, length, &value);
NAPI_THROW_IF_FAILED(env, status, String());
return String(env, value);
}
inline String String::New(napi_env env, const char16_t* val, size_t length) {
napi_value value;
napi_status status = napi_create_string_utf16(env, val, length, &value);
NAPI_THROW_IF_FAILED(env, status, String());
return String(env, value);
}
inline String::String() : Name() {
}
inline String::String(napi_env env, napi_value value) : Name(env, value) {
}
inline String::operator std::string() const {
return Utf8Value();
}
inline String::operator std::u16string() const {
return Utf16Value();
}
inline std::string String::Utf8Value() const {
size_t length;
napi_status status = napi_get_value_string_utf8(_env, _value, nullptr, 0, &length);
NAPI_THROW_IF_FAILED(_env, status, "");
std::string value;
value.reserve(length + 1);
value.resize(length);
status = napi_get_value_string_utf8(_env, _value, &value[0], value.capacity(), nullptr);
NAPI_THROW_IF_FAILED(_env, status, "");
return value;
}
inline std::u16string String::Utf16Value() const {
size_t length;
napi_status status = napi_get_value_string_utf16(_env, _value, nullptr, 0, &length);
NAPI_THROW_IF_FAILED(_env, status, NAPI_WIDE_TEXT(""));
std::u16string value;
value.reserve(length + 1);
value.resize(length);
status = napi_get_value_string_utf16(_env, _value, &value[0], value.capacity(), nullptr);
NAPI_THROW_IF_FAILED(_env, status, NAPI_WIDE_TEXT(""));
return value;
}
////////////////////////////////////////////////////////////////////////////////
// Symbol class
////////////////////////////////////////////////////////////////////////////////
inline Symbol Symbol::New(napi_env env, const char* description) {
napi_value descriptionValue = description != nullptr ?
String::New(env, description) : static_cast<napi_value>(nullptr);
return Symbol::New(env, descriptionValue);
}
inline Symbol Symbol::New(napi_env env, const std::string& description) {
napi_value descriptionValue = String::New(env, description);
return Symbol::New(env, descriptionValue);
}
inline Symbol Symbol::New(napi_env env, String description) {
napi_value descriptionValue = description;
return Symbol::New(env, descriptionValue);
}
inline Symbol Symbol::New(napi_env env, napi_value description) {
napi_value value;
napi_status status = napi_create_symbol(env, description, &value);
NAPI_THROW_IF_FAILED(env, status, Symbol());
return Symbol(env, value);
}
inline Symbol Symbol::WellKnown(napi_env env, const std::string& name) {
return Napi::Env(env).Global().Get("Symbol").As<Object>().Get(name).As<Symbol>();
}
inline Symbol::Symbol() : Name() {
}
inline Symbol::Symbol(napi_env env, napi_value value) : Name(env, value) {
}
////////////////////////////////////////////////////////////////////////////////
// Automagic value creation
////////////////////////////////////////////////////////////////////////////////
namespace details {
template <typename T>
struct vf_number {
static Number From(napi_env env, T value) {
return Number::New(env, static_cast<double>(value));
}
};
template<>
struct vf_number<bool> {
static Boolean From(napi_env env, bool value) {
return Boolean::New(env, value);
}
};
struct vf_utf8_charp {
static String From(napi_env env, const char* value) {
return String::New(env, value);
}
};
struct vf_utf16_charp {
static String From(napi_env env, const char16_t* value) {
return String::New(env, value);
}
};
struct vf_utf8_string {
static String From(napi_env env, const std::string& value) {
return String::New(env, value);
}
};