Support importing same-name statics from two modules

Closes #714
This commit is contained in:
Alex Crichton 2018-08-20 10:52:54 -07:00
parent adcc0dd23e
commit f8cf4ab732
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; 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() { if self.mutability.is_some() {
bail_span!(self.mutability, "cannot import mutable globals yet") 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 js_name = opts.js_name().unwrap_or(&default_name);
let shim = format!( let shim = format!(
"__wbg_static_accessor_{}_{}", "__wbg_static_accessor_{}_{}",
js_name self.ident,
.chars() ShortHash((&js_name, module, &self.ident)),
.filter(|c| c.is_ascii_alphanumeric())
.collect::<String>(),
self.ident
); );
Ok(ast::ImportKind::Static(ast::ImportStatic { Ok(ast::ImportKind::Static(ast::ImportStatic {
ty: *self.ty, ty: *self.ty,
@ -973,7 +972,7 @@ impl<'a> MacroParse<&'a BindgenAttrs> for syn::ForeignItem {
let kind = match self { let kind = match self {
syn::ForeignItem::Fn(f) => f.convert((item_opts, &module))?, syn::ForeignItem::Fn(f) => f.convert((item_opts, &module))?,
syn::ForeignItem::Type(t) => t.convert(item_opts)?, 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"), _ => 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")] #[wasm_bindgen(module = "tests/wasm/duplicates_a.js")]
extern { extern {
pub fn foo(); 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")] #[wasm_bindgen(module = "tests/wasm/duplicates_a.js")]
extern { extern {
pub fn foo(); pub fn foo();
pub static bar: JsValue;
} }
} }
@ -22,6 +24,8 @@ pub mod same_function_different_locations_b {
fn same_function_different_locations() { fn same_function_different_locations() {
same_function_different_locations_a::foo(); same_function_different_locations_a::foo();
same_function_different_locations_b::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 { 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")] #[wasm_bindgen(module = "tests/wasm/duplicates_b.js")]
extern { extern {
pub fn foo() -> bool; 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")] #[wasm_bindgen(module = "tests/wasm/duplicates_c.js")]
extern { extern {
pub fn foo() -> bool; pub fn foo() -> bool;
pub static bar: JsValue;
} }
} }
@ -46,4 +52,6 @@ pub mod same_function_different_modules_b {
fn same_function_different_modules() { fn same_function_different_modules() {
assert!(same_function_different_modules_a::foo()); assert!(same_function_different_modules_a::foo());
assert!(!same_function_different_modules_b::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.foo = () => {};
exports.bar = 3;

View File

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

View File

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