Class ScriptEnvironment
- java.lang.Object
-
- sleep.runtime.ScriptEnvironment
-
- All Implemented Interfaces:
java.io.Serializable
public class ScriptEnvironment extends java.lang.Object implements java.io.SerializableThis class contains methods for accessing the data stack, return value of a function, and the environment hashtable for a script. In sleep each ScriptInstance has a ScriptEnvironment object associated with it. Most of the functions in this class are used internally by sleep.
For the developers purposes, this class is your gateway into the runtime environment of a script.
If you use the evaluate methods to evaluate a snippet of code, they will be evaluated as if they were part of the script file that this ScriptEnvironment represents.
The Hashtable environment contains references for all of the loaded bridges this script has access to. Every function, predicate, and operator is specified in the environment hashtable. To force scripts to share this information use setEnvironment(Hashtable) and pass the same instance of Hashtable that other scripts are using.
This class is instantiated by sleep.runtime.ScriptInstance.
- See Also:
ScriptLoader,ScriptInstance, Serialized Form
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description protected static classScriptEnvironment.Contextprotected static classScriptEnvironment.ExceptionContext
-
Field Summary
Fields Modifier and Type Field Description protected java.util.Stackcontextprotected java.util.StackcontextStackprotected ScriptEnvironment.ExceptionContextcurrentHandlerprotected java.lang.StringdebugStringprotected java.util.Hashtableenvironmentthe environment hashtable that contains all of the functions, predicates, operators, and "environment keywords" this script has access to.protected java.util.StackenvironmentStackthe runtime data stack for this environmentprotected java.lang.ObjecterrorMessagestored error message...protected java.util.Stackexhandlersprotected intfindexstatic intFLOW_CONTROL_BREAKrequest a break out of the current loopstatic intFLOW_CONTROL_CALLCCadding a callcc keywordstatic intFLOW_CONTROL_CONTINUEadding a continue keyword as people keep demanding itstatic intFLOW_CONTROL_DEBUGa special case for debugs and suchstatic intFLOW_CONTROL_NONEcurrently no flow control change has been requestedstatic intFLOW_CONTROL_PASSa special case, pass control flow to the return value (it better be a function!)static intFLOW_CONTROL_RETURNrequest a return from the current functionstatic intFLOW_CONTROL_THROWadding a throw keyword -- sleep is now useable :)static intFLOW_CONTROL_YIELDadding a yield keywordprotected java.util.ArrayListframesprotected java.util.HashMapmetadataprotected java.util.StackmetaStackprotected booleanmoreHandlersprotected intrequestprotected Scalarrvprotected ScriptInstanceselfthe script instance that this is the environment forprotected java.util.Stacksourceshow many stacks does this damned class include?
-
Constructor Summary
Constructors Constructor Description ScriptEnvironment()Not recommended that you instantiate a script environment in this wayScriptEnvironment(java.util.Hashtable env, ScriptInstance myscript)Instantiate a new script environment with the specified environment (can be shared), and the specified ScriptInstance
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidaddToContext(Block b, Step s)ScalarcheckError()once an error is checked using this function, it is cleared, the orignal error message is returned as wellvoidcleanFrame(int mark)markFrame and cleanFrame are used to keep the sleep stack in good order after certain error conditionsvoidclearReturn()Clears the return value from the last executed function.voidCreateFrame()voidCreateFrame(java.util.Stack frame)ScalarevaluateExpression(java.lang.String code)evaluates an expressionScalarevaluateOldContext()ScalarevaluateParsedLiteral(java.lang.String code)evaluates the passed in code as if it was a sleep parsed literalbooleanevaluatePredicate(java.lang.String code)evaluates a predicate conditionScalarevaluateStatement(java.lang.String code)evaluate a full blown statement...voidflagError(java.lang.Object message)A utility for bridge writers to flag an error.voidflagReturn(Scalar value, int type_of_flow)flags a return value for this script environmentvoidFrameResult(Scalar value)kills the current frame and if there is a parent frame pushes the specified value on to itBlockgetBlock(java.lang.String name)java.lang.ObjectgetContextMetadata(java.lang.Object key)Returns the data associated with the particular key for this context.java.lang.ObjectgetContextMetadata(java.lang.Object key, java.lang.Object default_value)Returns the data associated with the particular key for this context.java.util.StackgetCurrentFrame()java.lang.StringgetCurrentSource()obtain the filename of the current source of executionjava.lang.StringgetDebugString()java.util.HashtablegetEnvironment()Returns the environment for this script.java.util.StackgetEnvironmentStack()returns the environment stack used for temporary calculations and such.BlockgetExceptionHandler()preps and returns the current exception handler...ScalargetExceptionMessage()if there is no handler, we'll just get the message which will clear the thrown message as wellFilterEnvironmentgetFilterEnvironment(java.lang.String env)intgetFlowControlRequest()FunctiongetFunction(java.lang.String func)EnvironmentgetFunctionEnvironment(java.lang.String env)OperatorgetOperator(java.lang.String oper)PredicategetPredicate(java.lang.String name)PredicateEnvironmentgetPredicateEnvironment(java.lang.String env)ScalargetReturnValue()ScalargetScalar(java.lang.String key)returns a scalar from this scripts environmentScriptInstancegetScriptInstance()returns a reference to the script associated with this environmentScriptVariablesgetScriptVariables()returns the variable manager for this scriptbooleanhasFrame()voidinstallExceptionHandler(Block owner, Block handler, java.lang.String varname)voidinstallExceptionHandler(ScriptEnvironment.ExceptionContext exc)booleanisCallCC()booleanisDebugInterrupt()booleanisExceptionHandlerInstalled()booleanisPassControl()booleanisResponsible(Block block)booleanisReturn()booleanisThrownValue()booleanisYield()voidKillFrame()voidloadContext(java.util.Stack _context, java.util.HashMap _metadata)intmarkFrame()markFrame and cleanFrame are used to keep the sleep stack in good order after certain error conditionsScriptEnvironment.ExceptionContextpopExceptionContext()voidpopSource()remove the latest source information from the source stackvoidpushSource(java.lang.String s)push source information onto the source stackvoidputScalar(java.lang.String key, Scalar value)puts a scalar into this scripts environment (global scope)voidresetEnvironment()Resets the script environment to include clearing the return of all flags (including thrown exceptions)java.util.StacksaveContext()voidsetContextMetadata(java.lang.Object key, java.lang.Object value)Use this function to save some meta data for this particular closure context, passing null for value will remove the key from the metadata for this context.voidsetEnvironment(java.util.Hashtable h)Sets the environment Hashtable this script is to use.voidshowDebugMessage(java.lang.String message)fires this debug message via a runtime warning complete with line number of current stepjava.lang.StringtoString()
-
-
-
Field Detail
-
self
protected ScriptInstance self
the script instance that this is the environment for
-
environmentStack
protected java.util.Stack environmentStack
the runtime data stack for this environment
-
environment
protected java.util.Hashtable environment
the environment hashtable that contains all of the functions, predicates, operators, and "environment keywords" this script has access to.
-
errorMessage
protected java.lang.Object errorMessage
stored error message...
-
context
protected java.util.Stack context
-
contextStack
protected java.util.Stack contextStack
-
metadata
protected java.util.HashMap metadata
-
metaStack
protected java.util.Stack metaStack
-
currentHandler
protected ScriptEnvironment.ExceptionContext currentHandler
-
exhandlers
protected java.util.Stack exhandlers
-
moreHandlers
protected boolean moreHandlers
-
FLOW_CONTROL_NONE
public static final int FLOW_CONTROL_NONE
currently no flow control change has been requested- See Also:
- Constant Field Values
-
FLOW_CONTROL_RETURN
public static final int FLOW_CONTROL_RETURN
request a return from the current function- See Also:
- Constant Field Values
-
FLOW_CONTROL_BREAK
public static final int FLOW_CONTROL_BREAK
request a break out of the current loop- See Also:
- Constant Field Values
-
FLOW_CONTROL_CONTINUE
public static final int FLOW_CONTROL_CONTINUE
adding a continue keyword as people keep demanding it- See Also:
- Constant Field Values
-
FLOW_CONTROL_YIELD
public static final int FLOW_CONTROL_YIELD
adding a yield keyword- See Also:
- Constant Field Values
-
FLOW_CONTROL_THROW
public static final int FLOW_CONTROL_THROW
adding a throw keyword -- sleep is now useable :)- See Also:
- Constant Field Values
-
FLOW_CONTROL_DEBUG
public static final int FLOW_CONTROL_DEBUG
a special case for debugs and such- See Also:
- Constant Field Values
-
FLOW_CONTROL_CALLCC
public static final int FLOW_CONTROL_CALLCC
adding a callcc keyword- See Also:
- Constant Field Values
-
FLOW_CONTROL_PASS
public static final int FLOW_CONTROL_PASS
a special case, pass control flow to the return value (it better be a function!)- See Also:
- Constant Field Values
-
debugString
protected java.lang.String debugString
-
rv
protected Scalar rv
-
request
protected int request
-
sources
protected java.util.Stack sources
how many stacks does this damned class include?
-
frames
protected java.util.ArrayList frames
-
findex
protected int findex
-
-
Constructor Detail
-
ScriptEnvironment
public ScriptEnvironment()
Not recommended that you instantiate a script environment in this way
-
ScriptEnvironment
public ScriptEnvironment(java.util.Hashtable env, ScriptInstance myscript)Instantiate a new script environment with the specified environment (can be shared), and the specified ScriptInstance
-
-
Method Detail
-
getScriptInstance
public ScriptInstance getScriptInstance()
returns a reference to the script associated with this environment
-
flagError
public void flagError(java.lang.Object message)
A utility for bridge writers to flag an error. flags an error that script writers can then check for with checkError(). Currently used by the IO bridge openf, exec, and connect functions. Major errors should bubble up as exceptions. Small stuff like being unable to open a certain file should be flagged this way.
-
checkError
public Scalar checkError()
once an error is checked using this function, it is cleared, the orignal error message is returned as well
-
getScriptVariables
public ScriptVariables getScriptVariables()
returns the variable manager for this script
-
getScalar
public Scalar getScalar(java.lang.String key)
returns a scalar from this scripts environment
-
putScalar
public void putScalar(java.lang.String key, Scalar value)puts a scalar into this scripts environment (global scope)
-
getBlock
public Block getBlock(java.lang.String name)
-
getFunction
public Function getFunction(java.lang.String func)
-
getFunctionEnvironment
public Environment getFunctionEnvironment(java.lang.String env)
-
getPredicateEnvironment
public PredicateEnvironment getPredicateEnvironment(java.lang.String env)
-
getFilterEnvironment
public FilterEnvironment getFilterEnvironment(java.lang.String env)
-
getPredicate
public Predicate getPredicate(java.lang.String name)
-
getOperator
public Operator getOperator(java.lang.String oper)
-
getEnvironment
public java.util.Hashtable getEnvironment()
Returns the environment for this script. The environment has the following formats for keys: &[keyname] - a sleep function -[keyname] - assumed to be a unary predicate [keyname] - assumed to be an environment binding, predicate, or operator
-
setEnvironment
public void setEnvironment(java.util.Hashtable h)
Sets the environment Hashtable this script is to use. Sharing an instance of this Hashtable allows scripts to share common environment data like functions, subroutines, etc. Also useful for bridge writers as their information can be stored in this hashtable as well
-
getEnvironmentStack
public java.util.Stack getEnvironmentStack()
returns the environment stack used for temporary calculations and such.
-
toString
public java.lang.String toString()
- Overrides:
toStringin classjava.lang.Object
-
loadContext
public void loadContext(java.util.Stack _context, java.util.HashMap _metadata)
-
setContextMetadata
public void setContextMetadata(java.lang.Object key, java.lang.Object value)Use this function to save some meta data for this particular closure context, passing null for value will remove the key from the metadata for this context. Note: context metadata is *not* serialized when the closure is serialized.
-
getContextMetadata
public java.lang.Object getContextMetadata(java.lang.Object key)
Returns the data associated with the particular key for this context.
-
getContextMetadata
public java.lang.Object getContextMetadata(java.lang.Object key, java.lang.Object default_value)Returns the data associated with the particular key for this context. If the key value is null then the specified default_value is returned
-
evaluateOldContext
public Scalar evaluateOldContext()
-
saveContext
public java.util.Stack saveContext()
-
isExceptionHandlerInstalled
public boolean isExceptionHandlerInstalled()
-
isResponsible
public boolean isResponsible(Block block)
-
installExceptionHandler
public void installExceptionHandler(ScriptEnvironment.ExceptionContext exc)
-
installExceptionHandler
public void installExceptionHandler(Block owner, Block handler, java.lang.String varname)
-
getExceptionMessage
public Scalar getExceptionMessage()
if there is no handler, we'll just get the message which will clear the thrown message as well
-
getExceptionHandler
public Block getExceptionHandler()
preps and returns the current exception handler...
-
popExceptionContext
public ScriptEnvironment.ExceptionContext popExceptionContext()
-
isThrownValue
public boolean isThrownValue()
-
isDebugInterrupt
public boolean isDebugInterrupt()
-
isYield
public boolean isYield()
-
isCallCC
public boolean isCallCC()
-
isPassControl
public boolean isPassControl()
-
getReturnValue
public Scalar getReturnValue()
-
isReturn
public boolean isReturn()
-
getFlowControlRequest
public int getFlowControlRequest()
-
getDebugString
public java.lang.String getDebugString()
-
showDebugMessage
public void showDebugMessage(java.lang.String message)
fires this debug message via a runtime warning complete with line number of current step
-
flagReturn
public void flagReturn(Scalar value, int type_of_flow)
flags a return value for this script environment
-
resetEnvironment
public void resetEnvironment()
Resets the script environment to include clearing the return of all flags (including thrown exceptions)
-
clearReturn
public void clearReturn()
Clears the return value from the last executed function.
-
pushSource
public void pushSource(java.lang.String s)
push source information onto the source stack
-
getCurrentSource
public java.lang.String getCurrentSource()
obtain the filename of the current source of execution
-
popSource
public void popSource()
remove the latest source information from the source stack
-
markFrame
public int markFrame()
markFrame and cleanFrame are used to keep the sleep stack in good order after certain error conditions
-
cleanFrame
public void cleanFrame(int mark)
markFrame and cleanFrame are used to keep the sleep stack in good order after certain error conditions
-
getCurrentFrame
public java.util.Stack getCurrentFrame()
-
FrameResult
public void FrameResult(Scalar value)
kills the current frame and if there is a parent frame pushes the specified value on to it
-
hasFrame
public boolean hasFrame()
-
KillFrame
public void KillFrame()
-
CreateFrame
public void CreateFrame(java.util.Stack frame)
-
CreateFrame
public void CreateFrame()
-
evaluateStatement
public Scalar evaluateStatement(java.lang.String code) throws YourCodeSucksException
evaluate a full blown statement... probably best to just load a script at this point- Throws:
YourCodeSucksException
-
evaluatePredicate
public boolean evaluatePredicate(java.lang.String code) throws YourCodeSucksExceptionevaluates a predicate condition- Throws:
YourCodeSucksException
-
evaluateExpression
public Scalar evaluateExpression(java.lang.String code) throws YourCodeSucksException
evaluates an expression- Throws:
YourCodeSucksException
-
evaluateParsedLiteral
public Scalar evaluateParsedLiteral(java.lang.String code) throws YourCodeSucksException
evaluates the passed in code as if it was a sleep parsed literal- Throws:
YourCodeSucksException
-
-