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 03d0533
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 9 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
12 changes: 9 additions & 3 deletions tests/wasm/char.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ const wasm = require('wasm-bindgen-test.js');
const assert = require('assert');

exports.js_identity = a => a;
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 +16,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/);
};
9 changes: 4 additions & 5 deletions tests/wasm/char.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ use wasm_bindgen_test::*;
#[wasm_bindgen(module = "tests/wasm/char.js")]
extern "C" {
fn js_identity(c: char) -> char;
fn js_option_identity(c: Option<char>) -> Option<char>;
fn js_works();
}

Expand All @@ -14,13 +13,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 03d0533

Please sign in to comment.