This is a simple demonstration of using WebAssembly (WASM).
See the files math.wat
and wat.html
.
To build and run this:
-
Install WebAssembly Binary Toolkit (WABT). This includes a set of command line tools including
wat2wasm
,wasm2wat
,wasm-validate
, andwasm-interp
. In macOS this can be installed using Homebrew by enteringbrew install wabt
. -
Enter
wat2wasm math.wat
to create the binary filemath.wasm
from the text filemath.wat
. -
Start a local HTTP file server. One approach is to install Deno and then enter these commands:
deno install --allow-net --allow-read https://deno.land/std@0.87.0/http/file_server.ts file_server .
-
Browse localhost:{port}/wat.html where port is the port on which the local server is listening.
See this wat2wasm issue.
See the files rust/Cargo.toml
, rust/src/lib.rs
and rust.html
.
To build and run this:
-
Install wasm-pack by entering the following command:
curl https://rustwasm.github.io/wasm-pack/installer/init.sh -sSf | sh
-
cd rust
-
Enter
wasm-pack build --target web
-
Start a local HTTP file server as described earlier.
-
Browse localhost:{port}/rust.html where port is the port on which the local server is listening.
WASM code can be run outside of a web browser. To try this:
- Enter
rustup target add wasm32-wasi
. - Enter
cargo new demo
- Enter
cd demo
- Enter
cargo build --target wasm32-wasi
- Install the Wasmtime CLI by entering
curl https://wasmtime.dev/install.sh -sSf | bash
- Open a new terminal.
- Run the program by entering
wasmtime target/wasm32-wasi/debug/demo.wasm