Java Code Examples for org.mozilla.javascript.Context#compileReader()

The following examples show how to use org.mozilla.javascript.Context#compileReader() . 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: Bug482203Test.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
public void testJsApi() throws Exception {
    Context cx = Context.enter();
    try {
        cx.setOptimizationLevel(-1);
        Script script = cx.compileReader(new InputStreamReader(
                Bug482203Test.class.getResourceAsStream("Bug482203.js")),
                "", 1, null);
        Scriptable scope = cx.initStandardObjects();
        script.exec(cx, scope);
        int counter = 0;
        for(;;)
        {
            Object cont = ScriptableObject.getProperty(scope, "c");
            if(cont == null)
            {
                break;
            }
            counter++;
            ((Callable)cont).call(cx, scope, scope, new Object[] { null });
        }
        assertEquals(counter, 5);
        assertEquals(Double.valueOf(3), ScriptableObject.getProperty(scope, "result"));
    } finally {
        Context.exit();
    }
}
 
Example 2
Source File: Bug482203Test.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
public void testJavaApi() throws Exception {
    Context cx = Context.enter();
    try {
        cx.setOptimizationLevel(-1);
        Script script = cx.compileReader(new InputStreamReader(
                Bug482203Test.class.getResourceAsStream("Bug482203.js")),
                "", 1, null);
        Scriptable scope = cx.initStandardObjects();
        cx.executeScriptWithContinuations(script, scope);
        int counter = 0;
        for(;;)
        {
            Object cont = ScriptableObject.getProperty(scope, "c");
            if(cont == null)
            {
                break;
            }
            counter++;
            cx.resumeContinuation(cont, scope, null);
        }
        assertEquals(counter, 5);
        assertEquals(Double.valueOf(3), ScriptableObject.getProperty(scope, "result"));
    } finally {
    	Context.exit();
    }
}
 
Example 3
Source File: CachingModuleScriptProviderBase.java    From JsDroidCmd with Mozilla Public License 2.0 5 votes vote down vote up
public ModuleScript getModuleScript(Context cx, String moduleId,
        URI moduleUri, URI baseUri, Scriptable paths) throws Exception
{
    final CachedModuleScript cachedModule1 = getLoadedModule(moduleId);
    final Object validator1 = getValidator(cachedModule1);
    final ModuleSource moduleSource = (moduleUri == null)
            ? moduleSourceProvider.loadSource(moduleId, paths, validator1)
            : moduleSourceProvider.loadSource(moduleUri, baseUri, validator1);
    if(moduleSource == ModuleSourceProvider.NOT_MODIFIED) {
        return cachedModule1.getModule();
    }
    if(moduleSource == null) {
        return null;
    }
    final Reader reader = moduleSource.getReader();
    try {
        final int idHash = moduleId.hashCode();
        synchronized(loadLocks[(idHash >>> loadLockShift) & loadLockMask]) {
            final CachedModuleScript cachedModule2 = getLoadedModule(moduleId);
            if(cachedModule2 != null) {
                if(!equal(validator1, getValidator(cachedModule2))) {
                    return cachedModule2.getModule();
                }
            }
            final URI sourceUri = moduleSource.getUri();
            final ModuleScript moduleScript = new ModuleScript(
                    cx.compileReader(reader, sourceUri.toString(), 1,
                            moduleSource.getSecurityDomain()),
                    sourceUri, moduleSource.getBase());
            putLoadedModule(moduleId, moduleScript,
                    moduleSource.getValidator());
            return moduleScript;
        }
    }
    finally {
        reader.close();
    }
}
 
Example 4
Source File: JavaScriptSupport.java    From phoebus with Eclipse Public License 1.0 5 votes vote down vote up
/** Parse and compile script file
*
*  @param name Name of script (file name, URL)
*  @param stream Stream for the script content
*  @return {@link Script}
*  @throws Exception on error
*/
public Script compile(final String name, final InputStream stream) throws Exception
{
    final Context thread_context = Context.enter();
    try
    {
        final org.mozilla.javascript.Script code = thread_context.compileReader(new InputStreamReader(stream), name, 1, null);
        return new JavaScript(this, name, code);
    }
    finally
    {
        Context.exit();
    }
}
 
Example 5
Source File: CaliperSpiderBenchmark.java    From rhino-android with Apache License 2.0 5 votes vote down vote up
static Script compileBenchmark(Context cx, String fileName)
    throws IOException
{
    File f = new File(FILE_BASE, fileName);
    FileReader rdr = new FileReader(f);
    try {
        return cx.compileReader(rdr, fileName, 1, null);
    } finally {
        rdr.close();
    }
}
 
Example 6
Source File: CachingModuleScriptProviderBase.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
public ModuleScript getModuleScript(Context cx, String moduleId,
        URI moduleUri, URI baseUri, Scriptable paths) throws Exception
{
    final CachedModuleScript cachedModule1 = getLoadedModule(moduleId);
    final Object validator1 = getValidator(cachedModule1);
    final ModuleSource moduleSource = (moduleUri == null)
            ? moduleSourceProvider.loadSource(moduleId, paths, validator1)
            : moduleSourceProvider.loadSource(moduleUri, baseUri, validator1);
    if(moduleSource == ModuleSourceProvider.NOT_MODIFIED) {
        return cachedModule1.getModule();
    }
    if(moduleSource == null) {
        return null;
    }
    final Reader reader = moduleSource.getReader();
    try {
        final int idHash = moduleId.hashCode();
        synchronized(loadLocks[(idHash >>> loadLockShift) & loadLockMask]) {
            final CachedModuleScript cachedModule2 = getLoadedModule(moduleId);
            if(cachedModule2 != null) {
                if(!equal(validator1, getValidator(cachedModule2))) {
                    return cachedModule2.getModule();
                }
            }
            final URI sourceUri = moduleSource.getUri();
            final ModuleScript moduleScript = new ModuleScript(
                    cx.compileReader(reader, sourceUri.toString(), 1,
                            moduleSource.getSecurityDomain()),
                    sourceUri, moduleSource.getBase());
            putLoadedModule(moduleId, moduleScript,
                    moduleSource.getValidator());
            return moduleScript;
        }
    }
    finally {
        reader.close();
    }
}
 
Example 7
Source File: FunctionProviderBaseImpl.java    From birt with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Register script functions to scope.
 * 
 * @param cx
 * @param scope
 * @throws BirtException
 */
public void registerScriptFunction( Context cx, Scriptable scope )
		throws BirtException
{
	List<CategoryWrapper> wrapperedCategories = getWrapperedCategories( );
	for ( CategoryWrapper category : wrapperedCategories )
	{
		ScriptableObject.putProperty( scope,
				category.getClassName( ),
				category );
	}

	if ( !jarLibs.isEmpty( ) )
	{
		ClassLoader classLoader = cx.getApplicationClassLoader( );
		URLClassLoader scriptClassLoader = createScriptClassLoader( jarLibs,
				classLoader );
		setApplicationClassLoader( scriptClassLoader, cx );
	}
	for ( URL url : jsLibs )
	{
		Script script;
		try
		{
			script = cx.compileReader(
					new BufferedReader(
							new InputStreamReader( url.openStream( ) ) ),
					null,
					0,
					null );
			script.exec( cx, scope );
		}
		catch ( IOException e )
		{
		}
	}
}