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

The following examples show how to use org.mozilla.javascript.Context#getOptimizationLevel() . 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: Main.java    From JsDroidCmd with Mozilla Public License 2.0 5 votes vote down vote up
static void processFileSecure(Context cx, Scriptable scope,
                              String path, Object securityDomain)
        throws IOException {

    boolean isClass = path.endsWith(".class");
    Object source = readFileOrUrl(path, !isClass);

    byte[] digest = getDigest(source);
    String key = path + "_" + cx.getOptimizationLevel();
    ScriptReference ref = scriptCache.get(key, digest);
    Script script = ref != null ? ref.get() : null;

    if (script == null) {
        if (isClass) {
            script = loadCompiledScript(cx, path, (byte[])source, securityDomain);
        } else {
            String strSrc = (String) source;
            // Support the executable script #! syntax:  If
            // the first line begins with a '#', treat the whole
            // line as a comment.
            if (strSrc.length() > 0 && strSrc.charAt(0) == '#') {
                for (int i = 1; i != strSrc.length(); ++i) {
                    int c = strSrc.charAt(i);
                    if (c == '\n' || c == '\r') {
                        strSrc = strSrc.substring(i);
                        break;
                    }
                }
            }
            script = cx.compileString(strSrc, path, 1, securityDomain);
        }
        scriptCache.put(key, digest, script);
    }

    if (script != null) {
        script.exec(cx, scope);
    }
}
 
Example 2
Source File: Main.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
static void processFileSecure(Context cx, Scriptable scope,
                              String path, Object securityDomain)
        throws IOException {

    boolean isClass = path.endsWith(".class");
    Object source = readFileOrUrl(path, !isClass);

    byte[] digest = getDigest(source);
    String key = path + "_" + cx.getOptimizationLevel();
    ScriptReference ref = scriptCache.get(key, digest);
    Script script = ref != null ? ref.get() : null;

    if (script == null) {
        if (isClass) {
            script = loadCompiledScript(cx, path, (byte[])source, securityDomain);
        } else {
            String strSrc = (String) source;
            // Support the executable script #! syntax:  If
            // the first line begins with a '#', treat the whole
            // line as a comment.
            if (strSrc.length() > 0 && strSrc.charAt(0) == '#') {
                for (int i = 1; i != strSrc.length(); ++i) {
                    int c = strSrc.charAt(i);
                    if (c == '\n' || c == '\r') {
                        strSrc = strSrc.substring(i);
                        break;
                    }
                }
            }
            script = cx.compileString(strSrc, path, 1, securityDomain);
        }
        scriptCache.put(key, digest, script);
    }

    if (script != null) {
        script.exec(cx, scope);
    }
}
 
Example 3
Source File: JsUtil.java    From birt with Eclipse Public License 1.0 5 votes vote down vote up
static boolean check( String source, int lineNumber )
{
	Context cx = Context.enter( );

	Debugger oldDebugger = cx.getDebugger( );
	Object oldContext = cx.getDebuggerContextData( );
	boolean oldGenerate = cx.isGeneratingDebug( );
	int oldLevel = cx.getOptimizationLevel( );

	try
	{
		BreakableSourceChecker checker = new BreakableSourceChecker( );
		checker.lineNumber = lineNumber + 2;

		cx.setDebugger( checker, null );
		cx.setGeneratingDebug( true );
		cx.setOptimizationLevel( -1 );

		cx.compileString( addHeader( source ), "<check>", 1, null ); //$NON-NLS-1$

		return checker.breakable;
	}
	catch ( Exception e )
	{
		return false;
	}
	finally
	{
		cx.setDebugger( oldDebugger, oldContext );
		cx.setGeneratingDebug( oldGenerate );
		cx.setOptimizationLevel( oldLevel );

		Context.exit( );
	}
}
 
Example 4
Source File: ProviderFactory.java    From cxf with Apache License 2.0 5 votes vote down vote up
private Object[] compileScript(Context cx, String scriptStr, Scriptable scriptScope, File f) {
    int opt = cx.getOptimizationLevel();
    cx.setOptimizationLevel(-1);
    Script script = cx.compileString(scriptStr, f.getName(), 1, null);
    script.exec(cx, scriptScope);
    Object[] ids = scriptScope.getIds();
    cx.setOptimizationLevel(opt);
    script = cx.compileString(scriptStr, f.getName(), 1, null);
    script.exec(cx, scriptScope);
    return ids;
}