Environment

Holds the variables and consts of a script stack frame. The global environment can be accessed by climbing the Environment.parent chain until reaching the Environment whose parent is null. This allows native functions to define local and global variables. Note that calling a native function does not create a stack frame so one could write a native function that adds local variables to the stack frame where it was called.

Constructors

this
this(Environment par, string nam)

Constructs a new Environment. This constructor cannot be used to create the global Environment.

this
this(Interpreter interpreter)

Constructs a global environment

Members

Functions

declareVariableOrConst
bool declareVariableOrConst(string nam, ScriptAny value, bool isConst)

Attempt to declare and assign a new variable in the current environment. Returns false if it already exists.

depth
size_t depth()

Returns the depth from this Environment to the root Environment

forceRemoveVarOrConst
void forceRemoveVarOrConst(string name)

Forces the removal of a const or variable in the current environment.

forceSetVarOrConst
void forceSetVarOrConst(string name, ScriptAny value, bool isConst)

Force sets a variable or const no matter if the variable was declared already or is const. This is used by the host application to set globals or locals.

g
Environment g()

climb environment stack until finding one without a parent

interpreter
Interpreter interpreter()

Retrieves the interpreter object from the top level environment

lookupVariableOrConst
ScriptAny* lookupVariableOrConst(string varName, bool isConst)

Attempts to look up existing variable or const throughout the stack. If found, returns a pointer to the variable location, and if it is const, sets isConst to true. Note, this pointer should not be stored by native functions because the variable table may be modified between function calls.

name
string name()

returns the name of the Environment

parent
Environment parent()

returns the parent Environment

reassignVariable
ScriptAny* reassignVariable(string name, ScriptAny newValue, bool failedBecauseConst)

Attempts to reassign a variable anywhere in the stack and returns a pointer to the variable or null if the variable doesn't exist or is const. If the failure is due to const, failedBecauseConst is set to true. Note: this pointer should not be stored by native functions due to modifications to the variable table that may invalidate it and result in undefined behavior.

toString
string toString()

Returns a string representing the type and name

unsetVariable
void unsetVariable(string name)

Removes a variable from anywhere on the Environment stack it is located. This function cannot be used to unset consts.

varDepth
int varDepth(string varName, bool isConst)

Returns the level of depth, relative to this Environment, 0-N for a variable location, or -1 if not found

variableOrConstExists
bool variableOrConstExists(string name)

Searches the entire Environment stack for a variable starting with the current environment and climbing the parent chain.

Meta