-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add webassembly component model
This commit adds a webassembly component module for the policy engine. The motivation for this is to be able to run the policy engine in any webassembly runtime that supports the new WebAssembly Component Model. This commit include examples of running the policy engine in JavaScript, Python, and Rust. Closes: #227 Signed-off-by: Daniel Bevenius <[email protected]>
- Loading branch information
Showing
31 changed files
with
4,632 additions
and
471 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
** xref:component_model.adoc[`Component Model`] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
= WebAssembly Component Model | ||
|
||
This is a work in progress with the goal being to provide a WebAssembly | ||
component for the policy engine. | ||
|
||
This example uses wit-bindgen macros to generate the Rust types, but this can | ||
also be done manually using the wit-bindgen-cli. First we need to install | ||
wit-bindgen-cli: | ||
[listing] | ||
$ cargo install --git https://github.com/bytecodealliance/wit-bindgen wit-bindgen-cli | ||
|
||
== WebAssembly Interface Types | ||
The main interface is defined in link:https://github.com/seedwing-io/seedwing-policy/tree/main/engine/wit/engine-world.wit[wit/engine-world.wit] | ||
which contains on the exposed functions. The data types are defined in | ||
link:https://github.com/seedwing-io/seedwing-policy/tree/main/engine/wit/engine-types.wit[wit/engine-types.wit]. | ||
|
||
== Building | ||
To build the WebAssembly component: | ||
[listing] | ||
$ make wit-compile | ||
cargo b -p seedwing-policy-engine --target=wasm32-wasi --no-default-features --features="" | ||
|
||
Note that `wit` stands for `WebAssembly Interface Types`. | ||
|
||
The above compilation will generate a core WebAssembly module which can be found | ||
in the `target` directory in the root of the checked out github repository. | ||
|
||
The next step is to create the WebAssembly component using this core WebAssembly | ||
module: | ||
[listing] | ||
$ make wit-component | ||
wasm-tools component new -v ../target/wasm32-wasi/debug/seedwing_policy_engine.wasm --adapt wasi_snapshot_preview1.wasm -o seedwing_policy-engine-component.wasm | ||
|
||
The above will build an optimized release build which is needed or the execution | ||
of the Rust example will be very slow. But is can be nice to build an | ||
unopptimized version: | ||
[listing] | ||
$ make wit-compile Build=debug | ||
|
||
Just make sure to also use `Build=debug` for the Rust targets or it will default | ||
to a release build and you might get an runtime error depending on what changes | ||
that have been made to the source code. | ||
|
||
== JavaScript example | ||
The directory link:https://github.com/seedwing-io/seedwing-policy/tree/main/engine/wit-examples/javascript/README.md[javascript] | ||
contains a JavaScript example of using the webassembly component. There are more | ||
details in the readme, but the example can be run directly using: | ||
[listing] | ||
$ make wit-java-bindings | ||
$ make wit-java-run | ||
|
||
== Python example | ||
The directory link:https://github.com/seedwing-io/seedwing-policy/tree/main/engine/wit-examples/python/README.md[python] contains a Python example of using | ||
the webassembly component. There are more details in the readme, but the example | ||
can be run directly | ||
using: | ||
[listing] | ||
$ make wit-python-bindings | ||
$ make wit-python-run | ||
|
||
== Rust example | ||
The directory link:https://github.com/seedwing-io/seedwing-policy/tree/main/engine/wit-examples/rust/README.md[rust] contains | ||
a Rust example of using the webassembly component. There are more details in the | ||
readme, but the example can be run directly | ||
using: | ||
[listing] | ||
$ make wit-rust-bindings | ||
$ make wit-rust-run | ||
|
||
This example uses wit-bindgen macros so the bindings step is not required here | ||
which is done for the other examples. | ||
|
||
== Go example | ||
There is project named https://github.com/bytecodealliance/wasmtime-go[wasmtime-go] | ||
which looked like it would be able to to do the same/simlar thing as the other | ||
examples. But it turns out that it does not support the WebAssembly Component | ||
Model yet as mentioned in | ||
https://github.com/bytecodealliance/wasmtime-go/issues/170[issue-170]. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
[target.wasm32-wasi] | ||
rustflags = ["--cfg=tokio_unstable"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
engine.rs |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
|
||
ifeq ($(Build), debug) | ||
BUILD_TYPE = debug | ||
else | ||
BUILD_TYPE = release | ||
BUILD = "--$(BUILD_TYPE)" | ||
endif | ||
|
||
corewasm=../target/wasm32-wasi/${BUILD_TYPE}/seedwing_policy_engine.wasm | ||
component=../target/seedwing-policy-engine-component.wasm | ||
|
||
|
||
# Seedwing WebAssembly Component targets | ||
wit-compile: | ||
cargo b ${BUILD} -p seedwing-policy-engine --target=wasm32-wasi --no-default-features --features="" | ||
|
||
# Can be used to inspect the custom section "component-type::engine-world" | ||
objdump-core-module: | ||
@wasm-tools objdump $(corewasm) | ||
|
||
test_from_engine_runtime: | ||
cargo t -p seedwing-policy-engine --target=wasm32-wasi \ | ||
--no-default-features --features="" \ | ||
--lib core::wit::test::from_engine_runtime -- --exact --show-output | ||
|
||
wit-component: | ||
wasm-tools component new \ | ||
-v $(corewasm) \ | ||
--adapt wasi_snapshot_preview1=wit-lib/wasi_preview1_component_adapter.wasm \ | ||
-o $(component) | ||
@wasm-tools strip $(component) -o $(component) | ||
|
||
wit-print-wat: | ||
wasm-tools print $(component) | ||
|
||
wit-print: | ||
@wasm-tools component wit $(component) | ||
|
||
objdump-component: | ||
@wasm-tools objdump $(component) | ||
|
||
# Can be used to generate the Rust source for that the bindgen::generate | ||
# macro will produce. | ||
wit-bindgen: | ||
wit-bindgen rust wit/ | ||
|
||
# Can be useful to inspect the expanded wit-bindgen macros | ||
cargo-expand: | ||
cargo expand -p seedwing-policy-engine --target=wasm32-wasi --no-default-features --features="" | ||
|
||
# JavaScript targets | ||
wit-javascript-bindings: | ||
cd wit-examples/javascript && npm run bindings | ||
|
||
wit-javascript-run: | ||
cd wit-examples/javascript && npm start | ||
|
||
wit-javascript-all: wit-compile wit-component wit-js-build | ||
|
||
# Python targets | ||
wit-python-bindings: | ||
@cd wit-examples/python && make --no-print-directory bindings | ||
|
||
wit-python-run: | ||
@cd wit-examples/python && make --no-print-directory run | ||
|
||
wit-python-all: wit-compile wit-component wit-python-bindings wit-python-run | ||
|
||
# Rust targets | ||
wit-rust-bindings: | ||
cd wit-examples/rust && cargo b ${BUILD} | ||
|
||
wit-rust-run: | ||
cd wit-examples/rust && cargo r ${BUILD} | ||
|
||
wit-rust-all: wit-rust-run |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.