diff --git a/runtime/vm.go b/runtime/vm.go index e89188d..ee05eb2 100644 --- a/runtime/vm.go +++ b/runtime/vm.go @@ -77,7 +77,6 @@ func (v *VM) Run() (err error) { v.framesIndex = 1 v.ip = -1 v.allocs = v.maxAllocs + 1 - atomic.StoreInt64(&v.aborting, 0) v.run() @@ -95,11 +94,6 @@ func (v *VM) Run() (err error) { return err } - // check if stack still has some objects left - if v.sp > 0 && atomic.LoadInt64(&v.aborting) == 0 { - panic(fmt.Errorf("non empty stack after execution: %d", v.sp)) - } - return nil } @@ -121,7 +115,7 @@ func (v *VM) run() { } }() - for atomic.LoadInt64(&v.aborting) == 0 { + for atomic.CompareAndSwapInt64(&v.aborting, 0, 0) { v.ip++ switch v.curInsts[v.ip] { @@ -1008,9 +1002,9 @@ func (v *VM) run() { } } -// Globals returns the global variables. -func (v *VM) Globals() []objects.Object { - return v.globals +// IsStackEmpty tests if the stack is empty or not. +func (v *VM) IsStackEmpty() bool { + return v.sp == 0 } func indexAssign(dst, src objects.Object, selectors []objects.Object) error { diff --git a/runtime/vm_test.go b/runtime/vm_test.go index 152b9e7..eb63c9e 100644 --- a/runtime/vm_test.go +++ b/runtime/vm_test.go @@ -1,6 +1,7 @@ package runtime_test import ( + "errors" "fmt" "reflect" _runtime "runtime" @@ -250,8 +251,8 @@ func traceCompileRun(file *ast.File, symbols map[string]objects.Object, modules } trace = append(trace, fmt.Sprintf("\n[Globals]\n\n%s", strings.Join(formatGlobals(globals), "\n"))) } - if err != nil { - return + if err == nil && !v.IsStackEmpty() { + err = errors.New("non empty stack after execution") } return