-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwasm.go
55 lines (44 loc) · 1.11 KB
/
wasm.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
package main
import (
"context"
"errors"
"github.com/pinebit/go-boot/boot"
"github.com/tetratelabs/wazero"
"github.com/tetratelabs/wazero/imports/wasi_snapshot_preview1"
)
type WasmService interface {
boot.Service
Run(ctx context.Context, wasm []byte, a, b uint64) (uint64, error)
}
type wasmService struct {
runtime wazero.Runtime
}
func NewWasmService() WasmService {
return &wasmService{}
}
func (s *wasmService) Start(ctx context.Context) error {
s.runtime = wazero.NewRuntime(ctx)
_, err := wasi_snapshot_preview1.Instantiate(ctx, s.runtime)
return err
}
func (s *wasmService) Stop(ctx context.Context) error {
return s.runtime.Close(ctx)
}
func (s *wasmService) Run(ctx context.Context, wasm []byte, a, b uint64) (uint64, error) {
mod, err := s.runtime.Instantiate(ctx, wasm)
if err != nil {
return 0, err
}
addFunc := mod.ExportedFunction("add")
if addFunc == nil {
return 0, errors.New("no add function is exported")
}
r, err := addFunc.Call(ctx, a, b)
if err != nil {
return 0, err
}
if len(r) == 0 {
return 0, errors.New("no data returned from run function")
}
return r[0], nil
}