sleep.runtime

Class SleepUtils

public class SleepUtils extends Object

This class contains generalized utilities for instantiating/wrapping data into the sleep Scalar type. Included for free are methods for executing sleep blocks as well.

See Also: Block SleepClosure Scalar ScalarType ScalarArray ScalarHash

Field Summary
protected static ScalarTypenullScalar
a shared instance of the dreaded null scalar...
static intSLEEP_RELEASE
A date stamp of this Sleep release in YYYYMMDD format
static StringSLEEP_VERSION
A string description of this Sleep release
Method Summary
static voidaddKeyword(String keyword)
registers "keyword" as a keyword with the parser.
static Stringdescribe(Stack arguments)
returns a comma separated list of descriptions of the scalars in the specified argument stack.
static Stringdescribe(Scalar scalar)
returns a string description of the specified scalar.
static ScalargetArrayScalar(ScalarArray value)
Creates a scalar with the specified ScalarArray as the value
static ScalargetArrayScalar()
returns a Scalar variable containing a SLEEP array as its value (everything in SLEEP is stored as a Scalar
static ScalargetArrayWrapper(Collection dataStructure)
An easy way to make your programs data structures available in the script environment.
static ScalargetEmptyScalar()
returns the null scalar, which will always be equal to 0, "", and null simultaneously.
static SleepClosuregetFunctionFromScalar(Scalar value, ScriptInstance script)
extracts a callable Function from the passed in Scalar.
static ScalargetHashScalar(ScalarHash value)
Creates a scalar with the specified ScalarHash as the value
static ScalargetHashScalar()
returns an empty hashmap scalar
static ScalargetHashWrapper(Map dataStructure)
An easy way to make your programs data structures available in the script environment.
static IOObjectgetIOHandle(InputStream in, OutputStream out)
creates an IO handle scalar suitable for use with the sleep IO API.
static ScalargetIOHandleScalar(InputStream in, OutputStream out)
creates an IO handle scalar suitable for use with the sleep IO API.
static ListgetListFromArray(Scalar array)
Generate a java.util.List from a scalar array.
static ListgetListFromArray(ScalarArray array)
Generate a java.util.List from a scalar array.
static MapgetMapFromHash(Scalar map)
Generate a java.util.Map from a scalar hash.
static MapgetMapFromHash(ScalarHash map)
Generate a java.util.Map from a scalar hash.
static ScalargetScalar(int x)
returns an int scalar with value x
static ScalargetScalar(short x)
returns an int scalar (coverted from the short) with value x
static ScalargetScalar(float x)
returns a double scalar (coverted from the float) with value x
static ScalargetScalar(double x)
returns a double scalar with value x
static ScalargetScalar(Scalar x)
Forces a copy of the value of the passed in scalar to be made.
static ScalargetScalar(long x)
returns a long scalar with value x
static ScalargetScalar(byte[] x)
constructs a string scalar with value x interpreted as an array of unsigned bytes
static ScalargetScalar(byte[] x, int length)
constructs a string scalar with value x interpreted as an array of unsigned bytes
static ScalargetScalar(String x)
returns a string scalar with value x
static ScalargetScalar(Object x)
returns an object scalar with value x
static ScalargetScalar(boolean x)
if x is true, the value will be 1, if x is false the value will be the empty scalar
static booleanisEmptyScalar(Scalar value)
returns true if the passed in scalar value is equivalent to the empty scalar or null
static booleanisFunctionScalar(Scalar value)
Determines if the passed in scalar represents a "function" value.
static ObjectnewInstance(Class initializeMe, SleepClosure closure, ScriptInstance owner)
Creates a proxy instance of the specified class (limited to interfaces at this time) that is backed with the specified closure
static ObjectnewInstance(Class initializeMe, Block code, ScriptInstance owner)
Creates a proxy instance of the specified class (limited to interfaces at this time) that is backed with the specified block of code (made into a closure)
static BlockParseCode(String code)
Parses the specified code into a runnable block.
static voidremoveScalar(Iterator collection, Scalar value)
Iterates over the specified collection and removes all items that are the same as the specified scalar value.
static ScalarrunCode(Block code, ScriptEnvironment env)
"safely" run a snippet of code.
static ScalarrunCode(Function func, String name, ScriptInstance script, Stack locals)
"safely" runs a "Function" of code.
static ScalarrunCode(ScriptInstance owner, Block code, HashMap locals)
"safely" run a snippet of code.
static ScalarrunCode(ScriptInstance owner, Block code)
"safely" run a snippet of code.

Field Detail

nullScalar

protected static ScalarType nullScalar
a shared instance of the dreaded null scalar...

SLEEP_RELEASE

public static final int SLEEP_RELEASE
A date stamp of this Sleep release in YYYYMMDD format

SLEEP_VERSION

public static final String SLEEP_VERSION
A string description of this Sleep release

Method Detail

addKeyword

public static void addKeyword(String keyword)
registers "keyword" as a keyword with the parser. This is a necessity if you have extra non standard bridges in sleep

describe

public static String describe(Stack arguments)
returns a comma separated list of descriptions of the scalars in the specified argument stack. This is used by the debugging mechanism to format arguments to strings based on their scalar type.

describe

public static String describe(Scalar scalar)
returns a string description of the specified scalar. Used by debugging mechanism to format scalars based on their value type, i.e. strings are enclosed in single quotes, objects in brackets, $null is displayed as $null, etc.

getArrayScalar

public static Scalar getArrayScalar(ScalarArray value)
Creates a scalar with the specified ScalarArray as the value

getArrayScalar

public static Scalar getArrayScalar()
returns a Scalar variable containing a SLEEP array as its value (everything in SLEEP is stored as a Scalar

getArrayWrapper

public static Scalar getArrayWrapper(Collection dataStructure)
An easy way to make your programs data structures available in the script environment. Using this wrapper method will make the contents of your Set data structure available in a read only way to scripters using your program. Values your data structure returns are turned into Scalar strings using the toString() method of the object. If you need something more specific than this then take a look at the source code for sleep.runtime.SetWrapper which implements sleep.runtime.ScalarArray.

getEmptyScalar

public static Scalar getEmptyScalar()
returns the null scalar, which will always be equal to 0, "", and null simultaneously. The instance of the null scalar is shared since the null scalar "value" is not modifiable.

getFunctionFromScalar

public static SleepClosure getFunctionFromScalar(Scalar value, ScriptInstance script)
extracts a callable Function from the passed in Scalar. The passed in Scalar can be either a SleepClosure scalar or a string scalar specifying a function name

getHashScalar

public static Scalar getHashScalar(ScalarHash value)
Creates a scalar with the specified ScalarHash as the value

getHashScalar

public static Scalar getHashScalar()
returns an empty hashmap scalar

getHashWrapper

public static Scalar getHashWrapper(Map dataStructure)
An easy way to make your programs data structures available in the script environment. Using this wrapper method will make the contents of your Map'd data structure available in a read only way to scripters using your program. Keys are automatically turned into strings and values your data structure give back are turned into Scalar strings using the toString() method of the object. If you need something more specific than this then take a look at the source code for sleep.runtime.MapWrapper which implements sleep.runtime.ScalarHash.

getIOHandle

public static IOObject getIOHandle(InputStream in, OutputStream out)
creates an IO handle scalar suitable for use with the sleep IO API. The passed in streams can each be null if necessary.

getIOHandleScalar

public static Scalar getIOHandleScalar(InputStream in, OutputStream out)
creates an IO handle scalar suitable for use with the sleep IO API. The passed in streams can each be null if necessary.

getListFromArray

public static List getListFromArray(Scalar array)
Generate a java.util.List from a scalar array. Values will be the Java object equivalents of the data stored in the scalar array.

getListFromArray

public static List getListFromArray(ScalarArray array)
Generate a java.util.List from a scalar array. Values will be the Java object equivalents of the data stored in the scalar array.

getMapFromHash

public static Map getMapFromHash(Scalar map)
Generate a java.util.Map from a scalar hash. Keys will be Java strings. Values will be the Java object equivalents of the data stored in the scalar hash.

getMapFromHash

public static Map getMapFromHash(ScalarHash map)
Generate a java.util.Map from a scalar hash. Keys will be Java strings. Values will be the Java object equivalents of the data stored in the scalar hash.

getScalar

public static Scalar getScalar(int x)
returns an int scalar with value x

getScalar

public static Scalar getScalar(short x)
returns an int scalar (coverted from the short) with value x

getScalar

public static Scalar getScalar(float x)
returns a double scalar (coverted from the float) with value x

getScalar

public static Scalar getScalar(double x)
returns a double scalar with value x

getScalar

public static Scalar getScalar(Scalar x)
Forces a copy of the value of the passed in scalar to be made. Sleep scalars in general are passed by *value*. When passing a scalar, a new scalar should be made with a copy of the old scalars value. Object scalars are passed by reference but this copying mechanism handles that. If you are ever storing scalars in a data structure call this method to get a copy. Otherwise chaos might ensue.

getScalar

public static Scalar getScalar(long x)
returns a long scalar with value x

getScalar

public static Scalar getScalar(byte[] x)
constructs a string scalar with value x interpreted as an array of unsigned bytes

getScalar

public static Scalar getScalar(byte[] x, int length)
constructs a string scalar with value x interpreted as an array of unsigned bytes

getScalar

public static Scalar getScalar(String x)
returns a string scalar with value x

getScalar

public static Scalar getScalar(Object x)
returns an object scalar with value x

getScalar

public static Scalar getScalar(boolean x)
if x is true, the value will be 1, if x is false the value will be the empty scalar

isEmptyScalar

public static boolean isEmptyScalar(Scalar value)
returns true if the passed in scalar value is equivalent to the empty scalar or null

isFunctionScalar

public static boolean isFunctionScalar(Scalar value)
Determines if the passed in scalar represents a "function" value. True iff the scalar contains a closure reference.

newInstance

public static Object newInstance(Class initializeMe, SleepClosure closure, ScriptInstance owner)
Creates a proxy instance of the specified class (limited to interfaces at this time) that is backed with the specified closure

newInstance

public static Object newInstance(Class initializeMe, Block code, ScriptInstance owner)
Creates a proxy instance of the specified class (limited to interfaces at this time) that is backed with the specified block of code (made into a closure)

ParseCode

public static Block ParseCode(String code)
Parses the specified code into a runnable block.

removeScalar

public static void removeScalar(Iterator collection, Scalar value)
Iterates over the specified collection and removes all items that are the same as the specified scalar value. Certain scalars (ints, doubles, etc.) are compared by string representation where as others (Object, Hash, Array) are compared by reference.

runCode

public static Scalar runCode(Block code, ScriptEnvironment env)
"safely" run a snippet of code. The main thing this function does is clear the return value before returning the value to the caller. This is important because the return value (if there is one) would not get cleared otherwise. Kind of important.

Parameters: code the block of code we want to execute env the environment to run the code in

Returns: the scalar returned by the executed code (if their is a return value), null otherwise.

runCode

public static Scalar runCode(Function func, String name, ScriptInstance script, Stack locals)
"safely" runs a "Function" of code. The main thing this method does is clear the return value before returning the value to the caller. This is important because the return value (if there is one) would not get cleared otherwise. Kind of important.

Parameters: func the Function object we want to execute name the name of the function we are executing (can be anything, depending on the function object) script the script we want to execute the function within locals a stack of scalars representing the arguments to this Function (first arg on top)

Returns: the scalar returned by the executed code or the sleep empty scalar if there is no return value (never returns null)

runCode

public static Scalar runCode(ScriptInstance owner, Block code, HashMap locals)
"safely" run a snippet of code. The main thing this function does is clear the return value before returning the value to the caller. This is important because the return value (if there is one) would not get cleared otherwise. Kind of important.

Parameters: owner the owning script instance of this block of code code the block of code we want to execute locals a hashmap containing Scalar objects that should be installed into the local scope. The keys should be Strings representing the $names for each of the Scalar variables.

Returns: the scalar returned by the executed code (if their is a return value), null otherwise.

runCode

public static Scalar runCode(ScriptInstance owner, Block code)
"safely" run a snippet of code. The main thing this function does is clear the return value before returning the value to the caller. This is important because the return value (if there is one) would not get cleared otherwise. Kind of important.

Parameters: owner the owning script instance of this block of code code the block of code we want to execute

Returns: the scalar returned by the executed code (if their is a return value), null otherwise.