Skip to content

Commit

Permalink
Fix fibonacci example in Readme (#229)
Browse files Browse the repository at this point in the history
  • Loading branch information
r8d8 authored Feb 12, 2022
1 parent a40e0fa commit c837989
Showing 1 changed file with 22 additions and 5 deletions.
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).

0 comments on commit c837989

Please sign in to comment.