From f2393e5efc924213e6dedc388caa0c21a01eb8dc Mon Sep 17 00:00:00 2001 From: Evgenii Stratonikov Date: Tue, 10 Sep 2019 14:43:40 +0300 Subject: [PATCH] vm: implement * -> Boolean conversion --- pkg/vm/stack.go | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/pkg/vm/stack.go b/pkg/vm/stack.go index 09aecf22d..740aa835e 100644 --- a/pkg/vm/stack.go +++ b/pkg/vm/stack.go @@ -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.