-
Notifications
You must be signed in to change notification settings - Fork 27
/
foo_host.pb.go
181 lines (152 loc) · 4.86 KB
/
foo_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
//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.5
// source: tests/import/proto/foo/foo.proto
package foo
import (
context "context"
errors "errors"
fmt "fmt"
bar "github.com/knqyf263/go-plugin/tests/import/proto/bar"
wazero "github.com/tetratelabs/wazero"
api "github.com/tetratelabs/wazero/api"
wasi_snapshot_preview1 "github.com/tetratelabs/wazero/imports/wasi_snapshot_preview1"
sys "github.com/tetratelabs/wazero/sys"
io "io"
fs "io/fs"
os "os"
)
const FooPluginAPIVersion = 1
type FooPluginOption struct {
Stdout io.Writer
Stderr io.Writer
FS fs.FS
}
type FooPlugin struct {
runtime wazero.Runtime
config wazero.ModuleConfig
}
func NewFooPlugin(ctx context.Context, opt FooPluginOption) (*FooPlugin, error) {
// Create a new WebAssembly Runtime.
r := wazero.NewRuntime(ctx)
// Combine the above into our baseline config, overriding defaults.
config := wazero.NewModuleConfig().
// By default, I/O streams are discarded and there's no file system.
WithStdout(opt.Stdout).WithStderr(opt.Stderr).WithFS(opt.FS)
return &FooPlugin{
runtime: r,
config: config,
}, nil
}
func (p *FooPlugin) Close(ctx context.Context) (err error) {
if r := p.runtime; r != nil {
err = r.Close(ctx)
}
return
}
func (p *FooPlugin) Load(ctx context.Context, pluginPath string) (Foo, error) {
b, err := os.ReadFile(pluginPath)
if err != nil {
return nil, err
}
// Create an empty namespace so that multiple modules will not conflict
ns := p.runtime.NewNamespace(ctx)
if _, err = wasi_snapshot_preview1.NewBuilder(p.runtime).Instantiate(ctx, ns); err != nil {
return nil, err
}
// Compile the WebAssembly module using the default configuration.
code, err := p.runtime.CompileModule(ctx, b)
if err != nil {
return nil, err
}
// InstantiateModule runs the "_start" function, WASI's "main".
module, err := ns.InstantiateModule(ctx, code, p.config)
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("foo_api_version")
if apiVersion == nil {
return nil, errors.New("foo_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 foo_api_version signature")
}
if results[0] != FooPluginAPIVersion {
return nil, fmt.Errorf("API version mismatch, host: %d, plugin: %d", FooPluginAPIVersion, results[0])
}
hello := module.ExportedFunction("foo_hello")
if hello == nil {
return nil, errors.New("foo_hello 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 &fooPlugin{module: module,
malloc: malloc,
free: free,
hello: hello,
}, nil
}
type fooPlugin struct {
module api.Module
malloc api.Function
free api.Function
hello api.Function
}
func (p *fooPlugin) Hello(ctx context.Context, request Request) (response bar.Reply, err error) {
data, err := request.MarshalVT()
if err != nil {
return response, 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 response, 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(ctx, uint32(dataPtr), data) {
return response, fmt.Errorf("Memory.Write(%d, %d) out of range of memory size %d", dataPtr, dataSize, p.module.Memory().Size(ctx))
}
}
ptrSize, err := p.hello.Call(ctx, dataPtr, dataSize)
if err != nil {
return response, 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])
// The pointer is a linear memory offset, which is where we write the name.
bytes, ok := p.module.Memory().Read(ctx, resPtr, resSize)
if !ok {
return response, fmt.Errorf("Memory.Read(%d, %d) out of range of memory size %d",
resPtr, resSize, p.module.Memory().Size(ctx))
}
if err = response.UnmarshalVT(bytes); err != nil {
return response, err
}
return response, nil
}