Skip to content

Commit

Permalink
Fix namespaced statics
Browse files Browse the repository at this point in the history
  • Loading branch information
daxpedda committed Dec 5, 2024
1 parent 92a7912 commit ee020b9
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 3 deletions.
8 changes: 7 additions & 1 deletion crates/cli-support/src/js/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
30 changes: 28 additions & 2 deletions crates/cli/tests/reference/static.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
};
Expand All @@ -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);
};
Expand Down
6 changes: 6 additions & 0 deletions crates/cli/tests/reference/static.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ extern "C" {
static NAMESPACE_PLAIN: JsValue;
#[wasm_bindgen(thread_local_v2, js_namespace = test)]
static NAMESPACE_OPTIONAL: Option<Number>;
#[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<Number>;
}

#[wasm_bindgen]
Expand All @@ -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);
}
16 changes: 16 additions & 0 deletions tests/wasm/imports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<u32>;

#[wasm_bindgen(thread_local_v2, js_namespace = test)]
static UNDECLARED_NAMESPACE: Option<u32>;

#[wasm_bindgen(thread_local_v2, js_namespace = ["test1", "test2"])]
static UNDECLARED_NESTED_NAMESPACE: Option<u32>;
}

#[wasm_bindgen(module = "tests/wasm/imports_2.js")]
Expand Down Expand Up @@ -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);
}

0 comments on commit ee020b9

Please sign in to comment.