sleep.runtime

Class ScriptEnvironment

public class ScriptEnvironment extends Object implements Serializable

This 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

Nested Class Summary
protected static classScriptEnvironment.Context
protected static classScriptEnvironment.ExceptionContext
Field Summary
protected Stackcontext
protected StackcontextStack
protected ScriptEnvironment.ExceptionContextcurrentHandler
protected StringdebugString
protected Hashtableenvironment
the environment hashtable that contains all of the functions, predicates, operators, and "environment keywords" this script has access to.
protected StackenvironmentStack
the runtime data stack for this environment
protected ObjecterrorMessage
stored error message...
protected Stackexhandlers
protected intfindex
protected ArrayListframes
static intFLOW_CONTROL_BREAK
request a break out of the current loop
static intFLOW_CONTROL_CONTINUE
adding a continue keyword as people keep demanding it
static intFLOW_CONTROL_DEBUG
a special case for debugs and such
static intFLOW_CONTROL_NONE
currently no flow control change has been requested
static intFLOW_CONTROL_RETURN
request a return from the current function
static intFLOW_CONTROL_THROW
adding a throw keyword -- sleep is now useable :)
static intFLOW_CONTROL_YIELD
adding a yield keyword
protected HashMapmetadata
protected StackmetaStack
protected booleanmoreHandlers
protected intrequest
protected Scalarrv
protected ScriptInstanceself
the script instance that this is the environment for
protected Stacksources
how many stacks does this damned class include?
Constructor Summary
ScriptEnvironment()
Not recommended that you instantiate a script environment in this way
ScriptEnvironment(Hashtable env, ScriptInstance myscript)
Instantiate a new script environment with the specified environment (can be shared), and the specified ScriptInstance
Method Summary
voidaddToContext(Block b, Step s)
ScalarcheckError()
once an error is checked using this function, it is cleared, the orignal error message is returned as well
voidclearReturn()
Clears the return value from the last executed function.
voidCreateFrame()
ScalarevaluateExpression(String code)
evaluates an expression
ScalarevaluateOldContext()
ScalarevaluateParsedLiteral(String code)
evaluates the passed in code as if it was a sleep parsed literal
booleanevaluatePredicate(String code)
evaluates a predicate condition
ScalarevaluateStatement(String code)
evaluate a full blown statement... probably best to just load a script at this point
voidflagError(Object message)
A utility for bridge writers to flag an error. flags an error that script writers can then check for with checkError().
voidflagReturn(Scalar value, int type_of_flow)
voidFrameResult(Scalar value)
kills the current frame and if there is a parent frame pushes the specified value on to it
ObjectgetContextMetadata(Object key)
Returns the data associated with the particular key for this context.
ObjectgetContextMetadata(Object key, Object default_value)
Returns the data associated with the particular key for this context.
StackgetCurrentFrame()
StringgetCurrentSource()
obtain the filename of the current source of execution
StringgetDebugString()
HashtablegetEnvironment()
Returns the environment for this script.
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 well
FilterEnvironmentgetFilterEnvironment(String env)
intgetFlowControlRequest()
FunctiongetFunction(String func)
EnvironmentgetFunctionEnvironment(String env)
OperatorgetOperator(String oper)
PredicategetPredicate(String name)
PredicateEnvironmentgetPredicateEnvironment(String env)
ScalargetReturnValue()
ScalargetScalar(String key)
returns a scalar from this scripts environment
ScriptInstancegetScriptInstance()
returns a reference to the script associated with this environment
ScriptVariablesgetScriptVariables()
returns the variable manager for this script
booleanhasFrame()
voidinstallExceptionHandler(ScriptEnvironment.ExceptionContext exc)
voidinstallExceptionHandler(Block owner, Block handler, String varname)
booleanisDebugInterrupt()
booleanisExceptionHandlerInstalled()
booleanisResponsible(Block block)
booleanisReturn()
booleanisThrownValue()
voidKillFrame()
voidloadContext(Stack _context, HashMap _metadata)
ScriptEnvironment.ExceptionContextpopExceptionContext()
voidpopSource()
remove the latest source information from the source stack
voidpushSource(String s)
push source information onto the source stack
voidputScalar(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)
StacksaveContext()
voidsetContextMetadata(Object key, 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(Hashtable h)
Sets the environment Hashtable this script is to use.
voidshowDebugMessage(String message)
fires this debug message via a runtime warning complete with line number of current step
StringtoString()

Field Detail

context

protected Stack context

contextStack

protected Stack contextStack

currentHandler

protected ScriptEnvironment.ExceptionContext currentHandler

debugString

protected String debugString

environment

protected Hashtable environment
the environment hashtable that contains all of the functions, predicates, operators, and "environment keywords" this script has access to.

environmentStack

protected Stack environmentStack
the runtime data stack for this environment

errorMessage

protected Object errorMessage
stored error message...

exhandlers

protected Stack exhandlers

findex

protected int findex

frames

protected ArrayList frames

FLOW_CONTROL_BREAK

public static final int FLOW_CONTROL_BREAK
request a break out of the current loop

FLOW_CONTROL_CONTINUE

public static final int FLOW_CONTROL_CONTINUE
adding a continue keyword as people keep demanding it

FLOW_CONTROL_DEBUG

public static final int FLOW_CONTROL_DEBUG
a special case for debugs and such

FLOW_CONTROL_NONE

public static final int FLOW_CONTROL_NONE
currently no flow control change has been requested

FLOW_CONTROL_RETURN

public static final int FLOW_CONTROL_RETURN
request a return from the current function

FLOW_CONTROL_THROW

public static final int FLOW_CONTROL_THROW
adding a throw keyword -- sleep is now useable :)

FLOW_CONTROL_YIELD

public static final int FLOW_CONTROL_YIELD
adding a yield keyword

metadata

protected HashMap metadata

metaStack

protected Stack metaStack

moreHandlers

protected boolean moreHandlers

request

protected int request

rv

protected Scalar rv

self

protected ScriptInstance self
the script instance that this is the environment for

sources

protected Stack sources
how many stacks does this damned class include?

Constructor Detail

ScriptEnvironment

public ScriptEnvironment()
Not recommended that you instantiate a script environment in this way

ScriptEnvironment

public ScriptEnvironment(Hashtable env, ScriptInstance myscript)
Instantiate a new script environment with the specified environment (can be shared), and the specified ScriptInstance

Method Detail

addToContext

public void addToContext(Block b, Step s)

checkError

public Scalar checkError()
once an error is checked using this function, it is cleared, the orignal error message is returned as well

clearReturn

public void clearReturn()
Clears the return value from the last executed function.

CreateFrame

public void CreateFrame()

evaluateExpression

public Scalar evaluateExpression(String code)
evaluates an expression

evaluateOldContext

public Scalar evaluateOldContext()

evaluateParsedLiteral

public Scalar evaluateParsedLiteral(String code)
evaluates the passed in code as if it was a sleep parsed literal

evaluatePredicate

public boolean evaluatePredicate(String code)
evaluates a predicate condition

evaluateStatement

public Scalar evaluateStatement(String code)
evaluate a full blown statement... probably best to just load a script at this point

flagError

public void flagError(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.

flagReturn

public void flagReturn(Scalar value, int type_of_flow)

FrameResult

public void FrameResult(Scalar value)
kills the current frame and if there is a parent frame pushes the specified value on to it

getContextMetadata

public Object getContextMetadata(Object key)
Returns the data associated with the particular key for this context.

getContextMetadata

public Object getContextMetadata(Object key, 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

getCurrentFrame

public Stack getCurrentFrame()

getCurrentSource

public String getCurrentSource()
obtain the filename of the current source of execution

getDebugString

public String getDebugString()

getEnvironment

public 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

getEnvironmentStack

public Stack getEnvironmentStack()
returns the environment stack used for temporary calculations and such.

getExceptionHandler

public Block getExceptionHandler()
preps and returns the current exception handler...

getExceptionMessage

public Scalar getExceptionMessage()
if there is no handler, we'll just get the message which will clear the thrown message as well

getFilterEnvironment

public FilterEnvironment getFilterEnvironment(String env)

getFlowControlRequest

public int getFlowControlRequest()

getFunction

public Function getFunction(String func)

getFunctionEnvironment

public Environment getFunctionEnvironment(String env)

getOperator

public Operator getOperator(String oper)

getPredicate

public Predicate getPredicate(String name)

getPredicateEnvironment

public PredicateEnvironment getPredicateEnvironment(String env)

getReturnValue

public Scalar getReturnValue()

getScalar

public Scalar getScalar(String key)
returns a scalar from this scripts environment

getScriptInstance

public ScriptInstance getScriptInstance()
returns a reference to the script associated with this environment

getScriptVariables

public ScriptVariables getScriptVariables()
returns the variable manager for this script

hasFrame

public boolean hasFrame()

installExceptionHandler

public void installExceptionHandler(ScriptEnvironment.ExceptionContext exc)

installExceptionHandler

public void installExceptionHandler(Block owner, Block handler, String varname)

isDebugInterrupt

public boolean isDebugInterrupt()

isExceptionHandlerInstalled

public boolean isExceptionHandlerInstalled()

isResponsible

public boolean isResponsible(Block block)

isReturn

public boolean isReturn()

isThrownValue

public boolean isThrownValue()

KillFrame

public void KillFrame()

loadContext

public void loadContext(Stack _context, HashMap _metadata)

popExceptionContext

public ScriptEnvironment.ExceptionContext popExceptionContext()

popSource

public void popSource()
remove the latest source information from the source stack

pushSource

public void pushSource(String s)
push source information onto the source stack

putScalar

public void putScalar(String key, Scalar value)
puts a scalar into this scripts environment (global scope)

resetEnvironment

public void resetEnvironment()
Resets the script environment to include clearing the return of all flags (including thrown exceptions)

saveContext

public Stack saveContext()

setContextMetadata

public void setContextMetadata(Object key, 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

setEnvironment

public void setEnvironment(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

showDebugMessage

public void showDebugMessage(String message)
fires this debug message via a runtime warning complete with line number of current step

toString

public String toString()