Skip to content

Commit

Permalink
Merge pull request #10 from knqyf263/close
Browse files Browse the repository at this point in the history
Adds Plugin.Close
  • Loading branch information
codefromthecrypt authored Nov 21, 2022
2 parents 83330ad + c038056 commit 55122e4
Show file tree
Hide file tree
Showing 19 changed files with 104 additions and 2 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,6 @@ import (
// main is required for TinyGo to compile to Wasm.
func main() {
greeting.RegisterGreeter(MyPlugin{})

}

type MyPlugin struct{}
Expand Down Expand Up @@ -196,6 +195,7 @@ func main() {
// Initialize a plugin loader
p, err := greeting.NewGreeterPlugin(ctx, greeting.GreeterPluginOption{})
if err != nil {...}
defer p.Close(ctx)

// Load a plugin
plugin, err := p.Load(ctx, "path/to/plugin.wasm")
Expand Down
8 changes: 8 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.

1 change: 1 addition & 0 deletions examples/helloworld/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ func run() error {
if err != nil {
return err
}
defer p.Close(ctx)

morningPlugin, err := p.Load(ctx, "plugin-morning/morning.wasm")
if err != nil {
Expand Down
8 changes: 8 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.

1 change: 1 addition & 0 deletions examples/host-functions/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ func run() error {
if err != nil {
return err
}
defer p.Close(ctx)

// Pass my host functions that are embedded into the plugin.
greetingPlugin, err := p.Load(ctx, "plugin/plugin.wasm", myHostFunctions{})
Expand Down
8 changes: 8 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.

1 change: 1 addition & 0 deletions examples/known-types/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ func run() error {
if err != nil {
return err
}
defer p.Close(ctx)

plugin, err := p.Load(ctx, "plugin/plugin.wasm")
if err != nil {
Expand Down
8 changes: 8 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.

1 change: 1 addition & 0 deletions examples/wasi/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ func run() error {
if err != nil {
return err
}
defer p.Close(ctx)

wasiPlugin, err := p.Load(ctx, "plugin/plugin.wasm")
if err != nil {
Expand Down
16 changes: 15 additions & 1 deletion gen/host.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,20 @@ func genHost(g *protogen.GeneratedFile, f *fileInfo, service *serviceInfo) {
runtime: r,
config: config,
}, nil
}`)
}
`)

// Close plugin
g.P(fmt.Sprintf(`func (p *%s) Close(ctx %s) (err error) {
if r := p.runtime; r != nil {
err = r.Close(ctx)
}
return
}
`,
pluginName,
g.QualifiedGoIdent(contextPackage.Ident("Context")),
))

// Plugin loading
structName := strings.ToLower(service.GoName[:1]) + service.GoName[1:] + "Plugin"
Expand All @@ -160,6 +173,7 @@ func genHost(g *protogen.GeneratedFile, f *fileInfo, service *serviceInfo) {
return nil, err
}`
}

g.P(fmt.Sprintf("func (p *%s) Load(ctx %s, pluginPath string %s) (%s, error) {",
pluginName,
g.QualifiedGoIdent(contextPackage.Ident("Context")),
Expand Down
1 change: 1 addition & 0 deletions tests/fields/fields_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ func TestFields(t *testing.T) {
ctx := context.Background()
p, err := proto.NewFieldTestPlugin(ctx, proto.FieldTestPluginOption{})
require.NoError(t, err)
defer p.Close(ctx)

plugin, err := p.Load(ctx, "plugin/plugin.wasm")
require.NoError(t, err)
Expand Down
8 changes: 8 additions & 0 deletions tests/fields/proto/fields_host.pb.go

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

1 change: 1 addition & 0 deletions tests/host-functions/host_functions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ func TestHostFunctions(t *testing.T) {
ctx := context.Background()
p, err := proto.NewGreeterPlugin(ctx, proto.GreeterPluginOption{Stdout: os.Stdout})
require.NoError(t, err)
defer p.Close(ctx)

// Pass my host functions that are embedded into the plugin.
plugin, err := p.Load(ctx, "plugin/plugin.wasm", myHostFunctions{})
Expand Down
8 changes: 8 additions & 0 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.

1 change: 1 addition & 0 deletions tests/import/import_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ func TestImport(t *testing.T) {
ctx := context.Background()
p, err := foo.NewFooPlugin(ctx, foo.FooPluginOption{})
require.NoError(t, err)
defer p.Close(ctx)

plugin, err := p.Load(ctx, "plugin/plugin.wasm")
require.NoError(t, err)
Expand Down
8 changes: 8 additions & 0 deletions tests/import/proto/bar/bar_host.pb.go

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

8 changes: 8 additions & 0 deletions tests/import/proto/foo/foo_host.pb.go

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

16 changes: 16 additions & 0 deletions tests/well-known/proto/known_host.pb.go

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

1 change: 1 addition & 0 deletions tests/well-known/well_known_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ func TestWellKnownTypes(t *testing.T) {
ctx := context.Background()
p, err := proto.NewKnownTypesTestPlugin(ctx, proto.KnownTypesTestPluginOption{})
require.NoError(t, err)
defer p.Close(ctx)

plugin, err := p.Load(ctx, "plugin/plugin.wasm")
require.NoError(t, err)
Expand Down

0 comments on commit 55122e4

Please sign in to comment.