Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improved variable names in generated JS #4280

Open
wants to merge 14 commits into
base: main
Choose a base branch
from
334 changes: 170 additions & 164 deletions crates/cli-support/src/js/binding.rs

Large diffs are not rendered by default.

460 changes: 229 additions & 231 deletions crates/cli/tests/reference/echo.js

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions crates/cli/tests/reference/getter-setter.js
Original file line number Diff line number Diff line change
Expand Up @@ -170,9 +170,9 @@ export class Foo {
* @param {string | null} [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
Expand Down
16 changes: 7 additions & 9 deletions crates/cli/tests/reference/result.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,21 +33,19 @@ function takeFromExternrefTable0(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];
let ptr = ret[0];
let len = ret[1];
if (ret[3]) {
ptr1 = 0; len1 = 0;
ptr = 0; len = 0;
throw takeFromExternrefTable0(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);
}
}

Expand Down
2 changes: 2 additions & 0 deletions crates/cli/tests/reference/string-arg.d.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
/* tslint:disable */
/* eslint-disable */
export function foo(a: string): void;
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;
48 changes: 45 additions & 3 deletions crates/cli/tests/reference/string-arg.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,51 @@ 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
* @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
* @param {number} ptr
* @param {number} len
* @returns {string}
*/
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.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]);
} finally {
wasm.__wbindgen_free(deferred0, deferred1, 1);
}
}

export function __wbindgen_init_externref_table() {
Expand Down
10 changes: 10 additions & 0 deletions crates/cli/tests/reference/string-arg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,13 @@ use wasm_bindgen::prelude::*;
pub fn foo(a: &str) {
drop(a);
}

#[wasm_bindgen]
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
}
13 changes: 11 additions & 2 deletions crates/cli/tests/reference/string-arg.wat
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,27 @@
(type (;0;) (func))
(type (;1;) (func (param i32 i32)))
(type (;2;) (func (param i32 i32) (result i32)))
(type (;3;) (func (param i32 i32 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) (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 3) (param i32 i32 i32 i32) (result i32))
(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 $"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 "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))
(export "__wbindgen_free" (func $__wbindgen_free))
(export "__wbindgen_start" (func 0))
(@custom "target_features" (after code) "\04+\0amultivalue+\0fmutable-globals+\0freference-types+\08sign-ext")
)
Expand Down
14 changes: 6 additions & 8 deletions crates/cli/tests/reference/wasm-export-types.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand Down
8 changes: 4 additions & 4 deletions crates/cli/tests/reference/web-sys.js
Original file line number Diff line number Diff line change
Expand Up @@ -195,10 +195,10 @@ export function __wbg_new_17f755666e48d1d8() { return handleError(function (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() {
Expand Down
Loading