diff --git a/lib/wasm.js b/lib/wasm.js index 3091d9ee..a83f0003 100644 --- a/lib/wasm.js +++ b/lib/wasm.js @@ -13,18 +13,21 @@ function Mapping() { this.name = null; } -let cachedWasm = null; - -module.exports = function wasm() { - if (cachedWasm) { - return cachedWasm; +let cachedCompiledWasm = null; +async function compile() { + if (!cachedCompiledWasm) { + const buf = await readWasm(); + cachedCompiledWasm = await WebAssembly.compile(buf); } + return cachedCompiledWasm; +} +module.exports = function wasm() { const callbackStack = []; - cachedWasm = readWasm() - .then(buffer => { - return WebAssembly.instantiate(buffer, { + return compile() + .then(module => { + return WebAssembly.instantiate(module, { env: { mapping_callback( generatedLine, @@ -116,9 +119,9 @@ module.exports = function wasm() { }, }); }) - .then(Wasm => { + .then(instance => { return { - exports: Wasm.instance.exports, + exports: instance.exports, withMappingCallback: (mappingCallback, f) => { callbackStack.push(mappingCallback); try { @@ -128,11 +131,5 @@ module.exports = function wasm() { } }, }; - }) - .then(null, e => { - cachedWasm = null; - throw e; }); - - return cachedWasm; };