Don't emit snippets without --split-linked-modules (#4066)

This commit is contained in:
daxpedda 2024-08-13 17:14:10 +02:00 committed by GitHub
parent 0f0b4e2d0d
commit d813df7bc1
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 32 additions and 27 deletions

View File

@ -196,6 +196,9 @@ Released 2024-08-13
* `#[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)

View File

@ -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<ImportModule, Diagnostic> {
fn resolve_import_module(
&self,
id: &str,
span: Span,
linked_module: bool,
) -> Result<ImportModule, Diagnostic> {
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<Import<
module: i
.module
.as_ref()
.map(|m| shared_module(m, intern))
.map(|m| shared_module(m, intern, false))
.transpose()?,
js_namespace: i.js_namespace.clone(),
kind: shared_import_kind(&i.kind, intern)?,
@ -274,7 +282,7 @@ fn shared_linked_module<'a>(
intern: &'a Interner,
) -> Result<LinkedModule<'a>, 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<ImportModule<'a>, 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),
})

View File

@ -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<String, Vec<String>> {
&self.generated.snippets
}
pub fn local_modules(&self) -> &HashMap<String, String> {
&self.generated.local_modules
}
pub fn npm_dependencies(&self) -> &HashMap<String, (PathBuf, String)> {
&self.generated.npm_dependencies
}

View File

@ -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<ThreadCount>,
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<decode::Program>,
externref_enabled: bool,
thread_count: Option<ThreadCount>,
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.

View File

@ -156,6 +156,7 @@ macro_rules! shared_api {
struct LocalModule<'a> {
identifier: &'a str,
contents: &'a str,
linked_module: bool,
}
}
}; // end of mac case

View File

@ -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() {