Propagates context to all api interface methods that aren't constant (#502)
This prepares for exposing operations like Memory.Grow while keeping the ability to trace what did that, by adding a `context.Context` initial parameter. This adds this to all API methods that mutate or return mutated data. Before, we made a change to trace functions and general lifecycle commands, but we missed this part. Ex. We track functions, but can't track what closed the module, changed memory or a mutable constant. Changing to do this now is not only more consistent, but helps us optimize at least the interpreter to help users identify otherwise opaque code that can cause harm. This is critical before we add more functions that can cause harm, such as Memory.Grow. Signed-off-by: Adrian Cole <adrian@tetrate.io>
This commit is contained in:
18
wasm.go
18
wasm.go
@@ -45,7 +45,7 @@ type Runtime interface {
|
||||
// * Improve performance when the same module is instantiated multiple times under different names
|
||||
// * Reduce the amount of errors that can occur during InstantiateModule.
|
||||
//
|
||||
// Note: when `ctx` is nil, it defaults to context.Background.
|
||||
// Note: When the context is nil, it defaults to context.Background.
|
||||
// Note: The resulting module name defaults to what was binary from the custom name section.
|
||||
// See https://www.w3.org/TR/2019/REC-wasm-core-1-20191205/#name-section%E2%91%A0
|
||||
CompileModule(ctx context.Context, source []byte) (*CompiledCode, error)
|
||||
@@ -58,7 +58,7 @@ type Runtime interface {
|
||||
// module, _ := wazero.NewRuntime().InstantiateModuleFromCode(ctx, source)
|
||||
// defer module.Close()
|
||||
//
|
||||
// Note: when `ctx` is nil, it defaults to context.Background.
|
||||
// Note: When the context is nil, it defaults to context.Background.
|
||||
// Note: This is a convenience utility that chains CompileModule with InstantiateModule. To instantiate the same
|
||||
// source multiple times, use CompileModule as InstantiateModule avoids redundant decoding and/or compilation.
|
||||
InstantiateModuleFromCode(ctx context.Context, source []byte) (api.Module, error)
|
||||
@@ -74,7 +74,7 @@ type Runtime interface {
|
||||
// )
|
||||
// defer wasm.Close()
|
||||
//
|
||||
// Note: When `ctx` is nil, it defaults to context.Background.
|
||||
// Note: When the context is nil, it defaults to context.Background.
|
||||
InstantiateModuleFromCodeWithConfig(ctx context.Context, source []byte, config *ModuleConfig) (api.Module, error)
|
||||
|
||||
// InstantiateModule instantiates the module namespace or errs if the configuration was invalid.
|
||||
@@ -92,7 +92,7 @@ type Runtime interface {
|
||||
// * The module has a table element initializer that resolves to an index outside the Table minimum size.
|
||||
// * The module has a start function, and it failed to execute.
|
||||
//
|
||||
// Note: When `ctx` is nil, it defaults to context.Background.
|
||||
// Note: When the context is nil, it defaults to context.Background.
|
||||
InstantiateModule(ctx context.Context, compiled *CompiledCode) (api.Module, error)
|
||||
|
||||
// InstantiateModuleWithConfig is like InstantiateModule, except you can override configuration such as the module
|
||||
@@ -111,7 +111,7 @@ type Runtime interface {
|
||||
// // Assign different configuration on each instantiation
|
||||
// module, _ := r.InstantiateModuleWithConfig(ctx, compiled, config.WithName("rotate").WithArgs("rotate", "angle=90", "dir=cw"))
|
||||
//
|
||||
// Note: when `ctx` is nil, it defaults to context.Background.
|
||||
// Note: When the context is nil, it defaults to context.Background.
|
||||
// Note: Config is copied during instantiation: Later changes to config do not affect the instantiated result.
|
||||
InstantiateModuleWithConfig(ctx context.Context, compiled *CompiledCode, config *ModuleConfig) (mod api.Module, err error)
|
||||
}
|
||||
@@ -188,8 +188,8 @@ func (r *runtime) InstantiateModuleFromCode(ctx context.Context, source []byte)
|
||||
if compiled, err := r.CompileModule(ctx, source); err != nil {
|
||||
return nil, err
|
||||
} else {
|
||||
// *wasm.ModuleInstance for the source cannot be tracked, so we release the cache inside of this function.
|
||||
defer compiled.Close()
|
||||
// *wasm.ModuleInstance for the source cannot be tracked, so we release the cache inside this function.
|
||||
defer compiled.Close(ctx)
|
||||
return r.InstantiateModule(ctx, compiled)
|
||||
}
|
||||
}
|
||||
@@ -199,8 +199,8 @@ func (r *runtime) InstantiateModuleFromCodeWithConfig(ctx context.Context, sourc
|
||||
if compiled, err := r.CompileModule(ctx, source); err != nil {
|
||||
return nil, err
|
||||
} else {
|
||||
// *wasm.ModuleInstance for the source cannot be tracked, so we release the cache inside of this function.
|
||||
defer compiled.Close()
|
||||
// *wasm.ModuleInstance for the source cannot be tracked, so we release the cache inside this function.
|
||||
defer compiled.Close(ctx)
|
||||
return r.InstantiateModuleWithConfig(ctx, compiled, config)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user