Skip to content

Commit

Permalink
Add example
Browse files Browse the repository at this point in the history
Signed-off-by: knqyf263 <knqyf263@gmail.com>
  • Loading branch information
knqyf263 committed May 17, 2022
1 parent 8763291 commit 8f4dd46
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 5 deletions.
33 changes: 30 additions & 3 deletions examples/wasi/cat.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package wasi_example
package main

import (
"context"
Expand All @@ -9,11 +9,12 @@ import (
"os"

"github.com/tetratelabs/wazero"
expfs "github.com/tetratelabs/wazero/experimental/fs"
"github.com/tetratelabs/wazero/wasi"
)

// catFS is an embedded filesystem limited to test.txt
//go:embed testdata/test.txt
//go:embed testdata/test.txt testdata/sub/test.txt
var catFS embed.FS

// catWasm was compiled the TinyGo source testdata/cat.go
Expand Down Expand Up @@ -55,7 +56,33 @@ func main() {
// InstantiateModule runs the "_start" function which is what TinyGo compiles "main" to.
// * Set the program name (arg[0]) to "wasi" and add args to write "test.txt" to stdout twice.
// * We use "/test.txt" or "./test.txt" because WithFS by default maps the workdir "." to "/".
if _, err = r.InstantiateModule(ctx, code, config.WithArgs("wasi", os.Args[1])); err != nil {
mod, err := r.InstantiateModule(ctx, code, config.WithArgs("wasi", os.Args[1]))
if err != nil {
log.Panicln(err)
}

// Pass another fs.FS through context when calling a function
sub, err := fs.Sub(rooted, "sub")
if err != nil {
log.Panicln(err)
}

fsConfig := wazero.NewFSConfig().WithFS(sub)
subCtx, closer, err := expfs.WithFS(ctx, *fsConfig)
if err != nil {
log.Panicln(err)
}
defer closer.Close(subCtx)

subdir := mod.ExportedFunction("subdir")

// Call the function with the new filesystem and display "greet sub dir"
if _, err = subdir.Call(subCtx); err != nil {
log.Panicln(err)
}

// Call the function with the previous filesystem and display "greet filesystem"
if _, err = subdir.Call(ctx); err != nil {
log.Panicln(err)
}
}
7 changes: 5 additions & 2 deletions examples/wasi/cat_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package wasi_example
package main

import (
"testing"
Expand All @@ -12,5 +12,8 @@ import (
// go run cat.go ./test.txt
func Test_main(t *testing.T) {
stdout, _ := maintester.TestMain(t, main, "cat", "./test.txt")
require.Equal(t, "greet filesystem\n", stdout)
require.Equal(t, `greet filesystem
greet sub dir
greet filesystem
`, stdout)
}
11 changes: 11 additions & 0 deletions examples/wasi/testdata/cat.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,14 @@ func main() {
os.Stdout.Write(bytes)
}
}

//export subdir
func subdir() {
bytes, err := ioutil.ReadFile("./test.txt")
if err != nil {
os.Exit(1)
}

// Use write to avoid needing to worry about Windows newlines.
os.Stdout.Write(bytes)
}
Binary file modified examples/wasi/testdata/cat.wasm
Binary file not shown.
1 change: 1 addition & 0 deletions examples/wasi/testdata/sub/test.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
greet sub dir

0 comments on commit 8f4dd46

Please sign in to comment.