Package sleep.runtime
Class ScriptVariables
- java.lang.Object
-
- sleep.runtime.ScriptVariables
-
- All Implemented Interfaces:
java.io.Serializable
public class ScriptVariables extends java.lang.Object implements java.io.Serializable
Maintains variables and variable scopes for a script instance. If you want to change the way variables are handled do not override this class. This class handles all accessing of variables through an object that implements the Variable interface.Set/Get a Variable without Parsing
script.getScriptVariables().putScalar("$var", SleepUtils.getScalar("value"));
The ScriptVariables object is the entry point for installing variables into a script's runtime environment. The above example illustrates how to set a variable named $var to a specified Scalar value.
Scalar value = script.getScriptVariables().getScalar("$var");
The code above illustrates how to retrieve a Scalar named $var from a script instance object.
Sleep has 3 levels of scope. They are (in order of precedence):
- Local - discarded after use
- Closure - specific to the current executing closure
- Global - global to all scripts sharing this script variables instance
- See Also:
Scalar
,ScriptInstance
,Variable
, Serialized Form
-
-
Constructor Summary
Constructors Constructor Description ScriptVariables()
Initializes this ScriptVariables container using a DefaultVariable object for default variable storageScriptVariables(Variable aVariableClass)
Initializes this class with your version of variable storage
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
beginToplevel(java.util.LinkedList l)
called when a closure is entered, allows an old stack of local scopes to be restored easilyVariable
getClosureVariables()
returns the current closure variable scopeVariable
getClosureVariables(SleepClosure closure)
returns the closure level variables for this specific script environmentVariable
getGlobalVariables()
returns the global variable scopeVariable
getLocalVariables()
returns the current local variable scopeScalar
getScalar(java.lang.String key)
retrieves a scalarScalar
getScalar(java.lang.String key, ScriptInstance i)
Returns the specified scalar, looking at each scope in order.Variable
getScalarLevel(java.lang.String key, ScriptInstance i)
retrieves the appropriate Variable container that has the specified key.boolean
haveMoreLocals()
used to check if other local scopes exist after the next popjava.util.LinkedList
leaveToplevel()
called when a closure is exited, returns local var scope for later restoration if desiredvoid
popClosureLevel()
discards the current closure variable scopevoid
popLocalLevel()
discards the current local variable scope, making the previous local scope the current local scope againvoid
pushClosureLevel(Variable variables)
pushes the specified variables into this closures level, once the closure has executed this should be poppedvoid
pushLocalLevel()
starts a new local variable scope.void
pushLocalLevel(Variable localVariables)
makes the specified variable container active for the local scope.void
putScalar(java.lang.String key, Scalar value)
puts a scalar into the global scopevoid
setClosureVariables(SleepClosure closure, Variable variables)
returns the closure level variables for this specific script environmentvoid
setScalarLevel(java.lang.String key, Scalar value, Variable level)
Puts the specified scalar in a specific scope
-
-
-
Field Detail
-
global
protected Variable global
-
closure
protected java.util.LinkedList closure
-
locals
protected java.util.LinkedList locals
-
marks
protected java.util.Stack marks
-
-
Constructor Detail
-
ScriptVariables
public ScriptVariables()
Initializes this ScriptVariables container using a DefaultVariable object for default variable storage
-
ScriptVariables
public ScriptVariables(Variable aVariableClass)
Initializes this class with your version of variable storage
-
-
Method Detail
-
beginToplevel
public void beginToplevel(java.util.LinkedList l)
called when a closure is entered, allows an old stack of local scopes to be restored easily
-
leaveToplevel
public java.util.LinkedList leaveToplevel()
called when a closure is exited, returns local var scope for later restoration if desired
-
haveMoreLocals
public boolean haveMoreLocals()
used to check if other local scopes exist after the next pop
-
putScalar
public void putScalar(java.lang.String key, Scalar value)
puts a scalar into the global scope
-
getScalar
public Scalar getScalar(java.lang.String key)
retrieves a scalar
-
getScalarLevel
public Variable getScalarLevel(java.lang.String key, ScriptInstance i)
retrieves the appropriate Variable container that has the specified key. Precedence is in the order of the current local variable container, the script specific container, and then the global container
-
getScalar
public Scalar getScalar(java.lang.String key, ScriptInstance i)
Returns the specified scalar, looking at each scope in order. It is worth noting that only one local variable level is qeuried. If a variable is not local, the previous local scope is not checked.
-
setScalarLevel
public void setScalarLevel(java.lang.String key, Scalar value, Variable level)
Puts the specified scalar in a specific scope- Parameters:
level
- the Variable container from the scope we want to store this scalar in.
-
getLocalVariables
public Variable getLocalVariables()
returns the current local variable scope
-
getClosureVariables
public Variable getClosureVariables()
returns the current closure variable scope
-
getGlobalVariables
public Variable getGlobalVariables()
returns the global variable scope
-
getClosureVariables
public Variable getClosureVariables(SleepClosure closure)
returns the closure level variables for this specific script environment
-
setClosureVariables
public void setClosureVariables(SleepClosure closure, Variable variables)
returns the closure level variables for this specific script environment
-
pushClosureLevel
public void pushClosureLevel(Variable variables)
pushes the specified variables into this closures level, once the closure has executed this should be popped
-
popClosureLevel
public void popClosureLevel()
discards the current closure variable scope
-
pushLocalLevel
public void pushLocalLevel(Variable localVariables)
makes the specified variable container active for the local scope. once the code that is using this has finished, it really should be popped.
-
pushLocalLevel
public void pushLocalLevel()
starts a new local variable scope. once the code that is using this has finished, it should be popped
-
popLocalLevel
public void popLocalLevel()
discards the current local variable scope, making the previous local scope the current local scope again
-
-