Skip to content

Commit

Permalink
feat(hostcallDuringLoad): Allow updating the context with InvokeConte…
Browse files Browse the repository at this point in the history
…xt which allows hostcalls during module load
  • Loading branch information
ritesh089 committed Sep 22, 2023
1 parent 70a4115 commit ba03c66
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
9 changes: 8 additions & 1 deletion engines/wazero/wazero.go
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ func (w *wapcHost) hostCall(ctx context.Context, m api.Module, stack []uint64) {
namespace := requireReadString(mem, "namespace", nsPtr, nsLen)
operation := requireReadString(mem, "operation", cmdPtr, cmdLen)
payload := requireRead(mem, "payload", payloadPtr, payloadLen)

if ic.hostResp, ic.hostErr = w.callHandler(ctx, binding, namespace, operation, payload); ic.hostErr != nil {
stack[0] = 0 // false: error (assumed to be logged already?)
} else {
Expand Down Expand Up @@ -465,3 +465,10 @@ func requireRead(mem api.Memory, fieldName string, offset, byteCount uint32) []b
}
return buf
}

// AddInvokeContext allows the use of invocation context during module load. This allows host calls from the wapc guest's main()
func AddInvokeContext(ctx context.Context) context.Context {
ic := invokeContext{}
ctx = newInvokeContext(ctx, &ic)
return ctx
}
13 changes: 13 additions & 0 deletions engines/wazero/wazero_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,16 @@ func TestMain(m *testing.M) {
os.Exit(m.Run())
}

// TestAddInvokeContext ensures we can read the example wasm prior to running unit tests.
func TestAddInvokeContext(t *testing.T) {
ctx := context.Background()
ctx = AddInvokeContext(ctx)
ic := fromInvokeContext(ctx)
if ic == nil {
t.Errorf("Error adding InvocationContext to context ")
}
}

// TestModule_UnwrapRuntime ensures the Unwrap returns the correct Runtime interface
func TestModule_UnwrapRuntime(t *testing.T) {
m, err := EngineWithRuntime(DefaultRuntime).New(testCtx, wapc.NoOpHostCallHandler, guest, mc)
Expand Down Expand Up @@ -141,4 +151,7 @@ func TestEngineWithRuntime(t *testing.T) {
t.Errorf("Unexpected error, got %v, expected %v", err, expectedErr)
}
})

}


0 comments on commit ba03c66

Please sign in to comment.