mirror of
https://github.com/extism/extism.git
synced 2026-01-08 21:38:13 -05:00
feat: per call context (#711)
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.
This commit is contained in:
@@ -17,6 +17,9 @@ done
|
||||
|
||||
cargo build --package extism-runtime-kernel --bin extism-runtime --release --target wasm32-unknown-unknown $CARGO_FLAGS
|
||||
cp target/wasm32-unknown-unknown/release/extism-runtime.wasm .
|
||||
wasm-strip extism-runtime.wasm
|
||||
mv extism-runtime.wasm ../runtime/src/extism-runtime.wasm
|
||||
|
||||
wasm-tools parse extism-context.wat -o extism-context.wasm
|
||||
wasm-merge --enable-reference-types ./extism-runtime.wasm runtime extism-context.wasm context -o ../runtime/src/extism-runtime.wasm
|
||||
rm extism-context.wasm
|
||||
rm extism-runtime.wasm
|
||||
wasm-strip ../runtime/src/extism-runtime.wasm
|
||||
|
||||
3
kernel/extism-context.wat
Normal file
3
kernel/extism-context.wat
Normal file
@@ -0,0 +1,3 @@
|
||||
(module
|
||||
(global (export "extism_context") (mut externref) (ref.null extern))
|
||||
)
|
||||
Reference in New Issue
Block a user