Skip to content

Commit

Permalink
fixes arg handling
Browse files Browse the repository at this point in the history
Signed-off-by: Adrian Cole <[email protected]>
  • Loading branch information
Adrian Cole committed Mar 19, 2022
1 parent b68e965 commit c7046e8
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 5 deletions.
8 changes: 6 additions & 2 deletions examples/file_system_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,16 @@ func Test_Cat(t *testing.T) {
stdoutBuf := bytes.NewBuffer(nil)
wasiConfig := wazero.NewWASIConfig().WithStdout(stdoutBuf)

// Next, configure a sand-boxed filesystem to include one file. We'll use that as the argument to `cat`.
// Next, configure a sand-boxed filesystem to include one file.
file := "cat.go" // arbitrary file
memFS := wazero.WASIMemFS()
err := writeFile(memFS, file, catGo)
require.NoError(t, err)
wasiConfig.WithPreopens(map[string]wasi.FS{".": memFS}).WithArgs(file)
wasiConfig.WithPreopens(map[string]wasi.FS{".": memFS})

// Since this runs a main function (_start in WASI), configure the arguments.
// Remember, arg[0] is the program name!
wasiConfig.WithArgs("cat", file)

// Now, instantiate WASI with the above configuration.
wasi, err := r.InstantiateModule(wazero.WASISnapshotPreview1WithConfig(wasiConfig))
Expand Down
7 changes: 4 additions & 3 deletions examples/testdata/cat.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ import (
"os"
)

// main is the same as cat.
// main is the same as cat: "concatenate and print files."
func main() {
for _, file := range os.Args {
bytes, err := os.ReadFile(file)
// Start at arg[1] because args[0] is the program name.
for i := 1; i < len(os.Args); i++ {
bytes, err := os.ReadFile(os.Args[i])
if err != nil {
os.Exit(1)
}
Expand Down
Binary file modified examples/testdata/cat.wasm
Binary file not shown.

0 comments on commit c7046e8

Please sign in to comment.