Skip to content

Commit

Permalink
update and fix examples (#120)
Browse files Browse the repository at this point in the history
An earlier commit which updated various dependencies (most notably, Wasmtime)
unmasked a bug in `add_wasi_and_stubs` such that we were overwriting the host
functions added by `wasmtime-wasi` with trapping stubs.  This wasn't a problem
previously because there was only one version of WASIp2: 0.2.0.  Now that we
have 0.2.1, 0.2.2, etc., with corresponding support in Wasmtime, we need to be a
bit more careful.

Signed-off-by: Joel Dice <[email protected]>
  • Loading branch information
dicej authored Oct 28, 2024
1 parent e92b223 commit 7a28de9
Show file tree
Hide file tree
Showing 9 changed files with 59 additions and 42 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "componentize-py"
version = "0.15.0"
version = "0.15.1"
edition = "2021"
exclude = ["cpython"]

Expand Down
10 changes: 5 additions & 5 deletions examples/cli/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,16 @@ run a Python-based component targetting the [wasi-cli] `command` world.

## Prerequisites

* `Wasmtime` 18.0.0 or later
* `componentize-py` 0.15.0
* `Wasmtime` 26.0.0 or later
* `componentize-py` 0.15.1

Below, we use [Rust](https://rustup.rs/)'s `cargo` to install `Wasmtime`. If
you don't have `cargo`, you can download and install from
https://github.com/bytecodealliance/wasmtime/releases/tag/v18.0.0.
https://github.com/bytecodealliance/wasmtime/releases/tag/v26.0.0.

```
cargo install --version 18.0.0 wasmtime-cli
pip install componentize-py==0.15.0
cargo install --version 26.0.0 wasmtime-cli
pip install componentize-py==0.15.1
```

## Running the demo
Expand Down
10 changes: 5 additions & 5 deletions examples/http/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,16 @@ run a Python-based component targetting the [wasi-http] `proxy` world.

## Prerequisites

* `Wasmtime` 18.0.0 or later
* `componentize-py` 0.15.0
* `Wasmtime` 26.0.0 or later
* `componentize-py` 0.15.1

Below, we use [Rust](https://rustup.rs/)'s `cargo` to install `Wasmtime`. If
you don't have `cargo`, you can download and install from
https://github.com/bytecodealliance/wasmtime/releases/tag/v18.0.0.
https://github.com/bytecodealliance/wasmtime/releases/tag/v26.0.0.

```
cargo install --version 18.0.0 wasmtime-cli
pip install componentize-py==0.15.0
cargo install --version 26.0.0 wasmtime-cli
pip install componentize-py==0.15.1
```

## Running the demo
Expand Down
10 changes: 5 additions & 5 deletions examples/matrix-math/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,20 @@ within a guest component.

## Prerequisites

* `wasmtime` 18.0.0 or later
* `componentize-py` 0.15.0
* `wasmtime` 26.0.0 or later
* `componentize-py` 0.15.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`. If
you don't have `cargo`, you can download and install from
https://github.com/bytecodealliance/wasmtime/releases/tag/v18.0.0.
https://github.com/bytecodealliance/wasmtime/releases/tag/v26.0.0.

```
cargo install --version 18.0.0 wasmtime-cli
pip install componentize-py==0.15.0
cargo install --version 26.0.0 wasmtime-cli
pip install componentize-py==0.15.1
curl -OL https://github.com/dicej/wasi-wheels/releases/download/v0.0.1/numpy-wasi.tar.gz
tar xf numpy-wasi.tar.gz
```
Expand Down
6 changes: 3 additions & 3 deletions examples/sandbox/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ sandboxed Python code snippets from within a Python app.

## Prerequisites

* `wasmtime-py` 18.0.0 or later
* `componentize-py` 0.15.0
* `wasmtime-py` 25.0.0 or later
* `componentize-py` 0.15.1

```
pip install componentize-py==0.15.0 wasmtime==18.0.2
pip install componentize-py==0.15.1 wasmtime==25.0.0
```

## Running the demo
Expand Down
10 changes: 5 additions & 5 deletions examples/tcp/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,16 @@ making an outbound TCP request using `wasi-sockets`.

## Prerequisites

* `Wasmtime` 18.0.0 or later
* `componentize-py` 0.15.0
* `Wasmtime` 26.0.0 or later
* `componentize-py` 0.15.1

Below, we use [Rust](https://rustup.rs/)'s `cargo` to install `Wasmtime`. If
you don't have `cargo`, you can download and install from
https://github.com/bytecodealliance/wasmtime/releases/tag/v18.0.0.
https://github.com/bytecodealliance/wasmtime/releases/tag/v26.0.0.

```
cargo install --version 18.0.0 wasmtime-cli
pip install componentize-py==0.15.0
cargo install --version 26.0.0 wasmtime-cli
pip install componentize-py==0.15.1
```

## Running the demo
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ features = ["pyo3/extension-module"]

[project]
name = "componentize-py"
version = "0.15.0"
version = "0.15.1"
description = "Tool to package Python applications as WebAssembly components"
readme = "README.md"
license = { file = "LICENSE" }
Expand Down
49 changes: 33 additions & 16 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -601,25 +601,32 @@ fn add_wasi_and_stubs(

for (interface_name, stubs) in stubs {
if let Some(interface_name) = interface_name {
if let Ok(mut instance) = linker.instance(&interface_name) {
for stub in stubs {
let interface_name = interface_name.clone();
match stub {
Stub::Function(name) => instance.func_new(name, {
let name = name.clone();
move |_, _, _| {
Err(anyhow!("called trapping stub: {interface_name}#{name}"))
}
}),
Stub::Resource(name) => instance
.resource(name, ResourceType::host::<()>(), {
// Note that we do _not_ stub interfaces which appear to be part of WASIp2 since those should be
// provided by the `wasmtime_wasi::add_to_linker_async` call above, and adding stubs to those same
// interfaces would just cause trouble.
if !is_wasip2_cli(&interface_name) {
if let Ok(mut instance) = linker.instance(&interface_name) {
for stub in stubs {
let interface_name = interface_name.clone();
match stub {
Stub::Function(name) => instance.func_new(name, {
let name = name.clone();
move |_, _| {
move |_, _, _| {
Err(anyhow!("called trapping stub: {interface_name}#{name}"))
}
})
.map(drop),
}?;
}),
Stub::Resource(name) => instance
.resource(name, ResourceType::host::<()>(), {
let name = name.clone();
move |_, _| {
Err(anyhow!(
"called trapping stub: {interface_name}#{name}"
))
}
})
.map(drop),
}?;
}
}
}
} else {
Expand All @@ -643,3 +650,13 @@ fn add_wasi_and_stubs(

Ok(())
}

fn is_wasip2_cli(interface_name: &str) -> bool {
(interface_name.starts_with("wasi:cli/")
|| interface_name.starts_with("wasi:clocks/")
|| interface_name.starts_with("wasi:random/")
|| interface_name.starts_with("wasi:io/")
|| interface_name.starts_with("wasi:filesystem/")
|| interface_name.starts_with("wasi:sockets/"))
&& interface_name.contains("@0.2.")
}

0 comments on commit 7a28de9

Please sign in to comment.