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

Support plugins with imported functions #881

Merged
merged 4 commits into from
Jan 22, 2025
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
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
Loading