-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathquickjs-api.c
More file actions
4330 lines (3360 loc) · 129 KB
/
quickjs-api.c
File metadata and controls
4330 lines (3360 loc) · 129 KB
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 <assert.h>
#include <sys/queue.h>
#include <limits.h>
#include <quickjs.h>
#include "js_native_api.h"
#include "quicks-runtime.h"
#include "libbf.h"
#ifdef __ANDROID__
#include <android/log.h>
#endif
#define USE_MIMALLOC
#ifdef USE_MIMALLOC
#include "mimalloc.h"
#else
#include <stdlib.h>
#define mi_malloc(size) \
malloc(size)
#define mi_calloc(count, size) \
calloc(count,size)
#define mi_free(ptr) \
free(ptr)
#define mi_realloc(ptr, size) \
realloc(ptr, size)
#endif
#ifdef USE_MIMALLOC
static void *js_mi_calloc(void *opaque, size_t count, size_t size) {
return mi_calloc(count, size);
}
static void *js_mi_malloc(void *opaque, size_t size) {
return mi_malloc(size);
}
static void js_mi_free(void *opaque, void *ptr) {
if (!ptr)
return;
mi_free(ptr);
}
static void *js_mi_realloc(void *opaque, void *ptr, size_t size) {
return mi_realloc(ptr, size);
}
static const JSMallocFunctions mi_mf = {
js_mi_calloc,
js_mi_malloc,
js_mi_free,
js_mi_realloc,
mi_malloc_usable_size
};
#endif
#define ToJS(value) \
*((JSValue*)value)
/**
* --------------------------------------
* SAFE LIST TRANSVERSAL MACROS
* --------------------------------------
*/
#ifndef SLIST_FOREACH_SAFE
#define SLIST_FOREACH_SAFE(var, head, field, tvar) \
for ((var) = SLIST_FIRST((head)); (var) && ((tvar) = SLIST_NEXT((var), field), 1); (var) = (tvar))
#endif
#ifndef LIST_FOREACH_SAFE
#define LIST_FOREACH_SAFE(var, head, field, tvar) \
for ((var) = LIST_FIRST((head)); (var) && ((tvar) = LIST_NEXT((var), field), 1); (var) = (tvar))
#endif
/**
* --------------------------------------
* NAPI UNDEFINED AND NULL
* --------------------------------------
*/
static JSValueConst JSUndefined = JS_UNDEFINED;
static JSValueConst JSNull = JS_NULL;
/**
* --------------------------------------
* NAPI DATA STRUCTURES
* --------------------------------------
*/
typedef enum {
HANDLE_STACK_ALLOCATED,
HANDLE_HEAP_ALLOCATED
} HandleType;
typedef struct Handle {
JSValue value; // size_t * 2
SLIST_ENTRY(Handle)
node; // size_t
HandleType type;
} Handle;
typedef struct napi_handle_scope__ {
LIST_ENTRY(napi_handle_scope__)
node; // size_t
SLIST_HEAD(, Handle)
handleList; // size_t
bool escapeCalled;
Handle stackHandles[8];
int handleCount;
HandleType type;
} napi_handle_scope__;
typedef struct napi_ref__ {
JSValue value; // size_t * 2
LIST_ENTRY(napi_ref__)
node; // size_t * 2
uint8_t referenceCount; // 8
} napi_ref__;
typedef struct napi_deferred__ {
napi_value resolve;
napi_value reject;
} napi_deferred__;
typedef struct ExternalInfo {
void *data; // size_t
void *finalizeHint; // size_t
napi_finalize finalizeCallback; // size_t
} ExternalInfo;
typedef struct NapiHostObjectInfo {
void *data;
napi_ref ref;
napi_finalize finalize_cb;
bool is_array;
napi_ref getter;
napi_ref setter;
} NapiHostObjectInfo;
typedef struct JsAtoms {
JSAtom napi_external;
JSAtom registerFinalizer;
JSAtom constructor;
JSAtom prototype;
JSAtom napi_buffer;
JSAtom NAPISymbolFor;
JSAtom object;
JSAtom freeze;
JSAtom seal;
JSAtom Symbol;
JSAtom length;
JSAtom is;
JSAtom byteLength;
JSAtom buffer;
JSAtom byteOffset;
JSAtom name;
JSAtom napi_typetag;
JSAtom weakref;
} JsAtoms;
typedef struct napi_env__ {
JSValue referenceSymbolValue; // size_t * 2
napi_runtime runtime; // size_t
JSContext *context; // size_t
LIST_HEAD(, napi_handle_scope__)
handleScopeList; // size_t
LIST_HEAD(, napi_ref__)
referencesList; // size_t
bool isThrowNull;
ExternalInfo *instanceData;
JSValue finalizationRegistry;
napi_extended_error_info last_error;
JsAtoms atoms;
ExternalInfo *gcBefore;
ExternalInfo *gcAfter;
int js_enter_state;
int64_t usedMemory;
} napi_env__;
typedef struct napi_runtime__ {
JSRuntime *runtime; // size_t
JSClassID constructorClassId; // uint32_t
JSClassID functionClassId; // uint32_t
JSClassID externalClassId; // uint32_t
JSClassID napiHostObjectClassId; // uint32_t
JSClassID napiObjectClassId; // uint32_t
} napi_runtime__;
typedef struct napi_callback_info__ {
JSValueConst newTarget; // size_t * 2
JSValueConst thisArg; // size_t * 2
JSValueConst *argv; // size_t * 2
void *data; // size_t
int argc;
} napi_callback_info__;
typedef struct FunctionInfo {
void *data; // size_t
napi_callback callback; // size_t
} FunctionInfo;
typedef struct ExternalBufferInfo {
void *hint;
napi_finalize finalize_cb;
} ExternalBufferInfo;
/**
* -------------------------------------
* MICROTASK HANDLING
* -------------------------------------
*/
static inline void js_enter(napi_env env) {
env->js_enter_state++;
}
static inline void js_exit(napi_env env) {
if (--env->js_enter_state <= 0) {
qjs_execute_pending_jobs(env);
}
}
/**
* --------------------------------------
* NAPI DATA FINALIZERS
* --------------------------------------
*/
static void function_finalizer(JSRuntime *rt, JSValue val) {
napi_env env = (napi_env) JS_GetRuntimeOpaque(rt);
FunctionInfo *functionInfo = (FunctionInfo *) JS_GetOpaque(val, env->runtime->functionClassId);
mi_free(functionInfo);
}
static void external_finalizer(JSRuntime *rt, JSValue val) {
napi_env env = (napi_env) JS_GetRuntimeOpaque(rt);
ExternalInfo *externalInfo = JS_GetOpaque(val, env->runtime->externalClassId);
if (externalInfo->finalizeCallback) {
externalInfo->finalizeCallback(env, externalInfo->data, externalInfo->finalizeHint);
}
mi_free(externalInfo);
}
static void buffer_finalizer(JSRuntime *rt, void *opaque, void *data) {
napi_env env = (napi_env) JS_GetRuntimeOpaque(rt);
ExternalBufferInfo *external_buffer_info = opaque;
if (external_buffer_info->finalize_cb) {
external_buffer_info->finalize_cb(env, data, external_buffer_info->hint);
}
mi_free(external_buffer_info);
}
/**
* -------------------------------------
* NAPI ERROR MANAGEMENT
* -------------------------------------
*/
static inline napi_status napi_set_last_error(napi_env env,
napi_status error_code,
const char *error_message,
uint32_t engine_error_code,
void *engine_reserved) {
if (error_code == napi_ok && env->last_error.error_code == napi_ok)
return napi_ok;
env->last_error.error_code = error_code;
env->last_error.engine_error_code = engine_error_code || 0;
env->last_error.engine_reserved = engine_reserved;
env->last_error.error_message = error_message;
return error_code;
}
static inline napi_status napi_clear_last_error(napi_env env) {
if (env->last_error.error_code == napi_ok)
return napi_ok;
return napi_set_last_error(env, napi_ok, NULL, 0, NULL);
}
/**
* --------------------------------------
* NAPI MACROS
* --------------------------------------
*/
#define TRUTHY(expr) \
__builtin_expect(expr, false)
#define RETURN_STATUS_IF_FALSE(condition, status) \
if (__builtin_expect(!(condition), false)) \
{ \
return napi_set_last_error(env, status, NULL, 0, NULL); \
}
#define CHECK_NAPI(expr) \
{ \
napi_status status = expr; \
if (__builtin_expect(status != napi_ok, false)) \
{ \
return napi_set_last_error(env, status, NULL, 0, NULL); \
} \
}
#define CHECK_ARG(arg) \
if (__builtin_expect(!(arg), false)) \
{ \
return napi_set_last_error(env, napi_invalid_arg, NULL, 0, NULL); \
}
#define NAPI_PREAMBLE(env) \
{ \
CHECK_ARG(env) \
JSValue exceptionValue = JS_GetException((env)->context); \
if (__builtin_expect(JS_IsException(exceptionValue), false)) { \
print_exception(env, exceptionValue); \
return napi_set_last_error(env, napi_pending_exception, NULL, 0, NULL); \
} \
RETURN_STATUS_IF_FALSE(!(env)->isThrowNull, napi_pending_exception) \
}
#ifndef NDEBUG
static const char *const FUNCTION_CLASS_ID_ZERO = "functionClassId must not be 0.";
#endif
/**
* --------------------------------------
* NAPI HANDLE SCOPES
* --------------------------------------
*/
static inline napi_status CreateJSValueHandle(napi_env env, JSValue value, struct Handle **result) {
CHECK_ARG(env)
CHECK_ARG(result)
RETURN_STATUS_IF_FALSE(!LIST_EMPTY(&env->handleScopeList), napi_handle_scope_empty)
napi_handle_scope handleScope = LIST_FIRST(&env->handleScopeList);
if (handleScope->handleCount < 8) {
Handle *handle = &handleScope->stackHandles[handleScope->handleCount];
handle->type = HANDLE_STACK_ALLOCATED;
*result = handle;
handleScope->handleCount++;
} else {
*result = (Handle *) mi_malloc(sizeof(Handle));
(*result)->type = HANDLE_HEAP_ALLOCATED;
}
(*result)->value = value;
SLIST_INSERT_HEAD(&handleScope->handleList, *result, node);
return napi_clear_last_error(env);
}
static inline napi_status CreateScopedResult(napi_env env, JSValue value, napi_value *result) {
struct Handle *jsValueHandle;
napi_status status = CreateJSValueHandle(env, value, &jsValueHandle);
if (status != napi_ok) {
JS_FreeValue(env->context, value);
value = JSUndefined;
return napi_set_last_error(env, status, NULL, 0, NULL);
}
*result = (napi_value) &jsValueHandle->value;
return napi_clear_last_error(env);
}
#define NAPI_OPEN_HANDLE_SCOPE \
napi_handle_scope__ handleScope; \
handleScope.type = HANDLE_STACK_ALLOCATED; \
handleScope.handleCount = 0;\
handleScope.escapeCalled = false; \
SLIST_INIT(&handleScope.handleList); \
LIST_INSERT_HEAD(&env->handleScopeList, &handleScope, node);
#define NAPI_CLOSE_HANDLE_SCOPE \
Handle *handle, *tempHandle; \
SLIST_FOREACH_SAFE(handle, &handleScope.handleList, node, tempHandle) { \
JS_FreeValue(env->context, handle->value); \
handle->value = JSUndefined; \
SLIST_REMOVE(&handleScope.handleList, handle, Handle, node); \
mi_free(handle); \
} \
LIST_REMOVE(&handleScope, node);
napi_status napi_open_handle_scope(napi_env env, napi_handle_scope *result) {
CHECK_ARG(env)
CHECK_ARG(result)
napi_handle_scope__ *handleScope = (napi_handle_scope__ *) mi_malloc(
sizeof(napi_handle_scope__));
RETURN_STATUS_IF_FALSE(handleScope, napi_memory_error)
handleScope->type = HANDLE_HEAP_ALLOCATED;
handleScope->handleCount = 0;
handleScope->escapeCalled = false;
*result = handleScope;
SLIST_INIT(&(*result)->handleList);
LIST_INSERT_HEAD(&env->handleScopeList, *result, node);
return napi_clear_last_error(env);
}
napi_status napi_close_handle_scope(napi_env env, napi_handle_scope scope) {
CHECK_ARG(env)
CHECK_ARG(scope)
assert(LIST_FIRST(&env->handleScopeList) == scope &&
"napi_close_handle_scope() or napi_close_escapable_handle_scope() should follow FILO rule.");
Handle *handle, *tempHandle;
SLIST_FOREACH_SAFE(handle, &scope->handleList, node, tempHandle) {
JS_FreeValue(env->context, handle->value);
handle->value = JSUndefined;
// Instead of freeing, return the handle to the pool for reuse
SLIST_REMOVE(&scope->handleList, handle, Handle, node);
if (handle->type == HANDLE_HEAP_ALLOCATED) {
mi_free(handle);
}
}
LIST_REMOVE(scope, node);
mi_free(scope);
return napi_clear_last_error(env);
}
napi_status napi_open_escapable_handle_scope(napi_env env, napi_escapable_handle_scope *result) {
CHECK_ARG(env)
CHECK_ARG(result)
napi_handle_scope__ *handleScope = (napi_handle_scope__ *) mi_malloc(
sizeof(napi_handle_scope__));
handleScope->type = HANDLE_HEAP_ALLOCATED;
handleScope->handleCount = 0;
RETURN_STATUS_IF_FALSE(handleScope, napi_memory_error)
SLIST_INIT(&handleScope->handleList);
handleScope->escapeCalled = false;
LIST_INSERT_HEAD(&env->handleScopeList, handleScope, node);
*result = handleScope;
return napi_clear_last_error(env);
}
napi_status
napi_close_escapable_handle_scope(napi_env env, napi_escapable_handle_scope escapableScope) {
CHECK_ARG(env)
CHECK_ARG(escapableScope)
assert(LIST_FIRST(&env->handleScopeList) == escapableScope &&
"napi_close_handle_scope() or napi_close_escapable_handle_scope() should follow FILO rule.");
Handle *handle, *tempHandle;
SLIST_FOREACH_SAFE(handle, &escapableScope->handleList, node, tempHandle) {
JS_FreeValue(env->context, handle->value);
handle->value = JSUndefined;
// Instead of freeing, return the handle to the pool for reuse
SLIST_REMOVE(&escapableScope->handleList, handle, Handle, node);
if (handle->type == HANDLE_HEAP_ALLOCATED) {
mi_free(handle);
}
}
LIST_REMOVE(escapableScope, node);
mi_free(escapableScope);
return napi_clear_last_error(env);
}
napi_status napi_escape_handle(napi_env env, napi_escapable_handle_scope scope, napi_value escapee,
napi_value *result) {
CHECK_ARG(env)
CHECK_ARG(scope)
CHECK_ARG(escapee)
RETURN_STATUS_IF_FALSE(!scope->escapeCalled, napi_escape_called_twice)
// Get the outer handle scope
napi_handle_scope handleScope = LIST_NEXT(scope, node);
RETURN_STATUS_IF_FALSE(handleScope, napi_handle_scope_empty)
Handle *handle = (Handle *) mi_malloc(sizeof(Handle));
RETURN_STATUS_IF_FALSE(handle, napi_memory_error)
scope->escapeCalled = true;
handle->value = JS_DupValue(env->context, ToJS(escapee));
SLIST_INSERT_HEAD(&handleScope->handleList, handle, node);
if (result != NULL) {
*result = (napi_value) &handle->value;
}
return napi_clear_last_error(env);
}
/**
* --------------------------------------
* EXCEPTIONS
* --------------------------------------
*/
napi_status napi_throw(napi_env env, napi_value error) {
CHECK_ARG(env)
CHECK_ARG(error)
JS_Throw(env->context, JS_IsNull((*(JSValue *) error)) ? JSNull : JS_DupValue(env->context,
ToJS(error)));
return napi_clear_last_error(env);
}
napi_status napi_throw_error(napi_env env, const char *code, const char *msg) {
CHECK_ARG(env)
CHECK_ARG(msg)
if (JS_HasException(env->context)) {
return napi_pending_exception;
}
JSValue error = JS_NewError(env->context);
JS_SetPropertyStr(env->context, error, "message", JS_NewString(env->context, msg));
if (code != NULL) {
JS_SetPropertyStr(env->context, error, "code", JS_NewString(env->context, code));
}
JS_Throw(env->context, error);
return napi_clear_last_error(env);
}
napi_status napi_throw_range_error(napi_env env, const char *code, const char *msg) {
CHECK_ARG(env)
CHECK_ARG(msg)
if (JS_HasException(env->context)) {
return napi_pending_exception;
}
JSValue error = JS_NewError(env->context);
JS_SetPropertyStr(env->context, error, "message", JS_NewString(env->context, msg));
if (code != NULL) {
JS_SetPropertyStr(env->context, error, "code", JS_NewString(env->context, code));
}
JS_SetPropertyStr(env->context, error, "name", JS_NewString(env->context, "RangeError"));
JS_Throw(env->context, error);
return napi_clear_last_error(env);
}
napi_status napi_throw_type_error(napi_env env, const char *code, const char *msg) {
CHECK_ARG(env)
CHECK_ARG(msg)
if (JS_HasException(env->context)) {
return napi_pending_exception;
}
JSValue error = JS_NewError(env->context);
JS_SetPropertyStr(env->context, error, "message", JS_NewString(env->context, msg));
if (code != NULL) {
JS_SetPropertyStr(env->context, error, "code", JS_NewString(env->context, code));
}
JS_SetPropertyStr(env->context, error, "name", JS_NewString(env->context, "TypeError"));
JS_Throw(env->context, error);
return napi_clear_last_error(env);
}
napi_status napi_is_exception_pending(napi_env env, bool *result) {
CHECK_ARG(env)
CHECK_ARG(result)
*result = JS_HasException(env->context);
return napi_clear_last_error(env);
}
napi_status napi_get_and_clear_last_exception(napi_env env, napi_value *result) {
CHECK_ARG(env)
CHECK_ARG(result)
CHECK_ARG(env->context)
JSValue exceptionValue = JS_GetException(env->context);
if (JS_IsUninitialized(exceptionValue) || JS_IsNull(exceptionValue)) {
*result = (napi_value) &JSUndefined;
return napi_clear_last_error(env);
}
return CreateScopedResult(env, exceptionValue, result);
}
napi_status napi_get_last_error_info(napi_env env, const napi_extended_error_info **result) {
CHECK_ARG(env)
CHECK_ARG(result)
*result = env->last_error.error_code == napi_ok ? NULL : &env->last_error;
return napi_clear_last_error(env);
}
napi_status napi_create_error(napi_env env, napi_value code, napi_value msg, napi_value *result) {
CHECK_ARG(env)
CHECK_ARG(msg);
CHECK_ARG(result)
JSValue error = JS_NewError(env->context);
JS_SetPropertyStr(env->context, error, "message",
JS_DupValue(env->context, ToJS(msg)));
if (code != NULL) {
JS_SetPropertyStr(env->context, error, "code",
JS_DupValue(env->context, ToJS(code)));
}
return CreateScopedResult(env, error, result);
}
napi_status
napi_create_type_error(napi_env env, napi_value code, napi_value msg, napi_value *result) {
CHECK_ARG(env)
CHECK_ARG(result)
JSValue error = JS_NewError(env->context);
JS_SetPropertyStr(env->context, error, "message",
JS_DupValue(env->context, ToJS(msg)));
if (code != NULL) {
JS_SetPropertyStr(env->context, error, "code",
JS_DupValue(env->context, ToJS(code)));
}
JS_SetPropertyStr(env->context, error, "name", JS_NewString(env->context, "TypeError"));
return CreateScopedResult(env, error, result);
}
napi_status
napi_create_range_error(napi_env env, napi_value code, napi_value msg, napi_value *result) {
CHECK_ARG(env)
CHECK_ARG(result)
JSValue error = JS_NewError(env->context);
JS_SetPropertyStr(env->context, error, "message",
JS_DupValue(env->context, ToJS(msg)));
if (code != NULL) {
JS_SetPropertyStr(env->context, error, "code",
JS_DupValue(env->context, ToJS(code)));
}
JS_SetPropertyStr(env->context, error, "name", JS_NewString(env->context, "RangeError"));
return CreateScopedResult(env, error, result);
}
napi_status
napi_create_syntax_error(napi_env env, napi_value code, napi_value msg, napi_value *result) {
CHECK_ARG(env)
CHECK_ARG(result)
JSValue error = JS_NewError(env->context);
JS_SetPropertyStr(env->context, error, "message",
JS_DupValue(env->context, ToJS(msg)));
if (code != NULL) {
JS_SetPropertyStr(env->context, error, "code",
JS_DupValue(env->context, ToJS(code)));
}
JS_SetPropertyStr(env->context, error, "name", JS_NewString(env->context, "SyntaxError"));
return CreateScopedResult(env, error, result);
}
/**
* --------------------------------------
* REFERENCE MANAGEMENT
* --------------------------------------
*/
napi_status
napi_create_reference(napi_env env, napi_value value, uint32_t initialRefCount, napi_ref *result) {
CHECK_ARG(env)
CHECK_ARG(value)
CHECK_ARG(result)
*result = (napi_ref__ *) mi_malloc(sizeof(napi_ref__));
RETURN_STATUS_IF_FALSE(*result, napi_memory_error)
JSValue jsValue = ToJS(value);
if (JS_IsUndefined(jsValue)) {
(*result)->value = JS_UNDEFINED;
(*result)->referenceCount = 0;
LIST_INSERT_HEAD(&env->referencesList, *result, node);
return napi_clear_last_error(env);
}
if (initialRefCount == 0) {
JSValue global = JS_GetGlobalObject(env->context);
JSValue JS_WeakRef_Ctor = JS_GetProperty(env->context, global, env->atoms.weakref);
JSValue args[1] = {
jsValue
};
JSValue weak_ref = JS_CallConstructor(env->context, JS_WeakRef_Ctor, 1, args);
JS_FreeValue(env->context, global);
JS_FreeValue(env->context, JS_WeakRef_Ctor);
(*result)->referenceCount = 0;
(*result)->value = weak_ref;
} else {
(*result)->referenceCount = initialRefCount;
(*result)->value = JS_DupValue(env->context, jsValue);
}
LIST_INSERT_HEAD(&env->referencesList, *result, node);
return napi_clear_last_error(env);
}
napi_status napi_reference_ref(napi_env env, napi_ref ref, uint32_t *result) {
CHECK_ARG(env)
CHECK_ARG(ref)
if (ref->referenceCount == 0) {
JSValue value = JS_WeakRef_Deref(env->context, ref->value);
JS_FreeValue(env->context, ref->value);
ref->value = value;
}
uint8_t count = ++ref->referenceCount;
if (result) {
*result = count;
}
return napi_clear_last_error(env);
}
napi_status napi_reference_unref(napi_env env, napi_ref ref, uint32_t *result) {
CHECK_ARG(env)
CHECK_ARG(ref)
RETURN_STATUS_IF_FALSE(ref->referenceCount, napi_generic_failure)
if (ref->referenceCount == 1) {
JSValue global = JS_GetGlobalObject(env->context);
JSValue JS_WeakRef_Ctor = JS_GetProperty(env->context, global, env->atoms.weakref);
JSValue args[1] = {
ref->value
};
JSValue weak_ref = JS_CallConstructor(env->context, JS_WeakRef_Ctor, 1, args);
JS_FreeValue(env->context, global);
JS_FreeValue(env->context, JS_WeakRef_Ctor);
JS_FreeValue(env->context, ref->value);
ref->value = weak_ref;
}
uint8_t count = --ref->referenceCount;
if (result) {
*result = count;
}
return napi_clear_last_error(env);
}
napi_status napi_get_reference_value(napi_env env, napi_ref ref, napi_value *result) {
CHECK_ARG(env)
CHECK_ARG(ref)
CHECK_ARG(result)
if (!ref->referenceCount && JS_IsUndefined(ref->value)) {
*result = NULL;
return napi_ok;
}
JSValue value;
if (ref->referenceCount > 0) {
value = JS_DupValue(env->context, ref->value);
} else {
value = JS_WeakRef_Deref(env->context, ref->value);
}
if (JS_IsUndefined(value)) {
*result = NULL;
return napi_ok;
}
return CreateScopedResult(env, value, result);
}
napi_status napi_delete_reference(napi_env env, napi_ref ref) {
CHECK_ARG(env)
CHECK_ARG(ref)
if (!JS_IsUndefined(ref->value)) {
JS_FreeValue(env->context, ref->value);
ref->value = JSUndefined;
}
LIST_REMOVE(ref, node);
mi_free(ref);
return napi_clear_last_error(env);
}
/**
* --------------------------------------
* NATIVE TO JS VALUE CONVERSION
* --------------------------------------
*/
napi_status napi_create_string_latin1(napi_env env,
const char *str,
size_t length,
napi_value *result) {
CHECK_ARG(env)
CHECK_ARG(result)
CHECK_ARG(str)
JSValue value = JS_NewStringLen(env->context, str,
(length == NAPI_AUTO_LENGTH) ? strlen(str) : length);
return CreateScopedResult(env, value, result);
}
size_t char16_t_length(const char16_t *str) {
size_t length = 0;
while (str[length] != 0) {
++length;
}
return length;
}
napi_status napi_create_string_utf16(napi_env env,
const char16_t *str,
size_t length,
napi_value *result) {
CHECK_ARG(env)
CHECK_ARG(result)
CHECK_ARG(str)
JSValue value = JS_NewString16(env->context, (uint16_t *) str,
length == NAPI_AUTO_LENGTH ? (int) char16_t_length(str)
: (int) length);
return CreateScopedResult(env, value, result);
}
napi_status napi_create_string_utf8(napi_env env,
const char *str,
size_t length,
napi_value *result) {
CHECK_ARG(env)
CHECK_ARG(result)
CHECK_ARG(str)
JSValue value = JS_NewStringLen(env->context, str,
(length == NAPI_AUTO_LENGTH) ? strlen(str) : length);
return CreateScopedResult(env, value, result);
}
napi_status napi_create_int32(napi_env env, int32_t value, napi_value *result) {
CHECK_ARG(env)
CHECK_ARG(result)
JSValue jsValue = JS_NewInt32(env->context, value);
return CreateScopedResult(env, jsValue, result);
}
napi_status napi_create_uint32(napi_env env, uint32_t value, napi_value *result) {
CHECK_ARG(env)
CHECK_ARG(result)
JSValue jsValue = JS_NewInt32(env->context, value);
return CreateScopedResult(env, jsValue, result);
}
napi_status napi_create_int64(napi_env env, int64_t value, napi_value *result) {
CHECK_ARG(env)
CHECK_ARG(result)
JSValue jsValue = JS_NewInt64(env->context, value);
return CreateScopedResult(env, jsValue, result);
}
napi_status napi_create_uint64(napi_env env, uint64_t value, napi_value *result) {
CHECK_ARG(env)
CHECK_ARG(result)
JSValue jsValue = JS_NewInt64(env->context, value);
return CreateScopedResult(env, jsValue, result);
}
napi_status napi_create_double(napi_env env, double value, napi_value *result) {
CHECK_ARG(env)
CHECK_ARG(result)
JSValue jsValue = JS_NewFloat64(env->context, value);
return CreateScopedResult(env, jsValue, result);
}
JSValue
JS_CreateBigIntWords(JSContext *context, int signBit, size_t wordCount, const uint64_t *words) {
JSValue thisVar = JS_UNDEFINED;
const size_t Count = 20;
const size_t Two = 2;
if (wordCount <= 0 || wordCount > Count || words == NULL) {
return JS_EXCEPTION;
}
JSValue signValue = JS_NewInt32(context, (signBit % Two));
if (JS_IsException(signValue)) {
return JS_EXCEPTION;
}
JSValue wordsValue = JS_NewArray(context);
if (JS_IsException(wordsValue)) {
return JS_EXCEPTION;
}
for (size_t i = 0; i < wordCount; i++) {
// shift 32 bits right to get high bit
JSValue idxValueHigh = JS_NewUint32(context, (uint32_t) (words[i] >> 32));
// gets lower 32 bits
JSValue idxValueLow = JS_NewUint32(context, (uint32_t) (words[i] & 0xFFFFFFFF));
if (!(JS_IsException(idxValueHigh)) && !(JS_IsException(idxValueLow))) {
JS_SetPropertyUint32(context, wordsValue, (i * Two), idxValueHigh);
JS_SetPropertyUint32(context, wordsValue, (i * Two + 1), idxValueLow);
JS_FreeValue(context, idxValueHigh);
JS_FreeValue(context, idxValueLow);
}
}
JSValue argv[2] = {signValue, wordsValue};
JSValue global = JS_GetGlobalObject(context);
JSValue CreateBigIntWords = JS_GetPropertyStr(context, global, "CreateBigIntWords");
JSValue ret = JS_Call(context, CreateBigIntWords, thisVar, 2, (JSValue *) &argv);
JS_FreeValue(context, global);
JS_FreeValue(context, CreateBigIntWords);
JS_FreeValue(context, signValue);
JS_FreeValue(context, wordsValue);
return ret;
}
bool ParseBigIntWordsInternal(JSContext *context, JSValue value, int *signBit, size_t *wordCount,
uint64_t *words) {
int cntValue = 0;
if (wordCount == NULL) {
return false;
}
JSValue jsValue = JS_GetPropertyStr(context, value, "count");
if (!JS_IsException(jsValue)) {
JS_ToInt32(context, &cntValue, jsValue);