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

The following examples show how to use org.mozilla.javascript.ContextFactory#getGlobal() . 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: 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 3
Source File: JavascriptEngineFactory.java    From birt with Eclipse Public License 1.0 6 votes vote down vote up
public static void destroyMyFactory( )
{
	ContextFactory factory = ContextFactory.getGlobal( );
	if ( factory != null && factory instanceof MyFactory )
	{
		try
		{
			Class factoryClass = Class
					.forName( "org.mozilla.javascript.ContextFactory" );
			Field field = factoryClass.getDeclaredField( "hasCustomGlobal" );
			field.setAccessible( true );
			field.setBoolean( factoryClass, false );
			field = factoryClass.getDeclaredField( "global" );
			field.setAccessible( true );
			field.set( factoryClass, new ContextFactory( ) );
		}
		catch ( Exception ex )
		{
			logger.log( Level.WARNING, ex.getMessage( ), ex );
		}

	}
}
 
Example 4
Source File: RhinoAndroidHelper.java    From rhino-android with Apache License 2.0 5 votes vote down vote up
/**
 * @return The Context factory which has to be used on android.
 */
@VisibleForTesting
public AndroidContextFactory getContextFactory() {
    AndroidContextFactory factory;
    if (!ContextFactory.hasExplicitGlobal()) {
        factory = new AndroidContextFactory(cacheDirectory);
        ContextFactory.getGlobalSetter().setContextFactoryGlobal(factory);
    } else if (!(ContextFactory.getGlobal() instanceof AndroidContextFactory)) {
        throw new IllegalStateException("Cannot initialize factory for Android Rhino: There is already another factory");
    } else {
        factory = (AndroidContextFactory) ContextFactory.getGlobal();
    }
    return factory;
}
 
Example 5
Source File: RhinoExecutor.java    From caja with Apache License 2.0 5 votes vote down vote up
public <T> T run(Map<String, ?> actuals, Class<T> expectedResultType)
    throws AbnormalExitException {
  if (SANDBOXINGFACTORY != ContextFactory.getGlobal()) {
    throw new IllegalStateException();
  }
  Context context = SANDBOXINGFACTORY.enterContext();
  // Don't bother to compile tests to a class file.  Removing this causes
  // a 5x slow-down in Rhino-heavy tests.
  context.setOptimizationLevel(-1);
  try {
    return runInContext(context, actuals, expectedResultType);
  } finally {
    Context.exit();
  }
}