From 89c369f7fe6000651e8181ae7edbce84f92e4480 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=98=BF=E6=B5=B7?= <978868928@qq.com> Date: Thu, 25 Mar 2021 16:40:35 +0800 Subject: [PATCH 1/2] Sync with "2020-11-08" update --- quickjs.pas | 48 ++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 44 insertions(+), 4 deletions(-) diff --git a/quickjs.pas b/quickjs.pas index 32d7952..56327b3 100644 --- a/quickjs.pas +++ b/quickjs.pas @@ -54,7 +54,7 @@ interface { QuickJS Constants } {===============================================================================} const - QJS_VERSION = '2020-04-12'; + QJS_VERSION = '2020-11-08'; const { all tags with a reference count are negative } JS_TAG_FIRST = -11; { first negative tag } @@ -136,9 +136,17 @@ interface JS_WRITE_OBJ_BYTECODE = (1 shl 0); { allow function/module } JS_WRITE_OBJ_BSWAP = (1 shl 1); { byte swapped output } + JS_WRITE_OBJ_SAB = (1 shl 2); { allow SharedArrayBuffer } + JS_WRITE_OBJ_REFERENCE = (1 shl 3); { allow object references to + encode arbitrary object + graph } + JS_READ_OBJ_BYTECODE = (1 shl 0); { allow function/module } JS_READ_OBJ_ROM_DATA = (1 shl 1); { avoid duplicating 'buf' data } + JS_READ_OBJ_SAB = (1 shl 2); { allow SharedArrayBuffer } + JS_READ_OBJ_REFERENCE = (1 shl 3); { allow object references } + { C property definition } JS_DEF_CFUNC = 0; JS_DEF_CGETSET = 1; @@ -180,6 +188,10 @@ interface { C Call Flags } JS_CALL_FLAG_CONSTRUCTOR = (1 shl 0); + + JS_PARSE_JSON_EXT = (1 shl 0); { allow extended JSON } + + JS_ATOM_NULL = 0; { atom support } {===============================================================================} {===============================================================================} @@ -203,6 +215,9 @@ interface psize_t = ^size_t; {$endif} + ppUInt8 = ^pUInt8; + pppUInt8 = ^ppUInt8; + JS_BOOL = Boolean; JSRuntime = Pointer; @@ -282,6 +297,14 @@ JSMallocFunctions = record end; PJSMallocFunctions = ^JSMallocFunctions; + JSSharedArrayBufferFunctions = record + sab_alloc : function (opaque : Pointer; size : size_t) : Pointer; cdecl; + sab_free : procedure (opaque : Pointer; Ptr : Pointer); cdecl; + sab_dup : procedure (opaque : Pointer; Ptr : Pointer); cdecl; + sab_opaque : Pointer; + end; + PJSSharedArrayBufferFunctions = ^JSSharedArrayBufferFunctions; + PJSMemoryUsage = ^JSMemoryUsage; JSMemoryUsage = record malloc_size, malloc_limit, memory_used_size, @@ -655,13 +678,14 @@ JSCFunctionListEntry = record 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_GetPrototype(ctx:JSContext; val:JSValueConst):JSValue;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_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_ParseJSON2(ctx:JSContext; buf:{$IFDEF FPC}PChar{$Else}PAnsiChar{$EndIf}; buf_len:size_t; filename:{$IFDEF FPC}PChar{$Else}PAnsiChar{$EndIf};flags:Integer):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}; @@ -670,7 +694,9 @@ JSCFunctionListEntry = record 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}; + + { same as JS_Eval() but with an explicit 'this_obj' parameter } + function JS_EvalThis(ctx:JSContext; val:JSValueConst;const 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_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; @@ -697,6 +723,9 @@ JSCFunctionListEntry = record function JS_NewPromiseCapability(ctx:JSContext; resolving_funcs:PJSValue):JSValue;cdecl; external {$IFDEF mswindows}QJSDLL{$endif}; + procedure JS_SetSharedArrayBufferFunctions(rt: JSRuntime; + const sf : PJSSharedArrayBufferFunctions); cdecl; external {$IFDEF mswindows}QJSDLL{$endif}; + procedure JS_SetHostPromiseRejectionTracker(rt: JSRuntime; cb : PJSHostPromiseRejectionTracker; opaque : Pointer); cdecl; external {$IFDEF mswindows}QJSDLL{$endif}; @@ -704,6 +733,8 @@ JSCFunctionListEntry = record { 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}; + { set the [IsHTMLDDA] internal slot } + procedure JS_SetIsHTMLDDA(ctx:JSContext; obj : JSValueConst);cdecl; external {$IFDEF mswindows}QJSDLL{$endif}; { module_normalize = NULL is allowed and invokes the default module filename normalizer } @@ -722,13 +753,22 @@ JSCFunctionListEntry = record { allow function/module } function JS_WriteObject(ctx: JSContext; psize:psize_t; obj:JSValueConst; flags:Integer):pUInt8; cdecl; external {$IFDEF mswindows}QJSDLL{$endif}; + function JS_WriteObject2(ctx: JSContext; psize:psize_t; obj:JSValueConst; flags:Integer; psab_tab:pppUInt8; psab_tab_len:psize_t):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}; + { instantiate and evaluate a bytecode function. Only used when + reading a script or module with JS_ReadObject() } + function JS_EvalFunction(ctx:JSContext; fun_obj : JSValue):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}; + { only exported for os.Worker() } + function JS_GetScriptOrModuleName(ctx: JSContext; n_stack_levels: Integer) : JSAtom; cdecl; external {$IFDEF mswindows}QJSDLL{$endif}; + { only exported for os.Worker() } + function JS_RunModule(ctx:JSContext; basename:{$IFDEF FPC}PChar{$Else}PAnsiChar{$EndIf}; filename:{$IFDEF FPC}PChar{$Else}PAnsiChar{$EndIf}):JSModuleDef;cdecl; external {$IFDEF mswindows}QJSDLL{$endif}; + { C function definition } procedure JS_SetConstructor(ctx : JSContext; func_obj, proto : JSValueConst);cdecl; external {$IFDEF mswindows}QJSDLL{$endif}; From 0d3eca329c5925ac1286d0928dc4352dcfafc524 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=98=BF=E6=B5=B7?= <978868928@qq.com> Date: Thu, 25 Mar 2021 20:04:13 +0800 Subject: [PATCH 2/2] add js_std_init_handlers and js_std_set_worker_new_context_func functions --- QJSPascalDemo/Demo.lpr | 3 +++ quickjs.pas | 4 ++++ 2 files changed, 7 insertions(+) diff --git a/QJSPascalDemo/Demo.lpr b/QJSPascalDemo/Demo.lpr index 8e80b3f..7cd74db 100644 --- a/QJSPascalDemo/Demo.lpr +++ b/QJSPascalDemo/Demo.lpr @@ -107,6 +107,9 @@ procedure RunCode(); rt := JS_NewRuntime; if Assigned(rt) then begin + + js_std_init_handlers(rt); + ctx := JS_NewContext(rt); if Assigned(rt) then begin diff --git a/quickjs.pas b/quickjs.pas index 56327b3..bc0f781 100644 --- a/quickjs.pas +++ b/quickjs.pas @@ -802,6 +802,7 @@ JSCFunctionListEntry = record 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_init_handlers(rt:JSRuntime);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}; @@ -811,6 +812,9 @@ JSCFunctionListEntry = record procedure js_std_promise_rejection_tracker(ctx : JSContext; promise, reason : JSValueConst; is_handled : JS_BOOL; opaque : Pointer); cdecl; external {$IFDEF mswindows}QJSDLL{$endif}; + // TODO: Check pctx if the type is right. + procedure js_std_set_worker_new_context_func(pctx : PPJSContext); cdecl; external {$IFDEF mswindows}QJSDLL{$endif}; + { internal implementations} function JS_VALUE_GET_TAG(v : JSValue): Int64;