@@ -24,7 +24,6 @@ import (
24
24
"io"
25
25
"io/ioutil"
26
26
"math/rand"
27
- "sync"
28
27
"time"
29
28
30
29
"github.com/ethereum/go-ethereum/common"
@@ -44,7 +43,7 @@ type JSRE struct {
44
43
output io.Writer
45
44
evalQueue chan * evalReq
46
45
stopEventLoop chan bool
47
- loopWg sync. WaitGroup
46
+ closed chan struct {}
48
47
}
49
48
50
49
// jsTimer is a single timer instance with a callback function
@@ -66,10 +65,10 @@ func New(assetPath string, output io.Writer) *JSRE {
66
65
re := & JSRE {
67
66
assetPath : assetPath ,
68
67
output : output ,
68
+ closed : make (chan struct {}),
69
69
evalQueue : make (chan * evalReq ),
70
70
stopEventLoop : make (chan bool ),
71
71
}
72
- re .loopWg .Add (1 )
73
72
go re .runEventLoop ()
74
73
re .Set ("loadScript" , re .loadScript )
75
74
re .Set ("inspect" , prettyPrintJS )
@@ -98,6 +97,8 @@ func randomSource() *rand.Rand {
98
97
// functions should be used if and only if running a routine that was already
99
98
// called from JS through an RPC call.
100
99
func (self * JSRE ) runEventLoop () {
100
+ defer close (self .closed )
101
+
101
102
vm := otto .New ()
102
103
r := randomSource ()
103
104
vm .SetRandomSource (r .Float64 )
@@ -213,8 +214,6 @@ loop:
213
214
timer .timer .Stop ()
214
215
delete (registry , timer )
215
216
}
216
-
217
- self .loopWg .Done ()
218
217
}
219
218
220
219
// Do executes the given function on the JS event loop.
@@ -227,8 +226,11 @@ func (self *JSRE) Do(fn func(*otto.Otto)) {
227
226
228
227
// stops the event loop before exit, optionally waits for all timers to expire
229
228
func (self * JSRE ) Stop (waitForCallbacks bool ) {
230
- self .stopEventLoop <- waitForCallbacks
231
- self .loopWg .Wait ()
229
+ select {
230
+ case <- self .closed :
231
+ case self .stopEventLoop <- waitForCallbacks :
232
+ <- self .closed
233
+ }
232
234
}
233
235
234
236
// Exec(file) loads and runs the contents of a file
0 commit comments