Skip to content

Commit

Permalink
use invocation interpreter
Browse files Browse the repository at this point in the history
  • Loading branch information
turbolent committed Nov 23, 2022
1 parent e2f0bc5 commit d216869
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 21 deletions.
52 changes: 36 additions & 16 deletions runtime/interpreter/interpreter.go
Original file line number Diff line number Diff line change
Expand Up @@ -1068,13 +1068,15 @@ func (interpreter *Interpreter) declareNonEnumCompositeValue(
interpreter,
func(invocation Invocation) Value {

interpreter := invocation.Interpreter

// Check that the resource is constructed
// in the same location as it was declared

locationRange := invocation.LocationRange

if compositeType.Kind == common.CompositeKindResource &&
invocation.Interpreter.Location != compositeType.Location {
interpreter.Location != compositeType.Location {

panic(ResourceConstructionError{
CompositeType: compositeType,
Expand Down Expand Up @@ -3211,6 +3213,8 @@ func (interpreter *Interpreter) newStorageIterationFunction(addressValue Address
return NewHostFunctionValue(
interpreter,
func(invocation Invocation) Value {
interpreter := invocation.Interpreter

fn, ok := invocation.Arguments[0].(FunctionValue)
if !ok {
panic(errors.NewUnreachableError())
Expand Down Expand Up @@ -3282,6 +3286,8 @@ func (interpreter *Interpreter) authAccountSaveFunction(addressValue AddressValu
return NewHostFunctionValue(
interpreter,
func(invocation Invocation) Value {
interpreter := invocation.Interpreter

value := invocation.Arguments[0]

path, ok := invocation.Arguments[1].(PathValue)
Expand Down Expand Up @@ -3336,6 +3342,8 @@ func (interpreter *Interpreter) authAccountTypeFunction(addressValue AddressValu
return NewHostFunctionValue(
interpreter,
func(invocation Invocation) Value {
interpreter := invocation.Interpreter

path, ok := invocation.Arguments[0].(PathValue)
if !ok {
panic(errors.NewUnreachableError())
Expand All @@ -3351,10 +3359,10 @@ func (interpreter *Interpreter) authAccountTypeFunction(addressValue AddressValu
}

return NewSomeValueNonCopying(
invocation.Interpreter,
interpreter,
NewTypeValue(
invocation.Interpreter,
value.StaticType(invocation.Interpreter),
interpreter,
value.StaticType(interpreter),
),
)
},
Expand All @@ -3379,6 +3387,8 @@ func (interpreter *Interpreter) authAccountReadFunction(addressValue AddressValu
return NewHostFunctionValue(
interpreter,
func(invocation Invocation) Value {
interpreter := invocation.Interpreter

path, ok := invocation.Arguments[0].(PathValue)
if !ok {
panic(errors.NewUnreachableError())
Expand All @@ -3403,7 +3413,7 @@ func (interpreter *Interpreter) authAccountReadFunction(addressValue AddressValu

ty := typeParameterPair.Value

valueStaticType := value.StaticType(invocation.Interpreter)
valueStaticType := value.StaticType(interpreter)

if !interpreter.IsSubTypeOfSemaType(valueStaticType, ty) {
valueSemaType := interpreter.MustConvertStaticToSemaType(valueStaticType)
Expand All @@ -3415,14 +3425,13 @@ func (interpreter *Interpreter) authAccountReadFunction(addressValue AddressValu
})
}

inter := invocation.Interpreter
locationRange := invocation.LocationRange

// We could also pass remove=true and the storable stored in storage,
// but passing remove=false here and writing nil below has the same effect
// TODO: potentially refactor and get storable in storage, pass it and remove=true
transferredValue := value.Transfer(
inter,
interpreter,
locationRange,
atree.Address{},
false,
Expand Down Expand Up @@ -3451,6 +3460,8 @@ func (interpreter *Interpreter) authAccountBorrowFunction(addressValue AddressVa
return NewHostFunctionValue(
interpreter,
func(invocation Invocation) Value {
interpreter := invocation.Interpreter

path, ok := invocation.Arguments[0].(PathValue)
if !ok {
panic(errors.NewUnreachableError())
Expand All @@ -3469,7 +3480,7 @@ func (interpreter *Interpreter) authAccountBorrowFunction(addressValue AddressVa
}

reference := NewStorageReferenceValue(
invocation.Interpreter,
interpreter,
referenceType.Authorized,
address,
path,
Expand All @@ -3488,7 +3499,7 @@ func (interpreter *Interpreter) authAccountBorrowFunction(addressValue AddressVa
return Nil
}

return NewSomeValueNonCopying(invocation.Interpreter, reference)
return NewSomeValueNonCopying(interpreter, reference)
},
sema.AuthAccountTypeBorrowFunctionType,
)
Expand All @@ -3502,6 +3513,7 @@ func (interpreter *Interpreter) authAccountLinkFunction(addressValue AddressValu
return NewHostFunctionValue(
interpreter,
func(invocation Invocation) Value {
interpreter := invocation.Interpreter

typeParameterPair := invocation.TypeParameterTypes.Oldest()
if typeParameterPair == nil {
Expand Down Expand Up @@ -3536,7 +3548,7 @@ func (interpreter *Interpreter) authAccountLinkFunction(addressValue AddressValu

// Write new value

borrowStaticType := ConvertSemaToStaticType(invocation.Interpreter, borrowType)
borrowStaticType := ConvertSemaToStaticType(interpreter, borrowType)

// Note that this will be metered twice if Atree validation is enabled.
linkValue := NewLinkValue(interpreter, targetPath, borrowStaticType)
Expand All @@ -3549,9 +3561,9 @@ func (interpreter *Interpreter) authAccountLinkFunction(addressValue AddressValu
)

return NewSomeValueNonCopying(
invocation.Interpreter,
interpreter,
NewCapabilityValue(
invocation.Interpreter,
interpreter,
addressValue,
newCapabilityPath,
borrowStaticType,
Expand All @@ -3571,6 +3583,7 @@ func (interpreter *Interpreter) accountGetLinkTargetFunction(addressValue Addres
return NewHostFunctionValue(
interpreter,
func(invocation Invocation) Value {
interpreter := invocation.Interpreter

capabilityPath, ok := invocation.Arguments[0].(PathValue)
if !ok {
Expand All @@ -3591,7 +3604,10 @@ func (interpreter *Interpreter) accountGetLinkTargetFunction(addressValue Addres
return Nil
}

return NewSomeValueNonCopying(invocation.Interpreter, link.TargetPath)
return NewSomeValueNonCopying(
interpreter,
link.TargetPath,
)
},
sema.AccountTypeGetLinkTargetFunctionType,
)
Expand All @@ -3605,6 +3621,7 @@ func (interpreter *Interpreter) authAccountUnlinkFunction(addressValue AddressVa
return NewHostFunctionValue(
interpreter,
func(invocation Invocation) Value {
interpreter := invocation.Interpreter

capabilityPath, ok := invocation.Arguments[0].(PathValue)
if !ok {
Expand Down Expand Up @@ -4022,6 +4039,8 @@ func (interpreter *Interpreter) isInstanceFunction(self Value) *HostFunctionValu
return NewHostFunctionValue(
interpreter,
func(invocation Invocation) Value {
interpreter := invocation.Interpreter

firstArgument := invocation.Arguments[0]
typeValue, ok := firstArgument.(TypeValue)

Expand All @@ -4037,7 +4056,7 @@ func (interpreter *Interpreter) isInstanceFunction(self Value) *HostFunctionValu
}

// NOTE: not invocation.Self, as that is only set for composite values
selfType := self.StaticType(invocation.Interpreter)
selfType := self.StaticType(interpreter)
return AsBoolValue(
interpreter.IsSubType(selfType, staticType),
)
Expand All @@ -4050,8 +4069,9 @@ func (interpreter *Interpreter) getTypeFunction(self Value) *HostFunctionValue {
return NewHostFunctionValue(
interpreter,
func(invocation Invocation) Value {
staticType := self.StaticType(invocation.Interpreter)
return NewTypeValue(invocation.Interpreter, staticType)
interpreter := invocation.Interpreter
staticType := self.StaticType(interpreter)
return NewTypeValue(interpreter, staticType)
},
sema.GetTypeFunctionType,
)
Expand Down
16 changes: 11 additions & 5 deletions runtime/interpreter/value.go
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,8 @@ func (v TypeValue) GetMember(interpreter *Interpreter, _ LocationRange, name str
return NewHostFunctionValue(
interpreter,
func(invocation Invocation) Value {
interpreter := invocation.Interpreter

staticType := v.Type
otherTypeValue, ok := invocation.Arguments[0].(TypeValue)
if !ok {
Expand All @@ -371,11 +373,9 @@ func (v TypeValue) GetMember(interpreter *Interpreter, _ LocationRange, name str
return FalseValue
}

inter := invocation.Interpreter

result := sema.IsSubType(
inter.MustConvertStaticToSemaType(staticType),
inter.MustConvertStaticToSemaType(otherStaticType),
interpreter.MustConvertStaticToSemaType(staticType),
interpreter.MustConvertStaticToSemaType(otherStaticType),
)
return AsBoolValue(result)
},
Expand Down Expand Up @@ -859,6 +859,8 @@ func (v CharacterValue) GetMember(interpreter *Interpreter, _ LocationRange, nam
return NewHostFunctionValue(
interpreter,
func(invocation Invocation) Value {
interpreter := invocation.Interpreter

memoryUsage := common.NewStringMemoryUsage(len(v))

return NewStringValue(
Expand Down Expand Up @@ -1122,6 +1124,7 @@ func (v *StringValue) GetMember(interpreter *Interpreter, _ LocationRange, name
return NewHostFunctionValue(
interpreter,
func(invocation Invocation) Value {
interpreter := invocation.Interpreter
otherArray, ok := invocation.Arguments[0].(*StringValue)
if !ok {
panic(errors.NewUnreachableError())
Expand Down Expand Up @@ -15623,13 +15626,15 @@ func (v *DictionaryValue) GetMember(
return NewHostFunctionValue(
interpreter,
func(invocation Invocation) Value {
interpreter := invocation.Interpreter

funcArgument, ok := invocation.Arguments[0].(FunctionValue)
if !ok {
panic(errors.NewUnreachableError())
}

v.ForEachKey(
invocation.Interpreter,
interpreter,
invocation.LocationRange,
funcArgument,
)
Expand Down Expand Up @@ -17576,6 +17581,7 @@ func (v AddressValue) GetMember(interpreter *Interpreter, _ LocationRange, name
return NewHostFunctionValue(
interpreter,
func(invocation Invocation) Value {
interpreter := invocation.Interpreter
address := common.Address(v)
return ByteSliceToByteArrayValue(interpreter, address[:])
},
Expand Down

0 comments on commit d216869

Please sign in to comment.