forked from rsenn/qjs-modules
-
Notifications
You must be signed in to change notification settings - Fork 0
/
quickjs-syscallerror.c
1174 lines (1090 loc) · 25.3 KB
/
quickjs-syscallerror.c
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
#include "defines.h"
#include "quickjs-syscallerror.h"
#include "buffer-utils.h"
#include "char-utils.h"
#include <errno.h>
#include "debug.h"
#if defined(_WIN32) && !defined(__MSYS__) && !defined(__CYGWIN__)
#include <windows.h>
#endif
/**
* \addtogroup quickjs-syscallerror
* @{
*/
VISIBLE JSClassID js_syscallerror_class_id = 0;
VISIBLE JSValue syscallerror_proto = {{0}, JS_TAG_UNDEFINED}, syscallerror_ctor = {{0}, JS_TAG_UNDEFINED};
int js_syscallerror_init(JSContext*, JSModuleDef*);
static const char* error_get(int number);
static char*
stack_get(JSContext* ctx) {
const char* stack;
char* ret;
JSValue st, error = js_global_new(ctx, "Error", 0, 0);
st = JS_GetPropertyStr(ctx, error, "stack");
stack = JS_ToCString(ctx, st);
size_t pos = str_chr(stack, '\n');
if(stack[pos])
pos++;
ret = js_strdup(ctx, stack + pos);
JS_FreeCString(ctx, stack);
JS_FreeValue(ctx, error);
return ret;
}
SyscallError*
js_syscallerror_data(JSValueConst value) {
return JS_GetOpaque(value, js_syscallerror_class_id);
}
SyscallError*
js_syscallerror_data2(JSContext* ctx, JSValueConst value) {
return JS_GetOpaque2(ctx, value, js_syscallerror_class_id);
}
SyscallError*
syscallerror_new(JSContext* ctx, const char* syscall, int number) {
SyscallError* err;
if(!(err = js_mallocz(ctx, sizeof(SyscallError))))
return 0;
err->syscall = syscall ? js_strdup(ctx, syscall) : 0;
err->number = number;
err->stack = stack_get(ctx);
return err;
}
JSValue
js_syscallerror_wrap(JSContext* ctx, SyscallError* err) {
JSValue obj;
obj = JS_NewObjectProtoClass(ctx, syscallerror_proto, js_syscallerror_class_id);
JS_SetOpaque(obj, err);
return obj;
}
JSValue
js_syscallerror_new(JSContext* ctx, const char* syscall, int number) {
SyscallError* err;
JSValue obj;
if(js_syscallerror_class_id == 0)
js_syscallerror_init(ctx, 0);
if(!(err = syscallerror_new(ctx, syscall, number)))
return JS_EXCEPTION;
obj = JS_NewObjectProtoClass(ctx, syscallerror_proto, js_syscallerror_class_id);
if(JS_IsException(obj))
goto fail;
JS_SetOpaque(obj, err);
return obj;
fail:
js_free(ctx, err);
JS_FreeValue(ctx, obj);
return JS_EXCEPTION;
}
JSValue
js_syscallerror_throw(JSContext* ctx, const char* syscall) {
JSValue error = js_syscallerror_new(ctx, syscall, errno);
return JS_Throw(ctx, error);
}
static JSValue
js_syscallerror_constructor(JSContext* ctx, JSValueConst new_target, int argc, JSValueConst argv[]) {
SyscallError* err;
JSValue obj = JS_UNDEFINED, proto = JS_UNDEFINED, st = JS_UNDEFINED;
if(!(err = js_mallocz(ctx, sizeof(SyscallError))))
return JS_EXCEPTION;
proto = JS_GetPropertyStr(ctx, new_target, "prototype");
if(JS_IsException(proto))
goto fail;
obj = JS_NewObjectProtoClass(ctx, proto, js_syscallerror_class_id);
JS_FreeValue(ctx, proto);
if(JS_IsException(obj))
goto fail;
if(argc >= 2) {
err->syscall = js_tostring(ctx, argv[0]);
argc--;
argv++;
}
if(argc >= 1) {
int32_t number;
// if(JS_IsNumber(argv[0])) {
JS_ToInt32(ctx, &number, argv[0]);
/* } else {
const char* code = JS_ToCString(ctx, argv[0]);
number = error_find(code);
JS_FreeCString(ctx, code);
}*/
err->number = number;
}
err->stack = stack_get(ctx);
JS_FreeValue(ctx, st);
JS_SetOpaque(obj, err);
return obj;
fail:
js_free(ctx, err);
JS_FreeValue(ctx, obj);
return JS_EXCEPTION;
}
static void
syscallerror_dump(SyscallError* err, DynBuf* dbuf) {
if(err->syscall) {
char buf[FMT_LONG];
dbuf_putstr(dbuf, err->syscall);
dbuf_putstr(dbuf, "() = -1 (errno = ");
dbuf_put(dbuf, (const void*)buf, fmt_longlong(buf, err->number));
dbuf_putstr(dbuf, ")");
}
if(err->number) {
if(err->syscall)
dbuf_putstr(dbuf, ": ");
#if defined(_WIN32) && !defined(__MSYS__) && !defined(__CYGWIN__)
dbuf->size += FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
err->number,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
(void*)dbuf_reserve(dbuf, 256),
256,
NULL);
while(dbuf->size > 0) {
if(dbuf->buf[dbuf->size - 1] > 0x20)
break;
--dbuf->size;
}
#else
const char* msg;
if((msg = strerror(err->number)))
dbuf_putstr(dbuf, msg);
#endif
}
if(err->stack) {
dbuf_putc(dbuf, '\n');
dbuf_putstr(dbuf, err->stack);
}
dbuf_0(dbuf);
}
enum {
SYSCALLERROR_TOSTRING,
SYSCALLERROR_VALUEOF,
};
static JSValue
js_syscallerror_method(JSContext* ctx, JSValueConst this_val, int argc, JSValueConst argv[], int magic) {
JSValue ret = JS_UNDEFINED;
SyscallError* err;
if(!(err = js_syscallerror_data2(ctx, this_val)))
return JS_EXCEPTION;
switch(magic) {
case SYSCALLERROR_TOSTRING: {
DynBuf dbuf = {0};
js_dbuf_init(ctx, &dbuf);
syscallerror_dump(err, &dbuf);
ret = JS_NewStringLen(ctx, (const char*)dbuf.buf, dbuf.size);
dbuf_free(&dbuf);
break;
}
case SYSCALLERROR_VALUEOF: {
ret = JS_NewInt32(ctx, err->number);
break;
}
}
return ret;
}
enum {
SYSCALLERROR_NAME,
};
static JSValue
js_syscallerror_functions(JSContext* ctx, JSValueConst this_val, int argc, JSValueConst argv[], int magic) {
JSValue ret = JS_UNDEFINED;
switch(magic) {
case SYSCALLERROR_NAME: {
int32_t err = 0;
JS_ToInt32(ctx, &err, argv[0]);
ret = JS_NewString(ctx, error_get(err));
break;
}
}
return ret;
}
static JSValue
js_syscallerror_inspect(JSContext* ctx, JSValueConst this_val, int argc, JSValueConst argv[]) {
const char* code = 0;
SyscallError* err = js_syscallerror_data(this_val);
/* if(!err)
return JS_DupValue(ctx, this_val);*/
JSValue obj = JS_NewObjectClass(ctx, js_syscallerror_class_id);
JS_DefinePropertyValueStr(ctx, obj, "errno", JS_NewInt32(ctx, err->number), JS_PROP_C_W_E);
JS_DefinePropertyValueStr(ctx, obj, "message", JS_GetPropertyStr(ctx, this_val, "message"), JS_PROP_C_W_E);
if(err->syscall)
JS_DefinePropertyValueStr(ctx, obj, "syscall", JS_NewString(ctx, err->syscall), JS_PROP_C_W_E);
if((code = error_get(err->number)))
JS_DefinePropertyValueStr(ctx, obj, "code", JS_NewString(ctx, code), JS_PROP_C_W_E);
if(js_has_propertystr(ctx, obj, "stack"))
JS_DefinePropertyValueStr(ctx, obj, "stack", JS_GetPropertyStr(ctx, this_val, "stack"), JS_PROP_CONFIGURABLE | JS_PROP_WRITABLE);
return obj;
}
enum {
PROP_SYSCALL,
PROP_CODE,
PROP_ERRNO,
PROP_STACK,
PROP_MESSAGE,
};
static JSValue
js_syscallerror_get(JSContext* ctx, JSValueConst this_val, int magic) {
JSValue ret = JS_UNDEFINED;
SyscallError* err = js_syscallerror_data(this_val);
switch(magic) {
case PROP_SYSCALL: {
if(err)
ret = err->syscall ? JS_NewString(ctx, err->syscall) : JS_NULL;
break;
}
case PROP_CODE: {
const char* code;
if(err)
ret = (code = error_get(err->number)) ? JS_NewString(ctx, code) : JS_NULL;
break;
}
case PROP_ERRNO: {
if(err)
ret = JS_NewInt32(ctx, err->number);
break;
}
case PROP_STACK: {
if(err)
ret = err->stack ? JS_NewString(ctx, err->stack) : JS_NULL;
break;
}
case PROP_MESSAGE: {
DynBuf dbuf = {0};
js_dbuf_init(ctx, &dbuf);
syscallerror_dump(err, &dbuf);
ret = JS_NewStringLen(ctx, (const char*)dbuf.buf, byte_chr(dbuf.buf, dbuf.size, '\n'));
break;
}
}
return ret;
}
/*static JSValue
js_syscallerror_set(JSContext* ctx, JSValueConst this_val, JSValueConst value, int magic) {
JSValue ret = JS_UNDEFINED;
SyscallError* err = js_syscallerror_data(this_val);
//if(!(err = js_syscallerror_data2(ctx, this_val))) return JS_EXCEPTION;
switch(magic) {
case PROP_SYSCALL: {
break;
}
case PROP_CODE: {
break;
}
case PROP_ERRNO: {
break;
}
case PROP_STACK: {
break;
}
case PROP_MESSAGE: {
break;
}
}
return ret;
}*/
const JSCFunctionListEntry js_syscallerror_proto_funcs[] = {
JS_CGETSET_MAGIC_DEF("syscall", js_syscallerror_get, 0, PROP_SYSCALL),
JS_CGETSET_MAGIC_DEF("name", js_syscallerror_get, 0, PROP_CODE),
JS_CGETSET_MAGIC_FLAGS_DEF("errno", js_syscallerror_get, 0, PROP_ERRNO, JS_PROP_C_W_E),
JS_CGETSET_MAGIC_DEF("stack", js_syscallerror_get, 0, PROP_STACK),
JS_CGETSET_MAGIC_FLAGS_DEF("message", js_syscallerror_get, 0, PROP_MESSAGE, JS_PROP_C_W_E),
JS_CFUNC_MAGIC_DEF("toString", 0, js_syscallerror_method, SYSCALLERROR_TOSTRING),
JS_ALIAS_DEF("[Symbol.toPrimitive]", "toString"),
JS_PROP_STRING_DEF("[Symbol.toStringTag]", "SyscallError", JS_PROP_CONFIGURABLE),
};
const JSCFunctionListEntry js_syscallerror_defines[] = {
// JS_CFUNC_MAGIC_DEF("name", 1, js_syscallerror_functions, SYSCALLERROR_NAME),
#ifdef EPERM
JS_CONSTANT(EPERM),
#endif
#ifdef ENOENT
JS_CONSTANT(ENOENT),
#endif
#ifdef ESRCH
JS_CONSTANT(ESRCH),
#endif
#ifdef EINTR
JS_CONSTANT(EINTR),
#endif
#ifdef EIO
JS_CONSTANT(EIO),
#endif
#ifdef ENXIO
JS_CONSTANT(ENXIO),
#endif
#ifdef E2BIG
JS_CONSTANT(E2BIG),
#endif
#ifdef ENOEXEC
JS_CONSTANT(ENOEXEC),
#endif
#ifdef EBADF
JS_CONSTANT(EBADF),
#endif
#ifdef ECHILD
JS_CONSTANT(ECHILD),
#endif
#ifdef EAGAIN
JS_CONSTANT(EAGAIN),
#endif
#ifdef EWOULDBLOCK
JS_CONSTANT(EWOULDBLOCK),
#endif
#ifdef ENOMEM
JS_CONSTANT(ENOMEM),
#endif
#ifdef EACCES
JS_CONSTANT(EACCES),
#endif
#ifdef EFAULT
JS_CONSTANT(EFAULT),
#endif
#ifdef EBUSY
JS_CONSTANT(EBUSY),
#endif
#ifdef EEXIST
JS_CONSTANT(EEXIST),
#endif
#ifdef EXDEV
JS_CONSTANT(EXDEV),
#endif
#ifdef ENODEV
JS_CONSTANT(ENODEV),
#endif
#ifdef ENOTDIR
JS_CONSTANT(ENOTDIR),
#endif
#ifdef EISDIR
JS_CONSTANT(EISDIR),
#endif
#ifdef EINVAL
JS_CONSTANT(EINVAL),
#endif
#ifdef ENFILE
JS_CONSTANT(ENFILE),
#endif
#ifdef EMFILE
JS_CONSTANT(EMFILE),
#endif
#ifdef ENOTTY
JS_CONSTANT(ENOTTY),
#endif
#ifdef ETXTBSY
JS_CONSTANT(ETXTBSY),
#endif
#ifdef EFBIG
JS_CONSTANT(EFBIG),
#endif
#ifdef ENOSPC
JS_CONSTANT(ENOSPC),
#endif
#ifdef ESPIPE
JS_CONSTANT(ESPIPE),
#endif
#ifdef EROFS
JS_CONSTANT(EROFS),
#endif
#ifdef EMLINK
JS_CONSTANT(EMLINK),
#endif
#ifdef EPIPE
JS_CONSTANT(EPIPE),
#endif
#ifdef EDOM
JS_CONSTANT(EDOM),
#endif
#ifdef ERANGE
JS_CONSTANT(ERANGE),
#endif
#ifdef EDEADLK
JS_CONSTANT(EDEADLK),
#endif
#ifdef ENAMETOOLONG
JS_CONSTANT(ENAMETOOLONG),
#endif
#ifdef ENOLCK
JS_CONSTANT(ENOLCK),
#endif
#ifdef ENOSYS
JS_CONSTANT(ENOSYS),
#endif
#ifdef ENOTEMPTY
JS_CONSTANT(ENOTEMPTY),
#endif
#ifdef ENOMSG
JS_CONSTANT(ENOMSG),
#endif
#ifdef EIDRM
JS_CONSTANT(EIDRM),
#endif
#ifdef ENOLINK
JS_CONSTANT(ENOLINK),
#endif
#ifdef EPROTO
JS_CONSTANT(EPROTO),
#endif
#ifdef EBADMSG
JS_CONSTANT(EBADMSG),
#endif
#ifdef EOVERFLOW
JS_CONSTANT(EOVERFLOW),
#endif
#ifdef EILSEQ
JS_CONSTANT(EILSEQ),
#endif
#ifdef ERESTART
JS_CONSTANT(ERESTART),
#endif
#ifdef ENOTSOCK
JS_CONSTANT(ENOTSOCK),
#endif
#ifdef EDESTADDRREQ
JS_CONSTANT(EDESTADDRREQ),
#endif
#ifdef EMSGSIZE
JS_CONSTANT(EMSGSIZE),
#endif
#ifdef EPROTOTYPE
JS_CONSTANT(EPROTOTYPE),
#endif
#ifdef ENOPROTOOPT
JS_CONSTANT(ENOPROTOOPT),
#endif
#ifdef EPROTONOSUPPORT
JS_CONSTANT(EPROTONOSUPPORT),
#endif
#ifdef EOPNOTSUPP
JS_CONSTANT(EOPNOTSUPP),
#endif
#ifdef EAFNOSUPPORT
JS_CONSTANT(EAFNOSUPPORT),
#endif
#ifdef EADDRINUSE
JS_CONSTANT(EADDRINUSE),
#endif
#ifdef EADDRNOTAVAIL
JS_CONSTANT(EADDRNOTAVAIL),
#endif
#ifdef ENETDOWN
JS_CONSTANT(ENETDOWN),
#endif
#ifdef ENETUNREACH
JS_CONSTANT(ENETUNREACH),
#endif
#ifdef ENETRESET
JS_CONSTANT(ENETRESET),
#endif
#ifdef ECONNABORTED
JS_CONSTANT(ECONNABORTED),
#endif
#ifdef ECONNRESET
JS_CONSTANT(ECONNRESET),
#endif
#ifdef ENOBUFS
JS_CONSTANT(ENOBUFS),
#endif
#ifdef EISCONN
JS_CONSTANT(EISCONN),
#endif
#ifdef ENOTCONN
JS_CONSTANT(ENOTCONN),
#endif
#ifdef ETIMEDOUT
JS_CONSTANT(ETIMEDOUT),
#endif
#ifdef ECONNREFUSED
JS_CONSTANT(ECONNREFUSED),
#endif
#ifdef EHOSTUNREACH
JS_CONSTANT(EHOSTUNREACH),
#endif
#ifdef EALREADY
JS_CONSTANT(EALREADY),
#endif
#ifdef EINPROGRESS
JS_CONSTANT(EINPROGRESS),
#endif
#ifdef ECANCELED
JS_CONSTANT(ECANCELED),
#endif
#ifdef EOWNERDEAD
JS_CONSTANT(EOWNERDEAD),
#endif
#ifdef ENOTRECOVERABLE
JS_CONSTANT(ENOTRECOVERABLE),
#endif
#ifdef WSAEINTR
JS_CONSTANT(WSAEINTR),
#endif
#ifdef WSAEBADF
JS_CONSTANT(WSAEBADF),
#endif
#ifdef WSAEACCES
JS_CONSTANT(WSAEACCES),
#endif
#ifdef WSAEFAULT
JS_CONSTANT(WSAEFAULT),
#endif
#ifdef WSAEINVAL
JS_CONSTANT(WSAEINVAL),
#endif
#ifdef WSAEMFILE
JS_CONSTANT(WSAEMFILE),
#endif
#ifdef WSAEWOULDBLOCK
JS_CONSTANT(WSAEWOULDBLOCK),
#endif
#ifdef WSAEINPROGRESS
JS_CONSTANT(WSAEINPROGRESS),
#endif
#ifdef WSAEALREADY
JS_CONSTANT(WSAEALREADY),
#endif
#ifdef WSAENOTSOCK
JS_CONSTANT(WSAENOTSOCK),
#endif
#ifdef WSAEDESTADDRREQ
JS_CONSTANT(WSAEDESTADDRREQ),
#endif
#ifdef WSAEMSGSIZE
JS_CONSTANT(WSAEMSGSIZE),
#endif
#ifdef WSAEPROTOTYPE
JS_CONSTANT(WSAEPROTOTYPE),
#endif
#ifdef WSAENOPROTOOPT
JS_CONSTANT(WSAENOPROTOOPT),
#endif
#ifdef WSAEPROTONOSUPPORT
JS_CONSTANT(WSAEPROTONOSUPPORT),
#endif
#ifdef WSAESOCKTNOSUPPORT
JS_CONSTANT(WSAESOCKTNOSUPPORT),
#endif
#ifdef WSAEOPNOTSUPP
JS_CONSTANT(WSAEOPNOTSUPP),
#endif
#ifdef WSAEPFNOSUPPORT
JS_CONSTANT(WSAEPFNOSUPPORT),
#endif
#ifdef WSAEAFNOSUPPORT
JS_CONSTANT(WSAEAFNOSUPPORT),
#endif
#ifdef WSAEADDRINUSE
JS_CONSTANT(WSAEADDRINUSE),
#endif
#ifdef WSAEADDRNOTAVAIL
JS_CONSTANT(WSAEADDRNOTAVAIL),
#endif
#ifdef WSAENETDOWN
JS_CONSTANT(WSAENETDOWN),
#endif
#ifdef WSAENETUNREACH
JS_CONSTANT(WSAENETUNREACH),
#endif
#ifdef WSAENETRESET
JS_CONSTANT(WSAENETRESET),
#endif
#ifdef WSAECONNABORTED
JS_CONSTANT(WSAECONNABORTED),
#endif
#ifdef WSAECONNRESET
JS_CONSTANT(WSAECONNRESET),
#endif
#ifdef WSAENOBUFS
JS_CONSTANT(WSAENOBUFS),
#endif
#ifdef WSAEISCONN
JS_CONSTANT(WSAEISCONN),
#endif
#ifdef WSAENOTCONN
JS_CONSTANT(WSAENOTCONN),
#endif
#ifdef WSAESHUTDOWN
JS_CONSTANT(WSAESHUTDOWN),
#endif
#ifdef WSAETOOMANYREFS
JS_CONSTANT(WSAETOOMANYREFS),
#endif
#ifdef WSAETIMEDOUT
JS_CONSTANT(WSAETIMEDOUT),
#endif
#ifdef WSAECONNREFUSED
JS_CONSTANT(WSAECONNREFUSED),
#endif
#ifdef WSAELOOP
JS_CONSTANT(WSAELOOP),
#endif
#ifdef WSAENAMETOOLONG
JS_CONSTANT(WSAENAMETOOLONG),
#endif
#ifdef WSAEHOSTDOWN
JS_CONSTANT(WSAEHOSTDOWN),
#endif
#ifdef WSAEHOSTUNREACH
JS_CONSTANT(WSAEHOSTUNREACH),
#endif
#ifdef WSAENOTEMPTY
JS_CONSTANT(WSAENOTEMPTY),
#endif
#ifdef WSAEPROCLIM
JS_CONSTANT(WSAEPROCLIM),
#endif
#ifdef WSAEUSERS
JS_CONSTANT(WSAEUSERS),
#endif
#ifdef WSAEDQUOT
JS_CONSTANT(WSAEDQUOT),
#endif
#ifdef WSAESTALE
JS_CONSTANT(WSAESTALE),
#endif
#ifdef WSAEREMOTE
JS_CONSTANT(WSAEREMOTE),
#endif
#ifdef WSAEDISCON
JS_CONSTANT(WSAEDISCON),
#endif
#ifdef WSAENOMORE
JS_CONSTANT(WSAENOMORE),
#endif
#ifdef WSAECANCELLED
JS_CONSTANT(WSAECANCELLED),
#endif
#ifdef WSAEINVALIDPROCTABLE
JS_CONSTANT(WSAEINVALIDPROCTABLE),
#endif
#ifdef WSAEINVALIDPROVIDER
JS_CONSTANT(WSAEINVALIDPROVIDER),
#endif
#ifdef WSAEPROVIDERFAILEDINIT
JS_CONSTANT(WSAEPROVIDERFAILEDINIT),
#endif
#ifdef WSAEREFUSED
JS_CONSTANT(WSAEREFUSED),
#endif
};
static void
js_syscallerror_finalizer(JSRuntime* rt, JSValue val) {
SyscallError* err;
if((err = JS_GetOpaque(val, js_syscallerror_class_id))) {
if(err->syscall)
js_free_rt(rt, err->syscall);
if(err->stack)
js_free_rt(rt, err->stack);
js_free_rt(rt, err);
}
}
static JSClassDef js_syscallerror_class = {
.class_name = "SyscallError",
.finalizer = js_syscallerror_finalizer,
};
int
js_syscallerror_init(JSContext* ctx, JSModuleDef* m) {
assert(js_syscallerror_class_id == 0);
JSValue error = js_global_prototype(ctx, "Error");
assert(JS_IsObject(error));
JS_NewClassID(&js_syscallerror_class_id);
JS_NewClass(JS_GetRuntime(ctx), js_syscallerror_class_id, &js_syscallerror_class);
syscallerror_ctor = JS_NewCFunction2(ctx, js_syscallerror_constructor, "SyscallError", 1, JS_CFUNC_constructor, 0);
syscallerror_proto = JS_NewObjectProto(ctx, error);
JS_FreeValue(ctx, error);
// syscallerror_proto = JS_NewObject(ctx);
JS_SetPropertyFunctionList(ctx, syscallerror_ctor, js_syscallerror_defines, countof(js_syscallerror_defines));
JS_SetPropertyFunctionList(ctx, syscallerror_proto, js_syscallerror_proto_funcs, countof(js_syscallerror_proto_funcs));
JS_SetClassProto(ctx, js_syscallerror_class_id, syscallerror_proto);
JS_SetConstructor(ctx, syscallerror_ctor, syscallerror_proto);
// js_set_inspect_method(ctx, syscallerror_proto, js_syscallerror_inspect);
if(m) {
JS_SetModuleExport(ctx, m, "SyscallError", syscallerror_ctor);
JS_SetModuleExportList(ctx, m, js_syscallerror_defines, countof(js_syscallerror_defines));
}
return 0;
}
#if defined(JS_SHARED_LIBRARY) && defined(JS_SYSCALLERROR_MODULE)
#define JS_INIT_MODULE js_init_module
#else
#define JS_INIT_MODULE js_init_module_syscallerror
#endif
VISIBLE JSModuleDef*
JS_INIT_MODULE(JSContext* ctx, const char* module_name) {
JSModuleDef* m;
if((m = JS_NewCModule(ctx, module_name, js_syscallerror_init))) {
JS_AddModuleExport(ctx, m, "SyscallError");
JS_AddModuleExportList(ctx, m, js_syscallerror_defines, countof(js_syscallerror_defines));
}
return m;
}
static const char*
error_get(int number) {
switch(number) {
#ifdef EPERM
case EPERM: return "EPERM";
#endif
#ifdef ENOENT
case ENOENT: return "ENOENT";
#endif
#ifdef ESRCH
case ESRCH: return "ESRCH";
#endif
#ifdef EINTR
case EINTR: return "EINTR";
#endif
#ifdef EIO
case EIO: return "EIO";
#endif
#ifdef ENXIO
case ENXIO: return "ENXIO";
#endif
#ifdef E2BIG
case E2BIG: return "E2BIG";
#endif
#ifdef ENOEXEC
case ENOEXEC: return "ENOEXEC";
#endif
#ifdef EBADF
case EBADF: return "EBADF";
#endif
#ifdef ECHILD
case ECHILD: return "ECHILD";
#endif
#ifdef EAGAIN
case EAGAIN: return "EAGAIN";
#endif
#ifdef ENOMEM
case ENOMEM: return "ENOMEM";
#endif
#ifdef EACCES
case EACCES: return "EACCES";
#endif
#ifdef EFAULT
case EFAULT: return "EFAULT";
#endif
#ifdef EBUSY
case EBUSY: return "EBUSY";
#endif
#ifdef EEXIST
case EEXIST: return "EEXIST";
#endif
#ifdef EXDEV
case EXDEV: return "EXDEV";
#endif
#ifdef ENODEV
case ENODEV: return "ENODEV";
#endif
#ifdef ENOTDIR
case ENOTDIR: return "ENOTDIR";
#endif
#ifdef EISDIR
case EISDIR: return "EISDIR";
#endif
#ifdef EINVAL
case EINVAL: return "EINVAL";
#endif
#ifdef ENFILE
case ENFILE: return "ENFILE";
#endif
#ifdef EMFILE
case EMFILE: return "EMFILE";
#endif
#ifdef ENOTTY
case ENOTTY: return "ENOTTY";
#endif
#ifdef ETXTBSY
case ETXTBSY: return "ETXTBSY";
#endif
#ifdef EFBIG
case EFBIG: return "EFBIG";
#endif
#ifdef ENOSPC
case ENOSPC: return "ENOSPC";
#endif
#ifdef ESPIPE
case ESPIPE: return "ESPIPE";
#endif
#ifdef EROFS
case EROFS: return "EROFS";
#endif
#ifdef EMLINK
case EMLINK: return "EMLINK";
#endif
#ifdef EPIPE
case EPIPE: return "EPIPE";
#endif
#ifdef EDOM
case EDOM: return "EDOM";
#endif
#ifdef ERANGE
case ERANGE: return "ERANGE";
#endif
#ifdef EDEADLK
case EDEADLK: return "EDEADLK";
#endif
#ifdef ENAMETOOLONG
case ENAMETOOLONG: return "ENAMETOOLONG";
#endif
#ifdef ENOLCK
case ENOLCK: return "ENOLCK";
#endif
#ifdef ENOSYS
case ENOSYS: return "ENOSYS";
#endif
#ifdef ENOTEMPTY
case ENOTEMPTY: return "ENOTEMPTY";
#endif
#ifdef ENOMSG
case ENOMSG: return "ENOMSG";
#endif
#ifdef EIDRM
case EIDRM: return "EIDRM";
#endif
#ifdef ENOLINK
case ENOLINK: return "ENOLINK";
#endif
#ifdef EPROTO
case EPROTO: return "EPROTO";
#endif
#ifdef EBADMSG
case EBADMSG: return "EBADMSG";
#endif
#ifdef EOVERFLOW
case EOVERFLOW: return "EOVERFLOW";
#endif
#ifdef EILSEQ
case EILSEQ: return "EILSEQ";
#endif
#ifdef ERESTART
case ERESTART: return "ERESTART";
#endif
#ifdef ENOTSOCK
case ENOTSOCK: return "ENOTSOCK";
#endif
#ifdef EDESTADDRREQ
case EDESTADDRREQ: return "EDESTADDRREQ";
#endif
#ifdef EMSGSIZE
case EMSGSIZE: return "EMSGSIZE";
#endif
#ifdef EPROTOTYPE
case EPROTOTYPE: return "EPROTOTYPE";
#endif
#ifdef ENOPROTOOPT
case ENOPROTOOPT: return "ENOPROTOOPT";
#endif
#ifdef EPROTONOSUPPORT
case EPROTONOSUPPORT: return "EPROTONOSUPPORT";
#endif
#ifdef EOPNOTSUPP
case EOPNOTSUPP: return "EOPNOTSUPP";
#endif
#ifdef EAFNOSUPPORT
case EAFNOSUPPORT: return "EAFNOSUPPORT";
#endif
#ifdef EADDRINUSE
case EADDRINUSE: return "EADDRINUSE";
#endif
#ifdef EADDRNOTAVAIL
case EADDRNOTAVAIL: return "EADDRNOTAVAIL";
#endif
#ifdef ENETDOWN
case ENETDOWN: return "ENETDOWN";
#endif
#ifdef ENETUNREACH
case ENETUNREACH: return "ENETUNREACH";
#endif
#ifdef ENETRESET
case ENETRESET: return "ENETRESET";
#endif
#ifdef ECONNABORTED
case ECONNABORTED: return "ECONNABORTED";
#endif
#ifdef ECONNRESET
case ECONNRESET: return "ECONNRESET";
#endif
#ifdef ENOBUFS
case ENOBUFS: return "ENOBUFS";
#endif
#ifdef EISCONN
case EISCONN: return "EISCONN";
#endif
#ifdef ENOTCONN
case ENOTCONN: return "ENOTCONN";
#endif
#ifdef ETIMEDOUT
case ETIMEDOUT: return "ETIMEDOUT";
#endif
#ifdef ECONNREFUSED
case ECONNREFUSED: return "ECONNREFUSED";
#endif
#ifdef EHOSTUNREACH
case EHOSTUNREACH: return "EHOSTUNREACH";