-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.go
169 lines (136 loc) · 3.91 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
package main
import (
"context"
"errors"
"flag"
"fmt"
"io/ioutil"
"log"
"os"
"path/filepath"
"github.com/prep/wasmexec"
"github.com/prep/wasmexec/wazeroexec"
"github.com/tetratelabs/wazero"
"github.com/tetratelabs/wazero/api"
)
var progname = filepath.Base(os.Args[0])
type Instance struct {
wasmexec.Memory
spFn api.Function
resumeFn api.Function
}
func (instance *Instance) Error(format string, params ...interface{}) {
log.Printf("ERROR: "+format+"\n", params...)
}
func (instance *Instance) GetSP() (uint32, error) {
results, err := instance.spFn.Call(context.Background())
switch {
case err != nil:
return 0, err
case len(results) == 0:
return 0, errors.New("getsp: no sp value returned")
}
return uint32(results[0]), nil
}
func (instance *Instance) Resume() error {
_, err := instance.resumeFn.Call(context.Background())
return err
}
func (instance *Instance) Write(fd int, b []byte) (n int, err error) {
switch fd {
case 1:
n, err = os.Stdout.Write(b)
case 2:
n, err = os.Stderr.Write(b)
default:
err = fmt.Errorf("%d: invalid file descriptor", fd)
}
return n, err
}
// HostCall is an optional method that allows the guest to use a fake waPC
// interface package to send messages to this host.
func (instance *Instance) HostCall(binding, namespace, operation string, payload []byte) ([]byte, error) {
if namespace == "sample" && operation == "hello" {
return []byte(fmt.Sprintf("Hello %s!", string(payload))), nil
}
return nil, fmt.Errorf("%s/%s: unsupported namespace + operation combination", namespace, operation)
}
func run(filename string) error {
// Read the Wasm file into memory.
data, err := ioutil.ReadFile(filename)
if err != nil {
return err
}
ctx := context.Background()
// Create the runtime.
runtime := wazero.NewRuntime()
defer runtime.Close(ctx)
// Create an instance. This is needed here because the imports need an
// instance to refer to, even though at this point no instance exists yet.
instance := &Instance{}
// Import the wasmexec functions.
gomod, err := wazeroexec.Import(ctx, runtime, instance)
if err != nil {
return err
}
// Create an instance of the module.
module, err := runtime.InstantiateModuleFromBinary(ctx, data)
if err != nil {
return err
}
defer module.Close(ctx)
// Fetch the memory export and set it on the instance, making the memory
// accessible by the imports.
mem := module.ExportedMemory("mem")
if mem == nil {
return errors.New("unable to find memory export")
}
instance.Memory = wazeroexec.NewMemory(mem)
// Fetch the getsp function and reference it on the instance.
instance.spFn = module.ExportedFunction("getsp")
if instance.spFn == nil {
return errors.New("getsp: missing export")
}
// Fetch the resume function and reference it on the instance.
instance.resumeFn = module.ExportedFunction("resume")
if instance.resumeFn == nil {
return errors.New("resume: missing export")
}
args := []string{filename, "-runtime=wazero", "arg1", "arg2"}
envs := []string{"HOME=/", "PWD=/home/test"}
// Set the args and the environment variables.
argc, argv, err := wasmexec.SetArgs(instance.Memory, args, envs)
if err != nil {
return err
}
// Fetch the "run" function and call it. This starts the program.
runFn := module.ExportedFunction("run")
if runFn == nil {
return errors.New("run: missing export")
}
_, err = runFn.Call(ctx, uint64(argc), uint64(argv))
if err != nil {
return err
}
// Silently fail, because not all examples implement waPC.
result, err := gomod.Invoke("hello", []byte("Host"))
if err == nil {
fmt.Printf("Message from guest: %s\n", string(result))
}
return nil
}
func main() {
flag.Usage = func() {
fmt.Printf("Usage: %s [flags] program.wasm\n", progname)
}
flag.Parse()
if flag.NArg() != 1 {
flag.Usage()
os.Exit(1)
}
filename := flag.Arg(0)
if err := run(filename); err != nil {
_, _ = fmt.Fprintf(os.Stderr, "%s: %s: %s\n", progname, filename, err)
os.Exit(1)
}
}