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

The following examples show how to use org.mozilla.javascript.ContextFactory#enterContext() . These examples are extracted from open source projects. 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 want to check out the right sidebar which shows the related API usage.
Example 1
Source Project: astor   File: JsTestsBase.java    License: 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
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 Project: astor   File: DoctestsTest.java    License: 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
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
/**
 * 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 Project: rhino-android   File: Utils.java    License: 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 Project: astor   File: Utils.java    License: 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();
   	}
}