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

Avoid errors if vendor-prefixed APIs do not exist (fix #2437) #2438

Merged
merged 1 commit into from
Feb 1, 2021
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
10 changes: 5 additions & 5 deletions crates/cli-support/src/js/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2095,18 +2095,18 @@ impl<'a> Context<'a> {
self.imports_post.push_str(";\n");

fn switch(dst: &mut String, name: &str, prefix: &str, left: &[String]) {
if left.len() == 0 {
dst.push_str(prefix);
return dst.push_str(name);
}
dst.push_str("(typeof ");
dst.push_str(prefix);
dst.push_str(name);
dst.push_str(" !== 'undefined' ? ");
dst.push_str(prefix);
dst.push_str(name);
dst.push_str(" : ");
switch(dst, name, &left[0], &left[1..]);
if left.is_empty() {
dst.push_str("undefined");
} else {
switch(dst, name, &left[0], &left[1..]);
}
dst.push_str(")");
}
format!("l{}", name)
Expand Down
8 changes: 8 additions & 0 deletions tests/wasm/vendor_prefix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,13 @@ extern "C" {
fn new() -> MySpecialApi3;
#[wasm_bindgen(method)]
fn foo(this: &MySpecialApi3) -> u32;

// This API does not exist at all;
// test that Rust gets a chance to catch the error (#2437)
#[wasm_bindgen(vendor_prefix = a, vendor_prefix = b)]
type MyMissingApi;
#[wasm_bindgen(constructor, catch)]
fn new() -> Result<MyMissingApi, JsValue>;
}

#[wasm_bindgen_test]
Expand All @@ -37,4 +44,5 @@ pub fn polyfill_works() {
assert_eq!(MySpecialApi::new().foo(), 123);
assert_eq!(MySpecialApi2::new().foo(), 124);
assert_eq!(MySpecialApi3::new().foo(), 125);
assert!(MyMissingApi::new().is_err());
}