From db5079388c7e6934738c2bace89194ba9d997769 Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Mon, 23 Sep 2019 08:23:21 -0700 Subject: [PATCH] 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 --- crates/backend/src/codegen.rs | 8 ++++++++ crates/cli-support/src/webidl/mod.rs | 7 ++++++- tests/wasm/imports.js | 2 ++ tests/wasm/imports.rs | 8 ++++++++ 4 files changed, 24 insertions(+), 1 deletion(-) diff --git a/crates/backend/src/codegen.rs b/crates/backend/src/codegen.rs index f2a2623a3e9..3bf0bb9abfa 100644 --- a/crates/backend/src/codegen.rs +++ b/crates/backend/src/codegen.rs @@ -1162,6 +1162,14 @@ impl ToTokens for ast::ImportStatic { }; }) .to_tokens(into); + + Descriptor( + &shim_name, + quote! { + <#ty as WasmDescribe>::describe(); + }, + ) + .to_tokens(into); } } diff --git a/crates/cli-support/src/webidl/mod.rs b/crates/cli-support/src/webidl/mod.rs index c18e8900fac..c81ddbd8fdd 100644 --- a/crates/cli-support/src/webidl/mod.rs +++ b/crates/cli-support/src/webidl/mod.rs @@ -1049,6 +1049,11 @@ impl<'a> Context<'a> { None => return Ok(()), }; + let descriptor = match self.descriptors.remove(static_.shim) { + None => return Ok(()), + Some(d) => d, + }; + // Register the signature of this imported shim bindings::register_import( self.module, @@ -1057,7 +1062,7 @@ impl<'a> Context<'a> { Function { arguments: Vec::new(), shim_idx: 0, - ret: Descriptor::Anyref, + ret: descriptor, }, ast::WebidlFunctionKind::Static, )?; diff --git a/tests/wasm/imports.js b/tests/wasm/imports.js index 9163a6ee00e..2628c9400ba 100644 --- a/tests/wasm/imports.js +++ b/tests/wasm/imports.js @@ -105,3 +105,5 @@ exports.assert_dead_import_not_generated = function() { exports.import_inside_function_works = function() {}; exports.import_inside_private_module = function() {}; exports.should_call_undefined_functions = () => false; + +exports.STATIC_STRING = 'x'; diff --git a/tests/wasm/imports.rs b/tests/wasm/imports.rs index eb7a5fa9145..5d596b5af09 100644 --- a/tests/wasm/imports.rs +++ b/tests/wasm/imports.rs @@ -51,6 +51,9 @@ extern "C" { fn unused_import(); fn assert_dead_import_not_generated(); fn should_call_undefined_functions() -> bool; + + + static STATIC_STRING: String; } #[wasm_bindgen] @@ -232,3 +235,8 @@ fn undefined_function_is_ok() { x.method(); x.set_property(x.property()); } + +#[wasm_bindgen_test] +fn static_string_ok() { + assert_eq!(*STATIC_STRING, "x"); +}