diff --git a/crates/cli-support/src/js/mod.rs b/crates/cli-support/src/js/mod.rs index f08bd05190a..1a4f904bf76 100644 --- a/crates/cli-support/src/js/mod.rs +++ b/crates/cli-support/src/js/mod.rs @@ -3274,7 +3274,13 @@ __wbg_set_wasm(wasm);" if *optional { writeln!( prelude, - "const result = typeof {js} === 'undefined' ? null : {js};" + "\ + let result; + try {{ + result = {js}; + }} catch (_) {{ + result = null; + }}", ) .unwrap(); Ok("result".to_owned()) diff --git a/crates/cli/tests/reference/static.js b/crates/cli/tests/reference/static.js index 1f9b56f3b69..86549f8bb6d 100644 --- a/crates/cli/tests/reference/static.js +++ b/crates/cli/tests/reference/static.js @@ -39,7 +39,12 @@ export function exported() { } export function __wbg_static_accessor_NAMESPACE_OPTIONAL_c9a4344c544120f4() { - const result = typeof test.NAMESPACE_OPTIONAL === 'undefined' ? null : test.NAMESPACE_OPTIONAL; + let result; + try { + result = test.NAMESPACE_OPTIONAL; + } catch (_) { + result = null; + } const ret = result; return isLikeNone(ret) ? 0 : addToExternrefTable0(ret); }; @@ -49,8 +54,29 @@ export function __wbg_static_accessor_NAMESPACE_PLAIN_784c8d7f5bbac62a() { return ret; }; +export function __wbg_static_accessor_NESTED_NAMESPACE_OPTIONAL_a414abbeb018a35a() { + let result; + try { + result = test1.test2.NESTED_NAMESPACE_OPTIONAL; + } catch (_) { + result = null; + } + const ret = result; + return isLikeNone(ret) ? 0 : addToExternrefTable0(ret); +}; + +export function __wbg_static_accessor_NESTED_NAMESPACE_PLAIN_1121b285cb8479df() { + const ret = test1.test2.NESTED_NAMESPACE_PLAIN; + return ret; +}; + export function __wbg_static_accessor_OPTIONAL_ade71b6402851d0c() { - const result = typeof OPTIONAL === 'undefined' ? null : OPTIONAL; + let result; + try { + result = OPTIONAL; + } catch (_) { + result = null; + } const ret = result; return isLikeNone(ret) ? 0 : addToExternrefTable0(ret); }; diff --git a/crates/cli/tests/reference/static.rs b/crates/cli/tests/reference/static.rs index ea6b37cbc22..4437f550e7d 100644 --- a/crates/cli/tests/reference/static.rs +++ b/crates/cli/tests/reference/static.rs @@ -13,6 +13,10 @@ extern "C" { static NAMESPACE_PLAIN: JsValue; #[wasm_bindgen(thread_local_v2, js_namespace = test)] static NAMESPACE_OPTIONAL: Option; + #[wasm_bindgen(thread_local_v2, js_namespace = ["test1", "test2"])] + static NESTED_NAMESPACE_PLAIN: JsValue; + #[wasm_bindgen(thread_local_v2, js_namespace = ["test1", "test2"])] + static NESTED_NAMESPACE_OPTIONAL: Option; } #[wasm_bindgen] @@ -21,4 +25,6 @@ pub fn exported() { let _ = OPTIONAL.with(Option::clone); let _ = NAMESPACE_PLAIN.with(JsValue::clone); let _ = NAMESPACE_OPTIONAL.with(Option::clone); + let _ = NESTED_NAMESPACE_PLAIN.with(JsValue::clone); + let _ = NESTED_NAMESPACE_OPTIONAL.with(Option::clone); } diff --git a/tests/wasm/imports.rs b/tests/wasm/imports.rs index a6eab0c5e32..119aefef487 100644 --- a/tests/wasm/imports.rs +++ b/tests/wasm/imports.rs @@ -90,6 +90,15 @@ extern "C" { #[wasm_bindgen(js_name = "\"string'literal\nbreakers\r")] fn string_literal_breakers() -> u32; + + #[wasm_bindgen(thread_local_v2)] + static UNDECLARED: Option; + + #[wasm_bindgen(thread_local_v2, js_namespace = test)] + static UNDECLARED_NAMESPACE: Option; + + #[wasm_bindgen(thread_local_v2, js_namespace = ["test1", "test2"])] + static UNDECLARED_NESTED_NAMESPACE: Option; } #[wasm_bindgen(module = "tests/wasm/imports_2.js")] @@ -336,3 +345,10 @@ fn invalid_idents() { assert_eq!(kebab_case(), 42); assert_eq!(string_literal_breakers(), 42); } + +#[wasm_bindgen_test] +fn undeclared() { + assert_eq!(UNDECLARED.with(Option::clone), None); + assert_eq!(UNDECLARED_NAMESPACE.with(Option::clone), None); + assert_eq!(UNDECLARED_NESTED_NAMESPACE.with(Option::clone), None); +}