Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make the number interfaces in the value-package generic #3790

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 11 additions & 11 deletions interpreter/value_int.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ func (v IntValue) MeteredString(interpreter *Interpreter, _ SeenReferences, _ Lo

func (v IntValue) Negate(context NumberValueArithmeticContext, _ LocationRange) NumberValue {
return IntValue{
IntValue: v.IntValue.Negate(context).(values.IntValue),
IntValue: v.IntValue.Negate(context),
}
}

Expand All @@ -186,7 +186,7 @@ func (v IntValue) Plus(context NumberValueArithmeticContext, other NumberValue,
if err != nil {
panic(err)
}
return IntValue{IntValue: result.(values.IntValue)}
return IntValue{IntValue: result}
}

func (v IntValue) SaturatingPlus(context NumberValueArithmeticContext, other NumberValue, locationRange LocationRange) NumberValue {
Expand Down Expand Up @@ -221,7 +221,7 @@ func (v IntValue) Minus(context NumberValueArithmeticContext, other NumberValue,
panic(err)
}
return IntValue{
IntValue: result.(values.IntValue),
IntValue: result,
}
}

Expand Down Expand Up @@ -258,7 +258,7 @@ func (v IntValue) Mod(context NumberValueArithmeticContext, other NumberValue, l
}

return IntValue{
IntValue: result.(values.IntValue),
IntValue: result,
}
}

Expand All @@ -278,7 +278,7 @@ func (v IntValue) Mul(context NumberValueArithmeticContext, other NumberValue, l
panic(err)
}
return IntValue{
IntValue: result.(values.IntValue),
IntValue: result,
}
}

Expand Down Expand Up @@ -315,7 +315,7 @@ func (v IntValue) Div(context NumberValueArithmeticContext, other NumberValue, l
}

return IntValue{
IntValue: result.(values.IntValue),
IntValue: result,
}
}

Expand Down Expand Up @@ -456,7 +456,7 @@ func (v IntValue) BitwiseOr(context ValueStaticTypeContext, other IntegerValue,
}

return IntValue{
IntValue: result.(values.IntValue),
IntValue: result,
}
}

Expand All @@ -477,7 +477,7 @@ func (v IntValue) BitwiseXor(context ValueStaticTypeContext, other IntegerValue,
}

return IntValue{
IntValue: result.(values.IntValue),
IntValue: result,
}
}

Expand All @@ -498,7 +498,7 @@ func (v IntValue) BitwiseAnd(context ValueStaticTypeContext, other IntegerValue,
}

return IntValue{
IntValue: result.(values.IntValue),
IntValue: result,
}
}

Expand All @@ -519,7 +519,7 @@ func (v IntValue) BitwiseLeftShift(context ValueStaticTypeContext, other Integer
}

return IntValue{
IntValue: result.(values.IntValue),
IntValue: result,
}
}

Expand All @@ -540,7 +540,7 @@ func (v IntValue) BitwiseRightShift(context ValueStaticTypeContext, other Intege
}

return IntValue{
IntValue: result.(values.IntValue),
IntValue: result,
}
}

Expand Down
148 changes: 49 additions & 99 deletions values/value_int.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,8 @@ func NewUnmeteredIntValueFromBigInt(value *big.Int) IntValue {
var _ Value = IntValue{}
var _ EquatableValue = IntValue{}
var _ ComparableValue = IntValue{}
var _ NumberValue = IntValue{}
var _ IntegerValue = IntValue{}
var _ NumberValue[IntValue] = IntValue{}
var _ IntegerValue[IntValue] = IntValue{}
var _ atree.Storable = IntValue{}
var _ atree.Value = IntValue{}

Expand All @@ -80,7 +80,7 @@ func (v IntValue) String() string {
return format.BigInt(v.BigInt)
}

func (v IntValue) Negate(gauge common.MemoryGauge) NumberValue {
func (v IntValue) Negate(gauge common.MemoryGauge) IntValue {
return NewIntValueFromBigInt(
gauge,
common.NewNegateBigIntMemoryUsage(v.BigInt),
Expand All @@ -90,109 +90,84 @@ func (v IntValue) Negate(gauge common.MemoryGauge) NumberValue {
)
}

func (v IntValue) Plus(gauge common.MemoryGauge, other NumberValue) (NumberValue, error) {
o, ok := other.(IntValue)
if !ok {
return nil, InvalidOperandsError{}
}

func (v IntValue) Plus(gauge common.MemoryGauge, other IntValue) (IntValue, error) {
return NewIntValueFromBigInt(
gauge,
common.NewPlusBigIntMemoryUsage(v.BigInt, o.BigInt),
common.NewPlusBigIntMemoryUsage(v.BigInt, other.BigInt),
func() *big.Int {
res := new(big.Int)
return res.Add(v.BigInt, o.BigInt)
return res.Add(v.BigInt, other.BigInt)
},
), nil
}

func (v IntValue) SaturatingPlus(gauge common.MemoryGauge, other NumberValue) (NumberValue, error) {
func (v IntValue) SaturatingPlus(gauge common.MemoryGauge, other IntValue) (IntValue, error) {
return v.Plus(gauge, other)
}

func (v IntValue) Minus(gauge common.MemoryGauge, other NumberValue) (NumberValue, error) {
o, ok := other.(IntValue)
if !ok {
return nil, InvalidOperandsError{}
}

func (v IntValue) Minus(gauge common.MemoryGauge, other IntValue) (IntValue, error) {
return NewIntValueFromBigInt(
gauge,
common.NewMinusBigIntMemoryUsage(v.BigInt, o.BigInt),
common.NewMinusBigIntMemoryUsage(v.BigInt, other.BigInt),
func() *big.Int {
res := new(big.Int)
return res.Sub(v.BigInt, o.BigInt)
return res.Sub(v.BigInt, other.BigInt)
},
), nil
}

func (v IntValue) SaturatingMinus(gauge common.MemoryGauge, other NumberValue) (NumberValue, error) {
func (v IntValue) SaturatingMinus(gauge common.MemoryGauge, other IntValue) (IntValue, error) {
return v.Minus(gauge, other)
}

func (v IntValue) Mod(gauge common.MemoryGauge, other NumberValue) (NumberValue, error) {
o, ok := other.(IntValue)
if !ok {
return nil, InvalidOperandsError{}
}

func (v IntValue) Mod(gauge common.MemoryGauge, other IntValue) (IntValue, error) {
// INT33-C
if o.BigInt.Sign() == 0 {
return nil, DivisionByZeroError{}
if other.BigInt.Sign() == 0 {
return IntValue{}, DivisionByZeroError{}
}

return NewIntValueFromBigInt(
gauge,
common.NewModBigIntMemoryUsage(v.BigInt, o.BigInt),
common.NewModBigIntMemoryUsage(v.BigInt, other.BigInt),
func() *big.Int {
res := new(big.Int)
return res.Rem(v.BigInt, o.BigInt)
return res.Rem(v.BigInt, other.BigInt)
},
), nil
}

func (v IntValue) Mul(gauge common.MemoryGauge, other NumberValue) (NumberValue, error) {
o, ok := other.(IntValue)
if !ok {
return nil, InvalidOperandsError{}
}

func (v IntValue) Mul(gauge common.MemoryGauge, other IntValue) (IntValue, error) {
return NewIntValueFromBigInt(
gauge,
common.NewMulBigIntMemoryUsage(v.BigInt, o.BigInt),
common.NewMulBigIntMemoryUsage(v.BigInt, other.BigInt),
func() *big.Int {
res := new(big.Int)
return res.Mul(v.BigInt, o.BigInt)
return res.Mul(v.BigInt, other.BigInt)
},
), nil
}

func (v IntValue) SaturatingMul(gauge common.MemoryGauge, other NumberValue) (NumberValue, error) {
func (v IntValue) SaturatingMul(gauge common.MemoryGauge, other IntValue) (IntValue, error) {
return v.Mul(gauge, other)
}

func (v IntValue) Div(gauge common.MemoryGauge, other NumberValue) (NumberValue, error) {
o, ok := other.(IntValue)
if !ok {
return nil, InvalidOperandsError{}
}

func (v IntValue) Div(gauge common.MemoryGauge, other IntValue) (IntValue, error) {
// INT33-C
if o.BigInt.Sign() == 0 {
return nil, DivisionByZeroError{}
if other.BigInt.Sign() == 0 {
return IntValue{}, DivisionByZeroError{}
}

return NewIntValueFromBigInt(
gauge,
common.NewDivBigIntMemoryUsage(v.BigInt, o.BigInt),
common.NewDivBigIntMemoryUsage(v.BigInt, other.BigInt),
func() *big.Int {
res := new(big.Int)
return res.Div(v.BigInt, o.BigInt)
return res.Div(v.BigInt, other.BigInt)
},
), nil
}

func (v IntValue) SaturatingDiv(gauge common.MemoryGauge, other NumberValue) (NumberValue, error) {
func (v IntValue) SaturatingDiv(gauge common.MemoryGauge, other IntValue) (IntValue, error) {
return v.Div(gauge, other)
}

Expand Down Expand Up @@ -246,98 +221,73 @@ func (v IntValue) Equal(other Value) BoolValue {
return cmp == 0
}

func (v IntValue) BitwiseOr(gauge common.MemoryGauge, other IntegerValue) (IntegerValue, error) {
o, ok := other.(IntValue)
if !ok {
return nil, InvalidOperandsError{}
}

func (v IntValue) BitwiseOr(gauge common.MemoryGauge, other IntValue) (IntValue, error) {
return NewIntValueFromBigInt(
gauge,
common.NewBitwiseOrBigIntMemoryUsage(v.BigInt, o.BigInt),
common.NewBitwiseOrBigIntMemoryUsage(v.BigInt, other.BigInt),
func() *big.Int {
res := new(big.Int)
return res.Or(v.BigInt, o.BigInt)
return res.Or(v.BigInt, other.BigInt)
},
), nil
}

func (v IntValue) BitwiseXor(gauge common.MemoryGauge, other IntegerValue) (IntegerValue, error) {
o, ok := other.(IntValue)
if !ok {
return nil, InvalidOperandsError{}
}

func (v IntValue) BitwiseXor(gauge common.MemoryGauge, other IntValue) (IntValue, error) {
return NewIntValueFromBigInt(
gauge,
common.NewBitwiseXorBigIntMemoryUsage(v.BigInt, o.BigInt),
common.NewBitwiseXorBigIntMemoryUsage(v.BigInt, other.BigInt),
func() *big.Int {
res := new(big.Int)
return res.Xor(v.BigInt, o.BigInt)
return res.Xor(v.BigInt, other.BigInt)
},
), nil
}

func (v IntValue) BitwiseAnd(gauge common.MemoryGauge, other IntegerValue) (IntegerValue, error) {
o, ok := other.(IntValue)
if !ok {
return nil, InvalidOperandsError{}
}

func (v IntValue) BitwiseAnd(gauge common.MemoryGauge, other IntValue) (IntValue, error) {
return NewIntValueFromBigInt(
gauge,
common.NewBitwiseAndBigIntMemoryUsage(v.BigInt, o.BigInt),
common.NewBitwiseAndBigIntMemoryUsage(v.BigInt, other.BigInt),
func() *big.Int {
res := new(big.Int)
return res.And(v.BigInt, o.BigInt)
return res.And(v.BigInt, other.BigInt)
},
), nil
}

func (v IntValue) BitwiseLeftShift(gauge common.MemoryGauge, other IntegerValue) (IntegerValue, error) {
o, ok := other.(IntValue)
if !ok {
return nil, InvalidOperandsError{}
}

if o.BigInt.Sign() < 0 {
return nil, NegativeShiftError{}
func (v IntValue) BitwiseLeftShift(gauge common.MemoryGauge, other IntValue) (IntValue, error) {
if other.BigInt.Sign() < 0 {
return IntValue{}, NegativeShiftError{}
}

if !o.BigInt.IsUint64() {
return nil, OverflowError{}
if !other.BigInt.IsUint64() {
return IntValue{}, OverflowError{}
}

return NewIntValueFromBigInt(
gauge,
common.NewBitwiseLeftShiftBigIntMemoryUsage(v.BigInt, o.BigInt),
common.NewBitwiseLeftShiftBigIntMemoryUsage(v.BigInt, other.BigInt),
func() *big.Int {
res := new(big.Int)
return res.Lsh(v.BigInt, uint(o.BigInt.Uint64()))
return res.Lsh(v.BigInt, uint(other.BigInt.Uint64()))
},
), nil
}

func (v IntValue) BitwiseRightShift(gauge common.MemoryGauge, other IntegerValue) (IntegerValue, error) {
o, ok := other.(IntValue)
if !ok {
return nil, InvalidOperandsError{}
}

if o.BigInt.Sign() < 0 {
return nil, NegativeShiftError{}
func (v IntValue) BitwiseRightShift(gauge common.MemoryGauge, other IntValue) (IntValue, error) {
if other.BigInt.Sign() < 0 {
return IntValue{}, NegativeShiftError{}
}

if !o.BigInt.IsUint64() {
return nil, OverflowError{}
if !other.BigInt.IsUint64() {
return IntValue{}, OverflowError{}
}

return NewIntValueFromBigInt(
gauge,
common.NewBitwiseRightShiftBigIntMemoryUsage(v.BigInt, o.BigInt),
common.NewBitwiseRightShiftBigIntMemoryUsage(v.BigInt, other.BigInt),
func() *big.Int {
res := new(big.Int)
return res.Rsh(v.BigInt, uint(o.BigInt.Uint64()))
return res.Rsh(v.BigInt, uint(other.BigInt.Uint64()))
},
), nil
}
Expand Down
14 changes: 7 additions & 7 deletions values/value_integer.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ package values

import "github.com/onflow/cadence/common"

type IntegerValue interface {
NumberValue
BitwiseOr(gauge common.MemoryGauge, other IntegerValue) (IntegerValue, error)
BitwiseXor(gauge common.MemoryGauge, other IntegerValue) (IntegerValue, error)
BitwiseAnd(gauge common.MemoryGauge, other IntegerValue) (IntegerValue, error)
BitwiseLeftShift(gauge common.MemoryGauge, other IntegerValue) (IntegerValue, error)
BitwiseRightShift(gauge common.MemoryGauge, other IntegerValue) (IntegerValue, error)
type IntegerValue[T Value] interface {
NumberValue[T]
BitwiseOr(gauge common.MemoryGauge, other T) (T, error)
BitwiseXor(gauge common.MemoryGauge, other T) (T, error)
BitwiseAnd(gauge common.MemoryGauge, other T) (T, error)
BitwiseLeftShift(gauge common.MemoryGauge, other T) (T, error)
BitwiseRightShift(gauge common.MemoryGauge, other T) (T, error)
}
Loading