Skip to content

Commit

Permalink
Merge pull request #2125 from onflow/bastian/more-view-functions
Browse files Browse the repository at this point in the history
  • Loading branch information
turbolent authored Nov 7, 2022
2 parents ce3978a + 46069fb commit 3394b66
Show file tree
Hide file tree
Showing 12 changed files with 78 additions and 55 deletions.
9 changes: 3 additions & 6 deletions runtime/interpreter/interpreter.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ import (

//

var emptyFunctionType = &sema.FunctionType{
var emptyImpureFunctionType = &sema.FunctionType{
Purity: sema.FunctionPurityImpure,
ReturnTypeAnnotation: &sema.TypeAnnotation{
Type: sema.VoidType,
Expand Down Expand Up @@ -1407,7 +1407,7 @@ func (interpreter *Interpreter) compositeDestructorFunction(
return NewInterpretedFunctionValue(
interpreter,
nil,
emptyFunctionType,
emptyImpureFunctionType,
lexicalScope,
beforeStatements,
preConditions,
Expand Down Expand Up @@ -3059,10 +3059,7 @@ var typeFunction = NewUnmeteredHostFunctionValue(
staticType := ConvertSemaToStaticType(invocation.Interpreter, ty)
return NewTypeValue(invocation.Interpreter, staticType)
},
&sema.FunctionType{
Purity: sema.FunctionPurityView,
ReturnTypeAnnotation: sema.NewTypeAnnotation(sema.MetaType),
},
sema.MetaTypeFunctionType,
)

func defineTypeFunction(activation *VariableActivation) {
Expand Down
7 changes: 1 addition & 6 deletions runtime/interpreter/value.go
Original file line number Diff line number Diff line change
Expand Up @@ -2729,12 +2729,7 @@ func getNumberValueMember(interpreter *Interpreter, v NumberValue, name string,
v.ToBigEndianBytes(),
)
},
&sema.FunctionType{
Purity: sema.FunctionPurityView,
ReturnTypeAnnotation: sema.NewTypeAnnotation(
sema.ByteArrayType,
),
},
sema.ToBigEndianBytesFunctionType,
)

case sema.NumericTypeSaturatingAddFunctionName:
Expand Down
3 changes: 3 additions & 0 deletions runtime/sema/authaccount_contracts.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ Returns the deployed contract.
`

var AuthAccountContractsTypeAddFunctionType = &FunctionType{
Purity: FunctionPurityImpure,
Parameters: []*Parameter{
{
Identifier: "name",
Expand Down Expand Up @@ -142,6 +143,7 @@ Returns the deployed contract for the updated contract.
`

var AuthAccountContractsTypeUpdateExperimentalFunctionType = &FunctionType{
Purity: FunctionPurityImpure,
Parameters: []*Parameter{
{
Identifier: "name",
Expand Down Expand Up @@ -193,6 +195,7 @@ Returns nil if no contract/contract interface with the given name exists in the
`

var AuthAccountContractsTypeRemoveFunctionType = &FunctionType{
Purity: FunctionPurityImpure,
Parameters: []*Parameter{
{
Identifier: "name",
Expand Down
12 changes: 12 additions & 0 deletions runtime/sema/authaccount_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,7 @@ var AuthAccountTypeSaveFunctionType = func() *FunctionType {
}

return &FunctionType{
Purity: FunctionPurityImpure,
TypeParameters: []*TypeParameter{
typeParameter,
},
Expand Down Expand Up @@ -326,6 +327,7 @@ var AuthAccountTypeLoadFunctionType = func() *FunctionType {
}

return &FunctionType{
Purity: FunctionPurityImpure,
TypeParameters: []*TypeParameter{
typeParameter,
},
Expand Down Expand Up @@ -523,6 +525,7 @@ The link is latent. The target value might be stored after the link is created,
`

var AuthAccountTypeUnlinkFunctionType = &FunctionType{
Purity: FunctionPurityImpure,
Parameters: []*Parameter{
{
Label: ArgumentLabelNotRequired,
Expand Down Expand Up @@ -637,6 +640,7 @@ var AuthAccountKeysType = func() *CompositeType {
}()

var AuthAccountKeysTypeAddFunctionType = &FunctionType{
Purity: FunctionPurityImpure,
Parameters: []*Parameter{
{
Identifier: AccountKeyPublicKeyField,
Expand Down Expand Up @@ -669,8 +673,11 @@ var AccountKeysTypeGetFunctionType = &FunctionType{

// fun keys.forEach(_ function: ((AccountKey): Bool)): Void
var AccountKeysTypeForEachFunctionType = func() *FunctionType {
const functionPurity = FunctionPurityImpure

// ((AccountKey): Bool)
iterFunctionType := &FunctionType{
Purity: functionPurity,
Parameters: []*Parameter{
{
TypeAnnotation: NewTypeAnnotation(AccountKeyType),
Expand All @@ -680,6 +687,7 @@ var AccountKeysTypeForEachFunctionType = func() *FunctionType {
}

return &FunctionType{
Purity: functionPurity,
Parameters: []*Parameter{
{
Label: ArgumentLabelNotRequired,
Expand All @@ -694,6 +702,7 @@ var AccountKeysTypeForEachFunctionType = func() *FunctionType {
var AccountKeysTypeCountFieldType = UInt64Type

var AuthAccountKeysTypeRevokeFunctionType = &FunctionType{
Purity: FunctionPurityImpure,
Parameters: []*Parameter{
{
Identifier: AccountKeyKeyIndexField,
Expand Down Expand Up @@ -774,6 +783,7 @@ Publishes the argument value under the given name, to be later claimed by the sp
`

var AuthAccountTypeInboxPublishFunctionType = &FunctionType{
Purity: FunctionPurityImpure,
Parameters: []*Parameter{
{
Label: ArgumentLabelNotRequired,
Expand Down Expand Up @@ -806,6 +816,7 @@ var AuthAccountTypeInboxUnpublishFunctionType = func() *FunctionType {
},
}
return &FunctionType{
Purity: FunctionPurityImpure,
TypeParameters: []*TypeParameter{
typeParameter,
},
Expand Down Expand Up @@ -840,6 +851,7 @@ var AuthAccountTypeInboxClaimFunctionType = func() *FunctionType {
},
}
return &FunctionType{
Purity: FunctionPurityImpure,
TypeParameters: []*TypeParameter{
typeParameter,
},
Expand Down
8 changes: 6 additions & 2 deletions runtime/sema/checker.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,10 @@ var _ ast.DeclarationVisitor[struct{}] = &Checker{}
var _ ast.StatementVisitor[struct{}] = &Checker{}
var _ ast.ExpressionVisitor[Type] = &Checker{}

var baseFunctionType = &FunctionType{
ReturnTypeAnnotation: NewTypeAnnotation(VoidType),
}

func NewChecker(
program *ast.Program,
location common.Location,
Expand All @@ -145,8 +149,8 @@ func NewChecker(
}

functionActivations := &FunctionActivations{}
functionActivations.EnterFunction(&FunctionType{
ReturnTypeAnnotation: NewTypeAnnotation(VoidType)},
functionActivations.EnterFunction(
baseFunctionType,
0,
)

Expand Down
4 changes: 4 additions & 0 deletions runtime/sema/publicaccount_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,10 @@ All the public paths of an account
`

func AccountForEachFunctionType(pathType Type) *FunctionType {
const functionPurity = FunctionPurityImpure

iterFunctionType := &FunctionType{
Purity: functionPurity,
Parameters: []*Parameter{
{
Label: ArgumentLabelNotRequired,
Expand All @@ -150,6 +153,7 @@ func AccountForEachFunctionType(pathType Type) *FunctionType {
ReturnTypeAnnotation: NewTypeAnnotation(BoolType),
}
return &FunctionType{
Purity: functionPurity,
Parameters: []*Parameter{
{
Label: ArgumentLabelNotRequired,
Expand Down
5 changes: 5 additions & 0 deletions runtime/sema/runtime_type_constructors.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ type RuntimeTypeConstructor struct {
DocString string
}

var MetaTypeFunctionType = &FunctionType{
Purity: FunctionPurityView,
ReturnTypeAnnotation: NewTypeAnnotation(MetaType),
}

var OptionalTypeFunctionType = &FunctionType{
Purity: FunctionPurityView,
Parameters: []*Parameter{
Expand Down
30 changes: 17 additions & 13 deletions runtime/sema/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,7 @@ func FromStringFunctionDocstring(ty Type) string {

func FromStringFunctionType(ty Type) *FunctionType {
return &FunctionType{
Purity: FunctionPurityView,
Parameters: []*Parameter{
{
Label: ArgumentLabelNotRequired,
Expand All @@ -409,7 +410,7 @@ func FromStringFunctionType(ty Type) *FunctionType {

const ToBigEndianBytesFunctionName = "toBigEndianBytes"

var toBigEndianBytesFunctionType = &FunctionType{
var ToBigEndianBytesFunctionType = &FunctionType{
Purity: FunctionPurityView,
ReturnTypeAnnotation: NewTypeAnnotation(
ByteArrayType,
Expand Down Expand Up @@ -484,7 +485,7 @@ func withBuiltinMembers(ty Type, members map[string]MemberResolver) map[string]M
memoryGauge,
ty,
identifier,
toBigEndianBytesFunctionType,
ToBigEndianBytesFunctionType,
toBigEndianBytesFunctionDocString,
)
},
Expand Down Expand Up @@ -661,8 +662,10 @@ func OptionalTypeMapFunctionType(typ Type) *FunctionType {
TypeParameter: typeParameter,
}

const functionPurity = FunctionPurityImpure

return &FunctionType{
Purity: FunctionPurityImpure,
Purity: functionPurity,
TypeParameters: []*TypeParameter{
typeParameter,
},
Expand All @@ -672,7 +675,7 @@ func OptionalTypeMapFunctionType(typ Type) *FunctionType {
Identifier: "transform",
TypeAnnotation: NewTypeAnnotation(
&FunctionType{
Purity: FunctionPurityImpure,
Purity: functionPurity,
Parameters: []*Parameter{
{
Label: ArgumentLabelNotRequired,
Expand Down Expand Up @@ -4636,9 +4639,7 @@ func DictionaryRemoveFunctionType(t *DictionaryType) *FunctionType {
}

func DictionaryForEachKeyFunctionType(t *DictionaryType) *FunctionType {
// fun forEachKey(_ function: ((K): Bool)): Void

// funcType: K -> Bool
// ((K): Bool)
funcType := &FunctionType{
Parameters: []*Parameter{
{
Expand All @@ -4649,6 +4650,7 @@ func DictionaryForEachKeyFunctionType(t *DictionaryType) *FunctionType {
ReturnTypeAnnotation: NewTypeAnnotation(BoolType),
}

// fun forEachKey(_ function: ((K): Bool)): Void
return &FunctionType{
Parameters: []*Parameter{
{
Expand Down Expand Up @@ -5719,13 +5721,15 @@ func (t *TransactionType) PrepareFunctionType() *FunctionType {
}
}

var transactionTypeExecuteFunctionType = &FunctionType{
Purity: FunctionPurityImpure,
IsConstructor: true,
Parameters: []*Parameter{},
ReturnTypeAnnotation: NewTypeAnnotation(VoidType),
}

func (*TransactionType) ExecuteFunctionType() *FunctionType {
return &FunctionType{
Purity: FunctionPurityImpure,
IsConstructor: true,
Parameters: []*Parameter{},
ReturnTypeAnnotation: NewTypeAnnotation(VoidType),
}
return transactionTypeExecuteFunctionType
}

func (*TransactionType) IsType() {}
Expand Down
2 changes: 2 additions & 0 deletions runtime/stdlib/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ Creates a new account, paid by the given existing account
`

var authAccountFunctionType = &sema.FunctionType{
Purity: sema.FunctionPurityImpure,
Parameters: []*sema.Parameter{
{
Identifier: "payer",
Expand Down Expand Up @@ -1782,6 +1783,7 @@ Returns the public account for the given address
`

var getAccountFunctionType = &sema.FunctionType{
Purity: sema.FunctionPurityView,
Parameters: []*sema.Parameter{
{
Label: sema.ArgumentLabelNotRequired,
Expand Down
1 change: 1 addition & 0 deletions runtime/stdlib/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ Returns the current block, i.e. the block which contains the currently executed
`

var getCurrentBlockFunctionType = &sema.FunctionType{
Purity: sema.FunctionPurityView,
ReturnTypeAnnotation: sema.NewTypeAnnotation(
sema.BlockType,
),
Expand Down
1 change: 1 addition & 0 deletions runtime/stdlib/random.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ Follow best practices to prevent security issues when using this function
`

var unsafeRandomFunctionType = &sema.FunctionType{
Purity: sema.FunctionPurityImpure,
ReturnTypeAnnotation: sema.NewTypeAnnotation(
sema.UInt64Type,
),
Expand Down
Loading

0 comments on commit 3394b66

Please sign in to comment.