Skip to content

Commit 920494c

Browse files
authored
Avoid errors if vendor-prefixed APIs do not exist (#2438)
Avoid errors in the generated vendor-prefix code if neither the standard API nor the vendor-prefixed API exists. Instead, fall back to undefined, which will give the Rust module a chance to run and handle the error when it tries to use the API.
1 parent 743135f commit 920494c

File tree

2 files changed

+13
-5
lines changed

2 files changed

+13
-5
lines changed

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2095,18 +2095,18 @@ impl<'a> Context<'a> {
20952095
self.imports_post.push_str(";\n");
20962096

20972097
fn switch(dst: &mut String, name: &str, prefix: &str, left: &[String]) {
2098-
if left.len() == 0 {
2099-
dst.push_str(prefix);
2100-
return dst.push_str(name);
2101-
}
21022098
dst.push_str("(typeof ");
21032099
dst.push_str(prefix);
21042100
dst.push_str(name);
21052101
dst.push_str(" !== 'undefined' ? ");
21062102
dst.push_str(prefix);
21072103
dst.push_str(name);
21082104
dst.push_str(" : ");
2109-
switch(dst, name, &left[0], &left[1..]);
2105+
if left.is_empty() {
2106+
dst.push_str("undefined");
2107+
} else {
2108+
switch(dst, name, &left[0], &left[1..]);
2109+
}
21102110
dst.push_str(")");
21112111
}
21122112
format!("l{}", name)

tests/wasm/vendor_prefix.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,13 @@ extern "C" {
2828
fn new() -> MySpecialApi3;
2929
#[wasm_bindgen(method)]
3030
fn foo(this: &MySpecialApi3) -> u32;
31+
32+
// This API does not exist at all;
33+
// test that Rust gets a chance to catch the error (#2437)
34+
#[wasm_bindgen(vendor_prefix = a, vendor_prefix = b)]
35+
type MyMissingApi;
36+
#[wasm_bindgen(constructor, catch)]
37+
fn new() -> Result<MyMissingApi, JsValue>;
3138
}
3239

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

0 commit comments

Comments
 (0)