mirror of
https://github.com/fluencelabs/wasm-bindgen
synced 2025-06-12 20:41:24 +00:00
Fix some situations with duplicate imports (#589)
* Fix importing the same identifier from two modules This needed a fix in two locations: * First the generated descriptor function needed its hash to include the module that the import came from in order to generate unique descriptor functions. * Second the generation of the JS shim needed to handle duplicate identifiers in a more uniform fashion, ensuring that imported names didn't clash. * Fix importing the same name in two modules Previously two descriptor functions with duplicate symbols were emitted, and now only one function is emitted by using a global table to keep track of state across macro invocations.
This commit is contained in:
78
tests/all/duplicates.rs
Normal file
78
tests/all/duplicates.rs
Normal file
@ -0,0 +1,78 @@
|
||||
use super::project;
|
||||
|
||||
#[test]
|
||||
fn same_function_different_locations() {
|
||||
project()
|
||||
.file(
|
||||
"src/lib.rs",
|
||||
r#"
|
||||
#![feature(use_extern_macros)]
|
||||
|
||||
extern crate wasm_bindgen;
|
||||
use wasm_bindgen::prelude::*;
|
||||
|
||||
pub mod a {
|
||||
use wasm_bindgen::prelude::*;
|
||||
#[wasm_bindgen(module = "./foo")]
|
||||
extern {
|
||||
pub fn foo();
|
||||
}
|
||||
}
|
||||
|
||||
pub mod b {
|
||||
use wasm_bindgen::prelude::*;
|
||||
#[wasm_bindgen(module = "./foo")]
|
||||
extern {
|
||||
pub fn foo();
|
||||
}
|
||||
}
|
||||
|
||||
#[wasm_bindgen]
|
||||
pub fn test() {
|
||||
a::foo();
|
||||
b::foo();
|
||||
}
|
||||
"#,
|
||||
)
|
||||
.file("foo.js", "export function foo() {}")
|
||||
.test();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn same_function_different_modules() {
|
||||
project()
|
||||
.file(
|
||||
"src/lib.rs",
|
||||
r#"
|
||||
#![feature(use_extern_macros)]
|
||||
|
||||
extern crate wasm_bindgen;
|
||||
use wasm_bindgen::prelude::*;
|
||||
|
||||
pub mod a {
|
||||
use wasm_bindgen::prelude::*;
|
||||
#[wasm_bindgen(module = "./foo")]
|
||||
extern {
|
||||
pub fn foo() -> bool;
|
||||
}
|
||||
}
|
||||
|
||||
pub mod b {
|
||||
use wasm_bindgen::prelude::*;
|
||||
#[wasm_bindgen(module = "./bar")]
|
||||
extern {
|
||||
pub fn foo() -> bool;
|
||||
}
|
||||
}
|
||||
|
||||
#[wasm_bindgen]
|
||||
pub fn test() {
|
||||
assert!(a::foo());
|
||||
assert!(!b::foo());
|
||||
}
|
||||
"#,
|
||||
)
|
||||
.file("foo.js", "export function foo() { return true; } ")
|
||||
.file("bar.js", "export function foo() { return false; } ")
|
||||
.test();
|
||||
}
|
@ -8,6 +8,7 @@ mod classes;
|
||||
mod closures;
|
||||
mod comments;
|
||||
mod dependencies;
|
||||
mod duplicates;
|
||||
mod enums;
|
||||
mod import_class;
|
||||
mod imports;
|
||||
|
Reference in New Issue
Block a user