vm: support Map in ARRAYSIZE

Also make logic the same as in reference implementation
and add tests.
This commit is contained in:
Evgenii Stratonikov 2019-09-24 16:50:37 +03:00
parent c66f493017
commit 13d7770c68
2 changed files with 30 additions and 2 deletions

View file

@ -807,10 +807,10 @@ func (v *VM) execute(ctx *Context, op Instruction) {
switch t := elem.value.Value().(type) {
case []StackItem:
v.estack.PushVal(len(t))
case []uint8:
case map[interface{}]StackItem:
v.estack.PushVal(len(t))
default:
panic("ARRAYSIZE: item not of type []StackItem")
v.estack.PushVal(len(elem.Bytes()))
}
case SIZE:

View file

@ -727,6 +727,34 @@ func TestSIZEBool(t *testing.T) {
assert.Equal(t, makeStackItem(1), vm.estack.Pop().value)
}
func TestARRAYSIZEArray(t *testing.T) {
prog := makeProgram(ARRAYSIZE)
vm := load(prog)
vm.estack.PushVal([]StackItem{
makeStackItem(1),
makeStackItem([]byte{}),
})
vm.Run()
assert.Equal(t, false, vm.HasFailed())
assert.Equal(t, 1, vm.estack.Len())
assert.Equal(t, makeStackItem(2), vm.estack.Pop().value)
}
func TestARRAYSIZEMap(t *testing.T) {
prog := makeProgram(ARRAYSIZE)
vm := load(prog)
m := NewMapItem()
m.Add(makeStackItem(5), makeStackItem(6))
m.Add(makeStackItem([]byte{0, 1}), makeStackItem(6))
vm.estack.Push(&Element{value: m})
vm.Run()
assert.Equal(t, false, vm.HasFailed())
assert.Equal(t, 1, vm.estack.Len())
assert.Equal(t, makeStackItem(2), vm.estack.Pop().value)
}
func TestKEYSMap(t *testing.T) {
prog := makeProgram(KEYS)
vm := load(prog)