forked from nodejs/nan
-
Notifications
You must be signed in to change notification settings - Fork 0
/
nan.h
2271 lines (1920 loc) · 66.9 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) 2016 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>
*
* MIT License <https://github.com/nodejs/nan/blob/master/LICENSE.md>
*
* Version 2.3.3: current Node 6.0.0, Node 12: 0.12.13, Node 10: 0.10.44, 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
#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
#include <uv.h>
#include <node.h>
#include <node_buffer.h>
#include <node_object_wrap.h>
#include <algorithm>
#include <cstring>
#include <climits>
#include <cstdlib>
#if defined(_MSC_VER)
# pragma warning( push )
# pragma warning( disable : 4530 )
# include <string>
# include <vector>
# pragma warning( pop )
#else
# include <string>
# include <vector>
#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 {
#if defined(__GNUC__) && !(defined(DEBUG) && DEBUG)
# define NAN_INLINE inline __attribute__((always_inline))
#elif defined(_MSC_VER) && !(defined(DEBUG) && DEBUG)
# define NAN_INLINE __forceinline
#else
# define NAN_INLINE inline
#endif
#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)
//=== 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
#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);
};
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);
};
//=== 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
NAN_INLINE bool HasCaught() const { return try_catch_.HasCaught(); }
NAN_INLINE bool CanContinue() const { return try_catch_.CanContinue(); }
NAN_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
}
NAN_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))
NAN_INLINE v8::MaybeLocal<v8::Value> StackTrace() const {
return try_catch_.StackTrace(GetCurrentContext());
}
#else
NAN_INLINE MaybeLocal<v8::Value> StackTrace() const {
return MaybeLocal<v8::Value>(try_catch_.StackTrace());
}
#endif
NAN_INLINE v8::Local<v8::Message> Message() const {
return try_catch_.Message();
}
NAN_INLINE void Reset() { try_catch_.Reset(); }
NAN_INLINE void SetVerbose(bool value) { try_catch_.SetVerbose(value); }
NAN_INLINE void SetCaptureMessage(bool value) {
try_catch_.SetCaptureMessage(value);
}
};
//============ =================================================================
/* node 0.12 */
#if NODE_MODULE_VERSION >= NODE_0_12_MODULE_VERSION
NAN_INLINE
void SetCounterFunction(v8::CounterLookupCallback cb) {
v8::Isolate::GetCurrent()->SetCounterFunction(cb);
}
NAN_INLINE
void SetCreateHistogramFunction(v8::CreateHistogramCallback cb) {
v8::Isolate::GetCurrent()->SetCreateHistogramFunction(cb);
}
NAN_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))
NAN_INLINE bool IdleNotification(int idle_time_in_ms) {
return v8::Isolate::GetCurrent()->IdleNotificationDeadline(
idle_time_in_ms * 0.001);
}
# else
NAN_INLINE bool IdleNotification(int idle_time_in_ms) {
return v8::Isolate::GetCurrent()->IdleNotification(idle_time_in_ms);
}
#endif
NAN_INLINE void LowMemoryNotification() {
v8::Isolate::GetCurrent()->LowMemoryNotification();
}
NAN_INLINE void ContextDisposedNotification() {
v8::Isolate::GetCurrent()->ContextDisposedNotification();
}
#else
NAN_INLINE
void SetCounterFunction(v8::CounterLookupCallback cb) {
v8::V8::SetCounterFunction(cb);
}
NAN_INLINE
void SetCreateHistogramFunction(v8::CreateHistogramCallback cb) {
v8::V8::SetCreateHistogramFunction(cb);
}
NAN_INLINE
void SetAddHistogramSampleFunction(v8::AddHistogramSampleCallback cb) {
v8::V8::SetAddHistogramSampleFunction(cb);
}
NAN_INLINE bool IdleNotification(int idle_time_in_ms) {
return v8::V8::IdleNotification(idle_time_in_ms);
}
NAN_INLINE void LowMemoryNotification() {
v8::V8::LowMemoryNotification();
}
NAN_INLINE void ContextDisposedNotification() {
v8::V8::ContextDisposedNotification();
}
#endif
#if (NODE_MODULE_VERSION > NODE_0_10_MODULE_VERSION) // Node 0.12
NAN_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
}
NAN_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
}
NAN_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
}
NAN_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
}
NAN_INLINE v8::Local<v8::String> EmptyString() {
return v8::String::Empty(v8::Isolate::GetCurrent());
}
NAN_INLINE int AdjustExternalMemory(int bc) {
return static_cast<int>(
v8::Isolate::GetCurrent()->AdjustAmountOfExternalAllocatedMemory(bc));
}
NAN_INLINE void SetTemplate(
v8::Local<v8::Template> templ
, const char *name
, v8::Local<v8::Data> value) {
templ->Set(v8::Isolate::GetCurrent(), name, value);
}
NAN_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);
}
NAN_INLINE v8::Local<v8::Context> GetCurrentContext() {
return v8::Isolate::GetCurrent()->GetCurrentContext();
}
NAN_INLINE void* GetInternalFieldPointer(
v8::Local<v8::Object> object
, int index) {
return object->GetAlignedPointerFromInternalField(index);
}
NAN_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
NAN_INLINE void AddGCEpilogueCallback(
GCEpilogueCallback callback
, v8::GCType gc_type_filter = v8::kGCTypeAll) {
v8::Isolate::GetCurrent()->AddGCEpilogueCallback(callback, gc_type_filter);
}
NAN_INLINE void RemoveGCEpilogueCallback(
GCEpilogueCallback callback) {
v8::Isolate::GetCurrent()->RemoveGCEpilogueCallback(callback);
}
NAN_INLINE void AddGCPrologueCallback(
GCPrologueCallback callback
, v8::GCType gc_type_filter = v8::kGCTypeAll) {
v8::Isolate::GetCurrent()->AddGCPrologueCallback(callback, gc_type_filter);
}
NAN_INLINE void RemoveGCPrologueCallback(
GCPrologueCallback callback) {
v8::Isolate::GetCurrent()->RemoveGCPrologueCallback(callback);
}
NAN_INLINE void GetHeapStatistics(
v8::HeapStatistics *heap_statistics) {
v8::Isolate::GetCurrent()->GetHeapStatistics(heap_statistics);
}
# define X(NAME) \
NAN_INLINE v8::Local<v8::Value> NAME(const char *msg) { \
EscapableHandleScope scope; \
return scope.Escape(v8::Exception::NAME(New(msg).ToLocalChecked())); \
} \
\
NAN_INLINE \
v8::Local<v8::Value> NAME(v8::Local<v8::String> msg) { \
return v8::Exception::NAME(msg); \
} \
\
NAN_INLINE void Throw ## NAME(const char *msg) { \
HandleScope scope; \
v8::Isolate::GetCurrent()->ThrowException( \
v8::Exception::NAME(New(msg).ToLocalChecked())); \
} \
\
NAN_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
NAN_INLINE void ThrowError(v8::Local<v8::Value> error) {
v8::Isolate::GetCurrent()->ThrowException(error);
}
NAN_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 MaybeLocal<v8::Object>(node::Buffer::New(
v8::Isolate::GetCurrent(), data, length, callback, hint));
#endif
}
NAN_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 MaybeLocal<v8::Object>(node::Buffer::New(
v8::Isolate::GetCurrent(), data, size));
#endif
}
NAN_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 MaybeLocal<v8::Object>(node::Buffer::New(
v8::Isolate::GetCurrent(), size));
#endif
}
NAN_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 MaybeLocal<v8::Object>(
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))
NAN_INLINE MaybeLocal<v8::String>
NewOneByteString(const uint8_t * value, int length = -1) {
return v8::String::NewFromOneByte(v8::Isolate::GetCurrent(), value,
v8::NewStringType::kNormal, length);
}
NAN_INLINE MaybeLocal<BoundScript> CompileScript(
v8::Local<v8::String> s
, const v8::ScriptOrigin& origin
) {
v8::ScriptCompiler::Source source(s, origin);
return v8::ScriptCompiler::Compile(GetCurrentContext(), &source);
}
NAN_INLINE MaybeLocal<BoundScript> CompileScript(
v8::Local<v8::String> s
) {
v8::ScriptCompiler::Source source(s);
return v8::ScriptCompiler::Compile(GetCurrentContext(), &source);
}
NAN_INLINE MaybeLocal<v8::Value> RunScript(
v8::Local<UnboundScript> script
) {
return script->BindToCurrentContext()->Run(GetCurrentContext());
}
NAN_INLINE MaybeLocal<v8::Value> RunScript(
v8::Local<BoundScript> script
) {
return script->Run(GetCurrentContext());
}
#else
NAN_INLINE MaybeLocal<v8::String>
NewOneByteString(const uint8_t * value, int length = -1) {
return MaybeLocal<v8::String>(
v8::String::NewFromOneByte(
v8::Isolate::GetCurrent()
, value
, v8::String::kNormalString, length));
}
NAN_INLINE MaybeLocal<BoundScript> CompileScript(
v8::Local<v8::String> s
, const v8::ScriptOrigin& origin
) {
v8::ScriptCompiler::Source source(s, origin);
return MaybeLocal<BoundScript>(
v8::ScriptCompiler::Compile(v8::Isolate::GetCurrent(), &source));
}
NAN_INLINE MaybeLocal<BoundScript> CompileScript(
v8::Local<v8::String> s
) {
v8::ScriptCompiler::Source source(s);
return MaybeLocal<BoundScript>(
v8::ScriptCompiler::Compile(v8::Isolate::GetCurrent(), &source));
}
NAN_INLINE MaybeLocal<v8::Value> RunScript(
v8::Local<UnboundScript> script
) {
return MaybeLocal<v8::Value>(script->BindToCurrentContext()->Run());
}
NAN_INLINE MaybeLocal<v8::Value> RunScript(
v8::Local<BoundScript> script
) {
return MaybeLocal<v8::Value>(script->Run());
}
#endif
NAN_INLINE v8::Local<v8::Value> MakeCallback(
v8::Local<v8::Object> target
, v8::Local<v8::Function> func
, int argc
, v8::Local<v8::Value>* argv) {
#if NODE_MODULE_VERSION < IOJS_3_0_MODULE_VERSION
return New(node::MakeCallback(
v8::Isolate::GetCurrent(), target, func, argc, argv));
#else
return node::MakeCallback(
v8::Isolate::GetCurrent(), target, func, argc, argv);
#endif
}
NAN_INLINE v8::Local<v8::Value> MakeCallback(
v8::Local<v8::Object> target
, v8::Local<v8::String> symbol
, int argc
, v8::Local<v8::Value>* argv) {
#if NODE_MODULE_VERSION < IOJS_3_0_MODULE_VERSION
return New(node::MakeCallback(
v8::Isolate::GetCurrent(), target, symbol, argc, argv));
#else
return node::MakeCallback(
v8::Isolate::GetCurrent(), target, symbol, argc, argv);
#endif
}
NAN_INLINE v8::Local<v8::Value> MakeCallback(
v8::Local<v8::Object> target
, const char* method
, int argc
, v8::Local<v8::Value>* argv) {
#if NODE_MODULE_VERSION < IOJS_3_0_MODULE_VERSION
return New(node::MakeCallback(
v8::Isolate::GetCurrent(), target, method, argc, argv));
#else
return node::MakeCallback(
v8::Isolate::GetCurrent(), target, method, argc, argv);
#endif
}
NAN_INLINE void FatalException(const TryCatch& try_catch) {
node::FatalException(v8::Isolate::GetCurrent(), try_catch.try_catch_);
}
NAN_INLINE v8::Local<v8::Value> ErrnoException(
int errorno
, const char* syscall = NULL
, const char* message = NULL
, const char* path = NULL) {
return node::ErrnoException(v8::Isolate::GetCurrent(), errorno, syscall,
message, path);
}
NAN_DEPRECATED NAN_INLINE v8::Local<v8::Value> NanErrnoException(
int errorno
, const char* syscall = NULL
, const char* message = NULL
, const char* path = NULL) {
return ErrnoException(errorno, syscall, message, path);
}
template<typename T>
NAN_INLINE void SetIsolateData(
v8::Isolate *isolate
, T *data
) {
isolate->SetData(0, data);
}
template<typename T>
NAN_INLINE T *GetIsolateData(
v8::Isolate *isolate
) {
return static_cast<T*>(isolate->GetData(0));
}
class Utf8String {
public:
NAN_INLINE explicit Utf8String(v8::Local<v8::Value> from) :
length_(0), str_(str_st_) {
if (!from.IsEmpty()) {
v8::Local<v8::String> string = from->ToString();
if (!string.IsEmpty()) {
size_t len = 3 * string->Length() + 1;
assert(len <= INT_MAX);
if (len > sizeof (str_st_)) {
str_ = static_cast<char*>(malloc(len));
assert(str_ != 0);
}
const int flags =
v8::String::NO_NULL_TERMINATION | imp::kReplaceInvalidUtf8;
length_ = string->WriteUtf8(str_, static_cast<int>(len), 0, flags);
str_[length_] = '\0';
}
}
}
NAN_INLINE int length() const {
return length_;
}
NAN_INLINE char* operator*() { return str_; }
NAN_INLINE const char* operator*() const { return str_; }
NAN_INLINE ~Utf8String() {
if (str_ != str_st_) {
free(str_);
}
}
private:
NAN_DISALLOW_ASSIGN_COPY_MOVE(Utf8String)
int length_;
char *str_;
char str_st_[1024];
};
#else // Node 0.8 and 0.10
NAN_INLINE v8::Local<v8::Primitive> Undefined() {
EscapableHandleScope scope;
return scope.Escape(New(v8::Undefined()));
}
NAN_INLINE v8::Local<v8::Primitive> Null() {
EscapableHandleScope scope;
return scope.Escape(New(v8::Null()));
}
NAN_INLINE v8::Local<v8::Boolean> True() {
EscapableHandleScope scope;
return scope.Escape(New(v8::True()));
}
NAN_INLINE v8::Local<v8::Boolean> False() {
EscapableHandleScope scope;
return scope.Escape(New(v8::False()));
}
NAN_INLINE v8::Local<v8::String> EmptyString() {
return v8::String::Empty();
}
NAN_INLINE int AdjustExternalMemory(int bc) {
return static_cast<int>(v8::V8::AdjustAmountOfExternalAllocatedMemory(bc));
}
NAN_INLINE void SetTemplate(
v8::Local<v8::Template> templ
, const char *name
, v8::Local<v8::Data> value) {
templ->Set(name, value);
}
NAN_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);
}
NAN_INLINE v8::Local<v8::Context> GetCurrentContext() {
return v8::Context::GetCurrent();
}
NAN_INLINE void* GetInternalFieldPointer(
v8::Local<v8::Object> object
, int index) {
return object->GetPointerFromInternalField(index);
}
NAN_INLINE void SetInternalFieldPointer(
v8::Local<v8::Object> object
, int index
, void* value) {
object->SetPointerInInternalField(index, value);
}
# define NAN_GC_CALLBACK(name) \
void name(v8::GCType type, v8::GCCallbackFlags flags)
NAN_INLINE void AddGCEpilogueCallback(
v8::GCEpilogueCallback callback
, v8::GCType gc_type_filter = v8::kGCTypeAll) {
v8::V8::AddGCEpilogueCallback(callback, gc_type_filter);