Skip to content

Commit

Permalink
Fix failing tests
Browse files Browse the repository at this point in the history
  • Loading branch information
eggyal committed Sep 1, 2019
1 parent 0aa19af commit df96753
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 17 deletions.
2 changes: 1 addition & 1 deletion crates/backend/src/codegen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1064,7 +1064,7 @@ impl TryToTokens for ast::ImportFunction {
&self.rust_name,
);

let const_name = encode::lookup_constant_for_fn(&self.rust_name);
let const_name = encode::lookup_constant_for_fn(rust_name);
let id_bytes = gen_id();

let invocation = quote! {
Expand Down
8 changes: 7 additions & 1 deletion crates/backend/src/encode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,13 @@ use crate::Diagnostic;

pub fn lookup_constant_for_fn(ident: &Ident) -> Ident {
Ident::new(
&format!("__WASM_BINDGEN_GENERATED_{}", ident.to_string().to_uppercase()),
&format!(
"__WASM_BINDGEN_GENERATED_{}",
ident
.to_string()
.trim_start_matches(&"r#")
.to_uppercase()
),
Span::call_site()
)
}
Expand Down
3 changes: 3 additions & 0 deletions crates/backend/src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,9 @@ impl<T: Hash> fmt::Display for ShortHash<T> {
env::var("CARGO_PKG_VERSION")
.expect("should have CARGO_PKG_VERSION env var")
.hash(&mut h);

std::time::SystemTime::now().hash(&mut h);

// This may chop off 32 bits on 32-bit platforms, but that's ok, we
// just want something to mix in below anyway.
HASH.store(h.finish() as usize, SeqCst);
Expand Down
52 changes: 37 additions & 15 deletions crates/cli-support/src/js/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ pub struct Context<'a> {
/// A map of the name of npm dependencies we've loaded so far to the path
/// they're defined in as well as their version specification.
pub npm_dependencies: HashMap<String, (PathBuf, String)>,
defined_exports: Vec<(String)>,
}

#[derive(Default)]
Expand Down Expand Up @@ -102,6 +103,7 @@ impl<'a> Context<'a> {
module,
memory,
npm_dependencies: Default::default(),
defined_exports: Default::default(),
})
}

Expand Down Expand Up @@ -183,11 +185,9 @@ impl<'a> Context<'a> {
}
id >>= 32;
}

self.expose_export_map();
self.expose_add_heap_object();
self.global(&format!(
"EXPORT_MAP[\"{}\"] = addHeapObject({});\n",

self.defined_exports.push(format!(
"'{}': {}",
result.into_iter().collect::<String>(),
definition_name,
));
Expand Down Expand Up @@ -247,7 +247,14 @@ impl<'a> Context<'a> {

// Cause any future calls to `should_write_global` to panic, making sure
// we don't ask for items which we can no longer emit.
drop(self.exposed_globals.take().unwrap());
let exposed_globals = self.exposed_globals.take().unwrap();
if exposed_globals.contains("definition_map") {
self.global(&format!(
"Object.assign(DEFINITION_MAP, {{\n{}\n}});",
self.defined_exports.join(",\n"),
));
}
drop(exposed_globals);

self.finalize_js(module_name, needs_manual_start)
}
Expand Down Expand Up @@ -1551,15 +1558,13 @@ impl<'a> Context<'a> {
));
}

fn expose_export_map(&mut self) {
if !self.should_write_global("export_map") {
return;
fn expose_definition_map(&mut self) {
if self.should_write_global("definition_map") {
self.global("
const DEFINITION_MAP = {};
const CACHED_DEFINITIONS = {};
");
}
self.global(
"
const EXPORT_MAP = {};
"
);
}

fn expose_handle_error(&mut self) -> Result<(), Error> {
Expand Down Expand Up @@ -2770,7 +2775,24 @@ impl<'a> Context<'a> {

Intrinsic::ExportGet => {
assert_eq!(args.len(), 2);
format!("EXPORT_MAP[`${{({}).toString(32)}}${{({}).toString(32)}}`]", args[0], args[1])
self.expose_definition_map();

let resolve_definition = if self.config.anyref {
self.expose_add_to_anyref_table()?;
"addToAnyrefTable"
} else {
self.expose_add_heap_object();
"addHeapObject"
};

prelude.push_str(&format!("
const str = `${{({}).toString(32)}}${{({}).toString(32)}}`;
if (!(str in CACHED_DEFINITIONS)) {{
CACHED_DEFINITIONS[str] = {}(DEFINITION_MAP[str]);
}}
", args[0], args[1], resolve_definition));

"CACHED_DEFINITIONS[str]".to_string()
}
};
Ok(expr)
Expand Down

0 comments on commit df96753

Please sign in to comment.