Skip to content

Commit

Permalink
Add API version check
Browse files Browse the repository at this point in the history
  • Loading branch information
knqyf263 committed Aug 27, 2022
1 parent 98c76f9 commit 0415fb5
Show file tree
Hide file tree
Showing 19 changed files with 251 additions and 2 deletions.
17 changes: 17 additions & 0 deletions examples/helloworld/greeting/greet_host.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions examples/helloworld/greeting/greet_plugin.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions examples/helloworld/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ func run() error {
return err
}

morningPlugin, err := p.Load(ctx, "examples/basic/plugin-morning/morning.wasm")
morningPlugin, err := p.Load(ctx, "examples/helloworld/plugin-morning/morning.wasm")
if err != nil {
return err
}

eveningPlugin, err := p.Load(ctx, "examples/basic/plugin-evening/evening.wasm")
eveningPlugin, err := p.Load(ctx, "examples/helloworld/plugin-evening/evening.wasm")
if err != nil {
return err
}
Expand Down
17 changes: 17 additions & 0 deletions examples/host-functions/greeting/greet_host.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions examples/host-functions/greeting/greet_plugin.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions examples/known-types/known/known_host.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions examples/known-types/known/known_plugin.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions examples/wasi/cat/cat_host.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions examples/wasi/cat/cat_plugin.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 23 additions & 0 deletions gen/host.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,8 @@ func (gg *Generator) genHostFunctions(g *protogen.GeneratedFile, f *fileInfo) {

func genHost(g *protogen.GeneratedFile, f *fileInfo, service *serviceInfo) {
pluginName := service.GoName + "Plugin"

g.P("const ", pluginName, "APIVersion = ", service.Version)
g.P(fmt.Sprintf(`
type %sOption struct {
Stdout %s
Expand Down Expand Up @@ -190,12 +192,33 @@ func genHost(g *protogen.GeneratedFile, f *fileInfo, service *serviceInfo) {
return nil, err
}
}
// Compare API versions with the loading plugin
apiVersion := module.ExportedFunction("%s_api_version")
if apiVersion == nil {
return nil, %s("%s_api_version is not exported")
}
results, err := apiVersion.Call(ctx)
if err != nil {
return nil, err
} else if len(results) != 1 {
return nil, %s("invalid %s_api_version signature")
}
if results[0] != %sAPIVersion {
return nil, fmt.Errorf("API version mismatch, host: %%d, plugin: %%d", %sAPIVersion, results[0])
}
`,
g.QualifiedGoIdent(osPackage.Ident("ReadFile")),
initHostFunctions, exportHostFunctions,
g.QualifiedGoIdent(wazeroWasiPackage.Ident("NewBuilder")),
g.QualifiedGoIdent(wazeroSysPackage.Ident("ExitError")),
g.QualifiedGoIdent(fmtPackage.Ident("Errorf")),
toSnakeCase(service.GoName),
g.QualifiedGoIdent(errorsPackage.Ident("New")),
toSnakeCase(service.GoName),
g.QualifiedGoIdent(errorsPackage.Ident("New")),
toSnakeCase(service.GoName),
pluginName, pluginName,
))

errorsNew := g.QualifiedGoIdent(errorsPackage.Ident("New"))
Expand Down
10 changes: 10 additions & 0 deletions gen/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,16 @@ func (gg *Generator) generatePluginFile(f *fileInfo) {
func genPlugin(g *protogen.GeneratedFile, f *fileInfo, service *serviceInfo) {
serviceVar := strings.ToLower(service.GoName[:1]) + service.GoName[1:]

// API version
g.P("const ", service.GoName, "PluginAPIVersion = ", service.Version)
g.P(fmt.Sprintf(`
//export %s_api_version
func _%s_api_version() uint64 {
return %sPluginAPIVersion
}`,
toSnakeCase(service.GoName), toSnakeCase(service.GoName), service.GoName,
))

// Variable definition
g.P("var ", serviceVar, " ", service.GoName)

Expand Down
17 changes: 17 additions & 0 deletions test/fields/proto/fields_host.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions test/fields/proto/fields_plugin.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions test/import/proto/bar/bar_host.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions test/import/proto/bar/bar_plugin.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions test/import/proto/foo/foo_host.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions test/import/proto/foo/foo_plugin.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 0415fb5

Please sign in to comment.