Skip to content

Commit 9398b97

Browse files
committed
cherry/wasmtest: test wasmexport function called before runtime initialization
This should give an explicit error. Change-Id: Ib0cff84366dd765c1384490a32890c5e23c13647 Reviewed-on: https://go-review.googlesource.com/c/scratch/+/643058 Reviewed-by: Cherry Mui <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]>
1 parent 279bf1a commit 9398b97

File tree

1 file changed

+27
-2
lines changed

1 file changed

+27
-2
lines changed

cherry/wasmtest/w.go

+27-2
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,10 @@
1919
package main
2020

2121
import (
22+
"bytes"
2223
"context"
2324
"fmt"
25+
"io"
2426
"os"
2527

2628
"github.com/tetratelabs/wazero"
@@ -50,6 +52,9 @@ func J(x int32) {
5052
println("J", x, "end")
5153
}
5254

55+
var errbuf bytes.Buffer
56+
var stderr = io.MultiWriter(os.Stderr, &errbuf)
57+
5358
func main() {
5459
ctx := context.Background()
5560

@@ -71,7 +76,7 @@ func main() {
7176
}
7277

7378
config := wazero.NewModuleConfig().
74-
WithStdout(os.Stdout).WithStderr(os.Stderr).
79+
WithStdout(os.Stdout).WithStderr(stderr).
7580
WithStartFunctions() // don't call _start
7681

7782
wasi_snapshot_preview1.MustInstantiate(ctx, r)
@@ -117,12 +122,32 @@ func main() {
117122
}
118123

119124
// Library mode.
120-
entry = m.ExportedFunction("_initialize")
125+
fmt.Println("Libaray mode: call export before initialization")
126+
shouldPanic(func() { I() })
127+
// reset module
128+
m, err = r.InstantiateWithConfig(ctx, buf, config)
129+
if err != nil {
130+
panic(err)
131+
}
121132
fmt.Println("Library mode: initialize")
133+
entry = m.ExportedFunction("_initialize")
122134
_, err = entry.Call(ctx)
123135
if err != nil {
124136
panic(err)
125137
}
126138
fmt.Println("\nLibrary mode: call export functions")
127139
I()
128140
}
141+
142+
func shouldPanic(f func()) {
143+
defer func() {
144+
e := recover()
145+
if e == nil {
146+
panic("did not panic")
147+
}
148+
if !bytes.Contains(errbuf.Bytes(), []byte("runtime: wasmexport function called before runtime initialization")) {
149+
panic("expected error message missing")
150+
}
151+
}()
152+
f()
153+
}

0 commit comments

Comments
 (0)