Skip to content

mvolkmann/wasm-demo

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

14 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

wasm-demo

This is a simple demonstration of using WebAssembly (WASM).

Manually coding WebAssembly

See the files math.wat and wat.html.

To build and run this:

  1. Install WebAssembly Binary Toolkit (WABT). This includes a set of command line tools including wat2wasm, wasm2wat, wasm-validate, and wasm-interp. In macOS this can be installed using Homebrew by entering brew install wabt.

  2. Enter wat2wasm math.wat to create the binary file math.wasm from the text file math.wat.

  3. 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 .
  4. Browse localhost:{port}/wat.html where port is the port on which the local server is listening.

See this wat2wasm issue.

Converting Rust to WASM

See the files rust/Cargo.toml, rust/src/lib.rs and rust.html.

To build and run this:

  1. Install wasm-pack by entering the following command:

    curl https://rustwasm.github.io/wasm-pack/installer/init.sh -sSf | sh
  2. cd rust

  3. Enter wasm-pack build --target web

  4. Start a local HTTP file server as described earlier.

  5. Browse localhost:{port}/rust.html where port is the port on which the local server is listening.

Running outside a web browser

WASM code can be run outside of a web browser. To try this:

  1. Enter rustup target add wasm32-wasi.
  2. Enter cargo new demo
  3. Enter cd demo
  4. Enter cargo build --target wasm32-wasi
  5. Install the Wasmtime CLI by entering curl https://wasmtime.dev/install.sh -sSf | bash
  6. Open a new terminal.
  7. Run the program by entering wasmtime target/wasm32-wasi/debug/demo.wasm

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published