vm: support Map in REMOVE

This commit is contained in:
Evgenii Stratonikov 2019-09-24 16:16:24 +03:00
parent 8b6bddca3c
commit c0be2b2a99
2 changed files with 30 additions and 5 deletions

View file

@ -766,23 +766,31 @@ func (v *VM) execute(ctx *Context, op Instruction) {
} }
} }
case REMOVE: case REMOVE:
key := int(v.estack.Pop().BigInt().Int64()) key := v.estack.Pop()
validateMapKey(key)
elem := v.estack.Pop() elem := v.estack.Pop()
switch t := elem.value.(type) { switch t := elem.value.(type) {
case *ArrayItem: case *ArrayItem:
a := t.value a := t.value
if key < 0 || key >= len(a) { k := int(key.BigInt().Int64())
if k < 0 || k >= len(a) {
panic("REMOVE: invalid index") panic("REMOVE: invalid index")
} }
a = append(a[:key], a[key+1:]...) a = append(a[:k], a[k+1:]...)
t.value = a t.value = a
case *StructItem: case *StructItem:
a := t.value a := t.value
if key < 0 || key >= len(a) { k := int(key.BigInt().Int64())
if k < 0 || k >= len(a) {
panic("REMOVE: invalid index") panic("REMOVE: invalid index")
} }
a = append(a[:key], a[key+1:]...) a = append(a[:k], a[k+1:]...)
t.value = a t.value = a
case *MapItem:
m := t.value
k := toMapKey(key.value)
delete(m, k)
default: default:
panic("REMOVE: invalid type") panic("REMOVE: invalid type")
} }

View file

@ -1696,6 +1696,23 @@ func TestREMOVEGood(t *testing.T) {
assert.Equal(t, makeStackItem(1), vm.estack.Pop().value) assert.Equal(t, makeStackItem(1), vm.estack.Pop().value)
} }
func TestREMOVEMap(t *testing.T) {
prog := makeProgram(REMOVE, PUSH5, HASKEY)
vm := load(prog)
m := NewMapItem()
m.Add(makeStackItem(5), makeStackItem(3))
m.Add(makeStackItem([]byte{0, 1}), makeStackItem([]byte{2, 3}))
vm.estack.Push(&Element{value: m})
vm.estack.Push(&Element{value: m})
vm.estack.PushVal(makeStackItem(5))
vm.Run()
assert.Equal(t, false, vm.HasFailed())
assert.Equal(t, 1, vm.estack.Len())
assert.Equal(t, makeStackItem(false), vm.estack.Pop().value)
}
func TestCHECKSIGNoArgs(t *testing.T) { func TestCHECKSIGNoArgs(t *testing.T) {
prog := makeProgram(CHECKSIG) prog := makeProgram(CHECKSIG)
vm := load(prog) vm := load(prog)