From e3473f5e9efb8230697f22cca56afaa027b4bd48 Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Tue, 2 Apr 2019 14:15:42 -0700 Subject: [PATCH] Fix instantiation with a `Module` This commit fixes the `init` function when passed a `WebAssembly.Module`. Upon closer reading of the [spec] we see there's two possible return values from `WebAssembly.instantiate`. If passed a `Module`, it will return only the `Instance`. If passed a buffer source, though, it'll return an object with the module/instance. The fix here is to check the result value is an `Instance`, and if so assume the input must have been a module so it's paired up in the output. Closes #1418 [spec]: http://webassembly.github.io/spec/js-api/index.html#webassembly-namespace --- crates/cli-support/src/js/mod.rs | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/crates/cli-support/src/js/mod.rs b/crates/cli-support/src/js/mod.rs index e8828d81db8..c44cd7ccd21 100644 --- a/crates/cli-support/src/js/mod.rs +++ b/crates/cli-support/src/js/mod.rs @@ -903,12 +903,12 @@ impl<'a> Context<'a> { let ts = Self::ts_for_init_fn(mem.import.is_some()); let js = format!( "\ - function init(module_or_path{init_memory_arg}) {{ + function init(module{init_memory_arg}) {{ let result; const imports = {{ './{module}': __exports }}; - if (module_or_path instanceof URL || typeof module_or_path === 'string' || module_or_path instanceof Request) {{ + if (module instanceof URL || typeof module === 'string' || module instanceof Request) {{ {init_memory2} - const response = fetch(module_or_path); + const response = fetch(module); if (typeof WebAssembly.instantiateStreaming === 'function') {{ result = WebAssembly.instantiateStreaming(response, imports) .catch(e => {{ @@ -928,9 +928,13 @@ impl<'a> Context<'a> { }} }} else {{ {init_memory1} - result = WebAssembly.instantiate(module_or_path, imports) - .then(instance => {{ - return {{ instance, module: module_or_path }}; + result = WebAssembly.instantiate(module, imports) + .then(result => {{ + if (result instanceof WebAssembly.Instance) {{ + return {{ instance: result, module }}; + }} else {{ + return result; + }} }}); }} return result.then(({{instance, module}}) => {{