chore(vendor): import pi 1.3.0
Imported as a linear vendor commit so forge rebases do not replay the upstream root tree. git-subtree-dir: third_party/pi git-subtree-split: 71023aec1546d6d677a65669c5665c0927ce0dd0
This commit is contained in:
1 parent
719370c8f7
commit
6081e3e325
1743 files changed
+205459
-131704
No files matched your search
+35
@@ -0,0 +1,35 @@
|
||||
import { readFile } from "node:fs/promises";
|
||||
import { createRequire } from "node:module";
|
||||
|
||||
/**
|
||||
* A compiled `WebAssembly.Module` of `quickjs-wasi/quickjs.wasm`. Typed opaquely because the Node
|
||||
* type definitions do not declare the WebAssembly globals (they live in the DOM lib).
|
||||
*/
|
||||
export type CodemodeWasmModule = object;
|
||||
|
||||
interface WebAssemblyGlobal {
|
||||
compile(bytes: Uint8Array): Promise<CodemodeWasmModule>;
|
||||
}
|
||||
|
||||
const modules = new Map<string, Promise<CodemodeWasmModule>>();
|
||||
|
||||
/**
|
||||
* Read and compile the QuickJS wasm once per path. `path` defaults to the file in the installed
|
||||
* `quickjs-wasi` package; pass it when that file lives elsewhere, for example embedded in a Bun
|
||||
* compiled executable. A failed load is retried on the next call.
|
||||
*/
|
||||
export function loadQuickJSWasm(path?: string): Promise<CodemodeWasmModule> {
|
||||
const resolved = path ?? createRequire(import.meta.url).resolve("quickjs-wasi/quickjs.wasm");
|
||||
let module = modules.get(resolved);
|
||||
if (!module) {
|
||||
const { WebAssembly } = globalThis as unknown as { WebAssembly: WebAssemblyGlobal };
|
||||
module = readFile(resolved)
|
||||
.then((bytes) => WebAssembly.compile(bytes))
|
||||
.catch((error: unknown) => {
|
||||
modules.delete(resolved);
|
||||
throw error;
|
||||
});
|
||||
modules.set(resolved, module);
|
||||
}
|
||||
return module;
|
||||
}
|
||||
Reference in new issue
Block a user