diff --git a/crates/macro-support/src/parser.rs b/crates/macro-support/src/parser.rs index a3de4d17..47fe0fb2 100644 --- a/crates/macro-support/src/parser.rs +++ b/crates/macro-support/src/parser.rs @@ -560,10 +560,12 @@ impl ConvertToAst for syn::ForeignItemType { } } -impl ConvertToAst for syn::ForeignItemStatic { +impl<'a> ConvertToAst<(BindgenAttrs, &'a Option)> for syn::ForeignItemStatic { type Target = ast::ImportKind; - fn convert(self, opts: BindgenAttrs) -> Result { + fn convert(self, (opts, module): (BindgenAttrs, &'a Option)) + -> Result + { if self.mutability.is_some() { bail_span!(self.mutability, "cannot import mutable globals yet") } @@ -571,11 +573,8 @@ impl ConvertToAst 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::(), - 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"), }; diff --git a/tests/wasm/duplicates.rs b/tests/wasm/duplicates.rs index f35e6d37..60b17c82 100644 --- a/tests/wasm/duplicates.rs +++ b/tests/wasm/duplicates.rs @@ -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); } diff --git a/tests/wasm/duplicates_a.js b/tests/wasm/duplicates_a.js index ee5ebb32..e52f346b 100644 --- a/tests/wasm/duplicates_a.js +++ b/tests/wasm/duplicates_a.js @@ -1 +1,2 @@ exports.foo = () => {}; +exports.bar = 3; diff --git a/tests/wasm/duplicates_b.js b/tests/wasm/duplicates_b.js index 00128ede..75263a24 100644 --- a/tests/wasm/duplicates_b.js +++ b/tests/wasm/duplicates_b.js @@ -1 +1,2 @@ exports.foo = () => true; +exports.bar = 4; diff --git a/tests/wasm/duplicates_c.js b/tests/wasm/duplicates_c.js index 90ca9240..601a99a5 100644 --- a/tests/wasm/duplicates_c.js +++ b/tests/wasm/duplicates_c.js @@ -1 +1,2 @@ exports.foo = () => false; +exports.bar = 5;