/
options.go
62 lines (51 loc) · 1.54 KB
/
options.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
package gen
import (
"fmt"
)
func (gg *Generator) generateOptionsFile(f *fileInfo) {
filename := f.GeneratedFilenamePrefix + "_options.pb.go"
g := gg.plugin.NewGeneratedFile(filename, f.GoImportPath)
if len(f.pluginServices) == 0 {
g.Skip()
}
// Build constraints
g.P("//go:build !tinygo.wasm")
gg.generateHeader(g, f)
g.P(fmt.Sprintf(`type wazeroConfigOption func(plugin *WazeroConfig)
type WazeroNewRuntime func(%s) (%s, error)
type WazeroConfig struct {
newRuntime func(%s) (%s, error)
moduleConfig %s
}
func WazeroRuntime(newRuntime WazeroNewRuntime) wazeroConfigOption {
return func(h *WazeroConfig) {
h.newRuntime = newRuntime
}
}
func DefaultWazeroRuntime() WazeroNewRuntime {
return func(ctx %s) (%s, error) {
r := %s(ctx)
if _, err := %s(ctx, r); err != nil {
return nil, err
}
return r, nil
}
}
func WazeroModuleConfig(moduleConfig %s) wazeroConfigOption {
return func(h *WazeroConfig) {
h.moduleConfig = moduleConfig
}
}
`,
g.QualifiedGoIdent(contextPackage.Ident("Context")),
g.QualifiedGoIdent(wazeroPackage.Ident("Runtime")),
g.QualifiedGoIdent(contextPackage.Ident("Context")),
g.QualifiedGoIdent(wazeroPackage.Ident("Runtime")),
g.QualifiedGoIdent(wazeroPackage.Ident("ModuleConfig")),
g.QualifiedGoIdent(contextPackage.Ident("Context")),
g.QualifiedGoIdent(wazeroPackage.Ident("Runtime")),
g.QualifiedGoIdent(wazeroPackage.Ident("NewRuntime")),
g.QualifiedGoIdent(wazeroWasiPackage.Ident("Instantiate")),
g.QualifiedGoIdent(wazeroPackage.Ident("ModuleConfig")),
))
}