Home

Awesome

This repository is a fork of Wasmtime extended with support for the WasmFX instruction set. WasmFX adds stack switching capabilities to core Wasm, thereby enabling interleaving of computation. The design of the instruction set extension is based on effect handlers, which provide a structured facility for handling non-local control flow.

Additional key resources are

Building

The build steps are equivalent to the standard steps for building Wasmtime from source, but using this repository instead. There is no need to build or install the original version of Wasmtime to use this fork.

Concretely, the steps are as follows:

  1. Make sure that you have a Rust toolchain installed, for example using rustup.
  2. Check out this repository:
git clone https://github.com/wasmfx/wasmfxtime.git
cd wasmfxtime
git submodule update --init
  1. Build in debug mode:
cargo build

As a result, a debug build of the wasmtime executable will be created at target/debug/wasmtime.

Analogously, to build in release mode run cargo build --release, which places the wasmtime artifact in target/release/.

Running programs

The wasmtime executable can compile and run WebAssembly modules in the usual way. However, if a module contains WasmFX instructions, then it is necessary to enable additional features, e.g.

wasmtime -W=exceptions,function-references,stack-switching my_module.wat

The first two features are prerequisite features, i.e. the exceptions feature is required to support WebAssembly tags and the function-references feature is required to support typed references. The stack-switching feature enables support for the stack switching (viz. WasmFX) instruction set.

To run an arbitrary function exported as foo by the module type

wasmtime -W=exceptions,function-references,stack-switching --invoke=foo my_module.wat

Example program

The following module implements a generator and consumer using stack switching.

(module
  (type $ft (func))
  (type $ct (cont $ft))

  ;; Tag used by generator, the i32 payload corresponds to the generated values
  (tag $yield (param i32))

  ;; Printing function for unsigned integers.
  ;; This function is unrelated to stack switching.
  (func $println_u32 (param $value i32)
    ;; See examples/generator.wat for actual implementation
  )


  ;; Simple generator yielding values from 100 down to 1
  (func $generator
    (local $i i32)
    (local.set $i (i32.const 100))
    (loop $l
      ;; Suspend generator, yield current value of $i to consumer
      (suspend $yield (local.get $i))
      ;; Decrement $i and exit loop once $i reaches 0
      (local.tee $i (i32.sub (local.get $i) (i32.const 1)))
      (br_if $l)
    )
  )
  (elem declare func $generator)

  (func $consumer
    (local $c (ref $ct))
    ;; Create continuation executing function $generator
    (local.set $c (cont.new $ct (ref.func $generator)))

    (loop $loop
      (block $on_yield (result i32 (ref $ct))
        ;; Resume continuation $c
        (resume $ct (on $yield $on_yield) (local.get $c))
        ;; Generator returned: no more data
        (return)
      )
      ;; Generator suspend, stack contains [i32 (ref $ct)]
      (local.set $c)
      ;; Stack now contains the i32 value yielded by generator
      (call $println_u32)

      (br $loop)
    )
  )

  (func $start (export "_start")
    (call $consumer)
  )
)

See examples/generator.wat for the full version of the file, including the definition of $println_u32.

Running the full version with

wasmtime -W=exceptions,function-references,stack-switching generator.wat

then prints the numbers 100 down to 1 in the terminal.

Current limitations

The implementation of the WasmFX proposal is currently limited in a few ways:

Original Wasmtime documentation below

<div align="center"> <h1><code>wasmtime</code></h1> <p> <strong>A standalone runtime for <a href="https://webassembly.org/">WebAssembly</a></strong> </p>

<strong>A <a href="https://bytecodealliance.org/">Bytecode Alliance</a> project</strong>

<p> <a href="https://github.com/bytecodealliance/wasmtime/actions?query=workflow%3ACI"><img src="https://github.com/bytecodealliance/wasmtime/workflows/CI/badge.svg" alt="build status" /></a> <a href="https://bytecodealliance.zulipchat.com/#narrow/stream/217126-wasmtime"><img src="https://img.shields.io/badge/zulip-join_chat-brightgreen.svg" alt="zulip chat" /></a> <img src="https://img.shields.io/badge/rustc-stable+-green.svg" alt="supported rustc stable" /> <a href="https://docs.rs/wasmtime"><img src="https://docs.rs/wasmtime/badge.svg" alt="Documentation Status" /></a> </p> <h3> <a href="https://bytecodealliance.github.io/wasmtime/">Guide</a> <span> | </span> <a href="https://bytecodealliance.github.io/wasmtime/contributing.html">Contributing</a> <span> | </span> <a href="https://wasmtime.dev/">Website</a> <span> | </span> <a href="https://bytecodealliance.zulipchat.com/#narrow/stream/217126-wasmtime">Chat</a> </h3> </div>

Installation

The Wasmtime CLI can be installed on Linux and macOS (locally) with a small install script:

curl https://wasmtime.dev/install.sh -sSf | bash

This script installs into $WASMTIME_HOME (defaults to $HOME/.wasmtime), and executable is placed in $WASMTIME_HOME/bin.

Windows or otherwise interested users can download installers and binaries directly from the GitHub Releases page.

Example

If you've got the Rust compiler installed then you can take some Rust source code:

fn main() {
    println!("Hello, world!");
}

and compile/run it with:

$ rustup target add wasm32-wasip1
$ rustc hello.rs --target wasm32-wasip1
$ wasmtime hello.wasm
Hello, world!

(Note: make sure you installed Rust using the rustup method in the official instructions above, and do not have a copy of the Rust toolchain installed on your system in some other way as well (e.g. the system package manager). Otherwise, the rustup target add... command may not install the target for the correct copy of Rust.)

Features

Language Support

You can use Wasmtime from a variety of different languages through embeddings of the implementation.

Languages supported by the Bytecode Alliance:

Languages supported by the community:

Documentation

📚 Read the Wasmtime guide here! 📚

The wasmtime guide is the best starting point to learn about what Wasmtime can do for you or help answer your questions about Wasmtime. If you're curious in contributing to Wasmtime, it can also help you do that!


It's Wasmtime.