Skip to content

Commit bb0c5fa

Browse files
committed
decouple iterator iteration from Interpreter
1 parent fbd8634 commit bb0c5fa

File tree

4 files changed

+13
-8
lines changed

4 files changed

+13
-8
lines changed

interpreter/inclusive_range_iterator.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -64,18 +64,18 @@ func NewInclusiveRangeIterator(
6464
}
6565
}
6666

67-
func (i *InclusiveRangeIterator) Next(interpreter *Interpreter, locationRange LocationRange) Value {
67+
func (i *InclusiveRangeIterator) Next(context ValueIteratorContext, locationRange LocationRange) Value {
6868
valueToReturn := i.next
6969

7070
// Ensure that valueToReturn is within the bounds.
71-
if i.stepNegative && bool(valueToReturn.Less(interpreter, i.end, locationRange)) {
71+
if i.stepNegative && bool(valueToReturn.Less(context, i.end, locationRange)) {
7272
return nil
73-
} else if !i.stepNegative && bool(valueToReturn.Greater(interpreter, i.end, locationRange)) {
73+
} else if !i.stepNegative && bool(valueToReturn.Greater(context, i.end, locationRange)) {
7474
return nil
7575
}
7676

7777
// Update the next value.
78-
nextValueToReturn, ok := valueToReturn.Plus(interpreter, i.step, locationRange).(IntegerValue)
78+
nextValueToReturn, ok := valueToReturn.Plus(context, i.step, locationRange).(IntegerValue)
7979
if !ok {
8080
panic(errors.NewUnreachableError())
8181
}

interpreter/value.go

+6-1
Original file line numberDiff line numberDiff line change
@@ -247,10 +247,15 @@ type OwnedValue interface {
247247
GetOwner() common.Address
248248
}
249249

250+
type ValueIteratorContext interface {
251+
common.MemoryGauge
252+
NumberValueArithmeticContext
253+
}
254+
250255
// ValueIterator is an iterator which returns values.
251256
// When Next returns nil, it signals the end of the iterator.
252257
type ValueIterator interface {
253-
Next(interpreter *Interpreter, locationRange LocationRange) Value
258+
Next(context ValueIteratorContext, locationRange LocationRange) Value
254259
}
255260

256261
func safeAdd(a, b int, locationRange LocationRange) int {

interpreter/value_array.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ func (v *ArrayValue) Iterator(_ *Interpreter, _ LocationRange) ValueIterator {
5757

5858
var _ ValueIterator = ArrayValueIterator{}
5959

60-
func (i ArrayValueIterator) Next(interpreter *Interpreter, _ LocationRange) Value {
60+
func (i ArrayValueIterator) Next(context ValueIteratorContext, _ LocationRange) Value {
6161
atreeValue, err := i.atreeIterator.Next()
6262
if err != nil {
6363
panic(errors.NewExternalError(err))
@@ -69,7 +69,7 @@ func (i ArrayValueIterator) Next(interpreter *Interpreter, _ LocationRange) Valu
6969

7070
// atree.Array iterator returns low-level atree.Value,
7171
// convert to high-level interpreter.Value
72-
return MustConvertStoredValue(interpreter, atreeValue)
72+
return MustConvertStoredValue(context, atreeValue)
7373
}
7474

7575
func NewArrayValue(

interpreter/value_string.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1068,7 +1068,7 @@ type StringValueIterator struct {
10681068

10691069
var _ ValueIterator = StringValueIterator{}
10701070

1071-
func (i StringValueIterator) Next(_ *Interpreter, _ LocationRange) Value {
1071+
func (i StringValueIterator) Next(_ ValueIteratorContext, _ LocationRange) Value {
10721072
if !i.graphemes.Next() {
10731073
return nil
10741074
}

0 commit comments

Comments
 (0)