diff --git a/api/wasm.go b/api/wasm.go index f366ef70..8c101e7d 100644 --- a/api/wasm.go +++ b/api/wasm.go @@ -129,22 +129,6 @@ type Module interface { // Name is the name this module was instantiated with. Exported functions can be imported with this name. Name() string - // Close is a convenience that invokes CloseWithExitCode with zero. - // Note: When the context is nil, it defaults to context.Background. - Close(context.Context) error - - // CloseWithExitCode releases resources allocated for this Module. Use a non-zero exitCode parameter to indicate a - // failure to ExportedFunction callers. - // - // The error returned here, if present, is about resource de-allocation (such as I/O errors). Only the last error is - // returned, so a non-nil return means at least one error happened. Regardless of error, this module instance will - // be removed, making its name available again. - // - // Calling this inside a host function is safe, and may cause ExportedFunction callers to receive a sys.ExitError - // with the exitCode. - // Note: When the context is nil, it defaults to context.Background. - CloseWithExitCode(ctx context.Context, exitCode uint32) error - // Memory returns a memory defined in this module or nil if there are none wasn't. Memory() Memory @@ -162,6 +146,30 @@ type Module interface { // ExportedGlobal a global exported from this module or nil if it wasn't. ExportedGlobal(name string) Global + + // CloseWithExitCode releases resources allocated for this Module. Use a non-zero exitCode parameter to indicate a + // failure to ExportedFunction callers. + // + // The error returned here, if present, is about resource de-allocation (such as I/O errors). Only the last error is + // returned, so a non-nil return means at least one error happened. Regardless of error, this module instance will + // be removed, making its name available again. + // + // Calling this inside a host function is safe, and may cause ExportedFunction callers to receive a sys.ExitError + // with the exitCode. + // Note: When the context is nil, it defaults to context.Background. + CloseWithExitCode(ctx context.Context, exitCode uint32) error + + // Closer closes this module by delegating to CloseWithExitCode with an exit code of zero. + Closer +} + +// Closer closes a resource. +// +// Note: This is an interface for decoupling, not third-party implementations. All implementations are in wazero. +type Closer interface { + // Close closes the resource. + // Note: When the context is nil, it defaults to context.Background. + Close(context.Context) error } // Function is a WebAssembly 1.0 (20191205) function exported from an instantiated module (wazero.Runtime InstantiateModule). diff --git a/internal/integration_test/vs/runtime.go b/internal/integration_test/vs/runtime.go index 28d104ec..7cff9c02 100644 --- a/internal/integration_test/vs/runtime.go +++ b/internal/integration_test/vs/runtime.go @@ -58,8 +58,9 @@ type wazeroRuntime struct { } type wazeroModule struct { - wasi, env, mod api.Module - funcs map[string]api.Function + wasi api.Closer + env, mod api.Module + funcs map[string]api.Function } func (r *wazeroRuntime) Name() string { diff --git a/wasi/wasi.go b/wasi/wasi.go index 8288847c..6e2e0949 100644 --- a/wasi/wasi.go +++ b/wasi/wasi.go @@ -36,8 +36,8 @@ const ModuleSnapshotPreview1 = "wasi_snapshot_preview1" // mod, _ := r.InstantiateModuleFromCode(ctx, source) // // Note: All WASI functions return a single Errno result, ErrnoSuccess on success. -// Note: Closing the wazero.Runtime closes any api.Module it instantiated. -func InstantiateSnapshotPreview1(ctx context.Context, r wazero.Runtime) (api.Module, error) { +// Note: Closing the wazero.Runtime closes this instance of WASI as well. +func InstantiateSnapshotPreview1(ctx context.Context, r wazero.Runtime) (api.Closer, error) { _, fns := snapshotPreview1Functions(ctx) return r.NewModuleBuilder(ModuleSnapshotPreview1).ExportFunctions(fns).Instantiate(ctx) } diff --git a/wasm.go b/wasm.go index ce73069c..bd34be14 100644 --- a/wasm.go +++ b/wasm.go @@ -98,19 +98,6 @@ type Runtime interface { // Note: When the context is nil, it defaults to context.Background. InstantiateModule(ctx context.Context, compiled CompiledModule, config ModuleConfig) (api.Module, error) - // Close closes all the modules that have been initialized in this Runtime with an exit code of 0. - // An error is returned if any module returns an error when closed. - // - // Ex. - // ctx := context.Background() - // r := wazero.NewRuntime() - // defer r.Close(ctx) // This closes everything this Runtime created. - // - // // Everything below here can be closed, but will anyway due to above. - // _, _ = wasi.InstantiateSnapshotPreview1(ctx, r) - // mod, _ := r.InstantiateModuleFromCode(ctx, source) - Close(context.Context) error - // CloseWithExitCode closes all the modules that have been initialized in this Runtime with the provided exit code. // An error is returned if any module returns an error when closed. // @@ -123,6 +110,10 @@ type Runtime interface { // _, _ = wasi.InstantiateSnapshotPreview1(ctx, r) // mod, _ := r.InstantiateModuleFromCode(ctx, source) CloseWithExitCode(ctx context.Context, exitCode uint32) error + + // Closer closes resources initialized by this Runtime by delegating to CloseWithExitCode with an exit code of + // zero. + api.Closer } func NewRuntime() Runtime {