-
-
Notifications
You must be signed in to change notification settings - Fork 24
/
quickjs.pas
1482 lines (1241 loc) · 64 KB
/
quickjs.pas
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
{
FreePascal / Delphi bindings for QuickJS Engine.
Copyright(c) 2019-2020 Coldzer0 <Coldzer0 [at] protonmail.ch>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to
deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
sell copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
IN THE SOFTWARE.
}
unit QuickJS; // sync with version - "2020-04-12".
{$IfDef FPC}
{$MODE Delphi}
{$PackRecords C}
{$EndIf}
{$IfDef FPC}
{$IfNDef windows}
{$LinkLib 'libquickjs.a'}
{$EndIf}
{$EndIf}
{$IfDef FPC}
{$IfNDef CPU64}
{$Define JS_NAN_BOXING}
{$ENDIF}
{$ELSE}
{$IfNDef CPUX64}
{$Define JS_NAN_BOXING}
{$ENDIF}
{$ENDIF}
interface
uses
math;
{===============================================================================}
{ QuickJS Constants }
{===============================================================================}
const
QJS_VERSION = '2020-04-12';
const
{ all tags with a reference count are negative }
JS_TAG_FIRST = -11; { first negative tag }
JS_TAG_BIG_DECIMAL = -11;
JS_TAG_BIG_INT = -10;
JS_TAG_BIG_FLOAT = -9;
JS_TAG_SYMBOL = -8;
JS_TAG_STRING = -7;
JS_TAG_MODULE = -3; { used internally }
JS_TAG_FUNCTION_BYTECODE = -2; { used internally }
JS_TAG_OBJECT = -1;
JS_TAG_INT = 0;
JS_TAG_BOOL = 1;
JS_TAG_NULL = 2;
JS_TAG_UNDEFINED = 3;
JS_TAG_UNINITIALIZED = 4;
JS_TAG_CATCH_OFFSET = 5;
JS_TAG_EXCEPTION = 6;
JS_TAG_FLOAT64 = 7;
{ any larger tag is FLOAT64 if JS_NAN_BOXING }
JS_FLOAT64_NAN = NaN;
const
{ flags for object properties }
JS_PROP_CONFIGURABLE = (1 shl 0);
JS_PROP_WRITABLE = (1 shl 1);
JS_PROP_ENUMERABLE = (1 shl 2);
JS_PROP_C_W_E = (JS_PROP_CONFIGURABLE or JS_PROP_WRITABLE or JS_PROP_ENUMERABLE);
JS_PROP_LENGTH = (1 shl 3); { used internally in Arrays }
JS_PROP_TMASK = (3 shl 4); { mask for NORMAL, GETSET, VARREF, AUTOINIT }
JS_PROP_NORMAL = (0 shl 4);
JS_PROP_GETSET = (1 shl 4);
JS_PROP_VARREF = (2 shl 4); { used internally }
JS_PROP_AUTOINIT = (3 shl 4); { used internally }
{ flags for JS_DefineProperty }
JS_PROP_HAS_SHIFT = 8;
JS_PROP_HAS_CONFIGURABLE = (1 shl 8);
JS_PROP_HAS_WRITABLE = (1 shl 9);
JS_PROP_HAS_ENUMERABLE = (1 shl 10);
JS_PROP_HAS_GET = (1 shl 11);
JS_PROP_HAS_SET = (1 shl 12);
JS_PROP_HAS_VALUE = (1 shl 13);
{ throw an exception if false would be returned /
(JS_DefineProperty/JS_SetProperty) }
JS_PROP_THROW = (1 shl 14);
{ throw an exception if false would be returned in strict mode /
(JS_SetProperty) }
JS_PROP_THROW_STRICT = (1 shl 15);
JS_PROP_NO_ADD = (1 shl 16); { internal use }
JS_PROP_NO_EXOTIC = (1 shl 17); { internal use }
JS_DEFAULT_STACK_SIZE = (256 * 1024);
{ JS_Eval() flags }
JS_EVAL_TYPE_GLOBAL = (0 shl 0); { global code (default) }
JS_EVAL_TYPE_MODULE = (1 shl 0); { module code }
JS_EVAL_TYPE_DIRECT = (2 shl 0); { direct call (internal use) }
JS_EVAL_TYPE_INDIRECT = (3 shl 0); { indirect call (internal use) }
JS_EVAL_TYPE_MASK = (3 shl 0);
JS_EVAL_FLAG_STRICT = (1 shl 3); { force 'strict' mode }
JS_EVAL_FLAG_STRIP = (1 shl 4); { force 'strip' mode }
(*
compile but do not run. The result is an object with a
JS_TAG_FUNCTION_BYTECODE or JS_TAG_MODULE tag. It can be executed
with JS_EvalFunction().
*)
JS_EVAL_FLAG_COMPILE_ONLY = (1 shl 5); { internal use }
{ don't include the stack frames before this eval in the Error() backtraces }
JS_EVAL_FLAG_BACKTRACE_BARRIER = (1 shl 6);
{ Object Writer/Reader (currently only used to handle precompiled code) }
JS_WRITE_OBJ_BYTECODE = (1 shl 0); { allow function/module }
JS_WRITE_OBJ_BSWAP = (1 shl 1); { byte swapped output }
JS_READ_OBJ_BYTECODE = (1 shl 0); { allow function/module }
JS_READ_OBJ_ROM_DATA = (1 shl 1); { avoid duplicating 'buf' data }
{ C property definition }
JS_DEF_CFUNC = 0;
JS_DEF_CGETSET = 1;
JS_DEF_CGETSET_MAGIC = 2;
JS_DEF_PROP_STRING = 3;
JS_DEF_PROP_INT32 = 4;
JS_DEF_PROP_INT64 = 5;
JS_DEF_PROP_DOUBLE = 6;
JS_DEF_PROP_UNDEFINED = 7;
JS_DEF_OBJECT = 8;
JS_DEF_ALIAS = 9;
{ C function definition }
{ JSCFunctionEnum }
JS_CFUNC_generic = 0;
JS_CFUNC_generic_magic = 1;
JS_CFUNC_constructor = 2;
JS_CFUNC_constructor_magic = 3;
JS_CFUNC_constructor_or_func = 4;
JS_CFUNC_constructor_or_func_magic = 5;
JS_CFUNC_f_f = 6;
JS_CFUNC_f_f_f = 7;
JS_CFUNC_getter = 8;
JS_CFUNC_setter = 9;
JS_CFUNC_getter_magic = 10;
JS_CFUNC_setter_magic = 11;
JS_CFUNC_iterator_next = 12;
JS_GPN_STRING_MASK = (1 shl 0);
JS_GPN_SYMBOL_MASK = (1 shl 1);
JS_GPN_PRIVATE_MASK = (1 shl 2);
{ only include the enumerable properties }
JS_GPN_ENUM_ONLY = (1 shl 4);
{ set theJSPropertyEnum.is_enumerable field }
JS_GPN_SET_ENUM = (1 shl 5);
{ C Call Flags }
JS_CALL_FLAG_CONSTRUCTOR = (1 shl 0);
{===============================================================================}
{===============================================================================}
type
{$IFNDEF FPC}
// Delphi Compatible.
// Anything under XE4.
{$IF (CompilerVersion <= 25)}
PUint32 = ^Uint32; // PUint32 not defined in XE4 - Fix by @edwinyzh
{$IFEND}
pUInt8 = PByte;
pInt8 = PShortInt;
pInt16 = PSmallint;
PInt32 = PLongint;
{$ENDIF}
{$ifdef cpu64}
size_t = QWord;
psize_t = ^size_t;
{$else}
size_t = Cardinal;
psize_t = ^size_t;
{$endif}
JS_BOOL = Boolean;
JSRuntime = Pointer;
PPJSContext = ^_PJSContext; // Pointer to Pointer.
_PJSContext = ^_JSContext;
_JSContext = record end; // Empty record to mimic the JSContext.
JSContext = Pointer;
JSObject = Pointer;
JSClass = Pointer;
JSModuleDef = Pointer;
JSString = Pointer;
JSClassID = UInt32;
PJSClassID = ^JSClassID;
JSAtom = UInt32;
JSCFunctionEnum = Integer;
JSGCObjectHeader = Pointer;
type
PJSRefCountHeader = ^JSRefCountHeader;
JSRefCountHeader = record
ref_count : Integer;
end;
{$If Defined(JS_NAN_BOXING)}
JSValue = UInt64;
PJSValue = ^JSValue;
JSValueConst = JSValue;
PJSValueConst = ^JSValueConst;
JSValueConstArr = array[0..(MaxInt div SizeOf(JSValueConst))-1] of JSValueConst;
PJSValueConstArr = ^JSValueConstArr;
const
JS_FLOAT64_TAG_ADDEND = $7ff80000 - JS_TAG_FIRST + 1; // quiet NaN encoding
JS_NAN = ($7ff8000000000000 - (JS_FLOAT64_TAG_ADDEND shl 32));
{$Else}
type
JSValueUnion = record
case byte of
0 : (&int32 : int32);
1 : (float64 : Double);
2 : (Ptr : Pointer);
end;
JSValue = record
u : JSValueUnion;
tag : Int64;
end;
PJSValue = ^JSValue;
JSValueConst = JSValue;
PJSValueConst = ^JSValueConst;
JSValueConstArr = array[0..(MaxInt div SizeOf(JSValueConst))-1] of JSValueConst;
PJSValueConstArr = ^JSValueConstArr;
{$ENDIF}
type
JSMallocState = record
malloc_count,
malloc_size,
malloc_limit : size_t;
opaque : Pointer;
end;
PJSMallocState = ^JSMallocState;
//c_malloc = function (s : JSMallocState; size : UInt64) : Pointer;
//Pc_malloc = ^c_malloc;
// TODO: Check If funcs need to be Pointers or not. ^^^^^
JSMallocFunctions = record
js_malloc : function (s : PJSMallocState; size : size_t) : Pointer; cdecl;
js_free : procedure (s : PJSMallocState; Ptr : Pointer); cdecl;
js_realloc : function (s : PJSMallocState; Ptr : Pointer ; size : size_t) : Pointer; cdecl;
js_malloc_usable_size : function (Ptr : Pointer) : size_t; cdecl;
end;
PJSMallocFunctions = ^JSMallocFunctions;
PJSMemoryUsage = ^JSMemoryUsage;
JSMemoryUsage = record
malloc_size, malloc_limit, memory_used_size,
malloc_count,
memory_used_count,
atom_count, atom_size,
str_count, str_size,
obj_count, obj_size,
prop_count, prop_size,
shape_count, shape_size,
js_func_count, js_func_size, js_func_code_size,
js_func_pc2line_count, js_func_pc2line_size,
c_func_count, array_count,
fast_array_count, fast_array_elements,
binary_object_count, binary_object_size : Int64;
end;
{===============================================================================}
{ Native Functions Callbcaks }
{===============================================================================}
PJSCFunction = ^JSCFunction;
JSCFunction = function (ctx : JSContext; this_val : JSValueConst;
argc : Integer; argv : PJSValueConstArr): JSValue; cdecl;
PJSCFunctionMagic = ^JSCFunctionMagic;
JSCFunctionMagic = function (ctx : JSContext; this_val : JSValueConst;
argc : Integer; argv : PJSValueConst; magic : Integer): JSValue; cdecl;
PJSCFunctionData = ^JSCFunctionData;
JSCFunctionData = function (ctx : JSContext; this_val : JSValueConst;
argc : Integer; argv : PJSValueConst; magic : Integer;
func_data : PJSValue ): JSValue; cdecl;
{===============================================================================}
PJS_MarkFunc = ^JS_MarkFunc;
JS_MarkFunc = procedure (rt : JSRuntime; gp : JSGCObjectHeader); cdecl;
PJSClassFinalizer = ^JSClassFinalizer;
JSClassFinalizer = procedure (rt : JSRuntime; val : JSValue); cdecl;
PJSClassGCMark = ^JSClassGCMark;
JSClassGCMark = procedure (rt : JSRuntime; val : JSValueConst; mark_func: PJS_MarkFunc); cdecl;
PJSClassCall = ^JSClassCall;
JSClassCall = function (ctx : JSContext;
func_obj : JSValueConst;
this_val : JSValueConst;
argc : Integer; argv : PJSValueConst;
flags : Integer) : JSValue; cdecl;
PJSFreeArrayBufferDataFunc = ^JSFreeArrayBufferDataFunc;
JSFreeArrayBufferDataFunc = procedure(rt : JSRuntime; opaque, Ptr : Pointer); cdecl;
{ return != 0 if the JS code needs to be interrupted }
PJSInterruptHandler = ^JSInterruptHandler;
JSInterruptHandler = function (rt : JSRuntime; opaque : Pointer): integer; cdecl;
{ return the module specifier (allocated with js_malloc()) or NULL if exception }
PJSModuleNormalizeFunc = ^JSModuleNormalizeFunc;
JSModuleNormalizeFunc = function (ctx : JSContext;
const module_base_name , module_name : {$IFDEF FPC}PChar{$Else}PAnsiChar{$EndIf};
opaque : Pointer): {$IFDEF FPC}PChar{$Else}PAnsiChar{$EndIf}; cdecl;
PJSModuleLoaderFunc = ^JSModuleLoaderFunc;
JSModuleLoaderFunc = function (ctx : JSContext; module_name : {$IFDEF FPC}PChar{$Else}PAnsiChar{$EndIf}; opaque : Pointer) : JSModuleDef; cdecl;
{ JS Job support }
PJSJobFunc = ^JSJobFunc;
JSJobFunc = function (ctx : JSContext; argc : Integer; argv : PJSValueConst): JSValue; cdecl;
{ C module definition }
PJSModuleInitFunc = ^JSModuleInitFunc;
JSModuleInitFunc = function (ctx : JSContext; m : JSModuleDef): Integer; cdecl;
{ Promises RejectionTracker CallBack }
{ is_handled = TRUE means that the rejection is handled }
PJSHostPromiseRejectionTracker = ^JSHostPromiseRejectionTracker;
JSHostPromiseRejectionTracker = procedure(ctx : JSContext;
promise, reason :JSValueConst;
is_handled : JS_BOOL; opaque : Pointer); cdecl;
{===============================================================================}
{ object class support }
PPJSPropertyEnum = ^PJSPropertyEnum;
PJSPropertyEnum = ^JSPropertyEnum;
JSPropertyEnum = record
is_enumerable : JS_BOOL;
atom : JSAtom;
end;
PJSPropertyDescriptor = ^JSPropertyDescriptor;
JSPropertyDescriptor = record
flags : Integer;
value,
getter,
setter : JSValue;
end;
PJSClassExoticMethods = ^JSClassExoticMethods;
JSClassExoticMethods = record
{ Return -1 if exception (can only happen in case of Proxy object),
FALSE if the property does not exists, TRUE if it exists. If 1 is
returned, the property descriptor 'desc' is filled if != NULL. }
get_own_property : function (ctx: JSContext; desc: PJSPropertyDescriptor; obj:JSValueConst; prop:JSAtom):Integer;cdecl;
{ '*ptab' should hold the '*plen' property keys. Return 0 if OK,
-1 if exception. The 'is_enumerable' field is ignored. }
get_own_property_names : function (ctx: JSContext; ptab:PPJSPropertyEnum; plen: pUInt32; obj:JSValueConst):Integer;cdecl;
{ return < 0 if exception, or TRUE/FALSE }
delete_property : function (ctx: JSContext; obj:JSValueConst; prop:JSAtom):Integer;cdecl;
{ return < 0 if exception or TRUE/FALSE }
define_own_property : function (ctx: JSContext; this_obj:JSValueConst; prop:JSAtom; val:JSValueConst; getter:JSValueConst;
setter:JSValueConst; flags:Integer):Integer;cdecl;
{ The following methods can be emulated with the previous ones,
so they are usually not needed }
{ return < 0 if exception or TRUE/FALSE }
has_property : function (ctx: JSContext; obj:JSValueConst; atom:JSAtom):Integer;cdecl;
get_property : function (ctx: JSContext; obj:JSValueConst; atom:JSAtom; receiver:JSValueConst):JSValue;cdecl;
set_property : function (ctx: JSContext; obj:JSValueConst; atom:JSAtom; value:JSValueConst; receiver:JSValueConst;
flags:Integer):Integer;cdecl;
end;
PJSClassDef = ^JSClassDef;
JSClassDef = record
class_name : {$IFDEF FPC}PChar{$Else}PAnsiChar{$EndIf};
finalizer : PJSClassFinalizer;
gc_mark : PJSClassGCMark;
{
if call != NULL, the object is a function. If (flags &
JS_CALL_FLAG_CONSTRUCTOR) != 0, the function is called as a
constructor. In this case, 'this_val' is new.target. A
constructor call only happens if the object constructor bit is
set (see JS_SetConstructorBit())
}
call : PJSClassCall;
{ XXX: suppress this indirection ? It is here only to save memory
because only a few classes need these methods }
exotic : PJSClassExoticMethods;
end;
{ C function definition }
constructor_magic_func = function (ctx: JSContext; new_target:JSValueConst; argc:Integer; argv:PJSValueConst;
magic:Integer):JSValue; cdecl;
f_f_func = function (_para1:double):double cdecl;
f_f_f_func = function (_para1:double; _para2:double):double; cdecl;
Getter_func = function (ctx: JSContext; this_val:JSValueConst):JSValue; cdecl;
Setter_func = function (ctx: JSContext; this_val:JSValueConst; val:JSValueConst):JSValue;cdecl;
getter_magic_func = function (ctx: JSContext; this_val:JSValueConst; magic:Integer):JSValue; cdecl;
setter_magic_func = function (ctx: JSContext; this_val:JSValueConst; val:JSValueConst; magic:Integer):JSValue; cdecl;
iterator_next_func = function (ctx: JSContext; this_val:JSValueConst; argc:Integer; argv:PJSValueConst; pdone:PInteger;
magic:Integer):JSValue; cdecl;
JSCFunctionType = record
case Integer of
0 : ( generic : JSCFunction );
1 : ( generic_magic : JSCFunctionMagic);
2 : ( &constructor : JSCFunction );
3 : ( constructor_magic : constructor_magic_func);
4 : ( constructor_or_func : JSCFunction );
5 : ( f_f : f_f_func);
6 : ( f_f_f : f_f_f_func);
7 : ( getter : Getter_func);
8 : ( setter : Setter_func);
9 : ( getter_magic : getter_magic_func);
10 : ( setter_magic : setter_magic_func);
11 : ( iterator_next : iterator_next_func);
end;
PJSCFunctionType = ^JSCFunctionType;
{ C property definition }
JSCFunctionListEntry = record
name : {$IFDEF FPC}PChar{$Else}PAnsiChar{$EndIf};
prop_flags : UInt8;
def_type : UInt8;
magic : Int16;
u : record
case Integer of
0 : ( func : record
length : UInt8; { XXX: should move outside union }
cproto : UInt8; { XXX: should move outside union }
cfunc : JSCFunctionType;
end );
1 : ( getset : record
get : JSCFunctionType;
_set : JSCFunctionType;
end );
2 : ( alias : record
name : {$IFDEF FPC}PChar{$Else}PAnsiChar{$EndIf};
base : Integer;
end );
3 : ( prop_list : record
tab : ^JSCFunctionListEntry;
len : Integer;
end );
4 : ( str : {$IFDEF FPC}PChar{$Else}PAnsiChar{$EndIf} );
5 : ( i32 : Int32 );
6 : ( i64 : Int64 );
7 : ( f64 : double );
end;
end;
PJSCFunctionListEntry = ^JSCFunctionListEntry;
{$IFDEF mswindows}const QJSDLL = {$IfDef WIN64}'quickjs64.dll'{$Else}'quickjs32.dll'{$EndIf};{$endif}
{ QuickJS external APIs }
function JS_NewRuntime : JSRuntime; cdecl; external {$IFDEF mswindows}QJSDLL{$endif};
(* info lifetime must exceed that of rt *)
procedure JS_SetRuntimeInfo(rt : JSRuntime; const info : {$IFDEF FPC}PChar{$Else}PAnsiChar{$EndIf}); cdecl; external {$IFDEF mswindows}QJSDLL{$endif};
procedure JS_SetMemoryLimit(rt : JSRuntime; limit : size_t); cdecl; external {$IFDEF mswindows}QJSDLL{$endif};
procedure JS_SetGCThreshold(rt : JSRuntime; gc_threshold : size_t); cdecl; external {$IFDEF mswindows}QJSDLL{$endif};
procedure JS_SetMaxStackSize(ctx: JSContext; stack_size:size_t); cdecl; external {$IFDEF mswindows}QJSDLL{$endif};
function JS_NewRuntime2(const mf : PJSMallocFunctions; opaque : Pointer) : JSRuntime; cdecl; external {$IFDEF mswindows}QJSDLL{$endif};
procedure JS_FreeRuntime(rt : JSRuntime); cdecl; external {$IFDEF mswindows}QJSDLL{$endif};
function JS_GetRuntimeOpaque(rt : JSRuntime) : Pointer; cdecl; external {$IFDEF mswindows}QJSDLL{$endif};
procedure JS_SetRuntimeOpaque(rt : JSRuntime; opaque : Pointer); cdecl; external {$IFDEF mswindows}QJSDLL{$endif};
procedure JS_MarkValue(rt:JSRuntime; val:JSValueConst; mark_func:PJS_MarkFunc);cdecl; external {$IFDEF mswindows}QJSDLL{$endif};
procedure JS_RunGC(rt:JSRuntime); cdecl; external {$IFDEF mswindows}QJSDLL{$endif};
function JS_IsLiveObject(rt:JSRuntime; obj:JSValueConst):JS_BOOL; cdecl; external {$IFDEF mswindows}QJSDLL{$endif};
//{REMOVE}function JS_IsInGCSweep(rt:JSRuntime):JS_BOOL; cdecl; external {$IFDEF mswindows}QJSDLL{$endif};
function JS_NewContext(rt:JSRuntime):JSContext; cdecl; external {$IFDEF mswindows}QJSDLL{$endif};
procedure JS_FreeContext(s: JSContext); cdecl; external {$IFDEF mswindows}QJSDLL{$endif};
function JS_DupContext(ctx : JSContext) : JSContext; cdecl; external {$IFDEF mswindows}QJSDLL{$endif};
function JS_GetContextOpaque(ctx: JSContext):pointer; cdecl; external {$IFDEF mswindows}QJSDLL{$endif};
procedure JS_SetContextOpaque(ctx: JSContext; opaque:pointer); cdecl; external {$IFDEF mswindows}QJSDLL{$endif};
function JS_GetRuntime(ctx: JSContext):JSRuntime; cdecl; external {$IFDEF mswindows}QJSDLL{$endif};
procedure JS_SetClassProto(ctx: JSContext; class_id:JSClassID; obj:JSValue); cdecl; external {$IFDEF mswindows}QJSDLL{$endif};
function JS_GetClassProto(ctx: JSContext; class_id:JSClassID):JSValue; cdecl; external {$IFDEF mswindows}QJSDLL{$endif};
{ the following functions are used to select the intrinsic object to save memory }
function JS_NewContextRaw(rt: JSRuntime): JSContext; cdecl; external {$IFDEF mswindows}QJSDLL{$endif};
procedure JS_AddIntrinsicBaseObjects(ctx: JSContext); cdecl; external {$IFDEF mswindows}QJSDLL{$endif};
procedure JS_AddIntrinsicDate(ctx: JSContext); cdecl; external {$IFDEF mswindows}QJSDLL{$endif};
procedure JS_AddIntrinsicEval(ctx: JSContext); cdecl; external {$IFDEF mswindows}QJSDLL{$endif};
procedure JS_AddIntrinsicStringNormalize(ctx: JSContext); cdecl; external {$IFDEF mswindows}QJSDLL{$endif};
procedure JS_AddIntrinsicRegExpCompiler(ctx: JSContext); cdecl; external {$IFDEF mswindows}QJSDLL{$endif};
procedure JS_AddIntrinsicRegExp(ctx: JSContext); cdecl; external {$IFDEF mswindows}QJSDLL{$endif};
procedure JS_AddIntrinsicJSON(ctx: JSContext); cdecl; external {$IFDEF mswindows}QJSDLL{$endif};
procedure JS_AddIntrinsicProxy(ctx: JSContext); cdecl; external {$IFDEF mswindows}QJSDLL{$endif};
procedure JS_AddIntrinsicMapSet(ctx: JSContext); cdecl; external {$IFDEF mswindows}QJSDLL{$endif};
procedure JS_AddIntrinsicTypedArrays(ctx: JSContext); cdecl; external {$IFDEF mswindows}QJSDLL{$endif};
procedure JS_AddIntrinsicPromise(ctx: JSContext); cdecl; external {$IFDEF mswindows}QJSDLL{$endif};
procedure JS_AddIntrinsicBigInt(ctx: JSContext); cdecl; external {$IFDEF mswindows}QJSDLL{$endif};
procedure JS_AddIntrinsicBigFloat(ctx: JSContext); cdecl; external {$IFDEF mswindows}QJSDLL{$endif};
procedure JS_AddIntrinsicBigDecimal(ctx: JSContext); cdecl; external {$IFDEF mswindows}QJSDLL{$endif};
{ enable operator overloading }
procedure JS_AddIntrinsicOperators(ctx: JSContext); cdecl; external {$IFDEF mswindows}QJSDLL{$endif};
{ enable "use math" }
procedure JS_EnableBignumExt(ctx: JSContext; enable : JS_BOOL); cdecl; external {$IFDEF mswindows}QJSDLL{$endif};
function js_string_codePointRange(ctx: JSContext; this_val:JSValueConst; argc:Integer; argv:PJSValueConst):JSValue; cdecl; external {$IFDEF mswindows}QJSDLL{$endif};
function js_malloc_rt(rt: JSRuntime; size:size_t):pointer; cdecl; external {$IFDEF mswindows}QJSDLL{$endif};
procedure js_free_rt(rt: JSRuntime; ptr:pointer); cdecl; external {$IFDEF mswindows}QJSDLL{$endif};
function js_realloc_rt(rt: JSRuntime; ptr:pointer; size:size_t):pointer; cdecl; external {$IFDEF mswindows}QJSDLL{$endif};
function js_malloc_usable_size_rt(rt: JSRuntime; ptr:pointer):size_t; cdecl; external {$IFDEF mswindows}QJSDLL{$endif};
function js_mallocz_rt(rt: JSRuntime; size:size_t):pointer; cdecl; external {$IFDEF mswindows}QJSDLL{$endif};
function js_malloc(ctx: JSContext; size:size_t):pointer; cdecl; external {$IFDEF mswindows}QJSDLL{$endif};
procedure js_free(ctx: JSContext; ptr:pointer); cdecl; external {$IFDEF mswindows}QJSDLL{$endif};
function js_realloc(ctx: JSContext; ptr:pointer; size:size_t):pointer; cdecl; external {$IFDEF mswindows}QJSDLL{$endif};
function js_malloc_usable_size(ctx: JSContext; ptr:pointer):size_t; cdecl; external {$IFDEF mswindows}QJSDLL{$endif};
function js_realloc2(ctx: JSContext; ptr:pointer; size:size_t; pslack:Psize_t):pointer; cdecl; external {$IFDEF mswindows}QJSDLL{$endif};
function js_mallocz(ctx: JSContext; size:size_t):pointer; cdecl; external {$IFDEF mswindows}QJSDLL{$endif};
function js_strdup(ctx: JSContext; str:{$IFDEF FPC}PChar{$Else}PAnsiChar{$EndIf}): {$IFDEF FPC}PChar{$Else}PAnsiChar{$EndIf}; cdecl; external {$IFDEF mswindows}QJSDLL{$endif};
function js_strndup(ctx: JSContext; s:{$IFDEF FPC}PChar{$Else}PAnsiChar{$EndIf}; n:size_t): {$IFDEF FPC}PChar{$Else}PAnsiChar{$EndIf}; cdecl; external {$IFDEF mswindows}QJSDLL{$endif};
procedure JS_ComputeMemoryUsage(rt: JSRuntime; s:PJSMemoryUsage); cdecl; external {$IFDEF mswindows}QJSDLL{$endif};
procedure JS_DumpMemoryUsage(fp: Pointer; s:PJSMemoryUsage; rt: JSRuntime); cdecl; external {$IFDEF mswindows}QJSDLL{$endif};
{ atom support }
function JS_NewAtomLen(ctx: JSContext; str:{$IFDEF FPC}PChar{$Else}PAnsiChar{$EndIf}; len:size_t):JSAtom; cdecl; external {$IFDEF mswindows}QJSDLL{$endif};
function JS_NewAtom(ctx: JSContext; str:{$IFDEF FPC}PChar{$Else}PAnsiChar{$EndIf}):JSAtom; cdecl; external {$IFDEF mswindows}QJSDLL{$endif};
function JS_NewAtomUInt32(ctx: JSContext; n:UInt32):JSAtom; cdecl; external {$IFDEF mswindows}QJSDLL{$endif};
function JS_DupAtom(ctx: JSContext; v:JSAtom):JSAtom; cdecl; external {$IFDEF mswindows}QJSDLL{$endif};
procedure JS_FreeAtom(ctx: JSContext; v:JSAtom); cdecl; external {$IFDEF mswindows}QJSDLL{$endif};
procedure JS_FreeAtomRT(rt: JSRuntime; v:JSAtom); cdecl; external {$IFDEF mswindows}QJSDLL{$endif};
function JS_AtomToValue(ctx: JSContext; atom:JSAtom):JSValue; cdecl; external {$IFDEF mswindows}QJSDLL{$endif};
function JS_AtomToString(ctx: JSContext; atom:JSAtom):JSValue; cdecl; external {$IFDEF mswindows}QJSDLL{$endif};
function JS_AtomToCString(ctx: JSContext; atom:JSAtom):{$IFDEF FPC}PChar{$Else}PAnsiChar{$EndIf}; cdecl; external {$IFDEF mswindows}QJSDLL{$endif};
function JS_ValueToAtom(ctx: JSContext; val:JSValueConst) : JSAtom; cdecl; external {$IFDEF mswindows}QJSDLL{$endif};
{ object class support }
function JS_NewClassID(pclass_id:PJSClassID):JSClassID; cdecl; external {$IFDEF mswindows}QJSDLL{$endif};
function JS_NewClass(rt: JSRuntime; class_id:JSClassID; class_def: PJSClassDef):Integer; cdecl; external {$IFDEF mswindows}QJSDLL{$endif};
function JS_IsRegisteredClass(rt: JSRuntime; class_id:JSClassID):Integer; cdecl; external {$IFDEF mswindows}QJSDLL{$endif};
{ JS Numbers }
function JS_NewBigInt64 (ctx : JSContext; v : Int64): JSValue; cdecl; external {$IFDEF mswindows}QJSDLL{$endif};
function JS_NewBigUint64 (ctx : JSContext; v : UInt64): JSValue; cdecl; external {$IFDEF mswindows}QJSDLL{$endif};
function JS_Throw(ctx: JSContext; obj:JSValue):JSValue; cdecl; external {$IFDEF mswindows}QJSDLL{$endif};
function JS_GetException(ctx: JSContext):JSValue; cdecl; external {$IFDEF mswindows}QJSDLL{$endif};
function JS_IsError(ctx: JSContext; val:JSValueConst):JS_BOOL; cdecl; external {$IFDEF mswindows}QJSDLL{$endif};
procedure JS_ResetUncatchableError(ctx: JSContext); cdecl; external {$IFDEF mswindows}QJSDLL{$endif};
function JS_NewError(ctx: JSContext):JSValue; cdecl; external {$IFDEF mswindows}QJSDLL{$endif};
function JS_ThrowSyntaxError(ctx: JSContext; fmt : {$IFDEF FPC}PChar{$Else}PAnsiChar{$EndIf}; args : Array of Const): JSValue; cdecl; external {$IFDEF mswindows}QJSDLL{$endif};
function JS_ThrowTypeError(ctx: JSContext; fmt : {$IFDEF FPC}PChar{$Else}PAnsiChar{$EndIf}; args : Array of Const): JSValue; cdecl; external {$IFDEF mswindows}QJSDLL{$endif};
function JS_ThrowReferenceError(ctx: JSContext; fmt : {$IFDEF FPC}PChar{$Else}PAnsiChar{$EndIf}; args : Array of Const): JSValue; cdecl; external {$IFDEF mswindows}QJSDLL{$endif};
function JS_ThrowRangeError(ctx: JSContext; fmt : {$IFDEF FPC}PChar{$Else}PAnsiChar{$EndIf}; args : Array of Const): JSValue; cdecl; external {$IFDEF mswindows}QJSDLL{$endif};
function JS_ThrowInternalError(ctx: JSContext; fmt : {$IFDEF FPC}PChar{$Else}PAnsiChar{$EndIf}; args : Array of Const): JSValue; cdecl; external {$IFDEF mswindows}QJSDLL{$endif};
function JS_ThrowOutOfMemory(ctx: JSContext): JSValue; cdecl; external {$IFDEF mswindows}QJSDLL{$endif};
procedure __JS_FreeValue(ctx: JSContext; v : JSValue); cdecl; external {$IFDEF mswindows}QJSDLL{$endif};
procedure __JS_FreeValueRT(rt: JSRuntime; v : JSValue); cdecl; external {$IFDEF mswindows}QJSDLL{$endif};
{ JS Values - return -1 for JS_EXCEPTION }
function JS_ToBool(ctx: JSContext; val:JSValueConst):Integer; cdecl; external {$IFDEF mswindows}QJSDLL{$endif};
function JS_ToInt32(ctx: JSContext; pres:pInt32; val:JSValueConst):Integer;cdecl; external {$IFDEF mswindows}QJSDLL{$endif};
function JS_ToInt64(ctx: JSContext; pres:PInt64; val:JSValueConst):Integer; cdecl; external {$IFDEF mswindows}QJSDLL{$endif};
function JS_ToIndex(ctx: JSContext; plen:PUInt64; val:JSValueConst):Integer; cdecl; external {$IFDEF mswindows}QJSDLL{$endif};
function JS_ToFloat64(ctx: JSContext; pres:PDouble; val:JSValueConst):Integer; cdecl; external {$IFDEF mswindows}QJSDLL{$endif};
{ return an exception if 'val' is a Number }
function JS_ToBigInt64(ctx: JSContext; pres:PInt64; val:JSValueConst):Integer; cdecl; external {$IFDEF mswindows}QJSDLL{$endif};
{ same as JS_ToInt64() but allow BigInt }
function JS_ToInt64Ext(ctx: JSContext; pres:PInt64; val:JSValueConst):Integer; cdecl; external {$IFDEF mswindows}QJSDLL{$endif};
function JS_NewStringLen(ctx:JSContext; str1:{$IFDEF FPC}PChar{$Else}PAnsiChar{$EndIf}; len1: size_t):JSValue; cdecl; external {$IFDEF mswindows}QJSDLL{$endif};
function JS_NewString(ctx:JSContext; str:{$IFDEF FPC}PChar{$Else}PAnsiChar{$EndIf}):JSValue; cdecl; external {$IFDEF mswindows}QJSDLL{$endif};
function JS_NewAtomString(ctx:JSContext; str:{$IFDEF FPC}PChar{$Else}PAnsiChar{$EndIf}):JSValue; cdecl; external {$IFDEF mswindows}QJSDLL{$endif};
function JS_ToString(ctx:JSContext; val:JSValueConst):JSValue; cdecl; external {$IFDEF mswindows}QJSDLL{$endif};
function JS_ToPropertyKey(ctx:JSContext; val:JSValueConst):JSValue; cdecl; external {$IFDEF mswindows}QJSDLL{$endif};
function JS_ToCStringLen2(ctx:JSContext; plen:psize_t; val1:JSValueConst; cesu8:JS_BOOL): {$IFDEF FPC}PChar{$Else}PAnsiChar{$EndIf}; cdecl; external {$IFDEF mswindows}QJSDLL{$endif};
procedure JS_FreeCString(ctx:JSContext; ptr:{$IFDEF FPC}PChar{$Else}PAnsiChar{$EndIf}); cdecl; external {$IFDEF mswindows}QJSDLL{$endif};
function JS_NewObjectProtoClass(ctx:JSContext; proto:JSValueConst; class_id:JSClassID):JSValue; cdecl; external {$IFDEF mswindows}QJSDLL{$endif};
function JS_NewObjectClass(ctx:JSContext; class_id:JSClassID):JSValue; cdecl; external {$IFDEF mswindows}QJSDLL{$endif};
function JS_NewObjectProto(ctx:JSContext; proto:JSValueConst):JSValue; cdecl; external {$IFDEF mswindows}QJSDLL{$endif};
function JS_NewObject(ctx:JSContext):JSValue; cdecl; external {$IFDEF mswindows}QJSDLL{$endif};
function JS_IsFunction(ctx:JSContext; val:JSValueConst):JS_BOOL; cdecl; external {$IFDEF mswindows}QJSDLL{$endif};
function JS_IsConstructor(ctx:JSContext; val:JSValueConst):JS_BOOL; cdecl; external {$IFDEF mswindows}QJSDLL{$endif};
function JS_SetConstructorBit(ctx:JSContext; func_obj : JSValueConst; val:JS_BOOL):JS_BOOL; cdecl; external {$IFDEF mswindows}QJSDLL{$endif};
function JS_NewArray(ctx:JSContext):JSValue; cdecl; external {$IFDEF mswindows}QJSDLL{$endif};
function JS_IsArray(ctx:JSContext; val:JSValueConst):Integer; cdecl; external {$IFDEF mswindows}QJSDLL{$endif};
function JS_GetPropertyInternal(ctx:JSContext; obj:JSValueConst; prop:JSAtom;
receiver:JSValueConst; throw_ref_error:JS_BOOL):JSValue; cdecl; external {$IFDEF mswindows}QJSDLL{$endif};
function JS_GetPropertyStr(ctx:JSContext; this_obj:JSValueConst; prop:{$IFDEF FPC}PChar{$Else}PAnsiChar{$EndIf}):JSValue; cdecl; external {$IFDEF mswindows}QJSDLL{$endif};
function JS_GetPropertyUint32(ctx:JSContext; this_obj:JSValueConst; idx:UInt32):JSValue; cdecl; external {$IFDEF mswindows}QJSDLL{$endif};
function JS_SetPropertyInternal(ctx:JSContext; this_obj:JSValueConst;
prop:JSAtom; val:JSValue; flags:Integer):Integer; cdecl; external {$IFDEF mswindows}QJSDLL{$endif};
function JS_SetPropertyUint32(ctx:JSContext; this_obj:JSValueConst; idx:UInt32; val:JSValue):Integer;cdecl; external {$IFDEF mswindows}QJSDLL{$endif};
function JS_SetPropertyInt64(ctx:JSContext; this_obj:JSValueConst; idx:Int64; val:JSValue):Integer;cdecl; external {$IFDEF mswindows}QJSDLL{$endif};
function JS_SetPropertyStr(ctx:JSContext; this_obj:JSValueConst; prop:{$IFDEF FPC}PChar{$Else}PAnsiChar{$EndIf}; val:JSValue):Integer;cdecl; external {$IFDEF mswindows}QJSDLL{$endif};
function JS_HasProperty(ctx:JSContext; this_obj:JSValueConst; prop:JSAtom):Integer;cdecl; external {$IFDEF mswindows}QJSDLL{$endif};
function JS_IsExtensible(ctx:JSContext; obj:JSValueConst):Integer;cdecl; external {$IFDEF mswindows}QJSDLL{$endif};
function JS_PreventExtensions(ctx:JSContext; obj:JSValueConst):Integer;cdecl; external {$IFDEF mswindows}QJSDLL{$endif};
function JS_DeleteProperty(ctx:JSContext; obj:JSValueConst; prop:JSAtom; flags:Integer):Integer;cdecl; external {$IFDEF mswindows}QJSDLL{$endif};
function JS_SetPrototype(ctx:JSContext; obj:JSValueConst; proto_val:JSValueConst):Integer;cdecl; external {$IFDEF mswindows}QJSDLL{$endif};
function JS_GetPrototype(ctx:JSContext; val:JSValueConst):JSValueConst;cdecl; external {$IFDEF mswindows}QJSDLL{$endif};
function JS_GetOwnPropertyNames(ctx: JSContext; ptab:PPJSPropertyEnum; plen: pUInt32; obj:JSValueConst; flags : Integer): Integer;cdecl; external {$IFDEF mswindows}QJSDLL{$endif};
function JS_GetOwnProperty(ctx: JSContext; desc : PJSPropertyDescriptor; obj : JSValueConst; prop : JSAtom): Integer; cdecl; external {$IFDEF mswindows}QJSDLL{$endif};
{ 'buf' must be zero terminated i.e. buf[buf_len] := #0. }
function JS_ParseJSON(ctx:JSContext; buf:{$IFDEF FPC}PChar{$Else}PAnsiChar{$EndIf}; buf_len:size_t; filename:{$IFDEF FPC}PChar{$Else}PAnsiChar{$EndIf}):JSValue;cdecl; external {$IFDEF mswindows}QJSDLL{$endif};
function JS_JSONStringify(ctx:JSContext; obj, replacer, space0 : JSValueConst):JSValue;cdecl; external {$IFDEF mswindows}QJSDLL{$endif};
function JS_Call(ctx:JSContext; func_obj:JSValueConst; this_obj:JSValueConst; argc:Integer; argv:PJSValueConstArr):JSValue;cdecl; external {$IFDEF mswindows}QJSDLL{$endif};
function JS_Invoke(ctx:JSContext; this_val:JSValueConst; atom:JSAtom; argc:Integer; argv:PJSValueConst):JSValue;cdecl; external {$IFDEF mswindows}QJSDLL{$endif};
function JS_CallConstructor(ctx:JSContext; func_obj:JSValueConst; argc:Integer; argv:PJSValueConst):JSValue;cdecl; external {$IFDEF mswindows}QJSDLL{$endif};
function JS_CallConstructor2(ctx:JSContext; func_obj:JSValueConst; new_target:JSValueConst; argc:Integer; argv:PJSValueConst):JSValue;cdecl; external {$IFDEF mswindows}QJSDLL{$endif};
function JS_DetectModule(const input:{$IFDEF FPC}PChar{$Else}PAnsiChar{$EndIf}; input_len : size_t):JS_BOOL;cdecl; external {$IFDEF mswindows}QJSDLL{$endif};
{ 'input' must be zero terminated i.e. buf[buf_len] := #0. }
function JS_Eval(ctx:JSContext; input:{$IFDEF FPC}PChar{$Else}PAnsiChar{$EndIf}; input_len:size_t; filename:{$IFDEF FPC}PChar{$Else}PAnsiChar{$EndIf}; eval_flags:Integer):JSValue;cdecl; external {$IFDEF mswindows}QJSDLL{$endif};
function JS_EvalFunction(ctx:JSContext; fun_obj : JSValue):JSValue;cdecl; external {$IFDEF mswindows}QJSDLL{$endif};
function JS_GetGlobalObject(ctx:JSContext):JSValue;cdecl; external {$IFDEF mswindows}QJSDLL{$endif};
function JS_IsInstanceOf(ctx:JSContext; val:JSValueConst; obj:JSValueConst):Integer;cdecl; external {$IFDEF mswindows}QJSDLL{$endif};
function JS_DefineProperty(ctx:JSContext; this_obj:JSValueConst; prop:JSAtom; val:JSValueConst; getter:JSValueConst;
setter:JSValueConst; flags:Integer):Integer;cdecl; external {$IFDEF mswindows}QJSDLL{$endif};
function JS_DefinePropertyValue(ctx:JSContext; this_obj:JSValueConst; prop:JSAtom; val:JSValue; flags:Integer):Integer;cdecl; external {$IFDEF mswindows}QJSDLL{$endif};
function JS_DefinePropertyValueUint32(ctx:JSContext; this_obj:JSValueConst; idx:UInt32; val:JSValue; flags:Integer):Integer;cdecl; external {$IFDEF mswindows}QJSDLL{$endif};
function JS_DefinePropertyValueStr(ctx:JSContext; this_obj:JSValueConst; prop:{$IFDEF FPC}PChar{$Else}PAnsiChar{$EndIf}; val:JSValue; flags:Integer):Integer;cdecl; external {$IFDEF mswindows}QJSDLL{$endif};
function JS_DefinePropertyGetSet(ctx:JSContext; this_obj:JSValueConst; prop:JSAtom; getter:JSValue; setter:JSValue;
flags:Integer):Integer;cdecl; external {$IFDEF mswindows}QJSDLL{$endif};
procedure JS_SetOpaque(obj:JSValue; opaque:pointer);cdecl; external {$IFDEF mswindows}QJSDLL{$endif};
function JS_GetOpaque(obj:JSValueConst; class_id:JSClassID):pointer;cdecl; external {$IFDEF mswindows}QJSDLL{$endif};
function JS_GetOpaque2(ctx:JSContext; obj:JSValueConst; class_id:JSClassID):pointer;cdecl; external {$IFDEF mswindows}QJSDLL{$endif};
function JS_NewArrayBuffer(ctx:JSContext; buf:pUInt8; len:size_t; free_func:PJSFreeArrayBufferDataFunc; opaque:pointer;
is_shared:JS_BOOL):JSValue;cdecl; external {$IFDEF mswindows}QJSDLL{$endif};
function JS_NewArrayBufferCopy(ctx:JSContext; buf:pUInt8; len:size_t):JSValue;cdecl; external {$IFDEF mswindows}QJSDLL{$endif};
procedure JS_DetachArrayBuffer(ctx:JSContext; obj:JSValueConst);cdecl; external {$IFDEF mswindows}QJSDLL{$endif};
function JS_GetArrayBuffer(ctx:JSContext; psize:Psize_t; obj:JSValueConst):pUInt8;cdecl; external {$IFDEF mswindows}QJSDLL{$endif};
function JS_GetTypedArrayBuffer(ctx : JSContext; obj : JSValueConst;
pbyte_offset, pbyte_length, pbytes_per_element : psize_t):JSValue;cdecl; external {$IFDEF mswindows}QJSDLL{$endif};
function JS_NewPromiseCapability(ctx:JSContext; resolving_funcs:PJSValue):JSValue;cdecl; external {$IFDEF mswindows}QJSDLL{$endif};
procedure JS_SetHostPromiseRejectionTracker(rt: JSRuntime;
cb : PJSHostPromiseRejectionTracker; opaque : Pointer); cdecl; external {$IFDEF mswindows}QJSDLL{$endif};
procedure JS_SetInterruptHandler(rt:JSRuntime; cb:PJSInterruptHandler; opaque:pointer);cdecl; external {$IFDEF mswindows}QJSDLL{$endif};
{ if can_block is TRUE, Atomics.wait() can be used }
procedure JS_SetCanBlock(rt:JSRuntime; can_block:JS_BOOL);cdecl; external {$IFDEF mswindows}QJSDLL{$endif};
{ module_normalize = NULL is allowed and invokes the default module
filename normalizer }
procedure JS_SetModuleLoaderFunc(rt:JSRuntime;
module_normalize:PJSModuleNormalizeFunc;
module_loader:PJSModuleLoaderFunc; opaque:pointer);cdecl; external {$IFDEF mswindows}QJSDLL{$endif};
{ JS Job support }
function JS_EnqueueJob(ctx:JSContext; job_func:PJSJobFunc; argc:Integer; argv:PJSValueConst):Integer;cdecl; external {$IFDEF mswindows}QJSDLL{$endif};
function JS_IsJobPending(rt:JSRuntime):JS_BOOL;cdecl; external {$IFDEF mswindows}QJSDLL{$endif};
// TODO: Check pctx if the type is right.
function JS_ExecutePendingJob(rt:JSRuntime; pctx: PPJSContext):Integer; cdecl; external {$IFDEF mswindows}QJSDLL{$endif};
{ Object Writer/Reader (currently only used to handle precompiled code) }
{ allow function/module }
function JS_WriteObject(ctx: JSContext; psize:psize_t; obj:JSValueConst; flags:Integer):pUInt8; cdecl; external {$IFDEF mswindows}QJSDLL{$endif};
function JS_ReadObject(ctx: JSContext; buf:pUInt8; buf_len:size_t; flags:Integer):JSValue; cdecl; external {$IFDEF mswindows}QJSDLL{$endif};
{
load the dependencies of the module 'obj'. Useful when JS_ReadObject()
returns a module.
}
function JS_ResolveModule(ctx: JSContext; obj : JSValueConst):Integer; cdecl; external {$IFDEF mswindows}QJSDLL{$endif};
{ C function definition }
procedure JS_SetConstructor(ctx : JSContext; func_obj, proto : JSValueConst);cdecl; external {$IFDEF mswindows}QJSDLL{$endif};
function JS_NewCFunction2(ctx: JSContext; func:PJSCFunction; name:{$IFDEF FPC}PChar{$Else}PAnsiChar{$EndIf}; length:Integer; cproto:JSCFunctionEnum;
magic:Integer):JSValue; cdecl; external {$IFDEF mswindows}QJSDLL{$endif};
function JS_NewCFunctionData(ctx: JSContext; func:PJSCFunctionData; length:Integer; magic:Integer; data_len:Integer;
data:PJSValueConst):JSValue; cdecl; external {$IFDEF mswindows}QJSDLL{$endif};
procedure JS_SetPropertyFunctionList(ctx: JSContext; obj:JSValueConst;
tab:PJSCFunctionListEntry; len:Integer); cdecl; external {$IFDEF mswindows}QJSDLL{$endif};
{ C module definition }
function JS_NewCModule(ctx: JSContext; name_str:{$IFDEF FPC}PChar{$Else}PAnsiChar{$EndIf}; func:PJSModuleInitFunc): JSModuleDef; cdecl; external {$IFDEF mswindows}QJSDLL{$endif};
{ can only be called before the module is instantiated }
function JS_AddModuleExport(ctx: JSContext; m: JSModuleDef; name_str:{$IFDEF FPC}PChar{$Else}PAnsiChar{$EndIf}):Integer; cdecl; external {$IFDEF mswindows}QJSDLL{$endif};
function JS_AddModuleExportList(ctx: JSContext; m: JSModuleDef; tab:PJSCFunctionListEntry; len:Integer):Integer; cdecl; external {$IFDEF mswindows}QJSDLL{$endif};
{ can only be called after the module is instantiated }
function JS_SetModuleExport(ctx: JSContext; m: JSModuleDef; export_name:{$IFDEF FPC}PChar{$Else}PAnsiChar{$EndIf}; val:JSValue):Integer; cdecl; external {$IFDEF mswindows}QJSDLL{$endif};
function JS_SetModuleExportList(ctx: JSContext; m: JSModuleDef; tab:PJSCFunctionListEntry; len:Integer):Integer; cdecl; external {$IFDEF mswindows}QJSDLL{$endif};
{ return the import.meta object of a module }
function JS_GetImportMeta(ctx: JSContext; m: JSModuleDef) : JSValue; cdecl; external {$IFDEF mswindows}QJSDLL{$endif};
function JS_GetModuleName(ctx: JSContext; m: JSModuleDef) : JSAtom; cdecl; external {$IFDEF mswindows}QJSDLL{$endif};
{ QuickJS libc }
function js_init_module_std(ctx: JSContext; module_name:{$IFDEF FPC}PChar{$Else}PAnsiChar{$EndIf}):JSModuleDef;cdecl; external {$IFDEF mswindows}QJSDLL{$endif};
function js_init_module_os(ctx: JSContext; module_name:{$IFDEF FPC}PChar{$Else}PAnsiChar{$EndIf}):JSModuleDef;cdecl; external {$IFDEF mswindows}QJSDLL{$endif};
procedure js_std_add_helpers(ctx : JSContext; argc : Integer; argv : Pointer);cdecl; external {$IFDEF mswindows}QJSDLL{$endif};
procedure js_std_loop(ctx : JSContext); cdecl; external {$IFDEF mswindows}QJSDLL{$endif};
procedure js_std_free_handlers(rt:JSRuntime);cdecl; external {$IFDEF mswindows}QJSDLL{$endif};
procedure js_std_dump_error(ctx:JSContext);cdecl; external {$IFDEF mswindows}QJSDLL{$endif};
function js_load_file(ctx:JSContext; pbuf_len: psize_t; filename:{$IFDEF FPC}PChar{$Else}PAnsiChar{$EndIf}): Pointer;cdecl; external {$IFDEF mswindows}QJSDLL{$endif};
function js_module_loader(ctx:JSContext; module_name:{$IFDEF FPC}PChar{$Else}PAnsiChar{$EndIf}; opaque:pointer):JSModuleDef;cdecl; external {$IFDEF mswindows}QJSDLL{$endif};
procedure js_std_eval_binary(ctx : JSContext; buf : Pointer; buf_len : size_t; flags : Integer); cdecl; external {$IFDEF mswindows}QJSDLL{$endif};
function js_module_set_import_meta(ctx : JSContext; func_val : JSValueConst; use_realpath, is_main : JS_BOOL) : Integer; cdecl; external {$IFDEF mswindows}QJSDLL{$endif};
procedure js_std_promise_rejection_tracker(ctx : JSContext;
promise, reason : JSValueConst; is_handled : JS_BOOL; opaque : Pointer); cdecl; external {$IFDEF mswindows}QJSDLL{$endif};
{ internal implementations}
function JS_VALUE_GET_TAG(v : JSValue): Int64;
{ same as JS_VALUE_GET_TAG, but return JS_TAG_FLOAT64 with NaN boxing }
function JS_VALUE_GET_NORM_TAG(v : JSValue): Int64;
function JS_VALUE_IS_NAN(v : JSValue) : JS_BOOL; inline;
function JS_VALUE_GET_INT(v : JSValue): Integer;
function JS_VALUE_GET_BOOL(v : JSValue): Boolean;
function JS_VALUE_GET_FLOAT64(v : JSValue): Double;
function JS_VALUE_GET_PTR(v : JSValue): Pointer;
function JS_MKVAL(tag : Int64; val : Int32): JSValue;
function JS_MKPTR(tag : Int64; ptr : Pointer): JSValue;
function JS_TAG_IS_FLOAT64(tag : Int64): Boolean; inline;
{$IfNDef JS_NAN_BOXING}
function JS_NAN : JSValue;
{$EndIf}
function __JS_NewFloat64({%H-}ctx : JSContext; d : Double): JSValue;
function JS_VALUE_IS_BOTH_INT(v1, v2 : JSValue): Boolean;
function JS_VALUE_IS_BOTH_FLOAT(v1, v2 : JSValue): Boolean;
function JS_VALUE_GET_OBJ(v : JSValue): JSObject;
function JS_VALUE_GET_STRING(v : JSValue): JSString;
function JS_VALUE_HAS_REF_COUNT(v : JSValue): Boolean;
{ special values }
function JS_NULL : JSValue;
function JS_UNDEFINED : JSValue;
function JS_FALSE : JSValue;
function JS_TRUE : JSValue;
function JS_EXCEPTION : JSValue;
function JS_UNINITIALIZED : JSValue;
{ value handling }
function JS_NewBool({%H-}ctx : JSContext; val : JS_BOOL): JSValue; inline;
function JS_NewInt32( {%H-}ctx : JSContext; val : Int32): JSValue; inline;
function JS_NewCatchOffset( {%H-}ctx : JSContext; val : Int32): JSValue; inline;
function JS_NewFloat64(ctx : JSContext; d : Double): JSValue;
function JS_IsBigInt(v : JSValueConst): JS_BOOL; inline;
function JS_IsBigFloat(v : JSValueConst): JS_BOOL; inline;
function JS_IsBigDecimal(v : JSValueConst): JS_BOOL; inline;
function JS_IsBool(v : JSValueConst): JS_BOOL; inline;
function JS_IsNull(v : JSValueConst): JS_BOOL; inline;
function JS_IsUndefined(v : JSValueConst): JS_BOOL; inline;
function JS_IsException(v : JSValueConst): JS_BOOL; inline;
function JS_IsUninitialized(v : JSValueConst): JS_BOOL; inline;
function JS_IsString(v : JSValueConst): JS_BOOL; inline;
function JS_IsNumber(v : JSValueConst): JS_BOOL; inline;
function JS_IsSymbol(v : JSValueConst): JS_BOOL; inline;
function JS_IsObject(v : JSValueConst): JS_BOOL; inline;
procedure JS_FreeValue(ctx : JSContext; v : JSValue); inline;
procedure JS_FreeValueRT(rt : JSRuntime; v : JSValue); inline;
function JS_DupValue({%H-}ctx : JSContext; v : JSValueConst) : JSValue; inline;
function JS_DupValueRT({%H-}rt : JSRuntime; v : JSValueConst) : JSValue; inline;
function JS_ToUint32(ctx : JSContext; pres : pUInt32; val : JSValueConst): Integer; inline;
function JS_ToCStringLen(ctx : JSContext; plen : psize_t; val : JSValueConst): {$IFDEF FPC}PChar{$Else}PAnsiChar{$EndIf}; inline;
function JS_ToCString(ctx : JSContext; val : JSValueConst): {$IFDEF FPC}PChar{$Else}PAnsiChar{$EndIf}; inline;
function JS_GetProperty(ctx : JSContext; this_obj : JSValueConst; prop : JSAtom): JSValue; inline;
function JS_SetProperty(ctx : JSContext; this_obj : JSValueConst; prop : JSAtom; val : JSValue): Integer; inline;
{ C function definition }
function JS_NewCFunction(ctx : JSContext; func : PJSCFunction; name : {$IFDEF FPC}PChar{$Else}PAnsiChar{$EndIf}; length : Integer): JSValue; inline;
function JS_NewCFunctionMagic(ctx : JSContext; func : PJSCFunctionMagic; name : {$IFDEF FPC}PChar{$Else}PAnsiChar{$EndIf}; length : Integer;
cproto : JSCFunctionEnum; magic : Integer): JSValue; inline;
{ C property definition }
function JS_CFUNC_DEF(name : {$IFDEF FPC}PChar{$Else}PAnsiChar{$EndIf}; length : Integer; func : JSCFunction) : JSCFunctionListEntry;
function JS_CFUNC_MAGIC_DEF(name : {$IFDEF FPC}PChar{$Else}PAnsiChar{$EndIf}; length : Integer; func : JSCFunctionMagic; magic : Int16) : JSCFunctionListEntry;
function JS_CFUNC_SPECIAL_DEF(name : {$IFDEF FPC}PChar{$Else}PAnsiChar{$EndIf}; length : Integer; cproto : JSCFunctionEnum ; func : f_f_func) : JSCFunctionListEntry; overload;
function JS_CFUNC_SPECIAL_DEF(name : {$IFDEF FPC}PChar{$Else}PAnsiChar{$EndIf}; length : Integer; cproto : JSCFunctionEnum ; func : f_f_f_func) : JSCFunctionListEntry; overload;
function JS_ITERATOR_NEXT_DEF(name : {$IFDEF FPC}PChar{$Else}PAnsiChar{$EndIf}; length : Integer; iterator_next : iterator_next_func; magic : Int16) : JSCFunctionListEntry;
function JS_CGETSET_DEF(name : {$IFDEF FPC}PChar{$Else}PAnsiChar{$EndIf}; fgetter : Getter_func; fsetter : Setter_func) : JSCFunctionListEntry;
function JS_CGETSET_MAGIC_DEF(name : {$IFDEF FPC}PChar{$Else}PAnsiChar{$EndIf}; fgetter_magic : getter_magic_func; fsetter_magic : setter_magic_func; magic : Int16) : JSCFunctionListEntry;
function JS_PROP_STRING_DEF(name : {$IFDEF FPC}PChar{$Else}PAnsiChar{$EndIf}; val : {$IFDEF FPC}PChar{$Else}PAnsiChar{$EndIf}; prop_flags : UInt8) : JSCFunctionListEntry;
function JS_PROP_INT32_DEF(name : {$IFDEF FPC}PChar{$Else}PAnsiChar{$EndIf}; val : Int32; prop_flags : UInt8) : JSCFunctionListEntry;
function JS_PROP_INT64_DEF(name : {$IFDEF FPC}PChar{$Else}PAnsiChar{$EndIf}; val : Int64; prop_flags : UInt8) : JSCFunctionListEntry;
function JS_PROP_DOUBLE_DEF(name : {$IFDEF FPC}PChar{$Else}PAnsiChar{$EndIf}; val : Double; prop_flags : UInt8) : JSCFunctionListEntry;
function JS_PROP_UNDEFINED_DEF(name : {$IFDEF FPC}PChar{$Else}PAnsiChar{$EndIf}; prop_flags : UInt8) : JSCFunctionListEntry;
function JS_OBJECT_DEF(name : {$IFDEF FPC}PChar{$Else}PAnsiChar{$EndIf}; tab : PJSCFunctionListEntry; length : Integer; prop_flags : UInt8) : JSCFunctionListEntry;
function JS_ALIAS_DEF(name, from : {$IFDEF FPC}PChar{$Else}PAnsiChar{$EndIf}) : JSCFunctionListEntry;
function JS_ALIAS_BASE_DEF(name, from : {$IFDEF FPC}PChar{$Else}PAnsiChar{$EndIf}; base : Integer) : JSCFunctionListEntry;
var
OldFPUMask : TFPUExceptionMask;
implementation
{$If Defined(JS_NAN_BOXING)}
function JS_VALUE_GET_TAG(v : JSValue): Int64;
begin
Result := Integer(v shr 32);
end;
function JS_VALUE_GET_INT(v : JSValue): Integer;
begin
Result := Integer(v);
end;
function JS_VALUE_GET_BOOL(v : JSValue): Boolean;
begin
Result := Boolean(v);
end;
function JS_VALUE_GET_PTR(v : JSValue): Pointer;
begin
Result := {%H-}Pointer(v); // TODO: check if this works the right way.
end;
function JS_MKVAL(tag : Int64; val : Int32): JSValue;
begin
Result := tag shl 32 or val;
end;
function JS_MKPTR(tag : Int64; ptr : Pointer): JSValue;
begin
Result := JSValue((tag shl 32) or UIntPtr(ptr));
end;
function JS_VALUE_GET_FLOAT64(v : JSValue): Double;
type
rec = record
case Byte of
0 : (v : JSValue);
1 : (d : Double);
end;
var
u : rec;
begin
u.v := v;
u.v {$IfDef FPC}+={$Else} := u.v +{$EndIf} UInt64(JS_FLOAT64_TAG_ADDEND shl 32);
Result := u.d;
end;
function __JS_NewFloat64({%H-}ctx : JSContext; d : Double): JSValue;
type
rec = record
case Byte of
0 : (d : Double);
1 : (u64 : UInt64);
end;
var
u : rec;
v : JSValue;
begin
u.d := d;
{ normalize NaN }
if ((u.u64 and $7fffffffffffffff) > $7ff0000000000000) then
v := UInt64(JS_NAN)
else
v := u.u64 - UInt64(JS_FLOAT64_TAG_ADDEND shl 32);
Result := v;
end;
function JS_TAG_IS_FLOAT64(tag : Int64): Boolean; inline;
begin
Result := Boolean( UInt64((tag) - JS_TAG_FIRST) >= (JS_TAG_FLOAT64 - JS_TAG_FIRST) );
end;
{ same as JS_VALUE_GET_TAG, but return JS_TAG_FLOAT64 with NaN boxing }
function JS_VALUE_GET_NORM_TAG(v : JSValue): Int64;
var
tag : UInt32;
begin
tag := JS_VALUE_GET_TAG(v);
if JS_TAG_IS_FLOAT64(tag) then
Result := JS_TAG_FLOAT64
else
Result := tag;
end;
function JS_VALUE_IS_NAN(v : JSValue) : JS_BOOL; inline;
begin
Result := (JS_VALUE_GET_TAG(v) = (JS_NAN shr 32));
end;
{$else}
function JS_VALUE_GET_TAG(v : JSValue): Int64;
begin
Result := v.tag;
end;
{ same as JS_VALUE_GET_TAG, but return JS_TAG_FLOAT64 with NaN boxing }
function JS_VALUE_GET_NORM_TAG(v : JSValue): Int64;
begin
Result := JS_VALUE_GET_TAG(v);
end;
function JS_VALUE_GET_INT(v : JSValue): Integer;
begin
Result := v.u.&int32;
end;
function JS_VALUE_GET_BOOL(v : JSValue): Boolean;
begin
Result := Boolean(v.u.&int32);
end;
function JS_VALUE_GET_FLOAT64(v : JSValue): Double;
begin
Result := v.u.float64;
end;
function JS_VALUE_GET_PTR(v : JSValue): Pointer;
begin
Result := v.u.Ptr;
end;
function JS_MKVAL(tag : Int64; val : Int32): JSValue;
begin
Result.u.&int32 := val;
Result.tag := tag;
end;
function JS_MKPTR(tag : Int64; ptr : Pointer): JSValue;
begin
Result.u.Ptr := ptr;
Result.tag := tag;
end;