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.
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()
returns a Scalar variable containing a SLEEP array as its value (everything in SLEEP is stored as a Scalar
getArrayScalar
public static Scalar getArrayScalar(ScalarArray value)
Creates a scalar with the specified ScalarArray as the value
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()
returns an empty hashmap scalar
getHashScalar
public static Scalar getHashScalar(ScalarHash value)
Creates a scalar with the specified ScalarHash as the value
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.
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(Object x)
returns an object scalar with value x
getScalar
public static Scalar getScalar(String x)
returns a string 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
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(double x)
returns a double scalar 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(int x)
returns an int scalar with value x
getScalar
public static Scalar getScalar(long x)
returns a long 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(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.
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.
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.
code
- the block of code we want to executeenv
- the environment to run the code in
- 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.
func
- the Function object we want to executename
- 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 withinlocals
- a stack of scalars representing the arguments to this Function (first arg on top)
- 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)
"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.
owner
- the owning script instance of this block of codecode
- the block of code we want to execute
- the scalar returned by the executed code (if their is a return value), null otherwise.
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.
owner
- the owning script instance of this block of codecode
- the block of code we want to executelocals
- 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.
- the scalar returned by the executed code (if their is a return value), null otherwise.