From 7f99f036d49792f3085f1321d783cd212dbe80e0 Mon Sep 17 00:00:00 2001 From: Ingvar Stepanyan Date: Thu, 4 Mar 2021 17:54:17 +0000 Subject: [PATCH] Make maybe_memory truly optional (#2469) * Make maybe_memory optional in TS As the name implies, it's already optional, but wasn't marked as such in TS. We could put some more complicated / stricter types here depending on type of the first argument, but at least this fixes the issue for TS consumers. Fixes #2133 * Add rust-toolchain to raytrace example It should always be built with nightly, and this file sets the toolchain for Rustup. * Rework init_memory - Unify `init_memory` for `maybe_memory` case to use either the explicitly given value or the default (`new WebAssembly.Memory(...)`). - Move it to the main `init` function where all other `imports` are assigned too. - Remove global `memory` variable which doesn't seem to be used by anything. * Format * Update cargo fmt & reformat again * Use explicit nightly version for Raytracer on CI * Delete rust-toolchain * Update azure-pipelines.yml --- crates/cli-support/src/js/mod.rs | 32 +++++++++++++++----------------- 1 file changed, 15 insertions(+), 17 deletions(-) diff --git a/crates/cli-support/src/js/mod.rs b/crates/cli-support/src/js/mod.rs index aafb1c2fe26..d3c0cf9dad2 100644 --- a/crates/cli-support/src/js/mod.rs +++ b/crates/cli-support/src/js/mod.rs @@ -560,7 +560,7 @@ impl<'a> Context<'a> { let (memory_doc, memory_param) = if has_memory { ( "* @param {WebAssembly.Memory} maybe_memory\n", - ", maybe_memory: WebAssembly.Memory", + ", maybe_memory?: WebAssembly.Memory", ) } else { ("", "") @@ -610,24 +610,23 @@ impl<'a> Context<'a> { ) -> Result<(String, String), Error> { let module_name = "wbg"; let mut init_memory_arg = ""; - let mut init_memory1 = String::new(); - let mut init_memory2 = String::new(); + let mut init_memory = String::new(); let mut has_memory = false; if let Some(mem) = self.module.memories.iter().next() { if let Some(id) = mem.import { self.module.imports.get_mut(id).module = module_name.to_string(); - let mut memory = String::from("new WebAssembly.Memory({"); - memory.push_str(&format!("initial:{}", mem.initial)); + init_memory = format!( + "imports.{}.memory = maybe_memory || new WebAssembly.Memory({{", + module_name + ); + init_memory.push_str(&format!("initial:{}", mem.initial)); if let Some(max) = mem.maximum { - memory.push_str(&format!(",maximum:{}", max)); + init_memory.push_str(&format!(",maximum:{}", max)); } if mem.shared { - memory.push_str(",shared:true"); + init_memory.push_str(",shared:true"); } - memory.push_str("})"); - self.imports_post.push_str("let memory;\n"); - init_memory1 = format!("memory = imports.{}.memory = maybe_memory;", module_name); - init_memory2 = format!("memory = imports.{}.memory = {};", module_name, memory); + init_memory.push_str("});"); init_memory_arg = ", maybe_memory"; has_memory = true; } @@ -706,9 +705,8 @@ impl<'a> Context<'a> { let js = format!( "\ - async function load(module, imports{init_memory_arg}) {{ + async function load(module, imports) {{ if (typeof Response === 'function' && module instanceof Response) {{ - {init_memory2} if (typeof WebAssembly.instantiateStreaming === 'function') {{ try {{ return await WebAssembly.instantiateStreaming(module, imports); @@ -731,7 +729,6 @@ impl<'a> Context<'a> { return await WebAssembly.instantiate(bytes, imports); }} else {{ - {init_memory1} const instance = await WebAssembly.instantiate(module, imports); if (instance instanceof WebAssembly.Instance) {{ @@ -752,7 +749,9 @@ impl<'a> Context<'a> { input = fetch(input); }} - const {{ instance, module }} = await load(await input, imports{init_memory_arg}); + {init_memory} + + const {{ instance, module }} = await load(await input, imports); wasm = instance.exports; init.__wbindgen_wasm_module = module; @@ -762,8 +761,7 @@ impl<'a> Context<'a> { ", init_memory_arg = init_memory_arg, default_module_path = default_module_path, - init_memory1 = init_memory1, - init_memory2 = init_memory2, + init_memory = init_memory, start = if needs_manual_start { "wasm.__wbindgen_start();" } else {