sleep.runtime
Class SleepUtils
public
class
SleepUtils
extends Object
Method Summary |
static void | addKeyword(String keyword) registers "keyword" as a keyword with the parser. |
static String | describe(Stack arguments) returns a comma separated list of descriptions of the scalars in the specified argument
stack. |
static String | describe(Scalar scalar) returns a string description of the specified scalar. |
static Scalar | getArrayScalar(ScalarArray value) Creates a scalar with the specified ScalarArray as the value |
static Scalar | getArrayScalar() returns a Scalar variable containing a SLEEP array as its value (everything in SLEEP is stored as a Scalar |
static Scalar | getArrayWrapper(Collection dataStructure) An easy way to make your programs data structures available in the script environment. |
static Scalar | getEmptyScalar() returns the null scalar, which will always be equal to 0, "", and null simultaneously. |
static SleepClosure | getFunctionFromScalar(Scalar value, ScriptInstance script) extracts a callable Function from the passed in Scalar. |
static Scalar | getHashScalar(ScalarHash value) Creates a scalar with the specified ScalarHash as the value |
static Scalar | getHashScalar() returns an empty hashmap scalar |
static Scalar | getHashWrapper(Map dataStructure) An easy way to make your programs data structures available in the script environment. |
static IOObject | getIOHandle(InputStream in, OutputStream out) creates an IO handle scalar suitable for use with the sleep IO API. |
static Scalar | getIOHandleScalar(InputStream in, OutputStream out) creates an IO handle scalar suitable for use with the sleep IO API. |
static List | getListFromArray(Scalar array) Generate a java.util.List from a scalar array. |
static List | getListFromArray(ScalarArray array) Generate a java.util.List from a scalar array. |
static Map | getMapFromHash(Scalar map) Generate a java.util.Map from a scalar hash. |
static Map | getMapFromHash(ScalarHash map) Generate a java.util.Map from a scalar hash. |
static Scalar | getScalar(int x) returns an int scalar with value x |
static Scalar | getScalar(short x) returns an int scalar (coverted from the short) with value x |
static Scalar | getScalar(float x) returns a double scalar (coverted from the float) with value x |
static Scalar | getScalar(double x) returns a double scalar with value x |
static Scalar | getScalar(Scalar x) Forces a copy of the value of the passed in scalar to be made. |
static Scalar | getScalar(long x) returns a long scalar with value x |
static Scalar | getScalar(byte[] x) constructs a string scalar with value x interpreted as an array of unsigned bytes |
static Scalar | getScalar(byte[] x, int length) constructs a string scalar with value x interpreted as an array of unsigned bytes |
static Scalar | getScalar(String x) returns a string scalar with value x |
static Scalar | getScalar(Object x) returns an object scalar with value x |
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 |
static boolean | isEmptyScalar(Scalar value) returns true if the passed in scalar value is equivalent to the empty scalar or null |
static boolean | isFunctionScalar(Scalar value) Determines if the passed in scalar represents a "function" value. |
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 |
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) |
static Block | ParseCode(String code) Parses the specified code into a runnable block. |
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. |
static Scalar | runCode(Block code, ScriptEnvironment env) "safely" run a snippet of code. |
static Scalar | runCode(Function func, String name, ScriptInstance script, Stack locals) "safely" runs a "Function" of code. |
static Scalar | runCode(ScriptInstance owner, Block code, HashMap locals) "safely" run a snippet of code. |
static Scalar | runCode(ScriptInstance owner, Block code) "safely" run a snippet of code. |
a shared instance of the dreaded null scalar...
public static final int SLEEP_RELEASE
A date stamp of this Sleep release in YYYYMMDD format
public static final String SLEEP_VERSION
A string description of this Sleep release
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
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.
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.
Creates a scalar with the specified ScalarArray as the value
public static
Scalar getArrayScalar()
returns a Scalar variable containing a SLEEP array as its value (everything in SLEEP is stored as a Scalar
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.
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.
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
Creates a scalar with the specified ScalarHash as the value
public static
Scalar getHashScalar()
returns an empty hashmap scalar
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.
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.
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.
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.
Generate a java.util.List from a scalar array. Values will be the Java object
equivalents of the data stored in the scalar array.
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.
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.
public static
Scalar getScalar(int x)
returns an int scalar with value x
public static
Scalar getScalar(short x)
returns an int scalar (coverted from the short) with value x
public static
Scalar getScalar(float x)
returns a double scalar (coverted from the float) with value x
public static
Scalar getScalar(double x)
returns a double scalar with value 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.
public static
Scalar getScalar(long x)
returns a long scalar with value x
public static
Scalar getScalar(byte[] x)
constructs a string scalar with value x interpreted as an array of unsigned bytes
public static
Scalar getScalar(byte[] x, int length)
constructs a string scalar with value x interpreted as an array of unsigned bytes
public static
Scalar getScalar(String x)
returns a string scalar with value x
public static
Scalar getScalar(Object x)
returns an object scalar with value x
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
public static boolean isEmptyScalar(
Scalar value)
returns true if the passed in scalar value is equivalent to the empty scalar or null
public static boolean isFunctionScalar(
Scalar value)
Determines if the passed in scalar represents a "function" value. True iff the scalar contains a closure reference.
Creates a proxy instance of the specified class (limited to interfaces at this time) that is backed with the specified closure
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)
public static
Block ParseCode(String code)
Parses the specified code into a runnable block.
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.
"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.
"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)
"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.
"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.