Skip to content

Commit 15defcf

Browse files
committed
Add a debug assert and more tests
1 parent 0c681ee commit 15defcf

File tree

6 files changed

+61
-1
lines changed

6 files changed

+61
-1
lines changed

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1499,12 +1499,19 @@ impl<'a> Context<'a> {
14991499
arg = arg.slice(offset);
15001500
ptr = wasm.__wbindgen_realloc(ptr, size, size = offset + arg.length * 3);
15011501
const view = getUint8Memory().subarray(ptr + offset, ptr + size);
1502+
const ret = cachedTextEncoder.encodeInto(arg, view);
1503+
{}
15021504
offset += cachedTextEncoder.encodeInto(arg, view).written;
15031505
}}
15041506
WASM_VECTOR_LEN = offset;
15051507
return ptr;
15061508
",
1507-
start_encoding_as_ascii
1509+
start_encoding_as_ascii,
1510+
if self.config.debug {
1511+
"if (ret.read != arg.length) throw new Error('failed to pass whole string');"
1512+
} else {
1513+
""
1514+
},
15081515
);
15091516

15101517
// Looks like `encodeInto` doesn't currently work when the memory passed

tests/headless/main.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,3 +50,4 @@ pub fn import_export_same_name() {
5050
pub mod snippets;
5151
pub mod modules;
5252
pub mod anyref_heap_live_count;
53+
pub mod strings;

tests/headless/strings.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
export function test_string_roundtrip(f) {
2+
const test = expected => {
3+
const actual = f(expected);
4+
if (actual === expected)
5+
return;
6+
throw new Error(`string roundtrip "${actual}" != "${expected}"`);
7+
};
8+
9+
test('');
10+
test('a');
11+
test('💖');
12+
13+
test('a longer string');
14+
test('a longer 💖 string');
15+
}

tests/headless/strings.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
use wasm_bindgen::prelude::*;
2+
use wasm_bindgen_test::*;
3+
4+
#[wasm_bindgen(module = "/tests/headless/strings.js")]
5+
extern "C" {
6+
fn test_string_roundtrip(c: &Closure<Fn(String) -> String>);
7+
}
8+
9+
#[wasm_bindgen_test]
10+
fn string_roundtrip() {
11+
test_string_roundtrip(&Closure::wrap(Box::new(|s| s)));
12+
}

tests/wasm/simple.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,3 +92,16 @@ exports.RenamedInRust = class {};
9292
exports.new_renamed = () => new exports.RenamedInRust;
9393

9494
exports.import_export_same_name = () => {};
95+
96+
exports.test_string_roundtrip = () => {
97+
const test = s => {
98+
assert.strictEqual(wasm.do_string_roundtrip(s), s);
99+
};
100+
101+
test('');
102+
test('a');
103+
test('💖');
104+
105+
test('a longer string');
106+
test('a longer 💖 string');
107+
};

tests/wasm/simple.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ extern "C" {
2727
#[wasm_bindgen(js_name = RenamedInRust)]
2828
type Renamed;
2929
fn new_renamed() -> Renamed;
30+
31+
fn test_string_roundtrip();
3032
}
3133

3234
#[wasm_bindgen_test]
@@ -201,3 +203,13 @@ fn renaming_imports_and_instanceof() {
201203
pub fn import_export_same_name() {
202204
js_import_export_same_name();
203205
}
206+
207+
#[wasm_bindgen_test]
208+
fn string_roundtrip() {
209+
test_string_roundtrip();
210+
}
211+
212+
#[wasm_bindgen]
213+
pub fn do_string_roundtrip(s: String) -> String {
214+
s
215+
}

0 commit comments

Comments
 (0)