-
Notifications
You must be signed in to change notification settings - Fork 465
/
napi-inl.h
6613 lines (5723 loc) · 214 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_
////////////////////////////////////////////////////////////////////////////////
// Node-API C++ Wrapper Classes
//
// Inline header-only implementations for "Node-API" ABI-stable C APIs for
// Node.js.
////////////////////////////////////////////////////////////////////////////////
// Note: Do not include this file directly! Include "napi.h" instead.
#include <algorithm>
#include <cstring>
#if NAPI_HAS_THREADS
#include <mutex>
#endif // NAPI_HAS_THREADS
#include <type_traits>
#include <utility>
namespace Napi {
#ifdef NAPI_CPP_CUSTOM_NAMESPACE
namespace NAPI_CPP_CUSTOM_NAMESPACE {
#endif
// Helpers to handle functions exposed from C++ and internal constants.
namespace details {
// New napi_status constants not yet available in all supported versions of
// Node.js releases. Only necessary when they are used in napi.h and napi-inl.h.
constexpr int napi_no_external_buffers_allowed = 22;
// 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>
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)>
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)>
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)>
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 ? (instance->*UnwrapCallback)(cbInfo) : Napi::Value();
});
}
template <typename T, void (T::*UnwrapCallback)(const CallbackInfo& info)>
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>());
if (instance) (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) NAPI_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) NAPI_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 && NAPI_HAS_THREADS)
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;
};
template <typename ContextType, typename DataType, typename CallJs, CallJs call>
inline typename std::enable_if<call != static_cast<CallJs>(nullptr)>::type
CallJsWrapper(napi_env env, napi_value jsCallback, void* context, void* data) {
details::WrapVoidCallback([&]() {
call(env,
Function(env, jsCallback),
static_cast<ContextType*>(context),
static_cast<DataType*>(data));
});
}
template <typename ContextType, typename DataType, typename CallJs, CallJs call>
inline typename std::enable_if<call == static_cast<CallJs>(nullptr)>::type
CallJsWrapper(napi_env env,
napi_value jsCallback,
void* /*context*/,
void* /*data*/) {
details::WrapVoidCallback([&]() {
if (jsCallback != nullptr) {
Function(env, jsCallback).Call(0, nullptr);
}
});
}
#if NAPI_VERSION > 4
template <typename CallbackType, typename TSFN>
napi_value DefaultCallbackWrapper(napi_env /*env*/, std::nullptr_t /*cb*/) {
return nullptr;
}
template <typename CallbackType, typename TSFN>
napi_value DefaultCallbackWrapper(napi_env /*env*/, Napi::Function cb) {
return cb;
}
#else
template <typename CallbackType, typename TSFN>
napi_value DefaultCallbackWrapper(napi_env env, Napi::Function cb) {
if (cb.IsEmpty()) {
return TSFN::EmptyFunctionFactory(env);
}
return cb;
}
#endif // NAPI_VERSION > 4
#endif // NAPI_VERSION > 3 && NAPI_HAS_THREADS
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) \
static 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)));
});
}
////////////////////////////////////////////////////////////////////////////////
// Maybe class
////////////////////////////////////////////////////////////////////////////////
template <class T>
bool Maybe<T>::IsNothing() const {
return !_has_value;
}
template <class T>
bool Maybe<T>::IsJust() const {
return _has_value;
}
template <class T>
void Maybe<T>::Check() const {
NAPI_CHECK(IsJust(), "Napi::Maybe::Check", "Maybe value is Nothing.");
}
template <class T>
T Maybe<T>::Unwrap() const {
NAPI_CHECK(IsJust(), "Napi::Maybe::Unwrap", "Maybe value is Nothing.");
return _value;
}
template <class T>
T Maybe<T>::UnwrapOr(const T& default_value) const {
return _has_value ? _value : default_value;
}
template <class T>
bool Maybe<T>::UnwrapTo(T* out) const {
if (IsJust()) {
*out = _value;
return true;
};
return false;
}
template <class T>
bool Maybe<T>::operator==(const Maybe& other) const {
return (IsJust() == other.IsJust()) &&
(!IsJust() || Unwrap() == other.Unwrap());
}
template <class T>
bool Maybe<T>::operator!=(const Maybe& other) const {
return !operator==(other);
}
template <class T>
Maybe<T>::Maybe() : _has_value(false) {}
template <class T>
Maybe<T>::Maybe(const T& t) : _has_value(true), _value(t) {}
template <class T>
inline Maybe<T> Nothing() {
return Maybe<T>();
}
template <class T>
inline Maybe<T> Just(const T& t) {
return Maybe<T>(t);
}
////////////////////////////////////////////////////////////////////////////////
// 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() const {
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 MaybeOrValue<Value> Env::RunScript(const char* utf8script) const {
String script = String::New(_env, utf8script);
return RunScript(script);
}
inline MaybeOrValue<Value> Env::RunScript(const std::string& utf8script) const {
return RunScript(utf8script.c_str());
}
inline MaybeOrValue<Value> Env::RunScript(String script) const {
napi_value result;
napi_status status = napi_run_script(_env, script, &result);
NAPI_RETURN_OR_THROW_IF_FAILED(
_env, status, Napi::Value(_env, result), Napi::Value);
}
#if NAPI_VERSION > 2
template <typename Hook, typename Arg>
void Env::CleanupHook<Hook, Arg>::Wrapper(void* data) NAPI_NOEXCEPT {
auto* cleanupData =
static_cast<typename Napi::Env::CleanupHook<Hook, Arg>::CleanupData*>(
data);
cleanupData->hook();
delete cleanupData;
}
template <typename Hook, typename Arg>
void Env::CleanupHook<Hook, Arg>::WrapperWithArg(void* data) NAPI_NOEXCEPT {
auto* cleanupData =
static_cast<typename Napi::Env::CleanupHook<Hook, Arg>::CleanupData*>(
data);
cleanupData->hook(static_cast<Arg*>(cleanupData->arg));
delete cleanupData;
}
#endif // NAPI_VERSION > 2
#if NAPI_VERSION > 5
template <typename T, Env::Finalizer<T> fini>
inline void Env::SetInstanceData(T* data) const {
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) const {
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() const {
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
#if NAPI_VERSION > 8
inline const char* Env::GetModuleFileName() const {
const char* result;
napi_status status = node_api_get_module_file_name(_env, &result);
NAPI_THROW_IF_FAILED(*this, status, nullptr);
return result;
}
#endif // NAPI_VERSION > 8
////////////////////////////////////////////////////////////////////////////////
// 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 {
#ifdef NODE_ADDON_API_ENABLE_TYPE_CHECK_ON_AS
T::CheckCast(_env, _value);
#endif
return T(_env, _value);
}
inline MaybeOrValue<Boolean> Value::ToBoolean() const {
napi_value result;
napi_status status = napi_coerce_to_bool(_env, _value, &result);
NAPI_RETURN_OR_THROW_IF_FAILED(
_env, status, Napi::Boolean(_env, result), Napi::Boolean);
}
inline MaybeOrValue<Number> Value::ToNumber() const {
napi_value result;
napi_status status = napi_coerce_to_number(_env, _value, &result);
NAPI_RETURN_OR_THROW_IF_FAILED(
_env, status, Napi::Number(_env, result), Napi::Number);
}
inline MaybeOrValue<String> Value::ToString() const {
napi_value result;
napi_status status = napi_coerce_to_string(_env, _value, &result);
NAPI_RETURN_OR_THROW_IF_FAILED(
_env, status, Napi::String(_env, result), Napi::String);
}
inline MaybeOrValue<Object> Value::ToObject() const {
napi_value result;
napi_status status = napi_coerce_to_object(_env, _value, &result);
NAPI_RETURN_OR_THROW_IF_FAILED(
_env, status, Napi::Object(_env, result), Napi::Object);
}
////////////////////////////////////////////////////////////////////////////////
// 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 void Boolean::CheckCast(napi_env env, napi_value value) {
NAPI_CHECK(value != nullptr, "Boolean::CheckCast", "empty value");
napi_valuetype type;
napi_status status = napi_typeof(env, value, &type);
NAPI_CHECK(status == napi_ok, "Boolean::CheckCast", "napi_typeof failed");
NAPI_CHECK(
type == napi_boolean, "Boolean::CheckCast", "value is not napi_boolean");
}
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 void Number::CheckCast(napi_env env, napi_value value) {
NAPI_CHECK(value != nullptr, "Number::CheckCast", "empty value");
napi_valuetype type;
napi_status status = napi_typeof(env, value, &type);
NAPI_CHECK(status == napi_ok, "Number::CheckCast", "napi_typeof failed");
NAPI_CHECK(
type == napi_number, "Number::CheckCast", "value is not napi_number");
}
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 void BigInt::CheckCast(napi_env env, napi_value value) {
NAPI_CHECK(value != nullptr, "BigInt::CheckCast", "empty value");
napi_valuetype type;
napi_status status = napi_typeof(env, value, &type);
NAPI_CHECK(status == napi_ok, "BigInt::CheckCast", "napi_typeof failed");
NAPI_CHECK(
type == napi_bigint, "BigInt::CheckCast", "value is not napi_bigint");
}
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 void Date::CheckCast(napi_env env, napi_value value) {
NAPI_CHECK(value != nullptr, "Date::CheckCast", "empty value");
bool result;
napi_status status = napi_is_date(env, value, &result);
NAPI_CHECK(status == napi_ok, "Date::CheckCast", "napi_is_date failed");
NAPI_CHECK(result, "Date::CheckCast", "value is not date");