@@ -14,19 +14,22 @@ import (
14
14
// This is the _start entry point, when using -buildmode=default.
15
15
func wasmEntryCommand () {
16
16
// These need to be initialized early so that the heap can be initialized.
17
+ initializeCalled = true
17
18
heapStart = uintptr (unsafe .Pointer (& heapStartSymbol ))
18
19
heapEnd = uintptr (wasm_memory_size (0 ) * wasmPageSize )
19
- wasmExportState = wasmExportStateInMain
20
20
run ()
21
- wasmExportState = wasmExportStateExited
22
- beforeExit ()
21
+ if mainExited {
22
+ beforeExit ()
23
+ }
23
24
}
24
25
25
26
// This is the _initialize entry point, when using -buildmode=c-shared.
26
27
func wasmEntryReactor () {
27
28
// This function is called before any //go:wasmexport functions are called
28
29
// to initialize everything. It must not block.
29
30
31
+ initializeCalled = true
32
+
30
33
// Initialize the heap.
31
34
heapStart = uintptr (unsafe .Pointer (& heapStartSymbol ))
32
35
heapEnd = uintptr (wasm_memory_size (0 ) * wasmPageSize )
@@ -38,38 +41,23 @@ func wasmEntryReactor() {
38
41
// goroutine.
39
42
go func () {
40
43
initAll ()
41
- wasmExportState = wasmExportStateReactor
42
44
}()
43
45
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
- }
49
46
} else {
50
47
// There are no goroutines (except for the main one, if you can call it
51
48
// that), so we can just run all the package initializers.
52
49
initAll ()
53
- wasmExportState = wasmExportStateReactor
54
50
}
55
51
}
56
52
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
67
55
68
56
func wasmExportCheckRun () {
69
- switch wasmExportState {
70
- case wasmExportStateInit :
57
+ switch {
58
+ case ! initializeCalled :
71
59
runtimePanic ("//go:wasmexport function called before runtime initialization" )
72
- case wasmExportStateExited :
60
+ case mainExited :
73
61
runtimePanic ("//go:wasmexport function called after main.main returned" )
74
62
}
75
63
}
0 commit comments