Skip to content

Commit

Permalink
Support plugins with imported functions (#881)
Browse files Browse the repository at this point in the history
* Support plugins with imported functions

* Move define_unknown_imports_as_trap before instantiate

* Also handle static case in test runner

* Exclude test-plugin from unit testing (it has no tests)
  • Loading branch information
jeffcharles authored Jan 22, 2025
1 parent 4186527 commit 3a08b45
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 6 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ jobs:
- name: Test
env:
CARGO_TARGET_WASM32_WASIP1_RUNNER: wasmtime --dir=.
run: cargo hack test --target=wasm32-wasip1 --workspace --exclude=javy-cli --exclude=javy-runner --each-feature -- --nocapture
run: cargo hack test --target=wasm32-wasip1 --workspace --exclude=javy-cli --exclude=javy-runner --exclude=javy-test-plugin --each-feature -- --nocapture

- name: Test Runner
run: cargo test --package=javy-runner
Expand Down
1 change: 1 addition & 0 deletions crates/cli/src/bytecode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ fn create_wasm_env(plugin: &Plugin) -> Result<(Store<WasiCtx>, Instance, Memory)
&mut linker,
|s| s,
)?;
linker.define_unknown_imports_as_traps(&module)?;
let wasi = WasiCtxBuilder::new().inherit_stderr().build();
let mut store = Store::new(&engine, wasi);
let instance = linker.instantiate(store.as_context_mut(), &module)?;
Expand Down
4 changes: 4 additions & 0 deletions crates/runner/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -669,12 +669,16 @@ impl Runner {

if let Some((name, bytes)) = &self.preload {
let module = Module::from_binary(self.linker.engine(), bytes)?;
// Allow unknown imports for dynamically linked `test-plugin`.
self.linker.define_unknown_imports_as_traps(&module)?;
let instance = self.linker.instantiate(store.as_context_mut(), &module)?;
self.linker.allow_shadowing(true);
self.linker
.instance(store.as_context_mut(), name, instance)?;
}

// Allow unknown imports for statically linked `test-plugin`.
self.linker.define_unknown_imports_as_traps(&module)?;
let instance = self.linker.instantiate(store.as_context_mut(), &module)?;
let run = instance.get_typed_func::<(), ()>(store.as_context_mut(), func)?;

Expand Down
17 changes: 12 additions & 5 deletions crates/test-plugin/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,25 @@
//! Plugin used for testing. We need a plugin with slightly different behavior
//! to validate a plugin is actually used when it should be.
use javy_plugin_api::{import_namespace, Config};
use javy_plugin_api::{import_namespace, javy::quickjs::prelude::Func, Config};

import_namespace!("test_plugin");

#[link(wasm_import_module = "some_host")]
extern "C" {
fn imported_function();
}

#[export_name = "initialize_runtime"]
pub extern "C" fn initialize_runtime() {
let config = Config::default();
javy_plugin_api::initialize_runtime(config, |runtime| {
runtime
.context()
.with(|ctx| ctx.globals().set("plugin", true))
.unwrap();
runtime.context().with(|ctx| {
ctx.globals().set("plugin", true).unwrap();
ctx.globals()
.set("func", Func::from(|| unsafe { imported_function() }))
.unwrap();
});
runtime
})
.unwrap();
Expand Down

0 comments on commit 3a08b45

Please sign in to comment.