-
Notifications
You must be signed in to change notification settings - Fork 27
/
greet_host.pb.go
188 lines (160 loc) · 4.78 KB
/
greet_host.pb.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
//go:build !tinygo.wasm
// Code generated by protoc-gen-go-plugin. DO NOT EDIT.
// versions:
// protoc-gen-go-plugin v0.1.0
// protoc v3.21.12
// source: examples/helloworld/greeting/greet.proto
package greeting
import (
context "context"
errors "errors"
fmt "fmt"
wazero "github.com/tetratelabs/wazero"
api "github.com/tetratelabs/wazero/api"
sys "github.com/tetratelabs/wazero/sys"
os "os"
)
const GreeterPluginAPIVersion = 1
type GreeterPlugin struct {
newRuntime func(context.Context) (wazero.Runtime, error)
moduleConfig wazero.ModuleConfig
}
func NewGreeterPlugin(ctx context.Context, opts ...wazeroConfigOption) (*GreeterPlugin, error) {
o := &WazeroConfig{
newRuntime: defaultWazeroRuntime(),
moduleConfig: wazero.NewModuleConfig(),
}
for _, opt := range opts {
opt(o)
}
return &GreeterPlugin{
newRuntime: o.newRuntime,
moduleConfig: o.moduleConfig,
}, nil
}
type greeter interface {
Close(ctx context.Context) error
Greeter
}
func (p *GreeterPlugin) Load(ctx context.Context, pluginPath string) (greeter, error) {
b, err := os.ReadFile(pluginPath)
if err != nil {
return nil, err
}
// Create a new runtime so that multiple modules will not conflict
r, err := p.newRuntime(ctx)
if err != nil {
return nil, err
}
// Compile the WebAssembly module using the default configuration.
code, err := r.CompileModule(ctx, b)
if err != nil {
return nil, err
}
// InstantiateModule runs the "_start" function, WASI's "main".
module, err := r.InstantiateModule(ctx, code, p.moduleConfig)
if err != nil {
// Note: Most compilers do not exit the module after running "_start",
// unless there was an Error. This allows you to call exported functions.
if exitErr, ok := err.(*sys.ExitError); ok && exitErr.ExitCode() != 0 {
return nil, fmt.Errorf("unexpected exit_code: %d", exitErr.ExitCode())
} else if !ok {
return nil, err
}
}
// Compare API versions with the loading plugin
apiVersion := module.ExportedFunction("greeter_api_version")
if apiVersion == nil {
return nil, errors.New("greeter_api_version is not exported")
}
results, err := apiVersion.Call(ctx)
if err != nil {
return nil, err
} else if len(results) != 1 {
return nil, errors.New("invalid greeter_api_version signature")
}
if results[0] != GreeterPluginAPIVersion {
return nil, fmt.Errorf("API version mismatch, host: %d, plugin: %d", GreeterPluginAPIVersion, results[0])
}
greet := module.ExportedFunction("greeter_greet")
if greet == nil {
return nil, errors.New("greeter_greet is not exported")
}
malloc := module.ExportedFunction("malloc")
if malloc == nil {
return nil, errors.New("malloc is not exported")
}
free := module.ExportedFunction("free")
if free == nil {
return nil, errors.New("free is not exported")
}
return &greeterPlugin{
runtime: r,
module: module,
malloc: malloc,
free: free,
greet: greet,
}, nil
}
func (p *greeterPlugin) Close(ctx context.Context) (err error) {
if r := p.runtime; r != nil {
r.Close(ctx)
}
return
}
type greeterPlugin struct {
runtime wazero.Runtime
module api.Module
malloc api.Function
free api.Function
greet api.Function
}
func (p *greeterPlugin) Greet(ctx context.Context, request *GreetRequest) (*GreetReply, error) {
data, err := request.MarshalVT()
if err != nil {
return nil, err
}
dataSize := uint64(len(data))
var dataPtr uint64
// If the input data is not empty, we must allocate the in-Wasm memory to store it, and pass to the plugin.
if dataSize != 0 {
results, err := p.malloc.Call(ctx, dataSize)
if err != nil {
return nil, err
}
dataPtr = results[0]
// This pointer is managed by TinyGo, but TinyGo is unaware of external usage.
// So, we have to free it when finished
defer p.free.Call(ctx, dataPtr)
// The pointer is a linear memory offset, which is where we write the name.
if !p.module.Memory().Write(uint32(dataPtr), data) {
return nil, fmt.Errorf("Memory.Write(%d, %d) out of range of memory size %d", dataPtr, dataSize, p.module.Memory().Size())
}
}
ptrSize, err := p.greet.Call(ctx, dataPtr, dataSize)
if err != nil {
return nil, err
}
// Note: This pointer is still owned by TinyGo, so don't try to free it!
resPtr := uint32(ptrSize[0] >> 32)
resSize := uint32(ptrSize[0])
var isErrResponse bool
if (resSize & (1 << 31)) > 0 {
isErrResponse = true
resSize &^= (1 << 31)
}
// The pointer is a linear memory offset, which is where we write the name.
bytes, ok := p.module.Memory().Read(resPtr, resSize)
if !ok {
return nil, fmt.Errorf("Memory.Read(%d, %d) out of range of memory size %d",
resPtr, resSize, p.module.Memory().Size())
}
if isErrResponse {
return nil, errors.New(string(bytes))
}
response := new(GreetReply)
if err = response.UnmarshalVT(bytes); err != nil {
return nil, err
}
return response, nil
}