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

Add support for injecting types into the environment #2811

Merged
merged 2 commits into from
Sep 26, 2023
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
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
Loading