forked from nodejs/nan
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnan.h
1958 lines (1682 loc) · 62.3 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) 2014 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>
*
* MIT +no-false-attribs License <https://github.com/rvagg/nan/blob/master/LICENSE>
*
* Version 1.0.0 (current Node unstable: 0.11.13, Node stable: 0.10.28)
*
* ChangeLog:
* * 1.0.0 May 4 2014
* - Heavy API changes for V8 3.25 / Node 0.11.13
* - Use cpplint.py
* - Removed NanInitPersistent
* - Removed NanPersistentToLocal
* - Removed NanFromV8String
* - Removed NanMakeWeak
* - Removed NanNewLocal
* - Removed NAN_WEAK_CALLBACK_OBJECT
* - Removed NAN_WEAK_CALLBACK_DATA
* - Introduce NanNew, replaces NanNewLocal, NanPersistentToLocal, adds many overloaded typed versions
* - Introduce NanUndefined, NanNull, NanTrue and NanFalse
* - Introduce NanEscapableScope and NanEscapeScope
* - Introduce NanMakeWeakPersistent (requires a special callback to work on both old and new node)
* - Introduce NanMakeCallback for node::MakeCallback
* - Introduce NanSetTemplate
* - Introduce NanGetCurrentContext
* - Introduce NanCompileScript and NanRunScript
* - Introduce NanAdjustExternalMemory
* - Introduce NanAddGCEpilogueCallback, NanAddGCPrologueCallback, NanRemoveGCEpilogueCallback, NanRemoveGCPrologueCallback
* - Introduce NanGetHeapStatistics
* - Rename NanAsyncWorker#SavePersistent() to SaveToPersistent()
*
* * 0.8.0 Jan 9 2014
* - NanDispose -> NanDisposePersistent, deprecate NanDispose
* - Extract _NAN_*_RETURN_TYPE, pull up NAN_*()
*
* * 0.7.1 Jan 9 2014
* - Fixes to work against debug builds of Node
* - Safer NanPersistentToLocal (avoid reinterpret_cast)
* - Speed up common NanRawString case by only extracting flattened string when necessary
*
* * 0.7.0 Dec 17 2013
* - New no-arg form of NanCallback() constructor.
* - NanCallback#Call takes Handle rather than Local
* - Removed deprecated NanCallback#Run method, use NanCallback#Call instead
* - Split off _NAN_*_ARGS_TYPE from _NAN_*_ARGS
* - Restore (unofficial) Node 0.6 compatibility at NanCallback#Call()
* - Introduce NanRawString() for char* (or appropriate void*) from v8::String
* (replacement for NanFromV8String)
* - Introduce NanCString() for null-terminated char* from v8::String
*
* * 0.6.0 Nov 21 2013
* - Introduce NanNewLocal<T>(v8::Handle<T> value) for use in place of
* v8::Local<T>::New(...) since v8 started requiring isolate in Node 0.11.9
*
* * 0.5.2 Nov 16 2013
* - Convert SavePersistent and GetFromPersistent in NanAsyncWorker from protected and public
*
* * 0.5.1 Nov 12 2013
* - Use node::MakeCallback() instead of direct v8::Function::Call()
*
* * 0.5.0 Nov 11 2013
* - Added @TooTallNate as collaborator
* - New, much simpler, "include_dirs" for binding.gyp
* - Added full range of NAN_INDEX_* macros to match NAN_PROPERTY_* macros
*
* * 0.4.4 Nov 2 2013
* - Isolate argument from v8::Persistent::MakeWeak removed for 0.11.8+
*
* * 0.4.3 Nov 2 2013
* - Include node_object_wrap.h, removed from node.h for Node 0.11.8.
*
* * 0.4.2 Nov 2 2013
* - Handle deprecation of v8::Persistent::Dispose(v8::Isolate* isolate)) for
* Node 0.11.8 release.
*
* * 0.4.1 Sep 16 2013
* - Added explicit `#include <uv.h>` as it was removed from node.h for v0.11.8
*
* * 0.4.0 Sep 2 2013
* - Added NAN_INLINE and NAN_DEPRECATED and made use of them
* - Added NanError, NanTypeError and NanRangeError
* - Cleaned up code
*
* * 0.3.2 Aug 30 2013
* - Fix missing scope declaration in GetFromPersistent() and SaveToPersistent
* in NanAsyncWorker
*
* * 0.3.1 Aug 20 2013
* - fix "not all control paths return a value" compile warning on some platforms
*
* * 0.3.0 Aug 19 2013
* - Made NAN work with NPM
* - Lots of fixes to NanFromV8String, pulling in features from new Node core
* - Changed node::encoding to Nan::Encoding in NanFromV8String to unify the API
* - Added optional error number argument for NanThrowError()
* - Added NanInitPersistent()
* - Added NanReturnNull() and NanReturnEmptyString()
* - Added NanLocker and NanUnlocker
* - Added missing scopes
* - Made sure to clear disposed Persistent handles
* - Changed NanAsyncWorker to allocate error messages on the heap
* - Changed NanThrowError(Local<Value>) to NanThrowError(Handle<Value>)
* - Fixed leak in NanAsyncWorker when errmsg is used
*
* * 0.2.2 Aug 5 2013
* - Fixed usage of undefined variable with node::BASE64 in NanFromV8String()
*
* * 0.2.1 Aug 5 2013
* - Fixed 0.8 breakage, node::BUFFER encoding type not available in 0.8 for
* NanFromV8String()
*
* * 0.2.0 Aug 5 2013
* - Added NAN_PROPERTY_GETTER, NAN_PROPERTY_SETTER, NAN_PROPERTY_ENUMERATOR,
* NAN_PROPERTY_DELETER, NAN_PROPERTY_QUERY
* - Extracted _NAN_METHOD_ARGS, _NAN_GETTER_ARGS, _NAN_SETTER_ARGS,
* _NAN_PROPERTY_GETTER_ARGS, _NAN_PROPERTY_SETTER_ARGS,
* _NAN_PROPERTY_ENUMERATOR_ARGS, _NAN_PROPERTY_DELETER_ARGS,
* _NAN_PROPERTY_QUERY_ARGS
* - Added NanGetInternalFieldPointer, NanSetInternalFieldPointer
* - Added NAN_WEAK_CALLBACK, NAN_WEAK_CALLBACK_OBJECT,
* NAN_WEAK_CALLBACK_DATA, NanMakeWeak
* - Renamed THROW_ERROR to _NAN_THROW_ERROR
* - Added NanNewBufferHandle(char*, size_t, node::smalloc::FreeCallback, void*)
* - Added NanBufferUse(char*, uint32_t)
* - Added NanNewContextHandle(v8::ExtensionConfiguration*,
* v8::Handle<v8::ObjectTemplate>, v8::Handle<v8::Value>)
* - Fixed broken NanCallback#GetFunction()
* - Added optional encoding and size arguments to NanFromV8String()
* - Added NanGetPointerSafe() and NanSetPointerSafe()
* - Added initial test suite (to be expanded)
* - Allow NanUInt32OptionValue to convert any Number object
*
* * 0.1.0 Jul 21 2013
* - Added `NAN_GETTER`, `NAN_SETTER`
* - Added `NanThrowError` with single Local<Value> argument
* - Added `NanNewBufferHandle` with single uint32_t argument
* - Added `NanHasInstance(Persistent<FunctionTemplate>&, Handle<Value>)`
* - Added `Local<Function> NanCallback#GetFunction()`
* - Added `NanCallback#Call(int, Local<Value>[])`
* - Deprecated `NanCallback#Run(int, Local<Value>[])` in favour of Call
*
* See https://github.com/rvagg/nan for the latest update to this file
**********************************************************************************/
#ifndef NAN_H_
#define NAN_H_
#include <uv.h>
#include <node.h>
#include <node_buffer.h>
#include <node_version.h>
#include <node_object_wrap.h>
#include <string.h>
#if defined(__GNUC__) && !defined(DEBUG)
# define NAN_INLINE inline __attribute__((always_inline))
#elif defined(_MSC_VER) && !defined(DEBUG)
# define NAN_INLINE __forceinline
#else
# define NAN_INLINE inline
#endif
#if defined(__GNUC__) && !V8_DISABLE_DEPRECATIONS
# define NAN_DEPRECATED __attribute__((deprecated))
#elif defined(_MSC_VER) && !V8_DISABLE_DEPRECATIONS
# define NAN_DEPRECATED __declspec(deprecated)
#else
# define NAN_DEPRECATED
#endif
// some generic helpers
template<typename T> NAN_INLINE bool NanSetPointerSafe(
T *var
, T val
) {
if (var) {
*var = val;
return true;
} else {
return false;
}
}
template<typename T> NAN_INLINE T NanGetPointerSafe(
T *var
, T fallback = reinterpret_cast<T>(0)
) {
if (var) {
return *var;
} else {
return fallback;
}
}
NAN_INLINE bool NanBooleanOptionValue(
v8::Local<v8::Object> optionsObj
, v8::Handle<v8::String> opt, bool def
) {
if (def) {
return optionsObj.IsEmpty()
|| !optionsObj->Has(opt)
|| optionsObj->Get(opt)->BooleanValue();
} else {
return !optionsObj.IsEmpty()
&& optionsObj->Has(opt)
&& optionsObj->Get(opt)->BooleanValue();
}
}
NAN_INLINE bool NanBooleanOptionValue(
v8::Local<v8::Object> optionsObj
, v8::Handle<v8::String> opt
) {
return NanBooleanOptionValue(optionsObj, opt, false);
}
NAN_INLINE uint32_t NanUInt32OptionValue(
v8::Local<v8::Object> optionsObj
, v8::Handle<v8::String> opt
, uint32_t def
) {
return !optionsObj.IsEmpty()
&& optionsObj->Has(opt)
&& optionsObj->Get(opt)->IsNumber()
? optionsObj->Get(opt)->Uint32Value()
: def;
}
#if (NODE_MODULE_VERSION > 0x000B)
// Node 0.11+ (0.11.3 and below won't compile with these)
# define _NAN_METHOD_ARGS_TYPE const v8::FunctionCallbackInfo<v8::Value>&
# define _NAN_METHOD_ARGS _NAN_METHOD_ARGS_TYPE args
# define _NAN_METHOD_RETURN_TYPE void
# define _NAN_GETTER_ARGS_TYPE const v8::PropertyCallbackInfo<v8::Value>&
# define _NAN_GETTER_ARGS _NAN_GETTER_ARGS_TYPE args
# define _NAN_GETTER_RETURN_TYPE void
# define _NAN_SETTER_ARGS_TYPE const v8::PropertyCallbackInfo<void>&
# define _NAN_SETTER_ARGS _NAN_SETTER_ARGS_TYPE args
# define _NAN_SETTER_RETURN_TYPE void
# define _NAN_PROPERTY_GETTER_ARGS_TYPE \
const v8::PropertyCallbackInfo<v8::Value>&
# define _NAN_PROPERTY_GETTER_ARGS _NAN_PROPERTY_GETTER_ARGS_TYPE args
# define _NAN_PROPERTY_GETTER_RETURN_TYPE void
# define _NAN_PROPERTY_SETTER_ARGS_TYPE \
const v8::PropertyCallbackInfo<v8::Value>&
# define _NAN_PROPERTY_SETTER_ARGS _NAN_PROPERTY_SETTER_ARGS_TYPE args
# define _NAN_PROPERTY_SETTER_RETURN_TYPE void
# define _NAN_PROPERTY_ENUMERATOR_ARGS_TYPE \
const v8::PropertyCallbackInfo<v8::Array>&
# define _NAN_PROPERTY_ENUMERATOR_ARGS _NAN_PROPERTY_ENUMERATOR_ARGS_TYPE args
# define _NAN_PROPERTY_ENUMERATOR_RETURN_TYPE void
# define _NAN_PROPERTY_DELETER_ARGS_TYPE \
const v8::PropertyCallbackInfo<v8::Boolean>&
# define _NAN_PROPERTY_DELETER_ARGS \
_NAN_PROPERTY_DELETER_ARGS_TYPE args
# define _NAN_PROPERTY_DELETER_RETURN_TYPE void
# define _NAN_PROPERTY_QUERY_ARGS_TYPE \
const v8::PropertyCallbackInfo<v8::Integer>&
# define _NAN_PROPERTY_QUERY_ARGS _NAN_PROPERTY_QUERY_ARGS_TYPE args
# define _NAN_PROPERTY_QUERY_RETURN_TYPE void
# define _NAN_INDEX_GETTER_ARGS_TYPE \
const v8::PropertyCallbackInfo<v8::Value>&
# define _NAN_INDEX_GETTER_ARGS _NAN_INDEX_GETTER_ARGS_TYPE args
# define _NAN_INDEX_GETTER_RETURN_TYPE void
# define _NAN_INDEX_SETTER_ARGS_TYPE \
const v8::PropertyCallbackInfo<v8::Value>&
# define _NAN_INDEX_SETTER_ARGS _NAN_INDEX_SETTER_ARGS_TYPE args
# define _NAN_INDEX_SETTER_RETURN_TYPE void
# define _NAN_INDEX_ENUMERATOR_ARGS_TYPE \
const v8::PropertyCallbackInfo<v8::Array>&
# define _NAN_INDEX_ENUMERATOR_ARGS _NAN_INDEX_ENUMERATOR_ARGS_TYPE args
# define _NAN_INDEX_ENUMERATOR_RETURN_TYPE void
# define _NAN_INDEX_DELETER_ARGS_TYPE \
const v8::PropertyCallbackInfo<v8::Boolean>&
# define _NAN_INDEX_DELETER_ARGS _NAN_INDEX_DELETER_ARGS_TYPE args
# define _NAN_INDEX_DELETER_RETURN_TYPE void
# define _NAN_INDEX_QUERY_ARGS_TYPE \
const v8::PropertyCallbackInfo<v8::Integer>&
# define _NAN_INDEX_QUERY_ARGS _NAN_INDEX_QUERY_ARGS_TYPE args
# define _NAN_INDEX_QUERY_RETURN_TYPE void
typedef v8::FunctionCallback NanFunctionCallback;
# define NanUndefined() NanNew(v8::Undefined(v8::Isolate::GetCurrent()))
# define NanNull() NanNew(v8::Null(v8::Isolate::GetCurrent()))
# define NanTrue() NanNew(v8::True(v8::Isolate::GetCurrent()))
# define NanFalse() NanNew(v8::False(v8::Isolate::GetCurrent()))
# define NanAdjustExternalMemory(amount) \
v8::Isolate::GetCurrent()->AdjustAmountOfExternalAllocatedMemory(amount)
# define NanSetTemplate(templ, name, value) \
templ->Set(v8::Isolate::GetCurrent(), name, value)
# define NanGetCurrentContext() v8::Isolate::GetCurrent()->GetCurrentContext()
# define NanMakeCallback(target, func, argc, argv) \
node::MakeCallback(v8::Isolate::GetCurrent(), target, func, argc, argv)
# define NanGetInternalFieldPointer(object, index) \
object->GetAlignedPointerFromInternalField(index)
# define NanSetInternalFieldPointer(object, index, value) \
object->SetAlignedPointerInInternalField(index, value)
template<typename T>
NAN_INLINE v8::Local<T> NanNew() {
return T::New(v8::Isolate::GetCurrent());
}
template<typename T, typename P>
NAN_INLINE v8::Local<T> NanNew(P arg1) {
return T::New(v8::Isolate::GetCurrent(), arg1);
}
template<typename T>
NAN_INLINE v8::Local<v8::Signature> NanNew(
v8::Handle<v8::FunctionTemplate> receiver
, int argc
, v8::Handle<v8::FunctionTemplate> argv[] = 0) {
return v8::Signature::New(v8::Isolate::GetCurrent(), receiver, argc, argv);
}
template<typename T>
NAN_INLINE v8::Local<v8::FunctionTemplate> NanNew(
NanFunctionCallback callback
, v8::Handle<v8::Value> data = v8::Handle<v8::Value>()
, v8::Handle<v8::Signature> signature = v8::Handle<v8::Signature>()) {
return T::New(v8::Isolate::GetCurrent(), callback, data, signature);
}
template<typename T>
NAN_INLINE v8::Local<T> NanNew(v8::Handle<T> arg1) {
return v8::Local<T>::New(v8::Isolate::GetCurrent(), arg1);
}
template<typename T>
NAN_INLINE v8::Local<T> NanNew(const v8::Persistent<T> &arg1) {
return v8::Local<T>::New(v8::Isolate::GetCurrent(), arg1);
}
template<typename T, typename P>
NAN_INLINE v8::Local<T> NanNew(P arg1, int arg2) {
return T::New(v8::Isolate::GetCurrent(), arg1, arg2);
}
template<>
NAN_INLINE v8::Local<v8::Array> NanNew<v8::Array>() {
return v8::Array::New(v8::Isolate::GetCurrent());
}
template<>
NAN_INLINE v8::Local<v8::Array> NanNew<v8::Array>(int length) {
return v8::Array::New(v8::Isolate::GetCurrent(), length);
}
template<>
NAN_INLINE v8::Local<v8::Date> NanNew<v8::Date>(double time) {
return v8::Date::New(v8::Isolate::GetCurrent(), time).As<v8::Date>();
}
template<>
NAN_INLINE v8::Local<v8::Date> NanNew<v8::Date>(int time) {
return v8::Date::New(v8::Isolate::GetCurrent(), time).As<v8::Date>();
}
typedef v8::UnboundScript NanUnboundScript;
typedef v8::Script NanBoundScript;
template<typename T, typename P>
NAN_INLINE v8::Local<T> NanNew(
P s
, const v8::ScriptOrigin& origin
) {
v8::ScriptCompiler::Source source(s, origin);
return v8::ScriptCompiler::CompileUnbound(
v8::Isolate::GetCurrent(), &source);
}
template<>
NAN_INLINE v8::Local<NanUnboundScript> NanNew<NanUnboundScript>(
v8::Local<v8::String> s
) {
v8::ScriptCompiler::Source source(s);
return v8::ScriptCompiler::CompileUnbound(
v8::Isolate::GetCurrent(), &source);
}
NAN_INLINE v8::Local<v8::String> NanNew(
v8::String::ExternalStringResource *resource) {
return v8::String::NewExternal(v8::Isolate::GetCurrent(), resource);
}
NAN_INLINE v8::Local<v8::String> NanNew(
v8::String::ExternalAsciiStringResource *resource) {
return v8::String::NewExternal(v8::Isolate::GetCurrent(), resource);
}
template<>
NAN_INLINE v8::Local<v8::BooleanObject> NanNew(bool value) {
return v8::BooleanObject::New(value).As<v8::BooleanObject>();
}
template<>
NAN_INLINE v8::Local<v8::StringObject>
NanNew<v8::StringObject, v8::Local<v8::String> >(
v8::Local<v8::String> value) {
return v8::StringObject::New(value).As<v8::StringObject>();
}
template<>
NAN_INLINE v8::Local<v8::StringObject>
NanNew<v8::StringObject, v8::Handle<v8::String> >(
v8::Handle<v8::String> value) {
return v8::StringObject::New(value).As<v8::StringObject>();
}
template<>
NAN_INLINE v8::Local<v8::NumberObject> NanNew<v8::NumberObject>(double val) {
return v8::NumberObject::New(
v8::Isolate::GetCurrent(), val).As<v8::NumberObject>();
}
template<typename T>
NAN_INLINE v8::Local<v8::RegExp> NanNew(
v8::Handle<v8::String> pattern, v8::RegExp::Flags flags) {
return v8::RegExp::New(pattern, flags);
}
template<typename T>
NAN_INLINE v8::Local<v8::RegExp> NanNew(
v8::Local<v8::String> pattern, v8::RegExp::Flags flags) {
return v8::RegExp::New(pattern, flags);
}
template<typename T, typename P>
NAN_INLINE v8::Local<v8::RegExp> NanNew(
v8::Handle<v8::String> pattern, v8::RegExp::Flags flags) {
return v8::RegExp::New(pattern, flags);
}
template<typename T, typename P>
NAN_INLINE v8::Local<v8::RegExp> NanNew(
v8::Local<v8::String> pattern, v8::RegExp::Flags flags) {
return v8::RegExp::New(pattern, flags);
}
template<>
NAN_INLINE v8::Local<v8::Uint32> NanNew<v8::Uint32, int32_t>(int32_t val) {
return v8::Uint32::NewFromUnsigned(
v8::Isolate::GetCurrent(), val)->ToUint32();
}
template<>
NAN_INLINE v8::Local<v8::Uint32> NanNew<v8::Uint32, uint32_t>(uint32_t val) {
return v8::Uint32::NewFromUnsigned(
v8::Isolate::GetCurrent(), val)->ToUint32();
}
template<>
NAN_INLINE v8::Local<v8::Int32> NanNew<v8::Int32, int32_t>(int32_t val) {
return v8::Int32::New(v8::Isolate::GetCurrent(), val)->ToInt32();
}
template<>
NAN_INLINE v8::Local<v8::Int32> NanNew<v8::Int32, uint32_t>(uint32_t val) {
return v8::Int32::New(v8::Isolate::GetCurrent(), val)->ToInt32();
}
template<>
NAN_INLINE v8::Local<v8::String> NanNew<v8::String, char *>(
char *arg
, int length) {
return v8::String::NewFromUtf8(
v8::Isolate::GetCurrent()
, arg
, v8::String::kNormalString
, length);
}
template<>
NAN_INLINE v8::Local<v8::String> NanNew<v8::String, const char *>(
const char *arg
, int length) {
return v8::String::NewFromUtf8(
v8::Isolate::GetCurrent()
, arg
, v8::String::kNormalString
, length);
}
template<>
NAN_INLINE v8::Local<v8::String> NanNew<v8::String, char *>(char *arg) {
return v8::String::NewFromUtf8(v8::Isolate::GetCurrent(), arg);
}
template<>
NAN_INLINE v8::Local<v8::String> NanNew<v8::String, const char *>(
const char *arg) {
return v8::String::NewFromUtf8(v8::Isolate::GetCurrent(), arg);
}
template<>
NAN_INLINE v8::Local<v8::String> NanNew<v8::String, uint8_t *>(
uint8_t *arg
, int length) {
return v8::String::NewFromOneByte(
v8::Isolate::GetCurrent()
, arg
, v8::String::kNormalString
, length);
}
template<>
NAN_INLINE v8::Local<v8::String> NanNew<v8::String, const uint8_t *>(
const uint8_t *arg
, int length) {
return v8::String::NewFromOneByte(
v8::Isolate::GetCurrent()
, arg
, v8::String::kNormalString
, length);
}
template<>
NAN_INLINE v8::Local<v8::String> NanNew<v8::String, uint8_t *>(uint8_t *arg) {
return v8::String::NewFromOneByte(v8::Isolate::GetCurrent(), arg);
}
template<>
NAN_INLINE v8::Local<v8::String> NanNew<v8::String, const uint8_t *>(
const uint8_t *arg) {
return v8::String::NewFromOneByte(v8::Isolate::GetCurrent(), arg);
}
template<>
NAN_INLINE v8::Local<v8::String> NanNew<v8::String, uint16_t *>(
uint16_t *arg
, int length) {
return v8::String::NewFromTwoByte(
v8::Isolate::GetCurrent()
, arg
, v8::String::kNormalString
, length);
}
template<>
NAN_INLINE v8::Local<v8::String> NanNew<v8::String, const uint16_t *>(
const uint16_t *arg
, int length) {
return v8::String::NewFromTwoByte(
v8::Isolate::GetCurrent()
, arg
, v8::String::kNormalString
, length);
}
template<>
NAN_INLINE v8::Local<v8::String> NanNew<v8::String, uint16_t *>(
uint16_t *arg) {
return v8::String::NewFromTwoByte(v8::Isolate::GetCurrent(), arg);
}
template<>
NAN_INLINE v8::Local<v8::String> NanNew<v8::String, const uint16_t *>(
const uint16_t *arg) {
return v8::String::NewFromTwoByte(v8::Isolate::GetCurrent(), arg);
}
template<>
NAN_INLINE v8::Local<v8::String> NanNew<v8::String>() {
return v8::String::Empty(v8::Isolate::GetCurrent());
}
NAN_INLINE void NanAddGCEpilogueCallback(
v8::Isolate::GCEpilogueCallback callback
, v8::GCType gc_type_filter = v8::kGCTypeAll) {
v8::Isolate::GetCurrent()->AddGCEpilogueCallback(callback, gc_type_filter);
}
NAN_INLINE void NanRemoveGCEpilogueCallback(
v8::Isolate::GCEpilogueCallback callback) {
v8::Isolate::GetCurrent()->RemoveGCEpilogueCallback(callback);
}
NAN_INLINE void NanAddGCPrologueCallback(
v8::Isolate::GCPrologueCallback callback
, v8::GCType gc_type_filter = v8::kGCTypeAll) {
v8::Isolate::GetCurrent()->AddGCPrologueCallback(callback, gc_type_filter);
}
NAN_INLINE void NanRemoveGCPrologueCallback(
v8::Isolate::GCPrologueCallback callback) {
v8::Isolate::GetCurrent()->RemoveGCPrologueCallback(callback);
}
NAN_INLINE void NanGetHeapStatistics(
v8::HeapStatistics *heap_statistics) {
v8::Isolate::GetCurrent()->GetHeapStatistics(heap_statistics);
}
# define NanSymbol(value) NanNew<v8::String>(value)
template<typename T>
NAN_INLINE void NanAssignPersistent(
v8::Persistent<T>& handle
, v8::Handle<T> obj) {
handle.Reset(v8::Isolate::GetCurrent(), obj);
}
template<typename T>
NAN_INLINE void NanAssignPersistent(
v8::Persistent<T>& handle
, const v8::Persistent<T>& obj) {
handle.Reset(v8::Isolate::GetCurrent(), obj);
}
template<typename T, typename P>
struct _NanWeakCallbackInfo {
typedef void (*Callback)(
const v8::WeakCallbackData<T, _NanWeakCallbackInfo<T, P> >& data);
_NanWeakCallbackInfo(v8::Handle<T> handle, P* param, Callback cb)
: parameter(param), callback(cb) {
NanAssignPersistent(persistent, handle);
}
~_NanWeakCallbackInfo() {
persistent.Reset();
}
P* const parameter;
Callback const callback;
v8::Persistent<T> persistent;
};
template<typename T, typename P>
class _NanWeakCallbackData {
public:
_NanWeakCallbackData(_NanWeakCallbackInfo<T, P> *info)
: info_(info) { }
NAN_INLINE v8::Local<T> GetValue() const {
return NanNew(info_->persistent);
}
NAN_INLINE P* GetParameter() const { return info_->parameter; }
NAN_INLINE void Revive() const {
info_->persistent.SetWeak(info_, info_->callback);
}
NAN_INLINE _NanWeakCallbackInfo<T, P>* GetCallbackInfo() const {
return info_;
}
NAN_INLINE void Dispose() const {
delete info_;
}
private:
_NanWeakCallbackInfo<T, P>* info_;
};
// do not use for declaration
# define NAN_WEAK_CALLBACK(name) \
template<typename T, typename P> \
static void name( \
const v8::WeakCallbackData<T, _NanWeakCallbackInfo<T, P> > &data) { \
_NanWeakCallbackData<T, P> wcbd( \
data.GetParameter()); \
_Nan_Weak_Callback_ ## name(wcbd); \
} \
\
template<typename T, typename P> \
static NAN_INLINE void _Nan_Weak_Callback_ ## name( \
const _NanWeakCallbackData<T, P> &data)
# define NanScope() v8::HandleScope scope(v8::Isolate::GetCurrent())
# define NanEscapableScope() \
v8::EscapableHandleScope scope(v8::Isolate::GetCurrent())
# define NanEscapeScope(val) scope.Escape(val)
# define NanLocker() v8::Locker locker(v8::Isolate::GetCurrent())
# define NanUnlocker() v8::Unlocker unlocker(v8::Isolate::GetCurrent())
# define NanReturnValue(value) return args.GetReturnValue().Set(value)
# define NanReturnUndefined() return
# define NanReturnNull() return args.GetReturnValue().SetNull()
# define NanReturnEmptyString() return args.GetReturnValue().SetEmptyString()
# define NanObjectWrapHandle(obj) obj->handle()
template<typename T, typename P>
NAN_INLINE _NanWeakCallbackInfo<T, P>* NanMakeWeakPersistent(
v8::Handle<T> handle
, P* parameter
, typename _NanWeakCallbackInfo<T, P>::Callback callback) {
_NanWeakCallbackInfo<T, P> *cbinfo =
new _NanWeakCallbackInfo<T, P>(handle, parameter, callback);
cbinfo->persistent.SetWeak(cbinfo, callback);
return cbinfo;
}
# define _NAN_ERROR(fun, errmsg) fun(NanNew<v8::String>(errmsg))
# define _NAN_THROW_ERROR(fun, errmsg) \
do { \
NanScope(); \
v8::Isolate::GetCurrent()->ThrowException(_NAN_ERROR(fun, errmsg)); \
} while (0);
NAN_INLINE v8::Local<v8::Value> NanError(const char* errmsg) {
return _NAN_ERROR(v8::Exception::Error, errmsg);
}
NAN_INLINE void NanThrowError(const char* errmsg) {
_NAN_THROW_ERROR(v8::Exception::Error, errmsg);
}
NAN_INLINE void NanThrowError(v8::Handle<v8::Value> error) {
NanScope();
v8::Isolate::GetCurrent()->ThrowException(error);
}
NAN_INLINE v8::Local<v8::Value> NanError(
const char *msg
, const int errorNumber
) {
v8::Local<v8::Value> err = v8::Exception::Error(NanNew<v8::String>(msg));
v8::Local<v8::Object> obj = err.As<v8::Object>();
obj->Set(NanSymbol("code"), NanNew<v8::Integer>(errorNumber));
return err;
}
NAN_INLINE void NanThrowError(
const char *msg
, const int errorNumber
) {
NanThrowError(NanError(msg, errorNumber));
}
NAN_INLINE v8::Local<v8::Value> NanTypeError(const char* errmsg) {
return _NAN_ERROR(v8::Exception::TypeError, errmsg);
}
NAN_INLINE void NanThrowTypeError(const char* errmsg) {
_NAN_THROW_ERROR(v8::Exception::TypeError, errmsg);
}
NAN_INLINE v8::Local<v8::Value> NanRangeError(const char* errmsg) {
return _NAN_ERROR(v8::Exception::RangeError, errmsg);
}
NAN_INLINE void NanThrowRangeError(const char* errmsg) {
_NAN_THROW_ERROR(v8::Exception::RangeError, errmsg);
}
template<typename T> NAN_INLINE void NanDisposePersistent(
v8::Persistent<T> &handle
) {
handle.Reset();
}
NAN_INLINE v8::Local<v8::Object> NanNewBufferHandle (
char *data
, size_t length
, node::smalloc::FreeCallback callback
, void *hint
) {
return node::Buffer::New(
v8::Isolate::GetCurrent(), data, length, callback, hint);
}
NAN_INLINE v8::Local<v8::Object> NanNewBufferHandle (
const char *data
, uint32_t size
) {
return node::Buffer::New(v8::Isolate::GetCurrent(), data, size);
}
NAN_INLINE v8::Local<v8::Object> NanNewBufferHandle (uint32_t size) {
return node::Buffer::New(v8::Isolate::GetCurrent(), size);
}
NAN_INLINE v8::Local<v8::Object> NanBufferUse(
char* data
, uint32_t size
) {
return node::Buffer::Use(v8::Isolate::GetCurrent(), data, size);
}
NAN_INLINE bool NanHasInstance(
v8::Persistent<v8::FunctionTemplate>& function_template
, v8::Handle<v8::Value> value
) {
return NanNew(function_template)->HasInstance(value);
}
NAN_INLINE v8::Local<v8::Context> NanNewContextHandle(
v8::ExtensionConfiguration* extensions = NULL
, v8::Handle<v8::ObjectTemplate> tmpl = v8::Handle<v8::ObjectTemplate>()
, v8::Handle<v8::Value> obj = v8::Handle<v8::Value>()
) {
v8::Isolate* isolate = v8::Isolate::GetCurrent();
return v8::Local<v8::Context>::New(
isolate
, v8::Context::New(isolate, extensions, tmpl, obj)
);
}
NAN_INLINE v8::Local<NanBoundScript> NanCompileScript(
v8::Local<v8::String> s
, const v8::ScriptOrigin& origin
) {
v8::ScriptCompiler::Source source(s, origin);
return v8::ScriptCompiler::Compile(v8::Isolate::GetCurrent(), &source);
}
NAN_INLINE v8::Local<NanBoundScript> NanCompileScript(
v8::Local<v8::String> s
) {
v8::ScriptCompiler::Source source(s);
return v8::ScriptCompiler::Compile(v8::Isolate::GetCurrent(), &source);
}
NAN_INLINE v8::Local<v8::Value> NanRunScript(
v8::Handle<NanUnboundScript> script
) {
return script->BindToCurrentContext()->Run();
}
NAN_INLINE v8::Local<v8::Value> NanRunScript(
v8::Handle<NanBoundScript> script
) {
return script->Run();
}
#else
// Node 0.8 and 0.10
# define _NAN_METHOD_ARGS_TYPE const v8::Arguments&
# define _NAN_METHOD_ARGS _NAN_METHOD_ARGS_TYPE args
# define _NAN_METHOD_RETURN_TYPE v8::Handle<v8::Value>
# define _NAN_GETTER_ARGS_TYPE const v8::AccessorInfo &
# define _NAN_GETTER_ARGS _NAN_GETTER_ARGS_TYPE args
# define _NAN_GETTER_RETURN_TYPE v8::Handle<v8::Value>
# define _NAN_SETTER_ARGS_TYPE const v8::AccessorInfo &
# define _NAN_SETTER_ARGS _NAN_SETTER_ARGS_TYPE args
# define _NAN_SETTER_RETURN_TYPE void
# define _NAN_PROPERTY_GETTER_ARGS_TYPE const v8::AccessorInfo&
# define _NAN_PROPERTY_GETTER_ARGS _NAN_PROPERTY_GETTER_ARGS_TYPE args
# define _NAN_PROPERTY_GETTER_RETURN_TYPE v8::Handle<v8::Value>
# define _NAN_PROPERTY_SETTER_ARGS_TYPE const v8::AccessorInfo&
# define _NAN_PROPERTY_SETTER_ARGS _NAN_PROPERTY_SETTER_ARGS_TYPE args
# define _NAN_PROPERTY_SETTER_RETURN_TYPE v8::Handle<v8::Value>
# define _NAN_PROPERTY_ENUMERATOR_ARGS_TYPE const v8::AccessorInfo&
# define _NAN_PROPERTY_ENUMERATOR_ARGS _NAN_PROPERTY_ENUMERATOR_ARGS_TYPE args
# define _NAN_PROPERTY_ENUMERATOR_RETURN_TYPE v8::Handle<v8::Array>
# define _NAN_PROPERTY_DELETER_ARGS_TYPE const v8::AccessorInfo&
# define _NAN_PROPERTY_DELETER_ARGS _NAN_PROPERTY_DELETER_ARGS_TYPE args
# define _NAN_PROPERTY_DELETER_RETURN_TYPE v8::Handle<v8::Boolean>
# define _NAN_PROPERTY_QUERY_ARGS_TYPE const v8::AccessorInfo&
# define _NAN_PROPERTY_QUERY_ARGS _NAN_PROPERTY_QUERY_ARGS_TYPE args
# define _NAN_PROPERTY_QUERY_RETURN_TYPE v8::Handle<v8::Integer>
# define _NAN_INDEX_GETTER_ARGS_TYPE const v8::AccessorInfo&
# define _NAN_INDEX_GETTER_ARGS _NAN_INDEX_GETTER_ARGS_TYPE args
# define _NAN_INDEX_GETTER_RETURN_TYPE v8::Handle<v8::Value>
# define _NAN_INDEX_SETTER_ARGS_TYPE const v8::AccessorInfo&
# define _NAN_INDEX_SETTER_ARGS _NAN_INDEX_SETTER_ARGS_TYPE args
# define _NAN_INDEX_SETTER_RETURN_TYPE v8::Handle<v8::Value>
# define _NAN_INDEX_ENUMERATOR_ARGS_TYPE const v8::AccessorInfo&
# define _NAN_INDEX_ENUMERATOR_ARGS _NAN_INDEX_ENUMERATOR_ARGS_TYPE args
# define _NAN_INDEX_ENUMERATOR_RETURN_TYPE v8::Handle<v8::Array>
# define _NAN_INDEX_DELETER_ARGS_TYPE const v8::AccessorInfo&
# define _NAN_INDEX_DELETER_ARGS _NAN_INDEX_DELETER_ARGS_TYPE args
# define _NAN_INDEX_DELETER_RETURN_TYPE v8::Handle<v8::Boolean>
# define _NAN_INDEX_QUERY_ARGS_TYPE const v8::AccessorInfo&
# define _NAN_INDEX_QUERY_ARGS _NAN_INDEX_QUERY_ARGS_TYPE args
# define _NAN_INDEX_QUERY_RETURN_TYPE v8::Handle<v8::Integer>
typedef v8::InvocationCallback NanFunctionCallback;
# define NanUndefined() NanNew(v8::Undefined())
# define NanNull() NanNew(v8::Null())
# define NanTrue() NanNew(v8::True())
# define NanFalse() NanNew(v8::False())
# define NanAdjustExternalMemory(amount) \
v8::V8::AdjustAmountOfExternalAllocatedMemory(amount)
# define NanSetTemplate(templ, name, value) templ->Set(name, value)
# define NanGetCurrentContext() v8::Context::GetCurrent()
# if NODE_VERSION_AT_LEAST(0, 8, 0)
# define NanMakeCallback(target, func, argc, argv) \
node::MakeCallback(target, func, argc, argv)
# else
# define NanMakeCallback(target, func, argc, argv) \
do { \
v8::TryCatch try_catch; \
func->Call(target, argc, argv); \
if (try_catch.HasCaught()) { \
v8::FatalException(try_catch); \
} \
} while (0)
# endif
# define NanSymbol(value) v8::String::NewSymbol(value)
template<typename T>
NAN_INLINE v8::Local<T> NanNew() {
return v8::Local<T>::New(T::New());
}
template<typename T>
NAN_INLINE v8::Local<T> NanNew(v8::Handle<T> arg) {
return v8::Local<T>::New(arg);
}
template<typename T>
NAN_INLINE v8::Local<v8::Signature> NanNew(
v8::Handle<v8::FunctionTemplate> receiver
, int argc
, v8::Handle<v8::FunctionTemplate> argv[] = 0) {
return v8::Signature::New(receiver, argc, argv);
}
template<typename T>
NAN_INLINE v8::Local<v8::FunctionTemplate> NanNew(
NanFunctionCallback callback
, v8::Handle<v8::Value> data = v8::Handle<v8::Value>()
, v8::Handle<v8::Signature> signature = v8::Handle<v8::Signature>()) {
return T::New(callback, data, signature);
}
template<typename T>
NAN_INLINE v8::Local<T> NanNew(const v8::Persistent<T> &arg) {
return v8::Local<T>::New(arg);
}
template<typename T, typename P>
NAN_INLINE v8::Local<T> NanNew(P arg) {
return v8::Local<T>::New(T::New(arg));
}
template<typename T, typename P>
NAN_INLINE v8::Local<T> NanNew(P arg, int length) {
return v8::Local<T>::New(T::New(arg, length));
}
template<typename T>
NAN_INLINE v8::Local<v8::RegExp> NanNew(
v8::Handle<v8::String> pattern, v8::RegExp::Flags flags) {
return v8::RegExp::New(pattern, flags);
}
template<typename T>
NAN_INLINE v8::Local<v8::RegExp> NanNew(
v8::Local<v8::String> pattern, v8::RegExp::Flags flags) {
return v8::RegExp::New(pattern, flags);
}
template<typename T, typename P>
NAN_INLINE v8::Local<v8::RegExp> NanNew(
v8::Handle<v8::String> pattern, v8::RegExp::Flags flags) {
return v8::RegExp::New(pattern, flags);
}
template<typename T, typename P>
NAN_INLINE v8::Local<v8::RegExp> NanNew(
v8::Local<v8::String> pattern, v8::RegExp::Flags flags) {
return v8::RegExp::New(pattern, flags);
}
template<>
NAN_INLINE v8::Local<v8::Array> NanNew<v8::Array>() {
return v8::Array::New();
}