Skip to content

Commit 33f24a6

Browse files
committed
decouple core UFix64 value functionality from interpreter
1 parent c87ee1a commit 33f24a6

17 files changed

+540
-286
lines changed

interpreter/decode.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -851,9 +851,9 @@ func (d StorableDecoder) decodeUFix64() (UFix64Value, error) {
851851
value, err := decodeUint64(d.decoder, d.memoryGauge)
852852
if err != nil {
853853
if e, ok := err.(*cbor.WrongTypeError); ok {
854-
return 0, errors.NewUnexpectedError("unknown UFix64 encoding: %s", e.ActualType.String())
854+
return UFix64Value{}, errors.NewUnexpectedError("unknown UFix64 encoding: %s", e.ActualType.String())
855855
}
856-
return 0, err
856+
return UFix64Value{}, err
857857
}
858858

859859
// Already metered at `decodeUint64`

interpreter/encode.go

-17
Original file line numberDiff line numberDiff line change
@@ -480,23 +480,6 @@ func (v Fix64Value) Encode(e *atree.Encoder) error {
480480
return e.CBOR.EncodeInt64(int64(v))
481481
}
482482

483-
// Encode encodes UFix64Value as
484-
//
485-
// cbor.Tag{
486-
// Number: CBORTagUFix64Value,
487-
// Content: uint64(v),
488-
// }
489-
func (v UFix64Value) Encode(e *atree.Encoder) error {
490-
err := e.CBOR.EncodeRawBytes([]byte{
491-
// tag number
492-
0xd8, values.CBORTagUFix64Value,
493-
})
494-
if err != nil {
495-
return err
496-
}
497-
return e.CBOR.EncodeUint64(uint64(v))
498-
}
499-
500483
var _ atree.ContainerStorable = &SomeStorable{}
501484

502485
func (s SomeStorable) Encode(e *atree.Encoder) error {

interpreter/storage.go

+3
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,9 @@ func ConvertStoredValue(gauge common.MemoryGauge, value atree.Value) (Value, err
101101
case values.IntValue:
102102
return IntValue{IntValue: value}, nil
103103

104+
case values.UFix64Value:
105+
return UFix64Value{UFix64Value: value}, nil
106+
104107
case Value:
105108
return value, nil
106109

interpreter/value_bool.go

+2-4
Original file line numberDiff line numberDiff line change
@@ -65,10 +65,8 @@ func (v BoolValue) Equal(_ ValueComparisonContext, _ LocationRange, other Value)
6565
if !ok {
6666
return false
6767
}
68-
return bool(
69-
values.BoolValue(v).
70-
EqualBool(values.BoolValue(otherBool)),
71-
)
68+
return values.BoolValue(v).
69+
Equal(values.BoolValue(otherBool))
7270
}
7371

7472
func (v BoolValue) Less(_ ValueComparisonContext, other ComparableValue, _ LocationRange) BoolValue {

interpreter/value_fix64.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -480,15 +480,15 @@ func ConvertFix64(memoryGauge common.MemoryGauge, value Value, locationRange Loc
480480
return value
481481

482482
case UFix64Value:
483-
if value > Fix64MaxValue {
483+
if value.UFix64Value > Fix64MaxValue {
484484
panic(OverflowError{
485485
LocationRange: locationRange,
486486
})
487487
}
488488
return NewFix64Value(
489489
memoryGauge,
490490
func() int64 {
491-
return int64(value)
491+
return int64(value.UFix64Value)
492492
},
493493
)
494494

interpreter/value_int.go

+1-4
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ package interpreter
2121
import (
2222
"math"
2323
"math/big"
24-
"unsafe"
2524

2625
"github.com/onflow/atree"
2726

@@ -38,8 +37,6 @@ type IntValue struct {
3837
values.IntValue
3938
}
4039

41-
const int64Size = int(unsafe.Sizeof(int64(0)))
42-
4340
func NewIntValueFromInt64(memoryGauge common.MemoryGauge, value int64) IntValue {
4441
return IntValue{
4542
IntValue: values.NewIntValueFromInt64(memoryGauge, value),
@@ -397,7 +394,7 @@ func (v IntValue) Equal(_ ValueComparisonContext, _ LocationRange, other Value)
397394
return false
398395
}
399396

400-
return bool(v.IntValue.Equal(otherInt.IntValue))
397+
return v.IntValue.Equal(otherInt.IntValue)
401398
}
402399

403400
// HashInput returns a byte slice containing:

interpreter/value_int64.go

+3
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ package interpreter
2121
import (
2222
"encoding/binary"
2323
"math"
24+
"unsafe"
2425

2526
"github.com/onflow/atree"
2627

@@ -36,6 +37,8 @@ import (
3637

3738
type Int64Value int64
3839

40+
const int64Size = int(unsafe.Sizeof(int64(0)))
41+
3942
var Int64MemoryUsage = common.NewNumberMemoryUsage(int64Size)
4043

4144
func NewInt64Value(gauge common.MemoryGauge, valueGetter func() int64) Int64Value {

interpreter/value_test.go

+6-4
Original file line numberDiff line numberDiff line change
@@ -1857,13 +1857,15 @@ func TestBlockValue(t *testing.T) {
18571857
ByteArrayStaticType,
18581858
common.ZeroAddress,
18591859
),
1860-
5.0,
1860+
NewUnmeteredUFix64ValueWithInteger(5, EmptyLocationRange),
18611861
)
18621862

18631863
// static type test
1864-
var actualTs = block.Fields[sema.BlockTypeTimestampFieldName]
1865-
const expectedTs UFix64Value = 5.0
1866-
assert.Equal(t, expectedTs, actualTs)
1864+
1865+
assert.Equal(t,
1866+
NewUnmeteredUFix64ValueWithInteger(5, EmptyLocationRange),
1867+
block.Fields[sema.BlockTypeTimestampFieldName],
1868+
)
18671869
}
18681870

18691871
func TestEphemeralReferenceTypeConformance(t *testing.T) {

0 commit comments

Comments
 (0)