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

Decouple interpreter values from interpreter – Part 2 #3748

Merged
merged 3 commits into from
Feb 13, 2025
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
42 changes: 23 additions & 19 deletions interpreter/for_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -862,25 +862,29 @@ func TestInclusiveRangeForInLoop(t *testing.T) {
)

count := 0
iterator := (loopElements).(*interpreter.ArrayValue).Iterator(inter, interpreter.EmptyLocationRange)
for {
elem := iterator.Next(inter, interpreter.EmptyLocationRange)
if elem == nil {
break
}

AssertValuesEqual(
t,
inter,
interpreter.GetSmallIntegerValue(
int8(testCase.loopElements[count]),
integerStaticType,
),
elem,
)

count += 1
}
loopElementsArray := loopElements.(*interpreter.ArrayValue)

loopElementsArray.ForEach(
inter,
nil,
func(value interpreter.Value) (resume bool) {
AssertValuesEqual(
t,
inter,
interpreter.GetSmallIntegerValue(
int8(testCase.loopElements[count]),
integerStaticType,
),
value,
)

count += 1

return true
},
false,
interpreter.EmptyLocationRange,
)

assert.Equal(t, len(testCase.loopElements), count)
})
Expand Down
8 changes: 3 additions & 5 deletions interpreter/interpreter.go
Original file line number Diff line number Diff line change
Expand Up @@ -5345,11 +5345,9 @@ func (interpreter *Interpreter) ValidateAtreeValue(value atree.Value) {
}
}

func (interpreter *Interpreter) maybeTrackReferencedResourceKindedValue(value Value) {
if referenceValue, ok := value.(*EphemeralReferenceValue); ok {
if value, ok := referenceValue.Value.(ReferenceTrackedResourceKindedValue); ok {
interpreter.trackReferencedResourceKindedValue(value.ValueID(), referenceValue)
}
func (interpreter *Interpreter) maybeTrackReferencedResourceKindedValue(referenceValue *EphemeralReferenceValue) {
if value, ok := referenceValue.Value.(ReferenceTrackedResourceKindedValue); ok {
interpreter.trackReferencedResourceKindedValue(value.ValueID(), referenceValue)
}
}

Expand Down
1 change: 0 additions & 1 deletion interpreter/value.go
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,6 @@ type ContractValue interface {
// IterableValue is a value which can be iterated over, e.g. with a for-loop
type IterableValue interface {
Value
Iterator(interpreter *Interpreter, locationRange LocationRange) ValueIterator
ForEach(
interpreter *Interpreter,
elementType sema.Type,
Expand Down
31 changes: 0 additions & 31 deletions interpreter/value_array.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,37 +41,6 @@ type ArrayValue struct {
isDestroyed bool
}

type ArrayValueIterator struct {
atreeIterator atree.ArrayIterator
}

func (v *ArrayValue) Iterator(_ *Interpreter, _ LocationRange) ValueIterator {
arrayIterator, err := v.array.Iterator()
if err != nil {
panic(errors.NewExternalError(err))
}
return ArrayValueIterator{
atreeIterator: arrayIterator,
}
}

var _ ValueIterator = ArrayValueIterator{}

func (i ArrayValueIterator) Next(context ValueIteratorContext, _ LocationRange) Value {
atreeValue, err := i.atreeIterator.Next()
if err != nil {
panic(errors.NewExternalError(err))
}

if atreeValue == nil {
return nil
}

// atree.Array iterator returns low-level atree.Value,
// convert to high-level interpreter.Value
return MustConvertStoredValue(context, atreeValue)
}

func NewArrayValue(
interpreter *Interpreter,
locationRange LocationRange,
Expand Down
1 change: 0 additions & 1 deletion interpreter/value_composite.go
Original file line number Diff line number Diff line change
Expand Up @@ -1789,7 +1789,6 @@ func (v *CompositeValue) forEachAttachment(
// We create a reference here for the purposes of tracking it during iteration.
vType := interpreter.MustSemaTypeOfValue(v)
compositeReference := NewEphemeralReferenceValue(interpreter, UnauthorizedAccess, v, vType, locationRange)
interpreter.maybeTrackReferencedResourceKindedValue(compositeReference)
SupunS marked this conversation as resolved.
Show resolved Hide resolved
forEachAttachment(interpreter, compositeReference, locationRange, f)
}

Expand Down
6 changes: 0 additions & 6 deletions interpreter/value_ephemeral_reference.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import (
"github.com/onflow/atree"

"github.com/onflow/cadence/common"
"github.com/onflow/cadence/errors"
"github.com/onflow/cadence/sema"
)

Expand Down Expand Up @@ -335,11 +334,6 @@ func (*EphemeralReferenceValue) DeepRemove(_ *Interpreter, _ bool) {

func (*EphemeralReferenceValue) isReference() {}

func (v *EphemeralReferenceValue) Iterator(_ *Interpreter, _ LocationRange) ValueIterator {
// Not used for now
panic(errors.NewUnreachableError())
}

func (v *EphemeralReferenceValue) ForEach(
interpreter *Interpreter,
elementType sema.Type,
Expand Down
5 changes: 0 additions & 5 deletions interpreter/value_storage_reference.go
Original file line number Diff line number Diff line change
Expand Up @@ -412,11 +412,6 @@ func (*StorageReferenceValue) DeepRemove(_ *Interpreter, _ bool) {

func (*StorageReferenceValue) isReference() {}

func (v *StorageReferenceValue) Iterator(_ *Interpreter, _ LocationRange) ValueIterator {
// Not used for now
panic(errors.NewUnreachableError())
}

func (v *StorageReferenceValue) ForEach(
interpreter *Interpreter,
elementType sema.Type,
Expand Down
6 changes: 3 additions & 3 deletions interpreter/value_string.go
Original file line number Diff line number Diff line change
Expand Up @@ -635,7 +635,7 @@ func (v *StringValue) Split(inter *Interpreter, locationRange LocationRange, sep
// Explode returns a Cadence array of type [String], where each element is a single character of the string
func (v *StringValue) Explode(inter *Interpreter, locationRange LocationRange) *ArrayValue {

iterator := v.Iterator(inter, locationRange)
iterator := v.Iterator()

return NewArrayValueWithIterator(
inter,
Expand Down Expand Up @@ -830,7 +830,7 @@ func (v *StringValue) ConformsToStaticType(
return true
}

func (v *StringValue) Iterator(_ *Interpreter, _ LocationRange) ValueIterator {
func (v *StringValue) Iterator() StringValueIterator {
return StringValueIterator{
graphemes: uniseg.NewGraphemes(v.Str),
}
Expand All @@ -843,7 +843,7 @@ func (v *StringValue) ForEach(
transferElements bool,
locationRange LocationRange,
) {
iterator := v.Iterator(interpreter, locationRange)
iterator := v.Iterator()
for {
value := iterator.Next(interpreter, locationRange)
if value == nil {
Expand Down
Loading