Skip to content

Commit

Permalink
Merge pull request #6 from codefromthecrypt/reflection-free
Browse files Browse the repository at this point in the history
Changes generator to not use reflection for host functions
  • Loading branch information
codefromthecrypt authored Nov 5, 2022
2 parents 145881b + 5ac6117 commit 708a56d
Show file tree
Hide file tree
Showing 18 changed files with 63 additions and 28 deletions.
2 changes: 1 addition & 1 deletion examples/helloworld/greeting/greet.pb.go

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

2 changes: 1 addition & 1 deletion examples/helloworld/greeting/greet_host.pb.go

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

2 changes: 1 addition & 1 deletion examples/helloworld/greeting/greet_plugin.pb.go

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

2 changes: 1 addition & 1 deletion examples/helloworld/greeting/greet_vtproto.pb.go

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

2 changes: 1 addition & 1 deletion examples/host-functions/greeting/greet.pb.go

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

29 changes: 22 additions & 7 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.

2 changes: 1 addition & 1 deletion 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.

2 changes: 1 addition & 1 deletion examples/host-functions/greeting/greet_vtproto.pb.go

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

2 changes: 1 addition & 1 deletion examples/known-types/known/known.pb.go

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

2 changes: 1 addition & 1 deletion 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.

2 changes: 1 addition & 1 deletion 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.

2 changes: 1 addition & 1 deletion examples/known-types/known/known_vtproto.pb.go

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

2 changes: 1 addition & 1 deletion examples/wasi/cat/cat.pb.go

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

2 changes: 1 addition & 1 deletion examples/wasi/cat/cat_host.pb.go

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

2 changes: 1 addition & 1 deletion examples/wasi/cat/cat_plugin.pb.go

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

2 changes: 1 addition & 1 deletion examples/wasi/cat/cat_vtproto.pb.go

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

16 changes: 13 additions & 3 deletions gen/host.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ func (gg *Generator) genHostFunctions(g *protogen.GeneratedFile, f *fileInfo) {
// Define host functions
structName := "_" + strings.ToLower(f.hostService.GoName[:1]) + f.hostService.GoName[1:]
g.P(fmt.Sprintf(`
const (
i32 = api.ValueTypeI32
i64 = api.ValueTypeI64
)
type %s struct {
%s
}
Expand All @@ -47,7 +52,10 @@ func (gg *Generator) genHostFunctions(g *protogen.GeneratedFile, f *fileInfo) {
envBuilder := r.NewHostModuleBuilder("env")`, structName))
for _, method := range f.hostService.Methods {
g.P(fmt.Sprintf(`
envBuilder.NewFunctionBuilder().WithFunc(h._%s).Export("%s")`,
envBuilder.NewFunctionBuilder().
WithGoModuleFunction(api.GoModuleFunc(h._%s), []api.ValueType{i32, i32}, []api.ValueType{i64}).
WithParameterNames("offset", "size").
Export("%s")`,
method.GoName, toSnakeCase(method.GoName)))
}
g.P(`
Expand All @@ -61,12 +69,13 @@ func (gg *Generator) genHostFunctions(g *protogen.GeneratedFile, f *fileInfo) {
}`
for _, method := range f.hostService.Methods {
g.P(method.Comments.Leading, fmt.Sprintf(`
func (h %s) _%s(ctx %s, m %s, offset, size uint32) uint64 {`,
func (h %s) _%s(ctx %s, m %s, params []uint64) []uint64 {`,
structName,
method.GoName,
g.QualifiedGoIdent(contextPackage.Ident("Context")),
g.QualifiedGoIdent(wazeroAPIPackage.Ident("Module")),
))
g.P("offset, size := uint32(params[0]), uint32(params[1])")
g.P("buf, err := ", g.QualifiedGoIdent(pluginWasmPackage.Ident("ReadMemory")), "(ctx, m, offset, size)")
g.P(errorHandling)

Expand All @@ -83,7 +92,8 @@ func (gg *Generator) genHostFunctions(g *protogen.GeneratedFile, f *fileInfo) {
g.P("ptr, err := ", g.QualifiedGoIdent(pluginWasmPackage.Ident("WriteMemory")), "(ctx, m, buf)")
g.P(errorHandling)

g.P("return (ptr << uint64(32)) | uint64(len(buf))")
g.P("ptrLen := (ptr << uint64(32)) | uint64(len(buf))")
g.P("return []uint64{ptrLen}")
g.P("}")
}
}
Expand Down
16 changes: 13 additions & 3 deletions tests/host-functions/proto/host_host.pb.go

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

0 comments on commit 708a56d

Please sign in to comment.