Convert Figma logo to code with AI

bytecodealliance logowasmtime

A fast and secure runtime for WebAssembly

15,089
1,260
15,089
708

Top Related Projects

18,439

🚀 The leading Wasm Runtime supporting WASIX, WASI and Emscripten

6,727

The WebAssembly Binary Toolkit

Emscripten: An LLVM-to-WebAssembly Compiler

2,639

WebAssembly Virtual Machine

Quick Overview

Wasmtime is a standalone runtime for WebAssembly, developed by the Bytecode Alliance. It's designed to be fast, secure, and embeddable, allowing WebAssembly to be used outside of web browsers in various environments, including servers, IoT devices, and desktop applications.

Pros

  • High performance and low overhead due to its JIT compiler
  • Strong security features, including sandboxing and capability-based security
  • Cross-platform support, running on multiple operating systems and architectures
  • Extensive language support, with bindings for Rust, C, Python, .NET, and more

Cons

  • Learning curve for developers new to WebAssembly concepts
  • Limited ecosystem compared to more established runtimes
  • Potential compatibility issues with some existing WebAssembly modules
  • Ongoing development may lead to breaking changes in future versions

Code Examples

  1. Running a WebAssembly module in Rust:
use wasmtime::*;

fn main() -> anyhow::Result<()> {
    let engine = Engine::default();
    let module = Module::from_file(&engine, "example.wasm")?;
    let store = Store::new(&engine);
    let instance = Instance::new(&store, &module, &[])?;
    let hello = instance.get_func("hello").expect("hello function not found");
    hello.call(&[])?;
    Ok(())
}
  1. Passing data between Rust and WebAssembly:
use wasmtime::*;

fn main() -> anyhow::Result<()> {
    let engine = Engine::default();
    let module = Module::from_file(&engine, "example.wasm")?;
    let mut store = Store::new(&engine);
    let instance = Instance::new(&mut store, &module, &[])?;
    let add = instance.get_typed_func::<(i32, i32), i32>(&mut store, "add")?;
    let result = add.call(&mut store, (5, 37))?;
    println!("5 + 37 = {}", result);
    Ok(())
}
  1. Using WASI (WebAssembly System Interface) with Wasmtime:
use wasmtime::*;
use wasmtime_wasi::*;

fn main() -> anyhow::Result<()> {
    let engine = Engine::default();
    let mut store = Store::new(&engine, ());
    let mut linker = Linker::new(&engine);
    wasmtime_wasi::add_to_linker(&mut linker, |s| s)?;

    let wasi = WasiCtxBuilder::new()
        .inherit_stdio()
        .inherit_args()?
        .build();
    store.data_mut().insert(wasi);

    let module = Module::from_file(&engine, "wasi_example.wasm")?;
    linker.module(&mut store, "", &module)?;
    linker.get_default(&mut store, "")?.call(&mut store, &[])?;
    Ok(())
}

Getting Started

To get started with Wasmtime in Rust:

  1. Add Wasmtime to your Cargo.toml:

    [dependencies]
    wasmtime = "8.0"
    
  2. Use Wasmtime in your Rust code:

    use wasmtime::*;
    
    fn main() -> anyhow::Result<()> {
        let engine = Engine::default();
        let module = Module::from_file(&engine, "example.wasm")?;
        let store = Store::new(&engine);
        let instance = Instance::new(&store, &module, &[])?;
        // Use the instance...
        Ok(())
    }
    
  3. Compile and run your Rust program with cargo run.

Competitor Comparisons

18,439

🚀 The leading Wasm Runtime supporting WASIX, WASI and Emscripten

Pros of Wasmer

  • Supports multiple backends (LLVM, Cranelift, Singlepass) for different performance/compilation speed trade-offs
  • Provides a more extensive ecosystem with additional tools like Wasmer-JS and WAPM (WebAssembly Package Manager)
  • Offers easier integration with various programming languages through language extensions

Cons of Wasmer

  • Generally slower runtime performance compared to Wasmtime
  • Less focus on standards compliance and cutting-edge WebAssembly features
  • Smaller community and corporate backing compared to Wasmtime's Bytecode Alliance support

Code Comparison

Wasmer:

let module = Module::from_file(store, "example.wasm")?;
let import_object = imports! {};
let instance = Instance::new(&module, &import_object)?;
let sum = instance.exports.get_function("sum")?;
let result = sum.call(&[Value::I32(5), Value::I32(37)])?;

Wasmtime:

let engine = Engine::default();
let module = Module::from_file(&engine, "example.wasm")?;
let store = Store::new(&engine, ());
let instance = Instance::new(&store, &module, &[])?;
let sum = instance.get_func("sum").unwrap();
let result = sum.call(&[5.into(), 37.into()])?;

Both examples demonstrate loading a WebAssembly module, creating an instance, and calling a function. Wasmtime's API is slightly more explicit with separate Engine and Store concepts.

6,727

The WebAssembly Binary Toolkit

Pros of wabt

  • Focused on WebAssembly binary toolkit utilities
  • Provides a suite of command-line tools for working with WebAssembly
  • Supports text-to-binary and binary-to-text conversions

Cons of wabt

  • Limited runtime capabilities compared to Wasmtime
  • Primarily a development and tooling project, not a full-featured runtime
  • Less emphasis on performance optimization for WebAssembly execution

Code Comparison

wabt (wat2wasm example):

wat2wasm input.wat -o output.wasm

Wasmtime (running a WebAssembly module):

use wasmtime::*;

let engine = Engine::default();
let module = Module::from_file(&engine, "example.wasm")?;
let instance = Instance::new(&mut store, &module, &[])?;

Summary

wabt is a toolkit for WebAssembly binary manipulation and conversion, offering various command-line utilities. It's excellent for development and debugging but lacks runtime features. Wasmtime, on the other hand, is a full-featured WebAssembly runtime with a focus on performance and embedability. While wabt is more suited for tooling and development workflows, Wasmtime is better for executing and integrating WebAssembly modules in production environments.

Emscripten: An LLVM-to-WebAssembly Compiler

Pros of Emscripten

  • Mature ecosystem with extensive documentation and community support
  • Broader compatibility with existing C/C++ codebases
  • Includes a complete toolchain for compiling to WebAssembly

Cons of Emscripten

  • Larger runtime overhead compared to Wasmtime
  • Less focus on performance optimization for server-side WebAssembly
  • More complex setup and configuration process

Code Comparison

Emscripten (compiling C++ to WebAssembly):

#include <emscripten.h>
#include <iostream>

EMSCRIPTEN_KEEPALIVE
extern "C" int add(int a, int b) {
    return a + b;
}

Wasmtime (running WebAssembly module in Rust):

use wasmtime::*;

let engine = Engine::default();
let module = Module::from_file(&engine, "add.wasm")?;
let instance = Instance::new(&mut store, &module, &[])?;
let add = instance.get_func(&mut store, "add")?;
let result = add.call(&mut store, &[Val::I32(5), Val::I32(37)])?;

This comparison highlights the different approaches of Emscripten and Wasmtime. Emscripten focuses on compiling existing code to WebAssembly, while Wasmtime provides a runtime for executing WebAssembly modules. The choice between them depends on specific project requirements and use cases.

2,639

WebAssembly Virtual Machine

Pros of WAVM

  • Supports a wider range of WebAssembly features, including SIMD and threading
  • Provides more low-level control over the execution environment
  • Offers better performance for certain use cases, especially compute-intensive tasks

Cons of WAVM

  • Less actively maintained compared to Wasmtime
  • Smaller community and ecosystem support
  • More complex to set up and use, with fewer high-level abstractions

Code Comparison

WAVM example:

#include "WAVM/Runtime/Runtime.h"
#include "WAVM/Runtime/Linker.h"

WAVM::Runtime::ModuleRef module = WAVM::Runtime::loadModule(wasmBytes);
WAVM::Runtime::Instance* instance = WAVM::Runtime::instantiateModule(compartment, module, {}, "module");

Wasmtime example:

use wasmtime::*;

let engine = Engine::default();
let module = Module::from_binary(&engine, wasm_bytes)?;
let instance = Instance::new(&mut store, &module, &[])?;

Both WAVM and Wasmtime are WebAssembly runtimes, but they have different focuses and trade-offs. WAVM offers more advanced features and potentially better performance for specific scenarios, while Wasmtime provides a more user-friendly experience with better community support and active development. The choice between them depends on the specific requirements of your project and the level of control you need over the WebAssembly execution environment.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

wasmtime

A standalone runtime for WebAssembly

A Bytecode Alliance project

build status zulip chat supported rustc stable Documentation Status

Guide | Contributing | Website | Chat

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

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

  • Fast. Wasmtime is built on the optimizing Cranelift code generator to quickly generate high-quality machine code either at runtime or ahead-of-time. Wasmtime is optimized for efficient instantiation, low-overhead calls between the embedder and wasm, and scalability of concurrent instances.

  • Secure. Wasmtime's development is strongly focused on correctness and security. Building on top of Rust's runtime safety guarantees, each Wasmtime feature goes through careful review and consideration via an RFC process. Once features are designed and implemented, they undergo 24/7 fuzzing donated by Google's OSS Fuzz. As features stabilize they become part of a release, and when things go wrong we have a well-defined security policy in place to quickly mitigate and patch any issues. We follow best practices for defense-in-depth and integrate protections and mitigations for issues like Spectre. Finally, we're working to push the state-of-the-art by collaborating with academic researchers to formally verify critical parts of Wasmtime and Cranelift.

  • Configurable. Wasmtime uses sensible defaults, but can also be configured to provide more fine-grained control over things like CPU and memory consumption. Whether you want to run Wasmtime in a tiny environment or on massive servers with many concurrent instances, we've got you covered.

  • WASI. Wasmtime supports a rich set of APIs for interacting with the host environment through the WASI standard.

  • Standards Compliant. Wasmtime passes the official WebAssembly test suite, implements the official C API of wasm, and implements future proposals to WebAssembly as well. Wasmtime developers are intimately engaged with the WebAssembly standards process all along the way too.

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.