/
utils.h
1109 lines (912 loc) · 39.3 KB
/
utils.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
/***************************************************************************
* Copyright (C) 2012 by Leo Hoo *
* lion@9465.net *
* *
***************************************************************************/
#ifndef _fj_utils_H_
#define _fj_utils_H_
/**
@author Leo Hoo <lion@9465.net>
*/
#include <exlib/include/osconfig.h>
#include <errno.h>
#ifdef _WIN32
#include <ws2tcpip.h>
#include <winsock2.h>
#include <mstcpip.h>
#ifndef IPV6_V6ONLY
#define IPV6_V6ONLY 27
#endif // IPV6_V6ONLY
#include <windows.h>
#ifndef EWOULDBLOCK
#define EINPROGRESS WSAEWOULDBLOCK
#endif // EWOULDBLOCK
#define SHUT_RD SD_READ
#define SHUT_WR SD_SEND
#define SHUT_RDWR SD_BOTH
#else // _WIN32
#include <sys/socket.h>
#include <sys/types.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <netinet/in.h>
typedef int32_t SOCKET;
#define SOCKET_ERROR -1
#define INVALID_SOCKET -1
#define closesocket close
#if defined(FreeBSD) || defined(OpenBSD)
#include <net/if.h>
#include <netinet/tcp.h>
#ifndef ETIME
#define ETIME ETIMEDOUT
#endif // ETIME
#else // FreeBSD
#include <netinet/tcp.h>
#ifndef TCP_KEEPIDLE
#define TCP_KEEPIDLE TCP_KEEPALIVE
#endif // TCP_KEEPIDLE
#endif // FreeBSD
#ifndef SOL_TCP
#define SOL_TCP IPPROTO_TCP
#endif // SOL_TCP
#endif // _WIN32
#ifndef MSG_NOSIGNAL
#define MSG_NOSIGNAL 0
#endif // MSG_NOSIGNAL
#include "qstring.h"
#include <v8/include/v8.h>
#include "obj_ptr.h"
#include <Isolate.h>
#include <math.h>
#include <vector>
#ifdef _WIN32
#ifndef INFINITY
#define INFINITY (DBL_MAX + DBL_MAX)
#endif // INFINITY
#ifndef NAN
#define NAN (INFINITY - INFINITY)
#endif // NAN
#ifndef isnan
#define isnan !!_isnan
#endif // isnan
#endif // _WIN32
#include "date.h"
#include "Variant.h"
#include <exlib/include/service.h>
#ifdef _DEBUG
#undef _DEBUG
#endif // _DEBUG
namespace fibjs {
typedef int32_t result_t;
#define CALL_RETURN_NULL 100000
#define CALL_E_MAX -100000
// Invalid number of parameters.
#define CALL_E_BADPARAMCOUNT (CALL_E_MAX - 1)
// Parameter not optional.
#define CALL_E_PARAMNOTOPTIONAL (CALL_E_MAX - 2)
// The input parameter is not a valid type.
#define CALL_E_BADVARTYPE (CALL_E_MAX - 3)
// Invalid argument.
#define CALL_E_INVALIDARG (CALL_E_MAX - 4)
// The argument could not be coerced to the specified type.
#define CALL_E_TYPEMISMATCH (CALL_E_MAX - 5)
// Value is out of range.
#define CALL_E_OUTRANGE (CALL_E_MAX - 6)
#define CALL_E_MIN_ARG (CALL_E_OUTRANGE - 1)
// Constructor cannot be called as a function.
#define CALL_E_CONSTRUCTOR (CALL_E_MAX - 7)
// Object is not an instance of declaring class.
#define CALL_E_NOTINSTANCE (CALL_E_MAX - 8)
// Invalid procedure call.
#define CALL_E_INVALID_CALL (CALL_E_MAX - 9)
// Re-entrant calls are not allowed.
#define CALL_E_REENTRANT (CALL_E_MAX - 10)
// Invalid input data
#define CALL_E_INVALID_DATA (CALL_E_MAX - 11)
// Index was out of range.
#define CALL_E_BADINDEX (CALL_E_MAX - 12)
// Memory overflow error.
#define CALL_E_OVERFLOW (CALL_E_MAX - 13)
// Collection is empty.
#define CALL_E_EMPTY (CALL_E_MAX - 14)
// Operation now in progress.
#define CALL_E_PENDDING (CALL_E_MAX - 15)
// Operation not support asynchronous call.
#define CALL_E_NOASYNC (CALL_E_MAX - 16)
// Operation not support synchronous call.
#define CALL_E_NOSYNC (CALL_E_MAX - 17)
// Operation is long synchronous call.
#define CALL_E_LONGSYNC (CALL_E_MAX - 18)
// Operation is GUI call.
#define CALL_E_GUICALL (CALL_E_MAX - 19)
// Internal error.
#define CALL_E_INTERNAL (CALL_E_MAX - 20)
// Invalid return type.
#define CALL_E_RETURN_TYPE (CALL_E_MAX - 21)
// Exception occurred.
#define CALL_E_EXCEPTION (CALL_E_MAX - 22)
// Javascript error.
#define CALL_E_JAVASCRIPT (CALL_E_MAX - 23)
// Permission denied
#define CALL_E_PERMIT (CALL_E_MAX - 24)
#define CALL_E_MIN -100100
#ifndef _WIN32
#define CALL_E_FILE_NOT_FOUND (-ENOENT)
#define CALL_E_PATH_NOT_FOUND (-ENOENT)
#define CALL_E_BAD_FILE (-EBADF)
#else
#define CALL_E_FILE_NOT_FOUND (-ERROR_FILE_NOT_FOUND)
#define CALL_E_PATH_NOT_FOUND (-ERROR_PATH_NOT_FOUND)
#define CALL_E_BAD_FILE (-ERROR_OPERATION_ABORTED)
#endif
#define STREAM_BUFF_SIZE 65536
#if 0
#define V8_SCOPE(isolate) v8::EscapableHandleScope handle_scope(isolate)
#define V8_RETURN(v) handle_scope.Escape(v)
#else
#define V8_SCOPE(isolate) (isolate = isolate)
#define V8_RETURN(v) (v)
#endif
#define PROPERTY_ENTER() \
v8::Isolate* isolate = args.GetIsolate(); \
V8_SCOPE(isolate); \
result_t hr = 0; \
bool bStrict = false; \
do { \
do {
#define METHOD_OVER(c, o) \
} \
while (0) \
; \
if (hr > CALL_E_MIN_ARG && hr < CALL_E_MAX) \
do { \
hr = 0; \
int32_t argc = args.Length(); \
while (argc > (o) && args[argc - 1]->IsUndefined()) \
argc--; \
if ((c) >= 0 && argc > (c)) { \
hr = CALL_E_BADPARAMCOUNT; \
break; \
} \
if ((o) > 0 && argc < (o)) { \
hr = CALL_E_PARAMNOTOPTIONAL; \
break; \
}
#define ASYNC_METHOD_OVER(c, o) \
} \
while (0) \
; \
if (hr > CALL_E_MIN_ARG && hr < CALL_E_MAX) \
do { \
hr = 0; \
int32_t argc = args.Length(); \
while (argc > (o + 1) && args[argc - 1]->IsUndefined()) \
argc--; \
v8::Local<v8::Function> cb; \
if (argc > 0 && args[argc - 1]->IsFunction()) \
cb = v8::Local<v8::Function>::Cast(args[--argc]); \
while (argc > (o) && args[argc - 1]->IsUndefined()) \
argc--; \
if ((c) >= 0 && argc > (c)) { \
hr = CALL_E_BADPARAMCOUNT; \
break; \
} \
if ((o) > 0 && argc < (o)) { \
hr = CALL_E_PARAMNOTOPTIONAL; \
break; \
}
#define METHOD_ENTER() \
v8::Isolate* isolate = args.GetIsolate(); \
V8_SCOPE(isolate); \
result_t hr = CALL_E_BADPARAMCOUNT; \
bool bStrict = true; \
do { \
do {
#define CONSTRUCT_INIT() \
if (class_info().init_isolate()) \
return;
#define CONSTRUCT_ENTER() \
if (!args.IsConstructCall()) { \
ThrowResult(CALL_E_CONSTRUCTOR); \
return; \
} \
METHOD_ENTER()
#define METHOD_INSTANCE(cls) \
obj_ptr<cls> pInst = cls::getInstance(args.This()); \
if (pInst == NULL) { \
ThrowResult(CALL_E_NOTINSTANCE); \
return; \
} \
scope l(pInst);
#define CHECK_ARGUMENT() \
} \
while (0) \
; \
if (!bStrict || (hr != CALL_E_BADPARAMCOUNT && hr != CALL_E_PARAMNOTOPTIONAL && hr != CALL_E_TYPEMISMATCH)) \
break; \
bStrict = false; \
} \
while (true) \
;
#define PROPERTY_SET_LEAVE() \
CHECK_ARGUMENT() \
if (hr < 0) \
ThrowResult(hr);
#define THROW_ERROR() \
if (hr == CALL_E_JAVASCRIPT) { \
args.GetReturnValue().Set(V8_RETURN(v8::Local<v8::Value>())); \
return; \
} \
ThrowResult(hr); \
return;
#define METHOD_RETURN() \
CHECK_ARGUMENT() \
if (hr == CALL_RETURN_NULL) { \
args.GetReturnValue().SetNull(); \
return; \
} \
if (hr >= 0) { \
args.GetReturnValue().Set(V8_RETURN(GetReturnValue(isolate, vr))); \
return; \
} \
THROW_ERROR()
#define METHOD_RETURN1() \
CHECK_ARGUMENT() \
args.GetReturnValue().Set(V8_RETURN(vr)); \
return;
#define METHOD_VOID() \
CHECK_ARGUMENT() \
if (hr >= 0) { \
args.GetReturnValue().SetUndefined(); \
return; \
} \
THROW_ERROR()
#define CONSTRUCT_RETURN() \
CHECK_ARGUMENT() \
if (hr >= 0) { \
args.GetReturnValue().Set(V8_RETURN(vr->wrap(args.This()))); \
return; \
} \
THROW_ERROR()
#define PROPERTY_VAL(t) \
t v0; \
hr = GetArgumentValue(isolate, value, v0, bStrict); \
if (hr < 0) \
break;
#define ARG(t, n) \
t v##n; \
hr = GetArgumentValue(isolate, args[n], v##n, bStrict); \
if (hr < 0) \
break;
#define STRICT_ARG(t, n) \
t v##n; \
hr = GetArgumentValue(isolate, args[n], v##n, true); \
if (hr < 0) \
break;
#define OPT_ARG(t, n, d) \
t v##n = (d); \
if ((n) < argc && !args[n]->IsUndefined()) { \
hr = GetArgumentValue(isolate, args[n], v##n, bStrict); \
if (hr < 0) \
break; \
}
#define DECLARE_CLASSINFO(c) \
public: \
static ClassInfo& class_info(); \
virtual ClassInfo& Classinfo() \
{ \
return class_info(); \
} \
static c* getInstance(void* o) \
{ \
return (c*)class_info().getInstance(o); \
} \
static c* getInstance(v8::Local<v8::Value> o) \
{ \
return (c*)class_info().getInstance(o); \
}
#define DECLARE_CLASS(c) \
public: \
c() { c::class_info().Ref(); } \
virtual ~c() { c::class_info().Unref(); } \
DECLARE_CLASSINFO(c)
#define DECLARE_MODULE(name) \
class RootModule_##name : public RootModule { \
public: \
virtual ClassInfo& class_info() \
{ \
return name##_base::class_info(); \
} \
} s_RootModule_##name;
#define DECLARE_MODULE_EX(mname, module) \
class RootModule_##mname : public RootModule { \
public: \
virtual ClassInfo& class_info() \
{ \
return module##_base::class_info(); \
} \
virtual const char* name() \
{ \
return #mname; \
} \
} s_RootModule_##mname;
#define EVENT_SUPPORT() \
public: \
virtual result_t on(exlib::string ev, v8::Local<v8::Function> func, \
v8::Local<v8::Object>& retVal) \
{ \
return object_base::on(ev, func, retVal); \
} \
virtual result_t on(v8::Local<v8::Object> map, v8::Local<v8::Object>& retVal) \
{ \
return object_base::on(map, retVal); \
} \
virtual result_t prependListener(exlib::string ev, v8::Local<v8::Function> func, \
v8::Local<v8::Object>& retVal) \
{ \
return object_base::prependListener(ev, func, retVal); \
} \
virtual result_t prependListener(v8::Local<v8::Object> map, \
v8::Local<v8::Object>& retVal) \
{ \
return object_base::prependListener(map, retVal); \
} \
virtual result_t addListener(exlib::string ev, v8::Local<v8::Function> func, \
v8::Local<v8::Object>& retVal) \
{ \
return object_base::on(ev, func, retVal); \
} \
virtual result_t addListener(v8::Local<v8::Object> map, v8::Local<v8::Object>& retVal) \
{ \
return object_base::on(map, retVal); \
} \
virtual result_t prependOnceListener(exlib::string ev, v8::Local<v8::Function> func, \
v8::Local<v8::Object>& retVal) \
{ \
return object_base::prependOnceListener(ev, func, retVal); \
} \
virtual result_t prependOnceListener(v8::Local<v8::Object> map, \
v8::Local<v8::Object>& retVal) \
{ \
return object_base::prependOnceListener(map, retVal); \
} \
virtual result_t once(exlib::string ev, v8::Local<v8::Function> func, \
v8::Local<v8::Object>& retVal) \
{ \
return object_base::once(ev, func, retVal); \
} \
virtual result_t once(v8::Local<v8::Object> map, v8::Local<v8::Object>& retVal) \
{ \
return object_base::once(map, retVal); \
} \
virtual result_t off(exlib::string ev, v8::Local<v8::Function> func, \
v8::Local<v8::Object>& retVal) \
{ \
return object_base::off(ev, func, retVal); \
} \
virtual result_t off(exlib::string ev, v8::Local<v8::Object>& retVal) \
{ \
return object_base::off(ev, retVal); \
} \
virtual result_t off(v8::Local<v8::Object> map, v8::Local<v8::Object>& retVal) \
{ \
return object_base::off(map, retVal); \
} \
virtual result_t removeListener(exlib::string ev, v8::Local<v8::Function> func, \
v8::Local<v8::Object>& retVal) \
{ \
return object_base::off(ev, func, retVal); \
} \
virtual result_t removeListener(exlib::string ev, v8::Local<v8::Object>& retVal) \
{ \
return object_base::off(ev, retVal); \
} \
virtual result_t removeListener(v8::Local<v8::Object> map, \
v8::Local<v8::Object>& retVal) \
{ \
return object_base::off(map, retVal); \
} \
virtual result_t removeAllListeners(v8::Local<v8::Array> evs, \
v8::Local<v8::Object>& retVal) \
{ \
return object_base::removeAllListeners(evs, retVal); \
} \
virtual result_t setMaxListeners(int32_t n) \
{ \
return object_base::setMaxListeners(n); \
} \
virtual result_t getMaxListeners(int32_t& retVal) \
{ \
return object_base::getMaxListeners(retVal); \
} \
virtual result_t listeners(exlib::string ev, v8::Local<v8::Array>& retVal) \
{ \
return object_base::listeners(ev, retVal); \
} \
virtual result_t listenerCount(exlib::string ev, int32_t& retVal) \
{ \
return object_base::listenerCount(ev, retVal); \
} \
virtual result_t emit(exlib::string ev, const v8::FunctionCallbackInfo<v8::Value>& args, \
bool& retVal) \
{ \
return object_base::emit(ev, args, retVal); \
} \
virtual result_t eventNames(v8::Local<v8::Array>& retVal) \
{ \
return object_base::eventNames(retVal); \
}
#define EVENT_FUNC(e) \
virtual result_t get_on##e(v8::Local<v8::Function>& retVal) \
{ \
return getListener(#e, retVal); \
} \
virtual result_t set_on##e(v8::Local<v8::Function> newVal) \
{ \
return setListener(#e, newVal); \
}
#define FIBER_FREE() \
public: \
virtual bool enterTask(exlib::Task_base* current) \
{ \
return true; \
} \
virtual void enter() \
{ \
} \
virtual void leave(exlib::Task_base* current = NULL) \
{ \
}
#ifndef ARRAYSIZE
#define ARRAYSIZE(a) \
((sizeof(a) / sizeof(*(a))) / static_cast<size_t>(!(sizeof(a) % sizeof(*(a)))))
#endif
inline result_t GetArgumentValue(v8::Local<v8::Value> v, exlib::string& n, bool bStrict = false)
{
if (v.IsEmpty())
return CALL_E_TYPEMISMATCH;
if (bStrict && !v->IsString() && !v->IsStringObject())
return CALL_E_TYPEMISMATCH;
v8::String::Utf8Value tmp(v);
n.assign(*tmp, tmp.length());
return 0;
}
inline result_t GetArgumentValue(v8::Isolate* isolate, v8::Local<v8::Value> v, exlib::string& n, bool bStrict = false)
{
return GetArgumentValue(v, n, bStrict);
}
inline result_t GetArgumentValue(v8::Local<v8::Value> v, double& n, bool bStrict = false)
{
if (v.IsEmpty())
return CALL_E_TYPEMISMATCH;
if (!v->IsNumber() && !v->IsNumberObject()) {
if (bStrict)
return CALL_E_TYPEMISMATCH;
v = v->ToNumber();
if (v.IsEmpty())
return CALL_E_TYPEMISMATCH;
}
n = v->NumberValue();
if (isnan(n))
return CALL_E_TYPEMISMATCH;
return 0;
}
inline result_t GetArgumentValue(v8::Isolate* isolate, v8::Local<v8::Value> v, double& n, bool bStrict = false)
{
return GetArgumentValue(v, n, bStrict);
}
inline result_t GetArgumentValue(v8::Local<v8::Value> v, int64_t& n, bool bStrict = false)
{
double num;
result_t hr = GetArgumentValue(v, num, bStrict);
if (hr < 0)
return hr;
if (num < -9007199254740992ll || num > 9007199254740992ll)
return CALL_E_OUTRANGE;
n = (int64_t)num;
return 0;
}
inline result_t GetArgumentValue(v8::Isolate* isolate, v8::Local<v8::Value> v, int64_t& n, bool bStrict = false)
{
return GetArgumentValue(v, n, bStrict);
}
inline result_t GetArgumentValue(v8::Local<v8::Value> v, int32_t& n, bool bStrict = false)
{
double num;
result_t hr = GetArgumentValue(v, num, bStrict);
if (hr < 0)
return hr;
if (num < -2147483648ll || num > 2147483647ll)
return CALL_E_OUTRANGE;
n = (int32_t)num;
return 0;
}
inline result_t GetArgumentValue(v8::Isolate* isolate, v8::Local<v8::Value> v, int32_t& n, bool bStrict = false)
{
return GetArgumentValue(v, n, bStrict);
}
inline result_t GetArgumentValue(v8::Local<v8::Value> v, bool& n, bool bStrict = false)
{
if (v.IsEmpty())
return CALL_E_TYPEMISMATCH;
if (bStrict && !v->IsBoolean() && !v->IsBooleanObject())
return CALL_E_TYPEMISMATCH;
n = v->BooleanValue();
return 0;
}
inline result_t GetArgumentValue(v8::Isolate* isolate, v8::Local<v8::Value> v, bool& n, bool bStrict = false)
{
return GetArgumentValue(v, n, bStrict);
}
inline result_t GetArgumentValue(v8::Local<v8::Value> v, date_t& d, bool bStrict = false)
{
if (v.IsEmpty())
return CALL_E_TYPEMISMATCH;
if (!v->IsDate())
return CALL_E_TYPEMISMATCH;
d = v;
return 0;
}
inline result_t GetArgumentValue(v8::Isolate* isolate, v8::Local<v8::Value> v, date_t& d, bool bStrict = false)
{
return GetArgumentValue(v, d, bStrict);
}
inline result_t GetArgumentValue(v8::Isolate* isolate, v8::Local<v8::Value> v, Variant& d, bool bStrict = false)
{
d = v;
return 0;
}
class Value2Args {
public:
Value2Args(v8::Isolate* isolate, v8::Local<v8::Value>& v, v8::Local<v8::Value>& vr)
: m_isolate(isolate)
, m_v(v)
, m_vr(vr)
{
}
int32_t Length() const
{
return 1;
}
bool IsConstructCall() const
{
return true;
}
v8::Local<v8::Object> This() const
{
return v8::Local<v8::Object>();
}
const Value2Args& GetReturnValue() const
{
return *this;
}
void Set(v8::Local<v8::Value> vr) const
{
m_vr = vr;
}
v8::Local<v8::Value>& operator[](size_t i) const
{
return m_v;
}
v8::Isolate* GetIsolate() const
{
return m_isolate;
}
private:
v8::Isolate* m_isolate;
v8::Local<v8::Value>& m_v;
v8::Local<v8::Value>& m_vr;
};
template <class T>
result_t GetArgumentValue(v8::Isolate* isolate, v8::Local<v8::Value> v, obj_ptr<T>& vr, bool bStrict = false)
{
vr = T::getInstance(v);
if (vr == NULL) {
if (bStrict)
return CALL_E_TYPEMISMATCH;
TryCatch try_catch;
v8::Local<v8::Value> vr1;
Value2Args a(isolate, v, vr1);
T::__new(a);
vr = T::getInstance(vr1);
if (vr == NULL)
return CALL_E_TYPEMISMATCH;
vr->dispose();
}
return 0;
}
inline result_t GetArgumentValue(v8::Local<v8::Value> v, v8::Local<v8::Object>& vr, bool bStrict = false)
{
Isolate* isolate = Isolate::current();
if (v.IsEmpty())
return CALL_E_TYPEMISMATCH;
if (!v->IsObject())
return CALL_E_TYPEMISMATCH;
v8::Local<v8::Value> proto;
if (isolate->m_proto.IsEmpty()) {
proto = v8::Object::New(isolate->m_isolate)->GetPrototype();
isolate->m_proto.Reset(isolate->m_isolate, proto);
} else
proto = v8::Local<v8::Value>::New(isolate->m_isolate, isolate->m_proto);
v8::Local<v8::Object> o = v8::Local<v8::Object>::Cast(v);
if (!proto->Equals(o->GetPrototype()))
return CALL_E_TYPEMISMATCH;
vr = o;
return 0;
}
inline result_t GetArgumentValue(v8::Isolate* isolate, v8::Local<v8::Value> v,
v8::Local<v8::Object>& vr, bool bStrict = false)
{
return GetArgumentValue(v, vr, bStrict);
}
inline result_t GetArgumentValue(v8::Local<v8::Value> v, v8::Local<v8::Array>& vr, bool bStrict = false)
{
if (v.IsEmpty())
return CALL_E_TYPEMISMATCH;
if (!v->IsArray())
return CALL_E_TYPEMISMATCH;
vr = v8::Local<v8::Array>::Cast(v);
return 0;
}
inline result_t GetArgumentValue(v8::Isolate* isolate, v8::Local<v8::Value> v, v8::Local<v8::Array>& vr, bool bStrict = false)
{
return GetArgumentValue(v, vr, bStrict);
}
inline result_t GetArgumentValue(v8::Local<v8::Value> v, v8::Local<v8::TypedArray>& vr, bool bStrict = false)
{
if (v.IsEmpty())
return CALL_E_TYPEMISMATCH;
if (!v->IsTypedArray())
return CALL_E_TYPEMISMATCH;
vr = v8::Local<v8::TypedArray>::Cast(v);
return 0;
}
inline result_t GetArgumentValue(v8::Isolate* isolate, v8::Local<v8::Value> v, v8::Local<v8::TypedArray>& vr, bool bStrict = false)
{
return GetArgumentValue(v, vr, bStrict);
}
inline result_t GetArgumentValue(v8::Local<v8::Value> v, v8::Local<v8::ArrayBuffer>& vr, bool bStrict = false)
{
if (v.IsEmpty())
return CALL_E_TYPEMISMATCH;
if (!v->IsArrayBuffer())
return CALL_E_TYPEMISMATCH;
vr = v8::Local<v8::ArrayBuffer>::Cast(v);
return 0;
}
inline result_t GetArgumentValue(v8::Isolate* isolate, v8::Local<v8::Value> v,
v8::Local<v8::ArrayBuffer>& vr, bool bStrict = false)
{
return GetArgumentValue(v, vr, bStrict);
}
inline result_t GetArgumentValue(v8::Local<v8::Value> v, v8::Local<v8::Value>& vr, bool bStrict = false)
{
vr = v;
return 0;
}
inline result_t GetArgumentValue(v8::Isolate* isolate, v8::Local<v8::Value> v,
v8::Local<v8::Value>& vr, bool bStrict = false)
{
return GetArgumentValue(v, vr, bStrict);
}
inline result_t GetArgumentValue(v8::Local<v8::Value> v, v8::Local<v8::Function>& vr, bool bStrict = false)
{
if (v.IsEmpty())
return CALL_E_TYPEMISMATCH;
if (!v->IsFunction())
return CALL_E_TYPEMISMATCH;
vr = v8::Local<v8::Function>::Cast(v);
return 0;
}
inline result_t GetArgumentValue(v8::Isolate* isolate, v8::Local<v8::Value> v,
v8::Local<v8::Function>& vr, bool bStrict = false)
{
return GetArgumentValue(v, vr, bStrict);
}
template <typename T>
result_t GetConfigValue(v8::Isolate* isolate, v8::Local<v8::Object> o,
const char* key, T& n, bool bStrict = false)
{
v8::Local<v8::Value> v = o->Get(v8::String::NewFromUtf8(isolate, key));
if (IsEmpty(v))
return CALL_E_PARAMNOTOPTIONAL;
return GetArgumentValue(isolate, v, n, bStrict);
}
inline v8::Local<v8::Value> GetReturnValue(v8::Isolate* isolate, int32_t v)
{
return v8::Int32::New(isolate, v);
}
inline v8::Local<v8::Value> GetReturnValue(v8::Isolate* isolate, bool v)
{
return v ? v8::True(isolate) : v8::False(isolate);
}
inline v8::Local<v8::Value> GetReturnValue(v8::Isolate* isolate, double v)
{
return v8::Number::New(isolate, v);
}
inline v8::Local<v8::Value> GetReturnValue(v8::Isolate* isolate, int64_t v)
{
return v8::Number::New(isolate, (double)v);
}
inline v8::Local<v8::Value> GetReturnValue(v8::Isolate* isolate, exlib::string& str)
{
return v8::String::NewFromUtf8(isolate, str.c_str(),
v8::String::kNormalString, (int32_t)str.length());
}
inline v8::Local<v8::Value> GetReturnValue(v8::Isolate* isolate, date_t& v)
{
return v.value(isolate);
}
inline v8::Local<v8::Value> GetReturnValue(v8::Isolate* isolate, Variant& v)
{
return v;
}
inline v8::Local<v8::Value> GetReturnValue(v8::Isolate* isolate, v8::Local<v8::Object>& obj)
{
return obj;
}
inline v8::Local<v8::Value> GetReturnValue(v8::Isolate* isolate, v8::Local<v8::Array>& array)
{
return array;
}
inline v8::Local<v8::Value> GetReturnValue(v8::Isolate* isolate, v8::Local<v8::Value>& value)
{
return value;
}
inline v8::Local<v8::Value> GetReturnValue(v8::Isolate* isolate, v8::Local<v8::Function>& func)
{
return func;
}
template <class T>
inline v8::Local<v8::Value> GetReturnValue(v8::Isolate* isolate, obj_ptr<T>& obj)
{
return obj->wrap();
}
inline v8::Local<v8::Value> ThrowError(const char* msg)
{
Isolate* isolate = Isolate::current();
return isolate->m_isolate->ThrowException(v8::Exception::Error(
isolate->NewFromUtf8(msg)));
}
inline v8::Local<v8::Value> ThrowTypeError(const char* msg)
{
Isolate* isolate = Isolate::current();
return isolate->m_isolate->ThrowException(v8::Exception::TypeError(
isolate->NewFromUtf8(msg)));
}
inline v8::Local<v8::Value> ThrowRangeError(const char* msg)
{
Isolate* isolate = Isolate::current();
return isolate->m_isolate->ThrowException(v8::Exception::RangeError(
isolate->NewFromUtf8(msg)));
}
inline v8::Local<v8::Value> ThrowError(exlib::string msg)
{
Isolate* isolate = Isolate::current();
return isolate->m_isolate->ThrowException(v8::Exception::Error(
isolate->NewFromUtf8(msg)));
}
inline v8::Local<v8::Value> ThrowTypeError(exlib::string msg)
{
Isolate* isolate = Isolate::current();
return isolate->m_isolate->ThrowException(v8::Exception::TypeError(
isolate->NewFromUtf8(msg)));
}
inline v8::Local<v8::Value> ThrowRangeError(exlib::string msg)
{
Isolate* isolate = Isolate::current();
return isolate->m_isolate->ThrowException(v8::Exception::RangeError(
isolate->NewFromUtf8(msg)));
}
inline result_t LastError()
{
#ifdef _WIN32
return -(int32_t)GetLastError();
#else
return -errno;
#endif
}
inline result_t SocketError()
{
#ifdef _WIN32
return -WSAGetLastError();
#else
return -errno;
#endif
}
exlib::string traceInfo(int32_t deep);
exlib::string getResultMessage(result_t hr);
v8::Local<v8::Value> ThrowResult(result_t hr);
void ReportException(TryCatch& try_catch, result_t hr);
exlib::string GetException(TryCatch& try_catch, result_t hr);
result_t throwSyntaxError(TryCatch& try_catch);
#ifdef _WIN32
#define PATH_SLASH '\\'
#define PATH_DELIMITER ';'
inline bool isPathSlash(char ch)
{