diff --git a/README.md b/README.md index 56893e9..37f7f80 100644 --- a/README.md +++ b/README.md @@ -158,7 +158,6 @@ import ( // main is required for TinyGo to compile to Wasm. func main() { greeting.RegisterGreeter(MyPlugin{}) - } type MyPlugin struct{} @@ -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") diff --git a/examples/helloworld/greeting/greet_host.pb.go b/examples/helloworld/greeting/greet_host.pb.go index ce25a19..ffbb2f5 100644 --- a/examples/helloworld/greeting/greet_host.pb.go +++ b/examples/helloworld/greeting/greet_host.pb.go @@ -49,6 +49,14 @@ func NewGreeterPlugin(ctx context.Context, opt GreeterPluginOption) (*GreeterPlu config: config, }, nil } + +func (p *GreeterPlugin) Close(ctx context.Context) (err error) { + if r := p.runtime; r != nil { + err = r.Close(ctx) + } + return +} + func (p *GreeterPlugin) Load(ctx context.Context, pluginPath string) (Greeter, error) { b, err := os.ReadFile(pluginPath) if err != nil { diff --git a/examples/helloworld/main.go b/examples/helloworld/main.go index 377ccdf..0590028 100644 --- a/examples/helloworld/main.go +++ b/examples/helloworld/main.go @@ -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 { diff --git a/examples/host-functions/greeting/greet_host.pb.go b/examples/host-functions/greeting/greet_host.pb.go index 124702a..e4f38d9 100644 --- a/examples/host-functions/greeting/greet_host.pb.go +++ b/examples/host-functions/greeting/greet_host.pb.go @@ -135,6 +135,14 @@ func NewGreeterPlugin(ctx context.Context, opt GreeterPluginOption) (*GreeterPlu config: config, }, nil } + +func (p *GreeterPlugin) Close(ctx context.Context) (err error) { + if r := p.runtime; r != nil { + err = r.Close(ctx) + } + return +} + func (p *GreeterPlugin) Load(ctx context.Context, pluginPath string, hostFunctions HostFunctions) (Greeter, error) { b, err := os.ReadFile(pluginPath) if err != nil { diff --git a/examples/host-functions/main.go b/examples/host-functions/main.go index 0e32051..f11465a 100644 --- a/examples/host-functions/main.go +++ b/examples/host-functions/main.go @@ -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{}) diff --git a/examples/known-types/known/known_host.pb.go b/examples/known-types/known/known_host.pb.go index 308faa3..0d4fa38 100644 --- a/examples/known-types/known/known_host.pb.go +++ b/examples/known-types/known/known_host.pb.go @@ -49,6 +49,14 @@ func NewWellKnownPlugin(ctx context.Context, opt WellKnownPluginOption) (*WellKn config: config, }, nil } + +func (p *WellKnownPlugin) Close(ctx context.Context) (err error) { + if r := p.runtime; r != nil { + err = r.Close(ctx) + } + return +} + func (p *WellKnownPlugin) Load(ctx context.Context, pluginPath string) (WellKnown, error) { b, err := os.ReadFile(pluginPath) if err != nil { diff --git a/examples/known-types/main.go b/examples/known-types/main.go index 5b4acd9..02c9a9d 100644 --- a/examples/known-types/main.go +++ b/examples/known-types/main.go @@ -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 { diff --git a/examples/wasi/cat/cat_host.pb.go b/examples/wasi/cat/cat_host.pb.go index 792c8d8..ecb0d67 100644 --- a/examples/wasi/cat/cat_host.pb.go +++ b/examples/wasi/cat/cat_host.pb.go @@ -49,6 +49,14 @@ func NewFileCatPlugin(ctx context.Context, opt FileCatPluginOption) (*FileCatPlu config: config, }, nil } + +func (p *FileCatPlugin) Close(ctx context.Context) (err error) { + if r := p.runtime; r != nil { + err = r.Close(ctx) + } + return +} + func (p *FileCatPlugin) Load(ctx context.Context, pluginPath string) (FileCat, error) { b, err := os.ReadFile(pluginPath) if err != nil { diff --git a/examples/wasi/main.go b/examples/wasi/main.go index 2321496..1053035 100644 --- a/examples/wasi/main.go +++ b/examples/wasi/main.go @@ -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 { diff --git a/gen/host.go b/gen/host.go index 54c48ec..212116f 100644 --- a/gen/host.go +++ b/gen/host.go @@ -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" @@ -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")), diff --git a/tests/fields/fields_test.go b/tests/fields/fields_test.go index 2beffe9..eac0a7c 100644 --- a/tests/fields/fields_test.go +++ b/tests/fields/fields_test.go @@ -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) diff --git a/tests/fields/proto/fields_host.pb.go b/tests/fields/proto/fields_host.pb.go index c805e8f..1e0c29c 100644 --- a/tests/fields/proto/fields_host.pb.go +++ b/tests/fields/proto/fields_host.pb.go @@ -49,6 +49,14 @@ func NewFieldTestPlugin(ctx context.Context, opt FieldTestPluginOption) (*FieldT config: config, }, nil } + +func (p *FieldTestPlugin) Close(ctx context.Context) (err error) { + if r := p.runtime; r != nil { + err = r.Close(ctx) + } + return +} + func (p *FieldTestPlugin) Load(ctx context.Context, pluginPath string) (FieldTest, error) { b, err := os.ReadFile(pluginPath) if err != nil { diff --git a/tests/host-functions/host_functions_test.go b/tests/host-functions/host_functions_test.go index 4dc29c8..8f50474 100644 --- a/tests/host-functions/host_functions_test.go +++ b/tests/host-functions/host_functions_test.go @@ -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{}) diff --git a/tests/host-functions/proto/host_host.pb.go b/tests/host-functions/proto/host_host.pb.go index 2ee00bf..880f087 100644 --- a/tests/host-functions/proto/host_host.pb.go +++ b/tests/host-functions/proto/host_host.pb.go @@ -99,6 +99,14 @@ func NewGreeterPlugin(ctx context.Context, opt GreeterPluginOption) (*GreeterPlu config: config, }, nil } + +func (p *GreeterPlugin) Close(ctx context.Context) (err error) { + if r := p.runtime; r != nil { + err = r.Close(ctx) + } + return +} + func (p *GreeterPlugin) Load(ctx context.Context, pluginPath string, hostFunctions HostFunctions) (Greeter, error) { b, err := os.ReadFile(pluginPath) if err != nil { diff --git a/tests/import/import_test.go b/tests/import/import_test.go index 139401d..9bb2745 100644 --- a/tests/import/import_test.go +++ b/tests/import/import_test.go @@ -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) diff --git a/tests/import/proto/bar/bar_host.pb.go b/tests/import/proto/bar/bar_host.pb.go index 2cb9244..9b2bcc9 100644 --- a/tests/import/proto/bar/bar_host.pb.go +++ b/tests/import/proto/bar/bar_host.pb.go @@ -49,6 +49,14 @@ func NewBarPlugin(ctx context.Context, opt BarPluginOption) (*BarPlugin, error) config: config, }, nil } + +func (p *BarPlugin) Close(ctx context.Context) (err error) { + if r := p.runtime; r != nil { + err = r.Close(ctx) + } + return +} + func (p *BarPlugin) Load(ctx context.Context, pluginPath string) (Bar, error) { b, err := os.ReadFile(pluginPath) if err != nil { diff --git a/tests/import/proto/foo/foo_host.pb.go b/tests/import/proto/foo/foo_host.pb.go index efd4496..1511dbe 100644 --- a/tests/import/proto/foo/foo_host.pb.go +++ b/tests/import/proto/foo/foo_host.pb.go @@ -50,6 +50,14 @@ func NewFooPlugin(ctx context.Context, opt FooPluginOption) (*FooPlugin, error) 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 { diff --git a/tests/well-known/proto/known_host.pb.go b/tests/well-known/proto/known_host.pb.go index 54f5951..0286d7e 100644 --- a/tests/well-known/proto/known_host.pb.go +++ b/tests/well-known/proto/known_host.pb.go @@ -50,6 +50,14 @@ func NewKnownTypesTestPlugin(ctx context.Context, opt KnownTypesTestPluginOption config: config, }, nil } + +func (p *KnownTypesTestPlugin) Close(ctx context.Context) (err error) { + if r := p.runtime; r != nil { + err = r.Close(ctx) + } + return +} + func (p *KnownTypesTestPlugin) Load(ctx context.Context, pluginPath string) (KnownTypesTest, error) { b, err := os.ReadFile(pluginPath) if err != nil { @@ -198,6 +206,14 @@ func NewEmptyTestPlugin(ctx context.Context, opt EmptyTestPluginOption) (*EmptyT config: config, }, nil } + +func (p *EmptyTestPlugin) Close(ctx context.Context) (err error) { + if r := p.runtime; r != nil { + err = r.Close(ctx) + } + return +} + func (p *EmptyTestPlugin) Load(ctx context.Context, pluginPath string) (EmptyTest, error) { b, err := os.ReadFile(pluginPath) if err != nil { diff --git a/tests/well-known/well_known_test.go b/tests/well-known/well_known_test.go index cdbc75c..5f1b65d 100644 --- a/tests/well-known/well_known_test.go +++ b/tests/well-known/well_known_test.go @@ -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)