@@ -24,7 +24,6 @@ import (
2424 "io"
2525 "io/ioutil"
2626 "math/rand"
27- "sync"
2827 "time"
2928
3029 "github.com/ethereum/go-ethereum/common"
@@ -44,7 +43,7 @@ type JSRE struct {
4443 output io.Writer
4544 evalQueue chan * evalReq
4645 stopEventLoop chan bool
47- loopWg sync. WaitGroup
46+ closed chan struct {}
4847}
4948
5049// jsTimer is a single timer instance with a callback function
@@ -66,10 +65,10 @@ func New(assetPath string, output io.Writer) *JSRE {
6665 re := & JSRE {
6766 assetPath : assetPath ,
6867 output : output ,
68+ closed : make (chan struct {}),
6969 evalQueue : make (chan * evalReq ),
7070 stopEventLoop : make (chan bool ),
7171 }
72- re .loopWg .Add (1 )
7372 go re .runEventLoop ()
7473 re .Set ("loadScript" , re .loadScript )
7574 re .Set ("inspect" , prettyPrintJS )
@@ -98,6 +97,8 @@ func randomSource() *rand.Rand {
9897// functions should be used if and only if running a routine that was already
9998// called from JS through an RPC call.
10099func (self * JSRE ) runEventLoop () {
100+ defer close (self .closed )
101+
101102 vm := otto .New ()
102103 r := randomSource ()
103104 vm .SetRandomSource (r .Float64 )
@@ -213,8 +214,6 @@ loop:
213214 timer .timer .Stop ()
214215 delete (registry , timer )
215216 }
216-
217- self .loopWg .Done ()
218217}
219218
220219// Do executes the given function on the JS event loop.
@@ -227,8 +226,11 @@ func (self *JSRE) Do(fn func(*otto.Otto)) {
227226
228227// stops the event loop before exit, optionally waits for all timers to expire
229228func (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+ }
232234}
233235
234236// Exec(file) loads and runs the contents of a file
0 commit comments