MCPcopy Index your code
hub / github.com/bytecodealliance/wasmtime-go

github.com/bytecodealliance/wasmtime-go @v46.0.1

Chat with this repo
repository ↗ · DeepWiki ↗ · release v46.0.1 ↗ · + Follow
494 symbols 2,514 edges 79 files 275 documented · 56% 4 cross-repo links updated 9d ago★ 90732 open issues
What it actually does AI analysis from the code graph — generated when you open this
loading…
README

wasmtime-go

<strong>Go embedding of
<a href="https://github.com/bytecodealliance/wasmtime">Wasmtime</a></strong>

A Bytecode Alliance project

<a href="https://github.com/bytecodealliance/wasmtime-go/actions?query=workflow%3ACI">
  <img src="https://github.com/bytecodealliance/wasmtime-go/workflows/CI/badge.svg" alt="CI status"/>
</a>
<a href="https://pkg.go.dev/github.com/bytecodealliance/wasmtime-go/v46">
  <img src="https://godoc.org/github.com/bytecodealliance/wasmtime-go/v46?status.svg" alt="Documentation"/>
</a>
<a href="https://bytecodealliance.github.io/wasmtime-go/coverage.html">
  <img src="https://img.shields.io/badge/coverage-main-green" alt="Code Coverage"/>
</a>

Installation

go get -u github.com/bytecodealliance/wasmtime-go/v46@v46.0.1

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/v46",
    version = "v46.0.1",
)

Usage

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

package main

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

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 'v46.0.0 release artifacts'
$ git tag v46.0.0

and push up the tag

Extension points exported contracts — how you extend this code

AsExternType (Interface)
AsExternType is an interface for all types which can be ExternType. [5 implementers]
externtype.go
AsExtern (Interface)
AsExtern is an interface for all types which can be imported or exported as an Extern [5 implementers]
extern.go
Storelike (Interface)
Storelike represents types that can be used to contextually reference a `Store`. This interface is implemented by `*Sto [2 …
store.go

Core symbols most depended-on inside this repo

Call
called by 82
func.go
NewEngine
called by 58
engine.go
Type
called by 56
func.go
NewStore
called by 54
store.go
Error
called by 50
trap.go
Kind
called by 48
val.go
NewConfig
called by 41
config.go
NewValType
called by 38
valtype.go

Shape

Method 229
Function 207
Struct 44
TypeAlias 11
Interface 3

Languages

Go99%
C1%

Modules by API surface

func_test.go26 symbols
config.go26 symbols
store.go25 symbols
trap.go19 symbols
wasi.go17 symbols
val.go16 symbols
linker.go16 symbols
func.go16 symbols
component_type_feat_component_model.go15 symbols
component_feat_component_model.go14 symbols
reftypes_test.go12 symbols
module.go12 symbols

For agents

$ claude mcp add wasmtime-go \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact

Ask about this repo answers extend the page