-
Notifications
You must be signed in to change notification settings - Fork 50
/
main.go
58 lines (48 loc) · 1.25 KB
/
main.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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package main
import (
"fmt"
"github.com/bytecodealliance/wasmtime-go/v15"
)
// https://pkg.go.dev/github.com/bytecodealliance/wasmtime-go#example-Config-Fuel
func main() {
// Let's assume we don't have WebAssembly bytes at hand. We
// will write WebAssembly manually.
wasmBytes, err := wasmtime.Wat2Wasm(`
(module
(type (func (param i32 i32) (result i32)))
(func (type 0)
local.get 0
local.get 1
i32.add)
(export "sum" (func 0)))
`)
if err != nil {
fmt.Println("Failed to parse WebAssembly code:", err)
return
}
engine := wasmtime.NewEngine()
module, err := wasmtime.NewModule(engine, wasmBytes)
if err != nil {
fmt.Println("failed to compile module ",err)
}
linker := wasmtime.NewLinker(engine)
err = linker.DefineWasi()
if err != nil {
fmt.Println("failed to define wasi ",err)
}
wasiConfig := wasmtime.NewWasiConfig()
store := wasmtime.NewStore(engine)
store.SetWasi(wasiConfig)
instance,err := linker.Instantiate(store, module)
if err != nil {
fmt.Println("failed to instantiate ",err)
}
sumFunc := instance.GetFunc(store,"sum")
if sumFunc != nil {
result ,err := sumFunc.Call(store,1,2)
if err != nil {
fmt.Println("failed to call sum ",err)
}
fmt.Println("Results of `sum`:", result)
}
}