From abb62e294868e0d8ef229d163a4d30b2b57cf4ff Mon Sep 17 00:00:00 2001 From: Sukka Date: Fri, 27 Dec 2024 00:05:52 +0800 Subject: [PATCH] feat: implement `initSync` (#185) --- src/lexer.ts | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/src/lexer.ts b/src/lexer.ts index 6548f6d..8ff4732 100755 --- a/src/lexer.ts +++ b/src/lexer.ts @@ -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; +};