Skip to content

Commit c6ab2b3

Browse files
committed
Decouple 'Destroy' and 'Walk' methods from interpreter
1 parent b035840 commit c6ab2b3

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

50 files changed

+152
-114
lines changed

interpreter/interface.go

+48-1
Original file line numberDiff line numberDiff line change
@@ -168,11 +168,17 @@ var _ MemberAccessibleContext = &Interpreter{}
168168

169169
type FunctionCreationContext interface {
170170
StaticTypeAndReferenceContext
171-
GetCompositeValueFunctions(v *CompositeValue, locationRange LocationRange) *FunctionOrderedMap
171+
CompositeFunctionContext
172172
}
173173

174174
var _ FunctionCreationContext = &Interpreter{}
175175

176+
type CompositeFunctionContext interface {
177+
GetCompositeValueFunctions(v *CompositeValue, locationRange LocationRange) *FunctionOrderedMap
178+
}
179+
180+
var _ CompositeFunctionContext = &Interpreter{}
181+
176182
type StaticTypeAndReferenceContext interface {
177183
common.MemoryGauge
178184
ValueStaticTypeContext
@@ -204,6 +210,12 @@ type ResourceDestructionHandler interface {
204210
valueID atree.ValueID,
205211
locationRange LocationRange,
206212
)
213+
214+
WithResourceDestruction(
215+
valueID atree.ValueID,
216+
locationRange LocationRange,
217+
f func(),
218+
)
207219
}
208220

209221
var _ ResourceDestructionHandler = &Interpreter{}
@@ -216,6 +228,29 @@ type CapConReferenceValueContext interface {
216228

217229
var _ CapConReferenceValueContext = &Interpreter{}
218230

231+
type ResourceDestructionContext interface {
232+
ValueWalkContext
233+
ResourceDestructionHandler
234+
CompositeFunctionContext
235+
EventContext
236+
237+
GetResourceDestructionContextForLocation(location common.Location) ResourceDestructionContext
238+
}
239+
240+
var _ ResourceDestructionContext = &Interpreter{}
241+
242+
type ValueWalkContext interface {
243+
ContainerMutationContext
244+
}
245+
246+
var _ ValueWalkContext = &Interpreter{}
247+
248+
type EventContext interface {
249+
EmitEvent(event *CompositeValue, eventType *sema.CompositeType, locationRange LocationRange)
250+
}
251+
252+
var _ EventContext = &Interpreter{}
253+
219254
// NoOpStringContext is the ValueStringContext implementation used in Value.RecursiveString method.
220255
// Since Value.RecursiveString is a non-mutating operation, it should only need the no-op memory metering
221256
// and a WithMutationPrevention implementation.
@@ -308,6 +343,10 @@ func (ctx NoOpStringContext) ReportArrayValueTransferTrace(_ string, _ int, _ ti
308343
panic(errors.NewUnreachableError())
309344
}
310345

346+
func (ctx NoOpStringContext) ReportArrayValueDestroyTrace(_ string, _ int, _ time.Duration) {
347+
panic(errors.NewUnreachableError())
348+
}
349+
311350
func (ctx NoOpStringContext) ReportArrayValueConstructTrace(_ string, _ int, _ time.Duration) {
312351
panic(errors.NewUnreachableError())
313352
}
@@ -316,6 +355,10 @@ func (ctx NoOpStringContext) ReportDictionaryValueTransferTrace(_ string, _ int,
316355
panic(errors.NewUnreachableError())
317356
}
318357

358+
func (ctx NoOpStringContext) ReportDictionaryValueDestroyTrace(_ string, _ int, _ time.Duration) {
359+
panic(errors.NewUnreachableError())
360+
}
361+
319362
func (ctx NoOpStringContext) ReportDictionaryValueDeepRemoveTrace(_ string, _ int, _ time.Duration) {
320363
panic(errors.NewUnreachableError())
321364
}
@@ -340,6 +383,10 @@ func (ctx NoOpStringContext) ReportCompositeValueSetMemberTrace(_ string, _ stri
340383
panic(errors.NewUnreachableError())
341384
}
342385

386+
func (ctx NoOpStringContext) ReportCompositeValueDestroyTrace(_ string, _ string, _ string, _ time.Duration) {
387+
panic(errors.NewUnreachableError())
388+
}
389+
343390
func (ctx NoOpStringContext) ReportCompositeValueGetMemberTrace(_ string, _ string, _ string, _ string, _ time.Duration) {
344391
panic(errors.NewUnreachableError())
345392
}

interpreter/interpreter.go

+9-1
Original file line numberDiff line numberDiff line change
@@ -5763,7 +5763,7 @@ func (interpreter *Interpreter) EnforceNotResourceDestruction(
57635763
}
57645764
}
57655765

5766-
func (interpreter *Interpreter) withResourceDestruction(
5766+
func (interpreter *Interpreter) WithResourceDestruction(
57675767
valueID atree.ValueID,
57685768
locationRange LocationRange,
57695769
f func(),
@@ -5844,6 +5844,14 @@ func (interpreter *Interpreter) MaybeSetMutationDuringCapConIteration(addressPat
58445844
}
58455845

58465846
func (interpreter *Interpreter) GetMemberAccessContextForLocation(location common.Location) MemberAccessibleContext {
5847+
return interpreter.ensureLoaded(location)
5848+
}
5849+
5850+
func (interpreter *Interpreter) GetResourceDestructionContextForLocation(location common.Location) ResourceDestructionContext {
5851+
return interpreter.ensureLoaded(location)
5852+
}
5853+
5854+
func (interpreter *Interpreter) ensureLoaded(location common.Location) *Interpreter {
58475855
if location == nil || interpreter.Location == location {
58485856
return interpreter
58495857
}

interpreter/interpreter_statement.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -362,7 +362,7 @@ func (interpreter *Interpreter) visitForStatementBody(
362362
return nil, false
363363
}
364364

365-
func (interpreter *Interpreter) emitEvent(event *CompositeValue, eventType *sema.CompositeType, locationRange LocationRange) {
365+
func (interpreter *Interpreter) EmitEvent(event *CompositeValue, eventType *sema.CompositeType, locationRange LocationRange) {
366366

367367
config := interpreter.SharedState.Config
368368

@@ -393,7 +393,7 @@ func (interpreter *Interpreter) VisitEmitStatement(statement *ast.EmitStatement)
393393
HasPosition: statement,
394394
}
395395

396-
interpreter.emitEvent(event, eventType, locationRange)
396+
interpreter.EmitEvent(event, eventType, locationRange)
397397

398398
return nil
399399
}

interpreter/interpreter_tracing.go

+6-3
Original file line numberDiff line numberDiff line change
@@ -54,17 +54,20 @@ type Tracer interface {
5454
ReportArrayValueDeepRemoveTrace(typeInfo string, count int, duration time.Duration)
5555
ReportArrayValueTransferTrace(info string, count int, since time.Duration)
5656
ReportArrayValueConstructTrace(typeInfo string, count int, duration time.Duration)
57+
ReportArrayValueDestroyTrace(info string, count int, since time.Duration)
5758

5859
ReportDictionaryValueTransferTrace(info string, count int, since time.Duration)
5960
ReportDictionaryValueDeepRemoveTrace(info string, count int, since time.Duration)
6061
ReportDictionaryValueGetMemberTrace(info string, count int, name string, since time.Duration)
6162
ReportDictionaryValueConstructTrace(info string, count int, since time.Duration)
63+
ReportDictionaryValueDestroyTrace(info string, count int, since time.Duration)
6264

6365
ReportCompositeValueDeepRemoveTrace(owner string, id string, kind string, since time.Duration)
6466
ReportCompositeValueTransferTrace(owner string, id string, kind string, since time.Duration)
6567
ReportCompositeValueSetMemberTrace(owner string, id string, kind string, name string, since time.Duration)
6668
ReportCompositeValueGetMemberTrace(owner string, typeID string, kind string, name string, duration time.Duration)
6769
ReportCompositeValueConstructTrace(owner string, id string, kind string, since time.Duration)
70+
ReportCompositeValueDestroyTrace(owner string, id string, kind string, since time.Duration)
6871

6972
ReportDomainStorageMapDeepRemoveTrace(info string, i int, since time.Duration)
7073
}
@@ -114,7 +117,7 @@ func (interpreter *Interpreter) ReportArrayValueDeepRemoveTrace(
114117
)
115118
}
116119

117-
func (interpreter *Interpreter) reportArrayValueDestroyTrace(
120+
func (interpreter *Interpreter) ReportArrayValueDestroyTrace(
118121
typeInfo string,
119122
count int,
120123
duration time.Duration,
@@ -198,7 +201,7 @@ func (interpreter *Interpreter) ReportDomainStorageMapDeepRemoveTrace(
198201
)
199202
}
200203

201-
func (interpreter *Interpreter) reportDictionaryValueDestroyTrace(
204+
func (interpreter *Interpreter) ReportDictionaryValueDestroyTrace(
202205
typeInfo string,
203206
count int,
204207
duration time.Duration,
@@ -293,7 +296,7 @@ func (interpreter *Interpreter) ReportCompositeValueDeepRemoveTrace(
293296
)
294297
}
295298

296-
func (interpreter *Interpreter) reportCompositeValueDestroyTrace(
299+
func (interpreter *Interpreter) ReportCompositeValueDestroyTrace(
297300
owner string,
298301
typeID string,
299302
kind string,

interpreter/simplecompositevalue.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ func (v *SimpleCompositeValue) ForEachField(
9494

9595
// Walk iterates over all field values of the composite value.
9696
// It does NOT walk the computed fields and functions!
97-
func (v *SimpleCompositeValue) Walk(_ *Interpreter, walkChild func(Value), _ LocationRange) {
97+
func (v *SimpleCompositeValue) Walk(_ ValueWalkContext, walkChild func(Value), _ LocationRange) {
9898
v.ForEachField(func(_ string, fieldValue Value) (resume bool) {
9999
walkChild(fieldValue)
100100

interpreter/value.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ type Value interface {
9393
fmt.Stringer
9494
//isValue()
9595
Accept(interpreter *Interpreter, visitor Visitor, locationRange LocationRange)
96-
Walk(interpreter *Interpreter, walkChild func(Value), locationRange LocationRange)
96+
Walk(interpreter ValueWalkContext, walkChild func(Value), locationRange LocationRange)
9797
StaticType(context ValueStaticTypeContext) StaticType
9898
// ConformsToStaticType returns true if the value (i.e. its dynamic type)
9999
// conforms to its own static type.
@@ -196,18 +196,18 @@ type ComparableValue interface {
196196

197197
type ResourceKindedValue interface {
198198
Value
199-
Destroy(interpreter *Interpreter, locationRange LocationRange)
199+
Destroy(context ResourceDestructionContext, locationRange LocationRange)
200200
IsDestroyed() bool
201201
isInvalidatedResource(context ValueStaticTypeContext) bool
202202
}
203203

204-
func maybeDestroy(interpreter *Interpreter, locationRange LocationRange, value Value) {
204+
func maybeDestroy(context ResourceDestructionContext, locationRange LocationRange, value Value) {
205205
resourceKindedValue, ok := value.(ResourceKindedValue)
206206
if !ok {
207207
return
208208
}
209209

210-
resourceKindedValue.Destroy(interpreter, locationRange)
210+
resourceKindedValue.Destroy(context, locationRange)
211211
}
212212

213213
// ReferenceTrackedResourceKindedValue is a resource-kinded value

interpreter/value_accountcapabilitycontroller.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ func (v *AccountCapabilityControllerValue) Accept(interpreter *Interpreter, visi
9393
visitor.VisitAccountCapabilityControllerValue(interpreter, v)
9494
}
9595

96-
func (v *AccountCapabilityControllerValue) Walk(_ *Interpreter, walkChild func(Value), _ LocationRange) {
96+
func (v *AccountCapabilityControllerValue) Walk(_ ValueWalkContext, walkChild func(Value), _ LocationRange) {
9797
walkChild(v.CapabilityID)
9898
}
9999

interpreter/value_address.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ func (v AddressValue) Accept(interpreter *Interpreter, visitor Visitor, _ Locati
9898
visitor.VisitAddressValue(interpreter, v)
9999
}
100100

101-
func (AddressValue) Walk(_ *Interpreter, _ func(Value), _ LocationRange) {
101+
func (AddressValue) Walk(_ ValueWalkContext, _ func(Value), _ LocationRange) {
102102
// NO-OP
103103
}
104104

interpreter/value_array.go

+10-12
Original file line numberDiff line numberDiff line change
@@ -290,12 +290,12 @@ func (v *ArrayValue) Iterator(_ ValueStaticTypeContext, _ LocationRange) ValueIt
290290
}
291291

292292
func (v *ArrayValue) Walk(
293-
interpreter *Interpreter,
293+
context ValueWalkContext,
294294
walkChild func(Value),
295295
locationRange LocationRange,
296296
) {
297297
v.Iterate(
298-
interpreter,
298+
context,
299299
func(element Value) (resume bool) {
300300
walkChild(element)
301301
return true
@@ -339,20 +339,18 @@ func (v *ArrayValue) IsStaleResource(interpreter *Interpreter) bool {
339339
return v.array == nil && v.IsResourceKinded(interpreter)
340340
}
341341

342-
func (v *ArrayValue) Destroy(interpreter *Interpreter, locationRange LocationRange) {
342+
func (v *ArrayValue) Destroy(context ResourceDestructionContext, locationRange LocationRange) {
343343

344-
interpreter.ReportComputation(common.ComputationKindDestroyArrayValue, 1)
345-
346-
config := interpreter.SharedState.Config
344+
context.ReportComputation(common.ComputationKindDestroyArrayValue, 1)
347345

348-
if config.TracingEnabled {
346+
if context.TracingEnabled() {
349347
startTime := time.Now()
350348

351349
typeInfo := v.Type.String()
352350
count := v.Count()
353351

354352
defer func() {
355-
interpreter.reportArrayValueDestroyTrace(
353+
context.ReportArrayValueDestroyTrace(
356354
typeInfo,
357355
count,
358356
time.Since(startTime),
@@ -362,14 +360,14 @@ func (v *ArrayValue) Destroy(interpreter *Interpreter, locationRange LocationRan
362360

363361
valueID := v.ValueID()
364362

365-
interpreter.withResourceDestruction(
363+
context.WithResourceDestruction(
366364
valueID,
367365
locationRange,
368366
func() {
369367
v.Walk(
370-
interpreter,
368+
context,
371369
func(element Value) {
372-
maybeDestroy(interpreter, locationRange, element)
370+
maybeDestroy(context, locationRange, element)
373371
},
374372
locationRange,
375373
)
@@ -378,7 +376,7 @@ func (v *ArrayValue) Destroy(interpreter *Interpreter, locationRange LocationRan
378376

379377
v.isDestroyed = true
380378

381-
InvalidateReferencedResources(interpreter, v, locationRange)
379+
InvalidateReferencedResources(context, v, locationRange)
382380

383381
v.array = nil
384382
}

interpreter/value_bool.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ func (v BoolValue) Accept(interpreter *Interpreter, visitor Visitor, _ LocationR
4444
visitor.VisitBoolValue(interpreter, v)
4545
}
4646

47-
func (BoolValue) Walk(_ *Interpreter, _ func(Value), _ LocationRange) {
47+
func (BoolValue) Walk(_ ValueWalkContext, _ func(Value), _ LocationRange) {
4848
// NO-OP
4949
}
5050

interpreter/value_capability.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ func (v *IDCapabilityValue) Accept(interpreter *Interpreter, visitor Visitor, _
104104
visitor.VisitCapabilityValue(interpreter, v)
105105
}
106106

107-
func (v *IDCapabilityValue) Walk(_ *Interpreter, walkChild func(Value), _ LocationRange) {
107+
func (v *IDCapabilityValue) Walk(_ ValueWalkContext, walkChild func(Value), _ LocationRange) {
108108
walkChild(v.ID)
109109
walkChild(v.address)
110110
}

interpreter/value_character.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ func (v CharacterValue) Accept(interpreter *Interpreter, visitor Visitor, _ Loca
8383
visitor.VisitCharacterValue(interpreter, v)
8484
}
8585

86-
func (CharacterValue) Walk(_ *Interpreter, _ func(Value), _ LocationRange) {
86+
func (CharacterValue) Walk(_ ValueWalkContext, _ func(Value), _ LocationRange) {
8787
// NO-OP
8888
}
8989

0 commit comments

Comments
 (0)