forked from nodejs/nan
-
Notifications
You must be signed in to change notification settings - Fork 0
/
nan.h
2899 lines (2466 loc) · 86 KB
/
nan.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
/*********************************************************************
* NAN - Native Abstractions for Node.js
*
* Copyright (c) 2018 NAN contributors:
* - Rod Vagg <https://github.com/rvagg>
* - Benjamin Byholm <https://github.com/kkoopa>
* - Trevor Norris <https://github.com/trevnorris>
* - Nathan Rajlich <https://github.com/TooTallNate>
* - Brett Lawson <https://github.com/brett19>
* - Ben Noordhuis <https://github.com/bnoordhuis>
* - David Siegel <https://github.com/agnat>
* - Michael Ira Krufky <https://github.com/mkrufky>
*
* MIT License <https://github.com/nodejs/nan/blob/master/LICENSE.md>
*
* Version 2.14.0: current Node 12.2.0, Node 12: 0.12.18, Node 10: 0.10.48, iojs: 3.3.1
*
* See https://github.com/nodejs/nan for the latest update to this file
**********************************************************************************/
#ifndef NAN_H_
#define NAN_H_
#include <node_version.h>
#define NODE_0_10_MODULE_VERSION 11
#define NODE_0_12_MODULE_VERSION 14
#define ATOM_0_21_MODULE_VERSION 41
#define IOJS_1_0_MODULE_VERSION 42
#define IOJS_1_1_MODULE_VERSION 43
#define IOJS_2_0_MODULE_VERSION 44
#define IOJS_3_0_MODULE_VERSION 45
#define NODE_4_0_MODULE_VERSION 46
#define NODE_5_0_MODULE_VERSION 47
#define NODE_6_0_MODULE_VERSION 48
#define NODE_7_0_MODULE_VERSION 51
#define NODE_8_0_MODULE_VERSION 57
#define NODE_9_0_MODULE_VERSION 59
#define NODE_10_0_MODULE_VERSION 64
#define NODE_11_0_MODULE_VERSION 67
#define NODE_12_0_MODULE_VERSION 72
#define NODE_13_0_MODULE_VERSION 79
#ifdef _MSC_VER
# define NAN_HAS_CPLUSPLUS_11 (_MSC_VER >= 1800)
#else
# define NAN_HAS_CPLUSPLUS_11 (__cplusplus >= 201103L)
#endif
#if NODE_MODULE_VERSION >= IOJS_3_0_MODULE_VERSION && !NAN_HAS_CPLUSPLUS_11
# error This version of node/NAN/v8 requires a C++11 compiler
#endif
#define NAN_HAS_CPLUSPLUS_17 (__cplusplus >= 201703L)
#ifndef NAN_ENABLE_STRING_VIEW
# define NAN_ENABLE_STRING_VIEW NAN_HAS_CPLUSPLUS_17
#endif
#include <uv.h>
#include <node.h>
#include <node_buffer.h>
#include <node_object_wrap.h>
#include <algorithm>
#include <cstring>
#include <climits>
#include <cstdlib>
#include <utility>
#if defined(_MSC_VER)
# pragma warning( push )
# pragma warning( disable : 4530 )
# include <queue>
# include <string>
# include <vector>
# pragma warning( pop )
#else
# include <queue>
# include <string>
# include <vector>
#endif
#if NAN_ENABLE_STRING_VIEW
# include <string_view>
#endif
// uv helpers
#ifdef UV_VERSION_MAJOR
# ifndef UV_VERSION_PATCH
# define UV_VERSION_PATCH 0
# endif
# define NAUV_UVVERSION ((UV_VERSION_MAJOR << 16) | \
(UV_VERSION_MINOR << 8) | \
(UV_VERSION_PATCH))
#else
# define NAUV_UVVERSION 0x000b00
#endif
#if NAUV_UVVERSION < 0x000b0b
# ifdef WIN32
# include <windows.h>
# else
# include <pthread.h>
# endif
#endif
namespace Nan {
#define NAN_CONCAT(a, b) NAN_CONCAT_HELPER(a, b)
#define NAN_CONCAT_HELPER(a, b) a##b
#define NAN_INLINE inline // TODO(bnoordhuis) Remove in v3.0.0.
#if defined(__GNUC__) && \
!(defined(V8_DISABLE_DEPRECATIONS) && V8_DISABLE_DEPRECATIONS)
# define NAN_DEPRECATED __attribute__((deprecated))
#elif defined(_MSC_VER) && \
!(defined(V8_DISABLE_DEPRECATIONS) && V8_DISABLE_DEPRECATIONS)
# define NAN_DEPRECATED __declspec(deprecated)
#else
# define NAN_DEPRECATED
#endif
#if NAN_HAS_CPLUSPLUS_11
# define NAN_DISALLOW_ASSIGN(CLASS) void operator=(const CLASS&) = delete;
# define NAN_DISALLOW_COPY(CLASS) CLASS(const CLASS&) = delete;
# define NAN_DISALLOW_MOVE(CLASS) \
CLASS(CLASS&&) = delete; /* NOLINT(build/c++11) */ \
void operator=(CLASS&&) = delete;
#else
# define NAN_DISALLOW_ASSIGN(CLASS) void operator=(const CLASS&);
# define NAN_DISALLOW_COPY(CLASS) CLASS(const CLASS&);
# define NAN_DISALLOW_MOVE(CLASS)
#endif
#define NAN_DISALLOW_ASSIGN_COPY(CLASS) \
NAN_DISALLOW_ASSIGN(CLASS) \
NAN_DISALLOW_COPY(CLASS)
#define NAN_DISALLOW_ASSIGN_MOVE(CLASS) \
NAN_DISALLOW_ASSIGN(CLASS) \
NAN_DISALLOW_MOVE(CLASS)
#define NAN_DISALLOW_COPY_MOVE(CLASS) \
NAN_DISALLOW_COPY(CLASS) \
NAN_DISALLOW_MOVE(CLASS)
#define NAN_DISALLOW_ASSIGN_COPY_MOVE(CLASS) \
NAN_DISALLOW_ASSIGN(CLASS) \
NAN_DISALLOW_COPY(CLASS) \
NAN_DISALLOW_MOVE(CLASS)
#define TYPE_CHECK(T, S) \
while (false) { \
*(static_cast<T *volatile *>(0)) = static_cast<S*>(0); \
}
//=== RegistrationFunction =====================================================
#if NODE_MODULE_VERSION < IOJS_3_0_MODULE_VERSION
typedef v8::Handle<v8::Object> ADDON_REGISTER_FUNCTION_ARGS_TYPE;
#else
typedef v8::Local<v8::Object> ADDON_REGISTER_FUNCTION_ARGS_TYPE;
#endif
#define NAN_MODULE_INIT(name) \
void name(Nan::ADDON_REGISTER_FUNCTION_ARGS_TYPE target)
#if NODE_MAJOR_VERSION >= 10 || \
NODE_MAJOR_VERSION == 9 && NODE_MINOR_VERSION >= 3
#define NAN_MODULE_WORKER_ENABLED(module_name, registration) \
extern "C" NODE_MODULE_EXPORT void \
NAN_CONCAT(node_register_module_v, NODE_MODULE_VERSION)( \
v8::Local<v8::Object> exports, v8::Local<v8::Value> module, \
v8::Local<v8::Context> context) \
{ \
registration(exports); \
}
#else
#define NAN_MODULE_WORKER_ENABLED(module_name, registration) \
NODE_MODULE(module_name, registration)
#endif
//=== CallbackInfo =============================================================
#include "nan_callbacks.h" // NOLINT(build/include)
//==============================================================================
#if (NODE_MODULE_VERSION < NODE_0_12_MODULE_VERSION)
typedef v8::Script UnboundScript;
typedef v8::Script BoundScript;
#else
typedef v8::UnboundScript UnboundScript;
typedef v8::Script BoundScript;
#endif
#if (NODE_MODULE_VERSION < ATOM_0_21_MODULE_VERSION)
typedef v8::String::ExternalAsciiStringResource
ExternalOneByteStringResource;
#else
typedef v8::String::ExternalOneByteStringResource
ExternalOneByteStringResource;
#endif
#if (NODE_MODULE_VERSION > NODE_0_10_MODULE_VERSION)
template<typename T>
class NonCopyablePersistentTraits :
public v8::NonCopyablePersistentTraits<T> {};
template<typename T>
class CopyablePersistentTraits :
public v8::CopyablePersistentTraits<T> {};
template<typename T>
class PersistentBase :
public v8::PersistentBase<T> {};
template<typename T, typename M = v8::NonCopyablePersistentTraits<T> >
class Persistent;
#else
template<typename T> class NonCopyablePersistentTraits;
template<typename T> class PersistentBase;
template<typename T, typename P> class WeakCallbackData;
template<typename T, typename M = NonCopyablePersistentTraits<T> >
class Persistent;
#endif // NODE_MODULE_VERSION
template<typename T>
class Maybe {
public:
inline bool IsNothing() const { return !has_value_; }
inline bool IsJust() const { return has_value_; }
inline T ToChecked() const { return FromJust(); }
inline void Check() const { FromJust(); }
inline bool To(T* out) const {
if (IsJust()) *out = value_;
return IsJust();
}
inline T FromJust() const {
#if defined(V8_ENABLE_CHECKS)
assert(IsJust() && "FromJust is Nothing");
#endif // V8_ENABLE_CHECKS
return value_;
}
inline T FromMaybe(const T& default_value) const {
return has_value_ ? value_ : default_value;
}
inline bool operator==(const Maybe &other) const {
return (IsJust() == other.IsJust()) &&
(!IsJust() || FromJust() == other.FromJust());
}
inline bool operator!=(const Maybe &other) const {
return !operator==(other);
}
#if defined(V8_MAJOR_VERSION) && (V8_MAJOR_VERSION > 4 || \
(V8_MAJOR_VERSION == 4 && defined(V8_MINOR_VERSION) && V8_MINOR_VERSION >= 3))
// Allow implicit conversions from v8::Maybe<T> to Nan::Maybe<T>.
Maybe(const v8::Maybe<T>& that) // NOLINT(runtime/explicit)
: has_value_(that.IsJust())
, value_(that.FromMaybe(T())) {}
#endif
private:
Maybe() : has_value_(false) {}
explicit Maybe(const T& t) : has_value_(true), value_(t) {}
bool has_value_;
T value_;
template<typename U>
friend Maybe<U> Nothing();
template<typename U>
friend Maybe<U> Just(const U& u);
};
template<typename T>
inline Maybe<T> Nothing() {
return Maybe<T>();
}
template<typename T>
inline Maybe<T> Just(const T& t) {
return Maybe<T>(t);
}
#if defined(V8_MAJOR_VERSION) && (V8_MAJOR_VERSION > 4 || \
(V8_MAJOR_VERSION == 4 && defined(V8_MINOR_VERSION) && V8_MINOR_VERSION >= 3))
# include "nan_maybe_43_inl.h" // NOLINT(build/include)
#else
# include "nan_maybe_pre_43_inl.h" // NOLINT(build/include)
#endif
#include "nan_converters.h" // NOLINT(build/include)
#include "nan_new.h" // NOLINT(build/include)
#if NAUV_UVVERSION < 0x000b17
#define NAUV_WORK_CB(func) \
void func(uv_async_t *async, int)
#else
#define NAUV_WORK_CB(func) \
void func(uv_async_t *async)
#endif
#if NAUV_UVVERSION >= 0x000b0b
typedef uv_key_t nauv_key_t;
inline int nauv_key_create(nauv_key_t *key) {
return uv_key_create(key);
}
inline void nauv_key_delete(nauv_key_t *key) {
uv_key_delete(key);
}
inline void* nauv_key_get(nauv_key_t *key) {
return uv_key_get(key);
}
inline void nauv_key_set(nauv_key_t *key, void *value) {
uv_key_set(key, value);
}
#else
/* Implement thread local storage for older versions of libuv.
* This is essentially a backport of libuv commit 5d2434bf
* written by Ben Noordhuis, adjusted for names and inline.
*/
#ifndef WIN32
typedef pthread_key_t nauv_key_t;
inline int nauv_key_create(nauv_key_t* key) {
return -pthread_key_create(key, NULL);
}
inline void nauv_key_delete(nauv_key_t* key) {
if (pthread_key_delete(*key))
abort();
}
inline void* nauv_key_get(nauv_key_t* key) {
return pthread_getspecific(*key);
}
inline void nauv_key_set(nauv_key_t* key, void* value) {
if (pthread_setspecific(*key, value))
abort();
}
#else
typedef struct {
DWORD tls_index;
} nauv_key_t;
inline int nauv_key_create(nauv_key_t* key) {
key->tls_index = TlsAlloc();
if (key->tls_index == TLS_OUT_OF_INDEXES)
return UV_ENOMEM;
return 0;
}
inline void nauv_key_delete(nauv_key_t* key) {
if (TlsFree(key->tls_index) == FALSE)
abort();
key->tls_index = TLS_OUT_OF_INDEXES;
}
inline void* nauv_key_get(nauv_key_t* key) {
void* value = TlsGetValue(key->tls_index);
if (value == NULL)
if (GetLastError() != ERROR_SUCCESS)
abort();
return value;
}
inline void nauv_key_set(nauv_key_t* key, void* value) {
if (TlsSetValue(key->tls_index, value) == FALSE)
abort();
}
#endif
#endif
#if NODE_MODULE_VERSION < IOJS_3_0_MODULE_VERSION
template<typename T>
v8::Local<T> New(v8::Handle<T>);
#endif
#if defined(V8_MAJOR_VERSION) && (V8_MAJOR_VERSION > 4 || \
(V8_MAJOR_VERSION == 4 && defined(V8_MINOR_VERSION) && V8_MINOR_VERSION >= 3))
typedef v8::WeakCallbackType WeakCallbackType;
#else
struct WeakCallbackType {
enum E {kParameter, kInternalFields};
E type;
WeakCallbackType(E other) : type(other) {} // NOLINT(runtime/explicit)
inline bool operator==(E other) { return other == this->type; }
inline bool operator!=(E other) { return !operator==(other); }
};
#endif
template<typename P> class WeakCallbackInfo;
#if NODE_MODULE_VERSION > NODE_0_10_MODULE_VERSION
# include "nan_persistent_12_inl.h" // NOLINT(build/include)
#else
# include "nan_persistent_pre_12_inl.h" // NOLINT(build/include)
#endif
namespace imp {
static const size_t kMaxLength = 0x3fffffff;
// v8::String::REPLACE_INVALID_UTF8 was introduced
// in node.js v0.10.29 and v0.8.27.
#if NODE_MAJOR_VERSION > 0 || \
NODE_MINOR_VERSION > 10 || \
NODE_MINOR_VERSION == 10 && NODE_PATCH_VERSION >= 29 || \
NODE_MINOR_VERSION == 8 && NODE_PATCH_VERSION >= 27
static const unsigned kReplaceInvalidUtf8 = v8::String::REPLACE_INVALID_UTF8;
#else
static const unsigned kReplaceInvalidUtf8 = 0;
#endif
} // end of namespace imp
//=== HandleScope ==============================================================
class HandleScope {
v8::HandleScope scope;
public:
#if NODE_MODULE_VERSION > NODE_0_10_MODULE_VERSION
inline HandleScope() : scope(v8::Isolate::GetCurrent()) {}
inline static int NumberOfHandles() {
return v8::HandleScope::NumberOfHandles(v8::Isolate::GetCurrent());
}
#else
inline HandleScope() : scope() {}
inline static int NumberOfHandles() {
return v8::HandleScope::NumberOfHandles();
}
#endif
private:
// Make it hard to create heap-allocated or illegal handle scopes by
// disallowing certain operations.
HandleScope(const HandleScope &);
void operator=(const HandleScope &);
void *operator new(size_t size);
void operator delete(void *, size_t) {
abort();
}
};
class EscapableHandleScope {
public:
#if NODE_MODULE_VERSION > NODE_0_10_MODULE_VERSION
inline EscapableHandleScope() : scope(v8::Isolate::GetCurrent()) {}
inline static int NumberOfHandles() {
return v8::EscapableHandleScope::NumberOfHandles(v8::Isolate::GetCurrent());
}
template<typename T>
inline v8::Local<T> Escape(v8::Local<T> value) {
return scope.Escape(value);
}
private:
v8::EscapableHandleScope scope;
#else
inline EscapableHandleScope() : scope() {}
inline static int NumberOfHandles() {
return v8::HandleScope::NumberOfHandles();
}
template<typename T>
inline v8::Local<T> Escape(v8::Local<T> value) {
return scope.Close(value);
}
private:
v8::HandleScope scope;
#endif
private:
// Make it hard to create heap-allocated or illegal handle scopes by
// disallowing certain operations.
EscapableHandleScope(const EscapableHandleScope &);
void operator=(const EscapableHandleScope &);
void *operator new(size_t size);
void operator delete(void *, size_t) {
abort();
}
};
//=== TryCatch =================================================================
class TryCatch {
v8::TryCatch try_catch_;
friend void FatalException(const TryCatch&);
public:
#if NODE_MODULE_VERSION > NODE_0_12_MODULE_VERSION
TryCatch() : try_catch_(v8::Isolate::GetCurrent()) {}
#endif
inline bool HasCaught() const { return try_catch_.HasCaught(); }
inline bool CanContinue() const { return try_catch_.CanContinue(); }
inline v8::Local<v8::Value> ReThrow() {
#if NODE_MODULE_VERSION < IOJS_3_0_MODULE_VERSION
return New(try_catch_.ReThrow());
#else
return try_catch_.ReThrow();
#endif
}
inline v8::Local<v8::Value> Exception() const {
return try_catch_.Exception();
}
#if defined(V8_MAJOR_VERSION) && (V8_MAJOR_VERSION > 4 || \
(V8_MAJOR_VERSION == 4 && defined(V8_MINOR_VERSION) && V8_MINOR_VERSION >= 3))
inline v8::MaybeLocal<v8::Value> StackTrace() const {
v8::Isolate *isolate = v8::Isolate::GetCurrent();
v8::EscapableHandleScope scope(isolate);
return scope.Escape(try_catch_.StackTrace(isolate->GetCurrentContext())
.FromMaybe(v8::Local<v8::Value>()));
}
#else
inline MaybeLocal<v8::Value> StackTrace() const {
return try_catch_.StackTrace();
}
#endif
inline v8::Local<v8::Message> Message() const {
return try_catch_.Message();
}
inline void Reset() { try_catch_.Reset(); }
inline void SetVerbose(bool value) { try_catch_.SetVerbose(value); }
inline void SetCaptureMessage(bool value) {
try_catch_.SetCaptureMessage(value);
}
};
v8::Local<v8::Value> MakeCallback(v8::Local<v8::Object> target,
v8::Local<v8::Function> func,
int argc,
v8::Local<v8::Value>* argv);
v8::Local<v8::Value> MakeCallback(v8::Local<v8::Object> target,
v8::Local<v8::String> symbol,
int argc,
v8::Local<v8::Value>* argv);
v8::Local<v8::Value> MakeCallback(v8::Local<v8::Object> target,
const char* method,
int argc,
v8::Local<v8::Value>* argv);
// === AsyncResource ===========================================================
class AsyncResource {
public:
AsyncResource(
v8::Local<v8::String> name
, v8::Local<v8::Object> resource = New<v8::Object>()) {
#if NODE_MODULE_VERSION >= NODE_9_0_MODULE_VERSION
v8::Isolate* isolate = v8::Isolate::GetCurrent();
if (resource.IsEmpty()) {
resource = New<v8::Object>();
}
context = node::EmitAsyncInit(isolate, resource, name);
#endif
}
AsyncResource(
const char* name
, v8::Local<v8::Object> resource = New<v8::Object>()) {
#if NODE_MODULE_VERSION >= NODE_9_0_MODULE_VERSION
v8::Isolate* isolate = v8::Isolate::GetCurrent();
if (resource.IsEmpty()) {
resource = New<v8::Object>();
}
v8::Local<v8::String> name_string =
New<v8::String>(name).ToLocalChecked();
context = node::EmitAsyncInit(isolate, resource, name_string);
#endif
}
~AsyncResource() {
#if NODE_MODULE_VERSION >= NODE_9_0_MODULE_VERSION
v8::Isolate* isolate = v8::Isolate::GetCurrent();
node::EmitAsyncDestroy(isolate, context);
#endif
}
inline MaybeLocal<v8::Value> runInAsyncScope(
v8::Local<v8::Object> target
, v8::Local<v8::Function> func
, int argc
, v8::Local<v8::Value>* argv) {
#if NODE_MODULE_VERSION < NODE_9_0_MODULE_VERSION
return MakeCallback(target, func, argc, argv);
#else
return node::MakeCallback(
v8::Isolate::GetCurrent(), target, func, argc, argv, context);
#endif
}
inline MaybeLocal<v8::Value> runInAsyncScope(
v8::Local<v8::Object> target
, v8::Local<v8::String> symbol
, int argc
, v8::Local<v8::Value>* argv) {
#if NODE_MODULE_VERSION < NODE_9_0_MODULE_VERSION
return MakeCallback(target, symbol, argc, argv);
#else
return node::MakeCallback(
v8::Isolate::GetCurrent(), target, symbol, argc, argv, context);
#endif
}
inline MaybeLocal<v8::Value> runInAsyncScope(
v8::Local<v8::Object> target
, const char* method
, int argc
, v8::Local<v8::Value>* argv) {
#if NODE_MODULE_VERSION < NODE_9_0_MODULE_VERSION
return MakeCallback(target, method, argc, argv);
#else
return node::MakeCallback(
v8::Isolate::GetCurrent(), target, method, argc, argv, context);
#endif
}
private:
NAN_DISALLOW_ASSIGN_COPY_MOVE(AsyncResource)
#if NODE_MODULE_VERSION >= NODE_9_0_MODULE_VERSION
node::async_context context;
#endif
};
inline uv_loop_t* GetCurrentEventLoop() {
#if NODE_MAJOR_VERSION >= 10 || \
NODE_MAJOR_VERSION == 9 && NODE_MINOR_VERSION >= 3 || \
NODE_MAJOR_VERSION == 8 && NODE_MINOR_VERSION >= 10
return node::GetCurrentEventLoop(v8::Isolate::GetCurrent());
#else
return uv_default_loop();
#endif
}
//============ =================================================================
/* node 0.12 */
#if NODE_MODULE_VERSION >= NODE_0_12_MODULE_VERSION
inline
void SetCounterFunction(v8::CounterLookupCallback cb) {
v8::Isolate::GetCurrent()->SetCounterFunction(cb);
}
inline
void SetCreateHistogramFunction(v8::CreateHistogramCallback cb) {
v8::Isolate::GetCurrent()->SetCreateHistogramFunction(cb);
}
inline
void SetAddHistogramSampleFunction(v8::AddHistogramSampleCallback cb) {
v8::Isolate::GetCurrent()->SetAddHistogramSampleFunction(cb);
}
#if defined(V8_MAJOR_VERSION) && (V8_MAJOR_VERSION > 4 || \
(V8_MAJOR_VERSION == 4 && defined(V8_MINOR_VERSION) && V8_MINOR_VERSION >= 3))
inline bool IdleNotification(int idle_time_in_ms) {
return v8::Isolate::GetCurrent()->IdleNotificationDeadline(
idle_time_in_ms * 0.001);
}
# else
inline bool IdleNotification(int idle_time_in_ms) {
return v8::Isolate::GetCurrent()->IdleNotification(idle_time_in_ms);
}
#endif
inline void LowMemoryNotification() {
v8::Isolate::GetCurrent()->LowMemoryNotification();
}
inline void ContextDisposedNotification() {
v8::Isolate::GetCurrent()->ContextDisposedNotification();
}
#else
inline
void SetCounterFunction(v8::CounterLookupCallback cb) {
v8::V8::SetCounterFunction(cb);
}
inline
void SetCreateHistogramFunction(v8::CreateHistogramCallback cb) {
v8::V8::SetCreateHistogramFunction(cb);
}
inline
void SetAddHistogramSampleFunction(v8::AddHistogramSampleCallback cb) {
v8::V8::SetAddHistogramSampleFunction(cb);
}
inline bool IdleNotification(int idle_time_in_ms) {
return v8::V8::IdleNotification(idle_time_in_ms);
}
inline void LowMemoryNotification() {
v8::V8::LowMemoryNotification();
}
inline void ContextDisposedNotification() {
v8::V8::ContextDisposedNotification();
}
#endif
#if (NODE_MODULE_VERSION > NODE_0_10_MODULE_VERSION) // Node 0.12
inline v8::Local<v8::Primitive> Undefined() {
# if NODE_MODULE_VERSION < IOJS_3_0_MODULE_VERSION
EscapableHandleScope scope;
return scope.Escape(New(v8::Undefined(v8::Isolate::GetCurrent())));
# else
return v8::Undefined(v8::Isolate::GetCurrent());
# endif
}
inline v8::Local<v8::Primitive> Null() {
# if NODE_MODULE_VERSION < IOJS_3_0_MODULE_VERSION
EscapableHandleScope scope;
return scope.Escape(New(v8::Null(v8::Isolate::GetCurrent())));
# else
return v8::Null(v8::Isolate::GetCurrent());
# endif
}
inline v8::Local<v8::Boolean> True() {
# if NODE_MODULE_VERSION < IOJS_3_0_MODULE_VERSION
EscapableHandleScope scope;
return scope.Escape(New(v8::True(v8::Isolate::GetCurrent())));
# else
return v8::True(v8::Isolate::GetCurrent());
# endif
}
inline v8::Local<v8::Boolean> False() {
# if NODE_MODULE_VERSION < IOJS_3_0_MODULE_VERSION
EscapableHandleScope scope;
return scope.Escape(New(v8::False(v8::Isolate::GetCurrent())));
# else
return v8::False(v8::Isolate::GetCurrent());
# endif
}
inline v8::Local<v8::String> EmptyString() {
return v8::String::Empty(v8::Isolate::GetCurrent());
}
inline int AdjustExternalMemory(int bc) {
return static_cast<int>(
v8::Isolate::GetCurrent()->AdjustAmountOfExternalAllocatedMemory(bc));
}
inline void SetTemplate(
v8::Local<v8::Template> templ
, const char *name
, v8::Local<v8::Data> value) {
templ->Set(v8::Isolate::GetCurrent(), name, value);
}
inline void SetTemplate(
v8::Local<v8::Template> templ
, v8::Local<v8::String> name
, v8::Local<v8::Data> value
, v8::PropertyAttribute attributes) {
templ->Set(name, value, attributes);
}
inline v8::Local<v8::Context> GetCurrentContext() {
return v8::Isolate::GetCurrent()->GetCurrentContext();
}
inline void* GetInternalFieldPointer(
v8::Local<v8::Object> object
, int index) {
return object->GetAlignedPointerFromInternalField(index);
}
inline void SetInternalFieldPointer(
v8::Local<v8::Object> object
, int index
, void* value) {
object->SetAlignedPointerInInternalField(index, value);
}
# define NAN_GC_CALLBACK(name) \
void name(v8::Isolate *isolate, v8::GCType type, v8::GCCallbackFlags flags)
#if NODE_MODULE_VERSION <= NODE_4_0_MODULE_VERSION
typedef v8::Isolate::GCEpilogueCallback GCEpilogueCallback;
typedef v8::Isolate::GCPrologueCallback GCPrologueCallback;
#else
typedef v8::Isolate::GCCallback GCEpilogueCallback;
typedef v8::Isolate::GCCallback GCPrologueCallback;
#endif
inline void AddGCEpilogueCallback(
GCEpilogueCallback callback
, v8::GCType gc_type_filter = v8::kGCTypeAll) {
v8::Isolate::GetCurrent()->AddGCEpilogueCallback(callback, gc_type_filter);
}
inline void RemoveGCEpilogueCallback(
GCEpilogueCallback callback) {
v8::Isolate::GetCurrent()->RemoveGCEpilogueCallback(callback);
}
inline void AddGCPrologueCallback(
GCPrologueCallback callback
, v8::GCType gc_type_filter = v8::kGCTypeAll) {
v8::Isolate::GetCurrent()->AddGCPrologueCallback(callback, gc_type_filter);
}
inline void RemoveGCPrologueCallback(
GCPrologueCallback callback) {
v8::Isolate::GetCurrent()->RemoveGCPrologueCallback(callback);
}
inline void GetHeapStatistics(
v8::HeapStatistics *heap_statistics) {
v8::Isolate::GetCurrent()->GetHeapStatistics(heap_statistics);
}
# define X(NAME) \
inline v8::Local<v8::Value> NAME(const char *msg) { \
EscapableHandleScope scope; \
return scope.Escape(v8::Exception::NAME(New(msg).ToLocalChecked())); \
} \
\
inline \
v8::Local<v8::Value> NAME(v8::Local<v8::String> msg) { \
return v8::Exception::NAME(msg); \
} \
\
inline void Throw ## NAME(const char *msg) { \
HandleScope scope; \
v8::Isolate::GetCurrent()->ThrowException( \
v8::Exception::NAME(New(msg).ToLocalChecked())); \
} \
\
inline void Throw ## NAME(v8::Local<v8::String> msg) { \
HandleScope scope; \
v8::Isolate::GetCurrent()->ThrowException( \
v8::Exception::NAME(msg)); \
}
X(Error)
X(RangeError)
X(ReferenceError)
X(SyntaxError)
X(TypeError)
# undef X
inline void ThrowError(v8::Local<v8::Value> error) {
v8::Isolate::GetCurrent()->ThrowException(error);
}
inline MaybeLocal<v8::Object> NewBuffer(
char *data
, size_t length
#if NODE_MODULE_VERSION > IOJS_2_0_MODULE_VERSION
, node::Buffer::FreeCallback callback
#else
, node::smalloc::FreeCallback callback
#endif
, void *hint
) {
// arbitrary buffer lengths requires
// NODE_MODULE_VERSION >= IOJS_3_0_MODULE_VERSION
assert(length <= imp::kMaxLength && "too large buffer");
#if NODE_MODULE_VERSION > IOJS_2_0_MODULE_VERSION
return node::Buffer::New(
v8::Isolate::GetCurrent(), data, length, callback, hint);
#else
return node::Buffer::New(v8::Isolate::GetCurrent(), data, length, callback,
hint);
#endif
}
inline MaybeLocal<v8::Object> CopyBuffer(
const char *data
, uint32_t size
) {
// arbitrary buffer lengths requires
// NODE_MODULE_VERSION >= IOJS_3_0_MODULE_VERSION
assert(size <= imp::kMaxLength && "too large buffer");
#if NODE_MODULE_VERSION > IOJS_2_0_MODULE_VERSION
return node::Buffer::Copy(
v8::Isolate::GetCurrent(), data, size);
#else
return node::Buffer::New(v8::Isolate::GetCurrent(), data, size);
#endif
}
inline MaybeLocal<v8::Object> NewBuffer(uint32_t size) {
// arbitrary buffer lengths requires
// NODE_MODULE_VERSION >= IOJS_3_0_MODULE_VERSION
assert(size <= imp::kMaxLength && "too large buffer");
#if NODE_MODULE_VERSION > IOJS_2_0_MODULE_VERSION
return node::Buffer::New(
v8::Isolate::GetCurrent(), size);
#else
return node::Buffer::New(v8::Isolate::GetCurrent(), size);
#endif
}
inline MaybeLocal<v8::Object> NewBuffer(
char* data
, uint32_t size
) {
// arbitrary buffer lengths requires
// NODE_MODULE_VERSION >= IOJS_3_0_MODULE_VERSION
assert(size <= imp::kMaxLength && "too large buffer");
#if NODE_MODULE_VERSION > IOJS_2_0_MODULE_VERSION
return node::Buffer::New(v8::Isolate::GetCurrent(), data, size);
#else
return node::Buffer::Use(v8::Isolate::GetCurrent(), data, size);
#endif
}
#if defined(V8_MAJOR_VERSION) && (V8_MAJOR_VERSION > 4 || \
(V8_MAJOR_VERSION == 4 && defined(V8_MINOR_VERSION) && V8_MINOR_VERSION >= 3))
inline MaybeLocal<v8::String>
NewOneByteString(const uint8_t * value, int length = -1) {
return v8::String::NewFromOneByte(v8::Isolate::GetCurrent(), value,
v8::NewStringType::kNormal, length);
}
inline MaybeLocal<BoundScript> CompileScript(
v8::Local<v8::String> s
, const v8::ScriptOrigin& origin
) {
v8::Isolate *isolate = v8::Isolate::GetCurrent();
v8::EscapableHandleScope scope(isolate);
v8::ScriptCompiler::Source source(s, origin);
return scope.Escape(
v8::ScriptCompiler::Compile(isolate->GetCurrentContext(), &source)
.FromMaybe(v8::Local<BoundScript>()));
}
inline MaybeLocal<BoundScript> CompileScript(
v8::Local<v8::String> s
) {
v8::Isolate *isolate = v8::Isolate::GetCurrent();
v8::EscapableHandleScope scope(isolate);
v8::ScriptCompiler::Source source(s);
return scope.Escape(
v8::ScriptCompiler::Compile(isolate->GetCurrentContext(), &source)
.FromMaybe(v8::Local<BoundScript>()));
}
inline MaybeLocal<v8::Value> RunScript(
v8::Local<UnboundScript> script
) {
v8::Isolate *isolate = v8::Isolate::GetCurrent();
v8::EscapableHandleScope scope(isolate);
return scope.Escape(script->BindToCurrentContext()
->Run(isolate->GetCurrentContext())
.FromMaybe(v8::Local<v8::Value>()));
}
inline MaybeLocal<v8::Value> RunScript(
v8::Local<BoundScript> script
) {
v8::Isolate *isolate = v8::Isolate::GetCurrent();
v8::EscapableHandleScope scope(isolate);
return scope.Escape(script->Run(isolate->GetCurrentContext())
.FromMaybe(v8::Local<v8::Value>()));
}
#else
inline MaybeLocal<v8::String>
NewOneByteString(const uint8_t * value, int length = -1) {