vm: implement Array <-> Struct conversion

Reference VM implementation supports convertation from
Struct to Array via NEWARRAY and vice versa.
https://github.com/neo-project/neo-vm/pull/91
This commit is contained in:
Evgenii 2019-09-06 19:00:04 +03:00
parent 8fac66d4af
commit bcc8234155
2 changed files with 106 additions and 6 deletions

View file

@ -569,14 +569,34 @@ func (v *VM) execute(ctx *Context, op Instruction) {
// Object operations.
case NEWARRAY:
n := v.estack.Pop().BigInt().Int64()
items := make([]StackItem, n)
v.estack.PushVal(&ArrayItem{items})
item := v.estack.Pop()
switch t := item.value.(type) {
case *BigIntegerItem:
n := t.value.Int64()
items := make([]StackItem, 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")
}
case NEWSTRUCT:
n := v.estack.Pop().BigInt().Int64()
items := make([]StackItem, n)
v.estack.PushVal(&StructItem{items})
item := v.estack.Pop()
switch t := item.value.(type) {
case *BigIntegerItem:
n := t.value.Int64()
items := make([]StackItem, 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")
}
case APPEND:
itemElem := v.estack.Pop()

View file

@ -216,6 +216,86 @@ func TestINC(t *testing.T) {
assert.Equal(t, big.NewInt(2), vm.estack.Pop().BigInt())
}
func TestNEWARRAYInteger(t *testing.T) {
prog := makeProgram(NEWARRAY)
vm := load(prog)
vm.estack.PushVal(1)
vm.Run()
assert.Equal(t, false, vm.state.HasFlag(faultState))
assert.Equal(t, 1, vm.estack.Len())
assert.Equal(t, &ArrayItem{make([]StackItem, 1)}, vm.estack.Pop().value)
}
func TestNEWARRAYStruct(t *testing.T) {
prog := makeProgram(NEWARRAY)
vm := load(prog)
arr := []StackItem{makeStackItem(42)}
vm.estack.Push(&Element{value: &StructItem{arr}})
vm.Run()
assert.Equal(t, false, vm.state.HasFlag(faultState))
assert.Equal(t, 1, vm.estack.Len())
assert.Equal(t, &ArrayItem{arr}, vm.estack.Pop().value)
}
func TestNEWARRAYArray(t *testing.T) {
prog := makeProgram(NEWARRAY)
vm := load(prog)
arr := []StackItem{makeStackItem(42)}
vm.estack.Push(&Element{value: &ArrayItem{arr}})
vm.Run()
assert.Equal(t, false, vm.state.HasFlag(faultState))
assert.Equal(t, 1, vm.estack.Len())
assert.Equal(t, &ArrayItem{arr}, vm.estack.Pop().value)
}
func TestNEWARRAYWrongType(t *testing.T) {
prog := makeProgram(NEWARRAY)
vm := load(prog)
vm.estack.Push(NewElement([]byte{}))
vm.Run()
assert.Equal(t, true, vm.state.HasFlag(faultState))
}
func TestNEWSTRUCTInteger(t *testing.T) {
prog := makeProgram(NEWSTRUCT)
vm := load(prog)
vm.estack.PushVal(1)
vm.Run()
assert.Equal(t, false, vm.state.HasFlag(faultState))
assert.Equal(t, 1, vm.estack.Len())
assert.Equal(t, &StructItem{make([]StackItem, 1)}, vm.estack.Pop().value)
}
func TestNEWSTRUCTArray(t *testing.T) {
prog := makeProgram(NEWSTRUCT)
vm := load(prog)
arr := []StackItem{makeStackItem(42)}
vm.estack.Push(&Element{value: &ArrayItem{arr}})
vm.Run()
assert.Equal(t, false, vm.state.HasFlag(faultState))
assert.Equal(t, 1, vm.estack.Len())
assert.Equal(t, &StructItem{arr}, vm.estack.Pop().value)
}
func TestNEWSTRUCTStruct(t *testing.T) {
prog := makeProgram(NEWSTRUCT)
vm := load(prog)
arr := []StackItem{makeStackItem(42)}
vm.estack.Push(&Element{value: &StructItem{arr}})
vm.Run()
assert.Equal(t, false, vm.state.HasFlag(faultState))
assert.Equal(t, 1, vm.estack.Len())
assert.Equal(t, &StructItem{arr}, vm.estack.Pop().value)
}
func TestNEWSTRUCTWrongType(t *testing.T) {
prog := makeProgram(NEWSTRUCT)
vm := load(prog)
vm.estack.Push(NewElement([]byte{}))
vm.Run()
assert.Equal(t, true, vm.state.HasFlag(faultState))
}
func TestAppCall(t *testing.T) {
prog := []byte{byte(APPCALL)}
hash := util.Uint160{}