Skip to content

Commit

Permalink
Fix importing static values of non-JS types
Browse files Browse the repository at this point in the history
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
  • Loading branch information
alexcrichton committed Sep 25, 2019
1 parent e809a45 commit 0afb6aa
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 1 deletion.
8 changes: 8 additions & 0 deletions crates/backend/src/codegen.rs
Expand Up @@ -1162,6 +1162,14 @@ impl ToTokens for ast::ImportStatic {
};
})
.to_tokens(into);

Descriptor(
&shim_name,
quote! {
<#ty as WasmDescribe>::describe();
},
)
.to_tokens(into);
}
}

Expand Down
7 changes: 6 additions & 1 deletion crates/cli-support/src/webidl/mod.rs
Expand Up @@ -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,
Expand All @@ -1057,7 +1062,7 @@ impl<'a> Context<'a> {
Function {
arguments: Vec::new(),
shim_idx: 0,
ret: Descriptor::Anyref,
ret: descriptor,
},
ast::WebidlFunctionKind::Static,
)?;
Expand Down
2 changes: 2 additions & 0 deletions tests/wasm/imports.js
Expand Up @@ -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';
8 changes: 8 additions & 0 deletions tests/wasm/imports.rs
Expand Up @@ -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]
Expand Down Expand Up @@ -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");
}

0 comments on commit 0afb6aa

Please sign in to comment.