Browse Source

Added ClearInterrupt(). Closes #120.

Dmitry Panov 5 years ago
parent
commit
0a0a0d8cb9
2 changed files with 16 additions and 0 deletions
  1. 12 0
      runtime.go
  2. 4 0
      vm.go

+ 12 - 0
runtime.go

@@ -915,10 +915,22 @@ func (r *Runtime) RunProgram(p *Program) (result Value, err error) {
 
 // Interrupt a running JavaScript. The corresponding Go call will return an *InterruptedError containing v.
 // Note, it only works while in JavaScript code, it does not interrupt native Go functions (which includes all built-ins).
+// If the runtime is currently not running, it will be immediately interrupted on the next Run*() call.
+// To avoid that use ClearInterrupt()
 func (r *Runtime) Interrupt(v interface{}) {
 	r.vm.Interrupt(v)
 }
 
+// ClearInterrupt resets the interrupt flag. Typically this needs to be called before the runtime
+// is made available for re-use if there is a chance it could have been interrupted with Interrupt().
+// Otherwise if Interrupt() was called when runtime was not running (e.g. if it had already finished)
+// so that Interrupt() didn't actually trigger, an attempt to use the runtime will immediately cause
+// an interruption. It is up to the user to ensure proper synchronisation so that ClearInterrupt() is
+// only called when the runtime has finished and there is no chance of a concurrent Interrupt() call.
+func (r *Runtime) ClearInterrupt() {
+	r.vm.ClearInterrupt()
+}
+
 /*
 ToValue converts a Go value into JavaScript value.
 

+ 4 - 0
vm.go

@@ -313,6 +313,10 @@ func (vm *vm) Interrupt(v interface{}) {
 	vm.interruptLock.Unlock()
 }
 
+func (vm *vm) ClearInterrupt() {
+	atomic.StoreUint32(&vm.interrupted, 0)
+}
+
 func (vm *vm) captureStack(stack []stackFrame, ctxOffset int) []stackFrame {
 	// Unroll the context stack
 	stack = append(stack, stackFrame{prg: vm.prg, pc: vm.pc, funcName: vm.funcName})