mirror of
https://github.com/rustwasm/wasm-bindgen.git
synced 2024-11-24 06:33:33 +03:00
Merge pull request #741 from alexcrichton/duplicate-statics
Support importing same-name statics from two modules
This commit is contained in:
commit
86d1ab513b
@ -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"),
|
||||
};
|
||||
|
||||
|
@ -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);
|
||||
}
|
||||
|
@ -1 +1,2 @@
|
||||
exports.foo = () => {};
|
||||
exports.bar = 3;
|
||||
|
@ -1 +1,2 @@
|
||||
exports.foo = () => true;
|
||||
exports.bar = 4;
|
||||
|
@ -1 +1,2 @@
|
||||
exports.foo = () => false;
|
||||
exports.bar = 5;
|
||||
|
Loading…
Reference in New Issue
Block a user