vm: fix non-dupped items in PICKITEM

TestPICKITEMDupMap and TestPICKITEMDupArray were failing.
This commit is contained in:
Roman Khimov 2019-12-17 17:20:44 +03:00
parent a6d60e387a
commit 21efcb012b
2 changed files with 26 additions and 2 deletions

View file

@ -958,14 +958,14 @@ func (v *VM) execute(ctx *Context, op opcode.Opcode, parameter []byte) (err erro
if index < 0 || index >= len(arr) { if index < 0 || index >= len(arr) {
panic("PICKITEM: invalid index") panic("PICKITEM: invalid index")
} }
item := arr[index] item := arr[index].Dup()
v.estack.PushVal(item) v.estack.PushVal(item)
case *MapItem: case *MapItem:
if !t.Has(key.value) { if !t.Has(key.value) {
panic("invalid key") panic("invalid key")
} }
k := toMapKey(key.value) k := toMapKey(key.value)
v.estack.Push(&Element{value: t.value[k]}) v.estack.Push(&Element{value: t.value[k].Dup()})
default: default:
arr := obj.Bytes() arr := obj.Bytes()
if index < 0 || index >= len(arr) { if index < 0 || index >= len(arr) {

View file

@ -1092,6 +1092,30 @@ func TestPICKITEMByteArray(t *testing.T) {
assert.Equal(t, makeStackItem(2), vm.estack.Pop().value) assert.Equal(t, makeStackItem(2), vm.estack.Pop().value)
} }
func TestPICKITEMDupArray(t *testing.T) {
prog := makeProgram(opcode.DUP, opcode.PUSH0, opcode.PICKITEM, opcode.ABS)
vm := load(prog)
vm.estack.PushVal([]StackItem{makeStackItem(-1)})
runVM(t, vm)
assert.Equal(t, 2, vm.estack.Len())
assert.Equal(t, int64(1), vm.estack.Pop().BigInt().Int64())
items := vm.estack.Pop().Value().([]StackItem)
assert.Equal(t, big.NewInt(-1), items[0].Value())
}
func TestPICKITEMDupMap(t *testing.T) {
prog := makeProgram(opcode.DUP, opcode.PUSHBYTES1, 42, opcode.PICKITEM, opcode.ABS)
vm := load(prog)
m := NewMapItem()
m.Add(makeStackItem([]byte{42}), makeStackItem(-1))
vm.estack.Push(&Element{value: m})
runVM(t, vm)
assert.Equal(t, 2, vm.estack.Len())
assert.Equal(t, int64(1), vm.estack.Pop().BigInt().Int64())
items := vm.estack.Pop().Value().(map[interface{}]StackItem)
assert.Equal(t, big.NewInt(-1), items[string([]byte{42})].Value())
}
func TestPICKITEMMap(t *testing.T) { func TestPICKITEMMap(t *testing.T) {
prog := makeProgram(opcode.PICKITEM) prog := makeProgram(opcode.PICKITEM)
vm := load(prog) vm := load(prog)