Skip to content

Commit

Permalink
Merge pull request #2811 from onflow/bastian/allow-type-injection
Browse files Browse the repository at this point in the history
  • Loading branch information
turbolent authored Sep 26, 2023
2 parents 1309c1d + 957de13 commit 10f50bc
Show file tree
Hide file tree
Showing 4 changed files with 429 additions and 21 deletions.
36 changes: 29 additions & 7 deletions runtime/environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ import (
type Environment interface {
ArgumentDecoder

Declare(valueDeclaration stdlib.StandardLibraryValue)
DeclareValue(valueDeclaration stdlib.StandardLibraryValue)
DeclareType(typeDeclaration stdlib.StandardLibraryType)
Configure(
runtimeInterface Interface,
codesAndPrograms CodesAndPrograms,
Expand Down Expand Up @@ -78,8 +79,9 @@ type interpreterEnvironmentReconfigured struct {

type interpreterEnvironment struct {
interpreterEnvironmentReconfigured
baseActivation *interpreter.VariableActivation
baseTypeActivation *sema.VariableActivation
baseValueActivation *sema.VariableActivation
baseActivation *interpreter.VariableActivation
InterpreterConfig *interpreter.Config
CheckerConfig *sema.Config
deployedContractConstructorInvocation *stdlib.DeployedContractConstructorInvocation
Expand Down Expand Up @@ -108,12 +110,14 @@ var _ common.MemoryGauge = &interpreterEnvironment{}

func newInterpreterEnvironment(config Config) *interpreterEnvironment {
baseValueActivation := sema.NewVariableActivation(sema.BaseValueActivation)
baseTypeActivation := sema.NewVariableActivation(sema.BaseTypeActivation)
baseActivation := activations.NewActivation[*interpreter.Variable](nil, interpreter.BaseActivation)

env := &interpreterEnvironment{
config: config,
baseActivation: baseActivation,
baseValueActivation: baseValueActivation,
baseTypeActivation: baseTypeActivation,
baseActivation: baseActivation,
stackDepthLimiter: newStackDepthLimiter(config.StackDepthLimit),
}
env.InterpreterConfig = env.newInterpreterConfig()
Expand Down Expand Up @@ -158,6 +162,7 @@ func (e *interpreterEnvironment) newCheckerConfig() *sema.Config {
return &sema.Config{
AccessCheckMode: sema.AccessCheckModeStrict,
BaseValueActivation: e.baseValueActivation,
BaseTypeActivation: e.baseTypeActivation,
ValidTopLevelDeclarationsHandler: validTopLevelDeclarations,
LocationHandler: e.newLocationHandler(),
ImportHandler: e.resolveImport,
Expand All @@ -171,15 +176,15 @@ func (e *interpreterEnvironment) newCheckerConfig() *sema.Config {
func NewBaseInterpreterEnvironment(config Config) *interpreterEnvironment {
env := newInterpreterEnvironment(config)
for _, valueDeclaration := range stdlib.DefaultStandardLibraryValues(env) {
env.Declare(valueDeclaration)
env.DeclareValue(valueDeclaration)
}
return env
}

func NewScriptInterpreterEnvironment(config Config) Environment {
env := newInterpreterEnvironment(config)
for _, valueDeclaration := range stdlib.DefaultScriptStandardLibraryValues(env) {
env.Declare(valueDeclaration)
env.DeclareValue(valueDeclaration)
}
return env
}
Expand All @@ -198,11 +203,15 @@ func (e *interpreterEnvironment) Configure(
e.stackDepthLimiter.depth = 0
}

func (e *interpreterEnvironment) Declare(valueDeclaration stdlib.StandardLibraryValue) {
func (e *interpreterEnvironment) DeclareValue(valueDeclaration stdlib.StandardLibraryValue) {
e.baseValueActivation.DeclareValue(valueDeclaration)
interpreter.Declare(e.baseActivation, valueDeclaration)
}

func (e *interpreterEnvironment) DeclareType(typeDeclaration stdlib.StandardLibraryType) {
e.baseTypeActivation.DeclareType(typeDeclaration)
}

func (e *interpreterEnvironment) NewAuthAccountValue(address interpreter.AddressValue) interpreter.Value {
return stdlib.NewAuthAccountValue(e, e, address)
}
Expand Down Expand Up @@ -888,8 +897,21 @@ func (e *interpreterEnvironment) newImportLocationHandler() interpreter.ImportLo

func (e *interpreterEnvironment) newCompositeTypeHandler() interpreter.CompositeTypeHandlerFunc {
return func(location common.Location, typeID common.TypeID) *sema.CompositeType {
if _, ok := location.(stdlib.FlowLocation); ok {

switch location.(type) {
case stdlib.FlowLocation:
return stdlib.FlowEventTypes[typeID]

case nil:
qualifiedIdentifier := string(typeID)
ty := sema.TypeActivationNestedType(e.baseTypeActivation, qualifiedIdentifier)
if ty == nil {
return nil
}

if compositeType, ok := ty.(*sema.CompositeType); ok {
return compositeType
}
}

return nil
Expand Down
18 changes: 10 additions & 8 deletions runtime/interpreter/interpreter.go
Original file line number Diff line number Diff line change
Expand Up @@ -4796,16 +4796,18 @@ func (interpreter *Interpreter) GetCompositeType(
if compositeType != nil {
return compositeType, nil
}
} else {
config := interpreter.SharedState.Config
compositeTypeHandler := config.CompositeTypeHandler
if compositeTypeHandler != nil {
compositeType = compositeTypeHandler(location, typeID)
if compositeType != nil {
return compositeType, nil
}
}

config := interpreter.SharedState.Config
compositeTypeHandler := config.CompositeTypeHandler
if compositeTypeHandler != nil {
compositeType = compositeTypeHandler(location, typeID)
if compositeType != nil {
return compositeType, nil
}
}

if location != nil {
compositeType = interpreter.getUserCompositeType(location, typeID)
if compositeType != nil {
return compositeType, nil
Expand Down
Loading

0 comments on commit 10f50bc

Please sign in to comment.