Skip to content

Commit d173a75

Browse files
committed
Account for empty string
1 parent 53a650f commit d173a75

File tree

3 files changed

+15
-3
lines changed

3 files changed

+15
-3
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2123,7 +2123,7 @@ impl<'a> Context<'a> {
21232123
self.global(
21242124
"
21252125
function _assertChar(c) {
2126-
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}`);
2126+
if (typeof(c) === 'number' && (c >= 0x110000 || (c >= 0xD800 && c < 0xE000))) throw new Error(`expected a valid Unicode scalar value, found ${c}`);
21272127
}
21282128
",
21292129
);

tests/wasm/char.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ exports.js_option_identity = a => a;
77
exports.js_works = () => {
88
assert.strictEqual(wasm.letter(), 'a');
99
assert.strictEqual(wasm.face(), '😀');
10+
assert.strictEqual(wasm.rust_identity(''), '\u0000');
1011
assert.strictEqual(wasm.rust_identity('Ղ'), 'Ղ');
1112
assert.strictEqual(wasm.rust_identity('ҝ'), 'ҝ');
1213
assert.strictEqual(wasm.rust_identity('Δ'), 'Δ');
@@ -16,6 +17,12 @@ exports.js_works = () => {
1617
wasm.rust_letter('a');
1718
wasm.rust_face('😀');
1819

19-
assert.throws(() => wasm.rust_js_identity('\uD83D'), /expected a number argument that is a valid Unicode scalar value, found 55357/);
20-
assert.throws(() => wasm.rust_js_option_identity('\uD83D'), /expected a number argument that is a valid Unicode scalar value, found 55357/);
20+
assert.strictEqual(wasm.rust_option_identity(undefined), undefined);
21+
assert.strictEqual(wasm.rust_option_identity(null), undefined);
22+
assert.strictEqual(wasm.rust_option_identity(''), '\u0000');
23+
assert.strictEqual(wasm.rust_option_identity('\u0000'), '\u0000');
24+
25+
assert.throws(() => wasm.rust_identity(55357), /c.codePointAt is not a function/);
26+
assert.throws(() => wasm.rust_identity('\uD83D'), /expected a valid Unicode scalar value, found 55357/);
27+
assert.throws(() => wasm.rust_option_identity('\uD83D'), /expected a valid Unicode scalar value, found 55357/);
2128
};

tests/wasm/char.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@ pub fn rust_identity(c: char) -> char {
1313
c
1414
}
1515

16+
#[wasm_bindgen]
17+
pub fn rust_option_identity(c: Option<char>) -> Option<char> {
18+
c
19+
}
20+
1621
#[wasm_bindgen]
1722
pub fn rust_js_identity(c: char) -> char {
1823
js_identity(c)

0 commit comments

Comments
 (0)