Java Code Examples for org.mozilla.javascript.ScriptableObject#defineProperty()

The following examples show how to use org.mozilla.javascript.ScriptableObject#defineProperty() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: BaseScript.java    From pentaho-reporting with GNU Lesser General Public License v2.1 6 votes vote down vote up
protected void executeScript( final Map<String, Object> params ) {
  final Context cx = Context.getCurrentContext();
  // env.js has methods that pass the 64k Java limit, so we can't compile
  // to bytecode. Interpreter mode to the rescue!
  cx.setOptimizationLevel( -1 );
  cx.setLanguageVersion( Context.VERSION_1_7 );

  final Object wrappedParams;
  if ( params != null ) {
    wrappedParams = Context.javaToJS( params, scope );
  } else {
    wrappedParams = Context.javaToJS( new HashMap<String, Object>(), scope );
  }

  try {
    ScriptableObject.defineProperty( scope, "params", wrappedParams, 0 );
    cx.evaluateReader( scope, new FileReader( source ), this.source, 1, null );
  } catch ( IOException ex ) {
    logger.error( "Failed to read " + source + ": " + ex.toString() );
  }
}
 
Example 2
Source File: ExternalArrayTest.java    From rhino-android with Apache License 2.0 5 votes vote down vote up
@Test
public void testIntArrayThenRemove()
{
    ScriptableObject a = (ScriptableObject)cx.newObject(root);
    // Set the external array data
    TestIntArray l = new TestIntArray(10);
    a.setExternalArrayData(l);
    for (int i = 0; i < 10; i++) {
        l.setArrayElement(i, i);
    }
    root.put("testArray", root, a);
    root.put("testArrayLength", root, 10);
    root.put("regularArray", root, false);
    runScript("jstests/extensions/external-array-test.js", 1);

    // Clear it and test again. When cleared, object should go back to behaving like a
    // regular JavaScript object.
    a.delete("stringField");
    a.delete("intField");
    a.setExternalArrayData(null);
    for (int i = 0; i < 10; i++) {
        a.put(i, a, i);
    }
    a.defineProperty("length", 10, ScriptableObject.DONTENUM);
    root.put("regularArray", root, true);
    runScript("jstests/extensions/external-array-test.js", 1);
}
 
Example 3
Source File: JsRuntimeReplFactoryBuilder.java    From stetho with MIT License 5 votes vote down vote up
private void importConsole(@NonNull ScriptableObject scope) throws StethoJsException {
  // Set the `console` object
  try {
    ScriptableObject.defineClass(scope, JsConsole.class);
    JsConsole console = new JsConsole(scope);
    scope.defineProperty("console", console, ScriptableObject.DONTENUM);
  } catch (Exception e) {
    throw new StethoJsException(e, "Failed to setup javascript console");
  }
}