@@ -14,19 +14,22 @@ import (
1414// This is the _start entry point, when using -buildmode=default.
1515func wasmEntryCommand () {
1616 // These need to be initialized early so that the heap can be initialized.
17+ initializeCalled = true
1718 heapStart = uintptr (unsafe .Pointer (& heapStartSymbol ))
1819 heapEnd = uintptr (wasm_memory_size (0 ) * wasmPageSize )
19- wasmExportState = wasmExportStateInMain
2020 run ()
21- wasmExportState = wasmExportStateExited
22- beforeExit ()
21+ if mainExited {
22+ beforeExit ()
23+ }
2324}
2425
2526// This is the _initialize entry point, when using -buildmode=c-shared.
2627func wasmEntryReactor () {
2728 // This function is called before any //go:wasmexport functions are called
2829 // to initialize everything. It must not block.
2930
31+ initializeCalled = true
32+
3033 // Initialize the heap.
3134 heapStart = uintptr (unsafe .Pointer (& heapStartSymbol ))
3235 heapEnd = uintptr (wasm_memory_size (0 ) * wasmPageSize )
@@ -38,38 +41,23 @@ func wasmEntryReactor() {
3841 // goroutine.
3942 go func () {
4043 initAll ()
41- wasmExportState = wasmExportStateReactor
4244 }()
4345 scheduler (true )
44- if wasmExportState != wasmExportStateReactor {
45- // Unlikely, but if package initializers do something blocking (like
46- // time.Sleep()), that's a bug.
47- runtimePanic ("package initializer blocks" )
48- }
4946 } else {
5047 // There are no goroutines (except for the main one, if you can call it
5148 // that), so we can just run all the package initializers.
5249 initAll ()
53- wasmExportState = wasmExportStateReactor
5450 }
5551}
5652
57- // Track which state we're in: before (or during) init, running inside
58- // main.main, after main.main returned, or reactor mode (after init).
59- var wasmExportState uint8
60-
61- const (
62- wasmExportStateInit = iota
63- wasmExportStateInMain
64- wasmExportStateExited
65- wasmExportStateReactor
66- )
53+ // Whether the runtime was initialized by a call to _initialize or _start.
54+ var initializeCalled bool
6755
6856func wasmExportCheckRun () {
69- switch wasmExportState {
70- case wasmExportStateInit :
57+ switch {
58+ case ! initializeCalled :
7159 runtimePanic ("//go:wasmexport function called before runtime initialization" )
72- case wasmExportStateExited :
60+ case mainExited :
7361 runtimePanic ("//go:wasmexport function called after main.main returned" )
7462 }
7563}
0 commit comments