Scope snippets within a crate

Use the same crate identifier for manually included snippets as well as
inline snippets to help with debugging.
This commit is contained in:
Alex Crichton 2019-03-05 14:53:14 -08:00
parent c463cc96df
commit d6e3770350
4 changed files with 56 additions and 21 deletions

View File

@ -78,7 +78,7 @@ impl Interner {
// Generate a unique ID which is somewhat readable as well, so mix in // Generate a unique ID which is somewhat readable as well, so mix in
// the crate name, hash to make it unique, and then the original path. // the crate name, hash to make it unique, and then the original path.
let new_identifier = format!("{}-{}{}", self.crate_name, ShortHash(0), id); let new_identifier = format!("{}{}", self.unique_crate_identifier(), id);
let file = LocalFile { let file = LocalFile {
path, path,
definition: span, definition: span,
@ -88,6 +88,10 @@ impl Interner {
drop(files); drop(files);
self.resolve_import_module(id, span) self.resolve_import_module(id, span)
} }
fn unique_crate_identifier(&self) -> String {
format!("{}-{}", self.crate_name, ShortHash(0))
}
} }
fn shared_program<'a>( fn shared_program<'a>(
@ -139,6 +143,7 @@ fn shared_program<'a>(
.iter() .iter()
.map(|js| intern.intern_str(js)) .map(|js| intern.intern_str(js))
.collect(), .collect(),
unique_crate_identifier: intern.intern_str(&intern.unique_crate_identifier()),
}) })
} }

View File

@ -58,9 +58,11 @@ pub struct Context<'a> {
/// known as to their actual JS contents. /// known as to their actual JS contents.
pub local_modules: HashMap<&'a str, &'a str>, pub local_modules: HashMap<&'a str, &'a str>,
/// An integer offset of where to start assigning indexes to `inline_js` /// A map of how many snippets we've seen from each unique crate identifier,
/// snippets. This is incremented each time a `Program` is processed. /// used to number snippets correctly when writing them to the filesystem
pub snippet_offset: usize, /// when there's multiple snippets within one crate that aren't all part of
/// the same `Program`.
pub snippet_offsets: HashMap<&'a str, usize>,
pub anyref: wasm_bindgen_anyref_xform::Context, pub anyref: wasm_bindgen_anyref_xform::Context,
} }
@ -118,7 +120,8 @@ enum Import<'a> {
}, },
/// Same as `Module`, except we're importing from an `inline_js` attribute /// Same as `Module`, except we're importing from an `inline_js` attribute
InlineJs { InlineJs {
idx: usize, unique_crate_identifier: &'a str,
snippet_idx_in_crate: usize,
name: &'a str, name: &'a str,
field: Option<&'a str>, field: Option<&'a str>,
}, },
@ -2133,9 +2136,14 @@ impl<'a> Context<'a> {
let path = match import { let path = match import {
Import::Module { module, .. } => module.to_string(), Import::Module { module, .. } => module.to_string(),
Import::LocalModule { module, .. } => format!("./snippets/{}", module), Import::LocalModule { module, .. } => format!("./snippets/{}", module),
Import::InlineJs { idx, .. } => { Import::InlineJs {
format!("./snippets/wbg-inline{}.js", idx) unique_crate_identifier,
} snippet_idx_in_crate,
..
} => format!(
"./snippets/{}/inline{}.js",
unique_crate_identifier, snippet_idx_in_crate
),
_ => unreachable!(), _ => unreachable!(),
}; };
if use_node_require { if use_node_require {
@ -2918,11 +2926,19 @@ impl<'a, 'b> SubContext<'a, 'b> {
name, name,
field, field,
}, },
decode::ImportModule::Inline(idx) => Import::InlineJs { decode::ImportModule::Inline(idx) => {
idx: idx as usize + self.cx.snippet_offset, let offset = *self
.cx
.snippet_offsets
.get(self.program.unique_crate_identifier)
.unwrap_or(&0);
Import::InlineJs {
unique_crate_identifier: self.program.unique_crate_identifier,
snippet_idx_in_crate: idx as usize + offset,
name, name,
field, field,
}, }
}
decode::ImportModule::None => Import::Global { name, field }, decode::ImportModule::None => Import::Global { name, field },
}) })
} }
@ -2936,7 +2952,7 @@ impl<'a, 'b> SubContext<'a, 'b> {
#[derive(Hash, Eq, PartialEq)] #[derive(Hash, Eq, PartialEq)]
pub enum ImportModule<'a> { pub enum ImportModule<'a> {
Named(&'a str), Named(&'a str),
Inline(usize), Inline(&'a str, usize),
None, None,
} }
@ -2946,7 +2962,11 @@ impl<'a> Import<'a> {
Import::Module { module, .. } | Import::LocalModule { module, .. } => { Import::Module { module, .. } | Import::LocalModule { module, .. } => {
ImportModule::Named(module) ImportModule::Named(module)
} }
Import::InlineJs { idx, .. } => ImportModule::Inline(*idx), Import::InlineJs {
unique_crate_identifier,
snippet_idx_in_crate,
..
} => ImportModule::Inline(unique_crate_identifier, *snippet_idx_in_crate),
Import::Global { .. } | Import::VendorPrefixed { .. } => ImportModule::None, Import::Global { .. } | Import::VendorPrefixed { .. } => ImportModule::None,
} }
} }

View File

@ -304,7 +304,7 @@ impl Bindgen {
local_modules: Default::default(), local_modules: Default::default(),
start: None, start: None,
anyref: Default::default(), anyref: Default::default(),
snippet_offset: 0, snippet_offsets: Default::default(),
}; };
cx.anyref.enabled = self.anyref; cx.anyref.enabled = self.anyref;
cx.anyref.prepare(cx.module)?; cx.anyref.prepare(cx.module)?;
@ -316,14 +316,21 @@ impl Bindgen {
} }
.generate()?; .generate()?;
for (i, js) in program.inline_js.iter().enumerate() { let offset = cx
let name = format!("wbg-inline{}.js", i + cx.snippet_offset); .snippet_offsets
let path = out_dir.join("snippets").join(name); .entry(program.unique_crate_identifier)
.or_insert(0);
for js in program.inline_js.iter() {
let name = format!("inline{}.js", *offset);
*offset += 1;
let path = out_dir
.join("snippets")
.join(program.unique_crate_identifier)
.join(name);
fs::create_dir_all(path.parent().unwrap())?; fs::create_dir_all(path.parent().unwrap())?;
fs::write(&path, js) fs::write(&path, js)
.with_context(|_| format!("failed to write `{}`", path.display()))?; .with_context(|_| format!("failed to write `{}`", path.display()))?;
} }
cx.snippet_offset += program.inline_js.len();
} }
// Write out all local JS snippets to the final destination now that // Write out all local JS snippets to the final destination now that
@ -630,7 +637,9 @@ fn demangle(module: &mut Module) {
impl OutputMode { impl OutputMode {
fn nodejs_experimental_modules(&self) -> bool { fn nodejs_experimental_modules(&self) -> bool {
match self { match self {
OutputMode::Node { experimental_modules } => *experimental_modules, OutputMode::Node {
experimental_modules,
} => *experimental_modules,
_ => false, _ => false,
} }
} }

View File

@ -16,6 +16,7 @@ macro_rules! shared_api {
typescript_custom_sections: Vec<&'a str>, typescript_custom_sections: Vec<&'a str>,
local_modules: Vec<LocalModule<'a>>, local_modules: Vec<LocalModule<'a>>,
inline_js: Vec<&'a str>, inline_js: Vec<&'a str>,
unique_crate_identifier: &'a str,
} }
struct Import<'a> { struct Import<'a> {