Skip to content

Commit

Permalink
Inline wasm binary loader
Browse files Browse the repository at this point in the history
  • Loading branch information
mochaaP committed Feb 12, 2024
1 parent 3c5407e commit e17dd9d
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 17 deletions.
17 changes: 16 additions & 1 deletion src/lexer.js
Expand Up @@ -90,13 +90,28 @@ function copyLE (src, outBuf16) {
outBuf16[i] = src.charCodeAt(i++);
}

const loadWasm = (typeof EXTERNAL_PATH === "string" && (async () => {
const { readFile } = await import('node:fs/promises');
const { fileURLToPath } = await import('node:url');
return readFile(fileURLToPath(
import.meta.resolve("undefined/dist/lexer.wasm")
));
})) || (async () => {
const binary = WASM_BINARY
if (typeof window !== "undefined" && typeof atob === "function") {
return Uint8Array.from(atob(binary), (x) => x.charCodeAt(0));
} else {
return Buffer.from(binary, "base64");
}
});

let initPromise;
export function init () {
if (initPromise)
return initPromise;
return initPromise = (async () => {
const compiled = await WebAssembly.compile(
(await import('./loadWasm')).default
await loadWasm()
)
const { exports } = await WebAssembly.instantiate(compiled);
wasm = exports;
Expand Down
16 changes: 0 additions & 16 deletions src/loadWasm.js

This file was deleted.

0 comments on commit e17dd9d

Please sign in to comment.