Refactors API to ensure context propagation (#482)

This is an API breaking change that does a few things:

* Stop encouraging practice that can break context propagation:
  * Stops caching `context.Context` in `wazero.RuntimeConfig`
  * Stops caching `context.Context` in `api.Module`

* Fixes context propagation in function calls:
  * Changes `api.Function`'s arg0 from `api.Module` to `context.Context`
  * Adds `context.Context` parameter in instantiation (propagates to
    .start)

* Allows context propagation for heavy operations like compile:
  * Adds `context.Context` as the initial parameter of `CompileModule`

The design we had earlier was a good start, but this is the only way to
ensure coherence when users start correlating or tracing. While adding a
`context.Context` parameter may seem difficult, wazero is a low-level
library and WebAssembly is notoriously difficult to troubleshoot. In
other words, it will be easier to explain to users to pass (even nil) as
the context parameter vs try to figure out things without coherent
context.

Signed-off-by: Adrian Cole <adrian@tetrate.io>
This commit is contained in:
Crypt Keeper
2022-04-19 16:52:57 +08:00
committed by GitHub
parent 64d7379ad0
commit 45ccab589b
42 changed files with 592 additions and 607 deletions

View File

@@ -1,7 +1,6 @@
package wazero
import (
"context"
"io"
"math"
"testing"
@@ -17,24 +16,6 @@ func TestRuntimeConfig(t *testing.T) {
with func(*RuntimeConfig) *RuntimeConfig
expected *RuntimeConfig
}{
{
name: "WithContext",
with: func(c *RuntimeConfig) *RuntimeConfig {
return c.WithContext(context.TODO())
},
expected: &RuntimeConfig{
ctx: context.TODO(),
},
},
{
name: "WithContext - nil",
with: func(c *RuntimeConfig) *RuntimeConfig {
return c.WithContext(nil) //nolint
},
expected: &RuntimeConfig{
ctx: context.Background(),
},
},
{
name: "WithMemoryMaxPages",
with: func(c *RuntimeConfig) *RuntimeConfig {