sleep.runtime

Class ScriptEnvironment

Implemented Interfaces:
Serializable

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 class
ScriptEnvironment.Context

Field Summary

static int
FLOW_CONTROL_BREAK
request a break out of the current loop
static int
FLOW_CONTROL_CONTINUE
adding a continue keyword as people keep demanding it
static int
FLOW_CONTROL_NONE
currently no flow contrl change has been requested
static int
FLOW_CONTROL_RETURN
request a return from the current function
static int
FLOW_CONTROL_YIELD
adding a yield keyword
protected Stack
context
protected Stack
contextStack
protected String
debugString
protected Hashtable
environment
the environment hashtable that contains all of the functions, predicates, operators, and "environment keywords" this script has access to.
protected Stack
environmentStack
the runtime data stack for this environment
protected String
errorMessage
stored error message...
protected int
findex
protected ArrayList
frames
protected boolean
isDebugInterrupt
protected Stack
metaStack
protected HashMap
metadata
protected int
request
protected Scalar
rv
protected ScriptInstance
self
the script instance that this is the environment for
protected Stack
sources
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

void
CreateFrame()
void
FrameResult(Scalar value)
kills the current frame and if there is a parent frame pushes the specified value on to it
void
KillFrame()
void
addToContext(Block b, Step s)
String
checkError()
once an error is checked using this function, it is cleared, the orignal error message is returned as well
void
clearReturn()
Scalar
evaluateExpression(String code)
evaluates an expression
Scalar
evaluateOldContext()
Scalar
evaluateParsedLiteral(String code)
evaluates the passed in code as if it was a sleep parsed literal
boolean
evaluatePredicate(String code)
evaluates a predicate condition
Scalar
evaluateStatement(String code)
evaluate a full blown statement...
void
flagError(String message)
A utility for bridge writers to flag an error.
void
flagReturn(Scalar value, int type_of_flow)
Object
getContextMetadata(Object key)
Returns the data associated with the particular key for this context.
Object
getContextMetadata(Object key, Object default_value)
Returns the data associated with the particular key for this context.
Stack
getCurrentFrame()
String
getCurrentSource()
obtain the filename of the current source of execution
String
getDebugString()
Hashtable
getEnvironment()
Returns the environment for this script.
Stack
getEnvironmentStack()
returns the environment stack used for temporary calculations and such.
FilterEnvironment
getFilterEnvironment(String env)
int
getFlowControlRequest()
Function
getFunction(String func)
Environment
getFunctionEnvironment(String env)
Operator
getOperator(String oper)
Predicate
getPredicate(String name)
PredicateEnvironment
getPredicateEnvironment(String env)
Scalar
getReturnValue()
Scalar
getScalar(String key)
returns a scalar from this scripts environment
ScriptInstance
getScriptInstance()
returns a reference to the script associated with this environment
ScriptVariables
getScriptVariables()
returns the variable manager for this script
boolean
hasFrame()
boolean
isDebugInterrupt()
boolean
isReturn()
void
loadContext(Stack _context, HashMap _metadata)
void
popSource()
remove the latest source information from the source stack
void
pushSource(String s)
push source information onto the source stack
void
putScalar(String key, Scalar value)
puts a scalar into this scripts environment
Stack
saveContext()
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
void
setEnvironment(Hashtable h)
Sets the environment Hashtable this script is to use.
void
showDebugMessage(String message)
fires this debug message via a runtime warning complete with line number of current step
String
toString()

Field Details

FLOW_CONTROL_BREAK

public static final int FLOW_CONTROL_BREAK
request a break out of the current loop
Field Value:
2

FLOW_CONTROL_CONTINUE

public static final int FLOW_CONTROL_CONTINUE
adding a continue keyword as people keep demanding it
Field Value:
3

FLOW_CONTROL_NONE

public static final int FLOW_CONTROL_NONE
currently no flow contrl change has been requested
Field Value:
0

FLOW_CONTROL_RETURN

public static final int FLOW_CONTROL_RETURN
request a return from the current function
Field Value:
1

FLOW_CONTROL_YIELD

public static final int FLOW_CONTROL_YIELD
adding a yield keyword
Field Value:
4

context

protected Stack context

contextStack

protected Stack contextStack

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 String errorMessage
stored error message...

findex

protected int findex

frames

protected ArrayList frames

isDebugInterrupt

protected boolean isDebugInterrupt

metaStack

protected Stack metaStack

metadata

protected HashMap metadata

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 Details

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 Details

CreateFrame

public void CreateFrame()

FrameResult

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

KillFrame

public void KillFrame()

addToContext

public void addToContext(Block b,
                         Step s)

checkError

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

clearReturn

public void clearReturn()

evaluateExpression

public Scalar evaluateExpression(String code)
            throws YourCodeSucksException
evaluates an expression

evaluateOldContext

public Scalar evaluateOldContext()

evaluateParsedLiteral

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

evaluatePredicate

public boolean evaluatePredicate(String code)
            throws YourCodeSucksException
evaluates a predicate condition

evaluateStatement

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

flagError

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

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.

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()

isDebugInterrupt

public boolean isDebugInterrupt()

isReturn

public boolean isReturn()

loadContext

public void loadContext(Stack _context,
                        HashMap _metadata)

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

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()