Makes all examples and docs use Runtime.Close (#537)

This removes tedium in our examples and docs by using `Runtime.Close`
instead of tracking everything. Internal tests still track too much, but
anyway at least this stops suggesting others should do it.

This also changes our examples to use log.PanicXX so that the line
number goes into the console output.

Signed-off-by: Adrian Cole <adrian@tetrate.io>
This commit is contained in:
Crypt Keeper
2022-05-10 12:08:25 +08:00
committed by GitHub
parent 8dd797e108
commit 03bfa31928
17 changed files with 127 additions and 109 deletions

View File

@@ -25,24 +25,23 @@ func main() {
// Create a new WebAssembly Runtime.
r := wazero.NewRuntime()
defer r.Close(ctx) // This closes everything this Runtime created.
// Instantiate a Go-defined module named "env" that exports a function to
// log to the console.
env, err := r.NewModuleBuilder("env").
_, err := r.NewModuleBuilder("env").
ExportFunction("log", logString).
Instantiate(ctx)
if err != nil {
log.Fatal(err)
log.Panicln(err)
}
defer env.Close(ctx)
// Instantiate a WebAssembly module that imports the "log" function defined
// in "env" and exports "memory" and functions we'll use in this example.
mod, err := r.InstantiateModuleFromCode(ctx, greetWasm)
if err != nil {
log.Fatal(err)
log.Panicln(err)
}
defer mod.Close(ctx)
// Get references to WebAssembly functions we'll use in this example.
greet := mod.ExportedFunction("greet")
@@ -59,7 +58,7 @@ func main() {
// function could be used to pass binary serialized data to Wasm.
results, err := allocate.Call(ctx, nameSize)
if err != nil {
log.Fatal(err)
log.Panicln(err)
}
namePtr := results[0]
// This pointer was allocated by Rust, but owned by Go, So, we have to
@@ -68,21 +67,21 @@ func main() {
// The pointer is a linear memory offset, which is where we write the name.
if !mod.Memory().Write(ctx, uint32(namePtr), []byte(name)) {
log.Fatalf("Memory.Write(%d, %d) out of range of memory size %d",
log.Panicf("Memory.Write(%d, %d) out of range of memory size %d",
namePtr, nameSize, mod.Memory().Size(ctx))
}
// Now, we can call "greet", which reads the string we wrote to memory!
_, err = greet.Call(ctx, namePtr, nameSize)
if err != nil {
log.Fatal(err)
log.Panicln(err)
}
// Finally, we get the greeting message "greet" printed. This shows how to
// read-back something allocated by Rust.
ptrSize, err := greeting.Call(ctx, namePtr, nameSize)
if err != nil {
log.Fatal(err)
log.Panicln(err)
}
greetingPtr := uint32(ptrSize[0] >> 32)
greetingSize := uint32(ptrSize[0])
@@ -92,7 +91,7 @@ func main() {
// The pointer is a linear memory offset, which is where we write the name.
if bytes, ok := mod.Memory().Read(ctx, greetingPtr, greetingSize); !ok {
log.Fatalf("Memory.Read(%d, %d) out of range of memory size %d",
log.Panicf("Memory.Read(%d, %d) out of range of memory size %d",
greetingPtr, greetingSize, mod.Memory().Size(ctx))
} else {
fmt.Println("go >>", string(bytes))
@@ -102,7 +101,7 @@ func main() {
func logString(ctx context.Context, m api.Module, offset, byteCount uint32) {
buf, ok := m.Memory().Read(ctx, offset, byteCount)
if !ok {
log.Fatalf("Memory.Read(%d, %d) out of range", offset, byteCount)
log.Panicf("Memory.Read(%d, %d) out of range", offset, byteCount)
}
fmt.Println(string(buf))
}

View File

@@ -26,32 +26,29 @@ func main() {
// Create a new WebAssembly Runtime.
r := wazero.NewRuntime()
defer r.Close(ctx) // This closes everything this Runtime created.
// Instantiate a Go-defined module named "env" that exports a function to
// log to the console.
env, err := r.NewModuleBuilder("env").
_, err := r.NewModuleBuilder("env").
ExportFunction("log", logString).
Instantiate(ctx)
if err != nil {
log.Fatal(err)
log.Panicln(err)
}
defer env.Close(ctx)
// Note: testdata/greet.go doesn't use WASI, but TinyGo needs it to
// implement functions such as panic.
wm, err := wasi.InstantiateSnapshotPreview1(ctx, r)
if err != nil {
log.Fatal(err)
if _, err = wasi.InstantiateSnapshotPreview1(ctx, r); err != nil {
log.Panicln(err)
}
defer wm.Close(ctx)
// Instantiate a WebAssembly module that imports the "log" function defined
// in "env" and exports "memory" and functions we'll use in this example.
mod, err := r.InstantiateModuleFromCode(ctx, greetWasm)
if err != nil {
log.Fatal(err)
log.Panicln(err)
}
defer mod.Close(ctx)
// Get references to WebAssembly functions we'll use in this example.
greet := mod.ExportedFunction("greet")
@@ -69,7 +66,7 @@ func main() {
// function could be used to pass binary serialized data to Wasm.
results, err := malloc.Call(ctx, nameSize)
if err != nil {
log.Fatal(err)
log.Panicln(err)
}
namePtr := results[0]
// This pointer is managed by TinyGo, but TinyGo is unaware of external usage.
@@ -78,28 +75,28 @@ func main() {
// The pointer is a linear memory offset, which is where we write the name.
if !mod.Memory().Write(ctx, uint32(namePtr), []byte(name)) {
log.Fatalf("Memory.Write(%d, %d) out of range of memory size %d",
log.Panicf("Memory.Write(%d, %d) out of range of memory size %d",
namePtr, nameSize, mod.Memory().Size(ctx))
}
// Now, we can call "greet", which reads the string we wrote to memory!
_, err = greet.Call(ctx, namePtr, nameSize)
if err != nil {
log.Fatal(err)
log.Panicln(err)
}
// Finally, we get the greeting message "greet" printed. This shows how to
// read-back something allocated by TinyGo.
ptrSize, err := greeting.Call(ctx, namePtr, nameSize)
if err != nil {
log.Fatal(err)
log.Panicln(err)
}
// Note: This pointer is still owned by TinyGo, so don't try to free it!
greetingPtr := uint32(ptrSize[0] >> 32)
greetingSize := uint32(ptrSize[0])
// The pointer is a linear memory offset, which is where we write the name.
if bytes, ok := mod.Memory().Read(ctx, greetingPtr, greetingSize); !ok {
log.Fatalf("Memory.Read(%d, %d) out of range of memory size %d",
log.Panicf("Memory.Read(%d, %d) out of range of memory size %d",
greetingPtr, greetingSize, mod.Memory().Size(ctx))
} else {
fmt.Println("go >>", string(bytes))
@@ -109,7 +106,7 @@ func main() {
func logString(ctx context.Context, m api.Module, offset, byteCount uint32) {
buf, ok := m.Memory().Read(ctx, offset, byteCount)
if !ok {
log.Fatalf("Memory.Read(%d, %d) out of range", offset, byteCount)
log.Panicf("Memory.Read(%d, %d) out of range", offset, byteCount)
}
fmt.Println(string(buf))
}