Skip to content

Commit df96753

Browse files
committed
Fix failing tests
1 parent 0aa19af commit df96753

File tree

4 files changed

+48
-17
lines changed

4 files changed

+48
-17
lines changed

crates/backend/src/codegen.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1064,7 +1064,7 @@ impl TryToTokens for ast::ImportFunction {
10641064
&self.rust_name,
10651065
);
10661066

1067-
let const_name = encode::lookup_constant_for_fn(&self.rust_name);
1067+
let const_name = encode::lookup_constant_for_fn(rust_name);
10681068
let id_bytes = gen_id();
10691069

10701070
let invocation = quote! {

crates/backend/src/encode.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,13 @@ use crate::Diagnostic;
1212

1313
pub fn lookup_constant_for_fn(ident: &Ident) -> Ident {
1414
Ident::new(
15-
&format!("__WASM_BINDGEN_GENERATED_{}", ident.to_string().to_uppercase()),
15+
&format!(
16+
"__WASM_BINDGEN_GENERATED_{}",
17+
ident
18+
.to_string()
19+
.trim_start_matches(&"r#")
20+
.to_uppercase()
21+
),
1622
Span::call_site()
1723
)
1824
}

crates/backend/src/util.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,9 @@ impl<T: Hash> fmt::Display for ShortHash<T> {
142142
env::var("CARGO_PKG_VERSION")
143143
.expect("should have CARGO_PKG_VERSION env var")
144144
.hash(&mut h);
145+
146+
std::time::SystemTime::now().hash(&mut h);
147+
145148
// This may chop off 32 bits on 32-bit platforms, but that's ok, we
146149
// just want something to mix in below anyway.
147150
HASH.store(h.finish() as usize, SeqCst);

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

Lines changed: 37 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ pub struct Context<'a> {
4949
/// A map of the name of npm dependencies we've loaded so far to the path
5050
/// they're defined in as well as their version specification.
5151
pub npm_dependencies: HashMap<String, (PathBuf, String)>,
52+
defined_exports: Vec<(String)>,
5253
}
5354

5455
#[derive(Default)]
@@ -102,6 +103,7 @@ impl<'a> Context<'a> {
102103
module,
103104
memory,
104105
npm_dependencies: Default::default(),
106+
defined_exports: Default::default(),
105107
})
106108
}
107109

@@ -183,11 +185,9 @@ impl<'a> Context<'a> {
183185
}
184186
id >>= 32;
185187
}
186-
187-
self.expose_export_map();
188-
self.expose_add_heap_object();
189-
self.global(&format!(
190-
"EXPORT_MAP[\"{}\"] = addHeapObject({});\n",
188+
189+
self.defined_exports.push(format!(
190+
"'{}': {}",
191191
result.into_iter().collect::<String>(),
192192
definition_name,
193193
));
@@ -247,7 +247,14 @@ impl<'a> Context<'a> {
247247

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

252259
self.finalize_js(module_name, needs_manual_start)
253260
}
@@ -1551,15 +1558,13 @@ impl<'a> Context<'a> {
15511558
));
15521559
}
15531560

1554-
fn expose_export_map(&mut self) {
1555-
if !self.should_write_global("export_map") {
1556-
return;
1561+
fn expose_definition_map(&mut self) {
1562+
if self.should_write_global("definition_map") {
1563+
self.global("
1564+
const DEFINITION_MAP = {};
1565+
const CACHED_DEFINITIONS = {};
1566+
");
15571567
}
1558-
self.global(
1559-
"
1560-
const EXPORT_MAP = {};
1561-
"
1562-
);
15631568
}
15641569

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

27712776
Intrinsic::ExportGet => {
27722777
assert_eq!(args.len(), 2);
2773-
format!("EXPORT_MAP[`${{({}).toString(32)}}${{({}).toString(32)}}`]", args[0], args[1])
2778+
self.expose_definition_map();
2779+
2780+
let resolve_definition = if self.config.anyref {
2781+
self.expose_add_to_anyref_table()?;
2782+
"addToAnyrefTable"
2783+
} else {
2784+
self.expose_add_heap_object();
2785+
"addHeapObject"
2786+
};
2787+
2788+
prelude.push_str(&format!("
2789+
const str = `${{({}).toString(32)}}${{({}).toString(32)}}`;
2790+
if (!(str in CACHED_DEFINITIONS)) {{
2791+
CACHED_DEFINITIONS[str] = {}(DEFINITION_MAP[str]);
2792+
}}
2793+
", args[0], args[1], resolve_definition));
2794+
2795+
"CACHED_DEFINITIONS[str]".to_string()
27742796
}
27752797
};
27762798
Ok(expr)

0 commit comments

Comments
 (0)