Skip to content

Commit 7e62aff

Browse files
committed
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.
1 parent 6283169 commit 7e62aff

File tree

3 files changed

+28
-6
lines changed

3 files changed

+28
-6
lines changed

crates/cli-support/src/js/mod.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2378,12 +2378,12 @@ impl<'a> Context<'a> {
23782378
impl<'a, 'b> SubContext<'a, 'b> {
23792379
pub fn generate(&mut self) -> Result<(), Error> {
23802380
for m in self.program.local_modules.iter() {
2381-
// All local modules we find should be unique, so assert such.
2382-
assert!(self
2383-
.cx
2384-
.local_modules
2385-
.insert(m.identifier, m.contents)
2386-
.is_none());
2381+
// All local modules we find should be unique, but the same module
2382+
// may have showed up in a few different blocks. If that's the case
2383+
// all the same identifiers should have the same contents.
2384+
if let Some(prev) = self.cx.local_modules.insert(m.identifier, m.contents) {
2385+
assert_eq!(prev, m.contents);
2386+
}
23872387
}
23882388
for f in self.program.exports.iter() {
23892389
self.generate_export(f).with_context(|_| {

tests/headless/snippets.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,29 @@ use wasm_bindgen_test::*;
44
#[wasm_bindgen(module = "/tests/headless/snippets1.js")]
55
extern {
66
fn get_two() -> u32;
7+
#[wasm_bindgen(js_name = get_stateful)]
8+
fn get_stateful1() -> u32;
9+
}
10+
11+
#[wasm_bindgen(module = "/tests/headless/snippets1.js")]
12+
extern {
13+
#[wasm_bindgen(js_name = get_stateful)]
14+
fn get_stateful2() -> u32;
715
}
816

917
#[wasm_bindgen_test]
1018
fn test_get_two() {
1119
assert_eq!(get_two(), 2);
1220
}
1321

22+
#[wasm_bindgen_test]
23+
fn stateful_deduplicated() {
24+
assert_eq!(get_stateful1(), 1);
25+
assert_eq!(get_stateful2(), 2);
26+
assert_eq!(get_stateful1(), 3);
27+
assert_eq!(get_stateful2(), 4);
28+
}
29+
1430
#[wasm_bindgen(inline_js = "export function get_three() { return 3; }")]
1531
extern {
1632
fn get_three() -> u32;

tests/headless/snippets1.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
11
export function get_two() {
22
return 2;
33
}
4+
5+
let a = 0;
6+
export function get_stateful() {
7+
a += 1;
8+
return a;
9+
}

0 commit comments

Comments
 (0)