Skip to content

Commit

Permalink
Make maybe_memory truly optional (#2469)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
RReverser committed Mar 4, 2021
1 parent 38ba374 commit 7f99f03
Showing 1 changed file with 15 additions and 17 deletions.
32 changes: 15 additions & 17 deletions crates/cli-support/src/js/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
("", "")
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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);
Expand All @@ -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) {{
Expand All @@ -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;
Expand All @@ -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 {
Expand Down

0 comments on commit 7f99f03

Please sign in to comment.