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

Address some tech debt #2155

Merged
merged 4 commits into from
Nov 18, 2022
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
2 changes: 1 addition & 1 deletion runtime/environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -765,7 +765,7 @@ func (e *interpreterEnvironment) newInjectedCompositeFieldsHandler() interpreter
)

return map[string]interpreter.Value{
"account": stdlib.NewAuthAccountValue(
sema.ContractAccountFieldName: stdlib.NewAuthAccountValue(
inter,
e,
addressValue,
Expand Down
121 changes: 108 additions & 13 deletions runtime/interpreter/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,17 @@ func NewAuthAccountValue(
var contracts Value
var keys Value
var inbox Value
var forEachStoredFunction *HostFunctionValue
var forEachPublicFunction *HostFunctionValue
var forEachPrivateFunction *HostFunctionValue
var typeFunction *HostFunctionValue
var loadFunction *HostFunctionValue
var copyFunction *HostFunctionValue
var saveFunction *HostFunctionValue
var borrowFunction *HostFunctionValue
var linkFunction *HostFunctionValue
var unlinkFunction *HostFunctionValue
var getLinkTargetFunction *HostFunctionValue

computeField := func(name string, inter *Interpreter, locationRange LocationRange) Value {
switch name {
Expand All @@ -84,42 +95,106 @@ func NewAuthAccountValue(
inbox = inboxConstructor()
}
return inbox

case sema.AuthAccountPublicPathsField:
return inter.publicAccountPaths(address, locationRange)

case sema.AuthAccountPrivatePathsField:
return inter.privateAccountPaths(address, locationRange)

case sema.AuthAccountStoragePathsField:
return inter.storageAccountPaths(address, locationRange)

case sema.AuthAccountForEachPublicField:
return inter.newStorageIterationFunction(address, common.PathDomainPublic, sema.PublicPathType)
if forEachPublicFunction == nil {
forEachPublicFunction = inter.newStorageIterationFunction(
address,
common.PathDomainPublic,
sema.PublicPathType,
)
}
return forEachPublicFunction

case sema.AuthAccountForEachPrivateField:
return inter.newStorageIterationFunction(address, common.PathDomainPrivate, sema.PrivatePathType)
if forEachPrivateFunction == nil {
forEachPrivateFunction = inter.newStorageIterationFunction(
address,
common.PathDomainPrivate,
sema.PrivatePathType,
)
}
return forEachPrivateFunction

case sema.AuthAccountForEachStoredField:
return inter.newStorageIterationFunction(address, common.PathDomainStorage, sema.StoragePathType)
if forEachStoredFunction == nil {
forEachStoredFunction = inter.newStorageIterationFunction(
address,
common.PathDomainStorage,
sema.StoragePathType,
)
}
return forEachStoredFunction

case sema.AuthAccountBalanceField:
return accountBalanceGet()

case sema.AuthAccountAvailableBalanceField:
return accountAvailableBalanceGet()

case sema.AuthAccountStorageUsedField:
return storageUsedGet(inter)

case sema.AuthAccountStorageCapacityField:
return storageCapacityGet(inter)

case sema.AuthAccountTypeField:
return inter.authAccountTypeFunction(address)
if typeFunction == nil {
typeFunction = inter.authAccountTypeFunction(address)
}
return typeFunction

case sema.AuthAccountLoadField:
return inter.authAccountLoadFunction(address)
if loadFunction == nil {
loadFunction = inter.authAccountLoadFunction(address)
}
return loadFunction

case sema.AuthAccountCopyField:
return inter.authAccountCopyFunction(address)
if copyFunction == nil {
copyFunction = inter.authAccountCopyFunction(address)
}
return copyFunction

case sema.AuthAccountSaveField:
return inter.authAccountSaveFunction(address)
if saveFunction == nil {
saveFunction = inter.authAccountSaveFunction(address)
}
return saveFunction

case sema.AuthAccountBorrowField:
return inter.authAccountBorrowFunction(address)
if borrowFunction == nil {
borrowFunction = inter.authAccountBorrowFunction(address)
}
return borrowFunction

case sema.AuthAccountLinkField:
return inter.authAccountLinkFunction(address)
if linkFunction == nil {
linkFunction = inter.authAccountLinkFunction(address)
}
return linkFunction

case sema.AuthAccountUnlinkField:
return inter.authAccountUnlinkFunction(address)
if unlinkFunction == nil {
unlinkFunction = inter.authAccountUnlinkFunction(address)
}
return unlinkFunction

case sema.AuthAccountGetLinkTargetField:
return inter.accountGetLinkTargetFunction(address)
if getLinkTargetFunction == nil {
getLinkTargetFunction = inter.accountGetLinkTargetFunction(address)
}
return getLinkTargetFunction

}

return nil
Expand Down Expand Up @@ -181,6 +256,7 @@ func NewPublicAccountValue(

var keys Value
var contracts Value
var forEachPublicFunction *HostFunctionValue

computeField := func(name string, inter *Interpreter, locationRange LocationRange) Value {
switch name {
Expand All @@ -189,25 +265,44 @@ func NewPublicAccountValue(
keys = keysConstructor()
}
return keys

case sema.PublicAccountContractsField:
if contracts == nil {
contracts = contractsConstructor()
}
return contracts

case sema.PublicAccountPathsField:
return inter.publicAccountPaths(address, locationRange)

case sema.PublicAccountForEachPublicField:
return inter.newStorageIterationFunction(address, common.PathDomainPublic, sema.PublicPathType)
if forEachPublicFunction == nil {
forEachPublicFunction = inter.newStorageIterationFunction(
address,
common.PathDomainPublic,
sema.PublicPathType,
)
}
return forEachPublicFunction

case sema.PublicAccountBalanceField:
return accountBalanceGet()

case sema.PublicAccountAvailableBalanceField:
return accountAvailableBalanceGet()

case sema.PublicAccountStorageUsedField:
return storageUsedGet(inter)

case sema.PublicAccountStorageCapacityField:
return storageCapacityGet(inter)

case sema.PublicAccountGetTargetLinkField:
return inter.accountGetLinkTargetFunction(address)
var getLinkTargetFunction *HostFunctionValue
if getLinkTargetFunction == nil {
getLinkTargetFunction = inter.accountGetLinkTargetFunction(address)
}
return getLinkTargetFunction
}

return nil
Expand Down
4 changes: 3 additions & 1 deletion runtime/sema/checker.go
Original file line number Diff line number Diff line change
Expand Up @@ -1798,6 +1798,8 @@ func (checker *Checker) withSelfResourceInvalidationAllowed(f func()) {
const ResourceOwnerFieldName = "owner"
const ResourceUUIDFieldName = "uuid"

const ContractAccountFieldName = "account"

const contractAccountFieldDocString = `
The account where the contract is deployed in
`
Expand Down Expand Up @@ -1866,7 +1868,7 @@ func (checker *Checker) predeclaredMembers(containerType Type) []*Member {
// which is ignored in serialization

addPredeclaredMember(
"account",
ContractAccountFieldName,
AuthAccountType,
common.DeclarationKindField,
ast.AccessPrivate,
Expand Down
4 changes: 2 additions & 2 deletions runtime/sema/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -6170,7 +6170,7 @@ const capabilityTypeCheckFunctionDocString = `
Returns true if the capability currently targets an object that satisfies the given type, i.e. could be borrowed using the given type
`

const addressTypeCheckFunctionDocString = `
const capabilityTypeAddressFieldDocString = `
The address of the capability
`

Expand Down Expand Up @@ -6218,7 +6218,7 @@ func (t *CapabilityType) initializeMemberResolvers() {
t,
identifier,
&AddressType{},
addressTypeCheckFunctionDocString,
capabilityTypeAddressFieldDocString,
)
},
},
Expand Down