Skip to content

Commit

Permalink
Add clearAbrupt and documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
mstoykov committed Jul 14, 2022
1 parent b8c622d commit f2d83ab
Showing 1 changed file with 11 additions and 5 deletions.
16 changes: 11 additions & 5 deletions runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -1368,8 +1368,7 @@ func (r *Runtime) RunProgram(p *Program) (result Value, err error) {
if ex, ok := x.(*uncatchableException); ok {
err = ex.err
if len(r.vm.callStack) == 0 {
r.vm.ClearInterrupt()
r.jobQueue = nil
r.leaveAbrupt()
}
} else {
panic(x)
Expand Down Expand Up @@ -1427,6 +1426,8 @@ func (r *Runtime) CaptureCallStack(depth int, stack []StackFrame) []StackFrame {
}

// Interrupt a running JavaScript. The corresponding Go call will return an *InterruptedError containing v.
// If the interrupt propagates until the stack is empty the currentlyt queued promise resolve/reject jobs will be cleared
// without being executed. This is the same time they would be executed otherwise.
// 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()
Expand Down Expand Up @@ -2334,8 +2335,7 @@ func AssertFunction(v Value) (Callable, bool) {
if ex, ok := x.(*uncatchableException); ok {
err = ex.err
if len(obj.runtime.vm.callStack) == 0 {
obj.runtime.vm.ClearInterrupt()
obj.runtime.jobQueue = nil
obj.runtime.leaveAbrupt()
}
} else {
panic(x)
Expand Down Expand Up @@ -2621,7 +2621,7 @@ func (r *Runtime) getHash() *maphash.Hash {
return r.hash
}

// called when the top level function returns (i.e. control is passed outside the Runtime).
// called when the top level function returns normally (i.e. control is passed outside the Runtime).
func (r *Runtime) leave() {
for {
jobs := r.jobQueue
Expand All @@ -2635,6 +2635,12 @@ func (r *Runtime) leave() {
}
}

// called when the top level function returns (i.e. control is passed outside the Runtime) but it was due to an interrupt
func (r *Runtime) leaveAbrupt() {
r.jobQueue = nil
r.ClearInterrupt()
}

func nilSafe(v Value) Value {
if v != nil {
return v
Expand Down

0 comments on commit f2d83ab

Please sign in to comment.