Java Code Examples for org.mozilla.javascript.ContextFactory#enterContext()

The following examples show how to use org.mozilla.javascript.ContextFactory#enterContext() . 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 astor with GNU General Public License v2.0 6 votes vote down vote up
public void runJsTests(File[] tests) throws IOException {
    ContextFactory factory = ContextFactory.getGlobal();
    Context cx = factory.enterContext();
    try {
        cx.setOptimizationLevel(this.optimizationLevel);
        Scriptable shared = cx.initStandardObjects();
        for (File f : tests) {
            int length = (int) f.length(); // don't worry about very long
                                           // files
            char[] buf = new char[length];
            new FileReader(f).read(buf, 0, length);
            String session = new String(buf);
            runJsTest(cx, shared, f.getName(), session);
        }
    } finally {
        Context.exit();
    }
}
 
Example 2
Source File: ContextFactoryTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
public void testCustomContextFactory() {
    ContextFactory factory = new MyFactory();
    Context cx = factory.enterContext();
    try {
        Scriptable globalScope = cx.initStandardObjects();
        // Test that FEATURE_MEMBER_EXPR_AS_FUNCTION_NAME is enabled
        /* TODO(stevey): fix this functionality in parser
        Object result = cx.evaluateString(globalScope,
                "var obj = {};" +
                "function obj.foo() { return 'bar'; }" +
                "obj.foo();",
                "test source", 1, null);
        assertEquals("bar", result);
        */
    } catch (RhinoException e) {
        fail(e.toString());
    } finally {
        Context.exit();
    }
}
 
Example 3
Source File: DoctestsTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void runDoctest() throws Exception {
    ContextFactory factory = ContextFactory.getGlobal();
    Context cx = factory.enterContext();
    try {
        cx.setOptimizationLevel(optimizationLevel);
        Global global = new Global(cx);
        // global.runDoctest throws an exception on any failure
        int testsPassed = global.runDoctest(cx, global, source, name, 1);
        System.out.println(name + "(" + optimizationLevel + "): " +
                testsPassed + " passed.");
        assertTrue(testsPassed > 0);
    } catch (Exception ex) {
      System.out.println(name + "(" + optimizationLevel + "): FAILED due to "+ex);
      throw ex;
    } finally {
        Context.exit();
    }
}
 
Example 4
Source File: CustomSetterAcceptNullScriptableTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
public void testSetNullForScriptableSetter() throws Exception {

		final String scriptCode = "foo.myProp = new Foo2();\n"
			+ "foo.myProp = null;";

		final ContextFactory factory = new ContextFactory();
		final Context cx = factory.enterContext();

		try {
	        final ScriptableObject topScope = cx.initStandardObjects();
	        final Foo foo = new Foo();

	        // define custom setter method
	        final Method setMyPropMethod = Foo.class.getMethod("setMyProp", Foo2.class);
	        foo.defineProperty("myProp", null, null, setMyPropMethod, ScriptableObject.EMPTY);

	        topScope.put("foo", topScope, foo);

	        ScriptableObject.defineClass(topScope, Foo2.class);

	        cx.evaluateString(topScope, scriptCode, "myScript", 1, null);
		}
		finally {
			Context.exit();
		}
	}
 
Example 5
Source File: RhinoExpression.java    From pentaho-reporting with GNU Lesser General Public License v2.1 6 votes vote down vote up
/**
 * Evaluates the defined expression. If an exception or an evaluation error occures, the evaluation returns null and
 * the error is logged. The current datarow and a copy of the expressions properties are set to script-internal
 * variables. Changes to the properties will not alter the expressions original properties and will be lost when the
 * evaluation is finished.
 * <p/>
 * Expressions do not maintain a state and no assumptions about the order of evaluation can be made.
 *
 * @return the evaluated value or null.
 */
public Object getValue() {
  if ( expression == null ) {
    return null;
  }

  LegacyDataRowWrapper wrapper = null;
  try {
    final ContextFactory contextFactory = new ContextFactory();
    final Context context = contextFactory.enterContext();
    final Scriptable scope = context.initStandardObjects();
    wrapper = initializeScope( scope );

    final Object o = context.evaluateString( scope, expression, getName(), 1, null );
    if ( o instanceof NativeJavaObject ) {
      final NativeJavaObject object = (NativeJavaObject) o;
      return object.unwrap();
    }
    return o;
  } finally {
    if ( wrapper != null ) {
      wrapper.setParent( null );
    }
    Context.exit();
  }
}
 
Example 6
Source File: Utils.java    From rhino-android with Apache License 2.0 5 votes vote down vote up
/**
 * Runs the provided action at the given optimization level
 */
public static void runWithOptimizationLevel(final ContextFactory contextFactory, final ContextAction action, final int optimizationLevel)
{
   	final Context cx = contextFactory.enterContext();
   	try
   	{
   		cx.setOptimizationLevel(optimizationLevel);
   		action.run(cx);
   	}
   	finally
   	{
   		Context.exit();
   	}
}
 
Example 7
Source File: Utils.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Runs the provided action at the given optimization level
 */
public static void runWithOptimizationLevel(final ContextFactory contextFactory, final ContextAction action, final int optimizationLevel)
{
   	final Context cx = contextFactory.enterContext();
   	try
   	{
   		cx.setOptimizationLevel(optimizationLevel);
   		action.run(cx);
   	}
   	finally
   	{
   		Context.exit();
   	}
}