Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix fibonacci example in Readme #229

Merged
merged 1 commit into from
Feb 12, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 22 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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])
}
Expand Down Expand Up @@ -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).