diff --git a/crates/cli-support/src/js/mod.rs b/crates/cli-support/src/js/mod.rs index 618b4282dc9..35bba115fdc 100644 --- a/crates/cli-support/src/js/mod.rs +++ b/crates/cli-support/src/js/mod.rs @@ -2123,7 +2123,7 @@ impl<'a> Context<'a> { self.global( " function _assertChar(c) { - if (typeof(c) !== 'number' || c >= 0x110000 || (c >= 0xD800 && c < 0xE000)) throw new Error(`expected a number argument that is a valid Unicode scalar value, found ${c}`); + if (typeof(c) === 'number' && (c >= 0x110000 || (c >= 0xD800 && c < 0xE000))) throw new Error(`expected a valid Unicode scalar value, found ${c}`); } ", ); diff --git a/tests/wasm/char.js b/tests/wasm/char.js index c9e676023e0..a1e7d83e4cd 100644 --- a/tests/wasm/char.js +++ b/tests/wasm/char.js @@ -7,6 +7,7 @@ exports.js_option_identity = a => a; exports.js_works = () => { assert.strictEqual(wasm.letter(), 'a'); assert.strictEqual(wasm.face(), '😀'); + assert.strictEqual(wasm.rust_identity(''), '\u0000'); assert.strictEqual(wasm.rust_identity('Ղ'), 'Ղ'); assert.strictEqual(wasm.rust_identity('ҝ'), 'ҝ'); assert.strictEqual(wasm.rust_identity('Δ'), 'Δ'); @@ -16,6 +17,12 @@ exports.js_works = () => { wasm.rust_letter('a'); wasm.rust_face('😀'); - assert.throws(() => wasm.rust_js_identity('\uD83D'), /expected a number argument that is a valid Unicode scalar value, found 55357/); - assert.throws(() => wasm.rust_js_option_identity('\uD83D'), /expected a number argument that is a valid Unicode scalar value, found 55357/); + assert.strictEqual(wasm.rust_option_identity(undefined), undefined); + assert.strictEqual(wasm.rust_option_identity(null), undefined); + assert.strictEqual(wasm.rust_option_identity(''), '\u0000'); + assert.strictEqual(wasm.rust_option_identity('\u0000'), '\u0000'); + + assert.throws(() => wasm.rust_identity(55357), /c.codePointAt is not a function/); + assert.throws(() => wasm.rust_identity('\uD83D'), /expected a valid Unicode scalar value, found 55357/); + assert.throws(() => wasm.rust_option_identity('\uD83D'), /expected a valid Unicode scalar value, found 55357/); }; diff --git a/tests/wasm/char.rs b/tests/wasm/char.rs index b23dd94306d..d942b0ff5af 100644 --- a/tests/wasm/char.rs +++ b/tests/wasm/char.rs @@ -13,6 +13,11 @@ pub fn rust_identity(c: char) -> char { c } +#[wasm_bindgen] +pub fn rust_option_identity(c: Option) -> Option { + c +} + #[wasm_bindgen] pub fn rust_js_identity(c: char) -> char { js_identity(c)