vm: convert SIZE argument to bytearray

This commit is contained in:
Evgenii Stratonikov 2019-09-12 11:53:11 +03:00
parent 9780889239
commit 9eca5ee317
2 changed files with 28 additions and 4 deletions

View file

@ -759,10 +759,7 @@ func (v *VM) execute(ctx *Context, op Instruction) {
case SIZE: case SIZE:
elem := v.estack.Pop() elem := v.estack.Pop()
arr, ok := elem.value.Value().([]uint8) arr := elem.Bytes()
if !ok {
panic("SIZE: item not of type []uint8")
}
v.estack.PushVal(len(arr)) v.estack.PushVal(len(arr))
case JMP, JMPIF, JMPIFNOT: case JMP, JMPIF, JMPIFNOT:

View file

@ -549,6 +549,33 @@ func TestPICKITEMByteArray(t *testing.T) {
assert.Equal(t, makeStackItem(2), vm.estack.Pop().value) assert.Equal(t, makeStackItem(2), vm.estack.Pop().value)
} }
func TestSIZENoArgument(t *testing.T) {
prog := makeProgram(SIZE)
vm := load(prog)
vm.Run()
assert.Equal(t, true, vm.state.HasFlag(faultState))
}
func TestSIZEByteArray(t *testing.T) {
prog := makeProgram(SIZE)
vm := load(prog)
vm.estack.PushVal([]byte{0, 1})
vm.Run()
assert.Equal(t, false, vm.state.HasFlag(faultState))
assert.Equal(t, 1, vm.estack.Len())
assert.Equal(t, makeStackItem(2), vm.estack.Pop().value)
}
func TestSIZEBool(t *testing.T) {
prog := makeProgram(SIZE)
vm := load(prog)
vm.estack.PushVal(false)
vm.Run()
assert.Equal(t, false, vm.state.HasFlag(faultState))
assert.Equal(t, 1, vm.estack.Len())
assert.Equal(t, makeStackItem(1), vm.estack.Pop().value)
}
func TestSIGNNoArgument(t *testing.T) { func TestSIGNNoArgument(t *testing.T) {
prog := makeProgram(SIGN) prog := makeProgram(SIGN)
vm := load(prog) vm := load(prog)