From f504b4967a75187ab81b9076808064c8bcaa3b97 Mon Sep 17 00:00:00 2001 From: r8d8 Date: Sat, 12 Feb 2022 21:10:01 +0200 Subject: [PATCH] Fix `fibonacci` example in Readme --- README.md | 27 ++++++++++++++++++++++----- 1 file changed, 22 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 5201fe28ba..469e169b16 100644 --- a/README.md +++ b/README.md @@ -5,23 +5,40 @@ portability features like cross compilation. Import wazero and extend your Go ap language! ## Example + Here's an example of using wazero to invoke a Fibonacci function included in a Wasm binary. -While our [source for this](examples/testdata/fibonacci.go) is [TinyGo](https://tinygo.org/), it could have been written in -another language that targets Wasm, such as Rust. +While our [source for this](examples/testdata/fibonacci.go) is [TinyGo](https://tinygo.org/), it could have been written in another language that targets Wasm, such as AssemblyScript/C/C++/Rust/Zig. ```golang +package main + +import ( + "context" + "fmt" + "os" + + "github.com/tetratelabs/wazero/wasi" + "github.com/tetratelabs/wazero/wasm" + "github.com/tetratelabs/wazero/wasm/binary" + "github.com/tetratelabs/wazero/wasm/interpreter" +) + func main() { + // Default context impl. by Go + ctx := context.Background() // Read WebAssembly binary. - source, _ := os.ReadFile("fibonacci.wasm") + source, _ := os.ReadFile("examples/testdata/fibonacci.wasm") // Decode the binary as WebAssembly module. mod, _ := binary.DecodeModule(source) // Initialize the execution environment called "store" with Interpreter-based engine. store := wasm.NewStore(interpreter.NewEngine()) + // To resolve WASI specific methods, such as `fd_write` + wasi.RegisterAPI(store) // Instantiate the decoded module. store.Instantiate(mod, "test") // Execute the exported "fibonacci" function from the instantiated module. - ret, _, err := store.CallFunction("test", "fibonacci", 20) + ret, _, _ := store.CallFunction(ctx, "test", "fibonacci", 20) // Give us the fibonacci number for 20, namely 6765! fmt.Println(ret[0]) } @@ -58,4 +75,4 @@ Currently any performance optimization hasn't been done to this runtime yet, and However _theoretically speaking_, this project have the potential to compete with these state-of-the-art JIT-style runtimes. The rationale for that is it is well-know that [CGO is slow](https://github.com/golang/go/issues/19574). More specifically, if you make large amount of CGO calls which cross the boundary between Go and C (stack) space, then the usage of CGO could be a bottleneck. -Since we can do JIT compilation purely in Go, this runtime could be the fastest one for some use cases where we have to make large amount of CGO calls (e.g. Proxy-Wasm host environment, or request-based plugin systems). +Since we can do JIT compilation purely in Go, this runtime could be the fastest one for some use cases where we have to make large amount of CGO calls (e.g. Proxy-Wasm host environment, or request-based plugin systems). \ No newline at end of file