UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

9 lines • 46.8 kB
//!node_modules/.pnpm/quickjs-wasi@3.6.3/node_modules/quickjs-wasi/dist/wasi-shim.js function createWasiShim(e){const t=0;const n=8;const r=52;const i=0;const a=1;return{clock_time_get(n,o,s){const c=e();const l=new DataView(c.buffer);if(n===i||n===a){const e=BigInt(Date.now())*1000000n;l.setBigUint64(s,e,true);return t}return r},fd_write(r,i,a,o){const s=e();const c=new DataView(s.buffer);const l=new Uint8Array(s.buffer);let u=0;if(r!==1&&r!==2)return n;for(let e=0;e<a;e++){const t=c.getUint32(i+e*8,true);const n=c.getUint32(i+e*8+4,true);const a=l.slice(t,t+n);const o=new TextDecoder().decode(a);if(r===1){if(typeof process!==`undefined`&&process.stdout)process.stdout.write(o);else console.log(o)}else if(typeof process!==`undefined`&&process.stderr)process.stderr.write(o);else console.error(o);u+=n}c.setUint32(o,u,true);return t},fd_close(e){return r},fd_fdstat_get(r,i){const a=e();const o=new DataView(a.buffer);if(r===1||r===2){o.setUint8(i,2);o.setUint16(i+2,0,true);o.setBigUint64(i+8,0n,true);o.setBigUint64(i+16,0n,true);return t}return n},fd_seek(e,t,n,i){return r},random_get(n,r){const i=e();const a=new Uint8Array(i.buffer,n,r);if(typeof crypto!==`undefined`&&crypto.getRandomValues)crypto.getRandomValues(a);else for(let e=0;e<r;e++)a[e]=Math.floor(Math.random()*256);return t}}} //!node_modules/.pnpm/quickjs-wasi@3.6.3/node_modules/quickjs-wasi/dist/extensions.js const decoder=new TextDecoder;function parseVersionString(e,t){const n=new Uint8Array(e.buffer);let r=t;while(n[r]!==0)r++;if(r===t)return{};const i=decoder.decode(n.slice(t,r));const a={};for(const e of i.split(` `)){const t=e.indexOf(`=`);if(t>0)a[e.slice(0,t)]=e.slice(t+1)}return a}function extractExtensionVersions(e,t){const n=`qjs_ext_${e.name.replace(/-/g,`_`)}_versions`;const r=e.instance.exports[n];if(typeof r!==`function`)return void 0;const i=r();if(i===0)return void 0;return parseVersionString(t,i)}function readULEB128(e,t){let n=0;let r=0;while(t.value<e.length){const i=e[t.value++];n|=(i&127)<<r;if((i&128)===0)break;r+=7}return n}function parseDylink(e){const t=WebAssembly.Module.customSections(e,`dylink.0`);if(t.length===0)return null;const n=new Uint8Array(t[0]);const r={value:0};const i={memorySize:0,memoryAlignment:0,tableSize:0,tableAlignment:0,needed:[]};while(r.value<n.length){const e=readULEB128(n,r);const t=readULEB128(n,r);const a=r.value+t;if(e===1){i.memorySize=readULEB128(n,r);i.memoryAlignment=readULEB128(n,r);i.tableSize=readULEB128(n,r);i.tableAlignment=readULEB128(n,r)}else if(e===2){const e=readULEB128(n,r);for(let t=0;t<e;t++){const e=readULEB128(n,r);const t=new TextDecoder().decode(n.slice(r.value,r.value+e));r.value+=e;i.needed.push(t)}}r.value=a}return i}async function loadExtension(e,t,n,r,i,a){let o;if(e.wasm instanceof WebAssembly.Module)o=e.wasm;else o=await WebAssembly.compile(e.wasm);const s=parseDylink(o);if(!s)throw new Error(`Extension "${e.name}" is not a WASM shared library (missing dylink.0 section)`);const c=t.memory;const l=t.__indirect_function_table;const u=t.__stack_pointer;const d=t.malloc;if(!c)throw new Error(`Main module does not export memory`);if(!l)throw new Error(`Main module does not export __indirect_function_table`);if(!d)throw new Error(`Main module does not export malloc`);let f;let p;if(a){f=a.memoryBase;p=a.tableBase}else if(s.memorySize>0){f=d(s.memorySize);if(f===0)throw new Error(`Failed to allocate ${s.memorySize} bytes for extension "${e.name}"`);new Uint8Array(c.buffer,f,s.memorySize).fill(0)}else f=0;if(a){p=a.tableBase;if(p+s.tableSize>l.length)l.grow(p+s.tableSize-l.length)}else{p=l.length;if(s.tableSize>0)l.grow(s.tableSize)}const m=WebAssembly.Module.imports(o);const h={env:{},"GOT.mem":{},"GOT.func":{}};if(m.some(e=>e.module===`wasi_snapshot_preview1`)){const t=e.wasi&&i?e.wasi(i):void 0;h[`wasi_snapshot_preview1`]={...n,...t,...r}}const g=new Set(WebAssembly.Module.exports(o).filter(e=>e.kind===`function`).map(e=>e.name));const _=/* @__PURE__ */ new Set;for(const n of m)if(n.module===`env`){if(n.name===`memory`&&n.kind===`memory`)h.env.memory=c;else if(n.name===`__indirect_function_table`&&n.kind===`table`)h.env.__indirect_function_table=l;else if(n.name===`__memory_base`&&n.kind===`global`)h.env.__memory_base=new WebAssembly.Global({value:`i32`,mutable:false},f);else if(n.name===`__table_base`&&n.kind===`global`)h.env.__table_base=new WebAssembly.Global({value:`i32`,mutable:false},p);else if(n.name===`__stack_pointer`&&n.kind===`global`)h.env.__stack_pointer=u;else if(n.kind===`function`){const r=t[n.name];if(r&&typeof r===`function`)h.env[n.name]=r;else{_.add(n.name);h.env[n.name]=()=>{throw new Error(`Extension "${e.name}" called unresolved symbol: env.${n.name}`)}}}else if(n.kind===`global`){const e=t[n.name];if(e instanceof WebAssembly.Global)h.env[n.name]=e;else h.env[n.name]=new WebAssembly.Global({value:`i32`,mutable:true},0)}}else if(n.module===`GOT.mem`&&n.kind===`global`)h[`GOT.mem`][n.name]=new WebAssembly.Global({value:`i32`,mutable:true},0);else if(n.module===`GOT.func`&&n.kind===`global`){const e=t[n.name];if(e&&typeof e===`function`){const t=l.length;l.grow(1);l.set(t,e);h[`GOT.func`][n.name]=new WebAssembly.Global({value:`i32`,mutable:true},t)}else h[`GOT.func`][n.name]=new WebAssembly.Global({value:`i32`,mutable:true},0)}const v=[..._].filter(e=>g.has(e));const y={};for(const t of v){const n={target:null};y[t]=n;h.env[t]=(...r)=>{if(!n.target)throw new Error(`Extension "${e.name}" called unresolved symbol during init: env.${t}`);return n.target(...r)}}const b=await WebAssembly.instantiate(o,h);const x=b.exports;for(const e of v){const t=x[e];if(typeof t===`function`)y[e].target=t}if(typeof x.__wasm_apply_data_relocs===`function`)x.__wasm_apply_data_relocs();if(typeof x.__wasm_call_ctors===`function`)x.__wasm_call_ctors();const S=e.initFn??`qjs_ext_${e.name.replace(/-/g,`_`)}_init`;const C={name:e.name,module:o,instance:b,dylink:s,memoryBase:f,tableBase:p,initFn:S};C.versions=extractExtensionVersions(C,c);return C}function initExtension(e,t){const n=e.instance.exports[e.initFn];if(typeof n!==`function`)throw new Error(`Extension "${e.name}" does not export init function "${e.initFn}"`);const r=n(t.qjs_get_context_ptr(),t.qjs_get_runtime_ptr());if(r!==0)throw new Error(`Extension "${e.name}" init function returned error code ${r}`)}async function restoreExtensions(e,t,n,r,i,a){const o=[];for(const s of t){const t=e.find(e=>e.name===s.name);if(!t)throw new Error(`Extension "${s.name}" required by snapshot but not provided`);const c=await loadExtension(t,n,r,i,a,{memoryBase:s.memoryBase,tableBase:s.tableBase});c.initFn=s.initFn;o.push(c)}return o} //!node_modules/.pnpm/quickjs-wasi@3.6.3/node_modules/quickjs-wasi/dist/version.js const VERSION=`3.6.3`; //!node_modules/.pnpm/quickjs-wasi@3.6.3/node_modules/quickjs-wasi/dist/index.js var __addDisposableResource=void 0&&(void 0).__addDisposableResource||function(e,t,n){if(t!==null&&t!==void 0){if(typeof t!==`object`&&typeof t!==`function`)throw new TypeError(`Object expected.`);var dispose,r;if(n){if(!Symbol.asyncDispose)throw new TypeError(`Symbol.asyncDispose is not defined.`);dispose=t[Symbol.asyncDispose]}if(dispose===void 0){if(!Symbol.dispose)throw new TypeError(`Symbol.dispose is not defined.`);dispose=t[Symbol.dispose];if(n)r=dispose}if(typeof dispose!==`function`)throw new TypeError(`Object not disposable.`);if(r)dispose=function(){try{r.call(this)}catch(e){return Promise.reject(e)}};e.stack.push({value:t,dispose,async:n})}else if(n)e.stack.push({async:true});return t};var __disposeResources=void 0&&(void 0).__disposeResources||(function(e){return function(t){function fail(n){t.error=t.hasError?new e(n,t.error,`An error was suppressed during disposal.`):n;t.hasError=true}var n,r=0;function next(){while(n=t.stack.pop())try{if(!n.async&&r===1)return r=0,t.stack.push(n),Promise.resolve().then(next);if(n.dispose){var e=n.dispose.call(n.value);if(n.async)return r|=2,Promise.resolve(e).then(next,function(e){fail(e);return next()})}else r|=1}catch(e){fail(e)}if(r===1)return t.hasError?Promise.reject(t.error):Promise.resolve();if(t.hasError)throw t.error}return next()}})(typeof SuppressedError===`function`?SuppressedError:function(e,t,n){var r=new Error(n);return r.name=`SuppressedError`,r.error=e,r.suppressed=t,r});const MAX_STACK_SIZE=524288;const EvalFlags={TYPE_GLOBAL:0,TYPE_MODULE:1,STRICT:8,COMPILE_ONLY:32,BACKTRACE_BARRIER:64,ASYNC:128};const SNAPSHOT_MAGIC=1363825491;const SNAPSHOT_VERSION=2;const SNAPSHOT_HEADER_SIZE=24;var QuickJS=class QuickJS{exports;module;instance;encoder=new TextEncoder;decoder=new TextDecoder;disposed=false;hostCallbacks=/* @__PURE__ */ new Map;nextInternalId=1;interruptHandler=null;unhandledRejectionHandler=null;moduleNormalizeHandler=null;moduleLoadHandler=null;timezoneOffsetHandler=null;_global=null;_versions=null;_undefined=null;_null=null;_true=null;_false=null;_ownedHandles=/* @__PURE__ */ new Set;_activeScope=null;loadedExtensions=[];constructor(e){this.module=e;this.instance=null;this.exports=null}setInstance(e){this.instance=e;this.exports=e.exports}get versions(){this.assertNotDisposed();if(!this._versions){const e={"quickjs-wasi":VERSION,quickjs:this.readCString(this.exports.qjs_get_quickjs_version())};for(const t of this.loadedExtensions)if(t.versions)Object.assign(e,t.versions);this._versions=e}return this._versions}get global(){if(!this._global)this._global=new JSValueHandle(this,this.exports.qjs_get_global(),true);return this._global}get undefined(){if(!this._undefined)this._undefined=new JSValueHandle(this,this.exports.qjs_get_undefined(),true);return this._undefined}get null(){if(!this._null)this._null=new JSValueHandle(this,this.exports.qjs_get_null(),true);return this._null}get true(){if(!this._true)this._true=new JSValueHandle(this,this.exports.qjs_get_true(),true);return this._true}get false(){if(!this._false)this._false=new JSValueHandle(this,this.exports.qjs_get_false(),true);return this._false}static async create(e){const t=QuickJS.normalizeOptions(e);const n=await QuickJS.resolveModule(t.wasm);const r=new QuickJS(n);const{instance:i,wasiBuiltins:a,wasiUserOverrides:o,memoryProxy:s}=await QuickJS.instantiate(n,r,t.wasi);r.setInstance(i);r.exports._initialize();if((t.intrinsics!==void 0?r.exports.qjs_init2(t.intrinsics):r.exports.qjs_init())!==0)throw new Error(`Failed to initialize QuickJS runtime`);if(t.extensions){const e=i.exports;for(const n of t.extensions){const t=await loadExtension(n,e,a,o,s);r.loadedExtensions.push(t);initExtension(t,e)}}QuickJS.applyLimits(r,t);return r}static async restore(e,t){const n=QuickJS.normalizeOptions(t);const r=await QuickJS.resolveModule(n.wasm);const i=new QuickJS(r);const{instance:a,wasiBuiltins:o,wasiUserOverrides:s,memoryProxy:c}=await QuickJS.instantiate(r,i,n.wasi);i.setInstance(a);const l=a.exports;const u=i.exports.memory;const d=u.buffer.byteLength/65536;const f=Math.ceil(e.memory.byteLength/65536);if(f>d)u.grow(f-d);if(e.extensions.length>0){const t=n.extensions??[];i.loadedExtensions=await restoreExtensions(t,e.extensions,l,o,s,c)}new Uint8Array(u.buffer).set(e.memory);i.exports.qjs_set_runtime_and_context(e.runtimePtr,e.contextPtr);i.exports.__stack_pointer.value=e.stackPointer;QuickJS.applyLimits(i,n);return i}static serializeSnapshot(e){const t=new TextEncoder;let n=4;const r=[];const i=[];for(const a of e.extensions){const e=t.encode(a.name);const o=t.encode(a.initFn);r.push(e);i.push(o);n+=4+e.length+4+4+4+o.length}const a=SNAPSHOT_HEADER_SIZE+n+e.memory.byteLength;const o=new ArrayBuffer(a);const s=new DataView(o);const c=new Uint8Array(o);s.setUint32(0,SNAPSHOT_MAGIC,false);s.setUint8(4,SNAPSHOT_VERSION);s.setUint32(8,e.memory.byteLength,true);s.setUint32(12,e.stackPointer,true);s.setUint32(16,e.runtimePtr,true);s.setUint32(20,e.contextPtr,true);let l=SNAPSHOT_HEADER_SIZE;s.setUint32(l,e.extensions.length,true);l+=4;for(let t=0;t<e.extensions.length;t++){const n=e.extensions[t];const a=r[t];const o=i[t];s.setUint32(l,a.length,true);l+=4;c.set(a,l);l+=a.length;s.setUint32(l,n.memoryBase,true);l+=4;s.setUint32(l,n.tableBase,true);l+=4;s.setUint32(l,o.length,true);l+=4;c.set(o,l);l+=o.length}c.set(e.memory,l);return c}static deserializeSnapshot(e){if(e.length<SNAPSHOT_HEADER_SIZE)throw new Error(`Invalid snapshot: too small`);const t=new DataView(e.buffer,e.byteOffset,e.byteLength);const n=t.getUint32(0,false);if(n!==SNAPSHOT_MAGIC)throw new Error(`Invalid snapshot: bad magic (expected 0x${SNAPSHOT_MAGIC.toString(16)}, got 0x${n.toString(16)})`);const r=t.getUint8(4);if(r!==SNAPSHOT_VERSION&&r!==1)throw new Error(`Unsupported snapshot version: ${r} (expected ${SNAPSHOT_VERSION})`);const i=t.getUint32(8,true);const a=t.getUint32(12,true);const o=t.getUint32(16,true);const s=t.getUint32(20,true);let c=[];let l=SNAPSHOT_HEADER_SIZE;if(r>=2){const n=t.getUint32(24,true);let r=28;const i=new TextDecoder;for(let a=0;a<n;a++){const n=t.getUint32(r,true);r+=4;const a=i.decode(e.slice(r,r+n));r+=n;const o=t.getUint32(r,true);r+=4;const s=t.getUint32(r,true);r+=4;const l=t.getUint32(r,true);r+=4;const u=i.decode(e.slice(r,r+l));r+=l;c.push({name:a,memoryBase:o,tableBase:s,initFn:u})}l=r}const u=l+i;if(e.length<u)throw new Error(`Invalid snapshot: expected ${u} bytes, got ${e.length}`);return{memory:e.slice(l,l+i),stackPointer:a,runtimePtr:o,contextPtr:s,extensions:c}}static normalizeOptions(e){if(!e)return{wasm:void 0};if(e instanceof WebAssembly.Module)return{wasm:e};if(typeof e===`object`&&(`wasm`in e||`wasi`in e||`memoryLimit`in e||`maxStackSize`in e||`interruptHandler`in e||`onUnhandledRejection`in e||`moduleLoader`in e||`intrinsics`in e||`extensions`in e||`timezoneOffset`in e)){const t=e;if(t.maxStackSize!==void 0&&(!Number.isInteger(t.maxStackSize)||t.maxStackSize<0||t.maxStackSize>524288))throw new RangeError(`maxStackSize must be an integer between 0 and ${MAX_STACK_SIZE}`);return t}return{wasm:e}}static applyLimits(e,t){if(t.memoryLimit!==void 0)e.exports.qjs_set_memory_limit(t.memoryLimit);if(t.maxStackSize!==void 0)e.exports.qjs_set_max_stack_size(t.maxStackSize);if(t.interruptHandler){e.interruptHandler=t.interruptHandler;e.exports.qjs_set_interrupt_handler(1)}if(t.onUnhandledRejection){e.unhandledRejectionHandler=t.onUnhandledRejection;e.exports.qjs_set_promise_rejection_handler(1)}if(t.moduleLoader){e.moduleLoadHandler=t.moduleLoader.load;e.moduleNormalizeHandler=t.moduleLoader.normalize??null;e.exports.qjs_set_module_loader(1)}const n=t.timezoneOffset;if(typeof n===`function`)e.timezoneOffsetHandler=e=>-n(e)*60;else if(typeof n===`number`){const t=-n*60;e.timezoneOffsetHandler=()=>t}else e.timezoneOffsetHandler=e=>{return-(/* @__PURE__ */ new Date(e*1e3)).getTimezoneOffset()*60}}static async resolveModule(e){if(e instanceof WebAssembly.Module)return e;if(e)return WebAssembly.compile(e);throw new TypeError("QuickJS: `wasm` option is required. Provide WASM bytes or a compiled `WebAssembly.Module`. The binary is shipped at `quickjs-wasi/quickjs.wasm` and can be loaded via your environment's preferred mechanism (e.g. `fetch()`, `node:fs/promises`, or a bundler import like Vite's `?url`).")}static async instantiate(e,t,n){let r=null;const i=new Proxy({},{get(e,t){return r[t]}});const a=createWasiShim(()=>r);const o=n?n(i):void 0;const s={...a,...o};const hostCall=(e,n,r,i,a)=>{return t.handleHostCall(e,n,r,i,a)};const hostInterrupt=()=>{return t.interruptHandler?t.interruptHandler()?1:0:0};const hostPromiseRejection=(e,n,r)=>{if(!t.unhandledRejectionHandler){t.exports.qjs_free_value(e);t.exports.qjs_free_value(n);return}const i=new JSValueHandle(t,e);const a=new JSValueHandle(t,n);try{t.unhandledRejectionHandler(i,a,r!==0)}finally{i.dispose();a.dispose()}};const throwIntoContext=e=>{const n=t.newError(e instanceof Error?e:String(e));t.exports.qjs_throw(n.ptr);n.dispose()};const assertSyncString=(e,t)=>{if(typeof e===`string`)return e;const n=e!==null&&typeof e===`object`&&typeof e.then===`function`?`a Promise`:`type ${typeof e}`;throw new TypeError(`moduleLoader.${t} must synchronously return a string (got ${n}). Async module loading is not supported. Pre-fetch module sources instead (see the "ES Modules" section of the quickjs-wasi README).`)};const hostModuleNormalize=(e,n)=>{if(!t.moduleNormalizeHandler){const e=t.readCString(n);return t.writeString(e).ptr}const r=t.readCString(e);const i=t.readCString(n);try{const e=assertSyncString(t.moduleNormalizeHandler(r,i),`normalize`);return t.writeString(e).ptr}catch(e){throwIntoContext(e);return 0}};const hostModuleLoad=(e,n)=>{if(!t.moduleLoadHandler)return 0;const r=t.readCString(e);try{const e=assertSyncString(t.moduleLoadHandler(r),`load`);const{ptr:i,len:a}=t.writeString(e);new Uint32Array(t.exports.memory.buffer,n,1)[0]=a;return i}catch(e){throwIntoContext(e);return 0}};const hostGetTimezoneOffset=(e,n)=>{const r=Number(BigInt(e)<<32n|BigInt(n>>>0));return t.timezoneOffsetHandler?t.timezoneOffsetHandler(r):0};const c=await WebAssembly.instantiate(e,{env:{host_call:hostCall,host_interrupt:hostInterrupt,host_promise_rejection:hostPromiseRejection,host_module_normalize:hostModuleNormalize,host_module_load:hostModuleLoad,host_get_timezone_offset:hostGetTimezoneOffset},wasi_snapshot_preview1:s});r=c.exports.memory;return{instance:c,wasiBuiltins:a,wasiUserOverrides:o,memoryProxy:i}}handleHostCall(e,t,n,r,i){const a=this.decoder.decode(new Uint8Array(this.exports.memory.buffer,e,t));const o=this.hostCallbacks.get(a);if(!o){const e=this.newError(`Host callback "${a}" is not registered: it was unregistered, its ephemeral function handle was disposed, or it was never re-registered after a snapshot restore.`);this.exports.qjs_throw(e.ptr);e.dispose();return 0}const s=new JSValueHandle(this,n,false,true);const c=[];if(r>0&&i!==0){const e=new DataView(this.exports.memory.buffer);for(let t=0;t<r;t++){const n=e.getUint32(i+t*4,true);c.push(new JSValueHandle(this,n,false,true))}}try{const e=o.call(s,...c);return this.exports.qjs_dup_value(e.ptr)}catch(e){const t=this.newError(e instanceof Error?e:String(e));this.exports.qjs_throw(t.ptr);t.dispose();return 0}}writeString(e){const t=encodeWtf8(e);const n=this.exports.wasm_malloc(t.length+1);if(n===0)throw new Error(`wasm_malloc failed`);const r=new Uint8Array(this.exports.memory.buffer);r.set(t,n);r[n+t.length]=0;return{ptr:n,len:t.length}}readCString(e){const t=new Uint8Array(this.exports.memory.buffer);let n=e;while(t[n]!==0)n++;return this.decoder.decode(t.slice(e,n))}throwIfException(e){if(this.exports.qjs_is_exception(e.ptr)!==0){const t=this.getException();e.dispose();this._ownedHandles.add(t);throw new JSException(t)}return e}evalCode(e,t=`<eval>`,n=0){this.assertNotDisposed();const r=this.writeString(e);const i=this.writeString(t);const a=this.exports.qjs_eval(r.ptr,r.len,i.ptr,n);this.exports.wasm_free(r.ptr);this.exports.wasm_free(i.ptr);return this.throwIfException(new JSValueHandle(this,a))}compile(e,t=`<compile>`,n=0,r=0){this.assertNotDisposed();const i=this.writeString(e);const a=this.writeString(t);const o=this.exports.wasm_malloc(4);const s=this.exports.qjs_compile(i.ptr,i.len,a.ptr,n,r,o);this.exports.wasm_free(i.ptr);this.exports.wasm_free(a.ptr);if(s===0){this.exports.wasm_free(o);const e=this.getException();throw new Error(`Compilation error: ${e.toString()}`)}const c=new Uint32Array(this.exports.memory.buffer,o,1)[0];this.exports.wasm_free(o);const l=new Uint8Array(this.exports.memory.buffer,s,c).slice();this.exports.wasm_free(s);return l}evalBytecode(e){this.assertNotDisposed();const t=this.exports.wasm_malloc(e.byteLength);new Uint8Array(this.exports.memory.buffer,t,e.byteLength).set(e);const n=this.exports.qjs_eval_bytecode(t,e.byteLength);this.exports.wasm_free(t);return this.throwIfException(new JSValueHandle(this,n))}executePendingJobs(){this.assertNotDisposed();let e=0;while(this.exports.qjs_is_job_pending()){if(this.exports.qjs_execute_pending_job()<0){const e=this.getException();throw new Error(`Job execution error: ${e.toString()}`)}e++}return e}runGC(){this.assertNotDisposed();this.exports.qjs_run_gc()}get gcThreshold(){this.assertNotDisposed();return this.exports.qjs_get_gc_threshold()}set gcThreshold(e){this.assertNotDisposed();this.exports.qjs_set_gc_threshold(e)}getMemoryUsage(){this.assertNotDisposed();const e=this.exports.wasm_malloc(208);this.exports.qjs_compute_memory_usage(e);const t=new BigInt64Array(this.exports.memory.buffer,e,26);const n={mallocSize:Number(t[0]),mallocLimit:Number(t[1]),memoryUsedSize:Number(t[2]),mallocCount:Number(t[3]),memoryUsedCount:Number(t[4]),atomCount:Number(t[5]),atomSize:Number(t[6]),strCount:Number(t[7]),strSize:Number(t[8]),objCount:Number(t[9]),objSize:Number(t[10]),propCount:Number(t[11]),propSize:Number(t[12]),shapeCount:Number(t[13]),shapeSize:Number(t[14]),jsFuncCount:Number(t[15]),jsFuncSize:Number(t[16]),jsFuncCodeSize:Number(t[17]),jsFuncPc2lineCount:Number(t[18]),jsFuncPc2lineSize:Number(t[19]),cFuncCount:Number(t[20]),arrayCount:Number(t[21]),fastArrayCount:Number(t[22]),fastArrayElements:Number(t[23]),binaryObjectCount:Number(t[24]),binaryObjectSize:Number(t[25])};this.exports.wasm_free(e);return n}getGlobal(){this.assertNotDisposed();return new JSValueHandle(this,this.exports.qjs_get_global())}newString(e){this.assertNotDisposed();const{ptr:t,len:n}=this.writeString(e);const r=this.exports.qjs_new_string(t,n);this.exports.wasm_free(t);return new JSValueHandle(this,r)}newNumber(e){this.assertNotDisposed();return new JSValueHandle(this,this.exports.qjs_new_number(e))}newBigInt(e){this.assertNotDisposed();const t=Number(e&4294967295n);const n=Number(e>>32n&4294967295n);return new JSValueHandle(this,this.exports.qjs_new_big_int64(t,n))}newObject(){this.assertNotDisposed();return new JSValueHandle(this,this.exports.qjs_new_object())}newArray(){this.assertNotDisposed();return new JSValueHandle(this,this.exports.qjs_new_array())}newSymbolFor(e){this.assertNotDisposed();const{ptr:t,len:n}=this.writeString(e);const r=new JSValueHandle(this,this.exports.qjs_new_symbol(t,n,1));this.exports.wasm_free(t);return r}newArrayBuffer(e){this.assertNotDisposed();const t=e instanceof ArrayBuffer?new Uint8Array(e):e;const n=this.exports.wasm_malloc(t.length);if(n===0)throw new Error(`wasm_malloc failed`);new Uint8Array(this.exports.memory.buffer).set(t,n);const r=new JSValueHandle(this,this.exports.qjs_new_array_buffer(n,t.length));this.exports.wasm_free(n);return r}newUint8Array(e){this.assertNotDisposed();const t=this.exports.wasm_malloc(e.length);if(t===0)throw new Error(`wasm_malloc failed`);new Uint8Array(this.exports.memory.buffer).set(e,t);const n=new JSValueHandle(this,this.exports.qjs_new_uint8_array(t,e.length));this.exports.wasm_free(t);return n}getUndefined(){this.assertNotDisposed();return new JSValueHandle(this,this.exports.qjs_get_undefined())}getNull(){this.assertNotDisposed();return new JSValueHandle(this,this.exports.qjs_get_null())}getTrue(){this.assertNotDisposed();return new JSValueHandle(this,this.exports.qjs_get_true())}getFalse(){this.assertNotDisposed();return new JSValueHandle(this,this.exports.qjs_get_false())}newFunction(e,t){this.assertNotDisposed();if(this.hostCallbacks.has(e))throw new Error(`Host callback with name "${e}" is already registered`);this.hostCallbacks.set(e,t);const{ptr:n,len:r}=this.writeString(e);const i=this.exports.qjs_new_host_function(n,r,0);this.exports.wasm_free(n);return new JSValueHandle(this,i)}withScope(e){this.assertNotDisposed();const t=this._activeScope;const n=/* @__PURE__ */ new Set;this._activeScope=n;const r={escape:e=>{n.delete(e);t?.add(e);return e}};try{return e(r)}finally{this._activeScope=t;for(const e of n)e.dispose()}}exportHandle(e){this.assertNotDisposed();if(e.vm!==this)throw new Error(`exportHandle: handle belongs to a different VM`);if(e.disposed)throw new Error(`exportHandle: handle is disposed`);if(e._isBorrowed)throw new Error(`exportHandle: cannot export a borrowed handle (host-callback this/argument); its box is freed when the callback returns. dup() it and export the duplicate.`);return e.ptr}importHandle(e){this.assertNotDisposed();if(!Number.isInteger(e)||e<=0||e>=this.exports.memory.buffer.byteLength)throw new Error(`importHandle: invalid token ${e}`);return new JSValueHandle(this,this.exports.qjs_dup_value(e))}newEphemeralFunction(e){this.assertNotDisposed();const t=`__ephemeral:${this.nextInternalId++}`;this.hostCallbacks.set(t,e);const{ptr:n,len:r}=this.writeString(t);const i=this.exports.qjs_new_host_function(n,r,0);this.exports.wasm_free(n);const a=new JSValueHandle(this,i);a._onDispose=()=>{this.hostCallbacks.delete(t)};return a}unregisterHostCallback(e){return this.hostCallbacks.delete(e)}newInternalFunction(e,t){this.hostCallbacks.set(e,t);const{ptr:n,len:r}=this.writeString(e);const i=this.exports.qjs_new_host_function(n,r,0);this.exports.wasm_free(n);return new JSValueHandle(this,i)}newPromise(){this.assertNotDisposed();const e=this.exports.wasm_malloc(4);const t=this.exports.wasm_malloc(4);const n=this.exports.qjs_new_promise(e,t);const r=new DataView(this.exports.memory.buffer);const i=r.getUint32(e,true);const a=r.getUint32(t,true);this.exports.wasm_free(e);this.exports.wasm_free(t);const o=new JSValueHandle(this,n);const s=new JSValueHandle(this,i);const c=new JSValueHandle(this,a);const l=this;l._ownedHandles.add(s);l._ownedHandles.add(c);let u=false;const settle=(e,t)=>{if(u)return;u=true;l.callFunctionRaw(e,l.undefined,t).dispose();l._ownedHandles.delete(s);l._ownedHandles.delete(c);s.dispose();c.dispose()};let d=null;return{handle:o,get settled(){if(!d){let e;d=new Promise(t=>{e=t});const t=`__settle:${l.nextInternalId++}`;const n=l.newInternalFunction(t,()=>{e();l.hostCallbacks.delete(t);return l.undefined});l.promiseThenRaw(o,n,n).dispose();n.dispose()}return d},resolve(e){settle(s,e)},reject(e){settle(c,e)}}}resolvePromise(e){this.assertNotDisposed();if(!this.exports.qjs_is_promise(e.ptr))return Promise.resolve({value:e.dup()});const t=this.exports.qjs_promise_state(e.ptr);if(t===1)return Promise.resolve({value:new JSValueHandle(this,this.exports.qjs_promise_result(e.ptr))});else if(t===2)return Promise.resolve({error:new JSValueHandle(this,this.exports.qjs_promise_result(e.ptr))});return new Promise(t=>{const n=this.nextInternalId++;const r=`__onFulfilled:${n}`;const i=`__onRejected:${n}`;const a=this.newInternalFunction(r,(...e)=>{const n=e[0]?.dup()??this.undefined;this.hostCallbacks.delete(r);this.hostCallbacks.delete(i);t({value:n});return this.undefined});const o=this.newInternalFunction(i,(...e)=>{const n=e[0]?.dup()??this.undefined;this.hostCallbacks.delete(r);this.hostCallbacks.delete(i);t({error:n});return this.undefined});this.promiseThenRaw(e,a,o).dispose();a.dispose();o.dispose()})}promiseThenRaw(e,t,n){return new JSValueHandle(this,this.exports.qjs_promise_then(e.ptr,t.ptr,n.ptr))}markPromiseHandled(e){this.assertNotDisposed();this.exports.qjs_promise_mark_as_handled(e.ptr)}callFunction(e,t,...n){return this.throwIfException(this.callFunctionRaw(e,t,...n))}construct(e,...t){this.assertNotDisposed();const n=t.length;let r=0;if(n>0){r=this.exports.wasm_malloc(n*4);const e=new DataView(this.exports.memory.buffer);for(let i=0;i<n;i++)e.setUint32(r+i*4,t[i].ptr,true)}const i=this.exports.qjs_call_constructor(e.ptr,n,r);if(r)this.exports.wasm_free(r);return this.throwIfException(new JSValueHandle(this,i))}callFunctionRaw(e,t,...n){this.assertNotDisposed();const r=n.length;let i=0;if(r>0){i=this.exports.wasm_malloc(r*4);const e=new DataView(this.exports.memory.buffer);for(let t=0;t<r;t++)e.setUint32(i+t*4,n[t].ptr,true)}const a=this.exports.qjs_call(e.ptr,t.ptr,r,i);if(i)this.exports.wasm_free(i);return new JSValueHandle(this,a)}setProp(e,t,n){this.assertNotDisposed();if(typeof t===`string`){const{ptr:r}=this.writeString(t);this.exports.qjs_set_prop_string(e.ptr,r,n.ptr);this.exports.wasm_free(r)}else this.exports.qjs_set_prop_value(e.ptr,t.ptr,n.ptr)}defineProp(e,t,n,r){this.assertNotDisposed();let i=0;if(r?.configurable)i|=1;if(r?.writable)i|=2;if(r?.enumerable)i|=4;if(typeof t===`string`){const{ptr:r}=this.writeString(t);this.exports.qjs_define_prop_string(e.ptr,r,n.ptr,i);this.exports.wasm_free(r)}else this.exports.qjs_define_prop_value(e.ptr,t.ptr,n.ptr,i)}getProp(e,t){this.assertNotDisposed();return new JSValueHandle(this,this.exports.qjs_get_prop_value(e.ptr,t.ptr))}getException(){this.assertNotDisposed();return new JSValueHandle(this,this.exports.qjs_get_exception())}newError(e){this.assertNotDisposed();const t=this.exports.qjs_new_error();const n=new JSValueHandle(this,t);if(typeof e===`string`){const t=this.newString(e);n.setProp(`message`,t);t.dispose()}else{const t=this.newString(e.message);n.setProp(`message`,t);t.dispose();if(e.name){const t=this.newString(e.name);n.setProp(`name`,t);t.dispose()}if(e.stack){const t=this.newString(e.stack);n.setProp(`stack`,t);t.dispose()}}return n}typeof(e){this.assertNotDisposed();const t=this.exports;if(t.qjs_is_undefined(e.ptr))return`undefined`;if(t.qjs_is_null(e.ptr))return`object`;if(t.qjs_is_bool(e.ptr))return`boolean`;if(t.qjs_is_number(e.ptr))return`number`;if(t.qjs_is_big_int(e.ptr))return`bigint`;if(t.qjs_is_string(e.ptr))return`string`;if(t.qjs_is_symbol(e.ptr))return`symbol`;if(t.qjs_is_function(e.ptr))return`function`;if(t.qjs_is_object(e.ptr))return`object`;return`unknown`}dump(e){this.assertNotDisposed();return this._dump(e,/* @__PURE__ */ new Map)}_dump(e,t){const n=this.exports;if(n.qjs_is_undefined(e.ptr))return void 0;if(n.qjs_is_null(e.ptr))return null;if(n.qjs_is_bool(e.ptr))return n.qjs_get_bool(e.ptr)!==0;if(n.qjs_is_number(e.ptr))return n.qjs_get_float64(e.ptr);if(n.qjs_is_string(e.ptr))return e.toString();if(n.qjs_is_big_int(e.ptr))return e.toBigInt();if(n.qjs_is_symbol(e.ptr)){const t=n.wasm_malloc(4);const r=n.qjs_get_symbol_description(e.ptr,t);const i=new DataView(n.memory.buffer).getUint32(t,true);n.wasm_free(t);if(r===1){const e=new JSValueHandle(this,i);const t=e.toString();e.dispose();return Symbol.for(t)}else if(r===2){new JSValueHandle(this,i).dispose();return}return}if(n.qjs_is_array_buffer(e.ptr))return e.toArrayBuffer();if(n.qjs_is_exception(e.ptr)){const e=this.getException();const t=e.toString();e.dispose();return new Error(t)}if(n.qjs_is_function(e.ptr))return void 0;if(n.qjs_is_object(e.ptr)){const r=n.qjs_get_value_ptr(e.ptr);if(r){const e=t.get(r);if(e!==void 0)return e}}if(n.qjs_is_object(e.ptr)){const t=n.wasm_malloc(4);const r=n.wasm_malloc(4);const i=n.wasm_malloc(4);const a=n.qjs_get_typed_array_buffer(e.ptr,t,r,i);const o=new JSValueHandle(this,a);if(n.qjs_is_exception(o.ptr)===0){const e=new DataView(n.memory.buffer);const a=e.getUint32(t,true);const s=e.getUint32(r,true);const c=e.getUint32(i,true);n.wasm_free(t);n.wasm_free(r);n.wasm_free(i);const l=n.wasm_malloc(4);const u=n.qjs_get_array_buffer(o.ptr,l);n.wasm_free(l);o.dispose();if(u!==0){const e=new Uint8Array(n.memory.buffer,u+a,s).slice();switch(c){case 1:return e;case 2:return new Uint16Array(e.buffer);case 4:return new Uint32Array(e.buffer);case 8:return new Float64Array(e.buffer);default:return e}}}else{o.dispose();n.wasm_free(t);n.wasm_free(r);n.wasm_free(i)}}if(n.qjs_is_array(e.ptr)){const r=e.getProp(`length`);const i=n.qjs_get_float64(r.ptr);r.dispose();const a=[];const o=n.qjs_get_value_ptr(e.ptr);if(o)t.set(o,a);for(let r=0;r<i;r++){const i=n.qjs_get_prop_uint32(e.ptr,r);const o=new JSValueHandle(this,i);a.push(this._dump(o,t));o.dispose()}return a}if(n.qjs_is_error(e.ptr)){const t=e.getProp(`name`);const n=e.getProp(`message`);const r=e.getProp(`stack`);const i=t.isUndefined?`Error`:t.toString();const a=n.isUndefined?``:n.toString();const o=r.isUndefined?void 0:r.toString();t.dispose();n.dispose();r.dispose();const s=new Error(a);s.name=i;if(o!==void 0)s.stack=o;return s}if(n.qjs_is_object(e.ptr)){const r=n.qjs_get_own_property_names(e.ptr);const i=new JSValueHandle(this,r);if(n.qjs_is_exception(i.ptr)!==0){i.dispose();return{}}const a=i.getProp(`length`);const o=n.qjs_get_float64(a.ptr);a.dispose();const s={};const c=n.qjs_get_value_ptr(e.ptr);if(c)t.set(c,s);for(let r=0;r<o;r++){const a=n.qjs_get_prop_uint32(i.ptr,r);const o=new JSValueHandle(this,a);const c=o.toString();o.dispose();const l=e.getProp(c);s[c]=this._dump(l,t);l.dispose()}i.dispose();return s}}hostToHandle(e){this.assertNotDisposed();if(e===void 0)return this.undefined;if(e===null)return this.null;if(e===true)return this.true;if(e===false)return this.false;if(typeof e===`number`)return this.newNumber(e);if(typeof e===`string`)return this.newString(e);if(typeof e===`bigint`)return this.newBigInt(e);if(typeof e===`symbol`){const t=Symbol.keyFor(e);if(t!==void 0)return this.newSymbolFor(t);throw new Error(`Cannot convert local symbol to QuickJS handle. Use Symbol.for() for cross-boundary symbols.`)}if(e instanceof Promise){const t=this.newPromise();e.then(e=>{t.resolve(this.hostToHandle(e));this.executePendingJobs()},e=>{t.reject(this.hostToHandle(e));this.executePendingJobs()});return t.handle}if(e instanceof Error)return this.newError(e);if(e instanceof ArrayBuffer)return this.newArrayBuffer(e);if(e instanceof Uint8Array)return this.newUint8Array(e);if(ArrayBuffer.isView(e))return this.newArrayBuffer(new Uint8Array(e.buffer,e.byteOffset,e.byteLength));if(Array.isArray(e)){const t=this.newArray();for(let n=0;n<e.length;n++){const r=this.hostToHandle(e[n]);this.exports.qjs_set_prop_uint32(t.ptr,n,r.ptr);r.dispose()}return t}if(typeof e===`object`&&e!==null){const t=this.newObject();for(const[n,r]of Object.entries(e)){const e=this.hostToHandle(r);t.setProp(n,e);e.dispose()}return t}return this.undefined}snapshot(){this.assertNotDisposed();return{memory:new Uint8Array(this.exports.memory.buffer).slice(),stackPointer:this.exports.__stack_pointer.value,runtimePtr:this.exports.qjs_get_runtime_ptr(),contextPtr:this.exports.qjs_get_context_ptr(),extensions:this.loadedExtensions.map(e=>({name:e.name,memoryBase:e.memoryBase,tableBase:e.tableBase,initFn:e.initFn}))}}registerHostCallback(e,t){this.hostCallbacks.set(e,t)}dispose(){if(!this.disposed){this.disposed=true;this._global=null;this._undefined=null;this._null=null;this._true=null;this._false=null;this._ownedHandles.clear();this.hostCallbacks.clear();this._activeScope=null;this.exports=null;this.instance=null;this.module=null}}[Symbol.dispose](){this.dispose()}assertNotDisposed(){if(this.disposed)throw new Error(`QuickJS instance has been disposed`)}_getExports(){return this.exports}_getMemory(){return this.exports.memory}_writeString(e){return this.writeString(e)}_readCString(e){return this.readCString(e)}};function stringKeyNeedsValuePath(e){return e.includes(`\0`)||LONE_SURROGATE_RE.test(e)}const wtf8Decoder=new TextDecoder;const wtf8Encoder=new TextEncoder;const LONE_SURROGATE_RE=/(?:[\uD800-\uDBFF](?![\uDC00-\uDFFF]))|(?:(?<![\uD800-\uDBFF])[\uDC00-\uDFFF])/;function encodeWtf8(e){if(!LONE_SURROGATE_RE.test(e))return wtf8Encoder.encode(e);const t=[];for(let n=0;n<e.length;n++){const r=e.charCodeAt(n);if(r<128)t.push(r);else if(r<2048)t.push(192|r>>6,128|r&63);else if(r>=55296&&r<=56319&&n+1<e.length){const i=e.charCodeAt(n+1);if(i>=56320&&i<=57343){const e=65536+(r-55296<<10)+(i-56320);t.push(240|e>>18,128|e>>12&63,128|e>>6&63,128|e&63);n++;continue}t.push(224|r>>12,128|r>>6&63,128|r&63)}else t.push(224|r>>12,128|r>>6&63,128|r&63)}return new Uint8Array(t)}function decodeWtf8(e){let t=false;for(let n=0;n<e.length-1;n++)if(e[n]===237&&e[n+1]>=160&&e[n+1]<=191){t=true;break}if(!t)return wtf8Decoder.decode(e);let n=``;let r=0;while(r<e.length){const t=e[r];if(t<128){n+=String.fromCharCode(t);r+=1}else if(t<224){n+=String.fromCharCode((t&31)<<6|e[r+1]&63);r+=2}else if(t<240){n+=String.fromCharCode((t&15)<<12|(e[r+1]&63)<<6|e[r+2]&63);r+=3}else{const i=(t&7)<<18|(e[r+1]&63)<<12|(e[r+2]&63)<<6|e[r+3]&63;n+=String.fromCodePoint(i);r+=4}}return n}var JSException=class extends Error{handle;#e;#t;#n;constructor(e){const t={stack:[],error:void 0,hasError:false};try{super();this.handle=e;delete this.stack;const n=__addDisposableResource(t,e.getProp(`message`),false);this.#e=e.getProp(`name`).consume(e=>e.isUndefined?`Error`:e.toString());this.#t=n.isUndefined?e.toString():n.toString();this.#n=e.getProp(`stack`).consume(e=>e.isUndefined?void 0:e.toString())}catch(e){t.error=e;t.hasError=true}finally{__disposeResources(t)}}get name(){return this.#e}set name(e){this.#e=e}get message(){return this.#t}set message(e){this.#t=e}get stack(){return this.#n}set stack(e){this.#n=e}dispose(){this.handle.dispose()}[Symbol.dispose](){this.handle.dispose()}};var JSValueHandle=class JSValueHandle{vm;ptr;disposed_=false;singleton;borrowed;_onDispose;constructor(e,t,n=false,r=false){this.vm=e;this.ptr=t;this.singleton=n;this.borrowed=r;if(!n&&!r)e._activeScope?.add(this)}get _isBorrowed(){return this.borrowed}get disposed(){return this.disposed_}get isUndefined(){return this.vm._getExports().qjs_is_undefined(this.ptr)!==0}get isNull(){return this.vm._getExports().qjs_is_null(this.ptr)!==0}get isBool(){return this.vm._getExports().qjs_is_bool(this.ptr)!==0}get isNumber(){return this.vm._getExports().qjs_is_number(this.ptr)!==0}get isString(){return this.vm._getExports().qjs_is_string(this.ptr)!==0}get isSymbol(){return this.vm._getExports().qjs_is_symbol(this.ptr)!==0}get isBigInt(){return this.vm._getExports().qjs_is_big_int(this.ptr)!==0}get isObject(){return this.vm._getExports().qjs_is_object(this.ptr)!==0}get isArray(){return this.vm._getExports().qjs_is_array(this.ptr)!==0}get isFunction(){return this.vm._getExports().qjs_is_function(this.ptr)!==0}get isError(){return this.vm._getExports().qjs_is_error(this.ptr)!==0}get isPromise(){return this.vm._getExports().qjs_is_promise(this.ptr)!==0}get isArrayBuffer(){return this.vm._getExports().qjs_is_array_buffer(this.ptr)!==0}get isProxy(){return this.vm._getExports().qjs_is_proxy(this.ptr)!==0}get isMap(){return this.vm._getExports().qjs_is_map(this.ptr)!==0}get isSet(){return this.vm._getExports().qjs_is_set(this.ptr)!==0}get isDate(){return this.vm._getExports().qjs_is_date(this.ptr)!==0}get isRegExp(){return this.vm._getExports().qjs_is_regexp(this.ptr)!==0}get isWeakRef(){return this.vm._getExports().qjs_is_weak_ref(this.ptr)!==0}get isWeakMap(){return this.vm._getExports().qjs_is_weak_map(this.ptr)!==0}get isWeakSet(){return this.vm._getExports().qjs_is_weak_set(this.ptr)!==0}get isDataView(){return this.vm._getExports().qjs_is_data_view(this.ptr)!==0}get identity(){return this.vm._getExports().qjs_get_value_ptr(this.ptr)}toBoolean(){return this.vm._getExports().qjs_get_bool(this.ptr)!==0}get classId(){return this.vm._getExports().qjs_get_class_id(this.ptr)}get className(){const e=new JSValueHandle(this.vm,this.vm._getExports().qjs_get_class_name(this.ptr));if(this.vm._getExports().qjs_is_exception(e.ptr)!==0){e.dispose();throw new JSException(this.vm.getException())}try{return e.isUndefined?void 0:e.toString()}finally{e.dispose()}}get promiseState(){return this.vm._getExports().qjs_promise_state(this.ptr)}get typeof(){return this.vm.typeof(this)}get length(){const e=this.getProp(`length`);const t=e.toNumber();e.dispose();return t}get constructorName(){const e=this.getProp(`constructor`);if(e.isUndefined||e.isNull){e.dispose();return}const t=e.getProp(`name`);e.dispose();if(t.isUndefined||t.isNull){t.dispose();return}const n=t.toString();t.dispose();return n}keys(){const e=this.vm._getExports();const t=e.qjs_get_own_property_names(this.ptr);const n=new JSValueHandle(this.vm,t);if(e.qjs_is_exception(n.ptr)!==0){n.dispose();return[]}const r=n.getProp(`length`);const i=e.qjs_get_float64(r.ptr);r.dispose();const a=[];for(let t=0;t<i;t++){const r=e.qjs_get_prop_uint32(n.ptr,t);const i=new JSValueHandle(this.vm,r);a.push(i.toString());i.dispose()}n.dispose();return a}getOwnPropertyNames(){const e=this.vm._getExports();const t=e.qjs_get_own_property_names_all(this.ptr);const n=new JSValueHandle(this.vm,t);if(e.qjs_is_exception(n.ptr)!==0){n.dispose();return[]}const r=n.getProp(`length`);const i=e.qjs_get_float64(r.ptr);r.dispose();const a=[];for(let t=0;t<i;t++){const r=e.qjs_get_prop_uint32(n.ptr,t);const i=new JSValueHandle(this.vm,r);a.push(i.toString());i.dispose()}n.dispose();return a}getOwnPropertyKeys(){const e=this.vm._getExports();const t=e.qjs_get_own_property_keys(this.ptr);const n=new JSValueHandle(this.vm,t);if(e.qjs_is_exception(n.ptr)!==0){n.dispose();return[]}const r=n.getProp(`length`);const i=e.qjs_get_float64(r.ptr);r.dispose();const a=[];for(let t=0;t<i;t++){const r=e.qjs_get_prop_uint32(n.ptr,t);const i=new JSValueHandle(this.vm,r);if(i.isSymbol)a.push(i);else{a.push(i.toString());i.dispose()}}n.dispose();return a}getOwnPropertyDescriptor(e){const t={stack:[],error:void 0,hasError:false};try{const n=this.vm._getExports();let r;let i;if(typeof e===`string`){r=this.vm.newString(e);i=r.ptr}else i=e.ptr;const a=n.qjs_get_own_property_descriptor(this.ptr,i);r?.dispose();if(a===0)return void 0;const o=__addDisposableResource(t,new JSValueHandle(this.vm,a),false);if(n.qjs_is_exception(o.ptr)!==0)throw new JSException(this.vm.getException());const s=o.getProp(`enumerable`).consume(e=>n.qjs_get_bool(e.ptr)!==0);const c=o.getProp(`configurable`).consume(e=>n.qjs_get_bool(e.ptr)!==0);if(o.hasOwnProperty(`value`))return{value:o.getProp(`value`),writable:o.getProp(`writable`).consume(e=>n.qjs_get_bool(e.ptr)!==0),enumerable:s,configurable:c};return{get:o.getProp(`get`),set:o.getProp(`set`),enumerable:s,configurable:c}}catch(e){t.error=e;t.hasError=true}finally{__disposeResources(t)}}hasOwnProperty(e){if(stringKeyNeedsValuePath(e)){const t={stack:[],error:void 0,hasError:false};try{const n=__addDisposableResource(t,this.vm.newString(e),false);return this.vm._getExports().qjs_has_own_property_value(this.ptr,n.ptr)===1}catch(e){t.error=e;t.hasError=true}finally{__disposeResources(t)}}const{ptr:t}=this.vm._writeString(e);const n=this.vm._getExports().qjs_has_own_property(this.ptr,t);this.vm._getExports().wasm_free(t);return n===1}propertyIsEnumerable(e){if(stringKeyNeedsValuePath(e)){const t={stack:[],error:void 0,hasError:false};try{const n=__addDisposableResource(t,this.vm.newString(e),false);return this.vm._getExports().qjs_property_is_enumerable_value(this.ptr,n.ptr)===1}catch(e){t.error=e;t.hasError=true}finally{__disposeResources(t)}}const{ptr:t}=this.vm._writeString(e);const n=this.vm._getExports().qjs_property_is_enumerable(this.ptr,t);this.vm._getExports().wasm_free(t);return n===1}getPrototypeOf(){const e=this.vm._getExports().qjs_get_prototype_of(this.ptr);return new JSValueHandle(this.vm,e)}getProxyTarget(){const e=this.vm._getExports().qjs_get_proxy_target(this.ptr);const t=new JSValueHandle(this.vm,e);if(this.vm._getExports().qjs_is_exception(t.ptr)!==0){t.dispose();throw new JSException(this.vm.getException())}return t}getProxyHandler(){const e=this.vm._getExports().qjs_get_proxy_handler(this.ptr);const t=new JSValueHandle(this.vm,e);if(this.vm._getExports().qjs_is_exception(t.ptr)!==0){t.dispose();throw new JSException(this.vm.getException())}return t}getProp(e){if(stringKeyNeedsValuePath(e)){const t={stack:[],error:void 0,hasError:false};try{const n=__addDisposableResource(t,this.vm.newString(e),false);return this.vm.getProp(this,n)}catch(e){t.error=e;t.hasError=true}finally{__disposeResources(t)}}const{ptr:t}=this.vm._writeString(e);const n=this.vm._getExports().qjs_get_prop_string(this.ptr,t);this.vm._getExports().wasm_free(t);return new JSValueHandle(this.vm,n)}setProp(e,t){if(stringKeyNeedsValuePath(e)){const n={stack:[],error:void 0,hasError:false};try{const r=__addDisposableResource(n,this.vm.newString(e),false);this.vm.setProp(this,r,t);return}catch(e){n.error=e;n.hasError=true}finally{__disposeResources(n)}}const{ptr:n}=this.vm._writeString(e);this.vm._getExports().qjs_set_prop_string(this.ptr,n,t.ptr);this.vm._getExports().wasm_free(n)}defineProp(e,t,n){let r=0;if(n?.configurable)r|=1;if(n?.writable)r|=2;if(n?.enumerable)r|=4;if(typeof e===`string`){if(stringKeyNeedsValuePath(e)){const n={stack:[],error:void 0,hasError:false};try{const i=__addDisposableResource(n,this.vm.newString(e),false);this.vm._getExports().qjs_define_prop_value(this.ptr,i.ptr,t.ptr,r);return}catch(e){n.error=e;n.hasError=true}finally{__disposeResources(n)}}const{ptr:n}=this.vm._writeString(e);this.vm._getExports().qjs_define_prop_string(this.ptr,n,t.ptr,r);this.vm._getExports().wasm_free(n)}else this.vm._getExports().qjs_define_prop_value(this.ptr,e.ptr,t.ptr,r)}toNumber(){return this.vm._getExports().qjs_get_float64(this.ptr)}toBigInt(){const e=this.vm._getExports();const t=e.wasm_malloc(4);const n=e.wasm_malloc(4);if(e.qjs_get_big_int64(this.ptr,t,n)!==0){e.wasm_free(t);e.wasm_free(n);throw new Error(`Failed to convert value to BigInt`)}const r=new DataView(e.memory.buffer);const i=r.getUint32(t,true);const a=r.getInt32(n,true);e.wasm_free(t);e.wasm_free(n);return BigInt(a)<<32n|BigInt(i)}toArrayBuffer(){const e=this.vm._getExports();const t=e.wasm_malloc(4);if(e.qjs_is_array_buffer(this.ptr)){const n=e.qjs_get_array_buffer(this.ptr,t);if(n===0){e.wasm_free(t);throw new Error(`Failed to get ArrayBuffer data`)}const r=new DataView(e.memory.buffer).getUint32(t,true);e.wasm_free(t);return new Uint8Array(e.memory.buffer,n,r).slice().buffer}e.wasm_free(t);const n=e.wasm_malloc(4);const r=e.wasm_malloc(4);const i=e.wasm_malloc(4);const a=e.qjs_get_typed_array_buffer(this.ptr,n,r,i);const o=new JSValueHandle(this.vm,a);if(this.vm._getExports().qjs_is_exception(o.ptr)!==0){o.dispose();e.wasm_free(n);e.wasm_free(r);e.wasm_free(i);throw new Error(`Value is not an ArrayBuffer or typed array`)}const s=new DataView(e.memory.buffer);const c=s.getUint32(n,true);const l=s.getUint32(r,true);e.wasm_free(n);e.wasm_free(r);e.wasm_free(i);const u=e.wasm_malloc(4);const d=e.qjs_get_array_buffer(o.ptr,u);e.wasm_free(u);o.dispose();if(d===0)throw new Error(`Failed to get ArrayBuffer data from typed array`);return new Uint8Array(e.memory.buffer,d+c,l).slice().buffer}toUint8Array(){return new Uint8Array(this.toArrayBuffer())}toString(){const e=this.vm._getExports();const t=e.wasm_malloc(4);if(t===0)throw new Error(`wasm_malloc failed`);try{const n=e.qjs_get_string_len(this.ptr,t);if(n===0)return`<null>`;const r=new DataView(e.memory.buffer).getUint32(t,true);const i=decodeWtf8(new Uint8Array(e.memory.buffer,n,r));e.qjs_free_cstring(n);return i}finally{e.wasm_free(t)}}consume(e){try{return e(this)}finally{this.dispose()}}dup(){return new JSValueHandle(this.vm,this.vm._getExports().qjs_dup_value(this.ptr))}dispose(){if(this.singleton||this.borrowed)return;if(!this.disposed_){this.disposed_=true;this._onDispose?.();this._onDispose=void 0;const e=this.vm._getExports();if(e)e.qjs_free_value(this.ptr)}}[Symbol.dispose](){this.dispose()}};export{QuickJS as i,JSException as n,MAX_STACK_SIZE as r,EvalFlags as t};