Skip to content

Commit

Permalink
feat: implement initSync (#185)
Browse files Browse the repository at this point in the history
  • Loading branch information
SukkaW authored Dec 26, 2024
1 parent 765927c commit abb62e2
Showing 1 changed file with 15 additions and 4 deletions.
19 changes: 15 additions & 4 deletions src/lexer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -294,13 +294,24 @@ let wasm: {
ss(): number;
};

const getWasmBytes = () => (
binary => typeof Buffer !== 'undefined'
? Buffer.from(binary, 'base64')
: Uint8Array.from(atob(binary), x => x.charCodeAt(0))
)('WASM_BINARY');

/**
* Wait for init to resolve before calling `parse`.
*/
export const init = WebAssembly.compile(
(binary => typeof Buffer !== 'undefined' ? Buffer.from(binary, 'base64') : Uint8Array.from(atob(binary), x => x.charCodeAt(0)))
('WASM_BINARY')
)
export const init = WebAssembly.compile(getWasmBytes())
.then(WebAssembly.instantiate)
.then(({ exports }) => { wasm = exports as typeof wasm; });

export const initSync = () => {
if (wasm) {
return;
}
const compiled = new WebAssembly.Module(getWasmBytes());
wasm = new WebAssembly.Instance(compiled).exports as typeof wasm;
return;
};

0 comments on commit abb62e2

Please sign in to comment.