From a9a2a3c711eb4563f520cf3e009c4e0823778927 Mon Sep 17 00:00:00 2001 From: Roman Khimov Date: Tue, 22 Oct 2019 15:16:29 +0300 Subject: [PATCH] vm: fix tests failing, follow-up to #426 Deal with input and transitional VM states properly. --- pkg/vm/vm.go | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/pkg/vm/vm.go b/pkg/vm/vm.go index 641386dc6..002378b08 100644 --- a/pkg/vm/vm.go +++ b/pkg/vm/vm.go @@ -258,6 +258,12 @@ func (v *VM) Run() error { return errors.New("no program loaded") } + if v.state.HasFlag(faultState) { + // VM already ran something and failed, in general its state is + // undefined in this case so we can't run anything. + return errors.New("VM has failed") + } + // haltState (the default) or breakState are safe to continue. v.state = noneState for { // check for breakpoint before executing the next instruction @@ -266,8 +272,13 @@ func (v *VM) Run() error { v.state |= breakState } switch { - case v.state.HasFlag(faultState), v.state.HasFlag(haltState), v.state.HasFlag(breakState): - return errors.New("VM stopped") + case v.state.HasFlag(faultState): + // Should be caught and reported already by the v.Step(), + // but we're checking here anyway just in case. + return errors.New("VM has failed") + case v.state.HasFlag(haltState), v.state.HasFlag(breakState): + // Normal exit from this loop. + return nil case v.state == noneState: if err := v.Step(); err != nil { return err