From 13d7770c6843cfaf42e3fc156043f875a6f8a3da Mon Sep 17 00:00:00 2001 From: Evgenii Stratonikov Date: Tue, 24 Sep 2019 16:50:37 +0300 Subject: [PATCH] vm: support Map in ARRAYSIZE Also make logic the same as in reference implementation and add tests. --- pkg/vm/vm.go | 4 ++-- pkg/vm/vm_test.go | 28 ++++++++++++++++++++++++++++ 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/pkg/vm/vm.go b/pkg/vm/vm.go index 17ac189b1..38e8b23b9 100644 --- a/pkg/vm/vm.go +++ b/pkg/vm/vm.go @@ -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: diff --git a/pkg/vm/vm_test.go b/pkg/vm/vm_test.go index 4b121d6e3..428a24735 100644 --- a/pkg/vm/vm_test.go +++ b/pkg/vm/vm_test.go @@ -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)