stackitem: use Bool item directly

It is always copied.

Signed-off-by: Evgeniy Stratonikov <evgeniy@nspcc.ru>
This commit is contained in:
Evgeniy Stratonikov 2021-08-03 19:37:06 +03:00
parent 5aff82aef4
commit cff8b1c24e
6 changed files with 40 additions and 44 deletions

View file

@ -17,7 +17,7 @@ func ParameterFromStackItem(i stackitem.Item, seen map[stackitem.Item]bool) Para
Type: IntegerType,
Value: i.Value().(*big.Int).Int64(),
}
case *stackitem.Bool:
case stackitem.Bool:
return Parameter{
Type: BoolType,
Value: i.Value().(bool),

View file

@ -195,7 +195,7 @@ func compareItems(t *testing.T, a, b stackitem.Item) {
require.Equal(t, val, ac.Value().(*big.Int).Int64())
case *stackitem.ByteArray:
require.Equal(t, val, bigint.FromBytes(ac.Value().([]byte)).Int64())
case *stackitem.Bool:
case stackitem.Bool:
if ac.Value().(bool) {
require.Equal(t, val, int64(1))
} else {

View file

@ -126,9 +126,7 @@ func Make(v interface{}) Item {
value: []byte(val),
}
case bool:
return &Bool{
value: val,
}
return Bool(val)
case []Item:
return &Array{
value: val,
@ -485,76 +483,72 @@ func (i *BigInteger) MarshalJSON() ([]byte, error) {
}
// Bool represents a boolean Item.
type Bool struct {
value bool
}
type Bool bool
// NewBool returns an new Bool object.
func NewBool(val bool) *Bool {
return &Bool{
value: val,
}
func NewBool(val bool) Bool {
return Bool(val)
}
// Value implements Item interface.
func (i *Bool) Value() interface{} {
return i.value
func (i Bool) Value() interface{} {
return bool(i)
}
// MarshalJSON implements the json.Marshaler interface.
func (i *Bool) MarshalJSON() ([]byte, error) {
return json.Marshal(i.value)
func (i Bool) MarshalJSON() ([]byte, error) {
return json.Marshal(bool(i))
}
func (i *Bool) String() string {
func (i Bool) String() string {
return "Boolean"
}
// Dup implements Item interface.
func (i *Bool) Dup() Item {
return &Bool{i.value}
func (i Bool) Dup() Item {
return i
}
// TryBool implements Item interface.
func (i *Bool) TryBool() (bool, error) { return i.value, nil }
func (i Bool) TryBool() (bool, error) { return bool(i), nil }
// Bytes converts Bool to bytes.
func (i *Bool) Bytes() []byte {
if i.value {
func (i Bool) Bytes() []byte {
if i {
return []byte{1}
}
return []byte{0}
}
// TryBytes implements Item interface.
func (i *Bool) TryBytes() ([]byte, error) {
func (i Bool) TryBytes() ([]byte, error) {
return i.Bytes(), nil
}
// TryInteger implements Item interface.
func (i *Bool) TryInteger() (*big.Int, error) {
if i.value {
func (i Bool) TryInteger() (*big.Int, error) {
if i {
return big.NewInt(1), nil
}
return big.NewInt(0), nil
}
// Equals implements Item interface.
func (i *Bool) Equals(s Item) bool {
func (i Bool) Equals(s Item) bool {
if i == s {
return true
} else if s == nil {
return false
}
val, ok := s.(*Bool)
return ok && i.value == val.value
val, ok := s.(Bool)
return ok && i == val
}
// Type implements Item interface.
func (i *Bool) Type() Type { return BooleanT }
func (i Bool) Type() Type { return BooleanT }
// Convert implements Item interface.
func (i *Bool) Convert(typ Type) (Item, error) {
func (i Bool) Convert(typ Type) (Item, error) {
return convertPrimitive(i, typ)
}
@ -861,7 +855,7 @@ func (i *Map) Drop(index int) {
// key.
func IsValidMapKey(key Item) error {
switch key.(type) {
case *Bool, *BigInteger:
case Bool, *BigInteger:
return nil
case *ByteArray:
size := len(key.Value().([]byte))
@ -1171,8 +1165,8 @@ func deepCopy(item Item, seen map[Item]Item) Item {
return NewByteArray(slice.Copy(it.value))
case *Buffer:
return NewBuffer(slice.Copy(it.value))
case *Bool:
return NewBool(it.value)
case Bool:
return it
case *Pointer:
return NewPointerWithHash(it.pos, it.script, it.hash)
case *Interop:

View file

@ -63,11 +63,11 @@ var makeStackItemTestCases = []struct {
},
{
input: true,
result: &Bool{value: true},
result: Bool(true),
},
{
input: false,
result: &Bool{value: false},
result: Bool(false),
},
{
input: []Item{&BigInteger{value: big.NewInt(3)}, &ByteArray{value: []byte{1, 2, 3}}},
@ -459,8 +459,8 @@ func TestMarshalJSON(t *testing.T) {
switch testCase.input.(type) {
case *BigInteger:
actual, err = testCase.input.(*BigInteger).MarshalJSON()
case *Bool:
actual, err = testCase.input.(*Bool).MarshalJSON()
case Bool:
actual, err = testCase.input.(Bool).MarshalJSON()
case *ByteArray:
actual, err = testCase.input.(*ByteArray).MarshalJSON()
case *Array:
@ -532,7 +532,9 @@ func TestDeepCopy(t *testing.T) {
t.Run(tc.name, func(t *testing.T) {
actual := DeepCopy(tc.item)
require.Equal(t, tc.item, actual)
if tc.item.Type() != BooleanT {
require.False(t, actual == tc.item)
}
})
}

View file

@ -120,8 +120,8 @@ func toJSON(data []byte, seen map[Item]sliceNoPointer, item Item) ([]byte, error
return nil, err
}
data = append(data, raw...)
case *Bool:
if it.value {
case Bool:
if it {
data = append(data, "true"...)
} else {
data = append(data, "false"...)
@ -288,8 +288,8 @@ func toJSONWithTypes(item Item, seen map[Item]bool) (interface{}, error) {
}
value = arr
delete(seen, item)
case *Bool:
value = it.value
case Bool:
value = bool(it)
case *Buffer, *ByteArray:
value = base64.StdEncoding.EncodeToString(it.Value().([]byte))
case *BigInteger:

View file

@ -103,9 +103,9 @@ func (w *serContext) serialize(item Item) error {
data := t.Value().([]byte)
w.appendVarUint(uint64(len(data)))
w.data = append(w.data, data...)
case *Bool:
case Bool:
w.data = append(w.data, byte(BooleanT))
if t.Value().(bool) {
if t {
w.data = append(w.data, 1)
} else {
w.data = append(w.data, 0)