Skip to content

Commit 7f99f03

Browse files
authored
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
1 parent 38ba374 commit 7f99f03

File tree

1 file changed

+15
-17
lines changed
  • crates/cli-support/src/js

1 file changed

+15
-17
lines changed

crates/cli-support/src/js/mod.rs

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -560,7 +560,7 @@ impl<'a> Context<'a> {
560560
let (memory_doc, memory_param) = if has_memory {
561561
(
562562
"* @param {WebAssembly.Memory} maybe_memory\n",
563-
", maybe_memory: WebAssembly.Memory",
563+
", maybe_memory?: WebAssembly.Memory",
564564
)
565565
} else {
566566
("", "")
@@ -610,24 +610,23 @@ impl<'a> Context<'a> {
610610
) -> Result<(String, String), Error> {
611611
let module_name = "wbg";
612612
let mut init_memory_arg = "";
613-
let mut init_memory1 = String::new();
614-
let mut init_memory2 = String::new();
613+
let mut init_memory = String::new();
615614
let mut has_memory = false;
616615
if let Some(mem) = self.module.memories.iter().next() {
617616
if let Some(id) = mem.import {
618617
self.module.imports.get_mut(id).module = module_name.to_string();
619-
let mut memory = String::from("new WebAssembly.Memory({");
620-
memory.push_str(&format!("initial:{}", mem.initial));
618+
init_memory = format!(
619+
"imports.{}.memory = maybe_memory || new WebAssembly.Memory({{",
620+
module_name
621+
);
622+
init_memory.push_str(&format!("initial:{}", mem.initial));
621623
if let Some(max) = mem.maximum {
622-
memory.push_str(&format!(",maximum:{}", max));
624+
init_memory.push_str(&format!(",maximum:{}", max));
623625
}
624626
if mem.shared {
625-
memory.push_str(",shared:true");
627+
init_memory.push_str(",shared:true");
626628
}
627-
memory.push_str("})");
628-
self.imports_post.push_str("let memory;\n");
629-
init_memory1 = format!("memory = imports.{}.memory = maybe_memory;", module_name);
630-
init_memory2 = format!("memory = imports.{}.memory = {};", module_name, memory);
629+
init_memory.push_str("});");
631630
init_memory_arg = ", maybe_memory";
632631
has_memory = true;
633632
}
@@ -706,9 +705,8 @@ impl<'a> Context<'a> {
706705

707706
let js = format!(
708707
"\
709-
async function load(module, imports{init_memory_arg}) {{
708+
async function load(module, imports) {{
710709
if (typeof Response === 'function' && module instanceof Response) {{
711-
{init_memory2}
712710
if (typeof WebAssembly.instantiateStreaming === 'function') {{
713711
try {{
714712
return await WebAssembly.instantiateStreaming(module, imports);
@@ -731,7 +729,6 @@ impl<'a> Context<'a> {
731729
return await WebAssembly.instantiate(bytes, imports);
732730
733731
}} else {{
734-
{init_memory1}
735732
const instance = await WebAssembly.instantiate(module, imports);
736733
737734
if (instance instanceof WebAssembly.Instance) {{
@@ -752,7 +749,9 @@ impl<'a> Context<'a> {
752749
input = fetch(input);
753750
}}
754751
755-
const {{ instance, module }} = await load(await input, imports{init_memory_arg});
752+
{init_memory}
753+
754+
const {{ instance, module }} = await load(await input, imports);
756755
757756
wasm = instance.exports;
758757
init.__wbindgen_wasm_module = module;
@@ -762,8 +761,7 @@ impl<'a> Context<'a> {
762761
",
763762
init_memory_arg = init_memory_arg,
764763
default_module_path = default_module_path,
765-
init_memory1 = init_memory1,
766-
init_memory2 = init_memory2,
764+
init_memory = init_memory,
767765
start = if needs_manual_start {
768766
"wasm.__wbindgen_start();"
769767
} else {

0 commit comments

Comments
 (0)