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AAA;IACJ,CAAC;CACF;AAzFD,wDAyFC","sourcesContent":["import { QuickJSAsyncContext } from \"./context-asyncify\"\nimport { QuickJSAsyncEmscriptenModule } from \"./emscripten-types\"\nimport { QuickJSNotImplemented } from \"./errors\"\nimport { QuickJSAsyncFFI } from \"./variants\"\nimport { Lifetime, Scope } from \"./lifetime\"\nimport {\n applyBaseRuntimeOptions,\n applyModuleEvalRuntimeOptions,\n ModuleEvalOptions,\n QuickJSWASMModule,\n} from \"./module\"\nimport { QuickJSAsyncRuntime } from \"./runtime-asyncify\"\nimport { AsyncRuntimeOptions, ContextOptions, RuntimeOptions } from \"./types\"\n\n/**\n * Asyncified version of [[QuickJSWASMModule]].\n *\n * Due to limitations of Emscripten's ASYNCIFY process, only a single async\n * function call can happen at a time across the entire WebAssembly module.\n *\n * That means that all runtimes, contexts, functions, etc created inside this\n * WebAssembly are limited to a single concurrent async action.\n * **Multiple concurrent async actions is an error.**\n *\n * To allow for multiple concurrent async actions, you must create multiple WebAssembly\n * modules.\n */\nexport class QuickJSAsyncWASMModule extends QuickJSWASMModule {\n /** @private */\n protected ffi: QuickJSAsyncFFI\n /** @private */\n protected module: QuickJSAsyncEmscriptenModule\n\n /** @private */\n constructor(module: QuickJSAsyncEmscriptenModule, ffi: QuickJSAsyncFFI) {\n super(module, ffi)\n this.ffi = ffi\n this.module = module\n }\n\n /**\n * Create a new async runtime inside this WebAssembly module. All runtimes inside a\n * module are limited to a single async call at a time. For multiple\n * concurrent async actions, create multiple WebAssembly modules.\n */\n override newRuntime(options: AsyncRuntimeOptions = {}): QuickJSAsyncRuntime {\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 const runtime = new QuickJSAsyncRuntime({\n module: this.module,\n ffi: this.ffi,\n rt,\n callbacks: this.callbacks,\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 override newContext(options: ContextOptions = {}): QuickJSAsyncContext {\n const runtime = this.newRuntime()\n const lifetimes = options.ownedLifetimes ? options.ownedLifetimes.concat([runtime]) : [runtime]\n const context = runtime.newContext({ ...options, ownedLifetimes: lifetimes })\n runtime.context = context\n return context\n }\n\n /** Synchronous evalCode is not supported. */\n override evalCode(): never {\n throw new QuickJSNotImplemented(\"QuickJSWASMModuleAsyncify.evalCode: use evalCodeAsync instead\")\n }\n\n /**\n * One-off evaluate code without needing to create a [[QuickJSRuntimeAsync]] or\n * [[QuickJSContextSync]] explicitly.\n *\n * This version allows for asynchronous Ecmascript module loading.\n *\n * Note that only a single async action can occur at a time inside the entire WebAssembly module.\n * **Multiple concurrent async actions is an error.**\n *\n * See the documentation for [[QuickJSWASMModule.evalCode]] for more details.\n */\n evalCodeAsync(code: string, options: ModuleEvalOptions): Promise<unknown> {\n // TODO: we should really figure out generator for the Promise monad...\n return Scope.withScopeAsync(async (scope) => {\n const vm = scope.manage(this.newContext())\n applyModuleEvalRuntimeOptions(vm.runtime, options)\n const result = await vm.evalCodeAsync(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"]}
|