Java Code Examples for org.mozilla.javascript.Scriptable#setPrototype()

The following examples show how to use org.mozilla.javascript.Scriptable#setPrototype() . 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: JsTestsBase.java    From rhino-android with Apache License 2.0 6 votes vote down vote up
public void runJsTest(Context cx, Scriptable shared, String name, String source) {
    // create a lightweight top-level scope
    Scriptable scope = cx.newObject(shared);
    scope.setPrototype(shared);
    System.out.print(name + ": ");
    Object result;
    try {
        result = cx.evaluateString(scope, source, "jstest input: " + name, 1, null);
    } catch (RuntimeException e) {
        e.printStackTrace(System.err);
        System.out.println("FAILED");
        throw e;
    }
    assertTrue(result != null);
    assertTrue("success".equals(result));
    System.out.println("passed");
}
 
Example 2
Source File: ExpressionLanguageJavaScriptImpl.java    From oval with Eclipse Public License 2.0 6 votes vote down vote up
@Override
public Object evaluate(final String expression, final Map<String, ?> values) throws ExpressionEvaluationException {
   LOG.debug("Evaluating JavaScript expression: {1}", expression);
   try {
      final Context ctx = ContextFactory.getGlobal().enterContext();
      final Scriptable scope = ctx.newObject(parentScope);
      scope.setPrototype(parentScope);
      scope.setParentScope(null);
      for (final Entry<String, ?> entry : values.entrySet()) {
         scope.put(entry.getKey(), scope, Context.javaToJS(entry.getValue(), scope));
      }
      final Script expr = expressionCache.get(expression);
      return expr.exec(ctx, scope);
   } catch (final EvaluatorException ex) {
      throw new ExpressionEvaluationException("Evaluating JavaScript expression failed: " + expression, ex);
   } finally {
      Context.exit();
   }
}
 
Example 3
Source File: JsTestsBase.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
public void runJsTest(Context cx, Scriptable shared, String name, String source) {
    // create a lightweight top-level scope
    Scriptable scope = cx.newObject(shared);
    scope.setPrototype(shared);
    System.out.print(name + ": ");
    Object result;
    try {
        result = cx.evaluateString(scope, source, "jstest input", 1, null);
    } catch (RuntimeException e) {
        System.out.println("FAILED");
        throw e;
    }
    assertTrue(result != null);
    assertTrue("success".equals(result));
    System.out.println("passed");
}
 
Example 4
Source File: PreparedDummyQuery.java    From birt with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * @param queryScope
 * @return
 * @throws BirtException 
 */
private Scriptable getScope( Scriptable queryScope ) throws BirtException
{
	Scriptable topScope = null;
	if ( queryScope != null )
		topScope = queryScope;
	else
		topScope = session.getSharedScope( );

	Scriptable executionScope = null;
	executionScope = ( (IDataScriptEngine) session.getEngineContext( )
			.getScriptContext( )
			.getScriptEngine( IDataScriptEngine.ENGINE_NAME ) ).getJSContext( session.getEngineContext( )
			.getScriptContext( ) )
			.newObject( topScope );
	executionScope.setParentScope( topScope );
	executionScope.setPrototype( session.getSharedScope( ) );
	
	return executionScope;
}
 
Example 5
Source File: SecureJavascriptUtil.java    From activiti6-boot2 with Apache License 2.0 5 votes vote down vote up
public static Object evaluateScript(VariableScope variableScope, String script, Map<Object, Object> beans) {
  Context context = Context.enter();
  try {
      Scriptable scope = context.initStandardObjects();
      SecureScriptScope secureScriptScope = new SecureScriptScope(variableScope, beans);
      scope.setPrototype(secureScriptScope);

      return context.evaluateString(scope, script, "<script>", 0, null);
  } finally {
      Context.exit();
  }
}
 
Example 6
Source File: Bug421071Test.java    From rhino-android with Apache License 2.0 5 votes vote down vote up
public void run() {
    Context context = factory.enterContext();
    try {
        // Run each script in its own scope, to keep global variables
        // defined in each script separate
        Scriptable threadScope = context.newObject(globalScope);
        threadScope.setPrototype(globalScope);
        threadScope.setParentScope(null);
        script.exec(context, threadScope);
    } catch (Exception ee) {
        ee.printStackTrace();
    } finally {
        Context.exit();
    }
}
 
Example 7
Source File: SecureJavascriptUtil.java    From flowable-engine with Apache License 2.0 5 votes vote down vote up
public static Object evaluateScript(VariableScope variableScope, String script, Map<Object, Object> beans) {
    Context context = Context.enter();
    try {
        Scriptable scope = context.initStandardObjects();
        SecureScriptScope secureScriptScope = new SecureScriptScope(variableScope, beans);
        scope.setPrototype(secureScriptScope);

        return context.evaluateString(scope, script, "<script>", 0, null);
    } finally {
        Context.exit();
    }
}
 
Example 8
Source File: Bug421071Test.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
public void run() {
    Context context = factory.enterContext();
    try {
        // Run each script in its own scope, to keep global variables
        // defined in each script separate
        Scriptable threadScope = context.newObject(globalScope);
        threadScope.setPrototype(globalScope);
        threadScope.setParentScope(null);
        script.exec(context, threadScope);
    } catch (Exception ee) {
        ee.printStackTrace();
    } finally {
        Context.exit();
    }
}
 
Example 9
Source File: UsesDetailFalseTest.java    From birt with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Get result list
 * @param resultIt
 * @return List[] rowList, sumList, sglList, eglList
 * @throws DataException
 */
private List[] getQueryResult( IResultIterator resultIt ) throws Exception
{
	List rowList = new ArrayList( );
	List sumList = new ArrayList( );
	List sglList = new ArrayList( );
	List eglList = new ArrayList( );
	List subList = new ArrayList( );

	Scriptable subScope = new NativeObject( );
	subScope.setPrototype( jsScope );
	subScope.setParentScope( jsScope );

	while ( resultIt.next( ) )
	{
		rowList.add( resultIt.getValue( keyName1 ) );
		sumList.add( resultIt.getValue( aggrName1 ) );
		sglList.add( new Integer( resultIt.getStartingGroupLevel( ) ) );
		eglList.add( new Integer( resultIt.getEndingGroupLevel( ) ) );

		List subKeyRow1List = new ArrayList( );
		List subKeyRow2List = new ArrayList( );
		List subAggregaList = new ArrayList( );
		IResultIterator subIterator = resultIt.getSecondaryIterator( "IAMTEST",
				subScope );			
		while ( subIterator.next( ) )
		{
			subKeyRow1List.add( subIterator.getValue( keyName2 ) );
			subKeyRow2List.add( subIterator.getValue( keyName3 ) );
			subAggregaList.add( subIterator.getValue( aggrName2 ) );
		}
		subList.add( subKeyRow1List );
		subList.add( subKeyRow2List );
		subList.add( subAggregaList );
	}

	return new List[]{
			rowList, sumList, sglList, eglList, subList
	};
}
 
Example 10
Source File: QueryExecutor.java    From birt with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Creates a subscope within parent scope
 * @param parentAndProtoScope parent scope. If null, the shared top-level scope is used as parent
 * @throws BirtException 
 */
private Scriptable newSubScope( Scriptable parentAndProtoScope ) throws BirtException
{
	if ( parentAndProtoScope == null )
		parentAndProtoScope = sharedScope;

	Scriptable scope = ((IDataScriptEngine)session.getEngineContext( ).getScriptContext( ).getScriptEngine( IDataScriptEngine.ENGINE_NAME )).getJSContext( session.getEngineContext( ).getScriptContext( ) )
			.newObject( parentAndProtoScope );
	scope.setParentScope( parentAndProtoScope );
	scope.setPrototype( parentAndProtoScope );

	
	return scope;
}