-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix binding generation for lists; add cli test (#46)
* fix binding generation for lists; add cli test This also: - moves the `examples/http/wit` directory to a shared location - bumps the version to 0.7.1 Signed-off-by: Joel Dice <joel.dice@fermyon.com> * update `sys.argv` at runtime; add matrix-math example This ensures we get the runtime set of CLI args rather than the empty set present during pre-init. Signed-off-by: Joel Dice <joel.dice@fermyon.com> * use `Once` to avoid redundant work in runtime; update example instructions Signed-off-by: Joel Dice <joel.dice@fermyon.com> --------- Signed-off-by: Joel Dice <joel.dice@fermyon.com>
- Loading branch information
Showing
47 changed files
with
156 additions
and
62 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
[package] | ||
name = "componentize-py" | ||
version = "0.7.0" | ||
version = "0.7.1" | ||
edition = "2021" | ||
exclude = ["cpython"] | ||
|
||
|
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,35 @@ | ||
# Example: `cli` | ||
|
||
This is an example of how to use [componentize-py] and [Wasmtime] to build and | ||
run a Python-based component targetting the [wasi-cli] `command` world. | ||
|
||
Note that, as of this writing, `wasi-cli` has not yet stabilized. Here we use a | ||
snapshot, which may differ from later revisions. | ||
|
||
[componentize-py]: https://github.com/bytecodealliance/componentize-py | ||
[Wasmtime]: https://github.com/bytecodealliance/wasmtime | ||
[wasi-cli]: https://github.com/WebAssembly/wasi-cli | ||
|
||
## Prerequisites | ||
|
||
* `Wasmtime` 15.0.1 (later versions may use a different, incompatible `wasi-cli` snapshot) | ||
* `componentize-py` 0.7.1 | ||
|
||
Below, we use [Rust](https://rustup.rs/)'s `cargo` to install `Wasmtime` since, | ||
as of this writing, 15.0.1 has not yet been released. Once it has been | ||
released, you'll be able to download and install from | ||
https://github.com/bytecodealliance/wasmtime/releases/tag/v15.0.1. | ||
|
||
``` | ||
cargo install --locked --git https://github.com/bytecodealliance/wasmtime --branch release-15.0.0 wasmtime-cli | ||
pip install componentize-py | ||
``` | ||
|
||
## Running the demo | ||
|
||
``` | ||
componentize-py -d ../../wit -w wasi:cli/command@0.2.0-rc-2023-11-10 componentize app -o cli.wasm | ||
wasmtime run --wasm component-model cli.wasm | ||
``` | ||
|
||
The `wasmtime run` command above should print "Hello, world!". |
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,5 @@ | ||
from command import exports | ||
|
||
class Run(exports.Run): | ||
def run(self): | ||
print("Hello, world!") |
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 was deleted.
Oops, something went wrong.
This file was deleted.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
# Example: `matrix-math` | ||
|
||
This is an example of how to use [componentize-py] to build a CLI app that does | ||
matrix multiplication using [NumPy] inside a sandboxed environment. This | ||
demonstrates using a non-trivial Python package containing native extensions | ||
within a guest component. | ||
|
||
[componentize-py]: https://github.com/bytecodealliance/componentize-py | ||
[NumPy]: https://numpy.org | ||
|
||
## Prerequisites | ||
|
||
* `wasmtime` 15.0.1 (later versions may use a different, incompatible `wasi-cli` snapshot) | ||
* `componentize-py` 0.7.1 | ||
* `NumPy`, built for WASI | ||
|
||
Note that we use an unofficial build of NumPy since the upstream project does | ||
not yet publish WASI builds. | ||
|
||
Below, we use [Rust](https://rustup.rs/)'s `cargo` to install `Wasmtime` since, | ||
as of this writing, 15.0.1 has not yet been released. Once it has been | ||
released, you'll be able to download and install from | ||
https://github.com/bytecodealliance/wasmtime/releases/tag/v15.0.1. | ||
|
||
``` | ||
cargo install --locked --git https://github.com/bytecodealliance/wasmtime --branch release-15.0.0 wasmtime-cli | ||
pip install componentize-py | ||
curl -OL https://github.com/dicej/wasi-wheels/releases/download/canary/numpy-wasi.tar.gz | ||
tar xf numpy-wasi.tar.gz | ||
``` | ||
|
||
## Running the demo | ||
|
||
``` | ||
componentize-py -d wit -w matrix-math componentize app -o matrix-math.wasm | ||
wasmtime run --wasm component-model matrix-math.wasm '[[1, 2], [4, 5], [6, 7]]' '[[1, 2, 3], [4, 5, 6]]' | ||
``` | ||
|
||
The second command above should print the following: | ||
|
||
``` | ||
matrix_multiply received arguments [[1, 2], [4, 5], [6, 7]] and [[1, 2, 3], [4, 5, 6]] | ||
[[9, 12, 15], [24, 33, 42], [34, 47, 60]] | ||
``` | ||
|
||
If you run into any problems, please file an issue! |
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,23 @@ | ||
# This app can either be used as a library (by calling `matrix-math#multiply`) | ||
# or a CLI command (by calling `wasi:cli/run#run`) | ||
|
||
import sys | ||
import numpy | ||
import matrix_math | ||
from matrix_math import exports | ||
from matrix_math.types import Err | ||
|
||
class MatrixMath(matrix_math.MatrixMath): | ||
def multiply(self, a: list[list[float]], b: list[list[float]]) -> list[list[float]]: | ||
print(f"matrix_multiply received arguments {a} and {b}") | ||
return numpy.matmul(a, b).tolist() | ||
|
||
class Run(exports.Run): | ||
def run(self): | ||
args = sys.argv[1:] | ||
if len(args) != 2: | ||
print(f"usage: matrix-math <matrix> <matrix> (got {args})", file=sys.stderr) | ||
exit(-1) | ||
|
||
print(MatrixMath().multiply(eval(args[0]), eval(args[1]))) | ||
|
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
File renamed without changes.
File renamed without changes.
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
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
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,5 @@ | ||
world matrix-math { | ||
include wasi:cli/command@0.2.0-rc-2023-11-10; | ||
|
||
export multiply: func(a: list<list<float64>>, b: list<list<float64>>) -> result<list<list<float64>>, string>; | ||
} |