vm: implement * -> Boolean conversion

This commit is contained in:
Evgenii Stratonikov 2019-09-10 14:43:40 +03:00
parent 7e14c5a274
commit f2393e5efc

View file

@ -77,10 +77,23 @@ func (e *Element) BigInt() *big.Int {
// Bool attempts to get the underlying value of the element as a boolean.
// Will panic if the assertion failed which will be caught by the VM.
func (e *Element) Bool() bool {
if v, ok := e.value.Value().(*big.Int); ok {
return v.Int64() == 1
switch t := e.value.(type) {
case *BigIntegerItem:
return t.value.Int64() != 0
case *BoolItem:
return t.value
case *ArrayItem, *StructItem:
return true
case *ByteArrayItem:
for _, b := range t.value {
if b != 0 {
return true
}
}
return false
default:
panic("can't convert to bool: " + t.String())
}
return e.value.Value().(bool)
}
// Bytes attempts to get the underlying value of the element as a byte array.