Skip to content

Commit 0afb6aa

Browse files
committed
Fix importing static values of non-JS types
This hasn't ever actually worked in `wasm-bindgen` but there's been enough refactorings since the initial implementation that it's actually quite trivial to implement now! Closes #1777
1 parent e809a45 commit 0afb6aa

File tree

4 files changed

+24
-1
lines changed

4 files changed

+24
-1
lines changed

crates/backend/src/codegen.rs

+8
Original file line numberDiff line numberDiff line change
@@ -1162,6 +1162,14 @@ impl ToTokens for ast::ImportStatic {
11621162
};
11631163
})
11641164
.to_tokens(into);
1165+
1166+
Descriptor(
1167+
&shim_name,
1168+
quote! {
1169+
<#ty as WasmDescribe>::describe();
1170+
},
1171+
)
1172+
.to_tokens(into);
11651173
}
11661174
}
11671175

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

+6-1
Original file line numberDiff line numberDiff line change
@@ -1049,6 +1049,11 @@ impl<'a> Context<'a> {
10491049
None => return Ok(()),
10501050
};
10511051

1052+
let descriptor = match self.descriptors.remove(static_.shim) {
1053+
None => return Ok(()),
1054+
Some(d) => d,
1055+
};
1056+
10521057
// Register the signature of this imported shim
10531058
bindings::register_import(
10541059
self.module,
@@ -1057,7 +1062,7 @@ impl<'a> Context<'a> {
10571062
Function {
10581063
arguments: Vec::new(),
10591064
shim_idx: 0,
1060-
ret: Descriptor::Anyref,
1065+
ret: descriptor,
10611066
},
10621067
ast::WebidlFunctionKind::Static,
10631068
)?;

tests/wasm/imports.js

+2
Original file line numberDiff line numberDiff line change
@@ -105,3 +105,5 @@ exports.assert_dead_import_not_generated = function() {
105105
exports.import_inside_function_works = function() {};
106106
exports.import_inside_private_module = function() {};
107107
exports.should_call_undefined_functions = () => false;
108+
109+
exports.STATIC_STRING = 'x';

tests/wasm/imports.rs

+8
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,9 @@ extern "C" {
5151
fn unused_import();
5252
fn assert_dead_import_not_generated();
5353
fn should_call_undefined_functions() -> bool;
54+
55+
56+
static STATIC_STRING: String;
5457
}
5558

5659
#[wasm_bindgen]
@@ -232,3 +235,8 @@ fn undefined_function_is_ok() {
232235
x.method();
233236
x.set_property(x.property());
234237
}
238+
239+
#[wasm_bindgen_test]
240+
fn static_string_ok() {
241+
assert_eq!(*STATIC_STRING, "x");
242+
}

0 commit comments

Comments
 (0)