Package org.rapidcontext.core.js
Class JsRuntime
java.lang.Object
org.rapidcontext.core.js.JsRuntime
An interface to the JavaScript engine (Mozilla Rhino).
- Version:
- 1.0
-
Method Summary
Modifier and TypeMethodDescriptionstatic Object
call
(org.mozilla.javascript.Function f, Object[] args, CallContext procCx) Calls a previously compiled JavaScript function.static org.mozilla.javascript.Function
Compiles a JavaScript function for later use.static Object
Removes all JavaScript classes and replaces them with the corresponding Java objects.static Object
Wraps a Java object for JavaScript access.
-
Method Details
-
compile
public static org.mozilla.javascript.Function compile(String name, String[] args, String body) throws JsException Compiles a JavaScript function for later use.- Parameters:
name
- the function name (must be valid JS)args
- the argument namesbody
- the function body (i.e. source code)- Returns:
- a compiled function object
- Throws:
JsException
- if the source code didn't compile
-
call
public static Object call(org.mozilla.javascript.Function f, Object[] args, CallContext procCx) throws JsException Calls a previously compiled JavaScript function.- Parameters:
f
- the compiled functionargs
- the argument values (will be wrapped)procCx
- the optional procedure call context or null- Returns:
- the function return value (possibly wrapped)
- Throws:
JsException
- if the call failed or threw an exception
-
wrap
Wraps a Java object for JavaScript access. This method only handles String, Number, Boolean and Data instances.- Parameters:
obj
- the object to wrapscope
- the parent scope- Returns:
- the wrapped object
- See Also:
-
unwrap
Removes all JavaScript classes and replaces them with the corresponding Java objects. This method will use instances of Dict and Array to replace native JavaScript objects and arrays. Also, it will replace both JavaScript "null" and "undefined" with null. Any Dict or Array object encountered will be traversed and copied recursively. Other objects will be returned as-is.- Parameters:
obj
- the object to unwrap- Returns:
- the unwrapped object
- See Also:
-