Skip to content

Commit dff35ce

Browse files
committed
Handle potentially throwing getters
1 parent e4f1004 commit dff35ce

File tree

3 files changed

+30
-42
lines changed

3 files changed

+30
-42
lines changed

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

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2578,6 +2578,30 @@ __wbg_set_wasm(wasm);"
25782578
Ok(name)
25792579
}
25802580

2581+
fn import_static(&mut self, import: &JsImport, optional: bool) -> Result<String, Error> {
2582+
let mut name = self.import_name(&JsImport {
2583+
name: import.name.clone(),
2584+
fields: Vec::new(),
2585+
})?;
2586+
2587+
// After we've got an actual name handle field projections
2588+
if optional {
2589+
name = format!("typeof {name} === 'undefined' ? null: {name}");
2590+
2591+
for field in import.fields.iter() {
2592+
name.push_str("?.");
2593+
name.push_str(field);
2594+
}
2595+
} else {
2596+
for field in import.fields.iter() {
2597+
name.push('.');
2598+
name.push_str(field);
2599+
}
2600+
}
2601+
2602+
Ok(name)
2603+
}
2604+
25812605
/// If a start function is present, it removes it from the `start` section
25822606
/// of the Wasm module and then moves it to an exported function, named
25832607
/// `__wbindgen_start`.
@@ -3269,24 +3293,7 @@ __wbg_set_wasm(wasm);"
32693293
assert!(kind == AdapterJsImportKind::Normal);
32703294
assert!(!variadic);
32713295
assert_eq!(args.len(), 0);
3272-
let js = self.import_name(js)?;
3273-
3274-
if *optional {
3275-
writeln!(
3276-
prelude,
3277-
"\
3278-
let result;
3279-
try {{
3280-
result = {js};
3281-
}} catch (_) {{
3282-
result = null;
3283-
}}",
3284-
)
3285-
.unwrap();
3286-
Ok("result".to_owned())
3287-
} else {
3288-
Ok(js)
3289-
}
3296+
self.import_static(js, *optional)
32903297
}
32913298

32923299
AuxImport::String(string) => {

crates/cli/tests/reference/static.js

Lines changed: 3 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,7 @@ export function exported() {
3939
}
4040

4141
export function __wbg_static_accessor_NAMESPACE_OPTIONAL_c9a4344c544120f4() {
42-
let result;
43-
try {
44-
result = test.NAMESPACE_OPTIONAL;
45-
} catch (_) {
46-
result = null;
47-
}
48-
const ret = result;
42+
const ret = typeof test === 'undefined' ? null: test?.NAMESPACE_OPTIONAL;
4943
return isLikeNone(ret) ? 0 : addToExternrefTable0(ret);
5044
};
5145

@@ -55,13 +49,7 @@ export function __wbg_static_accessor_NAMESPACE_PLAIN_784c8d7f5bbac62a() {
5549
};
5650

5751
export function __wbg_static_accessor_NESTED_NAMESPACE_OPTIONAL_a414abbeb018a35a() {
58-
let result;
59-
try {
60-
result = test1.test2.NESTED_NAMESPACE_OPTIONAL;
61-
} catch (_) {
62-
result = null;
63-
}
64-
const ret = result;
52+
const ret = typeof test1 === 'undefined' ? null: test1?.test2?.NESTED_NAMESPACE_OPTIONAL;
6553
return isLikeNone(ret) ? 0 : addToExternrefTable0(ret);
6654
};
6755

@@ -71,13 +59,7 @@ export function __wbg_static_accessor_NESTED_NAMESPACE_PLAIN_1121b285cb8479df()
7159
};
7260

7361
export function __wbg_static_accessor_OPTIONAL_ade71b6402851d0c() {
74-
let result;
75-
try {
76-
result = OPTIONAL;
77-
} catch (_) {
78-
result = null;
79-
}
80-
const ret = result;
62+
const ret = typeof OPTIONAL === 'undefined' ? null: OPTIONAL;
8163
return isLikeNone(ret) ? 0 : addToExternrefTable0(ret);
8264
};
8365

guide/src/reference/static-js-objects.md

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,8 @@ a named `static` in the `extern` block with an
77
`JsThreadLocal` for these objects, which can be cloned into a `JsValue`.
88

99
These values are cached in a thread-local and are meant to bind static values
10-
or objects only. Binding getters or expecting a new value or object when
11-
changed in JS is not supported. For these use
12-
[getters](attributes/on-js-imports/getter-and-setter.md).
10+
or objects only. For getters which can change their return value or throw see
11+
[how to import getters](attributes/on-js-imports/getter-and-setter.md).
1312

1413
For example, given the following JavaScript:
1514

0 commit comments

Comments
 (0)