mirror of
https://github.com/nspcc-dev/neo-go.git
synced 2024-11-29 23:33:37 +00:00
vm: support Map in PICKITEM and SETITEM
This commit is contained in:
parent
d7bb50c609
commit
8b6bddca3c
2 changed files with 53 additions and 11 deletions
31
pkg/vm/vm.go
31
pkg/vm/vm.go
|
@ -705,11 +705,11 @@ func (v *VM) execute(ctx *Context, op Instruction) {
|
||||||
v.estack.PushVal(l)
|
v.estack.PushVal(l)
|
||||||
|
|
||||||
case PICKITEM:
|
case PICKITEM:
|
||||||
var (
|
key := v.estack.Pop()
|
||||||
key = v.estack.Pop()
|
validateMapKey(key)
|
||||||
obj = v.estack.Pop()
|
|
||||||
index = int(key.BigInt().Int64())
|
obj := v.estack.Pop()
|
||||||
)
|
index := int(key.BigInt().Int64())
|
||||||
|
|
||||||
switch t := obj.value.(type) {
|
switch t := obj.value.(type) {
|
||||||
// Struct and Array items have their underlying value as []StackItem.
|
// Struct and Array items have their underlying value as []StackItem.
|
||||||
|
@ -720,6 +720,12 @@ func (v *VM) execute(ctx *Context, op Instruction) {
|
||||||
}
|
}
|
||||||
item := arr[index]
|
item := arr[index]
|
||||||
v.estack.PushVal(item)
|
v.estack.PushVal(item)
|
||||||
|
case *MapItem:
|
||||||
|
if !t.Has(key.value) {
|
||||||
|
panic("invalid key")
|
||||||
|
}
|
||||||
|
k := toMapKey(key.value)
|
||||||
|
v.estack.Push(&Element{value: t.value[k]})
|
||||||
default:
|
default:
|
||||||
arr := obj.Bytes()
|
arr := obj.Bytes()
|
||||||
if index < 0 || index >= len(arr) {
|
if index < 0 || index >= len(arr) {
|
||||||
|
@ -730,21 +736,24 @@ func (v *VM) execute(ctx *Context, op Instruction) {
|
||||||
}
|
}
|
||||||
|
|
||||||
case SETITEM:
|
case SETITEM:
|
||||||
var (
|
item := v.estack.Pop().value
|
||||||
item = v.estack.Pop().value
|
key := v.estack.Pop()
|
||||||
key = v.estack.Pop()
|
validateMapKey(key)
|
||||||
obj = v.estack.Pop()
|
|
||||||
index = int(key.BigInt().Int64())
|
obj := v.estack.Pop()
|
||||||
)
|
|
||||||
|
|
||||||
switch t := obj.value.(type) {
|
switch t := obj.value.(type) {
|
||||||
// Struct and Array items have their underlying value as []StackItem.
|
// Struct and Array items have their underlying value as []StackItem.
|
||||||
case *ArrayItem, *StructItem:
|
case *ArrayItem, *StructItem:
|
||||||
arr := t.Value().([]StackItem)
|
arr := t.Value().([]StackItem)
|
||||||
|
index := int(key.BigInt().Int64())
|
||||||
if index < 0 || index >= len(arr) {
|
if index < 0 || index >= len(arr) {
|
||||||
panic("SETITEM: invalid index")
|
panic("SETITEM: invalid index")
|
||||||
}
|
}
|
||||||
arr[index] = item
|
arr[index] = item
|
||||||
|
case *MapItem:
|
||||||
|
t.Add(key.value, item)
|
||||||
|
|
||||||
default:
|
default:
|
||||||
panic(fmt.Sprintf("SETITEM: invalid item type %s", t))
|
panic(fmt.Sprintf("SETITEM: invalid item type %s", t))
|
||||||
}
|
}
|
||||||
|
|
|
@ -646,6 +646,39 @@ func TestPICKITEMByteArray(t *testing.T) {
|
||||||
assert.Equal(t, makeStackItem(2), vm.estack.Pop().value)
|
assert.Equal(t, makeStackItem(2), vm.estack.Pop().value)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestPICKITEMMap(t *testing.T) {
|
||||||
|
prog := makeProgram(PICKITEM)
|
||||||
|
vm := load(prog)
|
||||||
|
|
||||||
|
m := NewMapItem()
|
||||||
|
m.Add(makeStackItem(5), makeStackItem(3))
|
||||||
|
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(3), vm.estack.Pop().value)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestSETITEMMap(t *testing.T) {
|
||||||
|
prog := makeProgram(SETITEM, PICKITEM)
|
||||||
|
vm := load(prog)
|
||||||
|
|
||||||
|
m := NewMapItem()
|
||||||
|
m.Add(makeStackItem(5), makeStackItem(3))
|
||||||
|
vm.estack.Push(&Element{value: m})
|
||||||
|
vm.estack.PushVal(5)
|
||||||
|
vm.estack.Push(&Element{value: m})
|
||||||
|
vm.estack.PushVal(5)
|
||||||
|
vm.estack.PushVal([]byte{0, 1})
|
||||||
|
|
||||||
|
vm.Run()
|
||||||
|
assert.Equal(t, false, vm.HasFailed())
|
||||||
|
assert.Equal(t, 1, vm.estack.Len())
|
||||||
|
assert.Equal(t, makeStackItem([]byte{0, 1}), vm.estack.Pop().value)
|
||||||
|
}
|
||||||
|
|
||||||
func TestSIZENoArgument(t *testing.T) {
|
func TestSIZENoArgument(t *testing.T) {
|
||||||
prog := makeProgram(SIZE)
|
prog := makeProgram(SIZE)
|
||||||
vm := load(prog)
|
vm := load(prog)
|
||||||
|
|
Loading…
Reference in a new issue