• Stars
    star
    729
  • Rank 59,706 (Top 2 %)
  • Language
    Go
  • License
    Apache License 2.0
  • Created about 4 years ago
  • Updated 2 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Go WebAssembly runtime powered by Wasmtime

wasmtime-go

Go embedding of Wasmtime

A Bytecode Alliance project

CI status Documentation Code Coverage

Installation

go get -u github.com/bytecodealliance/wasmtime-go/[email protected]

Be sure to check out the API documentation!

This Go library uses CGO to consume the C API of the Wasmtime project which is written in Rust. Precompiled binaries of Wasmtime are checked into this repository on tagged releases so you won't have to install Wasmtime locally, but it means that this project only works on Linux x86_64, macOS x86_64 , and Windows x86_64 currently. Building on other platforms will need to arrange to build Wasmtime and use CGO_* env vars to compile correctly.

This project has been tested with Go 1.13 or later.

If you are a bazel user, add following to your WORKSPACE file:

go_repository(
    name = "com_github_bytecodealliance_wasmtime_go",
    importpath = "github.com/bytecodealliance/wasmtime-go/v19",
    version = "v19.0.0",
)

Usage

A "Hello, world!" example of using this package looks like:

package main

import (
    "fmt"
    "github.com/bytecodealliance/wasmtime-go/v19"
)

func main() {
    // Almost all operations in wasmtime require a contextual `store`
    // argument to share, so create that first
    store := wasmtime.NewStore(wasmtime.NewEngine())

    // Compiling modules requires WebAssembly binary input, but the wasmtime
    // package also supports converting the WebAssembly text format to the
    // binary format.
    wasm, err := wasmtime.Wat2Wasm(`
      (module
        (import "" "hello" (func $hello))
        (func (export "run")
          (call $hello))
      )
    `)
    check(err)

    // Once we have our binary `wasm` we can compile that into a `*Module`
    // which represents compiled JIT code.
    module, err := wasmtime.NewModule(store.Engine, wasm)
    check(err)

    // Our `hello.wat` file imports one item, so we create that function
    // here.
    item := wasmtime.WrapFunc(store, func() {
        fmt.Println("Hello from Go!")
    })

    // Next up we instantiate a module which is where we link in all our
    // imports. We've got one import so we pass that in here.
    instance, err := wasmtime.NewInstance(store, module, []wasmtime.AsExtern{item})
    check(err)

    // After we've instantiated we can lookup our `run` function and call
    // it.
    run := instance.GetFunc(store, "run")
    if run == nil {
        panic("not a function")
    }
    _, err = run.Call(store)
    check(err)
}

func check(e error) {
    if e != nil {
        panic(e)
    }
}

Contributing

So far this extension has been written by folks who are primarily Rust programmers, so it's highly likely that there's some faux pas in terms of Go idioms. Feel free to send a PR to help make things more idiomatic if you see something!

To work on this extension locally you'll first want to clone the project:

$ git clone https://github.com/bytecodealliance/wasmtime-go

Next up you'll want to have a local Wasmtime build available.

You'll need to build at least the wasmtime-c-api crate, which, at the time of this writing, would be:

$ cargo build -p wasmtime-c-api

Once you've got that you can set up the environment of this library with:

$ ./ci/local.sh /path/to/wasmtime

This will create a build directory which has the compiled libraries and header files. Next up you can run normal commands such as:

$ go test

And after that you should be good to go!

Release Checklist

First run:

$ python3 ci/download-wasmtime.py
$ go test

Make sure everything passes at the current version.

Next run:

$ git ls-files | xargs sed -i 's/v16/v17/g'
$ python3 ci/download-wasmtime.py
$ go test

Fix all errors and such and then commit and make a PR.

Once merged checkout main and do:

$ rm .gitignore
$ python3 ci/download-wasmtime.py
$ git add .
$ git commit -m 'v19.0.0 release artifacts'
$ git tag v19.0.0

and push up the tag

More Repositories

1

wasmtime

A fast and secure runtime for WebAssembly
Rust
14,455
star
2

wasm-micro-runtime

WebAssembly Micro Runtime (WAMR)
C
4,398
star
3

lucet

Lucet, the Sandboxing WebAssembly Compiler.
Rust
4,074
star
4

cranelift

Cranelift code generator
2,482
star
5

javy

JS to WebAssembly toolchain
Rust
1,925
star
6

rustix

Safe Rust bindings to POSIX-ish APIs
Rust
1,286
star
7

wasm-tools

CLI and Rust libraries for low-level manipulation of WebAssembly modules
Rust
1,105
star
8

wit-bindgen

A language binding generator for WebAssembly interface types
Rust
880
star
9

wizer

The WebAssembly Pre-Initializer
Rust
859
star
10

cap-std

Capability-oriented version of the Rust standard library
Rust
614
star
11

cranelift-jit-demo

JIT compiler and runtime for a toy language, using Cranelift
Rust
588
star
12

jco

JavaScript tooling for working with WebAssembly Components
Rust
468
star
13

cargo-wasi

A lightweight Cargo subcommand to build Rust code for the `wasm32-wasi` target
Rust
435
star
14

wasmtime-dotnet

.NET embedding of Wasmtime https://bytecodealliance.github.io/wasmtime-dotnet/
C#
391
star
15

cargo-component

A Cargo subcommand for creating WebAssembly components based on the component model proposal.
Rust
378
star
16

wasmtime-py

Python WebAssembly runtime powered by Wasmtime
Python
352
star
17

wasi

Experimental WASI API bindings for Rust
Rust
210
star
18

regalloc2

A new register allocator
Rust
178
star
19

wasmparser

A simple event-driven library for parsing WebAssembly binary files
178
star
20

wasi.dev

HTML
170
star
21

ComponentizeJS

JS -> WebAssembly Component
Rust
163
star
22

wasmtime-demos

Historical and dated demos for Wasmtime usage and WASI content
C#
152
star
23

registry

WebAssembly Registry (Warg)
Rust
141
star
24

wat

Rust WAT and WAST parser (WebAssembly Text Format)
113
star
25

regalloc.rs

Modular register allocator algorithms
Rust
107
star
26

WASI-Virt

Virtual implementations of WASI APIs
Rust
90
star
27

componentize-py

Rust
89
star
28

spidermonkey-wasm-rs

Rust
84
star
29

preview2-prototyping

Polyfill adapter for preview1-using wasm modules to call preview2 functions.
Rust
78
star
30

wasmtime-rb

Ruby WebAssembly runtime powered by Wasmtime
Rust
72
star
31

wasmtime-cpp

C++
70
star
32

wasm-interface-types

Raw Rust toolchain support for Wasm Interface Types
Rust
70
star
33

wac

WebAssembly Composition (WAC) tooling
Rust
69
star
34

sightglass

A benchmark suite and tool to compare different implementations of the same primitives.
C
64
star
35

rfcs

RFC process for Bytecode Alliance projects
57
star
36

target-lexicon

Target "triple" support
Rust
44
star
37

component-docs

Documentation around creating and using WebAssembly Components
Rust
42
star
38

userfaultfd-rs

Rust bindings for the Linux userfaultfd functionality
Rust
42
star
39

system-interface

Extensions to the Rust standard library
Rust
40
star
40

wasi-nn

High-level bindings for wasi-nn system calls
CSS
36
star
41

wasmprinter

Rust library to print a WebAssembly binary to its textual format
32
star
42

wit-deps

WIT dependency manager
Rust
20
star
43

meetings

Python
20
star
44

spidermonkey-wasm-build

Utilities to compile SpiderMonkey to wasm32-wasi
JavaScript
20
star
45

filecheck

Library for writing tests for utilities that read text files and produce text output
Rust
20
star
46

vscode-wit

Visual Studio Code extension to recognize and highlight the WebAssembly Interface Type (WIT) IDL.
TypeScript
19
star
47

wasm-score

A benchmark for standalone WebAssembly
C
15
star
48

cranelift.vim

Vim editor configuration for working with cranelift IR (clif) files
Vim Script
14
star
49

arf-strings

Encoding and decoding for ARF strings
C
12
star
50

SIG-Registries

11
star
51

bytecodealliance.org

CSS
10
star
52

subscribe-to-label-action

A GitHub action that allows users to subscribe to a label and automatically get @'d when the label is applied
JavaScript
10
star
53

SIG-Guest-Languages

Special Interest Group (SIG) whose goal is to investigate how best to integrate Wasm and components into dynamic programming language ecosystems in a way that feels native to those ecosystems.
10
star
54

wasm-spec-interpreter

Rust bindings for the Wasm spec interpreter.
Rust
8
star
55

governance

7
star
56

wasm-parallel-gzip

Some example scripts for building a parallel compression/decompression tool for WebAssembly
Makefile
6
star
57

wamr-rust-sdk

Rust
6
star
58

wamr-python

Python
6
star
59

fs-set-times

Set filesystem timestamps
Rust
5
star
60

arena-btree

Rust
5
star
61

wasmtime-libfuzzer-corpus

libFuzzer corpus for our wasmtime fuzz targets
Shell
5
star
62

wamr.dev

The WAMR homepage
HTML
5
star
63

cm-go

4
star
64

wasmtime.dev

The Wasmtime homepage
CSS
3
star
65

libc-test

Mirror of git://nsz.repo.hu:49100/repo/libc-test (see https://wiki.musl-libc.org/libc-test.html for more information)
C
3
star
66

actions

GitHub actions to setup wasm-tools and wasmtime
TypeScript
2
star
67

wamr-app-framework

WebAssembly Micro Runtime Application Framework
C
2
star
68

wasmtime-wasi-nn

2
star
69

label-messager-action

Automatically leave a message when an issue or pull request has a certain label
JavaScript
1
star
70

wasm-ml-meetings

Informal working group for machine learning and WebAssembly, especially wasi-nn
1
star