mirror of
https://github.com/fluencelabs/wasm-bindgen
synced 2025-04-25 14:12:13 +00:00
Add a debug assert and more tests
This commit is contained in:
parent
0c681ee2ba
commit
15defcfd3a
@ -1499,12 +1499,19 @@ impl<'a> Context<'a> {
|
|||||||
arg = arg.slice(offset);
|
arg = arg.slice(offset);
|
||||||
ptr = wasm.__wbindgen_realloc(ptr, size, size = offset + arg.length * 3);
|
ptr = wasm.__wbindgen_realloc(ptr, size, size = offset + arg.length * 3);
|
||||||
const view = getUint8Memory().subarray(ptr + offset, ptr + size);
|
const view = getUint8Memory().subarray(ptr + offset, ptr + size);
|
||||||
|
const ret = cachedTextEncoder.encodeInto(arg, view);
|
||||||
|
{}
|
||||||
offset += cachedTextEncoder.encodeInto(arg, view).written;
|
offset += cachedTextEncoder.encodeInto(arg, view).written;
|
||||||
}}
|
}}
|
||||||
WASM_VECTOR_LEN = offset;
|
WASM_VECTOR_LEN = offset;
|
||||||
return ptr;
|
return ptr;
|
||||||
",
|
",
|
||||||
start_encoding_as_ascii
|
start_encoding_as_ascii,
|
||||||
|
if self.config.debug {
|
||||||
|
"if (ret.read != arg.length) throw new Error('failed to pass whole string');"
|
||||||
|
} else {
|
||||||
|
""
|
||||||
|
},
|
||||||
);
|
);
|
||||||
|
|
||||||
// Looks like `encodeInto` doesn't currently work when the memory passed
|
// Looks like `encodeInto` doesn't currently work when the memory passed
|
||||||
|
@ -50,3 +50,4 @@ pub fn import_export_same_name() {
|
|||||||
pub mod snippets;
|
pub mod snippets;
|
||||||
pub mod modules;
|
pub mod modules;
|
||||||
pub mod anyref_heap_live_count;
|
pub mod anyref_heap_live_count;
|
||||||
|
pub mod strings;
|
||||||
|
15
tests/headless/strings.js
Normal file
15
tests/headless/strings.js
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
export function test_string_roundtrip(f) {
|
||||||
|
const test = expected => {
|
||||||
|
const actual = f(expected);
|
||||||
|
if (actual === expected)
|
||||||
|
return;
|
||||||
|
throw new Error(`string roundtrip "${actual}" != "${expected}"`);
|
||||||
|
};
|
||||||
|
|
||||||
|
test('');
|
||||||
|
test('a');
|
||||||
|
test('💖');
|
||||||
|
|
||||||
|
test('a longer string');
|
||||||
|
test('a longer 💖 string');
|
||||||
|
}
|
12
tests/headless/strings.rs
Normal file
12
tests/headless/strings.rs
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
use wasm_bindgen::prelude::*;
|
||||||
|
use wasm_bindgen_test::*;
|
||||||
|
|
||||||
|
#[wasm_bindgen(module = "/tests/headless/strings.js")]
|
||||||
|
extern "C" {
|
||||||
|
fn test_string_roundtrip(c: &Closure<Fn(String) -> String>);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[wasm_bindgen_test]
|
||||||
|
fn string_roundtrip() {
|
||||||
|
test_string_roundtrip(&Closure::wrap(Box::new(|s| s)));
|
||||||
|
}
|
@ -92,3 +92,16 @@ exports.RenamedInRust = class {};
|
|||||||
exports.new_renamed = () => new exports.RenamedInRust;
|
exports.new_renamed = () => new exports.RenamedInRust;
|
||||||
|
|
||||||
exports.import_export_same_name = () => {};
|
exports.import_export_same_name = () => {};
|
||||||
|
|
||||||
|
exports.test_string_roundtrip = () => {
|
||||||
|
const test = s => {
|
||||||
|
assert.strictEqual(wasm.do_string_roundtrip(s), s);
|
||||||
|
};
|
||||||
|
|
||||||
|
test('');
|
||||||
|
test('a');
|
||||||
|
test('💖');
|
||||||
|
|
||||||
|
test('a longer string');
|
||||||
|
test('a longer 💖 string');
|
||||||
|
};
|
||||||
|
@ -27,6 +27,8 @@ extern "C" {
|
|||||||
#[wasm_bindgen(js_name = RenamedInRust)]
|
#[wasm_bindgen(js_name = RenamedInRust)]
|
||||||
type Renamed;
|
type Renamed;
|
||||||
fn new_renamed() -> Renamed;
|
fn new_renamed() -> Renamed;
|
||||||
|
|
||||||
|
fn test_string_roundtrip();
|
||||||
}
|
}
|
||||||
|
|
||||||
#[wasm_bindgen_test]
|
#[wasm_bindgen_test]
|
||||||
@ -201,3 +203,13 @@ fn renaming_imports_and_instanceof() {
|
|||||||
pub fn import_export_same_name() {
|
pub fn import_export_same_name() {
|
||||||
js_import_export_same_name();
|
js_import_export_same_name();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[wasm_bindgen_test]
|
||||||
|
fn string_roundtrip() {
|
||||||
|
test_string_roundtrip();
|
||||||
|
}
|
||||||
|
|
||||||
|
#[wasm_bindgen]
|
||||||
|
pub fn do_string_roundtrip(s: String) -> String {
|
||||||
|
s
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user