Package sleep.runtime

Class ScriptEnvironment

  • All Implemented Interfaces:
    java.io.Serializable

    public class ScriptEnvironment
    extends java.lang.Object
    implements java.io.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, Serialized Form
    • 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
      • 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
      • 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:
        toString in class java.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
      • addToContext

        public void addToContext​(Block b,
                                 Step s)
      • evaluateOldContext

        public Scalar evaluateOldContext()
      • saveContext

        public java.util.Stack saveContext()
      • isExceptionHandlerInstalled

        public boolean isExceptionHandlerInstalled()
      • isResponsible

        public boolean isResponsible​(Block block)
      • 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...
      • 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()