From 96266a06066ddd797e7e406c69f1e450d32adae5 Mon Sep 17 00:00:00 2001 From: Michael Schmidt Date: Tue, 19 Nov 2024 16:36:52 +0100 Subject: [PATCH 01/11] Improved variable names in generated JS --- crates/cli-support/src/js/binding.rs | 315 +++++++++--------- crates/cli/tests/reference/getter-setter.js | 6 +- crates/cli/tests/reference/result.js | 16 +- crates/cli/tests/reference/string-arg.d.ts | 1 + crates/cli/tests/reference/string-arg.js | 28 +- crates/cli/tests/reference/string-arg.rs | 5 + crates/cli/tests/reference/string-arg.wat | 10 +- .../cli/tests/reference/wasm-export-types.js | 14 +- crates/cli/tests/reference/web-sys.js | 8 +- 9 files changed, 213 insertions(+), 190 deletions(-) diff --git a/crates/cli-support/src/js/binding.rs b/crates/cli-support/src/js/binding.rs index f79936e1985..7213adc700b 100644 --- a/crates/cli-support/src/js/binding.rs +++ b/crates/cli-support/src/js/binding.rs @@ -9,7 +9,6 @@ use crate::wit::InstructionData; use crate::wit::{Adapter, AdapterId, AdapterKind, AdapterType, Instruction}; use anyhow::{anyhow, bail, Error}; use std::collections::HashSet; -use std::fmt::Write; use walrus::{Module, ValType}; /// A one-size-fits-all builder for processing WebIDL bindings and generating @@ -58,6 +57,10 @@ pub struct JsBuilder<'a, 'b> { /// use to translate the `arg.get` instruction. args: Vec, + /// A set of JS identifiers that cannot be used as temporary variables in + /// the generated JS code. + used_identifiers: HashSet, + /// The Wasm interface types "stack". The expressions pushed onto this stack /// are intended to be *pure*, and if they're not, they should be pushed /// into the `prelude`, assigned to a variable, and the variable should be @@ -151,10 +154,10 @@ impl<'a, 'b> Builder<'a, 'b> { ); } if consumes_self { - js.prelude("const ptr = this.__destroy_into_raw();"); - js.args.push("ptr".into()); + let ptr = js.alias("ptr", "this.__destroy_into_raw()".to_string()); + js.push_arg(ptr); } else { - js.args.push("this.__wbg_ptr".into()); + js.push_arg("this.__wbg_ptr".into()); } } for (i, param) in params.enumerate() { @@ -162,7 +165,7 @@ impl<'a, 'b> Builder<'a, 'b> { Some(list) => list[i].clone(), None => format!("arg{}", i), }; - js.args.push(arg.clone()); + js.push_arg(arg.clone()); function_args.push(arg); arg_tys.push(param); } @@ -436,6 +439,7 @@ impl<'a, 'b> JsBuilder<'a, 'b> { cx, debug_name, args: Vec::new(), + used_identifiers: HashSet::new(), tmp: 0, pre_try: String::new(), finally: String::new(), @@ -448,6 +452,61 @@ impl<'a, 'b> JsBuilder<'a, 'b> { &self.args[idx as usize] } + pub fn push_arg(&mut self, arg: String) { + self.args.push(arg.clone()); + self.used_identifiers.insert(arg); + } + + /// Creates a new variable with the given name and returns the name. + /// + /// If the name is already taken, this function panics. + pub fn guarantee_alias(&mut self, name: &str, expression: String) -> String { + let can_use = self.used_identifiers.insert(name.to_string()); + assert!( + can_use, + "identifier {} already in use in {}", + name, self.debug_name + ); + + self.prelude(&format!("const {name} = {expression};")); + name.to_string() + } + + /// Creates a new JS `const` variable with the given name and returns the + /// name. + /// + /// If the name is already taken, a different name is generated. + pub fn alias(&mut self, name: &str, expression: String) -> String { + let ident = self.unique_name(name); + self.prelude(&format!("const {} = {};", ident, expression)); + ident + } + + /// Creates a mutable JS `var`iable with the given name and returns the name. + /// + /// If the name is already taken, a different name is generated. + pub fn var(&mut self, name: &str, initial_value: String) -> String { + let ident = self.unique_name(name); + self.prelude(&format!("var {} = {};", ident, initial_value)); + ident + } + + /// Returns a unique variable name based on the given name. + pub fn unique_name(&mut self, preferred: &str) -> String { + if !self.used_identifiers.contains(preferred) { + self.used_identifiers.insert(preferred.to_string()); + return preferred.to_string(); + } + + loop { + self.tmp += 1; + let ident = format!("{}{}", preferred, self.tmp); + if self.used_identifiers.insert(ident.clone()) { + return ident; + } + } + } + pub fn prelude(&mut self, prelude: &str) { for line in prelude.trim().lines().map(|l| l.trim()) { if !line.is_empty() { @@ -466,12 +525,6 @@ impl<'a, 'b> JsBuilder<'a, 'b> { } } - pub fn tmp(&mut self) -> usize { - let ret = self.tmp; - self.tmp += 1; - ret - } - fn pop(&mut self) -> String { match self.stack.pop() { Some(s) => s, @@ -575,22 +628,14 @@ impl<'a, 'b> JsBuilder<'a, 'b> { let pass = self.cx.expose_pass_string_to_wasm(mem)?; let val = self.pop(); let malloc = self.cx.export_name_of(malloc); - let i = self.tmp(); let realloc = match realloc { Some(f) => format!(", wasm.{}", self.cx.export_name_of(f)), None => String::new(), }; - self.prelude(&format!( - "const ptr{i} = {f}({0}, wasm.{malloc}{realloc});", - val, - i = i, - f = pass, - malloc = malloc, - realloc = realloc, - )); - self.prelude(&format!("const len{} = WASM_VECTOR_LEN;", i)); - self.push(format!("ptr{}", i)); - self.push(format!("len{}", i)); + let ptr = self.alias("ptr", format!("{pass}({val}, wasm.{malloc}{realloc})")); + let len = self.alias("len", "WASM_VECTOR_LEN".to_string()); + self.push(ptr); + self.push(len); Ok(()) } } @@ -652,7 +697,6 @@ fn instruction( let (mut params, results) = invoc.params_results(js.cx); let mut args = Vec::new(); - let tmp = js.tmp(); if invoc.defer() { if let Instruction::DeferFree { .. } = instr { // Ignore `free`'s final `align` argument, since that's manually inserted later. @@ -661,11 +705,10 @@ fn instruction( // If the call is deferred, the arguments to the function still need to be // accessible in the `finally` block, so we declare variables to hold the args // outside of the try-finally block and then set those to the args. - for (i, arg) in js.stack[js.stack.len() - params..].iter().enumerate() { - let name = format!("deferred{tmp}_{i}"); - writeln!(js.pre_try, "let {name};").unwrap(); - writeln!(js.prelude, "{name} = {arg};").unwrap(); - args.push(name); + let mut slice = Vec::new(); + slice.extend(js.stack[js.stack.len() - params..].iter().cloned()); + for (i, arg) in slice.into_iter().enumerate() { + args.push(js.var(&format!("deferred{i}"), arg)); } if let Instruction::DeferFree { align, .. } = instr { // add alignment @@ -692,12 +735,12 @@ fn instruction( (true, _) => panic!("deferred calls must have no results"), (false, 0) => js.prelude(&format!("{};", call)), (false, n) => { - js.prelude(&format!("const ret = {};", call)); + let ret = js.alias("ret", call); if n == 1 { - js.push("ret".to_string()); + js.push(ret); } else { for i in 0..n { - js.push(format!("ret[{}]", i)); + js.push(format!("{ret}[{i}]")); } } } @@ -813,12 +856,12 @@ fn instruction( Instruction::Retptr { size } => { js.cx.inject_stack_pointer_shim()?; - js.prelude(&format!( - "const retptr = wasm.__wbindgen_add_to_stack_pointer(-{});", - size - )); + let return_ptr = js.guarantee_alias( + "retptr", + format!("wasm.__wbindgen_add_to_stack_pointer(-{size})"), + ); js.finally(&format!("wasm.__wbindgen_add_to_stack_pointer({});", size)); - js.stack.push("retptr".to_string()); + js.push(return_ptr); } Instruction::StoreRetptr { ty, offset, mem } => { @@ -878,11 +921,9 @@ fn instruction( Instruction::I32FromStringFirstChar => { let val = js.pop(); - let i = js.tmp(); - js.prelude(&format!("const char{i} = {val}.codePointAt(0);")); - let val = format!("char{i}"); - js.assert_char(&val); - js.push(val); + let char = js.alias("char", format!("{val}.codePointAt(0)")); + js.assert_char(&char); + js.push(char); } Instruction::I32FromExternrefOwned => { @@ -903,9 +944,8 @@ fn instruction( let val = js.pop(); js.assert_class(&val, class); js.assert_not_moved(&val); - let i = js.tmp(); - js.prelude(&format!("var ptr{} = {}.__destroy_into_raw();", i, val)); - js.push(format!("ptr{}", i)); + let ptr = js.alias("ptr", format!("{val}.__destroy_into_raw()")); + js.push(ptr); } Instruction::I32FromExternrefRustBorrow { class } => { @@ -918,14 +958,13 @@ fn instruction( Instruction::I32FromOptionRust { class } => { let val = js.pop(); js.cx.expose_is_like_none(); - let i = js.tmp(); - js.prelude(&format!("let ptr{} = 0;", i)); + let ptr = js.var("ptr", "0".to_string()); js.prelude(&format!("if (!isLikeNone({0})) {{", val)); js.assert_class(&val, class); js.assert_not_moved(&val); - js.prelude(&format!("ptr{} = {}.__destroy_into_raw();", i, val)); + js.prelude(&format!("{ptr} = {val}.__destroy_into_raw();")); js.prelude("}"); - js.push(format!("ptr{}", i)); + js.push(ptr); } Instruction::I32FromOptionExternref { table_and_alloc } => { @@ -959,13 +998,11 @@ fn instruction( Instruction::I32FromOptionChar => { let val = js.pop(); - let i = js.tmp(); js.cx.expose_is_like_none(); - js.prelude(&format!( - "const char{i} = isLikeNone({0}) ? 0xFFFFFF : {0}.codePointAt(0);", - val - )); - let val = format!("char{i}"); + let val = js.alias( + "char", + format!("isLikeNone({val}) ? 0xFFFFFF : {val}.codePointAt(0)"), + ); js.cx.expose_assert_char(); js.prelude(&format!( "if ({val} !== 0xFFFFFF) {{ _assertChar({val}); }}" @@ -1043,17 +1080,10 @@ fn instruction( let val = js.pop(); let func = js.cx.pass_to_wasm_function(kind.clone(), *mem)?; let malloc = js.cx.export_name_of(*malloc); - let i = js.tmp(); - js.prelude(&format!( - "const ptr{i} = {f}({0}, wasm.{malloc});", - val, - i = i, - f = func, - malloc = malloc, - )); - js.prelude(&format!("const len{} = WASM_VECTOR_LEN;", i)); - js.push(format!("ptr{}", i)); - js.push(format!("len{}", i)); + let ptr = js.alias("ptr", format!("{func}({val}, wasm.{malloc})")); + let len = js.alias("len", "WASM_VECTOR_LEN".to_string()); + js.push(ptr); + js.push(len); } Instruction::UnwrapResult { table_and_drop } => { @@ -1098,25 +1128,19 @@ fn instruction( let err = js.pop(); let len = js.pop(); let ptr = js.pop(); - let i = js.tmp(); + + let ptr = js.var("ptr", ptr); + let len = js.var("len", len); js.prelude(&format!( " - var ptr{i} = {ptr}; - var len{i} = {len}; if ({is_err}) {{ - ptr{i} = 0; len{i} = 0; + {ptr} = 0; {len} = 0; throw {take_object}({err}); }} - ", - take_object = take_object, - is_err = is_err, - err = err, - i = i, - ptr = ptr, - len = len, + " )); - js.push(format!("ptr{}", i)); - js.push(format!("len{}", i)); + js.push(ptr); + js.push(len); } Instruction::OptionString { @@ -1126,42 +1150,33 @@ fn instruction( } => { let func = js.cx.expose_pass_string_to_wasm(*mem)?; js.cx.expose_is_like_none(); - let i = js.tmp(); let malloc = js.cx.export_name_of(*malloc); let val = js.pop(); let realloc = match realloc { Some(f) => format!(", wasm.{}", js.cx.export_name_of(*f)), None => String::new(), }; - js.prelude(&format!( - "var ptr{i} = isLikeNone({0}) ? 0 : {f}({0}, wasm.{malloc}{realloc});", - val, - i = i, - f = func, - malloc = malloc, - realloc = realloc, - )); - js.prelude(&format!("var len{} = WASM_VECTOR_LEN;", i)); - js.push(format!("ptr{}", i)); - js.push(format!("len{}", i)); + let ptr = js.alias( + "ptr", + format!("isLikeNone({val}) ? 0 : {func}({val}, wasm.{malloc}{realloc})"), + ); + let len = js.alias("len", "WASM_VECTOR_LEN".to_string()); + js.push(ptr); + js.push(len); } Instruction::OptionVector { kind, mem, malloc } => { let func = js.cx.pass_to_wasm_function(kind.clone(), *mem)?; js.cx.expose_is_like_none(); - let i = js.tmp(); let malloc = js.cx.export_name_of(*malloc); let val = js.pop(); - js.prelude(&format!( - "var ptr{i} = isLikeNone({0}) ? 0 : {f}({0}, wasm.{malloc});", - val, - i = i, - f = func, - malloc = malloc, - )); - js.prelude(&format!("var len{} = WASM_VECTOR_LEN;", i)); - js.push(format!("ptr{}", i)); - js.push(format!("len{}", i)); + let ptr = js.alias( + "ptr", + format!("isLikeNone({val}) ? 0 : {func}({val}, wasm.{malloc})"), + ); + let len = js.alias("len", "WASM_VECTOR_LEN".to_string()); + js.push(ptr); + js.push(len); } Instruction::MutableSliceToMemory { kind, malloc, mem } => { @@ -1169,18 +1184,11 @@ fn instruction( let val = js.pop(); let func = js.cx.pass_to_wasm_function(kind.clone(), *mem)?; let malloc = js.cx.export_name_of(*malloc); - let i = js.tmp(); - js.prelude(&format!( - "var ptr{i} = {f}({val}, wasm.{malloc});", - val = val, - i = i, - f = func, - malloc = malloc, - )); - js.prelude(&format!("var len{} = WASM_VECTOR_LEN;", i)); + let ptr = js.alias("ptr", format!("{func}({val}, wasm.{malloc})")); + let len = js.alias("len", "WASM_VECTOR_LEN".to_string()); // Then pass it the pointer and the length of where we copied it. - js.push(format!("ptr{}", i)); - js.push(format!("len{}", i)); + js.push(ptr); + js.push(len); // Then we give Wasm a reference to the original typed array, so that it can // update it with modifications made on the Wasm side before returning. js.push(val); @@ -1247,11 +1255,10 @@ fn instruction( } => { let len = js.pop(); let ptr = js.pop(); - let tmp = js.tmp(); let get = js.cx.expose_get_cached_string_from_wasm(*mem, *table)?; - js.prelude(&format!("var v{} = {}({}, {});", tmp, get, ptr, len)); + let val = js.alias("v", format!("{get}({ptr}, {len})")); if *owned { let free = js.cx.export_name_of(*free); @@ -1263,7 +1270,7 @@ fn instruction( )); } - js.push(format!("v{}", tmp)); + js.push(val); } Instruction::TableGet => { @@ -1277,85 +1284,71 @@ fn instruction( nargs, mutable, } => { - let i = js.tmp(); let b = js.pop(); let a = js.pop(); - js.prelude(&format!("var state{} = {{a: {}, b: {}}};", i, a, b)); + let state = js.var("state", format!("{{a: {}, b: {}}}", a, b)); let args = (0..*nargs) .map(|i| format!("arg{}", i)) .collect::>() .join(", "); let wrapper = js.cx.adapter_name(*adapter); - if *mutable { - // Mutable closures need protection against being called - // recursively, so ensure that we clear out one of the - // internal pointers while it's being invoked. - js.prelude(&format!( - "var cb{i} = ({args}) => {{ - const a = state{i}.a; - state{i}.a = 0; - try {{ - return {name}(a, state{i}.b, {args}); - }} finally {{ - state{i}.a = a; - }} - }};", - i = i, - args = args, - name = wrapper, - )); - } else { - js.prelude(&format!( - "var cb{i} = ({args}) => {wrapper}(state{i}.a, state{i}.b, {args});", - i = i, - args = args, - wrapper = wrapper, - )); - } + let cb = js.var( + "cb", + if *mutable { + // Mutable closures need protection against being called + // recursively, so ensure that we clear out one of the + // internal pointers while it's being invoked. + format!( + "({args}) => {{ + const a = {state}.a; + {state}.a = 0; + try {{ + return {wrapper}(a, {state}.b, {args}); + }} finally {{ + {state}.a = a; + }} + }}" + ) + } else { + format!("({args}) => {wrapper}({state}.a, {state}.b, {args})") + }, + ); // Make sure to null out our internal pointers when we return // back to Rust to ensure that any lingering references to the // closure will fail immediately due to null pointers passed in // to Rust. - js.finally(&format!("state{}.a = state{0}.b = 0;", i)); - js.push(format!("cb{}", i)); + js.finally(&format!("{state}.a = {state}.b = 0;")); + js.push(cb); } Instruction::VectorLoad { kind, mem, free } => { let len = js.pop(); let ptr = js.pop(); let f = js.cx.expose_get_vector_from_wasm(kind.clone(), *mem)?; - let i = js.tmp(); let free = js.cx.export_name_of(*free); - js.prelude(&format!("var v{} = {}({}, {}).slice();", i, f, ptr, len)); + let val = js.alias("v", format!("{f}({ptr}, {len}).slice()")); js.prelude(&format!( - "wasm.{}({}, {} * {size}, {size});", - free, - ptr, - len, + "wasm.{free}({ptr}, {len} * {size}, {size});", size = kind.size() )); - js.push(format!("v{}", i)) + js.push(val); } Instruction::OptionVectorLoad { kind, mem, free } => { let len = js.pop(); let ptr = js.pop(); let f = js.cx.expose_get_vector_from_wasm(kind.clone(), *mem)?; - let i = js.tmp(); let free = js.cx.export_name_of(*free); - js.prelude(&format!("let v{};", i)); + let val = js.var("v", "undefined".to_string()); js.prelude(&format!("if ({} !== 0) {{", ptr)); - js.prelude(&format!("v{} = {}({}, {}).slice();", i, f, ptr, len)); + js.prelude(&format!("{val} = {f}({ptr}, {len}).slice();")); js.prelude(&format!( - "wasm.{}({}, {} * {size}, {size});", - free, - ptr, - len, + "wasm.{free}({ptr}, {len} * {size}, {size});", size = kind.size() )); js.prelude("}"); - js.push(format!("v{}", i)); + js.push(val); } Instruction::View { kind, mem } => { diff --git a/crates/cli/tests/reference/getter-setter.js b/crates/cli/tests/reference/getter-setter.js index 6c05b3065a9..1ff51f4b151 100644 --- a/crates/cli/tests/reference/getter-setter.js +++ b/crates/cli/tests/reference/getter-setter.js @@ -170,9 +170,9 @@ export class Foo { * @param {string | undefined} [value] */ set weird(value) { - var ptr0 = isLikeNone(value) ? 0 : passStringToWasm0(value, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); - var len0 = WASM_VECTOR_LEN; - wasm.foo_set_weird(this.__wbg_ptr, ptr0, len0); + const ptr = isLikeNone(value) ? 0 : passStringToWasm0(value, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); + const len = WASM_VECTOR_LEN; + wasm.foo_set_weird(this.__wbg_ptr, ptr, len); } /** * There can be static getters and setters too, and they can even have the diff --git a/crates/cli/tests/reference/result.js b/crates/cli/tests/reference/result.js index 62b1b902c59..abf083d949b 100644 --- a/crates/cli/tests/reference/result.js +++ b/crates/cli/tests/reference/result.js @@ -56,21 +56,19 @@ function takeObject(idx) { * @returns {string} */ export function result_string() { - let deferred2_0; - let deferred2_1; try { const ret = wasm.result_string(); - var ptr1 = ret[0]; - var len1 = ret[1]; + var ptr = ret[0]; + var len = ret[1]; if (ret[3]) { - ptr1 = 0; len1 = 0; + ptr = 0; len = 0; throw takeObject(ret[2]); } - deferred2_0 = ptr1; - deferred2_1 = len1; - return getStringFromWasm0(ptr1, len1); + var deferred0 = ptr; + var deferred1 = len; + return getStringFromWasm0(ptr, len); } finally { - wasm.__wbindgen_free(deferred2_0, deferred2_1, 1); + wasm.__wbindgen_free(deferred0, deferred1, 1); } } diff --git a/crates/cli/tests/reference/string-arg.d.ts b/crates/cli/tests/reference/string-arg.d.ts index a03bca0b63b..eb7998ba743 100644 --- a/crates/cli/tests/reference/string-arg.d.ts +++ b/crates/cli/tests/reference/string-arg.d.ts @@ -1,3 +1,4 @@ /* tslint:disable */ /* eslint-disable */ export function foo(a: string): void; +export function bar(a: string, b: string, ptr: number, len: number): string; diff --git a/crates/cli/tests/reference/string-arg.js b/crates/cli/tests/reference/string-arg.js index cc037461669..b79c4352d19 100644 --- a/crates/cli/tests/reference/string-arg.js +++ b/crates/cli/tests/reference/string-arg.js @@ -85,9 +85,31 @@ function passStringToWasm0(arg, malloc, realloc) { * @param {string} a */ export function foo(a) { - const ptr0 = passStringToWasm0(a, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); - const len0 = WASM_VECTOR_LEN; - wasm.foo(ptr0, len0); + const ptr = passStringToWasm0(a, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); + const len = WASM_VECTOR_LEN; + wasm.foo(ptr, len); +} + +/** + * @param {string} a + * @param {string} b + * @param {number} ptr + * @param {number} len + * @returns {string} + */ +export function bar(a, b, ptr, len) { + try { + const ptr1 = passStringToWasm0(a, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); + const len2 = WASM_VECTOR_LEN; + const ptr3 = passStringToWasm0(b, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); + const len4 = WASM_VECTOR_LEN; + const ret = wasm.bar(ptr1, len2, ptr3, len4, ptr, len); + var deferred0 = ret[0]; + var deferred1 = ret[1]; + return getStringFromWasm0(ret[0], ret[1]); + } finally { + wasm.__wbindgen_free(deferred0, deferred1, 1); + } } export function __wbindgen_throw(arg0, arg1) { diff --git a/crates/cli/tests/reference/string-arg.rs b/crates/cli/tests/reference/string-arg.rs index 64b4b417d06..e2b1bafbc16 100644 --- a/crates/cli/tests/reference/string-arg.rs +++ b/crates/cli/tests/reference/string-arg.rs @@ -4,3 +4,8 @@ use wasm_bindgen::prelude::*; pub fn foo(a: &str) { drop(a); } + +#[wasm_bindgen] +pub fn bar(a: &str, b: String, ptr: u32, len: u32) -> String { + b +} diff --git a/crates/cli/tests/reference/string-arg.wat b/crates/cli/tests/reference/string-arg.wat index fae75cfbcba..afb5f64b24b 100644 --- a/crates/cli/tests/reference/string-arg.wat +++ b/crates/cli/tests/reference/string-arg.wat @@ -1,15 +1,21 @@ (module $reference_test.wasm (type (;0;) (func (param i32 i32))) (type (;1;) (func (param i32 i32) (result i32))) - (type (;2;) (func (param i32 i32 i32 i32) (result i32))) - (func $__wbindgen_realloc (;0;) (type 2) (param i32 i32 i32 i32) (result i32)) + (type (;2;) (func (param i32 i32 i32))) + (type (;3;) (func (param i32 i32 i32 i32) (result i32))) + (type (;4;) (func (param i32 i32 i32 i32 i32 i32) (result i32 i32))) + (func $__wbindgen_realloc (;0;) (type 3) (param i32 i32 i32 i32) (result i32)) (func $__wbindgen_malloc (;1;) (type 1) (param i32 i32) (result i32)) (func $foo (;2;) (type 0) (param i32 i32)) + (func $__wbindgen_free (;3;) (type 2) (param i32 i32 i32)) + (func $"bar multivalue shim" (;4;) (type 4) (param i32 i32 i32 i32 i32 i32) (result i32 i32)) (memory (;0;) 17) (export "memory" (memory 0)) (export "foo" (func $foo)) + (export "bar" (func $"bar multivalue shim")) (export "__wbindgen_malloc" (func $__wbindgen_malloc)) (export "__wbindgen_realloc" (func $__wbindgen_realloc)) + (export "__wbindgen_free" (func $__wbindgen_free)) (@custom "target_features" (after code) "\04+\0amultivalue+\0fmutable-globals+\0freference-types+\08sign-ext") ) diff --git a/crates/cli/tests/reference/wasm-export-types.js b/crates/cli/tests/reference/wasm-export-types.js index de290e90502..013bfda2455 100644 --- a/crates/cli/tests/reference/wasm-export-types.js +++ b/crates/cli/tests/reference/wasm-export-types.js @@ -81,17 +81,15 @@ function passStringToWasm0(arg, malloc, realloc) { * @returns {string} */ export function example(a, b, c, d) { - let deferred2_0; - let deferred2_1; try { - const ptr0 = passStringToWasm0(d, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); - const len0 = WASM_VECTOR_LEN; - const ret = wasm.example(a, b, c, ptr0, len0); - deferred2_0 = ret[0]; - deferred2_1 = ret[1]; + const ptr = passStringToWasm0(d, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); + const len = WASM_VECTOR_LEN; + const ret = wasm.example(a, b, c, ptr, len); + var deferred0 = ret[0]; + var deferred1 = ret[1]; return getStringFromWasm0(ret[0], ret[1]); } finally { - wasm.__wbindgen_free(deferred2_0, deferred2_1, 1); + wasm.__wbindgen_free(deferred0, deferred1, 1); } } diff --git a/crates/cli/tests/reference/web-sys.js b/crates/cli/tests/reference/web-sys.js index 8332315d32d..9a46eba35ff 100644 --- a/crates/cli/tests/reference/web-sys.js +++ b/crates/cli/tests/reference/web-sys.js @@ -217,10 +217,10 @@ export function __wbg_new_561a91ce53f10a66() { return handleError(function (arg0 export function __wbindgen_debug_string(arg0, arg1) { const ret = debugString(getObject(arg1)); - const ptr1 = passStringToWasm0(ret, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); - const len1 = WASM_VECTOR_LEN; - getDataViewMemory0().setInt32(arg0 + 4 * 1, len1, true); - getDataViewMemory0().setInt32(arg0 + 4 * 0, ptr1, true); + const ptr = passStringToWasm0(ret, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); + const len = WASM_VECTOR_LEN; + getDataViewMemory0().setInt32(arg0 + 4 * 1, len, true); + getDataViewMemory0().setInt32(arg0 + 4 * 0, ptr, true); }; export function __wbindgen_object_drop_ref(arg0) { From f8c95021ebb0af962b8589acc28516f311dc2d71 Mon Sep 17 00:00:00 2001 From: Michael Schmidt Date: Tue, 19 Nov 2024 16:45:15 +0100 Subject: [PATCH 02/11] Comment --- crates/cli-support/src/js/binding.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/crates/cli-support/src/js/binding.rs b/crates/cli-support/src/js/binding.rs index 7213adc700b..b5626ef089a 100644 --- a/crates/cli-support/src/js/binding.rs +++ b/crates/cli-support/src/js/binding.rs @@ -482,7 +482,8 @@ impl<'a, 'b> JsBuilder<'a, 'b> { ident } - /// Creates a mutable JS `var`iable with the given name and returns the name. + /// Creates a mutable JS `var`iable with the given name and returns the + /// name. The create variable is also visible in the `finally` block. /// /// If the name is already taken, a different name is generated. pub fn var(&mut self, name: &str, initial_value: String) -> String { From 58273218e82399e90e4f5c06afa140185159fac6 Mon Sep 17 00:00:00 2001 From: RunDevelopment Date: Fri, 29 Nov 2024 19:10:04 +0100 Subject: [PATCH 03/11] Updated refs --- crates/cli/tests/reference/echo.js | 284 +++++++++++----------- crates/cli/tests/reference/result.js | 2 +- crates/cli/tests/reference/string-arg.wat | 25 +- crates/cli/tests/reference/web-sys.js | 2 +- 4 files changed, 158 insertions(+), 155 deletions(-) diff --git a/crates/cli/tests/reference/echo.js b/crates/cli/tests/reference/echo.js index f2eb7da3e3b..afbbfeedd3d 100644 --- a/crates/cli/tests/reference/echo.js +++ b/crates/cli/tests/reference/echo.js @@ -302,9 +302,9 @@ function _assertChar(c) { * @returns {string} */ export function echo_char(a) { - const char0 = a.codePointAt(0); - _assertChar(char0); - const ret = wasm.echo_char(char0); + const char = a.codePointAt(0); + _assertChar(char); + const ret = wasm.echo_char(char); return String.fromCodePoint(ret); } @@ -313,17 +313,15 @@ export function echo_char(a) { * @returns {string} */ export function echo_string(a) { - let deferred2_0; - let deferred2_1; try { - const ptr0 = passStringToWasm0(a, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); - const len0 = WASM_VECTOR_LEN; - const ret = wasm.echo_string(ptr0, len0); - deferred2_0 = ret[0]; - deferred2_1 = ret[1]; + const ptr = passStringToWasm0(a, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); + const len = WASM_VECTOR_LEN; + const ret = wasm.echo_string(ptr, len); + var deferred0 = ret[0]; + var deferred1 = ret[1]; return getStringFromWasm0(ret[0], ret[1]); } finally { - wasm.__wbindgen_free(deferred2_0, deferred2_1, 1); + wasm.__wbindgen_free(deferred0, deferred1, 1); } } @@ -343,12 +341,12 @@ function getArrayU8FromWasm0(ptr, len) { * @returns {Uint8Array} */ export function echo_vec_u8(a) { - const ptr0 = passArray8ToWasm0(a, wasm.__wbindgen_malloc); - const len0 = WASM_VECTOR_LEN; - const ret = wasm.echo_vec_u8(ptr0, len0); - var v2 = getArrayU8FromWasm0(ret[0], ret[1]).slice(); + const ptr = passArray8ToWasm0(a, wasm.__wbindgen_malloc); + const len = WASM_VECTOR_LEN; + const ret = wasm.echo_vec_u8(ptr, len); + const v = getArrayU8FromWasm0(ret[0], ret[1]).slice(); wasm.__wbindgen_free(ret[0], ret[1] * 1, 1); - return v2; + return v; } let cachedInt8ArrayMemory0 = null; @@ -369,12 +367,12 @@ function getArrayI8FromWasm0(ptr, len) { * @returns {Int8Array} */ export function echo_vec_i8(a) { - const ptr0 = passArray8ToWasm0(a, wasm.__wbindgen_malloc); - const len0 = WASM_VECTOR_LEN; - const ret = wasm.echo_vec_i8(ptr0, len0); - var v2 = getArrayI8FromWasm0(ret[0], ret[1]).slice(); + const ptr = passArray8ToWasm0(a, wasm.__wbindgen_malloc); + const len = WASM_VECTOR_LEN; + const ret = wasm.echo_vec_i8(ptr, len); + const v = getArrayI8FromWasm0(ret[0], ret[1]).slice(); wasm.__wbindgen_free(ret[0], ret[1] * 1, 1); - return v2; + return v; } let cachedUint16ArrayMemory0 = null; @@ -402,12 +400,12 @@ function getArrayU16FromWasm0(ptr, len) { * @returns {Uint16Array} */ export function echo_vec_u16(a) { - const ptr0 = passArray16ToWasm0(a, wasm.__wbindgen_malloc); - const len0 = WASM_VECTOR_LEN; - const ret = wasm.echo_vec_u16(ptr0, len0); - var v2 = getArrayU16FromWasm0(ret[0], ret[1]).slice(); + const ptr = passArray16ToWasm0(a, wasm.__wbindgen_malloc); + const len = WASM_VECTOR_LEN; + const ret = wasm.echo_vec_u16(ptr, len); + const v = getArrayU16FromWasm0(ret[0], ret[1]).slice(); wasm.__wbindgen_free(ret[0], ret[1] * 2, 2); - return v2; + return v; } let cachedInt16ArrayMemory0 = null; @@ -428,12 +426,12 @@ function getArrayI16FromWasm0(ptr, len) { * @returns {Int16Array} */ export function echo_vec_i16(a) { - const ptr0 = passArray16ToWasm0(a, wasm.__wbindgen_malloc); - const len0 = WASM_VECTOR_LEN; - const ret = wasm.echo_vec_i16(ptr0, len0); - var v2 = getArrayI16FromWasm0(ret[0], ret[1]).slice(); + const ptr = passArray16ToWasm0(a, wasm.__wbindgen_malloc); + const len = WASM_VECTOR_LEN; + const ret = wasm.echo_vec_i16(ptr, len); + const v = getArrayI16FromWasm0(ret[0], ret[1]).slice(); wasm.__wbindgen_free(ret[0], ret[1] * 2, 2); - return v2; + return v; } let cachedUint32ArrayMemory0 = null; @@ -461,12 +459,12 @@ function getArrayU32FromWasm0(ptr, len) { * @returns {Uint32Array} */ export function echo_vec_u32(a) { - const ptr0 = passArray32ToWasm0(a, wasm.__wbindgen_malloc); - const len0 = WASM_VECTOR_LEN; - const ret = wasm.echo_vec_u32(ptr0, len0); - var v2 = getArrayU32FromWasm0(ret[0], ret[1]).slice(); + const ptr = passArray32ToWasm0(a, wasm.__wbindgen_malloc); + const len = WASM_VECTOR_LEN; + const ret = wasm.echo_vec_u32(ptr, len); + const v = getArrayU32FromWasm0(ret[0], ret[1]).slice(); wasm.__wbindgen_free(ret[0], ret[1] * 4, 4); - return v2; + return v; } let cachedInt32ArrayMemory0 = null; @@ -487,12 +485,12 @@ function getArrayI32FromWasm0(ptr, len) { * @returns {Int32Array} */ export function echo_vec_i32(a) { - const ptr0 = passArray32ToWasm0(a, wasm.__wbindgen_malloc); - const len0 = WASM_VECTOR_LEN; - const ret = wasm.echo_vec_i32(ptr0, len0); - var v2 = getArrayI32FromWasm0(ret[0], ret[1]).slice(); + const ptr = passArray32ToWasm0(a, wasm.__wbindgen_malloc); + const len = WASM_VECTOR_LEN; + const ret = wasm.echo_vec_i32(ptr, len); + const v = getArrayI32FromWasm0(ret[0], ret[1]).slice(); wasm.__wbindgen_free(ret[0], ret[1] * 4, 4); - return v2; + return v; } let cachedBigUint64ArrayMemory0 = null; @@ -520,12 +518,12 @@ function getArrayU64FromWasm0(ptr, len) { * @returns {BigUint64Array} */ export function echo_vec_u64(a) { - const ptr0 = passArray64ToWasm0(a, wasm.__wbindgen_malloc); - const len0 = WASM_VECTOR_LEN; - const ret = wasm.echo_vec_u64(ptr0, len0); - var v2 = getArrayU64FromWasm0(ret[0], ret[1]).slice(); + const ptr = passArray64ToWasm0(a, wasm.__wbindgen_malloc); + const len = WASM_VECTOR_LEN; + const ret = wasm.echo_vec_u64(ptr, len); + const v = getArrayU64FromWasm0(ret[0], ret[1]).slice(); wasm.__wbindgen_free(ret[0], ret[1] * 8, 8); - return v2; + return v; } let cachedBigInt64ArrayMemory0 = null; @@ -546,12 +544,12 @@ function getArrayI64FromWasm0(ptr, len) { * @returns {BigInt64Array} */ export function echo_vec_i64(a) { - const ptr0 = passArray64ToWasm0(a, wasm.__wbindgen_malloc); - const len0 = WASM_VECTOR_LEN; - const ret = wasm.echo_vec_i64(ptr0, len0); - var v2 = getArrayI64FromWasm0(ret[0], ret[1]).slice(); + const ptr = passArray64ToWasm0(a, wasm.__wbindgen_malloc); + const len = WASM_VECTOR_LEN; + const ret = wasm.echo_vec_i64(ptr, len); + const v = getArrayI64FromWasm0(ret[0], ret[1]).slice(); wasm.__wbindgen_free(ret[0], ret[1] * 8, 8); - return v2; + return v; } function addToExternrefTable0(obj) { @@ -585,12 +583,12 @@ function getArrayJsValueFromWasm0(ptr, len) { * @returns {(string)[]} */ export function echo_vec_string(a) { - const ptr0 = passArrayJsValueToWasm0(a, wasm.__wbindgen_malloc); - const len0 = WASM_VECTOR_LEN; - const ret = wasm.echo_vec_string(ptr0, len0); - var v2 = getArrayJsValueFromWasm0(ret[0], ret[1]).slice(); + const ptr = passArrayJsValueToWasm0(a, wasm.__wbindgen_malloc); + const len = WASM_VECTOR_LEN; + const ret = wasm.echo_vec_string(ptr, len); + const v = getArrayJsValueFromWasm0(ret[0], ret[1]).slice(); wasm.__wbindgen_free(ret[0], ret[1] * 4, 4); - return v2; + return v; } function _assertClass(instance, klass) { @@ -604,8 +602,8 @@ function _assertClass(instance, klass) { */ export function echo_struct(a) { _assertClass(a, Foo); - var ptr0 = a.__destroy_into_raw(); - const ret = wasm.echo_struct(ptr0); + const ptr = a.__destroy_into_raw(); + const ret = wasm.echo_struct(ptr); return Foo.__wrap(ret); } @@ -614,12 +612,12 @@ export function echo_struct(a) { * @returns {(Foo)[]} */ export function echo_vec_struct(a) { - const ptr0 = passArrayJsValueToWasm0(a, wasm.__wbindgen_malloc); - const len0 = WASM_VECTOR_LEN; - const ret = wasm.echo_vec_struct(ptr0, len0); - var v2 = getArrayJsValueFromWasm0(ret[0], ret[1]).slice(); + const ptr = passArrayJsValueToWasm0(a, wasm.__wbindgen_malloc); + const len = WASM_VECTOR_LEN; + const ret = wasm.echo_vec_struct(ptr, len); + const v = getArrayJsValueFromWasm0(ret[0], ret[1]).slice(); wasm.__wbindgen_free(ret[0], ret[1] * 4, 4); - return v2; + return v; } /** @@ -762,9 +760,9 @@ export function echo_option_bool(a) { * @returns {string | undefined} */ export function echo_option_char(a) { - const char0 = isLikeNone(a) ? 0xFFFFFF : a.codePointAt(0); - if (char0 !== 0xFFFFFF) { _assertChar(char0); } - const ret = wasm.echo_option_char(char0); + const char = isLikeNone(a) ? 0xFFFFFF : a.codePointAt(0); + if (char !== 0xFFFFFF) { _assertChar(char); } + const ret = wasm.echo_option_char(char); return ret === 0xFFFFFF ? undefined : String.fromCodePoint(ret); } @@ -773,15 +771,15 @@ export function echo_option_char(a) { * @returns {string | undefined} */ export function echo_option_string(a) { - var ptr0 = isLikeNone(a) ? 0 : passStringToWasm0(a, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); - var len0 = WASM_VECTOR_LEN; - const ret = wasm.echo_option_string(ptr0, len0); - let v2; + const ptr = isLikeNone(a) ? 0 : passStringToWasm0(a, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); + const len = WASM_VECTOR_LEN; + const ret = wasm.echo_option_string(ptr, len); + var v = undefined; if (ret[0] !== 0) { - v2 = getStringFromWasm0(ret[0], ret[1]).slice(); + v = getStringFromWasm0(ret[0], ret[1]).slice(); wasm.__wbindgen_free(ret[0], ret[1] * 1, 1); } - return v2; + return v; } /** @@ -789,15 +787,15 @@ export function echo_option_string(a) { * @returns {Uint8Array | undefined} */ export function echo_option_vec_u8(a) { - var ptr0 = isLikeNone(a) ? 0 : passArray8ToWasm0(a, wasm.__wbindgen_malloc); - var len0 = WASM_VECTOR_LEN; - const ret = wasm.echo_option_vec_u8(ptr0, len0); - let v2; + const ptr = isLikeNone(a) ? 0 : passArray8ToWasm0(a, wasm.__wbindgen_malloc); + const len = WASM_VECTOR_LEN; + const ret = wasm.echo_option_vec_u8(ptr, len); + var v = undefined; if (ret[0] !== 0) { - v2 = getArrayU8FromWasm0(ret[0], ret[1]).slice(); + v = getArrayU8FromWasm0(ret[0], ret[1]).slice(); wasm.__wbindgen_free(ret[0], ret[1] * 1, 1); } - return v2; + return v; } /** @@ -805,15 +803,15 @@ export function echo_option_vec_u8(a) { * @returns {Int8Array | undefined} */ export function echo_option_vec_i8(a) { - var ptr0 = isLikeNone(a) ? 0 : passArray8ToWasm0(a, wasm.__wbindgen_malloc); - var len0 = WASM_VECTOR_LEN; - const ret = wasm.echo_option_vec_i8(ptr0, len0); - let v2; + const ptr = isLikeNone(a) ? 0 : passArray8ToWasm0(a, wasm.__wbindgen_malloc); + const len = WASM_VECTOR_LEN; + const ret = wasm.echo_option_vec_i8(ptr, len); + var v = undefined; if (ret[0] !== 0) { - v2 = getArrayI8FromWasm0(ret[0], ret[1]).slice(); + v = getArrayI8FromWasm0(ret[0], ret[1]).slice(); wasm.__wbindgen_free(ret[0], ret[1] * 1, 1); } - return v2; + return v; } /** @@ -821,15 +819,15 @@ export function echo_option_vec_i8(a) { * @returns {Uint16Array | undefined} */ export function echo_option_vec_u16(a) { - var ptr0 = isLikeNone(a) ? 0 : passArray16ToWasm0(a, wasm.__wbindgen_malloc); - var len0 = WASM_VECTOR_LEN; - const ret = wasm.echo_option_vec_u16(ptr0, len0); - let v2; + const ptr = isLikeNone(a) ? 0 : passArray16ToWasm0(a, wasm.__wbindgen_malloc); + const len = WASM_VECTOR_LEN; + const ret = wasm.echo_option_vec_u16(ptr, len); + var v = undefined; if (ret[0] !== 0) { - v2 = getArrayU16FromWasm0(ret[0], ret[1]).slice(); + v = getArrayU16FromWasm0(ret[0], ret[1]).slice(); wasm.__wbindgen_free(ret[0], ret[1] * 2, 2); } - return v2; + return v; } /** @@ -837,15 +835,15 @@ export function echo_option_vec_u16(a) { * @returns {Int16Array | undefined} */ export function echo_option_vec_i16(a) { - var ptr0 = isLikeNone(a) ? 0 : passArray16ToWasm0(a, wasm.__wbindgen_malloc); - var len0 = WASM_VECTOR_LEN; - const ret = wasm.echo_option_vec_i16(ptr0, len0); - let v2; + const ptr = isLikeNone(a) ? 0 : passArray16ToWasm0(a, wasm.__wbindgen_malloc); + const len = WASM_VECTOR_LEN; + const ret = wasm.echo_option_vec_i16(ptr, len); + var v = undefined; if (ret[0] !== 0) { - v2 = getArrayI16FromWasm0(ret[0], ret[1]).slice(); + v = getArrayI16FromWasm0(ret[0], ret[1]).slice(); wasm.__wbindgen_free(ret[0], ret[1] * 2, 2); } - return v2; + return v; } /** @@ -853,15 +851,15 @@ export function echo_option_vec_i16(a) { * @returns {Uint32Array | undefined} */ export function echo_option_vec_u32(a) { - var ptr0 = isLikeNone(a) ? 0 : passArray32ToWasm0(a, wasm.__wbindgen_malloc); - var len0 = WASM_VECTOR_LEN; - const ret = wasm.echo_option_vec_u32(ptr0, len0); - let v2; + const ptr = isLikeNone(a) ? 0 : passArray32ToWasm0(a, wasm.__wbindgen_malloc); + const len = WASM_VECTOR_LEN; + const ret = wasm.echo_option_vec_u32(ptr, len); + var v = undefined; if (ret[0] !== 0) { - v2 = getArrayU32FromWasm0(ret[0], ret[1]).slice(); + v = getArrayU32FromWasm0(ret[0], ret[1]).slice(); wasm.__wbindgen_free(ret[0], ret[1] * 4, 4); } - return v2; + return v; } /** @@ -869,15 +867,15 @@ export function echo_option_vec_u32(a) { * @returns {Int32Array | undefined} */ export function echo_option_vec_i32(a) { - var ptr0 = isLikeNone(a) ? 0 : passArray32ToWasm0(a, wasm.__wbindgen_malloc); - var len0 = WASM_VECTOR_LEN; - const ret = wasm.echo_option_vec_i32(ptr0, len0); - let v2; + const ptr = isLikeNone(a) ? 0 : passArray32ToWasm0(a, wasm.__wbindgen_malloc); + const len = WASM_VECTOR_LEN; + const ret = wasm.echo_option_vec_i32(ptr, len); + var v = undefined; if (ret[0] !== 0) { - v2 = getArrayI32FromWasm0(ret[0], ret[1]).slice(); + v = getArrayI32FromWasm0(ret[0], ret[1]).slice(); wasm.__wbindgen_free(ret[0], ret[1] * 4, 4); } - return v2; + return v; } /** @@ -885,15 +883,15 @@ export function echo_option_vec_i32(a) { * @returns {BigUint64Array | undefined} */ export function echo_option_vec_u64(a) { - var ptr0 = isLikeNone(a) ? 0 : passArray64ToWasm0(a, wasm.__wbindgen_malloc); - var len0 = WASM_VECTOR_LEN; - const ret = wasm.echo_option_vec_u64(ptr0, len0); - let v2; + const ptr = isLikeNone(a) ? 0 : passArray64ToWasm0(a, wasm.__wbindgen_malloc); + const len = WASM_VECTOR_LEN; + const ret = wasm.echo_option_vec_u64(ptr, len); + var v = undefined; if (ret[0] !== 0) { - v2 = getArrayU64FromWasm0(ret[0], ret[1]).slice(); + v = getArrayU64FromWasm0(ret[0], ret[1]).slice(); wasm.__wbindgen_free(ret[0], ret[1] * 8, 8); } - return v2; + return v; } /** @@ -901,15 +899,15 @@ export function echo_option_vec_u64(a) { * @returns {BigInt64Array | undefined} */ export function echo_option_vec_i64(a) { - var ptr0 = isLikeNone(a) ? 0 : passArray64ToWasm0(a, wasm.__wbindgen_malloc); - var len0 = WASM_VECTOR_LEN; - const ret = wasm.echo_option_vec_i64(ptr0, len0); - let v2; + const ptr = isLikeNone(a) ? 0 : passArray64ToWasm0(a, wasm.__wbindgen_malloc); + const len = WASM_VECTOR_LEN; + const ret = wasm.echo_option_vec_i64(ptr, len); + var v = undefined; if (ret[0] !== 0) { - v2 = getArrayI64FromWasm0(ret[0], ret[1]).slice(); + v = getArrayI64FromWasm0(ret[0], ret[1]).slice(); wasm.__wbindgen_free(ret[0], ret[1] * 8, 8); } - return v2; + return v; } /** @@ -917,15 +915,15 @@ export function echo_option_vec_i64(a) { * @returns {(string)[] | undefined} */ export function echo_option_vec_string(a) { - var ptr0 = isLikeNone(a) ? 0 : passArrayJsValueToWasm0(a, wasm.__wbindgen_malloc); - var len0 = WASM_VECTOR_LEN; - const ret = wasm.echo_option_vec_string(ptr0, len0); - let v2; + const ptr = isLikeNone(a) ? 0 : passArrayJsValueToWasm0(a, wasm.__wbindgen_malloc); + const len = WASM_VECTOR_LEN; + const ret = wasm.echo_option_vec_string(ptr, len); + var v = undefined; if (ret[0] !== 0) { - v2 = getArrayJsValueFromWasm0(ret[0], ret[1]).slice(); + v = getArrayJsValueFromWasm0(ret[0], ret[1]).slice(); wasm.__wbindgen_free(ret[0], ret[1] * 4, 4); } - return v2; + return v; } /** @@ -933,12 +931,12 @@ export function echo_option_vec_string(a) { * @returns {Foo | undefined} */ export function echo_option_struct(a) { - let ptr0 = 0; + var ptr = 0; if (!isLikeNone(a)) { _assertClass(a, Foo); - ptr0 = a.__destroy_into_raw(); + ptr = a.__destroy_into_raw(); } - const ret = wasm.echo_option_struct(ptr0); + const ret = wasm.echo_option_struct(ptr); return ret === 0 ? undefined : Foo.__wrap(ret); } @@ -947,15 +945,15 @@ export function echo_option_struct(a) { * @returns {(Foo)[] | undefined} */ export function echo_option_vec_struct(a) { - var ptr0 = isLikeNone(a) ? 0 : passArrayJsValueToWasm0(a, wasm.__wbindgen_malloc); - var len0 = WASM_VECTOR_LEN; - const ret = wasm.echo_option_vec_struct(ptr0, len0); - let v2; + const ptr = isLikeNone(a) ? 0 : passArrayJsValueToWasm0(a, wasm.__wbindgen_malloc); + const len = WASM_VECTOR_LEN; + const ret = wasm.echo_option_vec_struct(ptr, len); + var v = undefined; if (ret[0] !== 0) { - v2 = getArrayJsValueFromWasm0(ret[0], ret[1]).slice(); + v = getArrayJsValueFromWasm0(ret[0], ret[1]).slice(); wasm.__wbindgen_free(ret[0], ret[1] * 4, 4); } - return v2; + return v; } const FooFinalization = (typeof FinalizationRegistry === 'undefined') @@ -1004,10 +1002,10 @@ export function __wbg_foo_unwrap(arg0) { export function __wbindgen_debug_string(arg0, arg1) { const ret = debugString(arg1); - const ptr1 = passStringToWasm0(ret, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); - const len1 = WASM_VECTOR_LEN; - getDataViewMemory0().setInt32(arg0 + 4 * 1, len1, true); - getDataViewMemory0().setInt32(arg0 + 4 * 0, ptr1, true); + const ptr = passStringToWasm0(ret, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); + const len = WASM_VECTOR_LEN; + getDataViewMemory0().setInt32(arg0 + 4 * 1, len, true); + getDataViewMemory0().setInt32(arg0 + 4 * 0, ptr, true); }; export function __wbindgen_init_externref_table() { @@ -1024,10 +1022,10 @@ export function __wbindgen_init_externref_table() { export function __wbindgen_string_get(arg0, arg1) { const obj = arg1; const ret = typeof(obj) === 'string' ? obj : undefined; - var ptr1 = isLikeNone(ret) ? 0 : passStringToWasm0(ret, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); - var len1 = WASM_VECTOR_LEN; - getDataViewMemory0().setInt32(arg0 + 4 * 1, len1, true); - getDataViewMemory0().setInt32(arg0 + 4 * 0, ptr1, true); + const ptr = isLikeNone(ret) ? 0 : passStringToWasm0(ret, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); + const len = WASM_VECTOR_LEN; + getDataViewMemory0().setInt32(arg0 + 4 * 1, len, true); + getDataViewMemory0().setInt32(arg0 + 4 * 0, ptr, true); }; export function __wbindgen_string_new(arg0, arg1) { diff --git a/crates/cli/tests/reference/result.js b/crates/cli/tests/reference/result.js index c1da0f0d187..7861fade34f 100644 --- a/crates/cli/tests/reference/result.js +++ b/crates/cli/tests/reference/result.js @@ -39,7 +39,7 @@ export function result_string() { var len = ret[1]; if (ret[3]) { ptr = 0; len = 0; - throw takeObject(ret[2]); + throw takeFromExternrefTable0(ret[2]); } var deferred0 = ptr; var deferred1 = len; diff --git a/crates/cli/tests/reference/string-arg.wat b/crates/cli/tests/reference/string-arg.wat index afb5f64b24b..cc5327c7569 100644 --- a/crates/cli/tests/reference/string-arg.wat +++ b/crates/cli/tests/reference/string-arg.wat @@ -1,21 +1,26 @@ (module $reference_test.wasm - (type (;0;) (func (param i32 i32))) - (type (;1;) (func (param i32 i32) (result i32))) - (type (;2;) (func (param i32 i32 i32))) - (type (;3;) (func (param i32 i32 i32 i32) (result i32))) - (type (;4;) (func (param i32 i32 i32 i32 i32 i32) (result i32 i32))) - (func $__wbindgen_realloc (;0;) (type 3) (param i32 i32 i32 i32) (result i32)) - (func $__wbindgen_malloc (;1;) (type 1) (param i32 i32) (result i32)) - (func $foo (;2;) (type 0) (param i32 i32)) - (func $__wbindgen_free (;3;) (type 2) (param i32 i32 i32)) - (func $"bar multivalue shim" (;4;) (type 4) (param i32 i32 i32 i32 i32 i32) (result i32 i32)) + (type (;0;) (func)) + (type (;1;) (func (param i32 i32))) + (type (;2;) (func (param i32 i32) (result i32))) + (type (;3;) (func (param i32 i32 i32))) + (type (;4;) (func (param i32 i32 i32 i32) (result i32))) + (type (;5;) (func (param i32 i32 i32 i32 i32 i32) (result i32 i32))) + (import "./reference_test_bg.js" "__wbindgen_init_externref_table" (func (;0;) (type 0))) + (func $__wbindgen_realloc (;1;) (type 4) (param i32 i32 i32 i32) (result i32)) + (func $__wbindgen_malloc (;2;) (type 2) (param i32 i32) (result i32)) + (func $foo (;3;) (type 1) (param i32 i32)) + (func $__wbindgen_free (;4;) (type 3) (param i32 i32 i32)) + (func $"bar multivalue shim" (;5;) (type 5) (param i32 i32 i32 i32 i32 i32) (result i32 i32)) + (table (;0;) 128 externref) (memory (;0;) 17) (export "memory" (memory 0)) (export "foo" (func $foo)) (export "bar" (func $"bar multivalue shim")) + (export "__wbindgen_export_0" (table 0)) (export "__wbindgen_malloc" (func $__wbindgen_malloc)) (export "__wbindgen_realloc" (func $__wbindgen_realloc)) (export "__wbindgen_free" (func $__wbindgen_free)) + (export "__wbindgen_start" (func 0)) (@custom "target_features" (after code) "\04+\0amultivalue+\0fmutable-globals+\0freference-types+\08sign-ext") ) diff --git a/crates/cli/tests/reference/web-sys.js b/crates/cli/tests/reference/web-sys.js index 20f46c68043..16f2aed87ea 100644 --- a/crates/cli/tests/reference/web-sys.js +++ b/crates/cli/tests/reference/web-sys.js @@ -194,7 +194,7 @@ export function __wbg_new_dce808fb3f528314() { return handleError(function (arg0 }, arguments) }; export function __wbindgen_debug_string(arg0, arg1) { - const ret = debugString(getObject(arg1)); + const ret = debugString(arg1); const ptr = passStringToWasm0(ret, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); const len = WASM_VECTOR_LEN; getDataViewMemory0().setInt32(arg0 + 4 * 1, len, true); From 05a3945d7a7fdd891fd6b2c30de3419efd3f1841 Mon Sep 17 00:00:00 2001 From: RunDevelopment Date: Fri, 29 Nov 2024 19:19:45 +0100 Subject: [PATCH 04/11] Better string args test --- crates/cli/tests/reference/string-arg.d.ts | 3 ++- crates/cli/tests/reference/string-arg.js | 24 ++++++++++++++++++++-- crates/cli/tests/reference/string-arg.rs | 7 ++++++- crates/cli/tests/reference/string-arg.wat | 9 +++++--- 4 files changed, 36 insertions(+), 7 deletions(-) diff --git a/crates/cli/tests/reference/string-arg.d.ts b/crates/cli/tests/reference/string-arg.d.ts index eb7998ba743..4df139cb60c 100644 --- a/crates/cli/tests/reference/string-arg.d.ts +++ b/crates/cli/tests/reference/string-arg.d.ts @@ -1,4 +1,5 @@ /* tslint:disable */ /* eslint-disable */ export function foo(a: string): void; -export function bar(a: string, b: string, ptr: number, len: number): string; +export function two_strings(a: string, b: string): string; +export function two_strings_and_some_more(a: string, b: string, ptr: number, len: number): string; diff --git a/crates/cli/tests/reference/string-arg.js b/crates/cli/tests/reference/string-arg.js index 0b1cdaf3cd5..6fc22aabe3b 100644 --- a/crates/cli/tests/reference/string-arg.js +++ b/crates/cli/tests/reference/string-arg.js @@ -90,6 +90,26 @@ export function foo(a) { wasm.foo(ptr, len); } +/** + * @param {string} a + * @param {string} b + * @returns {string} + */ +export function two_strings(a, b) { + try { + const ptr = passStringToWasm0(a, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); + const len = WASM_VECTOR_LEN; + const ptr1 = passStringToWasm0(b, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); + const len2 = WASM_VECTOR_LEN; + const ret = wasm.two_strings(ptr, len, ptr1, len2); + var deferred0 = ret[0]; + var deferred1 = ret[1]; + return getStringFromWasm0(ret[0], ret[1]); + } finally { + wasm.__wbindgen_free(deferred0, deferred1, 1); + } +} + /** * @param {string} a * @param {string} b @@ -97,13 +117,13 @@ export function foo(a) { * @param {number} len * @returns {string} */ -export function bar(a, b, ptr, len) { +export function two_strings_and_some_more(a, b, ptr, len) { try { const ptr1 = passStringToWasm0(a, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); const len2 = WASM_VECTOR_LEN; const ptr3 = passStringToWasm0(b, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); const len4 = WASM_VECTOR_LEN; - const ret = wasm.bar(ptr1, len2, ptr3, len4, ptr, len); + const ret = wasm.two_strings_and_some_more(ptr1, len2, ptr3, len4, ptr, len); var deferred0 = ret[0]; var deferred1 = ret[1]; return getStringFromWasm0(ret[0], ret[1]); diff --git a/crates/cli/tests/reference/string-arg.rs b/crates/cli/tests/reference/string-arg.rs index e2b1bafbc16..161e24e6b49 100644 --- a/crates/cli/tests/reference/string-arg.rs +++ b/crates/cli/tests/reference/string-arg.rs @@ -6,6 +6,11 @@ pub fn foo(a: &str) { } #[wasm_bindgen] -pub fn bar(a: &str, b: String, ptr: u32, len: u32) -> String { +pub fn two_strings(a: &str, b: String) -> String { + b +} + +#[wasm_bindgen] +pub fn two_strings_and_some_more(a: &str, b: String, ptr: u32, len: u32) -> String { b } diff --git a/crates/cli/tests/reference/string-arg.wat b/crates/cli/tests/reference/string-arg.wat index cc5327c7569..1b530bca985 100644 --- a/crates/cli/tests/reference/string-arg.wat +++ b/crates/cli/tests/reference/string-arg.wat @@ -4,18 +4,21 @@ (type (;2;) (func (param i32 i32) (result i32))) (type (;3;) (func (param i32 i32 i32))) (type (;4;) (func (param i32 i32 i32 i32) (result i32))) - (type (;5;) (func (param i32 i32 i32 i32 i32 i32) (result i32 i32))) + (type (;5;) (func (param i32 i32 i32 i32) (result i32 i32))) + (type (;6;) (func (param i32 i32 i32 i32 i32 i32) (result i32 i32))) (import "./reference_test_bg.js" "__wbindgen_init_externref_table" (func (;0;) (type 0))) (func $__wbindgen_realloc (;1;) (type 4) (param i32 i32 i32 i32) (result i32)) (func $__wbindgen_malloc (;2;) (type 2) (param i32 i32) (result i32)) (func $foo (;3;) (type 1) (param i32 i32)) (func $__wbindgen_free (;4;) (type 3) (param i32 i32 i32)) - (func $"bar multivalue shim" (;5;) (type 5) (param i32 i32 i32 i32 i32 i32) (result i32 i32)) + (func $"two_strings_and_some_more multivalue shim" (;5;) (type 6) (param i32 i32 i32 i32 i32 i32) (result i32 i32)) + (func $"two_strings multivalue shim" (;6;) (type 5) (param i32 i32 i32 i32) (result i32 i32)) (table (;0;) 128 externref) (memory (;0;) 17) (export "memory" (memory 0)) (export "foo" (func $foo)) - (export "bar" (func $"bar multivalue shim")) + (export "two_strings" (func $"two_strings multivalue shim")) + (export "two_strings_and_some_more" (func $"two_strings_and_some_more multivalue shim")) (export "__wbindgen_export_0" (table 0)) (export "__wbindgen_malloc" (func $__wbindgen_malloc)) (export "__wbindgen_realloc" (func $__wbindgen_realloc)) From 95c9ae3212544d7af7405e6cd57a4329d0d44027 Mon Sep 17 00:00:00 2001 From: RunDevelopment Date: Sat, 7 Dec 2024 12:29:54 +0100 Subject: [PATCH 05/11] Add `retptr` comment --- crates/cli-support/src/js/binding.rs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/crates/cli-support/src/js/binding.rs b/crates/cli-support/src/js/binding.rs index c1f7a7dabdf..cd1ab5e6ae1 100644 --- a/crates/cli-support/src/js/binding.rs +++ b/crates/cli-support/src/js/binding.rs @@ -863,6 +863,9 @@ fn instruction( Instruction::Retptr { size } => { js.cx.inject_stack_pointer_shim()?; + // This variable name needs to be exactly `retptr` because it's + // used in `Instruction::LoadRetptr`. `LoadRetptr` can't access the + // variable name through the stack, so it needs to be hardcoded. let return_ptr = js.guarantee_alias( "retptr", format!("wasm.__wbindgen_add_to_stack_pointer(-{size})"), From 56ba69a645630400838e9328cc28c0965377ab94 Mon Sep 17 00:00:00 2001 From: RunDevelopment Date: Sat, 7 Dec 2024 12:35:23 +0100 Subject: [PATCH 06/11] bail!, VILE BAIL! --- crates/cli-support/src/js/binding.rs | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/crates/cli-support/src/js/binding.rs b/crates/cli-support/src/js/binding.rs index cd1ab5e6ae1..79f7bd7e330 100644 --- a/crates/cli-support/src/js/binding.rs +++ b/crates/cli-support/src/js/binding.rs @@ -460,16 +460,14 @@ impl<'a, 'b> JsBuilder<'a, 'b> { /// Creates a new variable with the given name and returns the name. /// /// If the name is already taken, this function panics. - pub fn guarantee_alias(&mut self, name: &str, expression: String) -> String { + pub fn guarantee_alias(&mut self, name: &str, expression: String) -> Result { let can_use = self.used_identifiers.insert(name.to_string()); - assert!( - can_use, - "identifier {} already in use in {}", - name, self.debug_name - ); + if !can_use { + bail!("identifier {} already in use in {}", name, self.debug_name); + } self.prelude(&format!("const {name} = {expression};")); - name.to_string() + Ok(name.to_string()) } /// Creates a new JS `const` variable with the given name and returns the @@ -869,7 +867,7 @@ fn instruction( let return_ptr = js.guarantee_alias( "retptr", format!("wasm.__wbindgen_add_to_stack_pointer(-{size})"), - ); + )?; js.finally(&format!("wasm.__wbindgen_add_to_stack_pointer({});", size)); js.push(return_ptr); } From 314f9b90b9ba79532c277098d8d25acfddf27ba7 Mon Sep 17 00:00:00 2001 From: RunDevelopment Date: Sat, 7 Dec 2024 12:39:46 +0100 Subject: [PATCH 07/11] Updated defer comment --- crates/cli-support/src/js/binding.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/cli-support/src/js/binding.rs b/crates/cli-support/src/js/binding.rs index 79f7bd7e330..185863002c0 100644 --- a/crates/cli-support/src/js/binding.rs +++ b/crates/cli-support/src/js/binding.rs @@ -703,7 +703,7 @@ fn instruction( } // If the call is deferred, the arguments to the function still need to be // accessible in the `finally` block, so we declare variables to hold the args - // outside of the try-finally block and then set those to the args. + // outside of the try-finally block with `var` and then set those to the args. let mut slice = Vec::new(); slice.extend(js.stack[js.stack.len() - params..].iter().cloned()); for (i, arg) in slice.into_iter().enumerate() { From 6e5f2e9f1b20312f0c5ad4a5e75fd2866ab258ac Mon Sep 17 00:00:00 2001 From: RunDevelopment Date: Sat, 7 Dec 2024 12:41:05 +0100 Subject: [PATCH 08/11] Updated refs --- crates/cli/tests/reference/echo.js | 176 ++++++++++++++--------------- 1 file changed, 88 insertions(+), 88 deletions(-) diff --git a/crates/cli/tests/reference/echo.js b/crates/cli/tests/reference/echo.js index 85438230bf8..f2717a3cbc4 100644 --- a/crates/cli/tests/reference/echo.js +++ b/crates/cli/tests/reference/echo.js @@ -557,12 +557,12 @@ export function echo_vec_i64(a) { * @returns {Uint8Array} */ export function echo_vec_uninit_u8(a) { - const ptr0 = passArray8ToWasm0(a, wasm.__wbindgen_malloc); - const len0 = WASM_VECTOR_LEN; - const ret = wasm.echo_vec_uninit_u8(ptr0, len0); - var v2 = getArrayU8FromWasm0(ret[0], ret[1]).slice(); + const ptr = passArray8ToWasm0(a, wasm.__wbindgen_malloc); + const len = WASM_VECTOR_LEN; + const ret = wasm.echo_vec_uninit_u8(ptr, len); + const v = getArrayU8FromWasm0(ret[0], ret[1]).slice(); wasm.__wbindgen_free(ret[0], ret[1] * 1, 1); - return v2; + return v; } /** @@ -570,12 +570,12 @@ export function echo_vec_uninit_u8(a) { * @returns {Int8Array} */ export function echo_vec_uninit_i8(a) { - const ptr0 = passArray8ToWasm0(a, wasm.__wbindgen_malloc); - const len0 = WASM_VECTOR_LEN; - const ret = wasm.echo_vec_uninit_i8(ptr0, len0); - var v2 = getArrayI8FromWasm0(ret[0], ret[1]).slice(); + const ptr = passArray8ToWasm0(a, wasm.__wbindgen_malloc); + const len = WASM_VECTOR_LEN; + const ret = wasm.echo_vec_uninit_i8(ptr, len); + const v = getArrayI8FromWasm0(ret[0], ret[1]).slice(); wasm.__wbindgen_free(ret[0], ret[1] * 1, 1); - return v2; + return v; } /** @@ -583,12 +583,12 @@ export function echo_vec_uninit_i8(a) { * @returns {Uint16Array} */ export function echo_vec_uninit_u16(a) { - const ptr0 = passArray16ToWasm0(a, wasm.__wbindgen_malloc); - const len0 = WASM_VECTOR_LEN; - const ret = wasm.echo_vec_uninit_u16(ptr0, len0); - var v2 = getArrayU16FromWasm0(ret[0], ret[1]).slice(); + const ptr = passArray16ToWasm0(a, wasm.__wbindgen_malloc); + const len = WASM_VECTOR_LEN; + const ret = wasm.echo_vec_uninit_u16(ptr, len); + const v = getArrayU16FromWasm0(ret[0], ret[1]).slice(); wasm.__wbindgen_free(ret[0], ret[1] * 2, 2); - return v2; + return v; } /** @@ -596,12 +596,12 @@ export function echo_vec_uninit_u16(a) { * @returns {Int16Array} */ export function echo_vec_uninit_i16(a) { - const ptr0 = passArray16ToWasm0(a, wasm.__wbindgen_malloc); - const len0 = WASM_VECTOR_LEN; - const ret = wasm.echo_vec_uninit_i16(ptr0, len0); - var v2 = getArrayI16FromWasm0(ret[0], ret[1]).slice(); + const ptr = passArray16ToWasm0(a, wasm.__wbindgen_malloc); + const len = WASM_VECTOR_LEN; + const ret = wasm.echo_vec_uninit_i16(ptr, len); + const v = getArrayI16FromWasm0(ret[0], ret[1]).slice(); wasm.__wbindgen_free(ret[0], ret[1] * 2, 2); - return v2; + return v; } /** @@ -609,12 +609,12 @@ export function echo_vec_uninit_i16(a) { * @returns {Uint32Array} */ export function echo_vec_uninit_u32(a) { - const ptr0 = passArray32ToWasm0(a, wasm.__wbindgen_malloc); - const len0 = WASM_VECTOR_LEN; - const ret = wasm.echo_vec_uninit_u32(ptr0, len0); - var v2 = getArrayU32FromWasm0(ret[0], ret[1]).slice(); + const ptr = passArray32ToWasm0(a, wasm.__wbindgen_malloc); + const len = WASM_VECTOR_LEN; + const ret = wasm.echo_vec_uninit_u32(ptr, len); + const v = getArrayU32FromWasm0(ret[0], ret[1]).slice(); wasm.__wbindgen_free(ret[0], ret[1] * 4, 4); - return v2; + return v; } /** @@ -622,12 +622,12 @@ export function echo_vec_uninit_u32(a) { * @returns {Int32Array} */ export function echo_vec_uninit_i32(a) { - const ptr0 = passArray32ToWasm0(a, wasm.__wbindgen_malloc); - const len0 = WASM_VECTOR_LEN; - const ret = wasm.echo_vec_uninit_i32(ptr0, len0); - var v2 = getArrayI32FromWasm0(ret[0], ret[1]).slice(); + const ptr = passArray32ToWasm0(a, wasm.__wbindgen_malloc); + const len = WASM_VECTOR_LEN; + const ret = wasm.echo_vec_uninit_i32(ptr, len); + const v = getArrayI32FromWasm0(ret[0], ret[1]).slice(); wasm.__wbindgen_free(ret[0], ret[1] * 4, 4); - return v2; + return v; } /** @@ -635,12 +635,12 @@ export function echo_vec_uninit_i32(a) { * @returns {BigUint64Array} */ export function echo_vec_uninit_u64(a) { - const ptr0 = passArray64ToWasm0(a, wasm.__wbindgen_malloc); - const len0 = WASM_VECTOR_LEN; - const ret = wasm.echo_vec_uninit_u64(ptr0, len0); - var v2 = getArrayU64FromWasm0(ret[0], ret[1]).slice(); + const ptr = passArray64ToWasm0(a, wasm.__wbindgen_malloc); + const len = WASM_VECTOR_LEN; + const ret = wasm.echo_vec_uninit_u64(ptr, len); + const v = getArrayU64FromWasm0(ret[0], ret[1]).slice(); wasm.__wbindgen_free(ret[0], ret[1] * 8, 8); - return v2; + return v; } /** @@ -648,12 +648,12 @@ export function echo_vec_uninit_u64(a) { * @returns {BigInt64Array} */ export function echo_vec_uninit_i64(a) { - const ptr0 = passArray64ToWasm0(a, wasm.__wbindgen_malloc); - const len0 = WASM_VECTOR_LEN; - const ret = wasm.echo_vec_uninit_i64(ptr0, len0); - var v2 = getArrayI64FromWasm0(ret[0], ret[1]).slice(); + const ptr = passArray64ToWasm0(a, wasm.__wbindgen_malloc); + const len = WASM_VECTOR_LEN; + const ret = wasm.echo_vec_uninit_i64(ptr, len); + const v = getArrayI64FromWasm0(ret[0], ret[1]).slice(); wasm.__wbindgen_free(ret[0], ret[1] * 8, 8); - return v2; + return v; } function addToExternrefTable0(obj) { @@ -1019,15 +1019,15 @@ export function echo_option_vec_i64(a) { * @returns {Uint8Array | undefined} */ export function echo_option_vec_uninit_u8(a) { - var ptr0 = isLikeNone(a) ? 0 : passArray8ToWasm0(a, wasm.__wbindgen_malloc); - var len0 = WASM_VECTOR_LEN; - const ret = wasm.echo_option_vec_uninit_u8(ptr0, len0); - let v2; + const ptr = isLikeNone(a) ? 0 : passArray8ToWasm0(a, wasm.__wbindgen_malloc); + const len = WASM_VECTOR_LEN; + const ret = wasm.echo_option_vec_uninit_u8(ptr, len); + var v = undefined; if (ret[0] !== 0) { - v2 = getArrayU8FromWasm0(ret[0], ret[1]).slice(); + v = getArrayU8FromWasm0(ret[0], ret[1]).slice(); wasm.__wbindgen_free(ret[0], ret[1] * 1, 1); } - return v2; + return v; } /** @@ -1035,15 +1035,15 @@ export function echo_option_vec_uninit_u8(a) { * @returns {Int8Array | undefined} */ export function echo_option_vec_uninit_i8(a) { - var ptr0 = isLikeNone(a) ? 0 : passArray8ToWasm0(a, wasm.__wbindgen_malloc); - var len0 = WASM_VECTOR_LEN; - const ret = wasm.echo_option_vec_uninit_i8(ptr0, len0); - let v2; + const ptr = isLikeNone(a) ? 0 : passArray8ToWasm0(a, wasm.__wbindgen_malloc); + const len = WASM_VECTOR_LEN; + const ret = wasm.echo_option_vec_uninit_i8(ptr, len); + var v = undefined; if (ret[0] !== 0) { - v2 = getArrayI8FromWasm0(ret[0], ret[1]).slice(); + v = getArrayI8FromWasm0(ret[0], ret[1]).slice(); wasm.__wbindgen_free(ret[0], ret[1] * 1, 1); } - return v2; + return v; } /** @@ -1051,15 +1051,15 @@ export function echo_option_vec_uninit_i8(a) { * @returns {Uint16Array | undefined} */ export function echo_option_vec_uninit_u16(a) { - var ptr0 = isLikeNone(a) ? 0 : passArray16ToWasm0(a, wasm.__wbindgen_malloc); - var len0 = WASM_VECTOR_LEN; - const ret = wasm.echo_option_vec_uninit_u16(ptr0, len0); - let v2; + const ptr = isLikeNone(a) ? 0 : passArray16ToWasm0(a, wasm.__wbindgen_malloc); + const len = WASM_VECTOR_LEN; + const ret = wasm.echo_option_vec_uninit_u16(ptr, len); + var v = undefined; if (ret[0] !== 0) { - v2 = getArrayU16FromWasm0(ret[0], ret[1]).slice(); + v = getArrayU16FromWasm0(ret[0], ret[1]).slice(); wasm.__wbindgen_free(ret[0], ret[1] * 2, 2); } - return v2; + return v; } /** @@ -1067,15 +1067,15 @@ export function echo_option_vec_uninit_u16(a) { * @returns {Int16Array | undefined} */ export function echo_option_vec_uninit_i16(a) { - var ptr0 = isLikeNone(a) ? 0 : passArray16ToWasm0(a, wasm.__wbindgen_malloc); - var len0 = WASM_VECTOR_LEN; - const ret = wasm.echo_option_vec_uninit_i16(ptr0, len0); - let v2; + const ptr = isLikeNone(a) ? 0 : passArray16ToWasm0(a, wasm.__wbindgen_malloc); + const len = WASM_VECTOR_LEN; + const ret = wasm.echo_option_vec_uninit_i16(ptr, len); + var v = undefined; if (ret[0] !== 0) { - v2 = getArrayI16FromWasm0(ret[0], ret[1]).slice(); + v = getArrayI16FromWasm0(ret[0], ret[1]).slice(); wasm.__wbindgen_free(ret[0], ret[1] * 2, 2); } - return v2; + return v; } /** @@ -1083,15 +1083,15 @@ export function echo_option_vec_uninit_i16(a) { * @returns {Uint32Array | undefined} */ export function echo_option_vec_uninit_u32(a) { - var ptr0 = isLikeNone(a) ? 0 : passArray32ToWasm0(a, wasm.__wbindgen_malloc); - var len0 = WASM_VECTOR_LEN; - const ret = wasm.echo_option_vec_uninit_u32(ptr0, len0); - let v2; + const ptr = isLikeNone(a) ? 0 : passArray32ToWasm0(a, wasm.__wbindgen_malloc); + const len = WASM_VECTOR_LEN; + const ret = wasm.echo_option_vec_uninit_u32(ptr, len); + var v = undefined; if (ret[0] !== 0) { - v2 = getArrayU32FromWasm0(ret[0], ret[1]).slice(); + v = getArrayU32FromWasm0(ret[0], ret[1]).slice(); wasm.__wbindgen_free(ret[0], ret[1] * 4, 4); } - return v2; + return v; } /** @@ -1099,15 +1099,15 @@ export function echo_option_vec_uninit_u32(a) { * @returns {Int32Array | undefined} */ export function echo_option_vec_uninit_i32(a) { - var ptr0 = isLikeNone(a) ? 0 : passArray32ToWasm0(a, wasm.__wbindgen_malloc); - var len0 = WASM_VECTOR_LEN; - const ret = wasm.echo_option_vec_uninit_i32(ptr0, len0); - let v2; + const ptr = isLikeNone(a) ? 0 : passArray32ToWasm0(a, wasm.__wbindgen_malloc); + const len = WASM_VECTOR_LEN; + const ret = wasm.echo_option_vec_uninit_i32(ptr, len); + var v = undefined; if (ret[0] !== 0) { - v2 = getArrayI32FromWasm0(ret[0], ret[1]).slice(); + v = getArrayI32FromWasm0(ret[0], ret[1]).slice(); wasm.__wbindgen_free(ret[0], ret[1] * 4, 4); } - return v2; + return v; } /** @@ -1115,15 +1115,15 @@ export function echo_option_vec_uninit_i32(a) { * @returns {BigUint64Array | undefined} */ export function echo_option_vec_uninit_u64(a) { - var ptr0 = isLikeNone(a) ? 0 : passArray64ToWasm0(a, wasm.__wbindgen_malloc); - var len0 = WASM_VECTOR_LEN; - const ret = wasm.echo_option_vec_uninit_u64(ptr0, len0); - let v2; + const ptr = isLikeNone(a) ? 0 : passArray64ToWasm0(a, wasm.__wbindgen_malloc); + const len = WASM_VECTOR_LEN; + const ret = wasm.echo_option_vec_uninit_u64(ptr, len); + var v = undefined; if (ret[0] !== 0) { - v2 = getArrayU64FromWasm0(ret[0], ret[1]).slice(); + v = getArrayU64FromWasm0(ret[0], ret[1]).slice(); wasm.__wbindgen_free(ret[0], ret[1] * 8, 8); } - return v2; + return v; } /** @@ -1131,15 +1131,15 @@ export function echo_option_vec_uninit_u64(a) { * @returns {BigInt64Array | undefined} */ export function echo_option_vec_uninit_i64(a) { - var ptr0 = isLikeNone(a) ? 0 : passArray64ToWasm0(a, wasm.__wbindgen_malloc); - var len0 = WASM_VECTOR_LEN; - const ret = wasm.echo_option_vec_uninit_i64(ptr0, len0); - let v2; + const ptr = isLikeNone(a) ? 0 : passArray64ToWasm0(a, wasm.__wbindgen_malloc); + const len = WASM_VECTOR_LEN; + const ret = wasm.echo_option_vec_uninit_i64(ptr, len); + var v = undefined; if (ret[0] !== 0) { - v2 = getArrayI64FromWasm0(ret[0], ret[1]).slice(); + v = getArrayI64FromWasm0(ret[0], ret[1]).slice(); wasm.__wbindgen_free(ret[0], ret[1] * 8, 8); } - return v2; + return v; } /** From 51792da23259a9b693d18a4932446f6a3e394b61 Mon Sep 17 00:00:00 2001 From: RunDevelopment Date: Mon, 9 Dec 2024 01:13:42 +0100 Subject: [PATCH 09/11] Add `let` binding --- crates/cli-support/src/js/binding.rs | 23 +++++++++++----- crates/cli/tests/reference/echo.js | 40 ++++++++++++++-------------- crates/cli/tests/reference/result.js | 4 +-- 3 files changed, 39 insertions(+), 28 deletions(-) diff --git a/crates/cli-support/src/js/binding.rs b/crates/cli-support/src/js/binding.rs index 50e4999890a..812e3527486 100644 --- a/crates/cli-support/src/js/binding.rs +++ b/crates/cli-support/src/js/binding.rs @@ -489,6 +489,17 @@ impl<'a, 'b> JsBuilder<'a, 'b> { ident } + /// Creates a mutable JS variable with the given name and returns the + /// name. The create variable will use the `let` binding and will not be + /// visible in the `finally` block. + /// + /// If the name is already taken, a different name is generated. + pub fn r#let(&mut self, name: &str, initial_value: String) -> String { + let ident = self.unique_name(name); + self.prelude(&format!("let {} = {};", ident, initial_value)); + ident + } + /// Creates a mutable JS `var`iable with the given name and returns the /// name. The create variable is also visible in the `finally` block. /// @@ -925,8 +936,8 @@ fn instruction( "{}().{}(retptr + {} * {}, true)", mem, method, size, scaled_offset ); - js.prelude(&format!("var r{} = {};", offset, expr)); - js.push(format!("r{}", offset)); + let r = js.alias(&format!("r{offset}"), expr); + js.push(r); } Instruction::I32FromBool => { @@ -975,7 +986,7 @@ fn instruction( Instruction::I32FromOptionRust { class } => { let val = js.pop(); js.cx.expose_is_like_none(); - let ptr = js.var("ptr", "0".to_string()); + let ptr = js.r#let("ptr", "0".to_string()); js.prelude(&format!("if (!isLikeNone({0})) {{", val)); js.assert_class(&val, class); js.assert_not_moved(&val); @@ -1146,8 +1157,8 @@ fn instruction( let len = js.pop(); let ptr = js.pop(); - let ptr = js.var("ptr", ptr); - let len = js.var("len", len); + let ptr = js.r#let("ptr", ptr); + let len = js.r#let("len", len); js.prelude(&format!( " if ({is_err}) {{ @@ -1356,7 +1367,7 @@ fn instruction( let ptr = js.pop(); let f = js.cx.expose_get_vector_from_wasm(kind.clone(), *mem)?; let free = js.cx.export_name_of(*free); - let val = js.var("v", "undefined".to_string()); + let val = js.r#let("v", "undefined".to_string()); js.prelude(&format!("if ({} !== 0) {{", ptr)); js.prelude(&format!("{val} = {f}({ptr}, {len}).slice();")); js.prelude(&format!( diff --git a/crates/cli/tests/reference/echo.js b/crates/cli/tests/reference/echo.js index 492eb4218f7..e6e7e60c788 100644 --- a/crates/cli/tests/reference/echo.js +++ b/crates/cli/tests/reference/echo.js @@ -878,7 +878,7 @@ export function echo_option_string(a) { const ptr = isLikeNone(a) ? 0 : passStringToWasm0(a, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); const len = WASM_VECTOR_LEN; const ret = wasm.echo_option_string(ptr, len); - var v = undefined; + let v = undefined; if (ret[0] !== 0) { v = getStringFromWasm0(ret[0], ret[1]).slice(); wasm.__wbindgen_free(ret[0], ret[1] * 1, 1); @@ -894,7 +894,7 @@ export function echo_option_vec_u8(a) { const ptr = isLikeNone(a) ? 0 : passArray8ToWasm0(a, wasm.__wbindgen_malloc); const len = WASM_VECTOR_LEN; const ret = wasm.echo_option_vec_u8(ptr, len); - var v = undefined; + let v = undefined; if (ret[0] !== 0) { v = getArrayU8FromWasm0(ret[0], ret[1]).slice(); wasm.__wbindgen_free(ret[0], ret[1] * 1, 1); @@ -910,7 +910,7 @@ export function echo_option_vec_i8(a) { const ptr = isLikeNone(a) ? 0 : passArray8ToWasm0(a, wasm.__wbindgen_malloc); const len = WASM_VECTOR_LEN; const ret = wasm.echo_option_vec_i8(ptr, len); - var v = undefined; + let v = undefined; if (ret[0] !== 0) { v = getArrayI8FromWasm0(ret[0], ret[1]).slice(); wasm.__wbindgen_free(ret[0], ret[1] * 1, 1); @@ -926,7 +926,7 @@ export function echo_option_vec_u16(a) { const ptr = isLikeNone(a) ? 0 : passArray16ToWasm0(a, wasm.__wbindgen_malloc); const len = WASM_VECTOR_LEN; const ret = wasm.echo_option_vec_u16(ptr, len); - var v = undefined; + let v = undefined; if (ret[0] !== 0) { v = getArrayU16FromWasm0(ret[0], ret[1]).slice(); wasm.__wbindgen_free(ret[0], ret[1] * 2, 2); @@ -942,7 +942,7 @@ export function echo_option_vec_i16(a) { const ptr = isLikeNone(a) ? 0 : passArray16ToWasm0(a, wasm.__wbindgen_malloc); const len = WASM_VECTOR_LEN; const ret = wasm.echo_option_vec_i16(ptr, len); - var v = undefined; + let v = undefined; if (ret[0] !== 0) { v = getArrayI16FromWasm0(ret[0], ret[1]).slice(); wasm.__wbindgen_free(ret[0], ret[1] * 2, 2); @@ -958,7 +958,7 @@ export function echo_option_vec_u32(a) { const ptr = isLikeNone(a) ? 0 : passArray32ToWasm0(a, wasm.__wbindgen_malloc); const len = WASM_VECTOR_LEN; const ret = wasm.echo_option_vec_u32(ptr, len); - var v = undefined; + let v = undefined; if (ret[0] !== 0) { v = getArrayU32FromWasm0(ret[0], ret[1]).slice(); wasm.__wbindgen_free(ret[0], ret[1] * 4, 4); @@ -974,7 +974,7 @@ export function echo_option_vec_i32(a) { const ptr = isLikeNone(a) ? 0 : passArray32ToWasm0(a, wasm.__wbindgen_malloc); const len = WASM_VECTOR_LEN; const ret = wasm.echo_option_vec_i32(ptr, len); - var v = undefined; + let v = undefined; if (ret[0] !== 0) { v = getArrayI32FromWasm0(ret[0], ret[1]).slice(); wasm.__wbindgen_free(ret[0], ret[1] * 4, 4); @@ -990,7 +990,7 @@ export function echo_option_vec_u64(a) { const ptr = isLikeNone(a) ? 0 : passArray64ToWasm0(a, wasm.__wbindgen_malloc); const len = WASM_VECTOR_LEN; const ret = wasm.echo_option_vec_u64(ptr, len); - var v = undefined; + let v = undefined; if (ret[0] !== 0) { v = getArrayU64FromWasm0(ret[0], ret[1]).slice(); wasm.__wbindgen_free(ret[0], ret[1] * 8, 8); @@ -1006,7 +1006,7 @@ export function echo_option_vec_i64(a) { const ptr = isLikeNone(a) ? 0 : passArray64ToWasm0(a, wasm.__wbindgen_malloc); const len = WASM_VECTOR_LEN; const ret = wasm.echo_option_vec_i64(ptr, len); - var v = undefined; + let v = undefined; if (ret[0] !== 0) { v = getArrayI64FromWasm0(ret[0], ret[1]).slice(); wasm.__wbindgen_free(ret[0], ret[1] * 8, 8); @@ -1022,7 +1022,7 @@ export function echo_option_vec_uninit_u8(a) { const ptr = isLikeNone(a) ? 0 : passArray8ToWasm0(a, wasm.__wbindgen_malloc); const len = WASM_VECTOR_LEN; const ret = wasm.echo_option_vec_uninit_u8(ptr, len); - var v = undefined; + let v = undefined; if (ret[0] !== 0) { v = getArrayU8FromWasm0(ret[0], ret[1]).slice(); wasm.__wbindgen_free(ret[0], ret[1] * 1, 1); @@ -1038,7 +1038,7 @@ export function echo_option_vec_uninit_i8(a) { const ptr = isLikeNone(a) ? 0 : passArray8ToWasm0(a, wasm.__wbindgen_malloc); const len = WASM_VECTOR_LEN; const ret = wasm.echo_option_vec_uninit_i8(ptr, len); - var v = undefined; + let v = undefined; if (ret[0] !== 0) { v = getArrayI8FromWasm0(ret[0], ret[1]).slice(); wasm.__wbindgen_free(ret[0], ret[1] * 1, 1); @@ -1054,7 +1054,7 @@ export function echo_option_vec_uninit_u16(a) { const ptr = isLikeNone(a) ? 0 : passArray16ToWasm0(a, wasm.__wbindgen_malloc); const len = WASM_VECTOR_LEN; const ret = wasm.echo_option_vec_uninit_u16(ptr, len); - var v = undefined; + let v = undefined; if (ret[0] !== 0) { v = getArrayU16FromWasm0(ret[0], ret[1]).slice(); wasm.__wbindgen_free(ret[0], ret[1] * 2, 2); @@ -1070,7 +1070,7 @@ export function echo_option_vec_uninit_i16(a) { const ptr = isLikeNone(a) ? 0 : passArray16ToWasm0(a, wasm.__wbindgen_malloc); const len = WASM_VECTOR_LEN; const ret = wasm.echo_option_vec_uninit_i16(ptr, len); - var v = undefined; + let v = undefined; if (ret[0] !== 0) { v = getArrayI16FromWasm0(ret[0], ret[1]).slice(); wasm.__wbindgen_free(ret[0], ret[1] * 2, 2); @@ -1086,7 +1086,7 @@ export function echo_option_vec_uninit_u32(a) { const ptr = isLikeNone(a) ? 0 : passArray32ToWasm0(a, wasm.__wbindgen_malloc); const len = WASM_VECTOR_LEN; const ret = wasm.echo_option_vec_uninit_u32(ptr, len); - var v = undefined; + let v = undefined; if (ret[0] !== 0) { v = getArrayU32FromWasm0(ret[0], ret[1]).slice(); wasm.__wbindgen_free(ret[0], ret[1] * 4, 4); @@ -1102,7 +1102,7 @@ export function echo_option_vec_uninit_i32(a) { const ptr = isLikeNone(a) ? 0 : passArray32ToWasm0(a, wasm.__wbindgen_malloc); const len = WASM_VECTOR_LEN; const ret = wasm.echo_option_vec_uninit_i32(ptr, len); - var v = undefined; + let v = undefined; if (ret[0] !== 0) { v = getArrayI32FromWasm0(ret[0], ret[1]).slice(); wasm.__wbindgen_free(ret[0], ret[1] * 4, 4); @@ -1118,7 +1118,7 @@ export function echo_option_vec_uninit_u64(a) { const ptr = isLikeNone(a) ? 0 : passArray64ToWasm0(a, wasm.__wbindgen_malloc); const len = WASM_VECTOR_LEN; const ret = wasm.echo_option_vec_uninit_u64(ptr, len); - var v = undefined; + let v = undefined; if (ret[0] !== 0) { v = getArrayU64FromWasm0(ret[0], ret[1]).slice(); wasm.__wbindgen_free(ret[0], ret[1] * 8, 8); @@ -1134,7 +1134,7 @@ export function echo_option_vec_uninit_i64(a) { const ptr = isLikeNone(a) ? 0 : passArray64ToWasm0(a, wasm.__wbindgen_malloc); const len = WASM_VECTOR_LEN; const ret = wasm.echo_option_vec_uninit_i64(ptr, len); - var v = undefined; + let v = undefined; if (ret[0] !== 0) { v = getArrayI64FromWasm0(ret[0], ret[1]).slice(); wasm.__wbindgen_free(ret[0], ret[1] * 8, 8); @@ -1150,7 +1150,7 @@ export function echo_option_vec_string(a) { const ptr = isLikeNone(a) ? 0 : passArrayJsValueToWasm0(a, wasm.__wbindgen_malloc); const len = WASM_VECTOR_LEN; const ret = wasm.echo_option_vec_string(ptr, len); - var v = undefined; + let v = undefined; if (ret[0] !== 0) { v = getArrayJsValueFromWasm0(ret[0], ret[1]).slice(); wasm.__wbindgen_free(ret[0], ret[1] * 4, 4); @@ -1163,7 +1163,7 @@ export function echo_option_vec_string(a) { * @returns {Foo | undefined} */ export function echo_option_struct(a) { - var ptr = 0; + let ptr = 0; if (!isLikeNone(a)) { _assertClass(a, Foo); ptr = a.__destroy_into_raw(); @@ -1180,7 +1180,7 @@ export function echo_option_vec_struct(a) { const ptr = isLikeNone(a) ? 0 : passArrayJsValueToWasm0(a, wasm.__wbindgen_malloc); const len = WASM_VECTOR_LEN; const ret = wasm.echo_option_vec_struct(ptr, len); - var v = undefined; + let v = undefined; if (ret[0] !== 0) { v = getArrayJsValueFromWasm0(ret[0], ret[1]).slice(); wasm.__wbindgen_free(ret[0], ret[1] * 4, 4); diff --git a/crates/cli/tests/reference/result.js b/crates/cli/tests/reference/result.js index 7861fade34f..26fdd2cf45b 100644 --- a/crates/cli/tests/reference/result.js +++ b/crates/cli/tests/reference/result.js @@ -35,8 +35,8 @@ function takeFromExternrefTable0(idx) { export function result_string() { try { const ret = wasm.result_string(); - var ptr = ret[0]; - var len = ret[1]; + let ptr = ret[0]; + let len = ret[1]; if (ret[3]) { ptr = 0; len = 0; throw takeFromExternrefTable0(ret[2]); From 2621c79b62c4ba687e198c00f414270c49693334 Mon Sep 17 00:00:00 2001 From: RunDevelopment Date: Mon, 9 Dec 2024 01:24:01 +0100 Subject: [PATCH 10/11] Added ref tests for closures --- crates/cli-support/src/js/binding.rs | 2 +- crates/cli/tests/reference/closure.d.ts | 3 + crates/cli/tests/reference/closure.js | 132 ++++++++++++++++++++++++ crates/cli/tests/reference/closure.rs | 20 ++++ crates/cli/tests/reference/closure.wat | 20 ++++ 5 files changed, 176 insertions(+), 1 deletion(-) create mode 100644 crates/cli/tests/reference/closure.d.ts create mode 100644 crates/cli/tests/reference/closure.js create mode 100644 crates/cli/tests/reference/closure.rs create mode 100644 crates/cli/tests/reference/closure.wat diff --git a/crates/cli-support/src/js/binding.rs b/crates/cli-support/src/js/binding.rs index 812e3527486..7d26e5053dd 100644 --- a/crates/cli-support/src/js/binding.rs +++ b/crates/cli-support/src/js/binding.rs @@ -1319,7 +1319,7 @@ fn instruction( .collect::>() .join(", "); let wrapper = js.cx.adapter_name(*adapter); - let cb = js.var( + let cb = js.alias( "cb", if *mutable { // Mutable closures need protection against being called diff --git a/crates/cli/tests/reference/closure.d.ts b/crates/cli/tests/reference/closure.d.ts new file mode 100644 index 00000000000..091dbcc51a6 --- /dev/null +++ b/crates/cli/tests/reference/closure.d.ts @@ -0,0 +1,3 @@ +/* tslint:disable */ +/* eslint-disable */ +export function exported(): void; diff --git a/crates/cli/tests/reference/closure.js b/crates/cli/tests/reference/closure.js new file mode 100644 index 00000000000..5782c14ad13 --- /dev/null +++ b/crates/cli/tests/reference/closure.js @@ -0,0 +1,132 @@ +let wasm; +export function __wbg_set_wasm(val) { + wasm = val; +} + + +const CLOSURE_DTORS = (typeof FinalizationRegistry === 'undefined') + ? { register: () => {}, unregister: () => {} } + : new FinalizationRegistry(state => { + wasm.__wbindgen_export_0.get(state.dtor)(state.a, state.b) +}); + +function makeMutClosure(arg0, arg1, dtor, f) { + const state = { a: arg0, b: arg1, cnt: 1, dtor }; + const real = (...args) => { + // First up with a closure we increment the internal reference + // count. This ensures that the Rust closure environment won't + // be deallocated while we're invoking it. + state.cnt++; + const a = state.a; + state.a = 0; + try { + return f(a, state.b, ...args); + } finally { + if (--state.cnt === 0) { + wasm.__wbindgen_export_0.get(state.dtor)(a, state.b); + CLOSURE_DTORS.unregister(state); + } else { + state.a = a; + } + } + }; + real.original = state; + CLOSURE_DTORS.register(real, state, state); + return real; +} + +const lTextDecoder = typeof TextDecoder === 'undefined' ? (0, module.require)('util').TextDecoder : TextDecoder; + +let cachedTextDecoder = new lTextDecoder('utf-8', { ignoreBOM: true, fatal: true }); + +cachedTextDecoder.decode(); + +let cachedUint8ArrayMemory0 = null; + +function getUint8ArrayMemory0() { + if (cachedUint8ArrayMemory0 === null || cachedUint8ArrayMemory0.byteLength === 0) { + cachedUint8ArrayMemory0 = new Uint8Array(wasm.memory.buffer); + } + return cachedUint8ArrayMemory0; +} + +function getStringFromWasm0(ptr, len) { + ptr = ptr >>> 0; + return cachedTextDecoder.decode(getUint8ArrayMemory0().subarray(ptr, ptr + len)); +} + +export function exported() { + wasm.exported(); +} + +function __wbg_adapter_8(arg0, arg1) { + wasm._dyn_core__ops__function__FnMut_____Output___R_as_wasm_bindgen__closure__WasmClosure___describe__invoke__h08d04a92620849c7(arg0, arg1); +} + +function __wbg_adapter_9(arg0, arg1) { + wasm.wasm_bindgen__convert__closures__invoke0__h0558a7c8fc0aaaa4(arg0, arg1); +} + +export function __wbg_setInterval_dcabc54c1b7f2a8c(arg0, arg1) { + const ret = setInterval(arg0, arg1 >>> 0); + return ret; +}; + +export function __wbg_takesimmutableclosure_19e990abb85ab0b6(arg0, arg1) { + try { + var state = {a: arg0, b: arg1}; + const cb = () => __wbg_adapter_9(state.a, state.b, ); + takes_immutable_closure(cb); + } finally { + state.a = state.b = 0; + } +}; + +export function __wbg_takesmutableclosure_792efe9eb99346a2(arg0, arg1) { + try { + var state = {a: arg0, b: arg1}; + const cb = () => { + const a = state.a; + state.a = 0; + try { + return __wbg_adapter_8(a, state.b, ); + } finally { + state.a = a; + } + }; + takes_mutable_closure(cb); + } finally { + state.a = state.b = 0; + } +}; + +export function __wbindgen_cb_drop(arg0) { + const obj = arg0.original; + if (obj.cnt-- == 1) { + obj.a = 0; + return true; + } + const ret = false; + return ret; +}; + +export function __wbindgen_closure_wrapper57(arg0, arg1, arg2) { + const ret = makeMutClosure(arg0, arg1, 4, __wbg_adapter_8); + return ret; +}; + +export function __wbindgen_init_externref_table() { + const table = wasm.__wbindgen_export_1; + const offset = table.grow(4); + table.set(0, undefined); + table.set(offset + 0, undefined); + table.set(offset + 1, null); + table.set(offset + 2, true); + table.set(offset + 3, false); + ; +}; + +export function __wbindgen_throw(arg0, arg1) { + throw new Error(getStringFromWasm0(arg0, arg1)); +}; + diff --git a/crates/cli/tests/reference/closure.rs b/crates/cli/tests/reference/closure.rs new file mode 100644 index 00000000000..98479cee726 --- /dev/null +++ b/crates/cli/tests/reference/closure.rs @@ -0,0 +1,20 @@ +use wasm_bindgen::prelude::*; + +#[wasm_bindgen] +extern "C" { + // stack + fn takes_immutable_closure(f: &dyn Fn()); + fn takes_mutable_closure(f: &mut dyn FnMut()); + + // heap + fn setInterval(closure: &Closure, millis: u32) -> f64; +} + +#[wasm_bindgen] +pub fn exported() { + takes_immutable_closure(&|| {}); + takes_mutable_closure(&mut || {}); + + let closure = Closure::new(|| {}); + setInterval(&closure, 1000); +} diff --git a/crates/cli/tests/reference/closure.wat b/crates/cli/tests/reference/closure.wat new file mode 100644 index 00000000000..caebc285a2f --- /dev/null +++ b/crates/cli/tests/reference/closure.wat @@ -0,0 +1,20 @@ +(module $reference_test.wasm + (type (;0;) (func)) + (type (;1;) (func (param i32 i32))) + (import "./reference_test_bg.js" "__wbindgen_init_externref_table" (func (;0;) (type 0))) + (func $"+Output = R as wasm_bindgen::closure::WasmClosure>::describe::invoke::h08d04a92620849c7" (;1;) (type 1) (param i32 i32)) + (func $wasm_bindgen::convert::closures::invoke0::h0558a7c8fc0aaaa4 (;2;) (type 1) (param i32 i32)) + (func $exported (;3;) (type 0)) + (table (;0;) 37 37 funcref) + (table (;1;) 128 externref) + (memory (;0;) 17) + (export "memory" (memory 0)) + (export "exported" (func $exported)) + (export "__wbindgen_export_0" (table 0)) + (export "__wbindgen_export_1" (table 1)) + (export "_dyn_core__ops__function__FnMut_____Output___R_as_wasm_bindgen__closure__WasmClosure___describe__invoke__h08d04a92620849c7" (func $"+Output = R as wasm_bindgen::closure::WasmClosure>::describe::invoke::h08d04a92620849c7")) + (export "wasm_bindgen__convert__closures__invoke0__h0558a7c8fc0aaaa4" (func $wasm_bindgen::convert::closures::invoke0::h0558a7c8fc0aaaa4)) + (export "__wbindgen_start" (func 0)) + (@custom "target_features" (after code) "\04+\0amultivalue+\0fmutable-globals+\0freference-types+\08sign-ext") +) + From 6ed38226e3b1b98a931c9452fd9e3d13ebe2b277 Mon Sep 17 00:00:00 2001 From: RunDevelopment Date: Mon, 9 Dec 2024 01:28:38 +0100 Subject: [PATCH 11/11] bye bye closures --- crates/cli/tests/reference/closure.d.ts | 3 - crates/cli/tests/reference/closure.js | 132 ------------------------ crates/cli/tests/reference/closure.rs | 20 ---- crates/cli/tests/reference/closure.wat | 20 ---- 4 files changed, 175 deletions(-) delete mode 100644 crates/cli/tests/reference/closure.d.ts delete mode 100644 crates/cli/tests/reference/closure.js delete mode 100644 crates/cli/tests/reference/closure.rs delete mode 100644 crates/cli/tests/reference/closure.wat diff --git a/crates/cli/tests/reference/closure.d.ts b/crates/cli/tests/reference/closure.d.ts deleted file mode 100644 index 091dbcc51a6..00000000000 --- a/crates/cli/tests/reference/closure.d.ts +++ /dev/null @@ -1,3 +0,0 @@ -/* tslint:disable */ -/* eslint-disable */ -export function exported(): void; diff --git a/crates/cli/tests/reference/closure.js b/crates/cli/tests/reference/closure.js deleted file mode 100644 index 5782c14ad13..00000000000 --- a/crates/cli/tests/reference/closure.js +++ /dev/null @@ -1,132 +0,0 @@ -let wasm; -export function __wbg_set_wasm(val) { - wasm = val; -} - - -const CLOSURE_DTORS = (typeof FinalizationRegistry === 'undefined') - ? { register: () => {}, unregister: () => {} } - : new FinalizationRegistry(state => { - wasm.__wbindgen_export_0.get(state.dtor)(state.a, state.b) -}); - -function makeMutClosure(arg0, arg1, dtor, f) { - const state = { a: arg0, b: arg1, cnt: 1, dtor }; - const real = (...args) => { - // First up with a closure we increment the internal reference - // count. This ensures that the Rust closure environment won't - // be deallocated while we're invoking it. - state.cnt++; - const a = state.a; - state.a = 0; - try { - return f(a, state.b, ...args); - } finally { - if (--state.cnt === 0) { - wasm.__wbindgen_export_0.get(state.dtor)(a, state.b); - CLOSURE_DTORS.unregister(state); - } else { - state.a = a; - } - } - }; - real.original = state; - CLOSURE_DTORS.register(real, state, state); - return real; -} - -const lTextDecoder = typeof TextDecoder === 'undefined' ? (0, module.require)('util').TextDecoder : TextDecoder; - -let cachedTextDecoder = new lTextDecoder('utf-8', { ignoreBOM: true, fatal: true }); - -cachedTextDecoder.decode(); - -let cachedUint8ArrayMemory0 = null; - -function getUint8ArrayMemory0() { - if (cachedUint8ArrayMemory0 === null || cachedUint8ArrayMemory0.byteLength === 0) { - cachedUint8ArrayMemory0 = new Uint8Array(wasm.memory.buffer); - } - return cachedUint8ArrayMemory0; -} - -function getStringFromWasm0(ptr, len) { - ptr = ptr >>> 0; - return cachedTextDecoder.decode(getUint8ArrayMemory0().subarray(ptr, ptr + len)); -} - -export function exported() { - wasm.exported(); -} - -function __wbg_adapter_8(arg0, arg1) { - wasm._dyn_core__ops__function__FnMut_____Output___R_as_wasm_bindgen__closure__WasmClosure___describe__invoke__h08d04a92620849c7(arg0, arg1); -} - -function __wbg_adapter_9(arg0, arg1) { - wasm.wasm_bindgen__convert__closures__invoke0__h0558a7c8fc0aaaa4(arg0, arg1); -} - -export function __wbg_setInterval_dcabc54c1b7f2a8c(arg0, arg1) { - const ret = setInterval(arg0, arg1 >>> 0); - return ret; -}; - -export function __wbg_takesimmutableclosure_19e990abb85ab0b6(arg0, arg1) { - try { - var state = {a: arg0, b: arg1}; - const cb = () => __wbg_adapter_9(state.a, state.b, ); - takes_immutable_closure(cb); - } finally { - state.a = state.b = 0; - } -}; - -export function __wbg_takesmutableclosure_792efe9eb99346a2(arg0, arg1) { - try { - var state = {a: arg0, b: arg1}; - const cb = () => { - const a = state.a; - state.a = 0; - try { - return __wbg_adapter_8(a, state.b, ); - } finally { - state.a = a; - } - }; - takes_mutable_closure(cb); - } finally { - state.a = state.b = 0; - } -}; - -export function __wbindgen_cb_drop(arg0) { - const obj = arg0.original; - if (obj.cnt-- == 1) { - obj.a = 0; - return true; - } - const ret = false; - return ret; -}; - -export function __wbindgen_closure_wrapper57(arg0, arg1, arg2) { - const ret = makeMutClosure(arg0, arg1, 4, __wbg_adapter_8); - return ret; -}; - -export function __wbindgen_init_externref_table() { - const table = wasm.__wbindgen_export_1; - const offset = table.grow(4); - table.set(0, undefined); - table.set(offset + 0, undefined); - table.set(offset + 1, null); - table.set(offset + 2, true); - table.set(offset + 3, false); - ; -}; - -export function __wbindgen_throw(arg0, arg1) { - throw new Error(getStringFromWasm0(arg0, arg1)); -}; - diff --git a/crates/cli/tests/reference/closure.rs b/crates/cli/tests/reference/closure.rs deleted file mode 100644 index 98479cee726..00000000000 --- a/crates/cli/tests/reference/closure.rs +++ /dev/null @@ -1,20 +0,0 @@ -use wasm_bindgen::prelude::*; - -#[wasm_bindgen] -extern "C" { - // stack - fn takes_immutable_closure(f: &dyn Fn()); - fn takes_mutable_closure(f: &mut dyn FnMut()); - - // heap - fn setInterval(closure: &Closure, millis: u32) -> f64; -} - -#[wasm_bindgen] -pub fn exported() { - takes_immutable_closure(&|| {}); - takes_mutable_closure(&mut || {}); - - let closure = Closure::new(|| {}); - setInterval(&closure, 1000); -} diff --git a/crates/cli/tests/reference/closure.wat b/crates/cli/tests/reference/closure.wat deleted file mode 100644 index caebc285a2f..00000000000 --- a/crates/cli/tests/reference/closure.wat +++ /dev/null @@ -1,20 +0,0 @@ -(module $reference_test.wasm - (type (;0;) (func)) - (type (;1;) (func (param i32 i32))) - (import "./reference_test_bg.js" "__wbindgen_init_externref_table" (func (;0;) (type 0))) - (func $"+Output = R as wasm_bindgen::closure::WasmClosure>::describe::invoke::h08d04a92620849c7" (;1;) (type 1) (param i32 i32)) - (func $wasm_bindgen::convert::closures::invoke0::h0558a7c8fc0aaaa4 (;2;) (type 1) (param i32 i32)) - (func $exported (;3;) (type 0)) - (table (;0;) 37 37 funcref) - (table (;1;) 128 externref) - (memory (;0;) 17) - (export "memory" (memory 0)) - (export "exported" (func $exported)) - (export "__wbindgen_export_0" (table 0)) - (export "__wbindgen_export_1" (table 1)) - (export "_dyn_core__ops__function__FnMut_____Output___R_as_wasm_bindgen__closure__WasmClosure___describe__invoke__h08d04a92620849c7" (func $"+Output = R as wasm_bindgen::closure::WasmClosure>::describe::invoke::h08d04a92620849c7")) - (export "wasm_bindgen__convert__closures__invoke0__h0558a7c8fc0aaaa4" (func $wasm_bindgen::convert::closures::invoke0::h0558a7c8fc0aaaa4)) - (export "__wbindgen_start" (func 0)) - (@custom "target_features" (after code) "\04+\0amultivalue+\0fmutable-globals+\0freference-types+\08sign-ext") -) -