Skip to content

Commit

Permalink
Refactor plugin use in test infra (#809)
Browse files Browse the repository at this point in the history
  • Loading branch information
jeffcharles authored Nov 4, 2024
1 parent af56e01 commit acd08d3
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 32 deletions.
59 changes: 34 additions & 25 deletions crates/runner/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,23 @@ pub enum JavyCommand {
Compile,
}

#[derive(Clone)]
pub enum Plugin {
V2,
Default,
}

impl Plugin {
pub fn namespace(&self) -> &'static str {
match self {
Self::V2 => "javy_quickjs_provider_v2",
// Could try and derive this but not going to for now since tests
// will break if it changes.
Self::Default => "javy_quickjs_provider_v3",
}
}
}

#[derive(Clone)]
pub struct Builder {
/// The JS source.
Expand Down Expand Up @@ -46,9 +63,9 @@ pub struct Builder {
preload: Option<(String, PathBuf)>,
/// Whether to use the `compile` or `build` command.
command: JavyCommand,
/// The javy plugin version.
/// The javy plugin.
/// Used for import validation purposes only.
plugin_version: u8,
plugin: Plugin,
}

impl Default for Builder {
Expand All @@ -67,7 +84,7 @@ impl Default for Builder {
javy_json: None,
simd_json_builtins: None,
text_encoding: None,
plugin_version: 3,
plugin: Plugin::Default,
}
}
}
Expand Down Expand Up @@ -133,8 +150,8 @@ impl Builder {
self
}

pub fn plugin_version(&mut self, vsn: u8) -> &mut Self {
self.plugin_version = vsn;
pub fn plugin(&mut self, plugin: Plugin) -> &mut Self {
self.plugin = plugin;
self
}

Expand Down Expand Up @@ -163,25 +180,17 @@ impl Builder {
built: _,
preload,
command,
plugin_version,
plugin,
} = std::mem::take(self);

self.built = true;

match command {
JavyCommand::Compile => {
if let Some(preload) = preload {
Runner::compile_dynamic(
bin_path,
root,
input,
wit,
world,
preload,
plugin_version,
)
Runner::compile_dynamic(bin_path, root, input, wit, world, preload, plugin)
} else {
Runner::compile_static(bin_path, root, input, wit, world, plugin_version)
Runner::compile_static(bin_path, root, input, wit, world, plugin)
}
}
JavyCommand::Build => Runner::build(
Expand All @@ -206,7 +215,7 @@ pub struct Runner {
linker: Linker<StoreContext>,
initial_fuel: u64,
preload: Option<(String, Vec<u8>)>,
plugin_version: u8,
plugin: Plugin,
}

#[derive(Debug)]
Expand Down Expand Up @@ -311,7 +320,7 @@ impl Runner {
linker,
initial_fuel: u64::MAX,
preload,
plugin_version: 3,
plugin: Plugin::Default,
})
}

Expand All @@ -321,7 +330,7 @@ impl Runner {
source: impl AsRef<Path>,
wit: Option<PathBuf>,
world: Option<String>,
vsn: u8,
plugin: Plugin,
) -> Result<Self> {
// This directory is unique and will automatically get deleted
// when `tempdir` goes out of scope.
Expand All @@ -344,7 +353,7 @@ impl Runner {
linker,
initial_fuel: u64::MAX,
preload: None,
plugin_version: vsn,
plugin,
})
}

Expand All @@ -355,7 +364,7 @@ impl Runner {
wit: Option<PathBuf>,
world: Option<String>,
preload: (String, PathBuf),
vsn: u8,
plugin: Plugin,
) -> Result<Self> {
let tempdir = tempfile::tempdir()?;
let wasm_file = Self::out_wasm(&tempdir);
Expand All @@ -378,7 +387,7 @@ impl Runner {
linker,
initial_fuel: u64::MAX,
preload: Some((preload.0, preload_module)),
plugin_version: vsn,
plugin,
})
}

Expand All @@ -389,13 +398,13 @@ impl Runner {
linker: Self::setup_linker(&engine)?,
initial_fuel: u64::MAX,
preload: None,
plugin_version: 3,
plugin: Plugin::Default,
})
}

pub fn assert_known_base_imports(&self) -> Result<()> {
let module = Module::from_binary(self.linker.engine(), &self.wasm)?;
let instance_name = format!("javy_quickjs_provider_v{}", self.plugin_version);
let instance_name = self.plugin.namespace();

let result = module.imports().filter(|i| {
if i.module() == instance_name && i.name() == "canonical_abi_realloc" {
Expand Down Expand Up @@ -435,7 +444,7 @@ impl Runner {
self.assert_known_base_imports()?;

let module = Module::from_binary(self.linker.engine(), &self.wasm)?;
let instance_name = format!("javy_quickjs_provider_v{}", self.plugin_version);
let instance_name = self.plugin.namespace();
let result = module.imports().filter(|i| {
if i.module() == instance_name && i.name() == "invoke" {
let ty = i.ty();
Expand Down
15 changes: 8 additions & 7 deletions crates/test-macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -291,11 +291,13 @@ fn expand_cli_tests(test_config: &CliTestConfig, func: syn::ItemFn) -> Result<To
if command_name == "Compile" {
quote! {
let root = std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR"));
let plugin = javy_runner::Plugin::V2;
let namespace = plugin.namespace();
builder.plugin(plugin);
builder.preload(
"javy_quickjs_provider_v2".into(),
namespace.into(),
root.join("src").join("javy_quickjs_provider_v2.wasm")
);
builder.plugin_version(2);
}
} else {
quote! {
Expand All @@ -308,11 +310,10 @@ fn expand_cli_tests(test_config: &CliTestConfig, func: syn::ItemFn) -> Result<To
.join("release")
.join("plugin_wizened.wasm"),
);
// TODO: Deriving the current plugin version could be done
// automatically somehow. It's fine for now, given that if the
// version changes and this is not updated, tests will fail.
builder.preload("javy_quickjs_provider_v3".into(), root);
builder.plugin_version(3);
let plugin = javy_runner::Plugin::Default;
let namespace = plugin.namespace();
builder.plugin(plugin);
builder.preload(namespace.into(), root);
}
}
} else {
Expand Down

0 comments on commit acd08d3

Please sign in to comment.