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

The following examples show how to use org.mozilla.javascript.Scriptable#setParentScope() . 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: 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 2
Source File: JsFunction.java    From spork with Apache License 2.0 6 votes vote down vote up
/**
 * converts a bag to javascript object based on a schema
 * @param bag the bag to convert
 * @param schema the schema to use for conversion
 * @param depth call depth used for debugging messages
 * @return the resulting javascript object
 * @throws FrontendException
 * @throws ExecException
 */
private Scriptable pigBagToJS(DataBag bag, Schema schema, int depth) throws FrontendException, ExecException {
    debugConvertPigToJS(depth, "Bag", bag, schema);
    if (schema.size() == 1 && schema.getField(0).type == DataType.TUPLE) {
        // unwrapping as bags always contain a tuple
        schema = schema.getField(0).schema;
    }
    Scriptable array = jsScriptEngine.jsNewArray(bag.size());
    array.setParentScope(jsScriptEngine.getScope());
    int i= 0;
    for (Tuple t : bag) {
        array.put(i++, array, pigTupleToJS(t, schema, depth + 1));
    }
    debugReturn(depth, array);
    return array;
}
 
Example 3
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 4
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 5
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 6
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 7
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;
}
 
Example 8
Source File: SubQueryTest.java    From birt with Eclipse Public License 1.0 4 votes vote down vote up
/**
 * Sub query test
 * Normal case: add subquery to GroupDefinition
 * @throws Exception
 */
@Test
   public void test( ) throws Exception
{
	// 1 prepare query execution
	Context cx = Context.enter( );
	Scriptable sharedScope = cx.initStandardObjects( );

	Scriptable subScope = cx.newObject( sharedScope );
	subScope.setParentScope( sharedScope );
	
	IQueryDefinition queryDefn = getDefaultQueryDefnWithSubQuery( dataSet.getName( ) );
	expressions = getExpressionsOfDefaultQuery( );
	
	// 2 do query execution
	IResultIterator resultIt = executeQuery( queryDefn );
	
	// 3.1 get sub query data
	resultIt.next( );
	IResultIterator subIterator = resultIt.getSecondaryIterator( "IAMTEST",
			sharedScope );
	
	// 3.2 get sub query of sub query data
	subIterator.next( );
	IResultIterator subSubIterator = subIterator.getSecondaryIterator( "IAMTEST2",
			subScope );

	bindingNameRow = this.getBindingExpressionName( );
	// 4.1 output sub query data
	testPrintln( "sub query data" );
	outputData( subIterator );
	testPrintln( "" );
	
	// 4.2 output sub query of sub query data
	testPrintln( "sub query of sub query data" );
	outputData( subSubIterator );
	testPrintln( "" );
	
	// check whether output is correct
	checkOutputFile();
}
 
Example 9
Source File: FeatureTest.java    From birt with Eclipse Public License 1.0 4 votes vote down vote up
/**
 *  execute the queries, output result and compare result with golden file.
 */
private void run( Collection inputParamDefns, Collection inputParamBindings )
		throws Exception
{
	Iterator iterator = inputParamDefns.iterator( );
	while ( iterator.hasNext( ) )
	{
		ParameterDefinition inputParamDefn = (ParameterDefinition) iterator
				.next( );
		( (OdaDataSetDesign) datasetCall ).addParameter( inputParamDefn );
	}

	iterator = inputParamBindings.iterator( );
	while ( iterator.hasNext( ) )
	{
		InputParameterBinding inputParamBinding = (InputParameterBinding) iterator
				.next( );
		queryDefnCall.addInputParamBinding( inputParamBinding );
	}

	IPreparedQuery preparedQueryCstm = dataEngine.prepare( queryDefnCstm );
	IPreparedQuery preparedQueryCall = dataEngine.prepare( queryDefnCall );

	IQueryResults queryResultsCstm = preparedQueryCstm.execute( jsScope );
	IResultIterator resultItCustomer = queryResultsCstm.getResultIterator( );

	// output result
	testPrintln( "*****A new Report Start!*****" );
	while ( resultItCustomer.next( ) )
	{
		testPrint( "Customer Name:" );
		testPrint( evalAsString( exprNameCstm[1], resultItCustomer ) );
		testPrint( "  Address:" );
		testPrint( evalAsString( exprNameCstm[2], resultItCustomer ) );
		testPrintln( "" );
		testPrint( "Starting Balance: $" );
		testPrint( evalAsString( exprNameCstm[3], resultItCustomer ) );
		testPrintln( "" );
		
		Scriptable newScope = jsContext.newObject( jsScope );
		newScope.setParentScope( jsScope );
		
		IQueryResults queryResultsCalls = preparedQueryCall.execute(
				queryResultsCstm, newScope);
		IResultIterator resultItCalls = queryResultsCalls
				.getResultIterator( );
		while ( resultItCalls.next( ) )
		{
			for ( int i = 1; i < expressionsCall.length; i++ )
			{
				testPrint( evalAsString( exprNameCall[i], resultItCalls ) );
				testPrint( " " ); 
			}
			testPrintln( "" );
		}
		testPrintln( "" );
	}
	checkOutputFile( );
}