-
Notifications
You must be signed in to change notification settings - Fork 267
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds wasm_exec for Go generated Wasm
Signed-off-by: Adrian Cole <adrian@tetrate.io>
- Loading branch information
Adrian Cole
committed
Jul 22, 2022
1 parent
866fac2
commit e758e20
Showing
22 changed files
with
2,206 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
wasi |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
## WASI example | ||
|
||
This example shows how to use I/O in your WebAssembly modules using WASI | ||
(WebAssembly System Interface). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"embed" | ||
_ "embed" | ||
"io/fs" | ||
"log" | ||
"os" | ||
|
||
"github.com/tetratelabs/wazero" | ||
"github.com/tetratelabs/wazero/sys" | ||
"github.com/tetratelabs/wazero/wasm_exec" | ||
) | ||
|
||
// catFS is an embedded filesystem limited to test.txt | ||
//go:embed testdata/test.txt | ||
var catFS embed.FS | ||
|
||
// catWasm was compiled the TinyGo source testdata/cat.go | ||
//go:embed testdata/cat.wasm | ||
var catWasm []byte | ||
|
||
// main writes an input file to stdout, just like `cat`. | ||
// | ||
// This is a basic introduction to the WebAssembly System Interface (WASI). | ||
// See https://github.com/WebAssembly/WASI | ||
func main() { | ||
// Choose the context to use for function calls. | ||
ctx := context.Background() | ||
|
||
// Create a new WebAssembly Runtime. | ||
r := wazero.NewRuntime() | ||
defer r.Close(ctx) // This closes everything this Runtime created. | ||
|
||
// Compile the WebAssembly module using the default configuration. | ||
compiled, err := r.CompileModule(ctx, catWasm, wazero.NewCompileConfig()) | ||
if err != nil { | ||
log.Panicln(err) | ||
} | ||
|
||
we, err := wasm_exec.NewBuilder().Build(ctx, r) | ||
if err != nil { | ||
log.Panicln(err) | ||
} | ||
|
||
// Since wazero uses fs.FS, we can use standard libraries to do things like trim the leading path. | ||
rooted, err := fs.Sub(catFS, "testdata") | ||
if err != nil { | ||
log.Panicln(err) | ||
} | ||
|
||
// Create a configuration for running main, overriding defaults (which discard stdout and has no file system). | ||
config := wazero.NewModuleConfig(). | ||
WithFS(rooted). | ||
WithStdout(os.Stdout). | ||
WithStderr(os.Stderr). | ||
WithArgs("cat", os.Args[1]) | ||
|
||
err = we.Run(ctx, compiled, config) | ||
if exitErr, ok := err.(*sys.ExitError); ok && exitErr.ExitCode() != 0 { | ||
log.Panicln(err) | ||
} else if !ok { | ||
log.Panicln(err) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"io/fs" | ||
"testing" | ||
|
||
"github.com/tetratelabs/wazero" | ||
"github.com/tetratelabs/wazero/internal/testing/maintester" | ||
"github.com/tetratelabs/wazero/internal/testing/require" | ||
"github.com/tetratelabs/wazero/sys" | ||
"github.com/tetratelabs/wazero/wasm_exec" | ||
) | ||
|
||
// Test_main ensures the following will work: | ||
// | ||
// go run cat.go /test.txt | ||
func Test_main(t *testing.T) { | ||
stdout, stderr := maintester.TestMain(t, main, "cat", "/test.txt") | ||
require.Equal(t, "", stderr) | ||
require.Equal(t, "greet filesystem\n", stdout) | ||
} | ||
|
||
func Benchmark_main(b *testing.B) { | ||
// Choose the context to use for function calls. | ||
ctx := context.Background() | ||
|
||
// Create a new WebAssembly Runtime. | ||
r := wazero.NewRuntimeWithConfig(wazero.NewRuntimeConfigCompiler()) | ||
defer r.Close(ctx) // This closes everything this Runtime created. | ||
|
||
// Compile the WebAssembly module using the default configuration. | ||
compiled, err := r.CompileModule(ctx, catWasm, wazero.NewCompileConfig()) | ||
if err != nil { | ||
b.Fatal(err) | ||
} | ||
|
||
we, err := wasm_exec.NewBuilder().Build(ctx, r) | ||
|
||
rooted, err := fs.Sub(catFS, "testdata") | ||
if err != nil { | ||
b.Fatal(err) | ||
} | ||
config := wazero.NewModuleConfig().WithFS(rooted).WithArgs("cat", "/test.txt") | ||
|
||
b.Run("go cat", func(b *testing.B) { | ||
b.ReportAllocs() | ||
for i := 0; i < b.N; i++ { | ||
err = we.Run(ctx, compiled, config) | ||
if exitErr, ok := err.(*sys.ExitError); ok && exitErr.ExitCode() != 0 { | ||
b.Fatal(err) | ||
} else if !ok { | ||
b.Fatal(err) | ||
} | ||
} | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package main | ||
|
||
import ( | ||
"io/ioutil" | ||
"os" | ||
) | ||
|
||
// main is the same as wasi: "concatenate and print files." | ||
func main() { | ||
// Start at arg[1] because args[0] is the program name. | ||
for i := 1; i < len(os.Args); i++ { | ||
// Intentionally use ioutil.ReadFile instead of os.ReadFile for TinyGo. | ||
bytes, err := ioutil.ReadFile(os.Args[i]) | ||
if err != nil { | ||
os.Exit(1) | ||
} | ||
|
||
// Use write to avoid needing to worry about Windows newlines. | ||
os.Stdout.Write(bytes) | ||
} | ||
} |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
greet sub dir |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
greet filesystem |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.