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 all commits
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
14 changes: 1 addition & 13 deletions crates/cli/tests/dynamic_linking_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,19 +60,7 @@ fn test_errors_in_exported_functions_are_correctly_reported(builder: &mut Builde
// If you need to change this test, then you've likely made a breaking change.
pub fn check_for_new_imports(builder: &mut Builder) -> Result<()> {
let runner = builder.input("console.js").build()?;
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()
runner.ensure_expected_imports()
}

#[javy_cli_test(dyn = true, root = "tests/dynamic-linking-scripts")]
Expand Down
102 changes: 49 additions & 53 deletions crates/runner/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -428,67 +428,63 @@ impl Runner {
})
}

pub fn assert_known_base_imports(&self) -> Result<()> {
pub fn ensure_expected_imports(&self) -> Result<()> {
let module = Module::from_binary(self.linker.engine(), &self.wasm)?;
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 instance_name = self.plugin.namespace();

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))
let imports = module
.imports()
.filter(|i| i.module() == instance_name)
.collect::<Vec<_>>();
if imports.len() != 4 {
bail!("Dynamically linked modules should have exactly 4 imports for {instance_name}");
}
}

pub fn assert_known_named_function_imports(&self) -> Result<()> {
self.assert_known_base_imports()?;
let realloc = imports
.iter()
.find(|i| i.name() == "canonical_abi_realloc")
.ok_or_else(|| anyhow!("Should have canonical_abi_realloc import"))?;
let ty = realloc.ty();
let f = ty.unwrap_func();
if !f.params().all(|p| p.is_i32()) || f.params().len() != 4 {
bail!("canonical_abi_realloc should accept 4 i32s as parameters");
}
if !f.results().all(|p| p.is_i32()) || f.results().len() != 1 {
bail!("canonical_abi_realloc should return 1 i32 as a result");
}

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;
}
imports
.iter()
.find(|i| i.name() == "memory" && i.ty().memory().is_some())
.ok_or_else(|| anyhow!("Should have memory import named memory"))?;

let eval_bytecode = imports
.iter()
.find(|i| i.name() == "eval_bytecode")
.ok_or_else(|| anyhow!("Should have eval_bytecode import"))?;
let ty = eval_bytecode.ty();
let f = ty.unwrap_func();
if !f.params().all(|p| p.is_i32()) || f.params().len() != 2 {
bail!("eval_bytecode should accept 2 i32s as parameters");
}
if f.results().len() != 0 {
bail!("eval_bytecode should return no results");
}

false
});
let count = result.count();
if count == 1 {
Ok(())
} else {
Err(anyhow!("Unexpected number of imports: {}", count))
let invoke = imports
.iter()
.find(|i| i.name() == "invoke")
.ok_or_else(|| anyhow!("Should have invoke import"))?;
let ty = invoke.ty();
let f = ty.unwrap_func();
if !f.params().all(|p| p.is_i32()) || f.params().len() != 4 {
bail!("invoke should accept 4 i32s as parameters");
}
if f.results().len() != 0 {
bail!("invoke should return no results");
}

Ok(())
}

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