Class ScriptUtils
java.lang.Object
com.randomnoun.common.jessop.engine.rhinoOracle.ScriptUtils
A collection of Rhino utility methods.
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionstatic void
assertHelper
(Object condition, Object... args) Helper for console.assert().static void
checkArguments
(Object[] args, int min, int max) Make sure that number of arguments is valid.static sun.org.mozilla.javascript.internal.Scriptable
getClassOrObjectProto
(sun.org.mozilla.javascript.internal.Scriptable scope, String className) Return a class prototype, or the object prototype if the class is not defined.static Map
getMapArgument
(Object[] args, int pos, boolean allowNull) Get an argument as Mapstatic Object
getObjectArgument
(Object[] args, int pos, boolean allowNull) Get an argument as objectstatic sun.org.mozilla.javascript.internal.ScriptableObject
getScriptableArgument
(Object[] args, int pos, boolean allowNull) Get an argument as ScriptableObjectstatic String
getStringArgument
(Object[] args, int pos, boolean allowNull) Get an argument as stringstatic Object
Coerce/wrap a java object to a JS object, and mask Lists and Maps as native JS objects.static Object
Unwrap a JS object to a java object.static int
Try to convert an object to an int value, returning the default value if conversion fails.static void
traceHelper
(sun.org.mozilla.javascript.internal.Function function, Object... args) Get a snapshot of the current JavaScript evaluation state by creating an Error object and invoke the function on it passing along any arguments.
-
Constructor Details
-
ScriptUtils
public ScriptUtils()
-
-
Method Details
-
javaToJS
Coerce/wrap a java object to a JS object, and mask Lists and Maps as native JS objects.- Parameters:
obj
- the object to coerce/wrapscope
- the scope- Returns:
- the wrapped/masked java object
-
jsToJava
Unwrap a JS object to a java object. This is much more conservative than Context.jsToJava in that it will preserve undefined values.- Parameters:
obj
- a JavaScript value- Returns:
- a Java object corresponding to obj
-
getClassOrObjectProto
public static sun.org.mozilla.javascript.internal.Scriptable getClassOrObjectProto(sun.org.mozilla.javascript.internal.Scriptable scope, String className) Return a class prototype, or the object prototype if the class is not defined.- Parameters:
scope
- the scopeclassName
- the class name- Returns:
- the class or object prototype
-
checkArguments
Make sure that number of arguments is valid.- Parameters:
args
- the argument arraymin
- the minimum number of argumentsmax
- the maximum number of arguments- Throws:
IllegalArgumentException
- if the number of arguments is not valid
-
getScriptableArgument
public static sun.org.mozilla.javascript.internal.ScriptableObject getScriptableArgument(Object[] args, int pos, boolean allowNull) throws IllegalArgumentException Get an argument as ScriptableObject- Parameters:
args
- the argument arraypos
- the position of the requested argument- Returns:
- the argument as ScriptableObject
- Throws:
IllegalArgumentException
- if the argument can't be converted to a map
-
getStringArgument
Get an argument as string- Parameters:
args
- the argument arraypos
- the position of the requested argument- Returns:
- the argument as string
-
getMapArgument
public static Map getMapArgument(Object[] args, int pos, boolean allowNull) throws IllegalArgumentException Get an argument as Map- Parameters:
args
- the argument arraypos
- the position of the requested argument- Returns:
- the argument as map
- Throws:
IllegalArgumentException
- if the argument can't be converted to a map
-
getObjectArgument
Get an argument as object- Parameters:
args
- the argument arraypos
- the position of the requested argument- Returns:
- the argument as object
-
toInt
Try to convert an object to an int value, returning the default value if conversion fails.- Parameters:
obj
- the valuedefaultValue
- the default value- Returns:
- the converted value
-
traceHelper
public static void traceHelper(sun.org.mozilla.javascript.internal.Function function, Object... args) Get a snapshot of the current JavaScript evaluation state by creating an Error object and invoke the function on it passing along any arguments. Used to invoke console.trace() and friends because implementing this in JavaScript would mess with the evaluation state.- Parameters:
function
- the function to callargs
- optional arguments to pass to the function.
-
assertHelper
Helper for console.assert(). Implemented in Java in order not to modify the JavaScript stack.- Parameters:
condition
- the condition to testargs
- one or more message parts
-