Skip to content

Commit

Permalink
Add let binding
Browse files Browse the repository at this point in the history
  • Loading branch information
RunDevelopment committed Dec 9, 2024
1 parent 5572877 commit 51792da
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 28 deletions.
23 changes: 17 additions & 6 deletions crates/cli-support/src/js/binding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
///
Expand Down Expand Up @@ -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 => {
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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}) {{
Expand Down Expand Up @@ -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!(
Expand Down
40 changes: 20 additions & 20 deletions crates/cli/tests/reference/echo.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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();
Expand All @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions crates/cli/tests/reference/result.js
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
Expand Down

0 comments on commit 51792da

Please sign in to comment.