diff --git a/pkg/compiler/codegen.go b/pkg/compiler/codegen.go index 20f558d71..79a9ef5b8 100644 --- a/pkg/compiler/codegen.go +++ b/pkg/compiler/codegen.go @@ -1632,7 +1632,7 @@ func (c *codegen) resolveFuncDecls(f *ast.File, pkg *types.Package) { func (c *codegen) writeJumps(b []byte) ([]byte, error) { ctx := vm.NewContext(b) var offsets []int - for op, _, err := ctx.Next(); err == nil && ctx.NextIP() < len(b); op, _, err = ctx.Next() { + for op, _, err := ctx.Next(); err == nil && ctx.IP() < len(b); op, _, err = ctx.Next() { switch op { case opcode.JMP, opcode.JMPIFNOT, opcode.JMPIF, opcode.CALL, opcode.JMPEQ, opcode.JMPNE, diff --git a/pkg/compiler/jumps_test.go b/pkg/compiler/jumps_test.go index cdfce463e..7f18a1c05 100644 --- a/pkg/compiler/jumps_test.go +++ b/pkg/compiler/jumps_test.go @@ -117,3 +117,13 @@ func TestWriteJumps(t *testing.T) { require.Equal(t, expProg, buf) require.Equal(t, expFuncs, c.funcs) } + +func TestWriteJumpsLastJump(t *testing.T) { + c := new(codegen) + c.l = []int{2} + prog := []byte{byte(opcode.JMP), 3, byte(opcode.RET), byte(opcode.JMPL), 0, 0, 0, 0} + expected := []byte{byte(opcode.JMP), 3, byte(opcode.RET), byte(opcode.JMP), 0xFF} + actual, err := c.writeJumps(prog) + require.NoError(t, err) + require.Equal(t, expected, actual) +}