Skip to content

Commit d6e3770

Browse files
committed
Scope snippets within a crate
Use the same crate identifier for manually included snippets as well as inline snippets to help with debugging.
1 parent c463cc9 commit d6e3770

File tree

4 files changed

+56
-21
lines changed

4 files changed

+56
-21
lines changed

crates/backend/src/encode.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ impl Interner {
7878

7979
// Generate a unique ID which is somewhat readable as well, so mix in
8080
// the crate name, hash to make it unique, and then the original path.
81-
let new_identifier = format!("{}-{}{}", self.crate_name, ShortHash(0), id);
81+
let new_identifier = format!("{}{}", self.unique_crate_identifier(), id);
8282
let file = LocalFile {
8383
path,
8484
definition: span,
@@ -88,6 +88,10 @@ impl Interner {
8888
drop(files);
8989
self.resolve_import_module(id, span)
9090
}
91+
92+
fn unique_crate_identifier(&self) -> String {
93+
format!("{}-{}", self.crate_name, ShortHash(0))
94+
}
9195
}
9296

9397
fn shared_program<'a>(
@@ -139,6 +143,7 @@ fn shared_program<'a>(
139143
.iter()
140144
.map(|js| intern.intern_str(js))
141145
.collect(),
146+
unique_crate_identifier: intern.intern_str(&intern.unique_crate_identifier()),
142147
})
143148
}
144149

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

Lines changed: 34 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,11 @@ pub struct Context<'a> {
5858
/// known as to their actual JS contents.
5959
pub local_modules: HashMap<&'a str, &'a str>,
6060

61-
/// An integer offset of where to start assigning indexes to `inline_js`
62-
/// snippets. This is incremented each time a `Program` is processed.
63-
pub snippet_offset: usize,
61+
/// A map of how many snippets we've seen from each unique crate identifier,
62+
/// used to number snippets correctly when writing them to the filesystem
63+
/// when there's multiple snippets within one crate that aren't all part of
64+
/// the same `Program`.
65+
pub snippet_offsets: HashMap<&'a str, usize>,
6466

6567
pub anyref: wasm_bindgen_anyref_xform::Context,
6668
}
@@ -118,7 +120,8 @@ enum Import<'a> {
118120
},
119121
/// Same as `Module`, except we're importing from an `inline_js` attribute
120122
InlineJs {
121-
idx: usize,
123+
unique_crate_identifier: &'a str,
124+
snippet_idx_in_crate: usize,
122125
name: &'a str,
123126
field: Option<&'a str>,
124127
},
@@ -2133,9 +2136,14 @@ impl<'a> Context<'a> {
21332136
let path = match import {
21342137
Import::Module { module, .. } => module.to_string(),
21352138
Import::LocalModule { module, .. } => format!("./snippets/{}", module),
2136-
Import::InlineJs { idx, .. } => {
2137-
format!("./snippets/wbg-inline{}.js", idx)
2138-
}
2139+
Import::InlineJs {
2140+
unique_crate_identifier,
2141+
snippet_idx_in_crate,
2142+
..
2143+
} => format!(
2144+
"./snippets/{}/inline{}.js",
2145+
unique_crate_identifier, snippet_idx_in_crate
2146+
),
21392147
_ => unreachable!(),
21402148
};
21412149
if use_node_require {
@@ -2918,11 +2926,19 @@ impl<'a, 'b> SubContext<'a, 'b> {
29182926
name,
29192927
field,
29202928
},
2921-
decode::ImportModule::Inline(idx) => Import::InlineJs {
2922-
idx: idx as usize + self.cx.snippet_offset,
2923-
name,
2924-
field,
2925-
},
2929+
decode::ImportModule::Inline(idx) => {
2930+
let offset = *self
2931+
.cx
2932+
.snippet_offsets
2933+
.get(self.program.unique_crate_identifier)
2934+
.unwrap_or(&0);
2935+
Import::InlineJs {
2936+
unique_crate_identifier: self.program.unique_crate_identifier,
2937+
snippet_idx_in_crate: idx as usize + offset,
2938+
name,
2939+
field,
2940+
}
2941+
}
29262942
decode::ImportModule::None => Import::Global { name, field },
29272943
})
29282944
}
@@ -2936,7 +2952,7 @@ impl<'a, 'b> SubContext<'a, 'b> {
29362952
#[derive(Hash, Eq, PartialEq)]
29372953
pub enum ImportModule<'a> {
29382954
Named(&'a str),
2939-
Inline(usize),
2955+
Inline(&'a str, usize),
29402956
None,
29412957
}
29422958

@@ -2946,7 +2962,11 @@ impl<'a> Import<'a> {
29462962
Import::Module { module, .. } | Import::LocalModule { module, .. } => {
29472963
ImportModule::Named(module)
29482964
}
2949-
Import::InlineJs { idx, .. } => ImportModule::Inline(*idx),
2965+
Import::InlineJs {
2966+
unique_crate_identifier,
2967+
snippet_idx_in_crate,
2968+
..
2969+
} => ImportModule::Inline(unique_crate_identifier, *snippet_idx_in_crate),
29502970
Import::Global { .. } | Import::VendorPrefixed { .. } => ImportModule::None,
29512971
}
29522972
}

crates/cli-support/src/lib.rs

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,7 @@ impl Bindgen {
304304
local_modules: Default::default(),
305305
start: None,
306306
anyref: Default::default(),
307-
snippet_offset: 0,
307+
snippet_offsets: Default::default(),
308308
};
309309
cx.anyref.enabled = self.anyref;
310310
cx.anyref.prepare(cx.module)?;
@@ -316,14 +316,21 @@ impl Bindgen {
316316
}
317317
.generate()?;
318318

319-
for (i, js) in program.inline_js.iter().enumerate() {
320-
let name = format!("wbg-inline{}.js", i + cx.snippet_offset);
321-
let path = out_dir.join("snippets").join(name);
319+
let offset = cx
320+
.snippet_offsets
321+
.entry(program.unique_crate_identifier)
322+
.or_insert(0);
323+
for js in program.inline_js.iter() {
324+
let name = format!("inline{}.js", *offset);
325+
*offset += 1;
326+
let path = out_dir
327+
.join("snippets")
328+
.join(program.unique_crate_identifier)
329+
.join(name);
322330
fs::create_dir_all(path.parent().unwrap())?;
323331
fs::write(&path, js)
324332
.with_context(|_| format!("failed to write `{}`", path.display()))?;
325333
}
326-
cx.snippet_offset += program.inline_js.len();
327334
}
328335

329336
// Write out all local JS snippets to the final destination now that
@@ -630,7 +637,9 @@ fn demangle(module: &mut Module) {
630637
impl OutputMode {
631638
fn nodejs_experimental_modules(&self) -> bool {
632639
match self {
633-
OutputMode::Node { experimental_modules } => *experimental_modules,
640+
OutputMode::Node {
641+
experimental_modules,
642+
} => *experimental_modules,
634643
_ => false,
635644
}
636645
}

crates/shared/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ macro_rules! shared_api {
1616
typescript_custom_sections: Vec<&'a str>,
1717
local_modules: Vec<LocalModule<'a>>,
1818
inline_js: Vec<&'a str>,
19+
unique_crate_identifier: &'a str,
1920
}
2021

2122
struct Import<'a> {

0 commit comments

Comments
 (0)