Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added "type found" msg to expose_assert_* fns #3766 #3787

Merged
merged 1 commit into from
Jan 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions crates/cli-support/src/js/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1147,7 +1147,7 @@ impl<'a> Context<'a> {
self.global(
"
function _assertNum(n) {
if (typeof(n) !== 'number') throw new Error('expected a number argument');
if (typeof(n) !== 'number') throw new Error(`expected a number argument, found ${typeof(n)}`);
}
",
);
Expand All @@ -1160,7 +1160,7 @@ impl<'a> Context<'a> {
self.global(
"
function _assertBigInt(n) {
if (typeof(n) !== 'bigint') throw new Error('expected a bigint argument');
if (typeof(n) !== 'bigint') throw new Error(`expected a bigint argument, found ${typeof(n)}`);
}
",
);
Expand All @@ -1174,7 +1174,7 @@ impl<'a> Context<'a> {
"
function _assertBoolean(n) {
if (typeof(n) !== 'boolean') {
throw new Error('expected a boolean argument');
throw new Error(`expected a boolean argument, found ${typeof(n)}`);
}
}
",
Expand All @@ -1193,7 +1193,7 @@ impl<'a> Context<'a> {

let debug = if self.config.debug {
"
if (typeof(arg) !== 'string') throw new Error('expected a string argument');
if (typeof(arg) !== 'string') throw new Error(`expected a string argument, found ${typeof(arg)}`);
"
} else {
""
Expand Down
5 changes: 5 additions & 0 deletions tests/wasm/simple.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ exports.test_wrong_types = function() {
return;
assert.throws(() => wasm.simple_int('a'), /expected a number argument/);
assert.throws(() => wasm.simple_str(3), /expected a string argument/);
assert.throws(() => wasm.simple_bool('a'), /expected a boolean argument/);

assert.doesNotThrow(() => wasm.simple_int(1));
assert.doesNotThrow(() => wasm.simple_str('a'));
assert.doesNotThrow(() => wasm.simple_bool(true));
};

exports.test_other_exports_still_available = function() {
Expand Down
3 changes: 3 additions & 0 deletions tests/wasm/simple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,9 @@ fn wrong_types() {
#[wasm_bindgen]
pub fn simple_int(_a: u32) {}

#[wasm_bindgen]
pub fn simple_bool(_a: bool) {}

#[wasm_bindgen]
pub fn simple_str(_a: &str) {}

Expand Down