Skip to content

Commit

Permalink
Fix inline_js offsets
Browse files Browse the repository at this point in the history
  • Loading branch information
lukaslihotzki committed Sep 26, 2022
1 parent 262fc1d commit b26ef80
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 16 deletions.
19 changes: 10 additions & 9 deletions crates/backend/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//! with all the added metadata necessary to generate WASM bindings
//! for it.
use crate::Diagnostic;
use crate::{util::ShortHash, Diagnostic};
use proc_macro2::{Ident, Span};
use std::hash::{Hash, Hasher};
use wasm_bindgen_shared as shared;
Expand Down Expand Up @@ -38,6 +38,15 @@ impl Program {
&& self.typescript_custom_sections.is_empty()
&& self.inline_js.is_empty()
}

/// Name of the link function for a specific linked module
pub fn link_function_name(&self, idx: usize) -> String {
let hash = match &self.linked_modules[idx] {
ImportModule::Inline(idx, _) => ShortHash((1, &self.inline_js[*idx])).to_string(),
other => ShortHash((0, other)).to_string(),
};
format!("__wbindgen_link_{}", hash)
}
}

/// An abstract syntax tree representing a link to a module in Rust.
Expand Down Expand Up @@ -116,14 +125,6 @@ impl Hash for ImportModule {
}
}

impl ImportModule {
/// Name of the link function when the ImportModule is used in
/// Program::linked_modules.
pub fn link_function_name(&self) -> String {
format!("__wbindgen_link_{}", crate::util::ShortHash(self))
}
}

/// The type of item being imported
#[cfg_attr(feature = "extra-traits", derive(Debug))]
#[derive(Clone)]
Expand Down
2 changes: 1 addition & 1 deletion crates/backend/src/codegen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ impl TryToTokens for ast::LinkToModule {
fn try_to_tokens(&self, tokens: &mut TokenStream) -> Result<(), Diagnostic> {
let mut program = TokenStream::new();
self.0.try_to_tokens(&mut program)?;
let link_function_name = self.0.linked_modules[0].link_function_name();
let link_function_name = self.0.link_function_name(0);
let name = Ident::new(&link_function_name, Span::call_site());
let abi_ret = quote! { <String as wasm_bindgen::convert::FromWasmAbi>::Abi };
let extern_fn = extern_fn(&name, &[], &[], &[], abi_ret);
Expand Down
6 changes: 4 additions & 2 deletions crates/backend/src/encode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,8 @@ fn shared_program<'a>(
linked_modules: prog
.linked_modules
.iter()
.map(|a| shared_linked_module(a, intern))
.enumerate()
.map(|(i, a)| shared_linked_module(&prog.link_function_name(i), a, intern))
.collect::<Result<Vec<_>, _>>()?,
local_modules: intern
.files
Expand Down Expand Up @@ -255,12 +256,13 @@ fn shared_import<'a>(i: &'a ast::Import, intern: &'a Interner) -> Result<Import<
}

fn shared_linked_module<'a>(
name: &str,
i: &'a ast::ImportModule,
intern: &'a Interner,
) -> Result<LinkedModule<'a>, Diagnostic> {
Ok(LinkedModule {
module: shared_module(i, intern)?,
link_function_name: intern.intern_str(&i.link_function_name()),
link_function_name: intern.intern_str(name),
})
}

Expand Down
23 changes: 19 additions & 4 deletions crates/cli-support/src/wit/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,12 @@ impl<'a> Context<'a> {
Ok(())
}

fn link_module(&mut self, id: ImportId, module: &decode::ImportModule) -> Result<(), Error> {
fn link_module(
&mut self,
id: ImportId,
module: &decode::ImportModule,
offset: usize,
) -> Result<(), Error> {
let descriptor = Function {
shim_idx: 0,
arguments: Vec::new(),
Expand All @@ -342,8 +347,12 @@ impl<'a> Context<'a> {
let path = match module {
decode::ImportModule::Named(n) => format!("snippets/{}", n),
decode::ImportModule::RawNamed(n) => n.to_string(),
decode::ImportModule::Inline(i) => {
format!("snippets/{}/inline{}.js", self.unique_crate_identifier, i)
decode::ImportModule::Inline(idx) => {
format!(
"snippets/{}/inline{}.js",
self.unique_crate_identifier,
*idx as usize + offset
)
}
};
self.aux.import_map.insert(id, AuxImport::LinkTo(path));
Expand Down Expand Up @@ -384,9 +393,15 @@ impl<'a> Context<'a> {
self.export(export)?;
}

let offset = self
.aux
.snippets
.get(unique_crate_identifier)
.map(|s| s.len())
.unwrap_or(0);
for module in linked_modules {
match self.function_imports.remove(module.link_function_name) {
Some((id, _)) => self.link_module(id, &module.module)?,
Some((id, _)) => self.link_module(id, &module.module, offset)?,
None => (),
}
}
Expand Down

0 comments on commit b26ef80

Please sign in to comment.