mirror of
https://github.com/fluencelabs/wasm-bindgen
synced 2025-06-19 07:51:26 +00:00
Add reference output tests for JS operations (#1894)
* Add reference output tests for JS operations This commit starts adding a test suite which checks in, to the repository, test assertions for both the JS and wasm file outputs of a Rust crate compiled with `#[wasm_bindgen]`. These aren't intended to be exhaustive or large scale tests, but rather micro-tests to help observe the changes in `wasm-bindgen`'s output over time. The motivation for this commit is basically overhauling how all the GC passes work in `wasm-bindgen` today. The reorganization is also included in this commit as well. Previously `wasm-bindgen` would, in an ad-hoc fashion, run the GC passes of `walrus` in a bunch of places to ensure that less "garbage" was seen by future passes. This not only was a source of slowdown but it also was pretty brittle since `wasm-bindgen` kept breaking if extra iteams leaked through. The strategy taken in this commit is to have one precise location for a GC pass, and everything goes through there. This is achieved by: * All internal exports are removed immediately when generating the nonstandard wasm interface types section. Internal exports, intrinsics, and runtime support are all referenced by the various instructions and/or sections that use them. This means that we now have precise tracking of what an adapter uses. * This in turn enables us to implement the `add_gc_roots` function for `walrus` custom sections, which in turn allows walrus GC passes to do what `unexport_unused_intrinsics` did before. That function is now no longer necessary, but effectively works the same way. All intrinsics are unexported at the beginning and then they're selectively re-imported and re-exported through the JS glue generation pass as necessary and defined by the bindings. * Passes like the `anyref` pass are now much more precise about the intrinsics that they work with. The `anyref` pass also deletes any internal intrinsics found and also does some rewriting of the adapters aftewards now to hook up calls to the heap count import to the heap count intrinsic in the wasm module. * Fix handling of __wbindgen_realloc The final user of the `require_internal_export` function was `__wbindgen_realloc`. This usage has now been removed by updating how we handle usage of the `realloc` function. The wasm interface types standard doesn't have a `realloc` function slot, nor do I think it ever will. This means that as a polyfill for wasm interface types we'll always have to support the lack of `realloc`. For direct Rust to JS, however, we can still optionally handle `realloc`. This is all handled with a few internal changes. * Custom `StringToMemory` instructions now exist. These have an extra `realloc` slot to store an intrinsic, if found. * Our custom instructions are lowered to the standard instructions when generating an interface types section. * The `realloc` function, if present, is passed as an argument like the malloc function when passing strings to wasm. If it's not present we use a slower fallback, but if it's present we use the faster implementation. This should mean that there's little-to-no impact on existing users of `wasm-bindgen`, but this should continue to still work for wasm interface types polyfills and such. Additionally the GC passes now work in that they don't delete `__wbindgen_realloc` which we later try to reference. * Add an empty test for the anyref pass * Precisely track I32FromOptionAnyref's dependencies This depends on the anyref table and a function to allocate an index if the anyref pass is running, so be sure to track that in the instruction itself for GC rooting. * Trim extraneous exports from nop anyref module Or if you're otherwise not using anyref slices, don't force some intrinsics to exist. * Remove globals from reference tests Looks like these values adjust in slight but insignificant ways over time * Update the anyref xform tests
This commit is contained in:
@ -83,6 +83,8 @@ pub fn process(
|
||||
|
||||
cx.verify()?;
|
||||
|
||||
cx.unexport_intrinsics();
|
||||
|
||||
let adapters = cx.module.customs.add(cx.adapters);
|
||||
let aux = cx.module.customs.add(cx.aux);
|
||||
Ok((adapters, aux))
|
||||
@ -502,9 +504,15 @@ impl<'a> Context<'a> {
|
||||
// itself but to the adapter shim we generated, so fetch that shim id
|
||||
// and flag it as catch here. This basically just needs to be kept in
|
||||
// sync with `js/mod.rs`.
|
||||
let adapter = self.adapters.implements.last().unwrap().1;
|
||||
//
|
||||
// For `catch` once we see that we'll need an internal intrinsic later
|
||||
// for JS glue generation, so be sure to find that here.
|
||||
let adapter = self.adapters.implements.last().unwrap().2;
|
||||
if *catch {
|
||||
self.aux.imports_with_catch.insert(adapter);
|
||||
if self.aux.exn_store.is_none() {
|
||||
self.find_exn_store();
|
||||
}
|
||||
}
|
||||
if *assert_no_shim {
|
||||
self.aux.imports_with_assert_no_shim.insert(adapter);
|
||||
@ -983,7 +991,7 @@ impl<'a> Context<'a> {
|
||||
};
|
||||
self.adapters
|
||||
.implements
|
||||
.push((import_id, walrus2us[&i.adapter_func]));
|
||||
.push((import_id, i.core_func, walrus2us[&i.adapter_func]));
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
@ -995,7 +1003,7 @@ impl<'a> Context<'a> {
|
||||
// `$PLACEHOLDER_MODULE` are connected to an adapter via the
|
||||
// `implements` section.
|
||||
let mut implemented = HashMap::new();
|
||||
for (core, adapter) in self.adapters.implements.iter() {
|
||||
for (core, _, adapter) in self.adapters.implements.iter() {
|
||||
implemented.insert(core, adapter);
|
||||
}
|
||||
for import in self.module.imports.iter() {
|
||||
@ -1006,6 +1014,15 @@ impl<'a> Context<'a> {
|
||||
walrus::ImportKind::Function(_) => {}
|
||||
_ => bail!("import from `{}` was not a function", PLACEHOLDER_MODULE),
|
||||
}
|
||||
|
||||
// These are special intrinsics which were handled in the descriptor
|
||||
// phase, but we don't have an implementation for them. We don't
|
||||
// need to error about them in this verification pass though,
|
||||
// having them lingering in the module is normal.
|
||||
if import.name == "__wbindgen_describe" || import.name == "__wbindgen_describe_closure"
|
||||
{
|
||||
continue;
|
||||
}
|
||||
if implemented.remove(&import.id()).is_none() {
|
||||
bail!("import of `{}` doesn't have an adapter listed", import.name);
|
||||
}
|
||||
@ -1065,6 +1082,10 @@ impl<'a> Context<'a> {
|
||||
let import = self.module.imports.get(import);
|
||||
let (import_module, import_name) = (import.module.clone(), import.name.clone());
|
||||
let import_id = import.id();
|
||||
let core_id = match import.kind {
|
||||
walrus::ImportKind::Function(f) => f,
|
||||
_ => bail!("bound import must be assigned to function"),
|
||||
};
|
||||
|
||||
// Process the returned type first to see if it needs an out-pointer. This
|
||||
// happens if the results of the incoming arguments translated to wasm take
|
||||
@ -1131,7 +1152,7 @@ impl<'a> Context<'a> {
|
||||
.cx
|
||||
.adapters
|
||||
.append(args.input, results, AdapterKind::Local { instructions });
|
||||
args.cx.adapters.implements.push((import_id, id));
|
||||
args.cx.adapters.implements.push((import_id, core_id, id));
|
||||
Ok(f)
|
||||
}
|
||||
|
||||
@ -1233,6 +1254,13 @@ impl<'a> Context<'a> {
|
||||
.ok_or_else(|| anyhow!("failed to find declaration of `__wbindgen_malloc` in module"))
|
||||
}
|
||||
|
||||
fn realloc(&self) -> Option<FunctionId> {
|
||||
self.function_exports
|
||||
.get("__wbindgen_realloc")
|
||||
.cloned()
|
||||
.map(|p| p.1)
|
||||
}
|
||||
|
||||
fn free(&self) -> Result<FunctionId, Error> {
|
||||
self.function_exports
|
||||
.get("__wbindgen_free")
|
||||
@ -1245,6 +1273,45 @@ impl<'a> Context<'a> {
|
||||
self.memory
|
||||
.ok_or_else(|| anyhow!("failed to find memory declaration in module"))
|
||||
}
|
||||
|
||||
/// Removes the export item for all `__wbindgen` intrinsics which are
|
||||
/// generally only purely internal helpers.
|
||||
///
|
||||
/// References to these functions are preserved through adapter instructions
|
||||
/// if necessary, otherwise they can all be gc'd out. By the time this
|
||||
/// function is called our discovery of these intrinsics has completed and
|
||||
/// there's no need to keep around these exports.
|
||||
fn unexport_intrinsics(&mut self) {
|
||||
let mut to_remove = Vec::new();
|
||||
for export in self.module.exports.iter() {
|
||||
match export.name.as_str() {
|
||||
n if n.starts_with("__wbindgen") => {
|
||||
to_remove.push(export.id());
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
for id in to_remove {
|
||||
self.module.exports.delete(id);
|
||||
}
|
||||
}
|
||||
|
||||
/// Attempts to locate the `__wbindgen_exn_store` intrinsic and stores it in
|
||||
/// our auxiliary information.
|
||||
///
|
||||
/// This is only invoked if the intrinsic will actually be needed for JS
|
||||
/// glue generation somewhere.
|
||||
fn find_exn_store(&mut self) {
|
||||
self.aux.exn_store = self
|
||||
.module
|
||||
.exports
|
||||
.iter()
|
||||
.find(|e| e.name == "__wbindgen_exn_store")
|
||||
.and_then(|e| match e.item {
|
||||
walrus::ExportItem::Function(f) => Some(f),
|
||||
_ => None,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
fn extract_programs<'a>(
|
||||
|
Reference in New Issue
Block a user