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

Improve state decoding tool #798

Merged
merged 13 commits into from
Apr 16, 2021
Merged
Show file tree
Hide file tree
Changes from 9 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
2 changes: 1 addition & 1 deletion runtime/ft_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -694,5 +694,5 @@ func BenchmarkRuntimeFungibleTokenTransfer(b *testing.B) {
sum = sum.Plus(value).(interpreter.UFix64Value)
}

require.True(b, bool(mintAmountValue.Equal(nil, sum)))
require.True(b, bool(mintAmountValue.Equal(sum, nil, true)))
}
25 changes: 4 additions & 21 deletions runtime/interpreter/interpreter_expression.go
Original file line number Diff line number Diff line change
Expand Up @@ -238,29 +238,12 @@ func (interpreter *Interpreter) testEqual(left, right Value) BoolValue {
left = interpreter.unbox(left)
right = interpreter.unbox(right)

// TODO: add support for arrays and dictionaries

switch left := left.(type) {
case NilValue:
_, ok := right.(NilValue)
return BoolValue(ok)

case EquatableValue:
// NOTE: might be NilValue
right, ok := right.(EquatableValue)
if !ok {
return false
}
return left.Equal(interpreter, right)

case *ArrayValue,
*DictionaryValue:
// TODO:
return false

default:
leftEquatable, ok := left.(EquatableValue)
if !ok {
return false
}

return BoolValue(leftEquatable.Equal(right, interpreter, true))
}

func (interpreter *Interpreter) VisitUnaryExpression(expression *ast.UnaryExpression) ast.Repr {
Expand Down
2 changes: 1 addition & 1 deletion runtime/interpreter/interpreter_statement.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ func (interpreter *Interpreter) VisitSwitchStatement(switchStatement *ast.Switch
// If the test value and case values are equal,
// evaluate the case's statements

if testValue.Equal(interpreter, caseValue) {
if testValue.Equal(caseValue, interpreter, true) {
return runStatements()
}

Expand Down
9 changes: 9 additions & 0 deletions runtime/interpreter/primitivestatictype.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,15 @@ import (

type PrimitiveStaticType uint

func (t PrimitiveStaticType) Equal(other StaticType) bool {
otherPrimitiveType, ok := other.(PrimitiveStaticType)
if !ok {
return false
}

return t == otherPrimitiveType
}

// !!! *WARNING* !!!
//
// Only add new types by:
Expand Down
123 changes: 113 additions & 10 deletions runtime/interpreter/statictype.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import (
type StaticType interface {
fmt.Stringer
IsStaticType()
Equal(other StaticType) bool
}

// CompositeStaticType
Expand All @@ -49,11 +50,20 @@ type CompositeStaticType struct {
func (CompositeStaticType) IsStaticType() {}

func (t CompositeStaticType) String() string {
return fmt.Sprintf(
"CompositeStaticType(Location: %s, QualifiedIdentifier: %s)",
t.Location,
t.QualifiedIdentifier,
)
if t.Location == nil {
return t.QualifiedIdentifier
}
return string(t.Location.TypeID(t.QualifiedIdentifier))
}

func (t CompositeStaticType) Equal(other StaticType) bool {
otherCompositeType, ok := other.(CompositeStaticType)
if !ok {
return false
}

return common.LocationsMatch(otherCompositeType.Location, t.Location) &&
otherCompositeType.QualifiedIdentifier == t.QualifiedIdentifier
}

// InterfaceStaticType
Expand All @@ -66,11 +76,20 @@ type InterfaceStaticType struct {
func (InterfaceStaticType) IsStaticType() {}

func (t InterfaceStaticType) String() string {
return fmt.Sprintf(
"InterfaceStaticType(Location: %s, QualifiedIdentifier: %s)",
t.Location,
t.QualifiedIdentifier,
)
if t.Location == nil {
return t.QualifiedIdentifier
}
return string(t.Location.TypeID(t.QualifiedIdentifier))
}

func (t InterfaceStaticType) Equal(other StaticType) bool {
otherInterfaceType, ok := other.(InterfaceStaticType)
if !ok {
return false
}

return common.LocationsMatch(otherInterfaceType.Location, t.Location) &&
otherInterfaceType.QualifiedIdentifier == t.QualifiedIdentifier
}

// VariableSizedStaticType
Expand All @@ -85,6 +104,15 @@ func (t VariableSizedStaticType) String() string {
return fmt.Sprintf("[%s]", t.Type)
}

func (t VariableSizedStaticType) Equal(other StaticType) bool {
otherVariableSizedType, ok := other.(VariableSizedStaticType)
if !ok {
return false
}

return t.Type.Equal(otherVariableSizedType.Type)
}

// ConstantSizedStaticType

type ConstantSizedStaticType struct {
Expand All @@ -98,6 +126,16 @@ func (t ConstantSizedStaticType) String() string {
return fmt.Sprintf("[%s; %d]", t.Type, t.Size)
}

func (t ConstantSizedStaticType) Equal(other StaticType) bool {
otherConstantSizedType, ok := other.(ConstantSizedStaticType)
if !ok {
return false
}

return t.Size == otherConstantSizedType.Size &&
t.Type.Equal(otherConstantSizedType.Type)
}

// DictionaryStaticType

type DictionaryStaticType struct {
Expand All @@ -111,6 +149,16 @@ func (t DictionaryStaticType) String() string {
return fmt.Sprintf("{%s: %s}", t.KeyType, t.ValueType)
}

func (t DictionaryStaticType) Equal(other StaticType) bool {
otherDictionaryType, ok := other.(DictionaryStaticType)
if !ok {
return false
}

return t.KeyType.Equal(otherDictionaryType.KeyType) &&
t.ValueType.Equal(otherDictionaryType.ValueType)
}

// OptionalStaticType

type OptionalStaticType struct {
Expand All @@ -123,6 +171,15 @@ func (t OptionalStaticType) String() string {
return fmt.Sprintf("%s?", t.Type)
}

func (t OptionalStaticType) Equal(other StaticType) bool {
otherOptionalType, ok := other.(OptionalStaticType)
if !ok {
return false
}

return t.Type.Equal(otherOptionalType.Type)
}

// RestrictedStaticType

type RestrictedStaticType struct {
Expand All @@ -147,6 +204,26 @@ func (t *RestrictedStaticType) String() string {
return fmt.Sprintf("%s{%s}", t.Type, strings.Join(restrictions, ", "))
}

func (t *RestrictedStaticType) Equal(other StaticType) bool {
otherRestrictedType, ok := other.(*RestrictedStaticType)
if !ok || len(t.Restrictions) != len(otherRestrictedType.Restrictions) {
return false
}

outer:
for _, restriction := range t.Restrictions {
for _, otherRestriction := range t.Restrictions {
if restriction.Equal(otherRestriction) {
continue outer
}
}

return false
}

return t.Type.Equal(otherRestrictedType.Type)
}

// ReferenceStaticType

type ReferenceStaticType struct {
Expand All @@ -165,6 +242,16 @@ func (t ReferenceStaticType) String() string {
return fmt.Sprintf("%s&%s", auth, t.Type)
}

func (t ReferenceStaticType) Equal(other StaticType) bool {
otherReferenceType, ok := other.(ReferenceStaticType)
if !ok {
return false
}

return t.Authorized == otherReferenceType.Authorized &&
t.Type.Equal(otherReferenceType.Type)
}

// CapabilityStaticType

type CapabilityStaticType struct {
Expand All @@ -180,6 +267,22 @@ func (t CapabilityStaticType) String() string {
return "Capability"
}

func (t CapabilityStaticType) Equal(other StaticType) bool {
otherCapabilityType, ok := other.(CapabilityStaticType)
if !ok {
return false
}

// The borrow types must either be both nil,
// or they must be equal

if t.BorrowType == nil {
return otherCapabilityType.BorrowType == nil
}

return t.BorrowType.Equal(otherCapabilityType.BorrowType)
}

// Conversion

func ConvertSemaToStaticType(t sema.Type) StaticType {
Expand Down
Loading