Skip to content

Commit d813df7

Browse files
authored
Don't emit snippets without --split-linked-modules (#4066)
1 parent 0f0b4e2 commit d813df7

File tree

6 files changed

+32
-27
lines changed

6 files changed

+32
-27
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,9 @@ Released 2024-08-13
196196
* `#[track_caller]` is now always applied on `UnwrapThrowExt` methods when not targetting `wasm32-unknown-unknown`.
197197
[#4042](https://github.com/rustwasm/wasm-bindgen/pull/4042)
198198

199+
* Fixed linked modules emitting snippet files when not using `--split-linked-modules`.
200+
[#4066](https://github.com/rustwasm/wasm-bindgen/pull/4066)
201+
199202
--------------------------------------------------------------------------------
200203

201204
## [0.2.92](https://github.com/rustwasm/wasm-bindgen/compare/0.2.91...0.2.92)

crates/backend/src/encode.rs

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ struct LocalFile {
5252
path: PathBuf,
5353
definition: Span,
5454
new_identifier: String,
55+
linked_module: bool,
5556
}
5657

5758
impl Interner {
@@ -85,7 +86,12 @@ impl Interner {
8586
///
8687
/// Note that repeated invocations of this function will be memoized, so the
8788
/// same `id` will always return the same resulting unique `id`.
88-
fn resolve_import_module(&self, id: &str, span: Span) -> Result<ImportModule, Diagnostic> {
89+
fn resolve_import_module(
90+
&self,
91+
id: &str,
92+
span: Span,
93+
linked_module: bool,
94+
) -> Result<ImportModule, Diagnostic> {
8995
let mut files = self.files.borrow_mut();
9096
if let Some(file) = files.get(id) {
9197
return Ok(ImportModule::Named(self.intern_str(&file.new_identifier)));
@@ -107,10 +113,11 @@ impl Interner {
107113
path,
108114
definition: span,
109115
new_identifier,
116+
linked_module,
110117
};
111118
files.insert(id.to_string(), file);
112119
drop(files);
113-
self.resolve_import_module(id, span)
120+
self.resolve_import_module(id, span, linked_module)
114121
}
115122

116123
fn unique_crate_identifier(&self) -> String {
@@ -169,6 +176,7 @@ fn shared_program<'a>(
169176
.map(|s| LocalModule {
170177
identifier: intern.intern_str(&file.new_identifier),
171178
contents: intern.intern_str(&s),
179+
linked_module: file.linked_module,
172180
})
173181
.map_err(|e| {
174182
let msg = format!("failed to read file `{}`: {}", file.path.display(), e);
@@ -254,7 +262,7 @@ fn shared_import<'a>(i: &'a ast::Import, intern: &'a Interner) -> Result<Import<
254262
module: i
255263
.module
256264
.as_ref()
257-
.map(|m| shared_module(m, intern))
265+
.map(|m| shared_module(m, intern, false))
258266
.transpose()?,
259267
js_namespace: i.js_namespace.clone(),
260268
kind: shared_import_kind(&i.kind, intern)?,
@@ -274,17 +282,20 @@ fn shared_linked_module<'a>(
274282
intern: &'a Interner,
275283
) -> Result<LinkedModule<'a>, Diagnostic> {
276284
Ok(LinkedModule {
277-
module: shared_module(i, intern)?,
285+
module: shared_module(i, intern, true)?,
278286
link_function_name: intern.intern_str(name),
279287
})
280288
}
281289

282290
fn shared_module<'a>(
283291
m: &'a ast::ImportModule,
284292
intern: &'a Interner,
293+
linked_module: bool,
285294
) -> Result<ImportModule<'a>, Diagnostic> {
286295
Ok(match m {
287-
ast::ImportModule::Named(m, span) => intern.resolve_import_module(m, *span)?,
296+
ast::ImportModule::Named(m, span) => {
297+
intern.resolve_import_module(m, *span, linked_module)?
298+
}
288299
ast::ImportModule::RawNamed(m, _span) => ImportModule::RawNamed(intern.intern_str(m)),
289300
ast::ImportModule::Inline(idx, _) => ImportModule::Inline(*idx as u32),
290301
})

crates/cli-support/src/lib.rs

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -368,13 +368,7 @@ impl Bindgen {
368368
// auxiliary section for all sorts of miscellaneous information and
369369
// features #[wasm_bindgen] supports that aren't covered by wasm
370370
// interface types.
371-
wit::process(
372-
&mut module,
373-
programs,
374-
self.externref,
375-
thread_count,
376-
self.emit_start,
377-
)?;
371+
wit::process(self, &mut module, programs, thread_count)?;
378372

379373
// Now that we've got type information from the webidl processing pass,
380374
// touch up the output of rustc to insert externref shims where necessary.
@@ -601,14 +595,6 @@ impl Output {
601595
self.generated.start.as_ref()
602596
}
603597

604-
pub fn snippets(&self) -> &HashMap<String, Vec<String>> {
605-
&self.generated.snippets
606-
}
607-
608-
pub fn local_modules(&self) -> &HashMap<String, String> {
609-
&self.generated.local_modules
610-
}
611-
612598
pub fn npm_dependencies(&self) -> &HashMap<String, (PathBuf, String)> {
613599
&self.generated.npm_dependencies
614600
}

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

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use crate::decode::LocalModule;
22
use crate::descriptor::{Descriptor, Function};
33
use crate::descriptors::WasmBindgenDescriptorsSection;
44
use crate::intrinsic::Intrinsic;
5-
use crate::{decode, PLACEHOLDER_MODULE};
5+
use crate::{decode, Bindgen, PLACEHOLDER_MODULE};
66
use anyhow::{anyhow, bail, Error};
77
use std::collections::{HashMap, HashSet};
88
use std::str;
@@ -36,6 +36,7 @@ struct Context<'a> {
3636
externref_enabled: bool,
3737
thread_count: Option<ThreadCount>,
3838
support_start: bool,
39+
linked_modules: bool,
3940
}
4041

4142
struct InstructionBuilder<'a, 'b> {
@@ -47,11 +48,10 @@ struct InstructionBuilder<'a, 'b> {
4748
}
4849

4950
pub fn process(
51+
bindgen: &mut Bindgen,
5052
module: &mut Module,
5153
programs: Vec<decode::Program>,
52-
externref_enabled: bool,
5354
thread_count: Option<ThreadCount>,
54-
support_start: bool,
5555
) -> Result<(NonstandardWitSectionId, WasmBindgenAuxId), Error> {
5656
let mut cx = Context {
5757
adapters: Default::default(),
@@ -65,9 +65,10 @@ pub fn process(
6565
memory: wasm_bindgen_wasm_conventions::get_memory(module).ok(),
6666
module,
6767
start_found: false,
68-
externref_enabled,
68+
externref_enabled: bindgen.externref,
6969
thread_count,
70-
support_start,
70+
support_start: bindgen.emit_start,
71+
linked_modules: bindgen.split_linked_modules,
7172
};
7273
cx.init()?;
7374

@@ -392,7 +393,10 @@ impl<'a> Context<'a> {
392393
linked_modules,
393394
} = program;
394395

395-
for module in &local_modules {
396+
for module in local_modules
397+
.iter()
398+
.filter(|module| self.linked_modules || !module.linked_module)
399+
{
396400
// All local modules we find should be unique, but the same module
397401
// may have showed up in a few different blocks. If that's the case
398402
// all the same identifiers should have the same contents.

crates/shared/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,7 @@ macro_rules! shared_api {
156156
struct LocalModule<'a> {
157157
identifier: &'a str,
158158
contents: &'a str,
159+
linked_module: bool,
159160
}
160161
}
161162
}; // end of mac case

crates/shared/src/schema_hash_approval.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
// If the schema in this library has changed then:
99
// 1. Bump the version in `crates/shared/Cargo.toml`
1010
// 2. Change the `SCHEMA_VERSION` in this library to this new Cargo.toml version
11-
const APPROVED_SCHEMA_FILE_HASH: &str = "3501400214978266446";
11+
const APPROVED_SCHEMA_FILE_HASH: &str = "9336383503182818021";
1212

1313
#[test]
1414
fn schema_version() {

0 commit comments

Comments
 (0)