vm: make NEWARRAY/NEWSTRUCT accept bytearray

This commit is contained in:
Evgenii Stratonikov 2019-09-12 11:48:39 +03:00
parent 09e197eaf3
commit 9780889239
2 changed files with 16 additions and 16 deletions

View file

@ -609,31 +609,27 @@ func (v *VM) execute(ctx *Context, op Instruction) {
case NEWARRAY:
item := v.estack.Pop()
switch t := item.value.(type) {
case *BigIntegerItem:
n := t.value.Int64()
items := makeArrayOfFalses(int(n))
v.estack.PushVal(&ArrayItem{items})
case *StructItem:
v.estack.PushVal(&ArrayItem{t.value})
case *ArrayItem:
v.estack.PushVal(t)
default:
panic("NEWARRAY: invalid operand")
n := item.BigInt()
items := makeArrayOfFalses(int(n.Int64()))
v.estack.PushVal(&ArrayItem{items})
}
case NEWSTRUCT:
item := v.estack.Pop()
switch t := item.value.(type) {
case *BigIntegerItem:
n := t.value.Int64()
items := makeArrayOfFalses(int(n))
v.estack.PushVal(&StructItem{items})
case *ArrayItem:
v.estack.PushVal(&StructItem{t.value})
case *StructItem:
v.estack.PushVal(t)
default:
panic("NEWSTRUCT: invalid operand")
n := item.BigInt()
items := makeArrayOfFalses(int(n.Int64()))
v.estack.PushVal(&StructItem{items})
}
case APPEND:

View file

@ -422,12 +422,14 @@ func TestNEWARRAYArray(t *testing.T) {
assert.Equal(t, &ArrayItem{arr}, vm.estack.Pop().value)
}
func TestNEWARRAYWrongType(t *testing.T) {
func TestNEWARRAYByteArray(t *testing.T) {
prog := makeProgram(NEWARRAY)
vm := load(prog)
vm.estack.Push(NewElement([]byte{}))
vm.estack.PushVal([]byte{})
vm.Run()
assert.Equal(t, true, vm.state.HasFlag(faultState))
assert.Equal(t, false, vm.state.HasFlag(faultState))
assert.Equal(t, 1, vm.estack.Len())
assert.Equal(t, &ArrayItem{[]StackItem{}}, vm.estack.Pop().value)
}
func TestNEWSTRUCTInteger(t *testing.T) {
@ -462,12 +464,14 @@ func TestNEWSTRUCTStruct(t *testing.T) {
assert.Equal(t, &StructItem{arr}, vm.estack.Pop().value)
}
func TestNEWSTRUCTWrongType(t *testing.T) {
func TestNEWSTRUCTByteArray(t *testing.T) {
prog := makeProgram(NEWSTRUCT)
vm := load(prog)
vm.estack.Push(NewElement([]byte{}))
vm.estack.PushVal([]byte{})
vm.Run()
assert.Equal(t, true, vm.state.HasFlag(faultState))
assert.Equal(t, false, vm.state.HasFlag(faultState))
assert.Equal(t, 1, vm.estack.Len())
assert.Equal(t, &StructItem{[]StackItem{}}, vm.estack.Pop().value)
}
func TestAPPENDArray(t *testing.T) {