Adds a Closer interface to encapsulate the API for closing a re… (#554)

* Adds a ModuleCloser interface to encapsulate the API for closing a Module

Signed-off-by: Anuraag Agrawal <anuraaga@gmail.com>

Co-authored-by: Crypt Keeper <64215+codefromthecrypt@users.noreply.github.com>
This commit is contained in:
Anuraag Agrawal
2022-05-13 14:00:12 +09:00
committed by GitHub
parent ab25a84b4f
commit 80da871681
4 changed files with 33 additions and 33 deletions

View File

@@ -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).

View File

@@ -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 {

View File

@@ -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)
}

17
wasm.go
View File

@@ -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 {