-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain-tinygo.go
38 lines (32 loc) · 857 Bytes
/
main-tinygo.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
package main
import (
"fmt"
"github.com/bytecodealliance/wasmtime-go"
)
func main() {
// to run
// tinygo build -o main.wasm -target wasi main.go
// go run main-tinygo.go
engine := wasmtime.NewEngine()
store := wasmtime.NewStore(engine)
module, err := wasmtime.NewModuleFromFile(engine, "main.wasm")
check(err)
linker := wasmtime.NewLinker(store.Engine)
err = linker.DefineWasi()
check(err)
wasiConfig := wasmtime.NewWasiConfig()
wasiConfig.InheritEnv()
wasiConfig.PreopenDir(".", ".")
store.SetWasi(wasiConfig)
instance, err := linker.Instantiate(store, module)
check(err)
main := instance.GetExport(store, "_start").Func()
_, err = main.Call(store)
check(err)
fmt.Printf("call finished \n")
}
func check(err error) {
if err != nil {
panic(err)
}
}