Class JsRuntime

java.lang.Object
org.rapidcontext.core.js.JsRuntime

public final class JsRuntime extends Object
An interface to the JavaScript engine (Mozilla Rhino).
Version:
1.0
  • 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 names
      body - 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 function
      args - 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

      public static Object wrap(Object obj, org.mozilla.javascript.Scriptable scope)
      Wraps a Java object for JavaScript access. This method only handles String, Number, Boolean and Data instances.
      Parameters:
      obj - the object to wrap
      scope - the parent scope
      Returns:
      the wrapped object
      See Also:
    • unwrap

      public static Object unwrap(Object obj)
      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: