Skip to content

Commit

Permalink
CallFunction: ensure context has a non-nil value.
Browse files Browse the repository at this point in the history
  • Loading branch information
pkedy committed Feb 7, 2022
1 parent 8dff7ae commit f0c1643
Showing 1 changed file with 11 additions and 0 deletions.
11 changes: 11 additions & 0 deletions wasm/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,12 @@ func (s *Store) Instantiate(module *Module, name string) error {
return nil
}

// CallFunction looks up an exported function given its module and function name and calls it.
// Any returned results and resultTypes are index-correlated, noting WebAssembly 1.0 (MVP) allows
// up to one result. An error is returned for any failure looking up or invoking the function including
// signature mismatch.
//
// Note: The ctx parameter will be the outer-most ancestor of HostFunctionCallContext.Context.
// Note: this API is unstable. See tetratelabs/wazero#170
func (s *Store) CallFunction(ctx context.Context, moduleName, funcName string, params ...uint64) (results []uint64, resultTypes []ValueType, err error) {
var exp *ExportInstance
Expand All @@ -328,6 +334,11 @@ func (s *Store) CallFunction(ctx context.Context, moduleName, funcName string, p
return
}

// Ensure context has a value so that host calls can assume it is non-nil.
if ctx == nil {
ctx = context.Background()
}

results, err = s.engine.Call(ctx, f, params...)
resultTypes = f.FunctionType.Type.Results
return
Expand Down

0 comments on commit f0c1643

Please sign in to comment.