diff --git a/crates/cli-support/src/js/mod.rs b/crates/cli-support/src/js/mod.rs index 7e6efdea..96ea33d4 100644 --- a/crates/cli-support/src/js/mod.rs +++ b/crates/cli-support/src/js/mod.rs @@ -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(|_| { diff --git a/tests/headless/snippets.rs b/tests/headless/snippets.rs index f52b5504..978efb29 100644 --- a/tests/headless/snippets.rs +++ b/tests/headless/snippets.rs @@ -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; diff --git a/tests/headless/snippets1.js b/tests/headless/snippets1.js index 88975843..12e5a6bf 100644 --- a/tests/headless/snippets1.js +++ b/tests/headless/snippets1.js @@ -1,3 +1,9 @@ export function get_two() { return 2; } + +let a = 0; +export function get_stateful() { + a += 1; + return a; +}