mirror of
https://github.com/extism/extism.git
synced 2026-01-08 21:38:13 -05:00
Add `plugin.call_with_host_context` and `current_plugin.host_context` methods, enabling per-call context to be looped from the guest invocation to any host functions it calls. In an HTTP server environment, this enables re-using a plugin across multiple requests while switching out backing connections and user information in host functions. (Imagine a host function, `update_user` -- previously the plugin would have to have been aware of the user to pass to the host function. Now that information is ambient.) This is a backwards-compatible change and requires no changes to existing plugins. Implement by adding a global, mutable externref to the extism kernel. Since most programming languages, including Rust, don't let you define these natively, we accomplish this by using `wasm-merge` to combine the kernel Wasm with Wasm generated by a WAT file containing only the global. (This pattern might be useful for other Wasm constructs we can't use directly from Rust, like `v128` in argument parameters.) Wasmtime requires extern refs to be `Any + Send + Sync + 'static`; we additionally add `Clone`. I haven't tried this with an `Arc` directly, but it should work at least for container structs that hold `Arc`'s themselves.
Extism kernel
The Extism kernel implements core parts of the Extism runtime in Rust compiled to WebAssembly. This code is a conceptual re-write of memory.rs with the goal of making core parts of the Extism implementation more portable across WebAssembly runtimes.
See lib.rs for more details about the implementation itself.
Building
Because this crate is built using the wasm32-unknown-unknown target, it is a separate build process from the extism-runtime crate.
To build extism-runtime.wasm, strip it and copy it to the proper location in the extism-runtime tree you can run:
$ sh build.sh