Skip to content

Commit

Permalink
Don't emit snippets without --split-linked-modules (#4066)
Browse files Browse the repository at this point in the history
  • Loading branch information
daxpedda authored Aug 13, 2024
1 parent 0f0b4e2 commit d813df7
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 27 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
21 changes: 16 additions & 5 deletions crates/backend/src/encode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ struct LocalFile {
path: PathBuf,
definition: Span,
new_identifier: String,
linked_module: bool,
}

impl Interner {
Expand Down Expand Up @@ -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)));
Expand All @@ -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 {
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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)?,
Expand All @@ -274,17 +282,20 @@ 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),
})
}

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),
})
Expand Down
16 changes: 1 addition & 15 deletions crates/cli-support/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
}
Expand Down
16 changes: 10 additions & 6 deletions crates/cli-support/src/wit/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -36,6 +36,7 @@ struct Context<'a> {
externref_enabled: bool,
thread_count: Option<ThreadCount>,
support_start: bool,
linked_modules: bool,
}

struct InstructionBuilder<'a, 'b> {
Expand All @@ -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(),
Expand All @@ -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()?;

Expand Down Expand Up @@ -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.
Expand Down
1 change: 1 addition & 0 deletions crates/shared/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ macro_rules! shared_api {
struct LocalModule<'a> {
identifier: &'a str,
contents: &'a str,
linked_module: bool,
}
}
}; // end of mac case
Expand Down
2 changes: 1 addition & 1 deletion crates/shared/src/schema_hash_approval.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down

0 comments on commit d813df7

Please sign in to comment.