Created
June 15, 2022 16:52
-
-
Save kyleconroy/a50054984159918d78aa7c0448e56db5 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module github.com/kyleconroy/wasmtimetest | |
go 1.18 | |
require github.com/bytecodealliance/wasmtime-go v0.37.0 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"github.com/bytecodealliance/wasmtime-go" | |
) | |
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) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment