19
19
package main
20
20
21
21
import (
22
+ "bytes"
22
23
"context"
23
24
"fmt"
25
+ "io"
24
26
"os"
25
27
26
28
"github.com/tetratelabs/wazero"
@@ -50,6 +52,9 @@ func J(x int32) {
50
52
println ("J" , x , "end" )
51
53
}
52
54
55
+ var errbuf bytes.Buffer
56
+ var stderr = io .MultiWriter (os .Stderr , & errbuf )
57
+
53
58
func main () {
54
59
ctx := context .Background ()
55
60
@@ -71,7 +76,7 @@ func main() {
71
76
}
72
77
73
78
config := wazero .NewModuleConfig ().
74
- WithStdout (os .Stdout ).WithStderr (os . Stderr ).
79
+ WithStdout (os .Stdout ).WithStderr (stderr ).
75
80
WithStartFunctions () // don't call _start
76
81
77
82
wasi_snapshot_preview1 .MustInstantiate (ctx , r )
@@ -117,12 +122,32 @@ func main() {
117
122
}
118
123
119
124
// 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
+ }
121
132
fmt .Println ("Library mode: initialize" )
133
+ entry = m .ExportedFunction ("_initialize" )
122
134
_ , err = entry .Call (ctx )
123
135
if err != nil {
124
136
panic (err )
125
137
}
126
138
fmt .Println ("\n Library mode: call export functions" )
127
139
I ()
128
140
}
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