vm: make (*Context).IP() return instruction pointer

It is misleading to return +1 in code, and user representation
can always be altered.
This commit is contained in:
Evgenii Stratonikov 2020-08-19 12:37:31 +03:00
parent 187a2d14b1
commit 681e81420a
3 changed files with 5 additions and 7 deletions

View file

@ -221,7 +221,7 @@ func TestBuiltinDoesNotCompile(t *testing.T) {
ctx := v.Context() ctx := v.Context()
retCount := 0 retCount := 0
for op, _, err := ctx.Next(); err == nil; op, _, err = ctx.Next() { for op, _, err := ctx.Next(); err == nil; op, _, err = ctx.Next() {
if ctx.IP() > len(ctx.Program()) { if ctx.IP() >= len(ctx.Program()) {
break break
} }
if op == opcode.RET { if op == opcode.RET {

View file

@ -433,8 +433,8 @@ func handleOps(c *ishell.Context) {
} }
func changePrompt(c ishell.Actions, v *vm.VM) { func changePrompt(c ishell.Actions, v *vm.VM) {
if v.Ready() && v.Context().IP()-1 >= 0 { if v.Ready() && v.Context().IP() >= 0 {
c.SetPrompt(fmt.Sprintf("NEO-GO-VM %d > ", v.Context().IP()-1)) c.SetPrompt(fmt.Sprintf("NEO-GO-VM %d > ", v.Context().IP()))
} else { } else {
c.SetPrompt("NEO-GO-VM > ") c.SetPrompt("NEO-GO-VM > ")
} }

View file

@ -141,11 +141,9 @@ func (c *Context) Next() (opcode.Opcode, []byte, error) {
return instr, parameter, nil return instr, parameter, nil
} }
// IP returns the absolute instruction without taking 0 into account. // IP returns current instruction offset in the context script.
// If that program starts the ip = 0 but IP() will return 1, cause its
// the first instruction.
func (c *Context) IP() int { func (c *Context) IP() int {
return c.ip + 1 return c.ip
} }
// LenInstr returns the number of instructions loaded. // LenInstr returns the number of instructions loaded.