Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify dynamic linking imports tests #818

Merged
merged 2 commits into from
Nov 6, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 0 additions & 12 deletions crates/cli/tests/dynamic_linking_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,18 +63,6 @@ pub fn check_for_new_imports(builder: &mut Builder) -> Result<()> {
runner.assert_known_base_imports()
}

#[javy_cli_test(dyn = true, root = "tests/dynamic-linking-scripts")]
// If you need to change this test, then you've likely made a breaking change.
pub fn check_for_new_imports_for_exports(builder: &mut Builder) -> Result<()> {
let runner = builder
.input("linking-with-func.js")
.wit("linking-with-func.wit")
.world("foo-test")
.build()?;

runner.assert_known_named_function_imports()
}

#[javy_cli_test(dyn = true, root = "tests/dynamic-linking-scripts")]
pub fn test_dynamic_linking_with_arrow_fn(builder: &mut Builder) -> Result<()> {
let mut runner = builder
Expand Down
100 changes: 43 additions & 57 deletions crates/runner/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use anyhow::{anyhow, bail, Result};
use anyhow::{bail, Result};
use std::error::Error;
use std::fmt::{self, Display, Formatter};
use std::io::{self, Cursor, Write};
Expand Down Expand Up @@ -432,63 +432,49 @@ impl Runner {
let module = Module::from_binary(self.linker.engine(), &self.wasm)?;
let instance_name = self.plugin.namespace();
Copy link
Member

@saulecabrera saulecabrera Nov 6, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As a nit, given the description of the PR, could you rename this function reflect the intention? e.g., ensure_expected_imports (or something along those lines)


let result = module.imports().filter(|i| {
if i.module() == instance_name && i.name() == "canonical_abi_realloc" {
let ty = i.ty();
let f = ty.unwrap_func();
return f.params().all(|p| p.is_i32())
&& f.params().len() == 4
&& f.results().len() == 1
&& f.results().all(|r| r.is_i32());
}

if i.module() == instance_name && i.name() == "eval_bytecode" {
let ty = i.ty();
let f = ty.unwrap_func();
return f.params().all(|p| p.is_i32())
&& f.params().len() == 2
&& f.results().len() == 0;
}

if i.module() == instance_name && i.name() == "memory" {
let ty = i.ty();
return ty.memory().is_some();
}

false
});

let count = result.count();
if count == 3 {
Ok(())
} else {
Err(anyhow!("Unexpected number of imports: {}", count))
}
}

pub fn assert_known_named_function_imports(&self) -> Result<()> {
self.assert_known_base_imports()?;

let module = Module::from_binary(self.linker.engine(), &self.wasm)?;
let instance_name = self.plugin.namespace();
let result = module.imports().filter(|i| {
if i.module() == instance_name && i.name() == "invoke" {
let ty = i.ty();
let f = ty.unwrap_func();

return f.params().len() == 4
&& f.params().all(|p| p.is_i32())
&& f.results().len() == 0;
}
let imports = module
.imports()
.filter(|i| i.module() == instance_name)
.collect::<Vec<_>>();
assert_eq!(4, imports.len());

let realloc = imports
.iter()
.find(|i| i.name() == "canonical_abi_realloc")
.expect("Should have canonical_abi_realloc import");
let ty = realloc.ty();
let f = ty.unwrap_func();
assert!(f.params().all(|p| p.is_i32()));
assert_eq!(4, f.params().len());
assert!(f.results().all(|p| p.is_i32()));
assert_eq!(1, f.results().len());

let memory = imports
.iter()
.find(|i| i.name() == "memory" && i.ty().memory().is_some());
assert!(memory.is_some(), "Should have memory import");

let eval_bytecode = imports
.iter()
.find(|i| i.name() == "eval_bytecode")
.expect("Should have eval_bytecode import");
let ty = eval_bytecode.ty();
let f = ty.unwrap_func();
assert!(f.params().all(|p| p.is_i32()));
assert_eq!(2, f.params().len());
assert_eq!(0, f.results().len());

let invoke = imports
.iter()
.find(|i| i.name() == "invoke")
.expect("Should have eval_bytecode import");
let ty = invoke.ty();
let f = ty.unwrap_func();
assert!(f.params().all(|p| p.is_i32()));
assert_eq!(4, f.params().len());
assert_eq!(0, f.results().len());

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you replace the asserts with bail! or Err(anyhow::Error)? I get that we're asserting and that the runner is mostly used in tests, however, I'd like to ensure that errors are recoverable here so that failures can bubble up with as much as context as possible to the test invoking them.

false
});
let count = result.count();
if count == 1 {
Ok(())
} else {
Err(anyhow!("Unexpected number of imports: {}", count))
}
Ok(())
}

pub fn assert_producers(&self) -> Result<()> {
Expand Down
Loading