1
0
mirror of https://github.com/fluencelabs/wasm-bindgen synced 2025-03-16 02:00:51 +00:00

Fix a case where snippet is specified twice

When importing a file across multiple locations in a module make sure it
doesn't trip an assert and it works as expected.
This commit is contained in:
Alex Crichton 2019-02-26 08:30:59 -08:00
parent 6283169a30
commit 7e62aff1ce
3 changed files with 28 additions and 6 deletions
crates/cli-support/src/js
tests/headless

@ -2378,12 +2378,12 @@ impl<'a> Context<'a> {
impl<'a, 'b> SubContext<'a, 'b> {
pub fn generate(&mut self) -> Result<(), Error> {
for m in self.program.local_modules.iter() {
// All local modules we find should be unique, so assert such.
assert!(self
.cx
.local_modules
.insert(m.identifier, m.contents)
.is_none());
// All local modules we find should be unique, but the same module
// may have showed up in a few different blocks. If that's the case
// all the same identifiers should have the same contents.
if let Some(prev) = self.cx.local_modules.insert(m.identifier, m.contents) {
assert_eq!(prev, m.contents);
}
}
for f in self.program.exports.iter() {
self.generate_export(f).with_context(|_| {

@ -4,6 +4,14 @@ use wasm_bindgen_test::*;
#[wasm_bindgen(module = "/tests/headless/snippets1.js")]
extern {
fn get_two() -> u32;
#[wasm_bindgen(js_name = get_stateful)]
fn get_stateful1() -> u32;
}
#[wasm_bindgen(module = "/tests/headless/snippets1.js")]
extern {
#[wasm_bindgen(js_name = get_stateful)]
fn get_stateful2() -> u32;
}
#[wasm_bindgen_test]
@ -11,6 +19,14 @@ fn test_get_two() {
assert_eq!(get_two(), 2);
}
#[wasm_bindgen_test]
fn stateful_deduplicated() {
assert_eq!(get_stateful1(), 1);
assert_eq!(get_stateful2(), 2);
assert_eq!(get_stateful1(), 3);
assert_eq!(get_stateful2(), 4);
}
#[wasm_bindgen(inline_js = "export function get_three() { return 3; }")]
extern {
fn get_three() -> u32;

@ -1,3 +1,9 @@
export function get_two() {
return 2;
}
let a = 0;
export function get_stateful() {
a += 1;
return a;
}