Adds StartWASICommandWithConfig to defer configuration (#367)

StartWASICommandWithConfig is like StartWASICommand, except you can override configuration based on the importing
module. For example, you can use this to define different args depending on the importing module.

```go
// Initialize base configuration:
r := wazero.NewRuntime()
config := wazero.NewWASIConfig().WithStdout(buf)
wasi, _ := r.NewHostModule(wazero.WASISnapshotPreview1WithConfig(config))
decoded, _ := r.CompileModule(source)

// Assign configuration only when ready to instantiate.
module, _ := StartWASICommandWithConfig(r, decoded, config.WithArgs("rotate", "angle=90", "dir=cw"))
```

This also changes the config to be immutable since it may be reused many
times. Instead of assigning fields directly, use `WithXXX`.

Finally, this changes the examples to encourage using `.Close()`

Fixes #364

Signed-off-by: Adrian Cole <adrian@tetrate.io>
This commit is contained in:
Crypt Keeper
2022-03-14 14:39:53 +08:00
committed by GitHub
parent ab2b737df1
commit a8944f6098
22 changed files with 836 additions and 462 deletions

View File

@@ -1,6 +1,7 @@
package internalwasm
import (
"context"
gobinary "encoding/binary"
"testing"
@@ -259,7 +260,7 @@ func TestPublicModule_Global(t *testing.T) {
s := newStore()
t.Run(tc.name, func(t *testing.T) {
// Instantiate the module and get the export of the above global
module, err := s.Instantiate(tc.module, "")
module, err := s.Instantiate(context.Background(), tc.module, "")
require.NoError(t, err)
if global := module.ExportedGlobal("global"); tc.expected != nil {