cpetig / resource-demo

A demo showing WASM component model resources in various environments

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

A demo showing WASM component model resources in various environments

Prerequisites

Rust target wasm32-wasi:

rustup target add wasm32-wasi

CMake

cpetig/wit-bindgen:

git clone https://github.com/cpetig/wit-bindgen.git
cd wit-bindgen
cargo install --path .

Working

Guest Host
C βœ… wit-bindgen see below1
C++ 🚧 cpetig/wit-bindgen 🚧 cpetig/wit-bindgen + WAMR
Rust βœ… wit-bindgen βœ… wasmtime 2
JavaScript 🚧 3 βœ… jco 2
Go ☁️ 4 ?
Python 🚧 5 🚧 5

Compiling and Running

cd guest-rust
cargo build --target wasm32-wasi --release

cd ../host-wamr
./generate.sh
mkdir build
cd build
cmake ..
make
./executor

Expected Output:

drop with value 34

C guest

Prerequisite: wasi-SDK

cd guest-c
./generate.sh
make
cd ..
ln -sf guest-c/guest-c.wasm guest.wasm 

Wasmtime host

You need a preview1 adapter matching the wasmtime version, see https://github.com/bytecodealliance/wasmtime/releases for the command adapter downloads.

But most likely you will need to check out the exact same wasmtime used for the host and build the adapter:

cd wasmtime/crates/wasi-preview1-component-adapter
cargo build -p wasi-preview1-component-adapter --target wasm32-unknown-unknown --release --features command --no-default-features
cp ../../target/wasm32-unknown-unknown/release/wasi_snapshot_preview1.wasm …/resource-demo/host-wasmtime

Jco host

Sorry, jco is currently broken until I figure out to compile typescript (shouldn't be that difficult to do).

Please use the adapter built from the wasmtime source (see above)

cargo install simple-http-server
./generate.sh
simple-http-server --cors --coop --coep --nocache -i html

Other environments

Host languages

  • Rust:

    Wasmer bindgen only supports the old witx, not the newer wit format. You could write a host binding generator within wit-bindgen though.

WASI preview2 adapter

The wasmtime host requires wasi_snapshot_preview1.wasm from (the dev branch) (WASI has recently migrated to resources)

JCO's preview2 browser shim still needs the older adapter

Guest languages

JavaScript: https://github.com/bytecodealliance/javy is potentially smaller than spidermonkey embedded by jco, you will need another tool to generate guest bindings for imported functions, IIRC. A path using C bindings with yet to be written JavaScript wrapping should be viable.

Footnotes

  1. Currently I generate C++ host bindings for WAMR, C would be feasible. ↩

  2. JCO and wasmtime currently need a different preview1 to preview2 adapter version ↩ ↩2

  3. Jco likely needs the unfinished https://github.com/bytecodealliance/componentize-js/tree/resources for resource support in guests ↩

  4. Go: WIT bindgen support is being worked on ↩

  5. Python: https://pypi.org/project/componentize-py/ recently gained resource support ↩ ↩2

About

A demo showing WASM component model resources in various environments


Languages

Language:JavaScript 36.6%Language:C++ 27.3%Language:Rust 16.5%Language:C 8.4%Language:Shell 5.0%Language:CMake 3.4%Language:Makefile 1.5%Language:HTML 1.2%