├── .github └── FUNDING.yml ├── .gitignore ├── LICENSE ├── LibQJS ├── IOS │ ├── Arch │ │ ├── arm64 │ │ │ └── libquickjs.a │ │ ├── arm64e │ │ │ └── libquickjs.a │ │ ├── armv7 │ │ │ └── libquickjs.a │ │ ├── i386 │ │ │ └── libquickjs.a │ │ └── x86_64 │ │ │ └── libquickjs.a │ └── Universal │ │ └── libquickjs.a ├── Linux │ └── x86_64 │ │ └── libquickjs.a ├── OSX │ ├── Arch │ │ ├── i386 │ │ │ └── libquickjs.a │ │ └── x86_64 │ │ │ └── libquickjs.a │ └── Universal │ │ └── libquickjs.a └── Windows │ ├── i386 │ └── quickjs32.dll │ └── x86_64 │ └── quickjs64.dll ├── QJSDelphiDemo ├── DelphiDemo.dpr ├── DelphiDemo.dproj ├── DelphiDemo.res ├── examples │ ├── ApiHook.js │ ├── fib_module.js │ ├── hello_module.js │ └── pi.js └── quickjsdemo.pas ├── QJSPascalDemo ├── Demo.lpi ├── Demo.lpr ├── examples │ ├── ApiHook.js │ ├── fib_module.js │ ├── hello_module.js │ └── pi.js ├── quickjsdemo.pas └── rawexecution.pas ├── README.md └── quickjs.pas /.github/FUNDING.yml: -------------------------------------------------------------------------------- 1 | github: [Coldzer0] 2 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Delphi compiler-generated binaries (safe to delete) 2 | *.exe 3 | *.bpl 4 | *.bpi 5 | *.dcp 6 | *.so 7 | *.apk 8 | *.drc 9 | *.map 10 | *.dres 11 | *.rsm 12 | *.tds 13 | *.dcu 14 | *.lib 15 | *.o 16 | *.ocx 17 | 18 | # Delphi autogenerated files (duplicated info) 19 | *.cfg 20 | *.hpp 21 | *Resource.rc 22 | 23 | # Delphi local files (user-specific info) 24 | *.local 25 | *.identcache 26 | *.projdata 27 | *.tvsconfig 28 | *.dsk 29 | 30 | # Delphi history and backups 31 | __history/ 32 | __recovery/ 33 | *.~* 34 | 35 | # Castalia statistics file (since XE7 Castalia is distributed with Delphi) 36 | *.stat 37 | 38 | # FPC 39 | backup/ 40 | lib/ 41 | 42 | Demo 43 | .DS_Store 44 | .vscode 45 | *.lps 46 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2019 Coldzer0 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /LibQJS/IOS/Arch/arm64/libquickjs.a: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Coldzer0/QuickJS-Pascal/75c1f2be24ce83554f79bc1952b9dd376ebae7c6/LibQJS/IOS/Arch/arm64/libquickjs.a -------------------------------------------------------------------------------- /LibQJS/IOS/Arch/arm64e/libquickjs.a: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Coldzer0/QuickJS-Pascal/75c1f2be24ce83554f79bc1952b9dd376ebae7c6/LibQJS/IOS/Arch/arm64e/libquickjs.a -------------------------------------------------------------------------------- /LibQJS/IOS/Arch/armv7/libquickjs.a: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Coldzer0/QuickJS-Pascal/75c1f2be24ce83554f79bc1952b9dd376ebae7c6/LibQJS/IOS/Arch/armv7/libquickjs.a -------------------------------------------------------------------------------- /LibQJS/IOS/Arch/i386/libquickjs.a: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Coldzer0/QuickJS-Pascal/75c1f2be24ce83554f79bc1952b9dd376ebae7c6/LibQJS/IOS/Arch/i386/libquickjs.a -------------------------------------------------------------------------------- /LibQJS/IOS/Arch/x86_64/libquickjs.a: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Coldzer0/QuickJS-Pascal/75c1f2be24ce83554f79bc1952b9dd376ebae7c6/LibQJS/IOS/Arch/x86_64/libquickjs.a -------------------------------------------------------------------------------- /LibQJS/IOS/Universal/libquickjs.a: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Coldzer0/QuickJS-Pascal/75c1f2be24ce83554f79bc1952b9dd376ebae7c6/LibQJS/IOS/Universal/libquickjs.a -------------------------------------------------------------------------------- /LibQJS/Linux/x86_64/libquickjs.a: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Coldzer0/QuickJS-Pascal/75c1f2be24ce83554f79bc1952b9dd376ebae7c6/LibQJS/Linux/x86_64/libquickjs.a -------------------------------------------------------------------------------- /LibQJS/OSX/Arch/i386/libquickjs.a: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Coldzer0/QuickJS-Pascal/75c1f2be24ce83554f79bc1952b9dd376ebae7c6/LibQJS/OSX/Arch/i386/libquickjs.a -------------------------------------------------------------------------------- /LibQJS/OSX/Arch/x86_64/libquickjs.a: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Coldzer0/QuickJS-Pascal/75c1f2be24ce83554f79bc1952b9dd376ebae7c6/LibQJS/OSX/Arch/x86_64/libquickjs.a -------------------------------------------------------------------------------- /LibQJS/OSX/Universal/libquickjs.a: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Coldzer0/QuickJS-Pascal/75c1f2be24ce83554f79bc1952b9dd376ebae7c6/LibQJS/OSX/Universal/libquickjs.a -------------------------------------------------------------------------------- /LibQJS/Windows/i386/quickjs32.dll: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Coldzer0/QuickJS-Pascal/75c1f2be24ce83554f79bc1952b9dd376ebae7c6/LibQJS/Windows/i386/quickjs32.dll -------------------------------------------------------------------------------- /LibQJS/Windows/x86_64/quickjs64.dll: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Coldzer0/QuickJS-Pascal/75c1f2be24ce83554f79bc1952b9dd376ebae7c6/LibQJS/Windows/x86_64/quickjs64.dll -------------------------------------------------------------------------------- /QJSDelphiDemo/DelphiDemo.dpr: -------------------------------------------------------------------------------- 1 | { 2 | Delphi Demo for QuickJS Engine \ 3 | Tested On Win x32 - x64 >> with "Delphi 10.3". 4 | 5 | Copyright(c) 2019 Coldzer0 . 6 | 7 | License: MIT . 8 | } 9 | 10 | program DelphiDemo; 11 | 12 | uses 13 | {$IFDEF mswindows} 14 | windows, 15 | {$ENDIF } 16 | quickjs, 17 | QuickJSDemo; 18 | 19 | function eval_buf(ctx : JSContext; Buf : PAnsiChar; buf_len : Integer; filename : PAnsiChar; eval_flags : Integer): Integer; 20 | var 21 | val : JSValue; 22 | begin 23 | val := JS_Eval(ctx, buf, buf_len, filename, eval_flags); 24 | if JS_IsException(val) then 25 | begin 26 | js_std_dump_error(ctx); 27 | Result := -1; 28 | end 29 | else 30 | Result := 0; 31 | JS_FreeValue(ctx, val); 32 | end; 33 | 34 | function eval_file(ctx : JSContext; filename : PAnsiChar; eval_flags : Integer): Integer; 35 | var 36 | buf_len : size_t; 37 | Buf : Pointer; 38 | begin 39 | buf := js_load_file(ctx, @buf_len, filename); 40 | if not Assigned(buf) then 41 | begin 42 | Writeln('Error While Loading : ',filename); 43 | exit(1); 44 | end; 45 | Result := eval_buf(ctx, buf, buf_len, filename, eval_flags); 46 | js_free(ctx, buf); 47 | end; 48 | 49 | 50 | function logme(ctx : JSContext; {%H-}this_val : JSValueConst; argc : Integer; argv : PJSValueConstArr): JSValue; cdecl; 51 | var 52 | i : Integer; 53 | str : PAnsiChar; 54 | begin 55 | for i := 0 to Pred(argc) do 56 | begin 57 | if i <> 0 then 58 | write(' '); 59 | str := JS_ToCString(ctx, argv[i]); 60 | if not Assigned(str) then 61 | exit(JS_EXCEPTION); 62 | Write(str); 63 | JS_FreeCString(ctx, str); 64 | end; 65 | Writeln; 66 | Result := JS_UNDEFINED; 67 | end; 68 | 69 | procedure RunCode(); 70 | var 71 | rt : JSRuntime; 72 | ctx : JSContext; 73 | m : JSModuleDef; 74 | global : JSValue; 75 | filename : PAnsiChar; 76 | const 77 | std_hepler : PAnsiChar = 78 | 'import * as std from ''std'';'#10+ 79 | 'import * as os from ''os'';'#10+ 80 | 'import * as Cmu from ''Cmu'';'#10+ // Our Custom Module. 81 | 'globalThis.std = std;'#10+ 82 | 'globalThis.os = os;'#10+ 83 | 'globalThis.Cmu = Cmu;'#10; 84 | begin 85 | rt := JS_NewRuntime; 86 | if Assigned(rt) then 87 | begin 88 | ctx := JS_NewContext(rt); 89 | if Assigned(rt) then 90 | begin 91 | // ES6 Module loader. 92 | JS_SetModuleLoaderFunc(rt, nil, @js_module_loader, nil); 93 | 94 | js_std_add_helpers(ctx,0,nil); 95 | js_init_module_std(ctx, 'std'); 96 | js_init_module_os(ctx, 'os'); 97 | 98 | { 99 | Functions init order is important \ 100 | cuz i init the class and it's obj's and constructor in \ 101 | RegisterNativeClass then i just point the Module constructor to the same one. 102 | } 103 | 104 | // Register with global object directly . 105 | RegisterNativeClass(ctx); 106 | 107 | // Register with module 108 | m := JS_NewCModule(ctx, PAnsiChar('Cmu'), @Emu_init); 109 | JS_AddModuleExport(ctx,m, PAnsiChar('ApiHook')); 110 | 111 | eval_buf(ctx, std_hepler, {$IFDEF FPC}strlen{$ELSE}lstrlenA{$ENDIF}(std_hepler), '', JS_EVAL_TYPE_MODULE); 112 | 113 | 114 | global := JS_GetGlobalObject(ctx); 115 | 116 | // Define a function in the global context. 117 | JS_SetPropertyStr(ctx,global,'log',JS_NewCFunction(ctx, @logme, 'log', 1)); 118 | 119 | JS_FreeValue(ctx, global); 120 | 121 | if ParamCount >= 1 then 122 | begin 123 | filename :=PAnsiChar(AnsiString(ParamStr(1))); 124 | eval_file(ctx,filename,JS_EVAL_TYPE_GLOBAL or JS_EVAL_TYPE_MODULE); 125 | end; 126 | 127 | js_std_loop(ctx); 128 | 129 | js_std_free_handlers(rt); 130 | JS_FreeContext(ctx); 131 | end; 132 | JS_FreeRuntime(rt); 133 | end; 134 | Writeln; 135 | end; 136 | 137 | 138 | 139 | begin 140 | RunCode; 141 | end. 142 | 143 | 144 | -------------------------------------------------------------------------------- /QJSDelphiDemo/DelphiDemo.dproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | {30F595B8-E044-46D7-B409-59DC51C518E1} 4 | 18.6 5 | None 6 | DelphiDemo.dpr 7 | True 8 | Debug 9 | Win32 10 | 3 11 | Console 12 | 13 | 14 | true 15 | 16 | 17 | true 18 | Base 19 | true 20 | 21 | 22 | true 23 | Base 24 | true 25 | 26 | 27 | true 28 | Base 29 | true 30 | 31 | 32 | true 33 | Base 34 | true 35 | 36 | 37 | true 38 | Base 39 | true 40 | 41 | 42 | true 43 | Base 44 | true 45 | 46 | 47 | true 48 | Base 49 | true 50 | 51 | 52 | true 53 | Base 54 | true 55 | 56 | 57 | true 58 | Base 59 | true 60 | 61 | 62 | true 63 | Cfg_1 64 | true 65 | true 66 | 67 | 68 | true 69 | Cfg_1 70 | true 71 | true 72 | 73 | 74 | true 75 | Base 76 | true 77 | 78 | 79 | true 80 | Cfg_2 81 | true 82 | true 83 | 84 | 85 | . 86 | .\$(Platform)\$(Config) 87 | false 88 | false 89 | false 90 | false 91 | false 92 | System;Xml;Data;Datasnap;Web;Soap;$(DCC_Namespace) 93 | DelphiDemo 94 | 95 | 96 | DBXSqliteDriver;RESTComponents;DBXInterBaseDriver;emsclientfiredac;DataSnapFireDAC;tethering;bindcompfmx;FmxTeeUI;FireDACIBDriver;fmx;FireDACDBXDriver;dbexpress;IndyCore;dsnap;DataSnapCommon;emsclient;FireDACCommon;RESTBackendComponents;soapserver;bindengine;CloudService;FireDACCommonDriver;DataSnapClient;inet;bindcompdbx;IndyIPCommon;IndyIPServer;IndySystem;fmxFireDAC;FireDAC;FireDACSqliteDriver;ibmonitor;FMXTee;soaprtl;DbxCommonDriver;ibxpress;xmlrtl;soapmidas;DataSnapNativeClient;ibxbindings;rtl;DbxClientDriver;FireDACDSDriver;CustomIPTransport;bindcomp;IndyIPClient;dbxcds;dsnapxml;DataSnapProviderClient;dbrtl;IndyProtocols;$(DCC_UsePackage) 97 | $(BDS)\bin\Artwork\Android\FM_LauncherIcon_36x36.png 98 | $(BDS)\bin\Artwork\Android\FM_LauncherIcon_48x48.png 99 | $(BDS)\bin\Artwork\Android\FM_LauncherIcon_72x72.png 100 | $(BDS)\bin\Artwork\Android\FM_LauncherIcon_96x96.png 101 | $(BDS)\bin\Artwork\Android\FM_LauncherIcon_144x144.png 102 | $(BDS)\bin\Artwork\Android\FM_SplashImage_426x320.png 103 | $(BDS)\bin\Artwork\Android\FM_SplashImage_470x320.png 104 | $(BDS)\bin\Artwork\Android\FM_SplashImage_640x480.png 105 | $(BDS)\bin\Artwork\Android\FM_SplashImage_960x720.png 106 | android-support-v4.dex.jar;cloud-messaging.dex.jar;fmx.dex.jar;google-analytics-v2.dex.jar;google-play-billing.dex.jar;google-play-licensing.dex.jar;google-play-services-ads-7.0.0.dex.jar;google-play-services-analytics-7.0.0.dex.jar;google-play-services-base-7.0.0.dex.jar;google-play-services-gcm-7.0.0.dex.jar;google-play-services-identity-7.0.0.dex.jar;google-play-services-maps-7.0.0.dex.jar;google-play-services-panorama-7.0.0.dex.jar;google-play-services-plus-7.0.0.dex.jar;google-play-services-wallet-7.0.0.dex.jar 107 | 108 | 109 | DBXSqliteDriver;RESTComponents;DBXInterBaseDriver;emsclientfiredac;DataSnapFireDAC;tethering;bindcompfmx;FmxTeeUI;FireDACIBDriver;fmx;FireDACDBXDriver;dbexpress;IndyCore;dsnap;DataSnapCommon;emsclient;FireDACCommon;RESTBackendComponents;soapserver;bindengine;CloudService;FireDACCommonDriver;DataSnapClient;inet;bindcompdbx;IndyIPCommon;IndyIPServer;IndySystem;fmxFireDAC;FireDAC;FireDACSqliteDriver;ibmonitor;FMXTee;soaprtl;DbxCommonDriver;ibxpress;xmlrtl;soapmidas;DataSnapNativeClient;ibxbindings;rtl;DbxClientDriver;FireDACDSDriver;CustomIPTransport;bindcomp;IndyIPClient;dbxcds;dsnapxml;DataSnapProviderClient;dbrtl;IndyProtocols;fmxase;$(DCC_UsePackage) 110 | 111 | 112 | DBXSqliteDriver;RESTComponents;DBXInterBaseDriver;emsclientfiredac;DataSnapFireDAC;tethering;bindcompfmx;FmxTeeUI;FireDACIBDriver;fmx;FireDACDBXDriver;dbexpress;IndyCore;dsnap;DataSnapCommon;emsclient;FireDACCommon;RESTBackendComponents;soapserver;bindengine;CloudService;FireDACCommonDriver;DataSnapClient;inet;bindcompdbx;IndyIPCommon;IndyIPServer;IndySystem;fmxFireDAC;FireDAC;FireDACSqliteDriver;ibmonitor;FMXTee;soaprtl;DbxCommonDriver;ibxpress;xmlrtl;soapmidas;DataSnapNativeClient;ibxbindings;rtl;DbxClientDriver;FireDACDSDriver;CustomIPTransport;bindcomp;IndyIPClient;dbxcds;dsnapxml;DataSnapProviderClient;dbrtl;IndyProtocols;fmxase;$(DCC_UsePackage) 113 | 114 | 115 | DBXSqliteDriver;RESTComponents;DBXInterBaseDriver;emsclientfiredac;DataSnapFireDAC;tethering;bindcompfmx;FmxTeeUI;FireDACIBDriver;fmx;FireDACDBXDriver;dbexpress;IndyCore;dsnap;DataSnapCommon;emsclient;FireDACCommon;RESTBackendComponents;soapserver;bindengine;CloudService;FireDACCommonDriver;DataSnapClient;inet;bindcompdbx;IndyIPCommon;IndyIPServer;IndySystem;fmxFireDAC;FireDAC;FireDACSqliteDriver;ibmonitor;FMXTee;soaprtl;DbxCommonDriver;ibxpress;xmlrtl;soapmidas;DataSnapNativeClient;ibxbindings;rtl;DbxClientDriver;FireDACDSDriver;CustomIPTransport;bindcomp;IndyIPClient;dbxcds;dsnapxml;DataSnapProviderClient;dbrtl;IndyProtocols;fmxase;$(DCC_UsePackage) 116 | 117 | 118 | RESTComponents;DataSnapServerMidas;emsclientfiredac;DataSnapFireDAC;FireDACADSDriver;DatasnapConnectorsFreePascal;FireDACMSSQLDriver;inetdb;emsedge;FireDACIBDriver;dbexpress;IndyCore;dsnap;DataSnapCommon;emsclient;FireDACCommon;RESTBackendComponents;DataSnapConnectors;soapserver;bindengine;FireDACOracleDriver;CloudService;FireDACMySQLDriver;FireDACCommonODBC;FireDACCommonDriver;DataSnapClient;inet;IndySystem;FireDACDb2Driver;FireDACInfxDriver;FireDAC;emshosting;FireDACSqliteDriver;FireDACPgDriver;FireDACASADriver;FireDACTDataDriver;soaprtl;DbxCommonDriver;DataSnapServer;xmlrtl;soapmidas;DataSnapNativeClient;rtl;emsserverresource;DbxClientDriver;CustomIPTransport;bindcomp;dbxcds;FireDACODBCDriver;DataSnapIndy10ServerTransport;dsnapxml;dbrtl;FireDACMongoDBDriver;IndyProtocols;$(DCC_UsePackage) 119 | 120 | 121 | DBXSqliteDriver;RESTComponents;DataSnapServerMidas;DBXInterBaseDriver;emsclientfiredac;DataSnapFireDAC;tethering;FireDACMSSQLDriver;bindcompfmx;DBXOracleDriver;inetdb;FmxTeeUI;FireDACIBDriver;fmx;fmxdae;FireDACDBXDriver;dbexpress;IndyCore;dsnap;DataSnapCommon;emsclient;FireDACCommon;RESTBackendComponents;soapserver;bindengine;DBXMySQLDriver;FireDACOracleDriver;CloudService;FireDACMySQLDriver;DBXFirebirdDriver;FireDACCommonODBC;FireDACCommonDriver;DataSnapClient;inet;bindcompdbx;IndyIPCommon;IndyIPServer;IndySystem;fmxFireDAC;FireDAC;FireDACSqliteDriver;FireDACPgDriver;ibmonitor;FireDACASADriver;FireDACTDataDriver;FMXTee;soaprtl;DbxCommonDriver;ibxpress;DataSnapServer;xmlrtl;soapmidas;DataSnapNativeClient;fmxobj;ibxbindings;rtl;DbxClientDriver;FireDACDSDriver;DBXSybaseASADriver;CustomIPTransport;bindcomp;DBXInformixDriver;IndyIPClient;dbxcds;FireDACODBCDriver;DataSnapIndy10ServerTransport;dsnapxml;DataSnapProviderClient;dbrtl;inetdbxpress;FireDACMongoDBDriver;IndyProtocols;fmxase;$(DCC_UsePackage) 122 | true 123 | 124 | 125 | DBXSqliteDriver;RESTComponents;DataSnapServerMidas;DBXDb2Driver;DBXInterBaseDriver;vclactnband;vclFireDAC;emsclientfiredac;DataSnapFireDAC;svnui;tethering;FireDACADSDriver;DBXMSSQLDriver;DatasnapConnectorsFreePascal;FireDACMSSQLDriver;vcltouch;vcldb;bindcompfmx;svn;DBXOracleDriver;inetdb;FmxTeeUI;emsedge;FireDACIBDriver;fmx;fmxdae;vclib;FireDACDBXDriver;dbexpress;IndyCore;vclx;dsnap;DataSnapCommon;emsclient;FireDACCommon;RESTBackendComponents;DataSnapConnectors;VCLRESTComponents;soapserver;vclie;bindengine;DBXMySQLDriver;FireDACOracleDriver;CloudService;FireDACMySQLDriver;DBXFirebirdDriver;FireDACCommonODBC;FireDACCommonDriver;DataSnapClient;inet;bindcompdbx;IndyIPCommon;vcl;DBXSybaseASEDriver;IndyIPServer;IndySystem;FireDACDb2Driver;dsnapcon;FireDACMSAccDriver;fmxFireDAC;FireDACInfxDriver;vclimg;TeeDB;FireDAC;emshosting;FireDACSqliteDriver;FireDACPgDriver;ibmonitor;FireDACASADriver;DBXOdbcDriver;FireDACTDataDriver;FMXTee;soaprtl;DbxCommonDriver;ibxpress;Tee;DataSnapServer;xmlrtl;soapmidas;DataSnapNativeClient;fmxobj;vclwinx;ibxbindings;rtl;emsserverresource;DbxClientDriver;FireDACDSDriver;DBXSybaseASADriver;CustomIPTransport;vcldsnap;bindcomp;appanalytics;DBXInformixDriver;IndyIPClient;bindcompvcl;TeeUI;dbxcds;VclSmp;adortl;FireDACODBCDriver;DataSnapIndy10ServerTransport;dsnapxml;DataSnapProviderClient;dbrtl;inetdbxpress;FireDACMongoDBDriver;IndyProtocols;fmxase;$(DCC_UsePackage) 126 | Winapi;System.Win;Data.Win;Datasnap.Win;Web.Win;Soap.Win;Xml.Win;Bde;$(DCC_Namespace) 127 | Debug 128 | CompanyName=;FileDescription=$(MSBuildProjectName);FileVersion=1.0.0.0;InternalName=;LegalCopyright=;LegalTrademarks=;OriginalFilename=;ProgramID=com.embarcadero.$(MSBuildProjectName);ProductName=$(MSBuildProjectName);ProductVersion=1.0.0.0;Comments= 129 | 1033 130 | true 131 | $(BDS)\bin\Artwork\Windows\UWP\delphi_UwpDefault_44.png 132 | $(BDS)\bin\Artwork\Windows\UWP\delphi_UwpDefault_150.png 133 | 134 | 135 | DBXSqliteDriver;RESTComponents;DataSnapServerMidas;DBXDb2Driver;DBXInterBaseDriver;vclactnband;vclFireDAC;emsclientfiredac;DataSnapFireDAC;tethering;FireDACADSDriver;DBXMSSQLDriver;DatasnapConnectorsFreePascal;FireDACMSSQLDriver;vcltouch;vcldb;bindcompfmx;DBXOracleDriver;inetdb;FmxTeeUI;emsedge;FireDACIBDriver;fmx;fmxdae;vclib;FireDACDBXDriver;dbexpress;IndyCore;vclx;dsnap;DataSnapCommon;emsclient;FireDACCommon;RESTBackendComponents;DataSnapConnectors;VCLRESTComponents;soapserver;vclie;bindengine;DBXMySQLDriver;FireDACOracleDriver;CloudService;FireDACMySQLDriver;DBXFirebirdDriver;FireDACCommonODBC;FireDACCommonDriver;DataSnapClient;inet;bindcompdbx;IndyIPCommon;vcl;DBXSybaseASEDriver;IndyIPServer;IndySystem;FireDACDb2Driver;dsnapcon;FireDACMSAccDriver;fmxFireDAC;FireDACInfxDriver;vclimg;TeeDB;FireDAC;emshosting;FireDACSqliteDriver;FireDACPgDriver;ibmonitor;FireDACASADriver;DBXOdbcDriver;FireDACTDataDriver;FMXTee;soaprtl;DbxCommonDriver;ibxpress;Tee;DataSnapServer;xmlrtl;soapmidas;DataSnapNativeClient;fmxobj;vclwinx;ibxbindings;rtl;emsserverresource;DbxClientDriver;FireDACDSDriver;DBXSybaseASADriver;CustomIPTransport;vcldsnap;bindcomp;appanalytics;DBXInformixDriver;IndyIPClient;bindcompvcl;TeeUI;dbxcds;VclSmp;adortl;FireDACODBCDriver;DataSnapIndy10ServerTransport;dsnapxml;DataSnapProviderClient;dbrtl;inetdbxpress;FireDACMongoDBDriver;IndyProtocols;fmxase;$(DCC_UsePackage) 136 | true 137 | $(BDS)\bin\Artwork\Windows\UWP\delphi_UwpDefault_44.png 138 | $(BDS)\bin\Artwork\Windows\UWP\delphi_UwpDefault_150.png 139 | Winapi;System.Win;Data.Win;Datasnap.Win;Web.Win;Soap.Win;Xml.Win;$(DCC_Namespace) 140 | Debug 141 | CompanyName=;FileDescription=$(MSBuildProjectName);FileVersion=1.0.0.0;InternalName=;LegalCopyright=;LegalTrademarks=;OriginalFilename=;ProgramID=com.embarcadero.$(MSBuildProjectName);ProductName=$(MSBuildProjectName);ProductVersion=1.0.0.0;Comments= 142 | 1033 143 | ../;$(DCC_UnitSearchPath) 144 | ..\QJSPascalDemo\(None) 145 | C:\Users\Coldzer0\Desktop\Cmulator\Core\QJS\QJSPascalDemo\examples\ApiHook.js 146 | .\$(Platform)\$(Config) 147 | . 148 | 149 | 150 | DEBUG;$(DCC_Define) 151 | true 152 | false 153 | true 154 | true 155 | true 156 | 157 | 158 | false 159 | ../;$(DCC_UnitSearchPath) 160 | 1033 161 | ..\QJSPascalDemo\(None) 162 | . 163 | 164 | 165 | 1033 166 | 167 | 168 | false 169 | RELEASE;$(DCC_Define) 170 | 0 171 | 0 172 | 173 | 174 | ..\QJSPascalDemo\(None) 175 | 176 | 177 | 178 | MainSource 179 | 180 | 181 | Cfg_2 182 | Base 183 | 184 | 185 | Base 186 | 187 | 188 | Cfg_1 189 | Base 190 | 191 | 192 | 193 | Delphi.Personality.12 194 | Application 195 | 196 | 197 | 198 | DelphiDemo.dpr 199 | 200 | 201 | Microsoft Office 2000 Sample Automation Server Wrapper Components 202 | Microsoft Office XP Sample Automation Server Wrapper Components 203 | 204 | 205 | 206 | 207 | 208 | true 209 | 210 | 211 | 212 | 213 | true 214 | 215 | 216 | 217 | 218 | true 219 | 220 | 221 | 222 | 223 | true 224 | 225 | 226 | 227 | 228 | true 229 | 230 | 231 | 232 | 233 | DelphiDemo.exe 234 | true 235 | 236 | 237 | 238 | 239 | 1 240 | 241 | 242 | Contents\MacOS 243 | 1 244 | 245 | 246 | 0 247 | 248 | 249 | 250 | 251 | classes 252 | 1 253 | 254 | 255 | 256 | 257 | res\xml 258 | 1 259 | 260 | 261 | 262 | 263 | library\lib\armeabi-v7a 264 | 1 265 | 266 | 267 | 268 | 269 | library\lib\armeabi 270 | 1 271 | 272 | 273 | 274 | 275 | library\lib\mips 276 | 1 277 | 278 | 279 | 280 | 281 | library\lib\armeabi-v7a 282 | 1 283 | 284 | 285 | 286 | 287 | res\drawable 288 | 1 289 | 290 | 291 | 292 | 293 | res\values 294 | 1 295 | 296 | 297 | 298 | 299 | res\values-v21 300 | 1 301 | 302 | 303 | 304 | 305 | res\drawable 306 | 1 307 | 308 | 309 | 310 | 311 | res\drawable-xxhdpi 312 | 1 313 | 314 | 315 | 316 | 317 | res\drawable-ldpi 318 | 1 319 | 320 | 321 | 322 | 323 | res\drawable-mdpi 324 | 1 325 | 326 | 327 | 328 | 329 | res\drawable-hdpi 330 | 1 331 | 332 | 333 | 334 | 335 | res\drawable-xhdpi 336 | 1 337 | 338 | 339 | 340 | 341 | res\drawable-small 342 | 1 343 | 344 | 345 | 346 | 347 | res\drawable-normal 348 | 1 349 | 350 | 351 | 352 | 353 | res\drawable-large 354 | 1 355 | 356 | 357 | 358 | 359 | res\drawable-xlarge 360 | 1 361 | 362 | 363 | 364 | 365 | 1 366 | 367 | 368 | Contents\MacOS 369 | 1 370 | 371 | 372 | 0 373 | 374 | 375 | 376 | 377 | Contents\MacOS 378 | 1 379 | .framework 380 | 381 | 382 | Contents\MacOS 383 | 1 384 | .framework 385 | 386 | 387 | 0 388 | 389 | 390 | 391 | 392 | 1 393 | .dylib 394 | 395 | 396 | 1 397 | .dylib 398 | 399 | 400 | 1 401 | .dylib 402 | 403 | 404 | Contents\MacOS 405 | 1 406 | .dylib 407 | 408 | 409 | Contents\MacOS 410 | 1 411 | .dylib 412 | 413 | 414 | 0 415 | .dll;.bpl 416 | 417 | 418 | 419 | 420 | 1 421 | .dylib 422 | 423 | 424 | 1 425 | .dylib 426 | 427 | 428 | 1 429 | .dylib 430 | 431 | 432 | Contents\MacOS 433 | 1 434 | .dylib 435 | 436 | 437 | Contents\MacOS 438 | 1 439 | .dylib 440 | 441 | 442 | 0 443 | .bpl 444 | 445 | 446 | 447 | 448 | 0 449 | 450 | 451 | 0 452 | 453 | 454 | 0 455 | 456 | 457 | 0 458 | 459 | 460 | Contents\Resources\StartUp\ 461 | 0 462 | 463 | 464 | Contents\Resources\StartUp\ 465 | 0 466 | 467 | 468 | 0 469 | 470 | 471 | 472 | 473 | 1 474 | 475 | 476 | 1 477 | 478 | 479 | 1 480 | 481 | 482 | 483 | 484 | 1 485 | 486 | 487 | 1 488 | 489 | 490 | 1 491 | 492 | 493 | 494 | 495 | 1 496 | 497 | 498 | 1 499 | 500 | 501 | 1 502 | 503 | 504 | 505 | 506 | 1 507 | 508 | 509 | 1 510 | 511 | 512 | 1 513 | 514 | 515 | 516 | 517 | 1 518 | 519 | 520 | 1 521 | 522 | 523 | 1 524 | 525 | 526 | 527 | 528 | 1 529 | 530 | 531 | 1 532 | 533 | 534 | 1 535 | 536 | 537 | 538 | 539 | 1 540 | 541 | 542 | 1 543 | 544 | 545 | 1 546 | 547 | 548 | 549 | 550 | 1 551 | 552 | 553 | 554 | 555 | ..\$(PROJECTNAME).app.dSYM\Contents\Resources\DWARF 556 | 1 557 | 558 | 559 | ..\$(PROJECTNAME).app.dSYM\Contents\Resources\DWARF 560 | 1 561 | 562 | 563 | 564 | 565 | 1 566 | 567 | 568 | 1 569 | 570 | 571 | 572 | 573 | ..\ 574 | 1 575 | 576 | 577 | ..\ 578 | 1 579 | 580 | 581 | 582 | 583 | 1 584 | 585 | 586 | 1 587 | 588 | 589 | 1 590 | 591 | 592 | 593 | 594 | 1 595 | 596 | 597 | 1 598 | 599 | 600 | 1 601 | 602 | 603 | 604 | 605 | ..\$(PROJECTNAME).app.dSYM\Contents\Resources\DWARF 606 | 1 607 | 608 | 609 | 610 | 611 | ..\ 612 | 1 613 | 614 | 615 | ..\ 616 | 1 617 | 618 | 619 | 620 | 621 | Contents 622 | 1 623 | 624 | 625 | Contents 626 | 1 627 | 628 | 629 | 630 | 631 | Contents\Resources 632 | 1 633 | 634 | 635 | Contents\Resources 636 | 1 637 | 638 | 639 | 640 | 641 | library\lib\armeabi-v7a 642 | 1 643 | 644 | 645 | 1 646 | 647 | 648 | 1 649 | 650 | 651 | 1 652 | 653 | 654 | 1 655 | 656 | 657 | Contents\MacOS 658 | 1 659 | 660 | 661 | Contents\MacOS 662 | 1 663 | 664 | 665 | 0 666 | 667 | 668 | 669 | 670 | 1 671 | 672 | 673 | 1 674 | 675 | 676 | 677 | 678 | Assets 679 | 1 680 | 681 | 682 | Assets 683 | 1 684 | 685 | 686 | 687 | 688 | Assets 689 | 1 690 | 691 | 692 | Assets 693 | 1 694 | 695 | 696 | 697 | 698 | 699 | 700 | 701 | 702 | 703 | 704 | 705 | 706 | 707 | False 708 | False 709 | False 710 | False 711 | False 712 | False 713 | True 714 | True 715 | 716 | 717 | 12 718 | 719 | 720 | 721 | 722 | 723 | -------------------------------------------------------------------------------- /QJSDelphiDemo/DelphiDemo.res: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Coldzer0/QuickJS-Pascal/75c1f2be24ce83554f79bc1952b9dd376ebae7c6/QJSDelphiDemo/DelphiDemo.res -------------------------------------------------------------------------------- /QJSDelphiDemo/examples/ApiHook.js: -------------------------------------------------------------------------------- 1 | // custom log function. 2 | log('\n====================================================\n') 3 | 4 | const ExitProcess = new Cmu.ApiHook(); // Create From Our Module 5 | 6 | ExitProcess.OnCallBack = function (Emu, API,ret) { 7 | 8 | ExitProcess.args[0] = 1007; 9 | ExitProcess.args[1] = 2222; 10 | 11 | console.log("Hello From ExitProcess -",ExitProcess.version); 12 | 13 | return true; 14 | }; 15 | ExitProcess.install('kernel32.dll', 'ExitProcess'); 16 | 17 | log('ExitProcess.args[0] : ',ExitProcess.args[0]) 18 | log('ExitProcess.args[1] : ',ExitProcess.args[1]) 19 | 20 | 21 | log('\n====================================================\n') 22 | 23 | const MessageBox = new ApiHook(); // From the Global context 24 | 25 | MessageBox.OnCallBack = function (Emu, API,ret) { 26 | 27 | MessageBox.args[0] = 'hello'; 28 | MessageBox.args[1] = 1010; 29 | console.log("Hello From MessageBox",MessageBox.version); 30 | return false; 31 | }; 32 | MessageBox.install('user32.dll', 'MessageBox'); 33 | log('MessageBox.args[0] : ',MessageBox.args[0]) 34 | log('MessageBox.args[1] : ',MessageBox.args[1]) 35 | 36 | -------------------------------------------------------------------------------- /QJSDelphiDemo/examples/fib_module.js: -------------------------------------------------------------------------------- 1 | /* fib module */ 2 | export function fib(n) 3 | { 4 | if (n <= 0) 5 | return 0; 6 | else if (n == 1) 7 | return 1; 8 | else 9 | return fib(n - 1) + fib(n - 2); 10 | } 11 | -------------------------------------------------------------------------------- /QJSDelphiDemo/examples/hello_module.js: -------------------------------------------------------------------------------- 1 | import { fib } from "fib_module.js"; 2 | 3 | console.log("Hello World"); 4 | console.log("fib(10)=", fib(10)); 5 | -------------------------------------------------------------------------------- /QJSDelphiDemo/examples/pi.js: -------------------------------------------------------------------------------- 1 | /* 2 | * PI computation in Javascript using the QuickJS bignum extensions 3 | */ 4 | "use strict"; 5 | "use bigint"; 6 | 7 | /* compute PI with a precision of 'prec' bits */ 8 | function calc_pi(prec) { 9 | const CHUD_A = 13591409; 10 | const CHUD_B = 545140134; 11 | const CHUD_C = 640320; 12 | const CHUD_C3 = 10939058860032000; /* C^3/24 */ 13 | const CHUD_BITS_PER_TERM = 47.11041313821584202247; /* log2(C/12)*3 */ 14 | 15 | /* return [P, Q, G] */ 16 | function chud_bs(a, b, need_G) { 17 | var c, P, Q, G, P1, Q1, G1, P2, Q2, G2; 18 | if (a == (b - 1)) { 19 | G = (2 * b - 1) * (6 * b - 1) * (6 * b - 5); 20 | P = BigFloat(G * (CHUD_B * b + CHUD_A)); 21 | if (b & 1) 22 | P = -P; 23 | G = BigFloat(G); 24 | Q = BigFloat(b * b * b * CHUD_C3); 25 | } else { 26 | c = (a + b) >> 1; 27 | [P1, Q1, G1] = chud_bs(a, c, true); 28 | [P2, Q2, G2] = chud_bs(c, b, need_G); 29 | P = P1 * Q2 + P2 * G1; 30 | Q = Q1 * Q2; 31 | if (need_G) 32 | G = G1 * G2; 33 | else 34 | G = 0; 35 | } 36 | return [P, Q, G]; 37 | } 38 | 39 | var n, P, Q, G; 40 | /* number of serie terms */ 41 | n = Math.ceil(BigFloatEnv.prec / CHUD_BITS_PER_TERM) + 10; 42 | [P, Q, G] = chud_bs(0, n, false); 43 | Q = Q / (P + Q * CHUD_A); 44 | G = (CHUD_C / 12) * BigFloat.sqrt(CHUD_C); 45 | return Q * G; 46 | } 47 | 48 | (function() { 49 | var r, n_digits, n_bits; 50 | if (typeof scriptArgs != "undefined") { 51 | if (scriptArgs.length < 2) { 52 | print("usage: pi n_digits"); 53 | return; 54 | } 55 | n_digits = scriptArgs[1]; 56 | } else { 57 | n_digits = 1000; 58 | } 59 | n_bits = Math.ceil(n_digits * Math.log2(10)); 60 | /* we add more bits to reduce the probability of bad rounding for 61 | the last digits */ 62 | BigFloatEnv.setPrec( () => { 63 | r = calc_pi(); 64 | print(r.toFixed(n_digits, BigFloatEnv.RNDZ)); 65 | }, n_bits + 32); 66 | })(); 67 | -------------------------------------------------------------------------------- /QJSDelphiDemo/quickjsdemo.pas: -------------------------------------------------------------------------------- 1 | { 2 | Native Class Demo - With module or Global context. 3 | } 4 | 5 | unit QuickJSDemo; 6 | {$IFDEF FPC} 7 | {$mode delphi}{$H+}{$M+} 8 | {$PackRecords C} 9 | {$ENDIF} 10 | interface 11 | 12 | uses 13 | quickjs; 14 | 15 | // if you want to make a native class you must follow the following \ 16 | // tab, JClass, Class_id and Class_proto Must be a global variables. 17 | var 18 | API_Class_id : JSClassID = 0; 19 | API_Class_Proto : JSValue; 20 | JClass : JSClassDef = (class_name:'ApiHook';finalizer:nil;gc_mark:nil;call:nil;exotic:nil); 21 | tab : array [0..1] of JSCFunctionListEntry; 22 | 23 | procedure Emu_mod_init(ctx : JSContext; m : JSModuleDef); cdecl; 24 | function Emu_init(ctx : JSContext; m : JSModuleDef): Integer;cdecl; 25 | 26 | procedure RegisterNativeClass(ctx : JSContext); cdecl; 27 | 28 | implementation 29 | 30 | function install(ctx : JSContext; {%H-}this_val : JSValueConst; argc : Integer; argv : PJSValueConstArr): JSValue; cdecl; 31 | var 32 | Module,API : PAnsiChar; 33 | OnCallBack,res : JSValue; 34 | begin 35 | Result := JS_UNDEFINED; 36 | if argc >= 2 then 37 | begin 38 | Module := JS_ToCString(ctx, argv[0]); 39 | API := JS_ToCString(ctx, argv[1]); 40 | try 41 | Writeln('API : ',Module, '.', API); 42 | 43 | OnCallBack := JS_GetPropertyStr(ctx,this_val,'OnCallBack'); 44 | if JS_IsFunction(ctx,OnCallBack) then 45 | begin 46 | res := JS_Call(ctx,OnCallBack,this_val,argc,argv); 47 | if JS_IsException(res) then 48 | exit(res); 49 | Writeln('OnCallBack return = ', JS_ToBool(ctx,res)); 50 | end; 51 | finally 52 | JS_FreeValue(ctx,OnCallBack); 53 | JS_FreeCString(ctx, Module); 54 | JS_FreeCString(ctx, API); 55 | end; 56 | end; 57 | end; 58 | 59 | function CConstructor(ctx : JSContext; new_target : JSValueConst; argc : Integer; argv : PJSValueConstArr): JSValue; cdecl; 60 | begin 61 | Result := JS_NewObjectClass(ctx,API_Class_id); 62 | // New Array for every new instance. 63 | JS_DefinePropertyValueStr(ctx,Result,'args',JS_NewArray(ctx),JS_PROP_CONFIGURABLE or JS_PROP_WRITABLE); 64 | end; 65 | 66 | procedure Emu_mod_init(ctx : JSContext; m : JSModuleDef); cdecl; 67 | var 68 | obj : JSValue; 69 | begin 70 | obj := JS_NewCFunction2(ctx, @CConstructor, PAnsiChar('ApiHook'), 1, JS_CFUNC_constructor, 0); 71 | JS_SetModuleExport(ctx, m, PAnsiChar('ApiHook'), obj); 72 | end; 73 | 74 | function Emu_init(ctx : JSContext; m : JSModuleDef): Integer;cdecl; 75 | begin 76 | Emu_mod_init(ctx,m); 77 | Result := 0; 78 | end; 79 | 80 | procedure RegisterNativeClass(ctx : JSContext); cdecl; 81 | var 82 | obj,global : JSValue; 83 | begin 84 | 85 | // Create New Class id. 86 | JS_NewClassID(@API_Class_id); 87 | // Create the Class Name and other stuff. 88 | JS_NewClass(JS_GetRuntime(ctx),API_Class_id,@JClass); 89 | 90 | // Properties list. 91 | tab[0] := JS_CFUNC_DEF('install', 1, JSCFunctionType(@install)); 92 | tab[1] := JS_PROP_INT32_DEF('version', 1337, JS_PROP_CONFIGURABLE); 93 | 94 | // New Object act as Prototype for the Class. 95 | API_Class_Proto := JS_NewObject(ctx); 96 | 97 | // Set list of Properties to the prototype Object. 98 | JS_SetPropertyFunctionList(ctx,API_Class_Proto,@tab,Length(tab)); 99 | 100 | 101 | // Set the Prototype to the Class. 102 | JS_SetClassProto(ctx, API_Class_id, API_Class_Proto); 103 | 104 | // Set the Class native constructor. 105 | obj := JS_NewCFunction2(ctx, @CConstructor, 'ApiHook', 1, JS_CFUNC_constructor, 0); 106 | 107 | // Add the Class to Global Object so we can use it. 108 | global := JS_GetGlobalObject(ctx); 109 | JS_SetPropertyStr(ctx,global,'ApiHook',obj); 110 | JS_FreeValue(ctx,global); 111 | end; 112 | 113 | end. 114 | 115 | -------------------------------------------------------------------------------- /QJSPascalDemo/Demo.lpi: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | <UseAppBundle Value="False"/> 15 | <ResourceType Value="res"/> 16 | </General> 17 | <BuildModes Count="1"> 18 | <Item1 Name="Default" Default="True"/> 19 | </BuildModes> 20 | <PublishOptions> 21 | <Version Value="2"/> 22 | <UseFileFilters Value="True"/> 23 | </PublishOptions> 24 | <RunParams> 25 | <FormatVersion Value="2"/> 26 | </RunParams> 27 | <Units Count="1"> 28 | <Unit0> 29 | <Filename Value="Demo.lpr"/> 30 | <IsPartOfProject Value="True"/> 31 | </Unit0> 32 | </Units> 33 | </ProjectOptions> 34 | <CompilerOptions> 35 | <Version Value="11"/> 36 | <Target> 37 | <Filename Value="Demo"/> 38 | </Target> 39 | <SearchPaths> 40 | <IncludeFiles Value="$(ProjOutDir)"/> 41 | <OtherUnitFiles Value=".."/> 42 | <UnitOutputDirectory Value="lib/$(TargetCPU)-$(TargetOS)"/> 43 | </SearchPaths> 44 | <Parsing> 45 | <SyntaxOptions> 46 | <SyntaxMode Value="Delphi"/> 47 | <CStyleMacros Value="True"/> 48 | </SyntaxOptions> 49 | </Parsing> 50 | <CodeGeneration> 51 | <TargetCPU Value="x86_64"/> 52 | </CodeGeneration> 53 | <Linking> 54 | <Debugging> 55 | <UseHeaptrc Value="True"/> 56 | </Debugging> 57 | </Linking> 58 | </CompilerOptions> 59 | <Debugging> 60 | <Exceptions Count="3"> 61 | <Item1> 62 | <Name Value="EAbort"/> 63 | </Item1> 64 | <Item2> 65 | <Name Value="ECodetoolError"/> 66 | </Item2> 67 | <Item3> 68 | <Name Value="EFOpenError"/> 69 | </Item3> 70 | </Exceptions> 71 | </Debugging> 72 | </CONFIG> 73 | -------------------------------------------------------------------------------- /QJSPascalDemo/Demo.lpr: -------------------------------------------------------------------------------- 1 | { 2 | FreePascal Demo for QuickJS Engine \ 3 | Tested On Mac - Win - Linux >> with FreePascal v3.0.4 . 4 | 5 | Copyright(c) 2019 Coldzer0 <Coldzer0 [at] protonmail.ch> . 6 | 7 | License: MIT . 8 | } 9 | 10 | program Demo; 11 | {$IFDEF FPC} 12 | {$Mode Delphi}{$H+} 13 | {$extendedsyntax on} 14 | {$ENDIF} 15 | uses 16 | {$IFDEF unix}cthreads,{$ENDIF} 17 | cmem, 18 | quickjs, 19 | QuickJSDemo,RawExecution; 20 | 21 | 22 | function eval_buf(ctx: JSContext; Buf: PChar; buf_len: integer; 23 | filename: PChar; is_main : boolean; eval_flags: integer = -1): JSValue; 24 | var 25 | ret: JSValue; 26 | begin 27 | if eval_flags = -1 then 28 | begin 29 | if JS_DetectModule(Buf,buf_len) then 30 | eval_flags := JS_EVAL_TYPE_MODULE 31 | else 32 | eval_flags := JS_EVAL_TYPE_GLOBAL; 33 | end; 34 | 35 | if (eval_flags and JS_EVAL_TYPE_MASK) = JS_EVAL_TYPE_MODULE then 36 | begin 37 | ret := JS_Eval(ctx, buf, buf_len, filename, eval_flags or JS_EVAL_FLAG_COMPILE_ONLY); 38 | if not JS_IsException(ret) then 39 | begin 40 | js_module_set_import_meta(ctx, ret, True, is_main); 41 | ret := JS_EvalFunction(ctx, ret); 42 | end; 43 | end 44 | else 45 | ret := JS_Eval(ctx, buf, buf_len, filename, eval_flags); 46 | 47 | if JS_IsException(ret) then 48 | begin 49 | js_std_dump_error(ctx); 50 | Result := JS_NULL; 51 | end 52 | else 53 | Result := ret; 54 | end; 55 | 56 | function eval_file(ctx : JSContext; filename : PChar; eval_flags : Integer = -1): JSValue; 57 | var 58 | buf_len : size_t; 59 | Buf : Pointer; 60 | begin 61 | buf := js_load_file(ctx, @buf_len, filename); 62 | if not Assigned(buf) then 63 | begin 64 | Writeln('Error While Loading : ',filename); 65 | exit(JS_EXCEPTION); 66 | end; 67 | Result := eval_buf(ctx, buf, buf_len, filename, true, eval_flags); 68 | js_free(ctx, buf); 69 | end; 70 | 71 | 72 | function logme(ctx : JSContext; {%H-}this_val : JSValueConst; argc : Integer; argv : PJSValueConstArr): JSValue; cdecl; 73 | var 74 | i : Integer; 75 | str : PChar; 76 | begin 77 | for i := 0 to Pred(argc) do 78 | begin 79 | if i <> 0 then 80 | write(' '); 81 | str := JS_ToCString(ctx, argv[i]); 82 | if not Assigned(str) then 83 | exit(JS_EXCEPTION); 84 | Write(str); 85 | JS_FreeCString(ctx, str); 86 | end; 87 | Writeln(); 88 | Result := JS_UNDEFINED; 89 | end; 90 | 91 | procedure RunCode(); 92 | var 93 | rt : JSRuntime; 94 | ctx : JSContext; 95 | m : JSModuleDef; 96 | global : JSValue; 97 | filename : PChar; 98 | const 99 | std_hepler : PChar = 100 | 'import * as std from ''std'';'#10+ 101 | 'import * as os from ''os'';'#10+ 102 | 'import * as Cmu from ''Cmu'';'#10+ // Our Custom Module. 103 | 'globalThis.std = std;'#10+ 104 | 'globalThis.os = os;'#10+ 105 | 'globalThis.Cmu = Cmu;'#10; 106 | begin 107 | rt := JS_NewRuntime; 108 | if Assigned(rt) then 109 | begin 110 | ctx := JS_NewContext(rt); 111 | if Assigned(rt) then 112 | begin 113 | // ES6 Module loader. 114 | JS_SetModuleLoaderFunc(rt, nil, @js_module_loader, nil); 115 | 116 | js_std_add_helpers(ctx,argc-1,@argv[1]); 117 | js_init_module_std(ctx, 'std'); 118 | js_init_module_os(ctx, 'os'); 119 | 120 | { 121 | Functions init order is important \ 122 | cuz i init the class and it's obj's and constructor in \ 123 | RegisterNativeClass then i just point the Module constructor to the same one. 124 | } 125 | 126 | // Register with global object directly . 127 | RegisterNativeClass(ctx); 128 | 129 | // Register with module 130 | m := JS_NewCModule(ctx, 'Cmu', @Emu_init); 131 | JS_AddModuleExport(ctx,m,'ApiHook'); 132 | 133 | eval_buf(ctx, std_hepler, strlen(std_hepler), '<global_helper>', False, JS_EVAL_TYPE_MODULE); 134 | 135 | 136 | global := JS_GetGlobalObject(ctx); 137 | 138 | // Define a function in the global context. 139 | JS_SetPropertyStr(ctx,global,'log',JS_NewCFunction(ctx, @logme, 'log', 1)); 140 | 141 | JS_FreeValue(ctx, global); 142 | 143 | if ParamCount >= 1 then 144 | begin 145 | filename := PChar(ParamStr(1)); 146 | eval_file(ctx,filename); 147 | end; 148 | 149 | js_std_loop(ctx); 150 | 151 | js_std_free_handlers(rt); 152 | JS_FreeContext(ctx); 153 | end; 154 | JS_FreeRuntime(rt); 155 | end; 156 | Writeln(); 157 | end; 158 | 159 | 160 | 161 | begin 162 | { TODO -oColdzer0 : RawTest Bytes need to be updated } 163 | //RawTest; // If you unComment this comment the next line. 164 | RunCode; 165 | end. 166 | 167 | -------------------------------------------------------------------------------- /QJSPascalDemo/examples/ApiHook.js: -------------------------------------------------------------------------------- 1 | // custom log function. 2 | log('\n====================================================\n') 3 | 4 | const ExitProcess = new Cmu.ApiHook(); // Create From Our Module 5 | 6 | ExitProcess.OnCallBack = function (Emu, API,ret) { 7 | 8 | ExitProcess.args[0] = 1007; 9 | ExitProcess.args[1] = 2222; 10 | 11 | console.log("Hello From ExitProcess -",ExitProcess.version); 12 | 13 | return true; 14 | }; 15 | ExitProcess.install('kernel32.dll', 'ExitProcess'); 16 | 17 | log('ExitProcess.args[0] : ',ExitProcess.args[0]) 18 | log('ExitProcess.args[1] : ',ExitProcess.args[1]) 19 | 20 | 21 | log('\n====================================================\n') 22 | 23 | const MessageBox = new ApiHook(); // From the Global context 24 | 25 | MessageBox.OnCallBack = function (Emu, API,ret) { 26 | 27 | MessageBox.args[0] = 'hello'; 28 | MessageBox.args[1] = 1010; 29 | console.log("Hello From MessageBox",MessageBox.version); 30 | return false; 31 | }; 32 | MessageBox.install('user32.dll', 'MessageBox'); 33 | log('MessageBox.args[0] : ',MessageBox.args[0]) 34 | log('MessageBox.args[1] : ',MessageBox.args[1]) 35 | 36 | -------------------------------------------------------------------------------- /QJSPascalDemo/examples/fib_module.js: -------------------------------------------------------------------------------- 1 | /* fib module */ 2 | export function fib(n) 3 | { 4 | if (n <= 0) 5 | return 0; 6 | else if (n == 1) 7 | return 1; 8 | else 9 | return fib(n - 1) + fib(n - 2); 10 | } 11 | -------------------------------------------------------------------------------- /QJSPascalDemo/examples/hello_module.js: -------------------------------------------------------------------------------- 1 | import { fib } from "./fib_module.js"; 2 | 3 | console.log("Hello World"); 4 | console.log("fib(10)=", fib(10)); 5 | -------------------------------------------------------------------------------- /QJSPascalDemo/examples/pi.js: -------------------------------------------------------------------------------- 1 | /* 2 | * PI computation in Javascript using the QuickJS bignum extensions 3 | */ 4 | "use strict"; 5 | "use bigint"; 6 | 7 | /* compute PI with a precision of 'prec' bits */ 8 | function calc_pi(prec) { 9 | const CHUD_A = 13591409; 10 | const CHUD_B = 545140134; 11 | const CHUD_C = 640320; 12 | const CHUD_C3 = 10939058860032000; /* C^3/24 */ 13 | const CHUD_BITS_PER_TERM = 47.11041313821584202247; /* log2(C/12)*3 */ 14 | 15 | /* return [P, Q, G] */ 16 | function chud_bs(a, b, need_G) { 17 | var c, P, Q, G, P1, Q1, G1, P2, Q2, G2; 18 | if (a == (b - 1)) { 19 | G = (2 * b - 1) * (6 * b - 1) * (6 * b - 5); 20 | P = BigFloat(G * (CHUD_B * b + CHUD_A)); 21 | if (b & 1) 22 | P = -P; 23 | G = BigFloat(G); 24 | Q = BigFloat(b * b * b * CHUD_C3); 25 | } else { 26 | c = (a + b) >> 1; 27 | [P1, Q1, G1] = chud_bs(a, c, true); 28 | [P2, Q2, G2] = chud_bs(c, b, need_G); 29 | P = P1 * Q2 + P2 * G1; 30 | Q = Q1 * Q2; 31 | if (need_G) 32 | G = G1 * G2; 33 | else 34 | G = 0; 35 | } 36 | return [P, Q, G]; 37 | } 38 | 39 | var n, P, Q, G; 40 | /* number of serie terms */ 41 | n = Math.ceil(BigFloatEnv.prec / CHUD_BITS_PER_TERM) + 10; 42 | [P, Q, G] = chud_bs(0, n, false); 43 | Q = Q / (P + Q * CHUD_A); 44 | G = (CHUD_C / 12) * BigFloat.sqrt(CHUD_C); 45 | return Q * G; 46 | } 47 | 48 | (function() { 49 | var r, n_digits, n_bits; 50 | if (typeof scriptArgs != "undefined") { 51 | if (scriptArgs.length < 2) { 52 | print("usage: pi n_digits"); 53 | return; 54 | } 55 | n_digits = scriptArgs[1]; 56 | } else { 57 | n_digits = 1000; 58 | } 59 | n_bits = Math.ceil(n_digits * Math.log2(10)); 60 | /* we add more bits to reduce the probability of bad rounding for 61 | the last digits */ 62 | BigFloatEnv.setPrec( () => { 63 | r = calc_pi(); 64 | print(r.toFixed(n_digits, BigFloatEnv.RNDZ)); 65 | }, n_bits + 32); 66 | })(); 67 | -------------------------------------------------------------------------------- /QJSPascalDemo/quickjsdemo.pas: -------------------------------------------------------------------------------- 1 | { 2 | Native Class Demo - With module or Global context. 3 | } 4 | 5 | unit QuickJSDemo; 6 | 7 | {$mode delphi}{$H+}{$M+} 8 | {$PackRecords C} 9 | 10 | interface 11 | 12 | uses 13 | quickjs; 14 | 15 | // if you want to make a native class you must follow the following \ 16 | // tab, JClass, Class_id and Class_proto Must be a global variables. 17 | var 18 | API_Class_id : JSClassID = 0; 19 | API_Class_Proto : JSValue; 20 | JClass : JSClassDef = (class_name:'ApiHook';finalizer:nil;gc_mark:nil;call:nil;exotic:nil); 21 | tab : array [0..1] of JSCFunctionListEntry; 22 | 23 | procedure Emu_mod_init(ctx : JSContext; m : JSModuleDef); cdecl; 24 | function Emu_init(ctx : JSContext; m : JSModuleDef): Integer;cdecl; 25 | 26 | procedure RegisterNativeClass(ctx : JSContext); cdecl; 27 | 28 | implementation 29 | 30 | function install(ctx : JSContext; {%H-}this_val : JSValueConst; argc : Integer; argv : PJSValueConstArr): JSValue; cdecl; 31 | var 32 | Module,API : PChar; 33 | OnCallBack,res : JSValue; 34 | begin 35 | Result := JS_UNDEFINED; 36 | if argc >= 2 then 37 | begin 38 | try 39 | Module := JS_ToCString(ctx, argv[0]); 40 | API := JS_ToCString(ctx, argv[1]); 41 | 42 | Writeln('API : ',Module, '.', API); 43 | 44 | OnCallBack := JS_GetPropertyStr(ctx,this_val,'OnCallBack'); 45 | if JS_IsFunction(ctx,OnCallBack) then 46 | begin 47 | res := JS_Call(ctx,OnCallBack,this_val,argc,argv); 48 | if JS_IsException(res) then 49 | exit(res); 50 | Writeln('OnCallBack return = ', JS_ToBool(ctx,res)); 51 | end; 52 | finally 53 | JS_FreeValue(ctx,OnCallBack); 54 | JS_FreeCString(ctx, Module); 55 | JS_FreeCString(ctx, API); 56 | end; 57 | end; 58 | end; 59 | 60 | function CConstructor(ctx : JSContext; new_target : JSValueConst; argc : Integer; argv : PJSValueConstArr): JSValue; cdecl; 61 | begin 62 | Result := JS_NewObjectClass(ctx,API_Class_id); 63 | // New Array for every new instance. 64 | JS_DefinePropertyValueStr(ctx,Result,'args',JS_NewArray(ctx),JS_PROP_CONFIGURABLE or JS_PROP_WRITABLE); 65 | end; 66 | 67 | procedure Emu_mod_init(ctx : JSContext; m : JSModuleDef); cdecl; 68 | var 69 | obj : JSValue; 70 | begin 71 | obj := JS_NewCFunction2(ctx, @CConstructor, 'ApiHook', 1, JS_CFUNC_constructor, 0); 72 | JS_SetModuleExport(ctx, m, 'ApiHook', obj); 73 | end; 74 | 75 | function Emu_init(ctx : JSContext; m : JSModuleDef): Integer;cdecl; 76 | begin 77 | Emu_mod_init(ctx,m); 78 | Result := 0; 79 | end; 80 | 81 | procedure RegisterNativeClass(ctx : JSContext); cdecl; 82 | var 83 | obj,global : JSValue; 84 | begin 85 | 86 | // Create New Class id. 87 | JS_NewClassID(@API_Class_id); 88 | // Create the Class Name and other stuff. 89 | JS_NewClass(JS_GetRuntime(ctx),API_Class_id,@JClass); 90 | 91 | // Properties list. 92 | tab[0] := JS_CFUNC_DEF('install', 1, install); 93 | tab[1] := JS_PROP_INT32_DEF('version', 1337, JS_PROP_CONFIGURABLE);// add "or JS_PROP_WRITABLE" to make it writable. 94 | 95 | // New Object act as Prototype for the Class. 96 | API_Class_Proto := JS_NewObject(ctx); 97 | 98 | // Set list of Properties to the prototype Object. 99 | JS_SetPropertyFunctionList(ctx,API_Class_Proto,@tab,Length(tab)); 100 | 101 | // Set the Prototype to the Class. 102 | JS_SetClassProto(ctx, API_Class_id, API_Class_Proto); 103 | 104 | // Set the Class native constructor. 105 | obj := JS_NewCFunction2(ctx, @CConstructor, 'ApiHook', 1, JS_CFUNC_constructor, 0); 106 | 107 | // Add the Class to Global Object so we can use it. 108 | global := JS_GetGlobalObject(ctx); 109 | JS_SetPropertyStr(ctx,global,'ApiHook',obj); 110 | JS_FreeValue(ctx,global); 111 | end; 112 | 113 | end. 114 | 115 | -------------------------------------------------------------------------------- /QJSPascalDemo/rawexecution.pas: -------------------------------------------------------------------------------- 1 | unit RawExecution; 2 | 3 | {$mode delphi} 4 | 5 | interface 6 | 7 | uses 8 | Classes, SysUtils, quickjs; 9 | 10 | const 11 | fib : array [0..130] of byte =( 12 | $01,$03,$2c,$65,$78,$61,$6d,$70, 13 | $6c,$65,$73,$2f,$66,$69,$62,$5f, 14 | $6d,$6f,$64,$75,$6c,$65,$2e,$6a, 15 | $73,$06,$66,$69,$62,$02,$6e,$0e, 16 | $90,$03,$00,$01,$00,$00,$92,$03, 17 | $00,$00,$0d,$00,$02,$01,$9e,$01, 18 | $00,$00,$00,$01,$01,$01,$04,$00, 19 | $92,$03,$00,$01,$c3,$00,$e4,$29, 20 | $90,$03,$01,$04,$01,$00,$03,$14, 21 | $0d,$43,$02,$01,$92,$03,$01,$00, 22 | $01,$04,$01,$00,$1a,$01,$94,$03, 23 | $00,$01,$00,$92,$03,$00,$00,$d4, 24 | $b8,$ab,$ed,$03,$b8,$28,$d4,$b9, 25 | $b0,$ed,$03,$b9,$28,$e0,$d4,$b9, 26 | $a5,$f2,$e0,$d4,$ba,$a5,$f2,$a4, 27 | $28,$90,$03,$02,$06,$04,$1c,$08, 28 | $21,$08,$08 29 | ); 30 | hello : array [0..160] of byte = ( 31 | $01,$07,$30,$65,$78,$61,$6d,$70, 32 | $6c,$65,$73,$2f,$68,$65,$6c,$6c, 33 | $6f,$5f,$6d,$6f,$64,$75,$6c,$65, 34 | $2e,$6a,$73,$1e,$2e,$2f,$66,$69, 35 | $62,$5f,$6d,$6f,$64,$75,$6c,$65, 36 | $2e,$6a,$73,$06,$66,$69,$62,$0e, 37 | $63,$6f,$6e,$73,$6f,$6c,$65,$06, 38 | $6c,$6f,$67,$16,$48,$65,$6c,$6c, 39 | $6f,$20,$57,$6f,$72,$6c,$64,$10, 40 | $66,$69,$62,$28,$31,$30,$29,$3d, 41 | $0e,$90,$03,$01,$92,$03,$00,$00, 42 | $01,$00,$94,$03,$00,$0d,$00,$02, 43 | $01,$9e,$01,$00,$00,$00,$05,$01, 44 | $00,$2c,$00,$94,$03,$00,$0c,$36, 45 | $cb,$00,$00,$00,$40,$cc,$00,$00, 46 | $00,$04,$cd,$00,$00,$00,$24,$01, 47 | $00,$0e,$36,$cb,$00,$00,$00,$40, 48 | $cc,$00,$00,$00,$04,$ce,$00,$00, 49 | $00,$5f,$00,$00,$c0,$0a,$f2,$24, 50 | $02,$00,$29,$90,$03,$01,$02,$04, 51 | $62 52 | ); 53 | 54 | 55 | procedure RawTest(); 56 | 57 | implementation 58 | 59 | procedure RawTest(); 60 | var 61 | rt : JSRuntime; 62 | ctx : JSContext; 63 | begin 64 | rt := JS_NewRuntime; 65 | if Assigned(rt) then 66 | begin 67 | ctx := JS_NewContextRaw(rt); 68 | JS_AddIntrinsicBaseObjects(ctx); 69 | js_std_add_helpers(ctx,argc,argv); 70 | js_std_eval_binary(ctx,@fib,Length(fib),0); 71 | js_std_eval_binary(ctx,@hello,Length(hello),0); 72 | js_std_loop(ctx); 73 | JS_FreeContext(ctx); 74 | JS_FreeRuntime(rt); 75 | end; 76 | Writeln(); 77 | end; 78 | 79 | end. 80 | 81 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # QuickJS-Pascal 2 | 3 | ## QuickJS FreePascal / Delphi Bindings sync with the latest version [QuickJS](https://bellard.org/quickjs) Headers. 4 | 5 | ### I added a small demo in how to use it ```(it's a very easy engine to use)```. 6 | 7 | <br><hr><br> 8 | 9 | * Change log 10 | - Upload Static libs for Mac, IOS, Linux and dynamic ones for Windows 11 | - sync with QJS Version "2020-03-16" 12 | - Contains all function and patches from 13 | - Version "2019-10-27", "2019-12-21", "2020-01-05", "2020-01-19" 14 | - Dlls not updated yet ( Compile your own for now till I do ^_^ ) 15 | - sync with QJS Version "2019-09-18" 16 | - Dlls is update to QJS Version "2019-09-18" 17 | - Sync with QJS Version "2019-09-01" 18 | - DLLs not updated yet (will upload static libs for mac, linux and dynamic ones for windows ). 19 | - Add Delphi Support 20 | - Sync with QJS Version "2019-08-18" 21 | - Init FPC binding - QJS Version "2019-08-10" 22 | 23 | - **TODO** 24 | - Add more demos 25 | - update the implementation with every new update :V 26 | 27 | <br> 28 | 29 | # With ❤️ From Home. 30 | -------------------------------------------------------------------------------- /quickjs.pas: -------------------------------------------------------------------------------- 1 | { 2 | FreePascal / Delphi bindings for QuickJS Engine. 3 | 4 | Copyright(c) 2019-2020 Coldzer0 <Coldzer0 [at] protonmail.ch> 5 | 6 | Permission is hereby granted, free of charge, to any person obtaining a copy 7 | of this software and associated documentation files (the "Software"), to 8 | deal in the Software without restriction, including without limitation the 9 | rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 10 | sell copies of the Software, and to permit persons to whom the Software is 11 | furnished to do so, subject to the following conditions: 12 | 13 | The above copyright notice and this permission notice shall be included in 14 | all copies or substantial portions of the Software. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 21 | FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 22 | IN THE SOFTWARE. 23 | } 24 | 25 | unit QuickJS; // sync with version - "2020-04-12". 26 | 27 | {$IfDef FPC} 28 | {$MODE Delphi} 29 | {$PackRecords C} 30 | {$EndIf} 31 | 32 | {$IfDef FPC} 33 | {$IfNDef windows} 34 | {$LinkLib 'libquickjs.a'} 35 | {$EndIf} 36 | {$EndIf} 37 | 38 | {$IfDef FPC} 39 | {$IfNDef CPU64} 40 | {$Define JS_NAN_BOXING} 41 | {$ENDIF} 42 | {$ELSE} 43 | {$IfNDef CPUX64} 44 | {$Define JS_NAN_BOXING} 45 | {$ENDIF} 46 | {$ENDIF} 47 | 48 | interface 49 | 50 | uses 51 | math; 52 | 53 | {===============================================================================} 54 | { QuickJS Constants } 55 | {===============================================================================} 56 | const 57 | QJS_VERSION = '2020-04-12'; 58 | const 59 | { all tags with a reference count are negative } 60 | JS_TAG_FIRST = -11; { first negative tag } 61 | JS_TAG_BIG_DECIMAL = -11; 62 | JS_TAG_BIG_INT = -10; 63 | JS_TAG_BIG_FLOAT = -9; 64 | JS_TAG_SYMBOL = -8; 65 | JS_TAG_STRING = -7; 66 | JS_TAG_MODULE = -3; { used internally } 67 | JS_TAG_FUNCTION_BYTECODE = -2; { used internally } 68 | JS_TAG_OBJECT = -1; 69 | 70 | JS_TAG_INT = 0; 71 | JS_TAG_BOOL = 1; 72 | JS_TAG_NULL = 2; 73 | JS_TAG_UNDEFINED = 3; 74 | JS_TAG_UNINITIALIZED = 4; 75 | JS_TAG_CATCH_OFFSET = 5; 76 | JS_TAG_EXCEPTION = 6; 77 | JS_TAG_FLOAT64 = 7; 78 | { any larger tag is FLOAT64 if JS_NAN_BOXING } 79 | 80 | JS_FLOAT64_NAN = NaN; 81 | 82 | 83 | const 84 | { flags for object properties } 85 | JS_PROP_CONFIGURABLE = (1 shl 0); 86 | JS_PROP_WRITABLE = (1 shl 1); 87 | JS_PROP_ENUMERABLE = (1 shl 2); 88 | JS_PROP_C_W_E = (JS_PROP_CONFIGURABLE or JS_PROP_WRITABLE or JS_PROP_ENUMERABLE); 89 | JS_PROP_LENGTH = (1 shl 3); { used internally in Arrays } 90 | JS_PROP_TMASK = (3 shl 4); { mask for NORMAL, GETSET, VARREF, AUTOINIT } 91 | JS_PROP_NORMAL = (0 shl 4); 92 | JS_PROP_GETSET = (1 shl 4); 93 | JS_PROP_VARREF = (2 shl 4); { used internally } 94 | JS_PROP_AUTOINIT = (3 shl 4); { used internally } 95 | 96 | { flags for JS_DefineProperty } 97 | JS_PROP_HAS_SHIFT = 8; 98 | JS_PROP_HAS_CONFIGURABLE = (1 shl 8); 99 | JS_PROP_HAS_WRITABLE = (1 shl 9); 100 | JS_PROP_HAS_ENUMERABLE = (1 shl 10); 101 | JS_PROP_HAS_GET = (1 shl 11); 102 | JS_PROP_HAS_SET = (1 shl 12); 103 | JS_PROP_HAS_VALUE = (1 shl 13); 104 | 105 | { throw an exception if false would be returned / 106 | (JS_DefineProperty/JS_SetProperty) } 107 | JS_PROP_THROW = (1 shl 14); 108 | { throw an exception if false would be returned in strict mode / 109 | (JS_SetProperty) } 110 | JS_PROP_THROW_STRICT = (1 shl 15); 111 | JS_PROP_NO_ADD = (1 shl 16); { internal use } 112 | JS_PROP_NO_EXOTIC = (1 shl 17); { internal use } 113 | 114 | JS_DEFAULT_STACK_SIZE = (256 * 1024); 115 | 116 | { JS_Eval() flags } 117 | JS_EVAL_TYPE_GLOBAL = (0 shl 0); { global code (default) } 118 | JS_EVAL_TYPE_MODULE = (1 shl 0); { module code } 119 | JS_EVAL_TYPE_DIRECT = (2 shl 0); { direct call (internal use) } 120 | JS_EVAL_TYPE_INDIRECT = (3 shl 0); { indirect call (internal use) } 121 | JS_EVAL_TYPE_MASK = (3 shl 0); 122 | 123 | JS_EVAL_FLAG_STRICT = (1 shl 3); { force 'strict' mode } 124 | JS_EVAL_FLAG_STRIP = (1 shl 4); { force 'strip' mode } 125 | (* 126 | compile but do not run. The result is an object with a 127 | JS_TAG_FUNCTION_BYTECODE or JS_TAG_MODULE tag. It can be executed 128 | with JS_EvalFunction(). 129 | *) 130 | JS_EVAL_FLAG_COMPILE_ONLY = (1 shl 5); { internal use } 131 | 132 | { don't include the stack frames before this eval in the Error() backtraces } 133 | JS_EVAL_FLAG_BACKTRACE_BARRIER = (1 shl 6); 134 | 135 | { Object Writer/Reader (currently only used to handle precompiled code) } 136 | JS_WRITE_OBJ_BYTECODE = (1 shl 0); { allow function/module } 137 | JS_WRITE_OBJ_BSWAP = (1 shl 1); { byte swapped output } 138 | 139 | JS_READ_OBJ_BYTECODE = (1 shl 0); { allow function/module } 140 | JS_READ_OBJ_ROM_DATA = (1 shl 1); { avoid duplicating 'buf' data } 141 | 142 | { C property definition } 143 | JS_DEF_CFUNC = 0; 144 | JS_DEF_CGETSET = 1; 145 | JS_DEF_CGETSET_MAGIC = 2; 146 | JS_DEF_PROP_STRING = 3; 147 | JS_DEF_PROP_INT32 = 4; 148 | JS_DEF_PROP_INT64 = 5; 149 | JS_DEF_PROP_DOUBLE = 6; 150 | JS_DEF_PROP_UNDEFINED = 7; 151 | JS_DEF_OBJECT = 8; 152 | JS_DEF_ALIAS = 9; 153 | 154 | 155 | { C function definition } 156 | { JSCFunctionEnum } 157 | JS_CFUNC_generic = 0; 158 | JS_CFUNC_generic_magic = 1; 159 | JS_CFUNC_constructor = 2; 160 | JS_CFUNC_constructor_magic = 3; 161 | JS_CFUNC_constructor_or_func = 4; 162 | JS_CFUNC_constructor_or_func_magic = 5; 163 | JS_CFUNC_f_f = 6; 164 | JS_CFUNC_f_f_f = 7; 165 | JS_CFUNC_getter = 8; 166 | JS_CFUNC_setter = 9; 167 | JS_CFUNC_getter_magic = 10; 168 | JS_CFUNC_setter_magic = 11; 169 | JS_CFUNC_iterator_next = 12; 170 | 171 | JS_GPN_STRING_MASK = (1 shl 0); 172 | JS_GPN_SYMBOL_MASK = (1 shl 1); 173 | JS_GPN_PRIVATE_MASK = (1 shl 2); 174 | 175 | { only include the enumerable properties } 176 | JS_GPN_ENUM_ONLY = (1 shl 4); 177 | { set theJSPropertyEnum.is_enumerable field } 178 | JS_GPN_SET_ENUM = (1 shl 5); 179 | 180 | { C Call Flags } 181 | 182 | JS_CALL_FLAG_CONSTRUCTOR = (1 shl 0); 183 | {===============================================================================} 184 | {===============================================================================} 185 | 186 | type 187 | {$IFNDEF FPC} 188 | // Delphi Compatible. 189 | // Anything under XE4. 190 | {$IF (CompilerVersion <= 25)} 191 | PUint32 = ^Uint32; // PUint32 not defined in XE4 - Fix by @edwinyzh 192 | {$IFEND} 193 | pUInt8 = PByte; 194 | pInt8 = PShortInt; 195 | pInt16 = PSmallint; 196 | PInt32 = PLongint; 197 | {$ENDIF} 198 | {$ifdef cpu64} 199 | size_t = QWord; 200 | psize_t = ^size_t; 201 | {$else} 202 | size_t = Cardinal; 203 | psize_t = ^size_t; 204 | {$endif} 205 | 206 | JS_BOOL = Boolean; 207 | JSRuntime = Pointer; 208 | 209 | PPJSContext = ^_PJSContext; // Pointer to Pointer. 210 | _PJSContext = ^_JSContext; 211 | _JSContext = record end; // Empty record to mimic the JSContext. 212 | JSContext = Pointer; 213 | 214 | JSObject = Pointer; 215 | JSClass = Pointer; 216 | 217 | JSModuleDef = Pointer; 218 | 219 | JSString = Pointer; 220 | 221 | JSClassID = UInt32; 222 | PJSClassID = ^JSClassID; 223 | 224 | JSAtom = UInt32; 225 | 226 | JSCFunctionEnum = Integer; 227 | 228 | JSGCObjectHeader = Pointer; 229 | 230 | type 231 | PJSRefCountHeader = ^JSRefCountHeader; 232 | JSRefCountHeader = record 233 | ref_count : Integer; 234 | end; 235 | 236 | {$If Defined(JS_NAN_BOXING)} 237 | JSValue = UInt64; 238 | PJSValue = ^JSValue; 239 | JSValueConst = JSValue; 240 | PJSValueConst = ^JSValueConst; 241 | JSValueConstArr = array[0..(MaxInt div SizeOf(JSValueConst))-1] of JSValueConst; 242 | PJSValueConstArr = ^JSValueConstArr; 243 | const 244 | JS_FLOAT64_TAG_ADDEND = $7ff80000 - JS_TAG_FIRST + 1; // quiet NaN encoding 245 | JS_NAN = ($7ff8000000000000 - (JS_FLOAT64_TAG_ADDEND shl 32)); 246 | {$Else} 247 | type 248 | JSValueUnion = record 249 | case byte of 250 | 0 : (&int32 : int32); 251 | 1 : (float64 : Double); 252 | 2 : (Ptr : Pointer); 253 | end; 254 | 255 | JSValue = record 256 | u : JSValueUnion; 257 | tag : Int64; 258 | end; 259 | PJSValue = ^JSValue; 260 | JSValueConst = JSValue; 261 | PJSValueConst = ^JSValueConst; 262 | JSValueConstArr = array[0..(MaxInt div SizeOf(JSValueConst))-1] of JSValueConst; 263 | PJSValueConstArr = ^JSValueConstArr; 264 | {$ENDIF} 265 | type 266 | JSMallocState = record 267 | malloc_count, 268 | malloc_size, 269 | malloc_limit : size_t; 270 | opaque : Pointer; 271 | end; 272 | PJSMallocState = ^JSMallocState; 273 | 274 | //c_malloc = function (s : JSMallocState; size : UInt64) : Pointer; 275 | //Pc_malloc = ^c_malloc; 276 | // TODO: Check If funcs need to be Pointers or not. ^^^^^ 277 | JSMallocFunctions = record 278 | js_malloc : function (s : PJSMallocState; size : size_t) : Pointer; cdecl; 279 | js_free : procedure (s : PJSMallocState; Ptr : Pointer); cdecl; 280 | js_realloc : function (s : PJSMallocState; Ptr : Pointer ; size : size_t) : Pointer; cdecl; 281 | js_malloc_usable_size : function (Ptr : Pointer) : size_t; cdecl; 282 | end; 283 | PJSMallocFunctions = ^JSMallocFunctions; 284 | 285 | PJSMemoryUsage = ^JSMemoryUsage; 286 | JSMemoryUsage = record 287 | malloc_size, malloc_limit, memory_used_size, 288 | malloc_count, 289 | memory_used_count, 290 | atom_count, atom_size, 291 | str_count, str_size, 292 | obj_count, obj_size, 293 | prop_count, prop_size, 294 | shape_count, shape_size, 295 | js_func_count, js_func_size, js_func_code_size, 296 | js_func_pc2line_count, js_func_pc2line_size, 297 | c_func_count, array_count, 298 | fast_array_count, fast_array_elements, 299 | binary_object_count, binary_object_size : Int64; 300 | end; 301 | 302 | {===============================================================================} 303 | { Native Functions Callbcaks } 304 | {===============================================================================} 305 | 306 | PJSCFunction = ^JSCFunction; 307 | JSCFunction = function (ctx : JSContext; this_val : JSValueConst; 308 | argc : Integer; argv : PJSValueConstArr): JSValue; cdecl; 309 | 310 | PJSCFunctionMagic = ^JSCFunctionMagic; 311 | JSCFunctionMagic = function (ctx : JSContext; this_val : JSValueConst; 312 | argc : Integer; argv : PJSValueConst; magic : Integer): JSValue; cdecl; 313 | 314 | 315 | PJSCFunctionData = ^JSCFunctionData; 316 | JSCFunctionData = function (ctx : JSContext; this_val : JSValueConst; 317 | argc : Integer; argv : PJSValueConst; magic : Integer; 318 | func_data : PJSValue ): JSValue; cdecl; 319 | 320 | {===============================================================================} 321 | 322 | PJS_MarkFunc = ^JS_MarkFunc; 323 | JS_MarkFunc = procedure (rt : JSRuntime; gp : JSGCObjectHeader); cdecl; 324 | 325 | PJSClassFinalizer = ^JSClassFinalizer; 326 | JSClassFinalizer = procedure (rt : JSRuntime; val : JSValue); cdecl; 327 | 328 | PJSClassGCMark = ^JSClassGCMark; 329 | JSClassGCMark = procedure (rt : JSRuntime; val : JSValueConst; mark_func: PJS_MarkFunc); cdecl; 330 | 331 | PJSClassCall = ^JSClassCall; 332 | JSClassCall = function (ctx : JSContext; 333 | func_obj : JSValueConst; 334 | this_val : JSValueConst; 335 | argc : Integer; argv : PJSValueConst; 336 | flags : Integer) : JSValue; cdecl; 337 | 338 | PJSFreeArrayBufferDataFunc = ^JSFreeArrayBufferDataFunc; 339 | JSFreeArrayBufferDataFunc = procedure(rt : JSRuntime; opaque, Ptr : Pointer); cdecl; 340 | 341 | { return != 0 if the JS code needs to be interrupted } 342 | PJSInterruptHandler = ^JSInterruptHandler; 343 | JSInterruptHandler = function (rt : JSRuntime; opaque : Pointer): integer; cdecl; 344 | 345 | { return the module specifier (allocated with js_malloc()) or NULL if exception } 346 | PJSModuleNormalizeFunc = ^JSModuleNormalizeFunc; 347 | JSModuleNormalizeFunc = function (ctx : JSContext; 348 | const module_base_name , module_name : {$IFDEF FPC}PChar{$Else}PAnsiChar{$EndIf}; 349 | opaque : Pointer): {$IFDEF FPC}PChar{$Else}PAnsiChar{$EndIf}; cdecl; 350 | 351 | 352 | PJSModuleLoaderFunc = ^JSModuleLoaderFunc; 353 | JSModuleLoaderFunc = function (ctx : JSContext; module_name : {$IFDEF FPC}PChar{$Else}PAnsiChar{$EndIf}; opaque : Pointer) : JSModuleDef; cdecl; 354 | 355 | { JS Job support } 356 | PJSJobFunc = ^JSJobFunc; 357 | JSJobFunc = function (ctx : JSContext; argc : Integer; argv : PJSValueConst): JSValue; cdecl; 358 | 359 | 360 | { C module definition } 361 | PJSModuleInitFunc = ^JSModuleInitFunc; 362 | JSModuleInitFunc = function (ctx : JSContext; m : JSModuleDef): Integer; cdecl; 363 | 364 | { Promises RejectionTracker CallBack } 365 | 366 | { is_handled = TRUE means that the rejection is handled } 367 | PJSHostPromiseRejectionTracker = ^JSHostPromiseRejectionTracker; 368 | JSHostPromiseRejectionTracker = procedure(ctx : JSContext; 369 | promise, reason :JSValueConst; 370 | is_handled : JS_BOOL; opaque : Pointer); cdecl; 371 | {===============================================================================} 372 | 373 | { object class support } 374 | PPJSPropertyEnum = ^PJSPropertyEnum; 375 | PJSPropertyEnum = ^JSPropertyEnum; 376 | JSPropertyEnum = record 377 | is_enumerable : JS_BOOL; 378 | atom : JSAtom; 379 | end; 380 | 381 | PJSPropertyDescriptor = ^JSPropertyDescriptor; 382 | JSPropertyDescriptor = record 383 | flags : Integer; 384 | value, 385 | getter, 386 | setter : JSValue; 387 | end; 388 | 389 | PJSClassExoticMethods = ^JSClassExoticMethods; 390 | JSClassExoticMethods = record 391 | { Return -1 if exception (can only happen in case of Proxy object), 392 | FALSE if the property does not exists, TRUE if it exists. If 1 is 393 | returned, the property descriptor 'desc' is filled if != NULL. } 394 | get_own_property : function (ctx: JSContext; desc: PJSPropertyDescriptor; obj:JSValueConst; prop:JSAtom):Integer;cdecl; 395 | 396 | { '*ptab' should hold the '*plen' property keys. Return 0 if OK, 397 | -1 if exception. The 'is_enumerable' field is ignored. } 398 | get_own_property_names : function (ctx: JSContext; ptab:PPJSPropertyEnum; plen: pUInt32; obj:JSValueConst):Integer;cdecl; 399 | 400 | { return < 0 if exception, or TRUE/FALSE } 401 | delete_property : function (ctx: JSContext; obj:JSValueConst; prop:JSAtom):Integer;cdecl; 402 | 403 | { return < 0 if exception or TRUE/FALSE } 404 | define_own_property : function (ctx: JSContext; this_obj:JSValueConst; prop:JSAtom; val:JSValueConst; getter:JSValueConst; 405 | setter:JSValueConst; flags:Integer):Integer;cdecl; 406 | 407 | { The following methods can be emulated with the previous ones, 408 | so they are usually not needed } 409 | 410 | { return < 0 if exception or TRUE/FALSE } 411 | has_property : function (ctx: JSContext; obj:JSValueConst; atom:JSAtom):Integer;cdecl; 412 | get_property : function (ctx: JSContext; obj:JSValueConst; atom:JSAtom; receiver:JSValueConst):JSValue;cdecl; 413 | set_property : function (ctx: JSContext; obj:JSValueConst; atom:JSAtom; value:JSValueConst; receiver:JSValueConst; 414 | flags:Integer):Integer;cdecl; 415 | end; 416 | 417 | PJSClassDef = ^JSClassDef; 418 | JSClassDef = record 419 | class_name : {$IFDEF FPC}PChar{$Else}PAnsiChar{$EndIf}; 420 | finalizer : PJSClassFinalizer; 421 | gc_mark : PJSClassGCMark; 422 | { 423 | if call != NULL, the object is a function. If (flags & 424 | JS_CALL_FLAG_CONSTRUCTOR) != 0, the function is called as a 425 | constructor. In this case, 'this_val' is new.target. A 426 | constructor call only happens if the object constructor bit is 427 | set (see JS_SetConstructorBit()) 428 | } 429 | call : PJSClassCall; 430 | { XXX: suppress this indirection ? It is here only to save memory 431 | because only a few classes need these methods } 432 | exotic : PJSClassExoticMethods; 433 | end; 434 | 435 | { C function definition } 436 | 437 | constructor_magic_func = function (ctx: JSContext; new_target:JSValueConst; argc:Integer; argv:PJSValueConst; 438 | magic:Integer):JSValue; cdecl; 439 | f_f_func = function (_para1:double):double cdecl; 440 | f_f_f_func = function (_para1:double; _para2:double):double; cdecl; 441 | Getter_func = function (ctx: JSContext; this_val:JSValueConst):JSValue; cdecl; 442 | Setter_func = function (ctx: JSContext; this_val:JSValueConst; val:JSValueConst):JSValue;cdecl; 443 | getter_magic_func = function (ctx: JSContext; this_val:JSValueConst; magic:Integer):JSValue; cdecl; 444 | setter_magic_func = function (ctx: JSContext; this_val:JSValueConst; val:JSValueConst; magic:Integer):JSValue; cdecl; 445 | iterator_next_func = function (ctx: JSContext; this_val:JSValueConst; argc:Integer; argv:PJSValueConst; pdone:PInteger; 446 | magic:Integer):JSValue; cdecl; 447 | JSCFunctionType = record 448 | case Integer of 449 | 0 : ( generic : JSCFunction ); 450 | 1 : ( generic_magic : JSCFunctionMagic); 451 | 2 : ( &constructor : JSCFunction ); 452 | 3 : ( constructor_magic : constructor_magic_func); 453 | 4 : ( constructor_or_func : JSCFunction ); 454 | 5 : ( f_f : f_f_func); 455 | 6 : ( f_f_f : f_f_f_func); 456 | 7 : ( getter : Getter_func); 457 | 8 : ( setter : Setter_func); 458 | 9 : ( getter_magic : getter_magic_func); 459 | 10 : ( setter_magic : setter_magic_func); 460 | 11 : ( iterator_next : iterator_next_func); 461 | end; 462 | PJSCFunctionType = ^JSCFunctionType; 463 | 464 | { C property definition } 465 | JSCFunctionListEntry = record 466 | name : {$IFDEF FPC}PChar{$Else}PAnsiChar{$EndIf}; 467 | prop_flags : UInt8; 468 | def_type : UInt8; 469 | magic : Int16; 470 | u : record 471 | case Integer of 472 | 0 : ( func : record 473 | length : UInt8; { XXX: should move outside union } 474 | cproto : UInt8; { XXX: should move outside union } 475 | cfunc : JSCFunctionType; 476 | end ); 477 | 1 : ( getset : record 478 | get : JSCFunctionType; 479 | _set : JSCFunctionType; 480 | end ); 481 | 2 : ( alias : record 482 | name : {$IFDEF FPC}PChar{$Else}PAnsiChar{$EndIf}; 483 | base : Integer; 484 | end ); 485 | 3 : ( prop_list : record 486 | tab : ^JSCFunctionListEntry; 487 | len : Integer; 488 | end ); 489 | 4 : ( str : {$IFDEF FPC}PChar{$Else}PAnsiChar{$EndIf} ); 490 | 5 : ( i32 : Int32 ); 491 | 6 : ( i64 : Int64 ); 492 | 7 : ( f64 : double ); 493 | end; 494 | end; 495 | PJSCFunctionListEntry = ^JSCFunctionListEntry; 496 | 497 | {$IFDEF mswindows}const QJSDLL = {$IfDef WIN64}'quickjs64.dll'{$Else}'quickjs32.dll'{$EndIf};{$endif} 498 | { QuickJS external APIs } 499 | 500 | function JS_NewRuntime : JSRuntime; cdecl; external {$IFDEF mswindows}QJSDLL{$endif}; 501 | (* info lifetime must exceed that of rt *) 502 | procedure JS_SetRuntimeInfo(rt : JSRuntime; const info : {$IFDEF FPC}PChar{$Else}PAnsiChar{$EndIf}); cdecl; external {$IFDEF mswindows}QJSDLL{$endif}; 503 | procedure JS_SetMemoryLimit(rt : JSRuntime; limit : size_t); cdecl; external {$IFDEF mswindows}QJSDLL{$endif}; 504 | procedure JS_SetGCThreshold(rt : JSRuntime; gc_threshold : size_t); cdecl; external {$IFDEF mswindows}QJSDLL{$endif}; 505 | procedure JS_SetMaxStackSize(ctx: JSContext; stack_size:size_t); cdecl; external {$IFDEF mswindows}QJSDLL{$endif}; 506 | 507 | function JS_NewRuntime2(const mf : PJSMallocFunctions; opaque : Pointer) : JSRuntime; cdecl; external {$IFDEF mswindows}QJSDLL{$endif}; 508 | procedure JS_FreeRuntime(rt : JSRuntime); cdecl; external {$IFDEF mswindows}QJSDLL{$endif}; 509 | function JS_GetRuntimeOpaque(rt : JSRuntime) : Pointer; cdecl; external {$IFDEF mswindows}QJSDLL{$endif}; 510 | procedure JS_SetRuntimeOpaque(rt : JSRuntime; opaque : Pointer); cdecl; external {$IFDEF mswindows}QJSDLL{$endif}; 511 | 512 | 513 | procedure JS_MarkValue(rt:JSRuntime; val:JSValueConst; mark_func:PJS_MarkFunc);cdecl; external {$IFDEF mswindows}QJSDLL{$endif}; 514 | procedure JS_RunGC(rt:JSRuntime); cdecl; external {$IFDEF mswindows}QJSDLL{$endif}; 515 | 516 | function JS_IsLiveObject(rt:JSRuntime; obj:JSValueConst):JS_BOOL; cdecl; external {$IFDEF mswindows}QJSDLL{$endif}; 517 | //{REMOVE}function JS_IsInGCSweep(rt:JSRuntime):JS_BOOL; cdecl; external {$IFDEF mswindows}QJSDLL{$endif}; 518 | function JS_NewContext(rt:JSRuntime):JSContext; cdecl; external {$IFDEF mswindows}QJSDLL{$endif}; 519 | procedure JS_FreeContext(s: JSContext); cdecl; external {$IFDEF mswindows}QJSDLL{$endif}; 520 | function JS_DupContext(ctx : JSContext) : JSContext; cdecl; external {$IFDEF mswindows}QJSDLL{$endif}; 521 | function JS_GetContextOpaque(ctx: JSContext):pointer; cdecl; external {$IFDEF mswindows}QJSDLL{$endif}; 522 | procedure JS_SetContextOpaque(ctx: JSContext; opaque:pointer); cdecl; external {$IFDEF mswindows}QJSDLL{$endif}; 523 | function JS_GetRuntime(ctx: JSContext):JSRuntime; cdecl; external {$IFDEF mswindows}QJSDLL{$endif}; 524 | procedure JS_SetClassProto(ctx: JSContext; class_id:JSClassID; obj:JSValue); cdecl; external {$IFDEF mswindows}QJSDLL{$endif}; 525 | function JS_GetClassProto(ctx: JSContext; class_id:JSClassID):JSValue; cdecl; external {$IFDEF mswindows}QJSDLL{$endif}; 526 | 527 | { the following functions are used to select the intrinsic object to save memory } 528 | 529 | function JS_NewContextRaw(rt: JSRuntime): JSContext; cdecl; external {$IFDEF mswindows}QJSDLL{$endif}; 530 | procedure JS_AddIntrinsicBaseObjects(ctx: JSContext); cdecl; external {$IFDEF mswindows}QJSDLL{$endif}; 531 | procedure JS_AddIntrinsicDate(ctx: JSContext); cdecl; external {$IFDEF mswindows}QJSDLL{$endif}; 532 | procedure JS_AddIntrinsicEval(ctx: JSContext); cdecl; external {$IFDEF mswindows}QJSDLL{$endif}; 533 | procedure JS_AddIntrinsicStringNormalize(ctx: JSContext); cdecl; external {$IFDEF mswindows}QJSDLL{$endif}; 534 | procedure JS_AddIntrinsicRegExpCompiler(ctx: JSContext); cdecl; external {$IFDEF mswindows}QJSDLL{$endif}; 535 | procedure JS_AddIntrinsicRegExp(ctx: JSContext); cdecl; external {$IFDEF mswindows}QJSDLL{$endif}; 536 | procedure JS_AddIntrinsicJSON(ctx: JSContext); cdecl; external {$IFDEF mswindows}QJSDLL{$endif}; 537 | procedure JS_AddIntrinsicProxy(ctx: JSContext); cdecl; external {$IFDEF mswindows}QJSDLL{$endif}; 538 | procedure JS_AddIntrinsicMapSet(ctx: JSContext); cdecl; external {$IFDEF mswindows}QJSDLL{$endif}; 539 | procedure JS_AddIntrinsicTypedArrays(ctx: JSContext); cdecl; external {$IFDEF mswindows}QJSDLL{$endif}; 540 | procedure JS_AddIntrinsicPromise(ctx: JSContext); cdecl; external {$IFDEF mswindows}QJSDLL{$endif}; 541 | 542 | procedure JS_AddIntrinsicBigInt(ctx: JSContext); cdecl; external {$IFDEF mswindows}QJSDLL{$endif}; 543 | procedure JS_AddIntrinsicBigFloat(ctx: JSContext); cdecl; external {$IFDEF mswindows}QJSDLL{$endif}; 544 | procedure JS_AddIntrinsicBigDecimal(ctx: JSContext); cdecl; external {$IFDEF mswindows}QJSDLL{$endif}; 545 | { enable operator overloading } 546 | procedure JS_AddIntrinsicOperators(ctx: JSContext); cdecl; external {$IFDEF mswindows}QJSDLL{$endif}; 547 | { enable "use math" } 548 | procedure JS_EnableBignumExt(ctx: JSContext; enable : JS_BOOL); cdecl; external {$IFDEF mswindows}QJSDLL{$endif}; 549 | 550 | function js_string_codePointRange(ctx: JSContext; this_val:JSValueConst; argc:Integer; argv:PJSValueConst):JSValue; cdecl; external {$IFDEF mswindows}QJSDLL{$endif}; 551 | 552 | function js_malloc_rt(rt: JSRuntime; size:size_t):pointer; cdecl; external {$IFDEF mswindows}QJSDLL{$endif}; 553 | procedure js_free_rt(rt: JSRuntime; ptr:pointer); cdecl; external {$IFDEF mswindows}QJSDLL{$endif}; 554 | function js_realloc_rt(rt: JSRuntime; ptr:pointer; size:size_t):pointer; cdecl; external {$IFDEF mswindows}QJSDLL{$endif}; 555 | function js_malloc_usable_size_rt(rt: JSRuntime; ptr:pointer):size_t; cdecl; external {$IFDEF mswindows}QJSDLL{$endif}; 556 | function js_mallocz_rt(rt: JSRuntime; size:size_t):pointer; cdecl; external {$IFDEF mswindows}QJSDLL{$endif}; 557 | function js_malloc(ctx: JSContext; size:size_t):pointer; cdecl; external {$IFDEF mswindows}QJSDLL{$endif}; 558 | procedure js_free(ctx: JSContext; ptr:pointer); cdecl; external {$IFDEF mswindows}QJSDLL{$endif}; 559 | function js_realloc(ctx: JSContext; ptr:pointer; size:size_t):pointer; cdecl; external {$IFDEF mswindows}QJSDLL{$endif}; 560 | function js_malloc_usable_size(ctx: JSContext; ptr:pointer):size_t; cdecl; external {$IFDEF mswindows}QJSDLL{$endif}; 561 | function js_realloc2(ctx: JSContext; ptr:pointer; size:size_t; pslack:Psize_t):pointer; cdecl; external {$IFDEF mswindows}QJSDLL{$endif}; 562 | function js_mallocz(ctx: JSContext; size:size_t):pointer; cdecl; external {$IFDEF mswindows}QJSDLL{$endif}; 563 | function js_strdup(ctx: JSContext; str:{$IFDEF FPC}PChar{$Else}PAnsiChar{$EndIf}): {$IFDEF FPC}PChar{$Else}PAnsiChar{$EndIf}; cdecl; external {$IFDEF mswindows}QJSDLL{$endif}; 564 | 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}; 565 | 566 | procedure JS_ComputeMemoryUsage(rt: JSRuntime; s:PJSMemoryUsage); cdecl; external {$IFDEF mswindows}QJSDLL{$endif}; 567 | procedure JS_DumpMemoryUsage(fp: Pointer; s:PJSMemoryUsage; rt: JSRuntime); cdecl; external {$IFDEF mswindows}QJSDLL{$endif}; 568 | 569 | { atom support } 570 | 571 | function JS_NewAtomLen(ctx: JSContext; str:{$IFDEF FPC}PChar{$Else}PAnsiChar{$EndIf}; len:size_t):JSAtom; cdecl; external {$IFDEF mswindows}QJSDLL{$endif}; 572 | function JS_NewAtom(ctx: JSContext; str:{$IFDEF FPC}PChar{$Else}PAnsiChar{$EndIf}):JSAtom; cdecl; external {$IFDEF mswindows}QJSDLL{$endif}; 573 | function JS_NewAtomUInt32(ctx: JSContext; n:UInt32):JSAtom; cdecl; external {$IFDEF mswindows}QJSDLL{$endif}; 574 | function JS_DupAtom(ctx: JSContext; v:JSAtom):JSAtom; cdecl; external {$IFDEF mswindows}QJSDLL{$endif}; 575 | procedure JS_FreeAtom(ctx: JSContext; v:JSAtom); cdecl; external {$IFDEF mswindows}QJSDLL{$endif}; 576 | procedure JS_FreeAtomRT(rt: JSRuntime; v:JSAtom); cdecl; external {$IFDEF mswindows}QJSDLL{$endif}; 577 | function JS_AtomToValue(ctx: JSContext; atom:JSAtom):JSValue; cdecl; external {$IFDEF mswindows}QJSDLL{$endif}; 578 | function JS_AtomToString(ctx: JSContext; atom:JSAtom):JSValue; cdecl; external {$IFDEF mswindows}QJSDLL{$endif}; 579 | function JS_AtomToCString(ctx: JSContext; atom:JSAtom):{$IFDEF FPC}PChar{$Else}PAnsiChar{$EndIf}; cdecl; external {$IFDEF mswindows}QJSDLL{$endif}; 580 | function JS_ValueToAtom(ctx: JSContext; val:JSValueConst) : JSAtom; cdecl; external {$IFDEF mswindows}QJSDLL{$endif}; 581 | 582 | { object class support } 583 | 584 | function JS_NewClassID(pclass_id:PJSClassID):JSClassID; cdecl; external {$IFDEF mswindows}QJSDLL{$endif}; 585 | function JS_NewClass(rt: JSRuntime; class_id:JSClassID; class_def: PJSClassDef):Integer; cdecl; external {$IFDEF mswindows}QJSDLL{$endif}; 586 | function JS_IsRegisteredClass(rt: JSRuntime; class_id:JSClassID):Integer; cdecl; external {$IFDEF mswindows}QJSDLL{$endif}; 587 | 588 | 589 | { JS Numbers } 590 | 591 | function JS_NewBigInt64 (ctx : JSContext; v : Int64): JSValue; cdecl; external {$IFDEF mswindows}QJSDLL{$endif}; 592 | function JS_NewBigUint64 (ctx : JSContext; v : UInt64): JSValue; cdecl; external {$IFDEF mswindows}QJSDLL{$endif}; 593 | 594 | 595 | function JS_Throw(ctx: JSContext; obj:JSValue):JSValue; cdecl; external {$IFDEF mswindows}QJSDLL{$endif}; 596 | function JS_GetException(ctx: JSContext):JSValue; cdecl; external {$IFDEF mswindows}QJSDLL{$endif}; 597 | function JS_IsError(ctx: JSContext; val:JSValueConst):JS_BOOL; cdecl; external {$IFDEF mswindows}QJSDLL{$endif}; 598 | procedure JS_ResetUncatchableError(ctx: JSContext); cdecl; external {$IFDEF mswindows}QJSDLL{$endif}; 599 | function JS_NewError(ctx: JSContext):JSValue; cdecl; external {$IFDEF mswindows}QJSDLL{$endif}; 600 | function JS_ThrowSyntaxError(ctx: JSContext; fmt : {$IFDEF FPC}PChar{$Else}PAnsiChar{$EndIf}; args : Array of Const): JSValue; cdecl; external {$IFDEF mswindows}QJSDLL{$endif}; 601 | function JS_ThrowTypeError(ctx: JSContext; fmt : {$IFDEF FPC}PChar{$Else}PAnsiChar{$EndIf}; args : Array of Const): JSValue; cdecl; external {$IFDEF mswindows}QJSDLL{$endif}; 602 | function JS_ThrowReferenceError(ctx: JSContext; fmt : {$IFDEF FPC}PChar{$Else}PAnsiChar{$EndIf}; args : Array of Const): JSValue; cdecl; external {$IFDEF mswindows}QJSDLL{$endif}; 603 | function JS_ThrowRangeError(ctx: JSContext; fmt : {$IFDEF FPC}PChar{$Else}PAnsiChar{$EndIf}; args : Array of Const): JSValue; cdecl; external {$IFDEF mswindows}QJSDLL{$endif}; 604 | function JS_ThrowInternalError(ctx: JSContext; fmt : {$IFDEF FPC}PChar{$Else}PAnsiChar{$EndIf}; args : Array of Const): JSValue; cdecl; external {$IFDEF mswindows}QJSDLL{$endif}; 605 | function JS_ThrowOutOfMemory(ctx: JSContext): JSValue; cdecl; external {$IFDEF mswindows}QJSDLL{$endif}; 606 | 607 | procedure __JS_FreeValue(ctx: JSContext; v : JSValue); cdecl; external {$IFDEF mswindows}QJSDLL{$endif}; 608 | procedure __JS_FreeValueRT(rt: JSRuntime; v : JSValue); cdecl; external {$IFDEF mswindows}QJSDLL{$endif}; 609 | 610 | { JS Values - return -1 for JS_EXCEPTION } 611 | 612 | function JS_ToBool(ctx: JSContext; val:JSValueConst):Integer; cdecl; external {$IFDEF mswindows}QJSDLL{$endif}; 613 | function JS_ToInt32(ctx: JSContext; pres:pInt32; val:JSValueConst):Integer;cdecl; external {$IFDEF mswindows}QJSDLL{$endif}; 614 | 615 | function JS_ToInt64(ctx: JSContext; pres:PInt64; val:JSValueConst):Integer; cdecl; external {$IFDEF mswindows}QJSDLL{$endif}; 616 | function JS_ToIndex(ctx: JSContext; plen:PUInt64; val:JSValueConst):Integer; cdecl; external {$IFDEF mswindows}QJSDLL{$endif}; 617 | function JS_ToFloat64(ctx: JSContext; pres:PDouble; val:JSValueConst):Integer; cdecl; external {$IFDEF mswindows}QJSDLL{$endif}; 618 | { return an exception if 'val' is a Number } 619 | function JS_ToBigInt64(ctx: JSContext; pres:PInt64; val:JSValueConst):Integer; cdecl; external {$IFDEF mswindows}QJSDLL{$endif}; 620 | { same as JS_ToInt64() but allow BigInt } 621 | function JS_ToInt64Ext(ctx: JSContext; pres:PInt64; val:JSValueConst):Integer; cdecl; external {$IFDEF mswindows}QJSDLL{$endif}; 622 | 623 | function JS_NewStringLen(ctx:JSContext; str1:{$IFDEF FPC}PChar{$Else}PAnsiChar{$EndIf}; len1: size_t):JSValue; cdecl; external {$IFDEF mswindows}QJSDLL{$endif}; 624 | function JS_NewString(ctx:JSContext; str:{$IFDEF FPC}PChar{$Else}PAnsiChar{$EndIf}):JSValue; cdecl; external {$IFDEF mswindows}QJSDLL{$endif}; 625 | function JS_NewAtomString(ctx:JSContext; str:{$IFDEF FPC}PChar{$Else}PAnsiChar{$EndIf}):JSValue; cdecl; external {$IFDEF mswindows}QJSDLL{$endif}; 626 | function JS_ToString(ctx:JSContext; val:JSValueConst):JSValue; cdecl; external {$IFDEF mswindows}QJSDLL{$endif}; 627 | function JS_ToPropertyKey(ctx:JSContext; val:JSValueConst):JSValue; cdecl; external {$IFDEF mswindows}QJSDLL{$endif}; 628 | 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}; 629 | 630 | 631 | 632 | procedure JS_FreeCString(ctx:JSContext; ptr:{$IFDEF FPC}PChar{$Else}PAnsiChar{$EndIf}); cdecl; external {$IFDEF mswindows}QJSDLL{$endif}; 633 | function JS_NewObjectProtoClass(ctx:JSContext; proto:JSValueConst; class_id:JSClassID):JSValue; cdecl; external {$IFDEF mswindows}QJSDLL{$endif}; 634 | function JS_NewObjectClass(ctx:JSContext; class_id:JSClassID):JSValue; cdecl; external {$IFDEF mswindows}QJSDLL{$endif}; 635 | function JS_NewObjectProto(ctx:JSContext; proto:JSValueConst):JSValue; cdecl; external {$IFDEF mswindows}QJSDLL{$endif}; 636 | function JS_NewObject(ctx:JSContext):JSValue; cdecl; external {$IFDEF mswindows}QJSDLL{$endif}; 637 | function JS_IsFunction(ctx:JSContext; val:JSValueConst):JS_BOOL; cdecl; external {$IFDEF mswindows}QJSDLL{$endif}; 638 | function JS_IsConstructor(ctx:JSContext; val:JSValueConst):JS_BOOL; cdecl; external {$IFDEF mswindows}QJSDLL{$endif}; 639 | function JS_SetConstructorBit(ctx:JSContext; func_obj : JSValueConst; val:JS_BOOL):JS_BOOL; cdecl; external {$IFDEF mswindows}QJSDLL{$endif}; 640 | function JS_NewArray(ctx:JSContext):JSValue; cdecl; external {$IFDEF mswindows}QJSDLL{$endif}; 641 | function JS_IsArray(ctx:JSContext; val:JSValueConst):Integer; cdecl; external {$IFDEF mswindows}QJSDLL{$endif}; 642 | function JS_GetPropertyInternal(ctx:JSContext; obj:JSValueConst; prop:JSAtom; 643 | receiver:JSValueConst; throw_ref_error:JS_BOOL):JSValue; cdecl; external {$IFDEF mswindows}QJSDLL{$endif}; 644 | 645 | function JS_GetPropertyStr(ctx:JSContext; this_obj:JSValueConst; prop:{$IFDEF FPC}PChar{$Else}PAnsiChar{$EndIf}):JSValue; cdecl; external {$IFDEF mswindows}QJSDLL{$endif}; 646 | function JS_GetPropertyUint32(ctx:JSContext; this_obj:JSValueConst; idx:UInt32):JSValue; cdecl; external {$IFDEF mswindows}QJSDLL{$endif}; 647 | function JS_SetPropertyInternal(ctx:JSContext; this_obj:JSValueConst; 648 | prop:JSAtom; val:JSValue; flags:Integer):Integer; cdecl; external {$IFDEF mswindows}QJSDLL{$endif}; 649 | 650 | function JS_SetPropertyUint32(ctx:JSContext; this_obj:JSValueConst; idx:UInt32; val:JSValue):Integer;cdecl; external {$IFDEF mswindows}QJSDLL{$endif}; 651 | function JS_SetPropertyInt64(ctx:JSContext; this_obj:JSValueConst; idx:Int64; val:JSValue):Integer;cdecl; external {$IFDEF mswindows}QJSDLL{$endif}; 652 | function JS_SetPropertyStr(ctx:JSContext; this_obj:JSValueConst; prop:{$IFDEF FPC}PChar{$Else}PAnsiChar{$EndIf}; val:JSValue):Integer;cdecl; external {$IFDEF mswindows}QJSDLL{$endif}; 653 | function JS_HasProperty(ctx:JSContext; this_obj:JSValueConst; prop:JSAtom):Integer;cdecl; external {$IFDEF mswindows}QJSDLL{$endif}; 654 | function JS_IsExtensible(ctx:JSContext; obj:JSValueConst):Integer;cdecl; external {$IFDEF mswindows}QJSDLL{$endif}; 655 | function JS_PreventExtensions(ctx:JSContext; obj:JSValueConst):Integer;cdecl; external {$IFDEF mswindows}QJSDLL{$endif}; 656 | function JS_DeleteProperty(ctx:JSContext; obj:JSValueConst; prop:JSAtom; flags:Integer):Integer;cdecl; external {$IFDEF mswindows}QJSDLL{$endif}; 657 | function JS_SetPrototype(ctx:JSContext; obj:JSValueConst; proto_val:JSValueConst):Integer;cdecl; external {$IFDEF mswindows}QJSDLL{$endif}; 658 | function JS_GetPrototype(ctx:JSContext; val:JSValueConst):JSValueConst;cdecl; external {$IFDEF mswindows}QJSDLL{$endif}; 659 | 660 | function JS_GetOwnPropertyNames(ctx: JSContext; ptab:PPJSPropertyEnum; plen: pUInt32; obj:JSValueConst; flags : Integer): Integer;cdecl; external {$IFDEF mswindows}QJSDLL{$endif}; 661 | function JS_GetOwnProperty(ctx: JSContext; desc : PJSPropertyDescriptor; obj : JSValueConst; prop : JSAtom): Integer; cdecl; external {$IFDEF mswindows}QJSDLL{$endif}; 662 | 663 | { 'buf' must be zero terminated i.e. buf[buf_len] := #0. } 664 | 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}; 665 | function JS_JSONStringify(ctx:JSContext; obj, replacer, space0 : JSValueConst):JSValue;cdecl; external {$IFDEF mswindows}QJSDLL{$endif}; 666 | function JS_Call(ctx:JSContext; func_obj:JSValueConst; this_obj:JSValueConst; argc:Integer; argv:PJSValueConstArr):JSValue;cdecl; external {$IFDEF mswindows}QJSDLL{$endif}; 667 | function JS_Invoke(ctx:JSContext; this_val:JSValueConst; atom:JSAtom; argc:Integer; argv:PJSValueConst):JSValue;cdecl; external {$IFDEF mswindows}QJSDLL{$endif}; 668 | function JS_CallConstructor(ctx:JSContext; func_obj:JSValueConst; argc:Integer; argv:PJSValueConst):JSValue;cdecl; external {$IFDEF mswindows}QJSDLL{$endif}; 669 | function JS_CallConstructor2(ctx:JSContext; func_obj:JSValueConst; new_target:JSValueConst; argc:Integer; argv:PJSValueConst):JSValue;cdecl; external {$IFDEF mswindows}QJSDLL{$endif}; 670 | function JS_DetectModule(const input:{$IFDEF FPC}PChar{$Else}PAnsiChar{$EndIf}; input_len : size_t):JS_BOOL;cdecl; external {$IFDEF mswindows}QJSDLL{$endif}; 671 | { 'input' must be zero terminated i.e. buf[buf_len] := #0. } 672 | 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}; 673 | function JS_EvalFunction(ctx:JSContext; fun_obj : JSValue):JSValue;cdecl; external {$IFDEF mswindows}QJSDLL{$endif}; 674 | function JS_GetGlobalObject(ctx:JSContext):JSValue;cdecl; external {$IFDEF mswindows}QJSDLL{$endif}; 675 | function JS_IsInstanceOf(ctx:JSContext; val:JSValueConst; obj:JSValueConst):Integer;cdecl; external {$IFDEF mswindows}QJSDLL{$endif}; 676 | function JS_DefineProperty(ctx:JSContext; this_obj:JSValueConst; prop:JSAtom; val:JSValueConst; getter:JSValueConst; 677 | setter:JSValueConst; flags:Integer):Integer;cdecl; external {$IFDEF mswindows}QJSDLL{$endif}; 678 | 679 | function JS_DefinePropertyValue(ctx:JSContext; this_obj:JSValueConst; prop:JSAtom; val:JSValue; flags:Integer):Integer;cdecl; external {$IFDEF mswindows}QJSDLL{$endif}; 680 | function JS_DefinePropertyValueUint32(ctx:JSContext; this_obj:JSValueConst; idx:UInt32; val:JSValue; flags:Integer):Integer;cdecl; external {$IFDEF mswindows}QJSDLL{$endif}; 681 | 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}; 682 | function JS_DefinePropertyGetSet(ctx:JSContext; this_obj:JSValueConst; prop:JSAtom; getter:JSValue; setter:JSValue; 683 | flags:Integer):Integer;cdecl; external {$IFDEF mswindows}QJSDLL{$endif}; 684 | 685 | procedure JS_SetOpaque(obj:JSValue; opaque:pointer);cdecl; external {$IFDEF mswindows}QJSDLL{$endif}; 686 | function JS_GetOpaque(obj:JSValueConst; class_id:JSClassID):pointer;cdecl; external {$IFDEF mswindows}QJSDLL{$endif}; 687 | function JS_GetOpaque2(ctx:JSContext; obj:JSValueConst; class_id:JSClassID):pointer;cdecl; external {$IFDEF mswindows}QJSDLL{$endif}; 688 | 689 | function JS_NewArrayBuffer(ctx:JSContext; buf:pUInt8; len:size_t; free_func:PJSFreeArrayBufferDataFunc; opaque:pointer; 690 | is_shared:JS_BOOL):JSValue;cdecl; external {$IFDEF mswindows}QJSDLL{$endif}; 691 | function JS_NewArrayBufferCopy(ctx:JSContext; buf:pUInt8; len:size_t):JSValue;cdecl; external {$IFDEF mswindows}QJSDLL{$endif}; 692 | procedure JS_DetachArrayBuffer(ctx:JSContext; obj:JSValueConst);cdecl; external {$IFDEF mswindows}QJSDLL{$endif}; 693 | function JS_GetArrayBuffer(ctx:JSContext; psize:Psize_t; obj:JSValueConst):pUInt8;cdecl; external {$IFDEF mswindows}QJSDLL{$endif}; 694 | 695 | function JS_GetTypedArrayBuffer(ctx : JSContext; obj : JSValueConst; 696 | pbyte_offset, pbyte_length, pbytes_per_element : psize_t):JSValue;cdecl; external {$IFDEF mswindows}QJSDLL{$endif}; 697 | 698 | function JS_NewPromiseCapability(ctx:JSContext; resolving_funcs:PJSValue):JSValue;cdecl; external {$IFDEF mswindows}QJSDLL{$endif}; 699 | 700 | procedure JS_SetHostPromiseRejectionTracker(rt: JSRuntime; 701 | cb : PJSHostPromiseRejectionTracker; opaque : Pointer); cdecl; external {$IFDEF mswindows}QJSDLL{$endif}; 702 | 703 | procedure JS_SetInterruptHandler(rt:JSRuntime; cb:PJSInterruptHandler; opaque:pointer);cdecl; external {$IFDEF mswindows}QJSDLL{$endif}; 704 | 705 | { if can_block is TRUE, Atomics.wait() can be used } 706 | procedure JS_SetCanBlock(rt:JSRuntime; can_block:JS_BOOL);cdecl; external {$IFDEF mswindows}QJSDLL{$endif}; 707 | 708 | { module_normalize = NULL is allowed and invokes the default module 709 | filename normalizer } 710 | procedure JS_SetModuleLoaderFunc(rt:JSRuntime; 711 | module_normalize:PJSModuleNormalizeFunc; 712 | module_loader:PJSModuleLoaderFunc; opaque:pointer);cdecl; external {$IFDEF mswindows}QJSDLL{$endif}; 713 | 714 | { JS Job support } 715 | 716 | function JS_EnqueueJob(ctx:JSContext; job_func:PJSJobFunc; argc:Integer; argv:PJSValueConst):Integer;cdecl; external {$IFDEF mswindows}QJSDLL{$endif}; 717 | function JS_IsJobPending(rt:JSRuntime):JS_BOOL;cdecl; external {$IFDEF mswindows}QJSDLL{$endif}; 718 | // TODO: Check pctx if the type is right. 719 | function JS_ExecutePendingJob(rt:JSRuntime; pctx: PPJSContext):Integer; cdecl; external {$IFDEF mswindows}QJSDLL{$endif}; 720 | 721 | { Object Writer/Reader (currently only used to handle precompiled code) } 722 | { allow function/module } 723 | 724 | function JS_WriteObject(ctx: JSContext; psize:psize_t; obj:JSValueConst; flags:Integer):pUInt8; cdecl; external {$IFDEF mswindows}QJSDLL{$endif}; 725 | function JS_ReadObject(ctx: JSContext; buf:pUInt8; buf_len:size_t; flags:Integer):JSValue; cdecl; external {$IFDEF mswindows}QJSDLL{$endif}; 726 | { 727 | load the dependencies of the module 'obj'. Useful when JS_ReadObject() 728 | returns a module. 729 | } 730 | function JS_ResolveModule(ctx: JSContext; obj : JSValueConst):Integer; cdecl; external {$IFDEF mswindows}QJSDLL{$endif}; 731 | 732 | { C function definition } 733 | 734 | procedure JS_SetConstructor(ctx : JSContext; func_obj, proto : JSValueConst);cdecl; external {$IFDEF mswindows}QJSDLL{$endif}; 735 | 736 | function JS_NewCFunction2(ctx: JSContext; func:PJSCFunction; name:{$IFDEF FPC}PChar{$Else}PAnsiChar{$EndIf}; length:Integer; cproto:JSCFunctionEnum; 737 | magic:Integer):JSValue; cdecl; external {$IFDEF mswindows}QJSDLL{$endif}; 738 | function JS_NewCFunctionData(ctx: JSContext; func:PJSCFunctionData; length:Integer; magic:Integer; data_len:Integer; 739 | data:PJSValueConst):JSValue; cdecl; external {$IFDEF mswindows}QJSDLL{$endif}; 740 | 741 | procedure JS_SetPropertyFunctionList(ctx: JSContext; obj:JSValueConst; 742 | tab:PJSCFunctionListEntry; len:Integer); cdecl; external {$IFDEF mswindows}QJSDLL{$endif}; 743 | 744 | { C module definition } 745 | 746 | function JS_NewCModule(ctx: JSContext; name_str:{$IFDEF FPC}PChar{$Else}PAnsiChar{$EndIf}; func:PJSModuleInitFunc): JSModuleDef; cdecl; external {$IFDEF mswindows}QJSDLL{$endif}; 747 | 748 | { can only be called before the module is instantiated } 749 | function JS_AddModuleExport(ctx: JSContext; m: JSModuleDef; name_str:{$IFDEF FPC}PChar{$Else}PAnsiChar{$EndIf}):Integer; cdecl; external {$IFDEF mswindows}QJSDLL{$endif}; 750 | function JS_AddModuleExportList(ctx: JSContext; m: JSModuleDef; tab:PJSCFunctionListEntry; len:Integer):Integer; cdecl; external {$IFDEF mswindows}QJSDLL{$endif}; 751 | 752 | { can only be called after the module is instantiated } 753 | function JS_SetModuleExport(ctx: JSContext; m: JSModuleDef; export_name:{$IFDEF FPC}PChar{$Else}PAnsiChar{$EndIf}; val:JSValue):Integer; cdecl; external {$IFDEF mswindows}QJSDLL{$endif}; 754 | function JS_SetModuleExportList(ctx: JSContext; m: JSModuleDef; tab:PJSCFunctionListEntry; len:Integer):Integer; cdecl; external {$IFDEF mswindows}QJSDLL{$endif}; 755 | 756 | { return the import.meta object of a module } 757 | function JS_GetImportMeta(ctx: JSContext; m: JSModuleDef) : JSValue; cdecl; external {$IFDEF mswindows}QJSDLL{$endif}; 758 | function JS_GetModuleName(ctx: JSContext; m: JSModuleDef) : JSAtom; cdecl; external {$IFDEF mswindows}QJSDLL{$endif}; 759 | 760 | { QuickJS libc } 761 | function js_init_module_std(ctx: JSContext; module_name:{$IFDEF FPC}PChar{$Else}PAnsiChar{$EndIf}):JSModuleDef;cdecl; external {$IFDEF mswindows}QJSDLL{$endif}; 762 | function js_init_module_os(ctx: JSContext; module_name:{$IFDEF FPC}PChar{$Else}PAnsiChar{$EndIf}):JSModuleDef;cdecl; external {$IFDEF mswindows}QJSDLL{$endif}; 763 | procedure js_std_add_helpers(ctx : JSContext; argc : Integer; argv : Pointer);cdecl; external {$IFDEF mswindows}QJSDLL{$endif}; 764 | procedure js_std_loop(ctx : JSContext); cdecl; external {$IFDEF mswindows}QJSDLL{$endif}; 765 | procedure js_std_free_handlers(rt:JSRuntime);cdecl; external {$IFDEF mswindows}QJSDLL{$endif}; 766 | procedure js_std_dump_error(ctx:JSContext);cdecl; external {$IFDEF mswindows}QJSDLL{$endif}; 767 | function js_load_file(ctx:JSContext; pbuf_len: psize_t; filename:{$IFDEF FPC}PChar{$Else}PAnsiChar{$EndIf}): Pointer;cdecl; external {$IFDEF mswindows}QJSDLL{$endif}; 768 | function js_module_loader(ctx:JSContext; module_name:{$IFDEF FPC}PChar{$Else}PAnsiChar{$EndIf}; opaque:pointer):JSModuleDef;cdecl; external {$IFDEF mswindows}QJSDLL{$endif}; 769 | procedure js_std_eval_binary(ctx : JSContext; buf : Pointer; buf_len : size_t; flags : Integer); cdecl; external {$IFDEF mswindows}QJSDLL{$endif}; 770 | function js_module_set_import_meta(ctx : JSContext; func_val : JSValueConst; use_realpath, is_main : JS_BOOL) : Integer; cdecl; external {$IFDEF mswindows}QJSDLL{$endif}; 771 | procedure js_std_promise_rejection_tracker(ctx : JSContext; 772 | promise, reason : JSValueConst; is_handled : JS_BOOL; opaque : Pointer); cdecl; external {$IFDEF mswindows}QJSDLL{$endif}; 773 | 774 | { internal implementations} 775 | 776 | function JS_VALUE_GET_TAG(v : JSValue): Int64; 777 | { same as JS_VALUE_GET_TAG, but return JS_TAG_FLOAT64 with NaN boxing } 778 | function JS_VALUE_GET_NORM_TAG(v : JSValue): Int64; 779 | function JS_VALUE_IS_NAN(v : JSValue) : JS_BOOL; inline; 780 | function JS_VALUE_GET_INT(v : JSValue): Integer; 781 | function JS_VALUE_GET_BOOL(v : JSValue): Boolean; 782 | function JS_VALUE_GET_FLOAT64(v : JSValue): Double; 783 | function JS_VALUE_GET_PTR(v : JSValue): Pointer; 784 | function JS_MKVAL(tag : Int64; val : Int32): JSValue; 785 | function JS_MKPTR(tag : Int64; ptr : Pointer): JSValue; 786 | function JS_TAG_IS_FLOAT64(tag : Int64): Boolean; inline; 787 | {$IfNDef JS_NAN_BOXING} 788 | function JS_NAN : JSValue; 789 | {$EndIf} 790 | function __JS_NewFloat64({%H-}ctx : JSContext; d : Double): JSValue; 791 | 792 | function JS_VALUE_IS_BOTH_INT(v1, v2 : JSValue): Boolean; 793 | function JS_VALUE_IS_BOTH_FLOAT(v1, v2 : JSValue): Boolean; 794 | function JS_VALUE_GET_OBJ(v : JSValue): JSObject; 795 | function JS_VALUE_GET_STRING(v : JSValue): JSString; 796 | function JS_VALUE_HAS_REF_COUNT(v : JSValue): Boolean; 797 | 798 | { special values } 799 | 800 | function JS_NULL : JSValue; 801 | function JS_UNDEFINED : JSValue; 802 | function JS_FALSE : JSValue; 803 | function JS_TRUE : JSValue; 804 | function JS_EXCEPTION : JSValue; 805 | function JS_UNINITIALIZED : JSValue; 806 | 807 | { value handling } 808 | 809 | function JS_NewBool({%H-}ctx : JSContext; val : JS_BOOL): JSValue; inline; 810 | function JS_NewInt32( {%H-}ctx : JSContext; val : Int32): JSValue; inline; 811 | function JS_NewCatchOffset( {%H-}ctx : JSContext; val : Int32): JSValue; inline; 812 | function JS_NewFloat64(ctx : JSContext; d : Double): JSValue; 813 | function JS_IsBigInt(v : JSValueConst): JS_BOOL; inline; 814 | function JS_IsBigFloat(v : JSValueConst): JS_BOOL; inline; 815 | function JS_IsBigDecimal(v : JSValueConst): JS_BOOL; inline; 816 | function JS_IsBool(v : JSValueConst): JS_BOOL; inline; 817 | function JS_IsNull(v : JSValueConst): JS_BOOL; inline; 818 | function JS_IsUndefined(v : JSValueConst): JS_BOOL; inline; 819 | function JS_IsException(v : JSValueConst): JS_BOOL; inline; 820 | function JS_IsUninitialized(v : JSValueConst): JS_BOOL; inline; 821 | function JS_IsString(v : JSValueConst): JS_BOOL; inline; 822 | function JS_IsNumber(v : JSValueConst): JS_BOOL; inline; 823 | function JS_IsSymbol(v : JSValueConst): JS_BOOL; inline; 824 | function JS_IsObject(v : JSValueConst): JS_BOOL; inline; 825 | 826 | procedure JS_FreeValue(ctx : JSContext; v : JSValue); inline; 827 | procedure JS_FreeValueRT(rt : JSRuntime; v : JSValue); inline; 828 | function JS_DupValue({%H-}ctx : JSContext; v : JSValueConst) : JSValue; inline; 829 | function JS_DupValueRT({%H-}rt : JSRuntime; v : JSValueConst) : JSValue; inline; 830 | 831 | function JS_ToUint32(ctx : JSContext; pres : pUInt32; val : JSValueConst): Integer; inline; 832 | function JS_ToCStringLen(ctx : JSContext; plen : psize_t; val : JSValueConst): {$IFDEF FPC}PChar{$Else}PAnsiChar{$EndIf}; inline; 833 | function JS_ToCString(ctx : JSContext; val : JSValueConst): {$IFDEF FPC}PChar{$Else}PAnsiChar{$EndIf}; inline; 834 | function JS_GetProperty(ctx : JSContext; this_obj : JSValueConst; prop : JSAtom): JSValue; inline; 835 | function JS_SetProperty(ctx : JSContext; this_obj : JSValueConst; prop : JSAtom; val : JSValue): Integer; inline; 836 | 837 | { C function definition } 838 | 839 | function JS_NewCFunction(ctx : JSContext; func : PJSCFunction; name : {$IFDEF FPC}PChar{$Else}PAnsiChar{$EndIf}; length : Integer): JSValue; inline; 840 | function JS_NewCFunctionMagic(ctx : JSContext; func : PJSCFunctionMagic; name : {$IFDEF FPC}PChar{$Else}PAnsiChar{$EndIf}; length : Integer; 841 | cproto : JSCFunctionEnum; magic : Integer): JSValue; inline; 842 | 843 | 844 | { C property definition } 845 | 846 | function JS_CFUNC_DEF(name : {$IFDEF FPC}PChar{$Else}PAnsiChar{$EndIf}; length : Integer; func : JSCFunction) : JSCFunctionListEntry; 847 | function JS_CFUNC_MAGIC_DEF(name : {$IFDEF FPC}PChar{$Else}PAnsiChar{$EndIf}; length : Integer; func : JSCFunctionMagic; magic : Int16) : JSCFunctionListEntry; 848 | function JS_CFUNC_SPECIAL_DEF(name : {$IFDEF FPC}PChar{$Else}PAnsiChar{$EndIf}; length : Integer; cproto : JSCFunctionEnum ; func : f_f_func) : JSCFunctionListEntry; overload; 849 | function JS_CFUNC_SPECIAL_DEF(name : {$IFDEF FPC}PChar{$Else}PAnsiChar{$EndIf}; length : Integer; cproto : JSCFunctionEnum ; func : f_f_f_func) : JSCFunctionListEntry; overload; 850 | function JS_ITERATOR_NEXT_DEF(name : {$IFDEF FPC}PChar{$Else}PAnsiChar{$EndIf}; length : Integer; iterator_next : iterator_next_func; magic : Int16) : JSCFunctionListEntry; 851 | function JS_CGETSET_DEF(name : {$IFDEF FPC}PChar{$Else}PAnsiChar{$EndIf}; fgetter : Getter_func; fsetter : Setter_func) : JSCFunctionListEntry; 852 | 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; 853 | function JS_PROP_STRING_DEF(name : {$IFDEF FPC}PChar{$Else}PAnsiChar{$EndIf}; val : {$IFDEF FPC}PChar{$Else}PAnsiChar{$EndIf}; prop_flags : UInt8) : JSCFunctionListEntry; 854 | function JS_PROP_INT32_DEF(name : {$IFDEF FPC}PChar{$Else}PAnsiChar{$EndIf}; val : Int32; prop_flags : UInt8) : JSCFunctionListEntry; 855 | function JS_PROP_INT64_DEF(name : {$IFDEF FPC}PChar{$Else}PAnsiChar{$EndIf}; val : Int64; prop_flags : UInt8) : JSCFunctionListEntry; 856 | function JS_PROP_DOUBLE_DEF(name : {$IFDEF FPC}PChar{$Else}PAnsiChar{$EndIf}; val : Double; prop_flags : UInt8) : JSCFunctionListEntry; 857 | function JS_PROP_UNDEFINED_DEF(name : {$IFDEF FPC}PChar{$Else}PAnsiChar{$EndIf}; prop_flags : UInt8) : JSCFunctionListEntry; 858 | function JS_OBJECT_DEF(name : {$IFDEF FPC}PChar{$Else}PAnsiChar{$EndIf}; tab : PJSCFunctionListEntry; length : Integer; prop_flags : UInt8) : JSCFunctionListEntry; 859 | function JS_ALIAS_DEF(name, from : {$IFDEF FPC}PChar{$Else}PAnsiChar{$EndIf}) : JSCFunctionListEntry; 860 | function JS_ALIAS_BASE_DEF(name, from : {$IFDEF FPC}PChar{$Else}PAnsiChar{$EndIf}; base : Integer) : JSCFunctionListEntry; 861 | 862 | 863 | var 864 | OldFPUMask : TFPUExceptionMask; 865 | 866 | implementation 867 | 868 | {$If Defined(JS_NAN_BOXING)} 869 | 870 | function JS_VALUE_GET_TAG(v : JSValue): Int64; 871 | begin 872 | Result := Integer(v shr 32); 873 | end; 874 | 875 | function JS_VALUE_GET_INT(v : JSValue): Integer; 876 | begin 877 | Result := Integer(v); 878 | end; 879 | 880 | function JS_VALUE_GET_BOOL(v : JSValue): Boolean; 881 | begin 882 | Result := Boolean(v); 883 | end; 884 | 885 | function JS_VALUE_GET_PTR(v : JSValue): Pointer; 886 | begin 887 | Result := {%H-}Pointer(v); // TODO: check if this works the right way. 888 | end; 889 | 890 | function JS_MKVAL(tag : Int64; val : Int32): JSValue; 891 | begin 892 | Result := tag shl 32 or val; 893 | end; 894 | 895 | function JS_MKPTR(tag : Int64; ptr : Pointer): JSValue; 896 | begin 897 | Result := JSValue((tag shl 32) or UIntPtr(ptr)); 898 | end; 899 | 900 | function JS_VALUE_GET_FLOAT64(v : JSValue): Double; 901 | type 902 | rec = record 903 | case Byte of 904 | 0 : (v : JSValue); 905 | 1 : (d : Double); 906 | end; 907 | var 908 | u : rec; 909 | begin 910 | u.v := v; 911 | u.v {$IfDef FPC}+={$Else} := u.v +{$EndIf} UInt64(JS_FLOAT64_TAG_ADDEND shl 32); 912 | Result := u.d; 913 | end; 914 | 915 | function __JS_NewFloat64({%H-}ctx : JSContext; d : Double): JSValue; 916 | type 917 | rec = record 918 | case Byte of 919 | 0 : (d : Double); 920 | 1 : (u64 : UInt64); 921 | end; 922 | var 923 | u : rec; 924 | v : JSValue; 925 | begin 926 | u.d := d; 927 | { normalize NaN } 928 | if ((u.u64 and $7fffffffffffffff) > $7ff0000000000000) then 929 | v := UInt64(JS_NAN) 930 | else 931 | v := u.u64 - UInt64(JS_FLOAT64_TAG_ADDEND shl 32); 932 | Result := v; 933 | end; 934 | 935 | function JS_TAG_IS_FLOAT64(tag : Int64): Boolean; inline; 936 | begin 937 | Result := Boolean( UInt64((tag) - JS_TAG_FIRST) >= (JS_TAG_FLOAT64 - JS_TAG_FIRST) ); 938 | end; 939 | 940 | { same as JS_VALUE_GET_TAG, but return JS_TAG_FLOAT64 with NaN boxing } 941 | function JS_VALUE_GET_NORM_TAG(v : JSValue): Int64; 942 | var 943 | tag : UInt32; 944 | begin 945 | tag := JS_VALUE_GET_TAG(v); 946 | if JS_TAG_IS_FLOAT64(tag) then 947 | Result := JS_TAG_FLOAT64 948 | else 949 | Result := tag; 950 | end; 951 | 952 | function JS_VALUE_IS_NAN(v : JSValue) : JS_BOOL; inline; 953 | begin 954 | Result := (JS_VALUE_GET_TAG(v) = (JS_NAN shr 32)); 955 | end; 956 | 957 | {$else} 958 | 959 | function JS_VALUE_GET_TAG(v : JSValue): Int64; 960 | begin 961 | Result := v.tag; 962 | end; 963 | { same as JS_VALUE_GET_TAG, but return JS_TAG_FLOAT64 with NaN boxing } 964 | function JS_VALUE_GET_NORM_TAG(v : JSValue): Int64; 965 | begin 966 | Result := JS_VALUE_GET_TAG(v); 967 | end; 968 | 969 | function JS_VALUE_GET_INT(v : JSValue): Integer; 970 | begin 971 | Result := v.u.&int32; 972 | end; 973 | 974 | function JS_VALUE_GET_BOOL(v : JSValue): Boolean; 975 | begin 976 | Result := Boolean(v.u.&int32); 977 | end; 978 | 979 | function JS_VALUE_GET_FLOAT64(v : JSValue): Double; 980 | begin 981 | Result := v.u.float64; 982 | end; 983 | 984 | function JS_VALUE_GET_PTR(v : JSValue): Pointer; 985 | begin 986 | Result := v.u.Ptr; 987 | end; 988 | 989 | function JS_MKVAL(tag : Int64; val : Int32): JSValue; 990 | begin 991 | Result.u.&int32 := val; 992 | Result.tag := tag; 993 | end; 994 | 995 | function JS_MKPTR(tag : Int64; ptr : Pointer): JSValue; 996 | begin 997 | Result.u.Ptr := ptr; 998 | Result.tag := tag; 999 | end; 1000 | 1001 | function JS_TAG_IS_FLOAT64(tag : Int64): Boolean; inline; 1002 | begin 1003 | Result := UInt64(tag) = JS_TAG_FLOAT64; 1004 | end; 1005 | 1006 | function JS_NAN : JSValue; 1007 | begin 1008 | Result.u.float64 := JS_FLOAT64_NAN; 1009 | Result.tag := JS_TAG_FLOAT64; 1010 | end; 1011 | 1012 | function __JS_NewFloat64({%H-}ctx : JSContext; d : Double): JSValue; 1013 | begin 1014 | Result.u.float64 := d; 1015 | Result.tag := JS_TAG_FLOAT64; 1016 | end; 1017 | 1018 | function JS_VALUE_IS_NAN(v : JSValue) : JS_BOOL; inline; 1019 | type 1020 | UnionRec = record 1021 | case Byte of 1022 | 0 : (d : Double); 1023 | 1 : (u64 : UInt64); 1024 | end; 1025 | var 1026 | u : UnionRec; 1027 | begin 1028 | if (v.tag <> JS_TAG_FLOAT64) then 1029 | Exit(False); 1030 | u.d := v.u.float64; 1031 | Result := (u.u64 and $7fffffffffffffff) > $7ff0000000000000; 1032 | end; 1033 | 1034 | {$ENDIF} 1035 | 1036 | function JS_VALUE_IS_BOTH_INT(v1, v2 : JSValue): Boolean; 1037 | begin 1038 | Result := ((JS_VALUE_GET_TAG(v1) or JS_VALUE_GET_TAG(v2)) = JS_TAG_INT); 1039 | end; 1040 | 1041 | function JS_VALUE_IS_BOTH_FLOAT(v1, v2 : JSValue): Boolean; 1042 | begin 1043 | Result := (JS_TAG_IS_FLOAT64(JS_VALUE_GET_TAG(v1)) and JS_TAG_IS_FLOAT64(JS_VALUE_GET_TAG(v2))) 1044 | end; 1045 | 1046 | function JS_VALUE_GET_OBJ(v : JSValue): JSObject; 1047 | begin 1048 | Result := JS_VALUE_GET_PTR(v); 1049 | end; 1050 | 1051 | function JS_VALUE_GET_STRING(v : JSValue): JSString; 1052 | begin 1053 | Result := JS_VALUE_GET_PTR(v); 1054 | end; 1055 | 1056 | function JS_VALUE_HAS_REF_COUNT(v : JSValue): Boolean; 1057 | begin 1058 | Result := UInt64(JS_VALUE_GET_TAG(v)) >= UInt64(JS_TAG_FIRST); 1059 | end; 1060 | 1061 | { special values } 1062 | 1063 | function JS_NULL : JSValue; 1064 | begin 1065 | Result := JS_MKVAL(JS_TAG_NULL, 0); 1066 | end; 1067 | 1068 | function JS_UNDEFINED : JSValue; 1069 | begin 1070 | Result := JS_MKVAL(JS_TAG_UNDEFINED, 0); 1071 | end; 1072 | 1073 | function JS_FALSE : JSValue; 1074 | begin 1075 | Result := JS_MKVAL(JS_TAG_BOOL, 0); 1076 | end; 1077 | 1078 | function JS_TRUE : JSValue; 1079 | begin 1080 | Result := JS_MKVAL(JS_TAG_BOOL, 1); 1081 | end; 1082 | 1083 | function JS_EXCEPTION : JSValue; 1084 | begin 1085 | Result := JS_MKVAL(JS_TAG_EXCEPTION, 0); 1086 | end; 1087 | 1088 | function JS_UNINITIALIZED : JSValue; 1089 | begin 1090 | Result := JS_MKVAL(JS_TAG_UNINITIALIZED, 0); 1091 | end; 1092 | 1093 | { value handling } 1094 | 1095 | function JS_NewBool({%H-}ctx : JSContext; val : JS_BOOL): JSValue; 1096 | begin 1097 | Result := JS_MKVAL(JS_TAG_BOOL, Int32(val)); 1098 | end; 1099 | 1100 | function JS_NewInt32( {%H-}ctx : JSContext; val : Int32): JSValue; inline; 1101 | begin 1102 | Result := JS_MKVAL(JS_TAG_INT, val); 1103 | end; 1104 | 1105 | function JS_NewCatchOffset( {%H-}ctx : JSContext; val : Int32): JSValue; inline; 1106 | begin 1107 | Result := JS_MKVAL(JS_TAG_CATCH_OFFSET, val); 1108 | end; 1109 | 1110 | function JS_NewInt64(ctx : JSContext; val : Int64): JSValue; 1111 | begin 1112 | if val = Int32(val) then 1113 | Result := JS_NewInt32(ctx, val) 1114 | else 1115 | Result := __JS_NewFloat64(ctx, val); 1116 | end; 1117 | 1118 | function JS_NewUint32(ctx : JSContext; val : UInt32): JSValue; 1119 | begin 1120 | if val <= $7fffffff then 1121 | Result := JS_NewInt32(ctx, val) 1122 | else 1123 | Result := __JS_NewFloat64(ctx, val); 1124 | end; 1125 | 1126 | function JS_NewFloat64(ctx : JSContext; d : Double): JSValue; 1127 | type 1128 | rec = record 1129 | case Byte of 1130 | 0 : (d : Double); 1131 | 1 : (u : UInt64); 1132 | end; 1133 | var 1134 | u,t : rec; 1135 | val : Int32; 1136 | begin 1137 | u.d := d; 1138 | val := Int32(Round(d)); 1139 | t.d := val; 1140 | { -0 cannot be represented as integer, so we compare the bit representation } 1141 | if u.u = t.u then 1142 | Result := JS_MKVAL(JS_TAG_INT, val) 1143 | else 1144 | Result := __JS_NewFloat64(ctx, d); 1145 | end; 1146 | 1147 | function JS_IsBigInt(v : JSValueConst): Boolean; inline; 1148 | begin 1149 | Result := Boolean(JS_VALUE_GET_TAG(v) = JS_TAG_BIG_INT); 1150 | end; 1151 | 1152 | function JS_IsBigFloat(v : JSValueConst): Boolean; inline; 1153 | begin 1154 | Result := Boolean(JS_VALUE_GET_TAG(v) = JS_TAG_BIG_FLOAT); 1155 | end; 1156 | 1157 | function JS_IsBigDecimal(v : JSValueConst): JS_BOOL; inline; 1158 | begin 1159 | Result := Boolean(JS_VALUE_GET_TAG(v) = JS_TAG_BIG_DECIMAL); 1160 | end; 1161 | 1162 | function JS_IsBool(v : JSValueConst): JS_BOOL; inline; 1163 | begin 1164 | Result := Boolean(JS_VALUE_GET_TAG(v) = JS_TAG_BOOL); 1165 | end; 1166 | 1167 | function JS_IsNull(v : JSValueConst): Boolean; inline; 1168 | begin 1169 | Result := Boolean(JS_VALUE_GET_TAG(v) = JS_TAG_NULL); 1170 | end; 1171 | 1172 | function JS_IsUndefined(v : JSValueConst): Boolean; inline; 1173 | begin 1174 | Result := Boolean(JS_VALUE_GET_TAG(v) = JS_TAG_UNDEFINED); 1175 | end; 1176 | 1177 | function JS_IsException(v : JSValueConst): Boolean; inline; 1178 | begin 1179 | Result := Boolean(JS_VALUE_GET_TAG(v) = JS_TAG_EXCEPTION); 1180 | end; 1181 | 1182 | function JS_IsUninitialized(v : JSValueConst): Boolean; inline; 1183 | begin 1184 | Result := Boolean(JS_VALUE_GET_TAG(v) = JS_TAG_UNINITIALIZED); 1185 | end; 1186 | 1187 | function JS_IsString(v : JSValueConst): Boolean; inline; 1188 | begin 1189 | Result := Boolean(JS_VALUE_GET_TAG(v) = JS_TAG_STRING); 1190 | end; 1191 | 1192 | function JS_IsNumber(v: JSValueConst): JS_BOOL; inline; 1193 | var 1194 | tag : Integer; 1195 | begin 1196 | tag := JS_VALUE_GET_TAG(v); 1197 | Result := (tag = JS_TAG_INT) or JS_TAG_IS_FLOAT64(tag); 1198 | end; 1199 | 1200 | function JS_IsSymbol(v : JSValueConst): Boolean; inline; 1201 | begin 1202 | Result := Boolean(JS_VALUE_GET_TAG(v) = JS_TAG_SYMBOL); 1203 | end; 1204 | 1205 | function JS_IsObject(v : JSValueConst): Boolean; inline; 1206 | begin 1207 | Result := Boolean(JS_VALUE_GET_TAG(v) = JS_TAG_OBJECT); 1208 | end; 1209 | 1210 | procedure JS_FreeValue(ctx : JSContext; v : JSValue); inline; 1211 | var 1212 | p : PJSRefCountHeader; 1213 | begin 1214 | if JS_VALUE_HAS_REF_COUNT(v) then 1215 | begin 1216 | p := PJSRefCountHeader(JS_VALUE_GET_PTR(v)); 1217 | Dec(p^.ref_count); 1218 | if (p^.ref_count <= 0) then 1219 | __JS_FreeValue(ctx, v); 1220 | end; 1221 | end; 1222 | 1223 | procedure JS_FreeValueRT(rt : JSRuntime; v : JSValue); inline; 1224 | var 1225 | p : PJSRefCountHeader; 1226 | begin 1227 | if JS_VALUE_HAS_REF_COUNT(v) then 1228 | begin 1229 | p := PJSRefCountHeader(JS_VALUE_GET_PTR(v)); 1230 | Dec(p^.ref_count); 1231 | if (p^.ref_count <= 0) then 1232 | __JS_FreeValueRT(rt, v); 1233 | end; 1234 | end; 1235 | 1236 | function JS_DupValue({%H-}ctx : JSContext; v : JSValueConst) : JSValue; inline; 1237 | var 1238 | p : PJSRefCountHeader; 1239 | begin 1240 | if JS_VALUE_HAS_REF_COUNT(v) then 1241 | begin 1242 | p := PJSRefCountHeader(JS_VALUE_GET_PTR(v)); 1243 | inc(p^.ref_count); 1244 | end; 1245 | Result := JSValue(v); 1246 | end; 1247 | 1248 | function JS_DupValueRT({%H-}rt : JSRuntime; v : JSValueConst) : JSValue; inline; 1249 | var 1250 | p : PJSRefCountHeader; 1251 | begin 1252 | if JS_VALUE_HAS_REF_COUNT(v) then 1253 | begin 1254 | p := PJSRefCountHeader(JS_VALUE_GET_PTR(v)); 1255 | inc(p^.ref_count); 1256 | end; 1257 | Result := JSValue(v); 1258 | end; 1259 | 1260 | function JS_ToUint32(ctx : JSContext; pres : pUInt32; val : JSValueConst): Integer; inline; 1261 | begin 1262 | Result := JS_ToInt32(ctx, pInt32(pres), val); 1263 | end; 1264 | 1265 | function JS_ToCStringLen(ctx : JSContext; plen : psize_t; val : JSValueConst): {$IFDEF FPC}PChar{$Else}PAnsiChar{$EndIf}; inline; 1266 | begin 1267 | Result := JS_ToCStringLen2(ctx, plen, val, False); 1268 | end; 1269 | 1270 | function JS_ToCString(ctx : JSContext; val : JSValueConst): {$IFDEF FPC}PChar{$Else}PAnsiChar{$EndIf}; inline; 1271 | begin 1272 | Result := JS_ToCStringLen2(ctx, nil, val, False); 1273 | end; 1274 | 1275 | function JS_GetProperty(ctx : JSContext; this_obj : JSValueConst; prop : JSAtom): JSValue; inline; 1276 | begin 1277 | Result := JS_GetPropertyInternal(ctx, this_obj, prop, this_obj, False); 1278 | end; 1279 | 1280 | function JS_SetProperty(ctx : JSContext; this_obj : JSValueConst; prop : JSAtom; val : JSValue): Integer; inline; 1281 | begin 1282 | Result := JS_SetPropertyInternal(ctx, this_obj, prop, val, JS_PROP_THROW); 1283 | end; 1284 | 1285 | { C function definition } 1286 | 1287 | function JS_NewCFunction(ctx : JSContext; func : PJSCFunction; name : {$IFDEF FPC}PChar{$Else}PAnsiChar{$EndIf}; length : Integer): JSValue; inline; 1288 | begin 1289 | Result := JS_NewCFunction2(ctx, func, name, length, JS_CFUNC_generic, 0); 1290 | end; 1291 | 1292 | function JS_NewCFunctionMagic(ctx : JSContext; func : PJSCFunctionMagic; name : {$IFDEF FPC}PChar{$Else}PAnsiChar{$EndIf}; length : Integer; 1293 | cproto : JSCFunctionEnum; magic : Integer): JSValue; inline; 1294 | begin 1295 | Result := JS_NewCFunction2(ctx, PJSCFunction(func), name, length, cproto, magic);; 1296 | end; 1297 | 1298 | { C property definition } 1299 | 1300 | function JS_CFUNC_DEF(name : {$IFDEF FPC}PChar{$Else}PAnsiChar{$EndIf}; length : Integer; 1301 | func : JSCFunction) : JSCFunctionListEntry; 1302 | begin 1303 | Result.name := name; 1304 | Result.prop_flags := JS_PROP_WRITABLE or JS_PROP_CONFIGURABLE; 1305 | Result.def_type := JS_DEF_CFUNC; 1306 | Result.magic := 0; 1307 | Result.u.func.length := length; 1308 | Result.u.func.cproto := JS_CFUNC_generic; 1309 | Result.u.func.cfunc.generic := func; 1310 | end; 1311 | 1312 | function JS_CFUNC_MAGIC_DEF(name : {$IFDEF FPC}PChar{$Else}PAnsiChar{$EndIf}; length : Integer; 1313 | func : JSCFunctionMagic; magic : Int16) : JSCFunctionListEntry; 1314 | begin 1315 | Result.name := name; 1316 | Result.prop_flags := JS_PROP_WRITABLE or JS_PROP_CONFIGURABLE; 1317 | Result.def_type := JS_DEF_CFUNC; 1318 | Result.magic := magic; 1319 | Result.u.func.length := length; 1320 | Result.u.func.cproto := JS_CFUNC_generic_magic; 1321 | Result.u.func.cfunc.generic_magic := func; 1322 | end; 1323 | 1324 | function JS_CFUNC_SPECIAL_DEF(name : {$IFDEF FPC}PChar{$Else}PAnsiChar{$EndIf}; length : Integer; 1325 | cproto : JSCFunctionEnum ; func : f_f_func) : JSCFunctionListEntry; overload; 1326 | begin 1327 | Result.name := name; 1328 | Result.prop_flags := JS_PROP_WRITABLE or JS_PROP_CONFIGURABLE; 1329 | Result.def_type := JS_DEF_CFUNC; 1330 | Result.magic := 0; 1331 | Result.u.func.length := length; 1332 | Result.u.func.cproto := cproto; 1333 | Result.u.func.cfunc.f_f := func; 1334 | end; 1335 | 1336 | function JS_CFUNC_SPECIAL_DEF(name : {$IFDEF FPC}PChar{$Else}PAnsiChar{$EndIf}; length : Integer; 1337 | cproto : JSCFunctionEnum ; func : f_f_f_func) : JSCFunctionListEntry; overload; 1338 | begin 1339 | Result.name := name; 1340 | Result.prop_flags := JS_PROP_WRITABLE or JS_PROP_CONFIGURABLE; 1341 | Result.def_type := JS_DEF_CFUNC; 1342 | Result.magic := 0; 1343 | Result.u.func.length := length; 1344 | Result.u.func.cproto := cproto; 1345 | Result.u.func.cfunc.f_f_f := func; 1346 | end; 1347 | 1348 | function JS_ITERATOR_NEXT_DEF(name : {$IFDEF FPC}PChar{$Else}PAnsiChar{$EndIf}; length : Integer; 1349 | iterator_next : iterator_next_func; magic : Int16) : JSCFunctionListEntry; 1350 | begin 1351 | Result.name := name; 1352 | Result.prop_flags := JS_PROP_WRITABLE or JS_PROP_CONFIGURABLE; 1353 | Result.def_type := JS_DEF_CFUNC; 1354 | Result.magic := magic; 1355 | Result.u.func.length := length; 1356 | Result.u.func.cproto := JS_CFUNC_iterator_next; 1357 | Result.u.func.cfunc.iterator_next := iterator_next; 1358 | end; 1359 | 1360 | function JS_CGETSET_DEF(name : {$IFDEF FPC}PChar{$Else}PAnsiChar{$EndIf}; 1361 | fgetter : Getter_func; fsetter : Setter_func ) : JSCFunctionListEntry; 1362 | begin 1363 | Result.name := name; 1364 | Result.prop_flags := JS_PROP_CONFIGURABLE; 1365 | Result.def_type := JS_DEF_CGETSET; 1366 | Result.magic := 0; 1367 | Result.u.getset.get.getter := fgetter; 1368 | Result.u.getset._set.setter := fsetter; 1369 | end; 1370 | 1371 | function JS_CGETSET_MAGIC_DEF(name : {$IFDEF FPC}PChar{$Else}PAnsiChar{$EndIf}; 1372 | fgetter_magic : getter_magic_func; 1373 | fsetter_magic : setter_magic_func; magic : Int16) : JSCFunctionListEntry; 1374 | begin 1375 | Result.name := name; 1376 | Result.prop_flags := JS_PROP_CONFIGURABLE; 1377 | Result.def_type := JS_DEF_CGETSET_MAGIC; 1378 | Result.magic := magic; 1379 | Result.u.getset.get.getter_magic := fgetter_magic; 1380 | Result.u.getset._set.setter_magic := fsetter_magic; 1381 | end; 1382 | 1383 | function JS_PROP_STRING_DEF(name : {$IFDEF FPC}PChar{$Else}PAnsiChar{$EndIf}; 1384 | val : {$IFDEF FPC}PChar{$Else}PAnsiChar{$EndIf}; prop_flags : UInt8) : JSCFunctionListEntry; 1385 | begin 1386 | Result.name := name; 1387 | Result.prop_flags := prop_flags; 1388 | Result.def_type := JS_DEF_PROP_STRING; 1389 | Result.magic := 0; 1390 | Result.u.str := val; 1391 | end; 1392 | 1393 | function JS_PROP_INT32_DEF(name : {$IFDEF FPC}PChar{$Else}PAnsiChar{$EndIf}; 1394 | val : Int32; prop_flags : UInt8) : JSCFunctionListEntry; 1395 | begin 1396 | Result.name := name; 1397 | Result.prop_flags := prop_flags; 1398 | Result.def_type := JS_DEF_PROP_INT32; 1399 | Result.magic := 0; 1400 | Result.u.i32 := val; 1401 | end; 1402 | 1403 | function JS_PROP_INT64_DEF(name : {$IFDEF FPC}PChar{$Else}PAnsiChar{$EndIf}; 1404 | val : Int64; prop_flags : UInt8) : JSCFunctionListEntry; 1405 | begin 1406 | Result.name := name; 1407 | Result.prop_flags := prop_flags; 1408 | Result.def_type := JS_DEF_PROP_INT64; 1409 | Result.magic := 0; 1410 | Result.u.i64 := val; 1411 | end; 1412 | 1413 | function JS_PROP_DOUBLE_DEF(name : {$IFDEF FPC}PChar{$Else}PAnsiChar{$EndIf}; 1414 | val : Double; prop_flags : UInt8) : JSCFunctionListEntry; 1415 | begin 1416 | Result.name := name; 1417 | Result.prop_flags := prop_flags; 1418 | Result.def_type := JS_DEF_PROP_DOUBLE; 1419 | Result.magic := 0; 1420 | Result.u.f64 := val; 1421 | end; 1422 | 1423 | function JS_PROP_UNDEFINED_DEF(name : {$IFDEF FPC}PChar{$Else}PAnsiChar{$EndIf}; 1424 | prop_flags : UInt8) : JSCFunctionListEntry; 1425 | begin 1426 | Result.name := name; 1427 | Result.prop_flags := prop_flags; 1428 | Result.def_type := JS_DEF_PROP_UNDEFINED; 1429 | Result.magic := 0; 1430 | Result.u.i32 := 0; 1431 | end; 1432 | 1433 | function JS_OBJECT_DEF(name : {$IFDEF FPC}PChar{$Else}PAnsiChar{$EndIf}; tab : PJSCFunctionListEntry; 1434 | length : Integer; prop_flags : UInt8) : JSCFunctionListEntry; 1435 | begin 1436 | Result.name := name; 1437 | Result.prop_flags := prop_flags; 1438 | Result.def_type := JS_DEF_OBJECT; 1439 | Result.magic := 0; 1440 | Result.u.prop_list.tab := {$IfDef FPC}tab{$Else}Pointer(tab){$EndIf}; 1441 | Result.u.prop_list.len := length; 1442 | end; 1443 | 1444 | function JS_ALIAS_DEF(name, from : {$IFDEF FPC}PChar{$Else}PAnsiChar{$EndIf}) : JSCFunctionListEntry; 1445 | begin 1446 | Result.name := name; 1447 | Result.prop_flags := JS_PROP_WRITABLE or JS_PROP_CONFIGURABLE; 1448 | Result.def_type := JS_DEF_ALIAS; 1449 | Result.magic := 0; 1450 | Result.u.alias.name := from; 1451 | Result.u.alias.base := -1; 1452 | end; 1453 | 1454 | function JS_ALIAS_BASE_DEF(name, from : {$IFDEF FPC}PChar{$Else}PAnsiChar{$EndIf}; 1455 | base : Integer) : JSCFunctionListEntry; 1456 | begin 1457 | Result.name := name; 1458 | Result.prop_flags := JS_PROP_WRITABLE or JS_PROP_CONFIGURABLE; 1459 | Result.def_type := JS_DEF_ALIAS; 1460 | Result.magic := 0; 1461 | Result.u.alias.name := from; 1462 | Result.u.alias.base := base; 1463 | end; 1464 | 1465 | { bignum stuff :D } 1466 | 1467 | function c_udivti3(num,den:uint64):uint64; cdecl; public alias: {$ifdef darwin}'___udivti3'{$else} '__udivdi3'{$endif}; 1468 | begin 1469 | result:=num div den; 1470 | end; 1471 | 1472 | 1473 | initialization 1474 | // fix the Invalid floating point operation . 1475 | OldFPUMask := GetExceptionMask; 1476 | SetExceptionMask([exInvalidOp,exDenormalized,exZeroDivide,exOverflow,exUnderflow,exPrecision]); 1477 | 1478 | finalization 1479 | SetExceptionMask(OldFPUMask); 1480 | 1481 | end. 1482 | 1483 | --------------------------------------------------------------------------------