Files
wazero/examples/assemblyscript/assemblyscript.go
Crypt Keeper 57a705e594 Disallows nil context and fixes linters (#754)
staticcheck linters broke until recent golangci-lint. Now, normal
behaviour of enforcing no nil context works again. Ex.
```
assemblyscript/assemblyscript_example_test.go:16:25: SA1012: do not pass a nil Context, even if a function permits it; pass context.TODO if you are unsure about which Context to use (staticcheck)
	r := wazero.NewRuntime(nil)
```

Since default lint already checks for nil context, this removes our
permission of nil context args. The original reason we permitted nil is
no longer valid: we once allowed context to be stashed in config, and
removed that as it caused bugs. We forgot to undo allowing nil
explicitly.

Note: this doesn't particularly check in our code for nil context,
similar as we don't particularly check in our code for nil anything
else. End users should use linters as none of our parameters should be
nil anyway.

Signed-off-by: Adrian Cole <adrian@tetrate.io>
2022-08-19 14:52:50 +08:00

83 lines
2.3 KiB
Go

package main
import (
"context"
_ "embed"
"fmt"
"log"
"os"
"strconv"
"github.com/tetratelabs/wazero"
"github.com/tetratelabs/wazero/assemblyscript"
)
// asWasm compiled using `npm install && npm run build`
//
//go:embed testdata/assemblyscript.wasm
var asWasm []byte
// main shows how to interact with a WebAssembly function that was compiled
// from AssemblyScript
//
// See README.md for a full description.
func main() {
// Choose the context to use for function calls.
ctx := context.Background()
// Create a new WebAssembly Runtime.
// Use WebAssembly 2.0 because AssemblyScript uses some >1.0 features.
r := wazero.NewRuntimeWithConfig(ctx, wazero.NewRuntimeConfig().
WithWasmCore2())
defer r.Close(ctx) // This closes everything this Runtime created.
// Instantiate a module implementing functions used by AssemblyScript.
// Thrown errors will be logged to os.Stderr
_, err := assemblyscript.Instantiate(ctx, r)
if err != nil {
log.Panicln(err)
}
// Compile the WebAssembly module using the default configuration.
code, err := r.CompileModule(ctx, asWasm, wazero.NewCompileConfig())
if err != nil {
log.Panicln(err)
}
// Instantiate a WebAssembly module that imports the "abort" and "trace"
// functions defined by assemblyscript.Instantiate and exports functions
// we'll use in this example.
mod, err := r.InstantiateModule(ctx, code, wazero.NewModuleConfig().
// Override the default module config that discards stdout and stderr.
WithStdout(os.Stdout).WithStderr(os.Stderr))
if err != nil {
log.Panicln(err)
}
// Get references to WebAssembly functions we'll use in this example.
helloWorld := mod.ExportedFunction("hello_world")
goodbyeWorld := mod.ExportedFunction("goodbye_world")
// Let's use the argument to this main function in Wasm.
numStr := os.Args[1]
num, err := strconv.Atoi(numStr)
if err != nil {
log.Panicln(err)
}
// Call hello_world, which returns the input value incremented by 3.
// While this calls trace(), our configuration didn't enable it.
results, err := helloWorld.Call(ctx, uint64(num))
if err != nil {
log.Panicln(err)
}
fmt.Printf("hello_world returned: %v", results[0])
// Call goodbye_world, which aborts with an error.
// assemblyscript.Instantiate was configured above to abort to stderr.
if _, err = goodbyeWorld.Call(ctx); err == nil {
log.Panicln("goodbye_world did not fail")
}
}