Skip to content

Commit

Permalink
Account for empty string
Browse files Browse the repository at this point in the history
  • Loading branch information
daxpedda committed Mar 3, 2024
1 parent 53a650f commit 6d32878
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 7 deletions.
2 changes: 1 addition & 1 deletion crates/cli-support/src/js/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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}`);
}
",
);
Expand Down
11 changes: 9 additions & 2 deletions tests/wasm/char.js
Original file line number Diff line number Diff line change
Expand Up @@ -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('Δ'), 'Δ');
Expand All @@ -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/);
};
8 changes: 4 additions & 4 deletions tests/wasm/char.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ pub fn rust_identity(c: char) -> char {
}

#[wasm_bindgen]
pub fn rust_js_identity(c: char) -> char {
js_identity(c)
pub fn rust_option_identity(c: Option<char>) -> Option<char> {
c
}

#[wasm_bindgen]
pub fn rust_js_option_identity(c: Option<char>) -> Option<char> {
js_option_identity(c)
pub fn rust_js_identity(c: char) -> char {
js_identity(c)
}

#[wasm_bindgen]
Expand Down

0 comments on commit 6d32878

Please sign in to comment.