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Log } from \"./debug\"\nimport {\n Asyncify,\n AsyncifySleepResult,\n EitherModule,\n EmscriptenModuleCallbacks,\n} from \"./emscripten-types\"\nimport { QuickJSAsyncifyError, QuickJSAsyncifySuspended } from \"./errors\"\nimport {\n BorrowedHeapCharPointer,\n JSContextPointer,\n JSRuntimePointer,\n JSValuePointer,\n} from \"./types-ffi\"\nimport { Lifetime, Scope } from \"./lifetime\"\nimport { InterruptHandler, QuickJSRuntime } from \"./runtime\"\nimport {\n AsyncRuntimeOptions,\n concat,\n ContextOptions,\n EitherFFI,\n JSModuleLoader,\n RuntimeOptions,\n RuntimeOptionsBase,\n} from \"./types\"\n\ntype EmscriptenCallback = (\n ...args: [Asyncify | undefined, ...BaseArgs]\n) => Result | AsyncifySleepResult\ntype MaybeAsyncEmscriptenCallback> =\n T extends EmscriptenCallback\n ? (...args: Args) => Result | Promise\n : never\ntype MaybeAsyncEmscriptenCallbacks = {\n [K in keyof EmscriptenModuleCallbacks]: MaybeAsyncEmscriptenCallback\n}\n\n/**\n * @private\n */\nexport interface ContextCallbacks {\n callFunction: MaybeAsyncEmscriptenCallbacks[\"callFunction\"]\n}\n\n/**\n * @private\n */\nexport interface RuntimeCallbacks {\n shouldInterrupt: MaybeAsyncEmscriptenCallbacks[\"shouldInterrupt\"]\n loadModuleSource: MaybeAsyncEmscriptenCallbacks[\"loadModuleSource\"]\n normalizeModule: MaybeAsyncEmscriptenCallbacks[\"normalizeModule\"]\n}\n\nclass QuickJSEmscriptenModuleCallbacks implements EmscriptenModuleCallbacks {\n public callFunction: EmscriptenModuleCallbacks[\"callFunction\"]\n public shouldInterrupt: EmscriptenModuleCallbacks[\"shouldInterrupt\"]\n public loadModuleSource: EmscriptenModuleCallbacks[\"loadModuleSource\"]\n public normalizeModule: EmscriptenModuleCallbacks[\"normalizeModule\"]\n constructor(args: EmscriptenModuleCallbacks) {\n this.callFunction = args.callFunction\n this.shouldInterrupt = args.shouldInterrupt\n this.loadModuleSource = args.loadModuleSource\n this.normalizeModule = args.normalizeModule\n }\n}\n\n/**\n * Options for [[QuickJSWASMModule.evalCode]].\n */\nexport interface ModuleEvalOptions {\n /**\n * Interrupt evaluation if `shouldInterrupt` returns `true`.\n * See [[shouldInterruptAfterDeadline]].\n */\n shouldInterrupt?: InterruptHandler\n\n /**\n * Memory limit, in bytes, of WebAssembly heap memory used by the QuickJS VM.\n */\n memoryLimitBytes?: number\n\n /**\n * Stack size limit for this vm, in bytes\n * To remove the limit, set to `0`.\n */\n maxStackSizeBytes?: number\n\n /**\n * Module loader for any `import` statements or expressions.\n */\n moduleLoader?: JSModuleLoader\n}\n\n/**\n * We use static functions per module to dispatch runtime or context calls from\n * C to the host. This class manages the indirection from a specific runtime or\n * context pointer to the appropriate callback handler.\n *\n * @private\n */\nexport class QuickJSModuleCallbacks {\n private module: EitherModule\n private contextCallbacks = new Map()\n private runtimeCallbacks = new Map()\n\n constructor(module: EitherModule) {\n this.module = module\n this.module.callbacks = this.cToHostCallbacks\n }\n\n setRuntimeCallbacks(rt: JSRuntimePointer, callbacks: RuntimeCallbacks) {\n this.runtimeCallbacks.set(rt, callbacks)\n }\n\n deleteRuntime(rt: JSRuntimePointer) {\n this.runtimeCallbacks.delete(rt)\n }\n\n setContextCallbacks(ctx: JSContextPointer, callbacks: ContextCallbacks) {\n this.contextCallbacks.set(ctx, callbacks)\n }\n\n deleteContext(ctx: JSContextPointer) {\n this.contextCallbacks.delete(ctx)\n }\n\n private suspendedCount = 0\n private suspended: QuickJSAsyncifySuspended | undefined\n\n private handleAsyncify(\n asyncify: Asyncify | undefined,\n fn: () => T | Promise\n ): T | AsyncifySleepResult {\n if (asyncify) {\n // We must always call asyncify.handleSync around our function.\n // This allows asyncify to resume suspended execution on the second call.\n // Asyncify internally can detect sync behavior, and avoid suspending.\n return asyncify.handleSleep((done) => {\n try {\n const result = fn()\n if (!(result instanceof Promise)) {\n debugLog(\"asyncify.handleSleep: not suspending:\", result)\n done(result)\n return\n }\n\n // Is promise, we intend to suspend.\n if (this.suspended) {\n throw new QuickJSAsyncifyError(\n `Already suspended at: ${this.suspended.stack}\\nAttempted to suspend at:`\n )\n } else {\n this.suspended = new QuickJSAsyncifySuspended(`(${this.suspendedCount++})`)\n debugLog(\"asyncify.handleSleep: suspending:\", this.suspended)\n }\n\n result.then(\n (resolvedResult) => {\n this.suspended = undefined\n debugLog(\"asyncify.handleSleep: resolved:\", resolvedResult)\n done(resolvedResult)\n },\n (error) => {\n debugLog(\"asyncify.handleSleep: rejected:\", error)\n console.error(\"QuickJS: cannot handle error in suspended function\", error)\n this.suspended = undefined\n }\n )\n } catch (error) {\n debugLog(\"asyncify.handleSleep: error:\", error)\n this.suspended = undefined\n throw error\n }\n })\n }\n\n // No asyncify - we should never return a promise.\n const value = fn()\n if (value instanceof Promise) {\n throw new Error(\"Promise return value not supported in non-asyncify context.\")\n }\n return value\n }\n\n private cToHostCallbacks = new QuickJSEmscriptenModuleCallbacks({\n callFunction: (asyncify, ctx, this_ptr, argc, argv, fn_id) =>\n this.handleAsyncify(asyncify, () => {\n try {\n const vm = this.contextCallbacks.get(ctx)\n if (!vm) {\n throw new Error(`QuickJSContext(ctx = ${ctx}) not found for C function call \"${fn_id}\"`)\n }\n return vm.callFunction(ctx, this_ptr, argc, argv, fn_id)\n } catch (error) {\n console.error(\"[C to host error: returning null]\", error)\n return 0 as JSValuePointer\n }\n }),\n\n shouldInterrupt: (asyncify, rt) =>\n this.handleAsyncify(asyncify, () => {\n try {\n const vm = this.runtimeCallbacks.get(rt)\n if (!vm) {\n throw new Error(`QuickJSRuntime(rt = ${rt}) not found for C interrupt`)\n }\n return vm.shouldInterrupt(rt)\n } catch (error) {\n console.error(\"[C to host interrupt: returning error]\", error)\n return 1\n }\n }),\n\n loadModuleSource: (asyncify, rt, ctx, moduleName) =>\n this.handleAsyncify(asyncify, () => {\n try {\n const runtimeCallbacks = this.runtimeCallbacks.get(rt)\n if (!runtimeCallbacks) {\n throw new Error(`QuickJSRuntime(rt = ${rt}) not found for C module loader`)\n }\n\n const loadModule = runtimeCallbacks.loadModuleSource\n if (!loadModule) {\n throw new Error(`QuickJSRuntime(rt = ${rt}) does not support module loading`)\n }\n return loadModule(rt, ctx, moduleName)\n } catch (error) {\n console.error(\"[C to host module loader error: returning null]\", error)\n return 0 as BorrowedHeapCharPointer\n }\n }),\n\n normalizeModule: (asyncify, rt, ctx, moduleBaseName, moduleName) =>\n this.handleAsyncify(asyncify, () => {\n try {\n const runtimeCallbacks = this.runtimeCallbacks.get(rt)\n if (!runtimeCallbacks) {\n throw new Error(`QuickJSRuntime(rt = ${rt}) not found for C module loader`)\n }\n\n const normalizeModule = runtimeCallbacks.normalizeModule\n if (!normalizeModule) {\n throw new Error(`QuickJSRuntime(rt = ${rt}) does not support module loading`)\n }\n return normalizeModule(rt, ctx, moduleBaseName, moduleName)\n } catch (error) {\n console.error(\"[C to host module loader error: returning null]\", error)\n return 0 as BorrowedHeapCharPointer\n }\n }),\n })\n}\n\n/**\n * Process RuntimeOptions and apply them to a QuickJSRuntime.\n * @private\n */\nexport function applyBaseRuntimeOptions(\n runtime: QuickJSRuntime,\n options: RuntimeOptionsBase\n): void {\n if (options.interruptHandler) {\n runtime.setInterruptHandler(options.interruptHandler)\n }\n\n if (options.maxStackSizeBytes !== undefined) {\n runtime.setMaxStackSize(options.maxStackSizeBytes)\n }\n\n if (options.memoryLimitBytes !== undefined) {\n runtime.setMemoryLimit(options.memoryLimitBytes)\n }\n}\n\n/**\n * Process ModuleEvalOptions and apply them to a QuickJSRuntime.\n * @private\n */\nexport function applyModuleEvalRuntimeOptions(\n runtime: T,\n options: ModuleEvalOptions\n) {\n if (options.moduleLoader) {\n runtime.setModuleLoader(options.moduleLoader)\n }\n\n if (options.shouldInterrupt) {\n runtime.setInterruptHandler(options.shouldInterrupt)\n }\n\n if (options.memoryLimitBytes !== undefined) {\n runtime.setMemoryLimit(options.memoryLimitBytes)\n }\n\n if (options.maxStackSizeBytes !== undefined) {\n runtime.setMaxStackSize(options.maxStackSizeBytes)\n }\n}\n\n/**\n * This class presents a Javascript interface to QuickJS, a Javascript interpreter\n * that supports EcmaScript 2020 (ES2020).\n *\n * It wraps a single WebAssembly module containing the QuickJS library and\n * associated helper C code. WebAssembly modules are completely isolated from\n * each other by the host's WebAssembly runtime. Separate WebAssembly modules\n * have the most isolation guarantees possible with this library.\n *\n * The simplest way to start running code is {@link evalCode}. This shortcut\n * method will evaluate Javascript safely and return the result as a native\n * Javascript value.\n *\n * For more control over the execution environment, or to interact with values\n * inside QuickJS, create a context with {@link newContext} or a runtime with\n * {@link newRuntime}.\n */\nexport class QuickJSWASMModule {\n /** @private */\n protected ffi: EitherFFI\n /** @private */\n protected callbacks: QuickJSModuleCallbacks\n /** @private */\n protected module: EitherModule\n\n /** @private */\n constructor(module: EitherModule, ffi: EitherFFI) {\n this.module = module\n this.ffi = ffi\n this.callbacks = new QuickJSModuleCallbacks(module)\n }\n\n /**\n * Create a runtime.\n * Use the runtime to set limits on CPU and memory usage and configure module\n * loading for one or more [[QuickJSContext]]s inside the runtime.\n */\n newRuntime(options: RuntimeOptions = {}): QuickJSRuntime {\n const rt = new Lifetime(this.ffi.QTS_NewRuntime(), undefined, (rt_ptr) => {\n this.callbacks.deleteRuntime(rt_ptr)\n this.ffi.QTS_FreeRuntime(rt_ptr)\n })\n\n const runtime = new QuickJSRuntime({\n module: this.module,\n callbacks: this.callbacks,\n ffi: this.ffi,\n rt,\n })\n\n applyBaseRuntimeOptions(runtime, options)\n\n if (options.moduleLoader) {\n runtime.setModuleLoader(options.moduleLoader)\n }\n\n return runtime\n }\n\n /**\n * A simplified API to create a new [[QuickJSRuntime]] and a\n * [[QuickJSContext]] inside that runtime at the same time. The runtime will\n * be disposed when the context is disposed.\n */\n newContext(options: ContextOptions = {}): QuickJSContext {\n const runtime = this.newRuntime()\n const context = runtime.newContext({\n ...options,\n ownedLifetimes: concat(runtime, options.ownedLifetimes),\n })\n runtime.context = context\n return context\n }\n\n /**\n * One-off evaluate code without needing to create a [[QuickJSRuntime]] or\n * [[QuickJSContext]] explicitly.\n *\n * To protect against infinite loops, use the `shouldInterrupt` option. The\n * [[shouldInterruptAfterDeadline]] function will create a time-based deadline.\n *\n * If you need more control over how the code executes, create a\n * [[QuickJSRuntime]] (with [[newRuntime]]) or a [[QuickJSContext]] (with\n * [[newContext]] or [[QuickJSRuntime.newContext]]), and use its\n * [[QuickJSContext.evalCode]] method.\n *\n * Asynchronous callbacks may not run during the first call to `evalCode`. If\n * you need to work with async code inside QuickJS, create a runtime and use\n * [[QuickJSRuntime.executePendingJobs]].\n *\n * @returns The result is coerced to a native Javascript value using JSON\n * serialization, so properties and values unsupported by JSON will be dropped.\n *\n * @throws If `code` throws during evaluation, the exception will be\n * converted into a native Javascript value and thrown.\n *\n * @throws if `options.shouldInterrupt` interrupted execution, will throw a Error\n * with name `\"InternalError\"` and message `\"interrupted\"`.\n */\n evalCode(code: string, options: ModuleEvalOptions = {}): unknown {\n return Scope.withScope((scope) => {\n const vm = scope.manage(this.newContext())\n\n applyModuleEvalRuntimeOptions(vm.runtime, options)\n const result = vm.evalCode(code, \"eval.js\")\n\n if (options.memoryLimitBytes !== undefined) {\n // Remove memory limit so we can dump the result without exceeding it.\n vm.runtime.setMemoryLimit(-1)\n }\n\n if (result.error) {\n const error = vm.dump(scope.manage(result.error))\n throw error\n }\n\n const value = vm.dump(scope.manage(result.value))\n return value\n })\n }\n\n /**\n * Get a low-level interface to the QuickJS functions in this WebAssembly\n * module.\n * @experimental\n * @unstable No warranty is provided with this API. It could change at any time.\n * @private\n */\n getFFI(): EitherFFI {\n return this.ffi\n }\n}\n"]}