Redirects users to tested example (#791)
Rather than clutter both the README and home page with details that can drift and have caused maintenance, this directs users to the tested basic example. This also adds a FAQ entry about TinyGo's main, thanks to @basvanbeek for the help. Signed-off-by: Adrian Cole <adrian@tetrate.io>
This commit is contained in:
@@ -1,48 +1,62 @@
|
||||
package wazero
|
||||
package wazero_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
_ "embed"
|
||||
"fmt"
|
||||
"log"
|
||||
|
||||
"github.com/tetratelabs/wazero"
|
||||
"github.com/tetratelabs/wazero/imports/wasi_snapshot_preview1"
|
||||
)
|
||||
|
||||
// addWasm was generated by the following:
|
||||
//
|
||||
// cd examples/basic/testdata/testdata; wat2wasm --debug-names add.wat
|
||||
// cd examples/basic/testdata; tinygo build -o add.wasm -target=wasi add.go
|
||||
//
|
||||
//go:embed examples/basic/testdata/add.wasm
|
||||
var addWasm []byte
|
||||
|
||||
// This is an example of how to use WebAssembly via adding two numbers.
|
||||
// This is an example of how to extend a Go application with an addition
|
||||
// function defined in WebAssembly.
|
||||
//
|
||||
// See https://github.com/tetratelabs/wazero/tree/main/examples for more.
|
||||
// Since addWasm was compiled with TinyGo's `wasi` target, we need to configure
|
||||
// WASI host imports.
|
||||
//
|
||||
// A complete project that does the same as this is available here:
|
||||
// https://github.com/tetratelabs/wazero/tree/main/examples/basic
|
||||
func Example() {
|
||||
// Choose the context to use for function calls.
|
||||
ctx := context.Background()
|
||||
|
||||
// Create a new WebAssembly Runtime.
|
||||
r := NewRuntime(ctx)
|
||||
r := wazero.NewRuntimeWithConfig(ctx, wazero.NewRuntimeConfig().
|
||||
// WebAssembly 2.0 allows use of any version of TinyGo, including 0.24+.
|
||||
WithWasmCore2())
|
||||
defer r.Close(ctx) // This closes everything this Runtime created.
|
||||
|
||||
// Add a module to the runtime named "wasm/math" which exports one function
|
||||
// "add", implemented in WebAssembly.
|
||||
// Instantiate WASI, which implements host functions needed for TinyGo to
|
||||
// implement `panic`.
|
||||
if _, err := wasi_snapshot_preview1.Instantiate(ctx, r); err != nil {
|
||||
log.Panicln(err)
|
||||
}
|
||||
|
||||
// Instantiate the guest Wasm into the same runtime. It exports the `add`
|
||||
// function, implemented in WebAssembly.
|
||||
mod, err := r.InstantiateModuleFromBinary(ctx, addWasm)
|
||||
if err != nil {
|
||||
log.Panicln(err)
|
||||
}
|
||||
defer mod.Close(ctx)
|
||||
|
||||
// Get a function that can be reused until its module is closed:
|
||||
add := mod.ExportedFunction("add")
|
||||
|
||||
// Call the `add` function and print the results to the console.
|
||||
x, y := uint64(1), uint64(2)
|
||||
results, err := add.Call(ctx, x, y)
|
||||
results, err := mod.ExportedFunction("add").Call(ctx, x, y)
|
||||
if err != nil {
|
||||
log.Panicln(err)
|
||||
}
|
||||
|
||||
fmt.Printf("%s: %d + %d = %d\n", mod.Name(), x, y, results[0])
|
||||
fmt.Printf("%d + %d = %d\n", x, y, results[0])
|
||||
|
||||
// Output:
|
||||
// wasm/math: 1 + 2 = 3
|
||||
// 1 + 2 = 3
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user