Skip to content

Commit d2100ec

Browse files
committed
Add OpInvalid
1 parent 38b27f3 commit d2100ec

File tree

4 files changed

+20
-1
lines changed

4 files changed

+20
-1
lines changed

vm/opcodes.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ package vm
33
type Opcode byte
44

55
const (
6-
OpPush Opcode = iota
6+
OpInvalid Opcode = iota
7+
OpPush
78
OpPushInt
89
OpPop
910
OpLoadConst

vm/program.go

+3
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,9 @@ func (program *Program) Disassemble() string {
7373
}
7474

7575
switch op {
76+
case OpInvalid:
77+
code("OpInvalid")
78+
7679
case OpPush:
7780
constant("OpPush")
7881

vm/vm.go

+3
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,9 @@ func (vm *VM) Run(program *Program, env interface{}) (_ interface{}, err error)
9393

9494
switch op {
9595

96+
case OpInvalid:
97+
panic("invalid opcode")
98+
9699
case OpPush:
97100
vm.push(program.Constants[arg])
98101

vm/vm_test.go

+12
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"github.com/antonmedv/expr/checker"
1212
"github.com/antonmedv/expr/compiler"
1313
"github.com/antonmedv/expr/conf"
14+
"github.com/antonmedv/expr/file"
1415
"github.com/antonmedv/expr/parser"
1516
"github.com/antonmedv/expr/vm"
1617
"github.com/stretchr/testify/require"
@@ -380,3 +381,14 @@ func TestRun_TaggedFieldName(t *testing.T) {
380381

381382
require.Equal(t, "hello world", out)
382383
}
384+
385+
func TestRun_OpInvalid(t *testing.T) {
386+
program := &vm.Program{
387+
Locations: []file.Location{{0, 0}},
388+
Bytecode: []vm.Opcode{vm.OpInvalid},
389+
Arguments: []int{0},
390+
}
391+
392+
_, err := vm.Run(program, nil)
393+
require.EqualError(t, err, "invalid opcode")
394+
}

0 commit comments

Comments
 (0)