Merge pull request #741 from alexcrichton/duplicate-statics

Support importing same-name statics from two modules
This commit is contained in:
Alex Crichton 2018-08-20 11:37:21 -07:00 committed by GitHub
commit 86d1ab513b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 18 additions and 8 deletions

View File

@ -560,10 +560,12 @@ impl ConvertToAst<BindgenAttrs> for syn::ForeignItemType {
}
}
impl ConvertToAst<BindgenAttrs> for syn::ForeignItemStatic {
impl<'a> ConvertToAst<(BindgenAttrs, &'a Option<String>)> for syn::ForeignItemStatic {
type Target = ast::ImportKind;
fn convert(self, opts: BindgenAttrs) -> Result<Self::Target, Diagnostic> {
fn convert(self, (opts, module): (BindgenAttrs, &'a Option<String>))
-> Result<Self::Target, Diagnostic>
{
if self.mutability.is_some() {
bail_span!(self.mutability, "cannot import mutable globals yet")
}
@ -571,11 +573,8 @@ impl ConvertToAst<BindgenAttrs> for syn::ForeignItemStatic {
let js_name = opts.js_name().unwrap_or(&default_name);
let shim = format!(
"__wbg_static_accessor_{}_{}",
js_name
.chars()
.filter(|c| c.is_ascii_alphanumeric())
.collect::<String>(),
self.ident
self.ident,
ShortHash((&js_name, module, &self.ident)),
);
Ok(ast::ImportKind::Static(ast::ImportStatic {
ty: *self.ty,
@ -973,7 +972,7 @@ impl<'a> MacroParse<&'a BindgenAttrs> for syn::ForeignItem {
let kind = match self {
syn::ForeignItem::Fn(f) => f.convert((item_opts, &module))?,
syn::ForeignItem::Type(t) => t.convert(item_opts)?,
syn::ForeignItem::Static(s) => s.convert(item_opts)?,
syn::ForeignItem::Static(s) => s.convert((item_opts, &module))?,
_ => panic!("only foreign functions/types allowed for now"),
};

View File

@ -6,6 +6,7 @@ pub mod same_function_different_locations_a {
#[wasm_bindgen(module = "tests/wasm/duplicates_a.js")]
extern {
pub fn foo();
pub static bar: JsValue;
}
}
@ -15,6 +16,7 @@ pub mod same_function_different_locations_b {
#[wasm_bindgen(module = "tests/wasm/duplicates_a.js")]
extern {
pub fn foo();
pub static bar: JsValue;
}
}
@ -22,6 +24,8 @@ pub mod same_function_different_locations_b {
fn same_function_different_locations() {
same_function_different_locations_a::foo();
same_function_different_locations_b::foo();
assert_eq!(*same_function_different_locations_a::bar, 3);
assert_eq!(*same_function_different_locations_a::bar, 3);
}
pub mod same_function_different_modules_a {
@ -30,6 +34,7 @@ pub mod same_function_different_modules_a {
#[wasm_bindgen(module = "tests/wasm/duplicates_b.js")]
extern {
pub fn foo() -> bool;
pub static bar: JsValue;
}
}
@ -39,6 +44,7 @@ pub mod same_function_different_modules_b {
#[wasm_bindgen(module = "tests/wasm/duplicates_c.js")]
extern {
pub fn foo() -> bool;
pub static bar: JsValue;
}
}
@ -46,4 +52,6 @@ pub mod same_function_different_modules_b {
fn same_function_different_modules() {
assert!(same_function_different_modules_a::foo());
assert!(!same_function_different_modules_b::foo());
assert_eq!(*same_function_different_modules_a::bar, 4);
assert_eq!(*same_function_different_modules_b::bar, 5);
}

View File

@ -1 +1,2 @@
exports.foo = () => {};
exports.bar = 3;

View File

@ -1 +1,2 @@
exports.foo = () => true;
exports.bar = 4;

View File

@ -1 +1,2 @@
exports.foo = () => false;
exports.bar = 5;