From eddb1157f77381869929b2b5a8011e1cf2ec172d Mon Sep 17 00:00:00 2001 From: daxpedda Date: Mon, 12 Aug 2024 20:39:20 +0200 Subject: [PATCH] Don't emit snippets without `--split-linked-modules` --- CHANGELOG.md | 3 +++ crates/backend/src/encode.rs | 21 ++++++++++++++++----- crates/cli-support/src/lib.rs | 16 +--------------- crates/cli-support/src/wit/mod.rs | 16 ++++++++++------ crates/shared/src/lib.rs | 1 + crates/shared/src/schema_hash_approval.rs | 2 +- 6 files changed, 32 insertions(+), 27 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 02e23a00a4f..492e4bf8324 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -191,6 +191,9 @@ * `#[track_caller]` is now always applied on `UnwrapThrowExt` methods when not targetting `wasm32-unknown-unknown`. [#4042](https://github.com/rustwasm/wasm-bindgen/pull/4042) +* Fixed linked modules emitting snippet files when not using `--split-linked-modules`. + [#4066](https://github.com/rustwasm/wasm-bindgen/pull/4066) + -------------------------------------------------------------------------------- ## [0.2.92](https://github.com/rustwasm/wasm-bindgen/compare/0.2.91...0.2.92) diff --git a/crates/backend/src/encode.rs b/crates/backend/src/encode.rs index 0daa9ff1123..44f36942b36 100644 --- a/crates/backend/src/encode.rs +++ b/crates/backend/src/encode.rs @@ -52,6 +52,7 @@ struct LocalFile { path: PathBuf, definition: Span, new_identifier: String, + linked_module: bool, } impl Interner { @@ -85,7 +86,12 @@ impl Interner { /// /// Note that repeated invocations of this function will be memoized, so the /// same `id` will always return the same resulting unique `id`. - fn resolve_import_module(&self, id: &str, span: Span) -> Result { + fn resolve_import_module( + &self, + id: &str, + span: Span, + linked_module: bool, + ) -> Result { let mut files = self.files.borrow_mut(); if let Some(file) = files.get(id) { return Ok(ImportModule::Named(self.intern_str(&file.new_identifier))); @@ -107,10 +113,11 @@ impl Interner { path, definition: span, new_identifier, + linked_module, }; files.insert(id.to_string(), file); drop(files); - self.resolve_import_module(id, span) + self.resolve_import_module(id, span, linked_module) } fn unique_crate_identifier(&self) -> String { @@ -169,6 +176,7 @@ fn shared_program<'a>( .map(|s| LocalModule { identifier: intern.intern_str(&file.new_identifier), contents: intern.intern_str(&s), + linked_module: file.linked_module, }) .map_err(|e| { 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( intern: &'a Interner, ) -> Result, Diagnostic> { Ok(LinkedModule { - module: shared_module(i, intern)?, + module: shared_module(i, intern, true)?, link_function_name: intern.intern_str(name), }) } @@ -282,9 +290,12 @@ fn shared_linked_module<'a>( fn shared_module<'a>( m: &'a ast::ImportModule, intern: &'a Interner, + linked_module: bool, ) -> Result, Diagnostic> { Ok(match m { - ast::ImportModule::Named(m, span) => intern.resolve_import_module(m, *span)?, + ast::ImportModule::Named(m, span) => { + intern.resolve_import_module(m, *span, linked_module)? + } ast::ImportModule::RawNamed(m, _span) => ImportModule::RawNamed(intern.intern_str(m)), ast::ImportModule::Inline(idx, _) => ImportModule::Inline(*idx as u32), }) diff --git a/crates/cli-support/src/lib.rs b/crates/cli-support/src/lib.rs index 53270eb6eda..7a3d70cc704 100755 --- a/crates/cli-support/src/lib.rs +++ b/crates/cli-support/src/lib.rs @@ -368,13 +368,7 @@ impl Bindgen { // auxiliary section for all sorts of miscellaneous information and // features #[wasm_bindgen] supports that aren't covered by wasm // interface types. - wit::process( - &mut module, - programs, - self.externref, - thread_count, - self.emit_start, - )?; + wit::process(self, &mut module, programs, thread_count)?; // Now that we've got type information from the webidl processing pass, // touch up the output of rustc to insert externref shims where necessary. @@ -601,14 +595,6 @@ impl Output { self.generated.start.as_ref() } - pub fn snippets(&self) -> &HashMap> { - &self.generated.snippets - } - - pub fn local_modules(&self) -> &HashMap { - &self.generated.local_modules - } - pub fn npm_dependencies(&self) -> &HashMap { &self.generated.npm_dependencies } diff --git a/crates/cli-support/src/wit/mod.rs b/crates/cli-support/src/wit/mod.rs index f85537b8fa2..ca0306967be 100644 --- a/crates/cli-support/src/wit/mod.rs +++ b/crates/cli-support/src/wit/mod.rs @@ -2,7 +2,7 @@ use crate::decode::LocalModule; use crate::descriptor::{Descriptor, Function}; use crate::descriptors::WasmBindgenDescriptorsSection; use crate::intrinsic::Intrinsic; -use crate::{decode, PLACEHOLDER_MODULE}; +use crate::{decode, Bindgen, PLACEHOLDER_MODULE}; use anyhow::{anyhow, bail, Error}; use std::collections::{HashMap, HashSet}; use std::str; @@ -36,6 +36,7 @@ struct Context<'a> { externref_enabled: bool, thread_count: Option, support_start: bool, + linked_modules: bool, } struct InstructionBuilder<'a, 'b> { @@ -47,11 +48,10 @@ struct InstructionBuilder<'a, 'b> { } pub fn process( + bindgen: &mut Bindgen, module: &mut Module, programs: Vec, - externref_enabled: bool, thread_count: Option, - support_start: bool, ) -> Result<(NonstandardWitSectionId, WasmBindgenAuxId), Error> { let mut cx = Context { adapters: Default::default(), @@ -65,9 +65,10 @@ pub fn process( memory: wasm_bindgen_wasm_conventions::get_memory(module).ok(), module, start_found: false, - externref_enabled, + externref_enabled: bindgen.externref, thread_count, - support_start, + support_start: bindgen.emit_start, + linked_modules: bindgen.split_linked_modules, }; cx.init()?; @@ -392,7 +393,10 @@ impl<'a> Context<'a> { linked_modules, } = program; - for module in &local_modules { + for module in local_modules + .iter() + .filter(|module| self.linked_modules || !module.linked_module) + { // 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. diff --git a/crates/shared/src/lib.rs b/crates/shared/src/lib.rs index e85ddf9b87c..0b9af3b581b 100644 --- a/crates/shared/src/lib.rs +++ b/crates/shared/src/lib.rs @@ -156,6 +156,7 @@ macro_rules! shared_api { struct LocalModule<'a> { identifier: &'a str, contents: &'a str, + linked_module: bool, } } }; // end of mac case diff --git a/crates/shared/src/schema_hash_approval.rs b/crates/shared/src/schema_hash_approval.rs index a52716f360f..1de5ee31cd3 100644 --- a/crates/shared/src/schema_hash_approval.rs +++ b/crates/shared/src/schema_hash_approval.rs @@ -8,7 +8,7 @@ // If the schema in this library has changed then: // 1. Bump the version in `crates/shared/Cargo.toml` // 2. Change the `SCHEMA_VERSION` in this library to this new Cargo.toml version -const APPROVED_SCHEMA_FILE_HASH: &str = "3501400214978266446"; +const APPROVED_SCHEMA_FILE_HASH: &str = "9336383503182818021"; #[test] fn schema_version() {