- Notifications
You must be signed in to change notification settings - Fork88
Go WebAssembly runtime powered by Wasmtime
License
bytecodealliance/wasmtime-go
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
go get -u github.com/bytecodealliance/wasmtime-go/v39@v39.0.1
Be sure to check out theAPI documentation!
This Go library uses CGO to consume the C API of theWasmtimeproject which is written in Rust. Precompiled binaries of Wasmtimeare checked into this repository on tagged releases so you won't have to installWasmtime 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 willneed to arrange to build Wasmtime and useCGO_* 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/v39", version = "v39.0.1",)A "Hello, world!" example of using this package looks like:
package mainimport ("fmt""github.com/bytecodealliance/wasmtime-go/v39")funcmain() {// Almost all operations in wasmtime require a contextual `store`// argument to share, so create that firststore:=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")ifrun==nil {panic("not a function") }_,err=run.Call(store)check(err)}funccheck(eerror) {ife!=nil {panic(e) }}
So far this extension has been written by folks who are primarily Rustprogrammers, so it's highly likely that there's some faux pas in terms of Goidioms. Feel free to send a PR to help make things more idiomatic if you seesomething!
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 alocal Wasmtime buildavailable.
You'll need to build at least thewasmtime-c-api crate, which, at the time ofthis 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 abuild directory which has the compiled libraries and headerfiles. Next up you can run normal commands such as:
$ gotestAnd after that you should be good to go!
First run:
$ python3 ci/download-wasmtime.py$ go testMake 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 testFix all errors and such and then commit and make a PR.
Once merged checkoutmain and do:
$ rm .gitignore$ python3 ci/download-wasmtime.py$ git add .$ git commit -m 'v39.0.0 release artifacts'$ git tag v39.0.0and push up the tag
About
Go WebAssembly runtime powered by Wasmtime
Topics
Resources
License
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Uh oh!
There was an error while loading.Please reload this page.