Java Code Examples for jdk.nashorn.internal.codegen.Compiler#compile()

The following examples show how to use jdk.nashorn.internal.codegen.Compiler#compile() . 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: RecompilableScriptFunctionData.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
private FunctionInitializer compileTypeSpecialization(final MethodType actualCallSiteType, final ScriptObject runtimeScope, final boolean persist) {
    // We're creating an empty script object for holding local variables. AssignSymbols will populate it with
    // explicit Undefined values for undefined local variables (see AssignSymbols#defineSymbol() and
    // CompilationEnvironment#declareLocalSymbol()).

    if (log.isEnabled()) {
        log.info("Parameter type specialization of '", functionName, "' signature: ", actualCallSiteType);
    }

    final boolean persistentCache = persist && usePersistentCodeCache();
    String cacheKey = null;
    if (persistentCache) {
        final TypeMap typeMap = typeMap(actualCallSiteType);
        final Type[] paramTypes = typeMap == null ? null : typeMap.getParameterTypes(functionNodeId);
        cacheKey = CodeStore.getCacheKey(functionNodeId, paramTypes);
        final CodeInstaller newInstaller = getInstallerForNewCode();
        final StoredScript script = newInstaller.loadScript(source, cacheKey);

        if (script != null) {
            Compiler.updateCompilationId(script.getCompilationId());
            return script.installFunction(this, newInstaller);
        }
    }

    final FunctionNode fn = reparse();
    final Compiler compiler = getCompiler(fn, actualCallSiteType, runtimeScope);
    final FunctionNode compiledFn = compiler.compile(fn,
            fn.isCached() ? CompilationPhases.COMPILE_ALL_CACHED : CompilationPhases.COMPILE_ALL);

    if (persist && !compiledFn.hasApplyToCallSpecialization()) {
        compiler.persistClassInfo(cacheKey, compiledFn);
    }
    return new FunctionInitializer(compiledFn, compiler.getInvalidatedProgramPoints());
}
 
Example 2
Source File: RecompilableScriptFunctionData.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
private FunctionInitializer compileTypeSpecialization(final MethodType actualCallSiteType, final ScriptObject runtimeScope, final boolean persist) {
    // We're creating an empty script object for holding local variables. AssignSymbols will populate it with
    // explicit Undefined values for undefined local variables (see AssignSymbols#defineSymbol() and
    // CompilationEnvironment#declareLocalSymbol()).

    if (log.isEnabled()) {
        log.info("Parameter type specialization of '", functionName, "' signature: ", actualCallSiteType);
    }

    final boolean persistentCache = persist && usePersistentCodeCache();
    String cacheKey = null;
    if (persistentCache) {
        final TypeMap typeMap = typeMap(actualCallSiteType);
        final Type[] paramTypes = typeMap == null ? null : typeMap.getParameterTypes(functionNodeId);
        cacheKey = CodeStore.getCacheKey(functionNodeId, paramTypes);
        final CodeInstaller<ScriptEnvironment> newInstaller = getInstallerForNewCode();
        final StoredScript script = newInstaller.loadScript(source, cacheKey);

        if (script != null) {
            Compiler.updateCompilationId(script.getCompilationId());
            return script.installFunction(this, newInstaller);
        }
    }

    final FunctionNode fn = reparse();
    final Compiler compiler = getCompiler(fn, actualCallSiteType, runtimeScope);
    final FunctionNode compiledFn = compiler.compile(fn,
            isSerialized() ? CompilationPhases.COMPILE_ALL_SERIALIZED : CompilationPhases.COMPILE_ALL);

    if (persist && !compiledFn.getFlag(FunctionNode.HAS_APPLY_TO_CALL_SPECIALIZATION)) {
        compiler.persistClassInfo(cacheKey, compiledFn);
    }
    return new FunctionInitializer(compiledFn, compiler.getInvalidatedProgramPoints());
}
 
Example 3
Source File: RecompilableScriptFunctionData.java    From jdk8u_nashorn with GNU General Public License v2.0 5 votes vote down vote up
private FunctionInitializer compileTypeSpecialization(final MethodType actualCallSiteType, final ScriptObject runtimeScope, final boolean persist) {
    // We're creating an empty script object for holding local variables. AssignSymbols will populate it with
    // explicit Undefined values for undefined local variables (see AssignSymbols#defineSymbol() and
    // CompilationEnvironment#declareLocalSymbol()).

    if (log.isEnabled()) {
        log.info("Parameter type specialization of '", functionName, "' signature: ", actualCallSiteType);
    }

    final boolean persistentCache = persist && usePersistentCodeCache();
    String cacheKey = null;
    if (persistentCache) {
        final TypeMap typeMap = typeMap(actualCallSiteType);
        final Type[] paramTypes = typeMap == null ? null : typeMap.getParameterTypes(functionNodeId);
        cacheKey = CodeStore.getCacheKey(functionNodeId, paramTypes);
        final CodeInstaller newInstaller = getInstallerForNewCode();
        final StoredScript script = newInstaller.loadScript(source, cacheKey);

        if (script != null) {
            Compiler.updateCompilationId(script.getCompilationId());
            return script.installFunction(this, newInstaller);
        }
    }

    final FunctionNode fn = reparse();
    final Compiler compiler = getCompiler(fn, actualCallSiteType, runtimeScope);
    final FunctionNode compiledFn = compiler.compile(fn,
            fn.isCached() ? CompilationPhases.COMPILE_ALL_CACHED : CompilationPhases.COMPILE_ALL);

    if (persist && !compiledFn.hasApplyToCallSpecialization()) {
        compiler.persistClassInfo(cacheKey, compiledFn);
    }
    return new FunctionInitializer(compiledFn, compiler.getInvalidatedProgramPoints());
}
 
Example 4
Source File: RecompilableScriptFunctionData.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
private FunctionInitializer compileTypeSpecialization(final MethodType actualCallSiteType, final ScriptObject runtimeScope, final boolean persist) {
    // We're creating an empty script object for holding local variables. AssignSymbols will populate it with
    // explicit Undefined values for undefined local variables (see AssignSymbols#defineSymbol() and
    // CompilationEnvironment#declareLocalSymbol()).

    if (log.isEnabled()) {
        log.info("Parameter type specialization of '", functionName, "' signature: ", actualCallSiteType);
    }

    final boolean persistentCache = persist && usePersistentCodeCache();
    String cacheKey = null;
    if (persistentCache) {
        final TypeMap typeMap = typeMap(actualCallSiteType);
        final Type[] paramTypes = typeMap == null ? null : typeMap.getParameterTypes(functionNodeId);
        cacheKey = CodeStore.getCacheKey(functionNodeId, paramTypes);
        final CodeInstaller newInstaller = getInstallerForNewCode();
        final StoredScript script = newInstaller.loadScript(source, cacheKey);

        if (script != null) {
            Compiler.updateCompilationId(script.getCompilationId());
            return script.installFunction(this, newInstaller);
        }
    }

    final FunctionNode fn = reparse();
    final Compiler compiler = getCompiler(fn, actualCallSiteType, runtimeScope);
    final FunctionNode compiledFn = compiler.compile(fn,
            fn.isCached() ? CompilationPhases.COMPILE_ALL_CACHED : CompilationPhases.COMPILE_ALL);

    if (persist && !compiledFn.hasApplyToCallSpecialization()) {
        compiler.persistClassInfo(cacheKey, compiledFn);
    }
    return new FunctionInitializer(compiledFn, compiler.getInvalidatedProgramPoints());
}
 
Example 5
Source File: RecompilableScriptFunctionData.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
private FunctionInitializer compileTypeSpecialization(final MethodType actualCallSiteType, final ScriptObject runtimeScope, final boolean persist) {
    // We're creating an empty script object for holding local variables. AssignSymbols will populate it with
    // explicit Undefined values for undefined local variables (see AssignSymbols#defineSymbol() and
    // CompilationEnvironment#declareLocalSymbol()).

    if (log.isEnabled()) {
        log.info("Parameter type specialization of '", functionName, "' signature: ", actualCallSiteType);
    }

    final boolean persistentCache = persist && usePersistentCodeCache();
    String cacheKey = null;
    if (persistentCache) {
        final TypeMap typeMap = typeMap(actualCallSiteType);
        final Type[] paramTypes = typeMap == null ? null : typeMap.getParameterTypes(functionNodeId);
        cacheKey = CodeStore.getCacheKey(functionNodeId, paramTypes);
        final CodeInstaller newInstaller = getInstallerForNewCode();
        final StoredScript script = newInstaller.loadScript(source, cacheKey);

        if (script != null) {
            Compiler.updateCompilationId(script.getCompilationId());
            return script.installFunction(this, newInstaller);
        }
    }

    final FunctionNode fn = reparse();
    final Compiler compiler = getCompiler(fn, actualCallSiteType, runtimeScope);
    final FunctionNode compiledFn = compiler.compile(fn,
            fn.isCached() ? CompilationPhases.COMPILE_ALL_CACHED : CompilationPhases.COMPILE_ALL);

    if (persist && !compiledFn.hasApplyToCallSpecialization()) {
        compiler.persistClassInfo(cacheKey, compiledFn);
    }
    return new FunctionInitializer(compiledFn, compiler.getInvalidatedProgramPoints());
}
 
Example 6
Source File: RecompilableScriptFunctionData.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
private FunctionInitializer compileTypeSpecialization(final MethodType actualCallSiteType, final ScriptObject runtimeScope, final boolean persist) {
    // We're creating an empty script object for holding local variables. AssignSymbols will populate it with
    // explicit Undefined values for undefined local variables (see AssignSymbols#defineSymbol() and
    // CompilationEnvironment#declareLocalSymbol()).

    if (log.isEnabled()) {
        log.info("Parameter type specialization of '", functionName, "' signature: ", actualCallSiteType);
    }

    final boolean persistentCache = persist && usePersistentCodeCache();
    String cacheKey = null;
    if (persistentCache) {
        final TypeMap typeMap = typeMap(actualCallSiteType);
        final Type[] paramTypes = typeMap == null ? null : typeMap.getParameterTypes(functionNodeId);
        cacheKey = CodeStore.getCacheKey(functionNodeId, paramTypes);
        final CodeInstaller newInstaller = getInstallerForNewCode();
        final StoredScript script = newInstaller.loadScript(source, cacheKey);

        if (script != null) {
            Compiler.updateCompilationId(script.getCompilationId());
            return script.installFunction(this, newInstaller);
        }
    }

    final FunctionNode fn = reparse();
    final Compiler compiler = getCompiler(fn, actualCallSiteType, runtimeScope);
    final FunctionNode compiledFn = compiler.compile(fn,
            fn.isCached() ? CompilationPhases.COMPILE_ALL_CACHED : CompilationPhases.COMPILE_ALL);

    if (persist && !compiledFn.hasApplyToCallSpecialization()) {
        compiler.persistClassInfo(cacheKey, compiledFn);
    }
    return new FunctionInitializer(compiledFn, compiler.getInvalidatedProgramPoints());
}
 
Example 7
Source File: RecompilableScriptFunctionData.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
private FunctionInitializer compileTypeSpecialization(final MethodType actualCallSiteType, final ScriptObject runtimeScope, final boolean persist) {
    // We're creating an empty script object for holding local variables. AssignSymbols will populate it with
    // explicit Undefined values for undefined local variables (see AssignSymbols#defineSymbol() and
    // CompilationEnvironment#declareLocalSymbol()).

    if (log.isEnabled()) {
        log.info("Parameter type specialization of '", functionName, "' signature: ", actualCallSiteType);
    }

    final boolean persistentCache = persist && usePersistentCodeCache();
    String cacheKey = null;
    if (persistentCache) {
        final TypeMap typeMap = typeMap(actualCallSiteType);
        final Type[] paramTypes = typeMap == null ? null : typeMap.getParameterTypes(functionNodeId);
        cacheKey = CodeStore.getCacheKey(functionNodeId, paramTypes);
        final CodeInstaller newInstaller = getInstallerForNewCode();
        final StoredScript script = newInstaller.loadScript(source, cacheKey);

        if (script != null) {
            Compiler.updateCompilationId(script.getCompilationId());
            return script.installFunction(this, newInstaller);
        }
    }

    final FunctionNode fn = reparse();
    final Compiler compiler = getCompiler(fn, actualCallSiteType, runtimeScope);
    final FunctionNode compiledFn = compiler.compile(fn,
            fn.isCached() ? CompilationPhases.COMPILE_ALL_CACHED : CompilationPhases.COMPILE_ALL);

    if (persist && !compiledFn.hasApplyToCallSpecialization()) {
        compiler.persistClassInfo(cacheKey, compiledFn);
    }
    return new FunctionInitializer(compiledFn, compiler.getInvalidatedProgramPoints());
}
 
Example 8
Source File: Context.java    From nashorn with GNU General Public License v2.0 4 votes vote down vote up
private synchronized Class<?> compile(final Source source, final ErrorManager errMan, final boolean strict) {
    // start with no errors, no warnings.
    errMan.reset();

    GlobalObject global = null;
    Class<?> script;

    if (env._class_cache_size > 0) {
        global = (GlobalObject)Context.getGlobalTrusted();
        script = global.findCachedClass(source);
        if (script != null) {
            Compiler.LOG.fine("Code cache hit for ", source, " avoiding recompile.");
            return script;
        }
    }

    final FunctionNode functionNode = new Parser(env, source, errMan, strict).parse();
    if (errors.hasErrors()) {
        return null;
    }

    if (env._print_ast) {
        getErr().println(new ASTWriter(functionNode));
    }

    if (env._print_parse) {
        getErr().println(new PrintVisitor(functionNode));
    }

    if (env._parse_only) {
        return null;
    }

    final URL          url    = source.getURL();
    final ScriptLoader loader = env._loader_per_compile ? createNewLoader() : scriptLoader;
    final CodeSource   cs     = url == null ? null : new CodeSource(url, (CodeSigner[])null);
    final CodeInstaller<ScriptEnvironment> installer = new ContextCodeInstaller(this, loader, cs);

    final Compiler compiler = new Compiler(installer, strict);

    final FunctionNode newFunctionNode = compiler.compile(functionNode);
    script = compiler.install(newFunctionNode);

    if (global != null) {
        global.cacheClass(source, script);
    }

    return script;
}
 
Example 9
Source File: Context.java    From TencentKona-8 with GNU General Public License v2.0 4 votes vote down vote up
private synchronized Class<?> compile(final Source source, final ErrorManager errMan, final boolean strict) {
    // start with no errors, no warnings.
    errMan.reset();

    Class<?> script = findCachedClass(source);
    if (script != null) {
        final DebugLogger log = getLogger(Compiler.class);
        if (log.isEnabled()) {
            log.fine(new RuntimeEvent<>(Level.INFO, source), "Code cache hit for ", source, " avoiding recompile.");
        }
        return script;
    }

    StoredScript storedScript = null;
    FunctionNode functionNode = null;
    // Don't use code store if optimistic types is enabled but lazy compilation is not.
    // This would store a full script compilation with many wrong optimistic assumptions that would
    // do more harm than good on later runs with both optimistic types and lazy compilation enabled.
    final boolean useCodeStore = codeStore != null && !env._parse_only && (!env._optimistic_types || env._lazy_compilation);
    final String cacheKey = useCodeStore ? CodeStore.getCacheKey("script", null) : null;

    if (useCodeStore) {
        storedScript = codeStore.load(source, cacheKey);
    }

    if (storedScript == null) {
        if (env._dest_dir != null) {
            source.dump(env._dest_dir);
        }

        functionNode = new Parser(env, source, errMan, strict, getLogger(Parser.class)).parse();

        if (errMan.hasErrors()) {
            return null;
        }

        if (env._print_ast || functionNode.getFlag(FunctionNode.IS_PRINT_AST)) {
            getErr().println(new ASTWriter(functionNode));
        }

        if (env._print_parse || functionNode.getFlag(FunctionNode.IS_PRINT_PARSE)) {
            getErr().println(new PrintVisitor(functionNode, true, false));
        }
    }

    if (env._parse_only) {
        return null;
    }

    final URL          url    = source.getURL();
    final ScriptLoader loader = env._loader_per_compile ? createNewLoader() : scriptLoader;
    final CodeSource   cs     = new CodeSource(url, (CodeSigner[])null);
    final CodeInstaller installer = new ContextCodeInstaller(this, loader, cs);

    if (storedScript == null) {
        final CompilationPhases phases = Compiler.CompilationPhases.COMPILE_ALL;

        final Compiler compiler = Compiler.forInitialCompilation(
                installer,
                source,
                errMan,
                strict | functionNode.isStrict());

        final FunctionNode compiledFunction = compiler.compile(functionNode, phases);
        if (errMan.hasErrors()) {
            return null;
        }
        script = compiledFunction.getRootClass();
        compiler.persistClassInfo(cacheKey, compiledFunction);
    } else {
        Compiler.updateCompilationId(storedScript.getCompilationId());
        script = storedScript.installScript(source, installer);
    }

    cacheClass(source, script);
    return script;
}
 
Example 10
Source File: Context.java    From jdk8u_nashorn with GNU General Public License v2.0 4 votes vote down vote up
private synchronized Class<?> compile(final Source source, final ErrorManager errMan, final boolean strict) {
    // start with no errors, no warnings.
    errMan.reset();

    Class<?> script = findCachedClass(source);
    if (script != null) {
        final DebugLogger log = getLogger(Compiler.class);
        if (log.isEnabled()) {
            log.fine(new RuntimeEvent<>(Level.INFO, source), "Code cache hit for ", source, " avoiding recompile.");
        }
        return script;
    }

    StoredScript storedScript = null;
    FunctionNode functionNode = null;
    // Don't use code store if optimistic types is enabled but lazy compilation is not.
    // This would store a full script compilation with many wrong optimistic assumptions that would
    // do more harm than good on later runs with both optimistic types and lazy compilation enabled.
    final boolean useCodeStore = codeStore != null && !env._parse_only && (!env._optimistic_types || env._lazy_compilation);
    final String cacheKey = useCodeStore ? CodeStore.getCacheKey("script", null) : null;

    if (useCodeStore) {
        storedScript = codeStore.load(source, cacheKey);
    }

    if (storedScript == null) {
        if (env._dest_dir != null) {
            source.dump(env._dest_dir);
        }

        functionNode = new Parser(env, source, errMan, strict, getLogger(Parser.class)).parse();

        if (errMan.hasErrors()) {
            return null;
        }

        if (env._print_ast || functionNode.getFlag(FunctionNode.IS_PRINT_AST)) {
            getErr().println(new ASTWriter(functionNode));
        }

        if (env._print_parse || functionNode.getFlag(FunctionNode.IS_PRINT_PARSE)) {
            getErr().println(new PrintVisitor(functionNode, true, false));
        }
    }

    if (env._parse_only) {
        return null;
    }

    final URL          url    = source.getURL();
    final ScriptLoader loader = env._loader_per_compile ? createNewLoader() : scriptLoader;
    final CodeSource   cs     = new CodeSource(url, (CodeSigner[])null);
    final CodeInstaller installer = new ContextCodeInstaller(this, loader, cs);

    if (storedScript == null) {
        final CompilationPhases phases = Compiler.CompilationPhases.COMPILE_ALL;

        final Compiler compiler = Compiler.forInitialCompilation(
                installer,
                source,
                errMan,
                strict | functionNode.isStrict());

        final FunctionNode compiledFunction = compiler.compile(functionNode, phases);
        if (errMan.hasErrors()) {
            return null;
        }
        script = compiledFunction.getRootClass();
        compiler.persistClassInfo(cacheKey, compiledFunction);
    } else {
        Compiler.updateCompilationId(storedScript.getCompilationId());
        script = storedScript.installScript(source, installer);
    }

    cacheClass(source, script);
    return script;
}
 
Example 11
Source File: Context.java    From openjdk-8 with GNU General Public License v2.0 4 votes vote down vote up
private synchronized Class<?> compile(final Source source, final ErrorManager errMan, final boolean strict) {
    // start with no errors, no warnings.
    errMan.reset();

    GlobalObject global = null;
    Class<?> script;

    if (env._class_cache_size > 0) {
        global = (GlobalObject)Context.getGlobalTrusted();
        script = global.findCachedClass(source);
        if (script != null) {
            Compiler.LOG.fine("Code cache hit for ", source, " avoiding recompile.");
            return script;
        }
    }

    final FunctionNode functionNode = new Parser(env, source, errMan, strict).parse();
    if (errors.hasErrors()) {
        return null;
    }

    if (env._print_ast) {
        getErr().println(new ASTWriter(functionNode));
    }

    if (env._print_parse) {
        getErr().println(new PrintVisitor(functionNode));
    }

    if (env._parse_only) {
        return null;
    }

    final URL          url    = source.getURL();
    final ScriptLoader loader = env._loader_per_compile ? createNewLoader() : scriptLoader;
    final CodeSource   cs     = url == null ? null : new CodeSource(url, (CodeSigner[])null);
    final CodeInstaller<ScriptEnvironment> installer = new ContextCodeInstaller(this, loader, cs);

    final Compiler compiler = new Compiler(installer, strict);

    final FunctionNode newFunctionNode = compiler.compile(functionNode);
    script = compiler.install(newFunctionNode);

    if (global != null) {
        global.cacheClass(source, script);
    }

    return script;
}
 
Example 12
Source File: Context.java    From openjdk-8-source with GNU General Public License v2.0 4 votes vote down vote up
private synchronized Class<?> compile(final Source source, final ErrorManager errMan, final boolean strict) {
    // start with no errors, no warnings.
    errMan.reset();

    GlobalObject global = null;
    Class<?> script;

    if (env._class_cache_size > 0) {
        global = (GlobalObject)Context.getGlobalTrusted();
        script = global.findCachedClass(source);
        if (script != null) {
            Compiler.LOG.fine("Code cache hit for ", source, " avoiding recompile.");
            return script;
        }
    }

    final FunctionNode functionNode = new Parser(env, source, errMan, strict).parse();
    if (errors.hasErrors()) {
        return null;
    }

    if (env._print_ast) {
        getErr().println(new ASTWriter(functionNode));
    }

    if (env._print_parse) {
        getErr().println(new PrintVisitor(functionNode));
    }

    if (env._parse_only) {
        return null;
    }

    final URL          url    = source.getURL();
    final ScriptLoader loader = env._loader_per_compile ? createNewLoader() : scriptLoader;
    final CodeSource   cs     = url == null ? null : new CodeSource(url, (CodeSigner[])null);
    final CodeInstaller<ScriptEnvironment> installer = new ContextCodeInstaller(this, loader, cs);

    final Compiler compiler = new Compiler(installer, strict);

    final FunctionNode newFunctionNode = compiler.compile(functionNode);
    script = compiler.install(newFunctionNode);

    if (global != null) {
        global.cacheClass(source, script);
    }

    return script;
}
 
Example 13
Source File: Context.java    From hottub with GNU General Public License v2.0 4 votes vote down vote up
private synchronized Class<?> compile(final Source source, final ErrorManager errMan, final boolean strict) {
    // start with no errors, no warnings.
    errMan.reset();

    Class<?> script = findCachedClass(source);
    if (script != null) {
        final DebugLogger log = getLogger(Compiler.class);
        if (log.isEnabled()) {
            log.fine(new RuntimeEvent<>(Level.INFO, source), "Code cache hit for ", source, " avoiding recompile.");
        }
        return script;
    }

    StoredScript storedScript = null;
    FunctionNode functionNode = null;
    // Don't use code store if optimistic types is enabled but lazy compilation is not.
    // This would store a full script compilation with many wrong optimistic assumptions that would
    // do more harm than good on later runs with both optimistic types and lazy compilation enabled.
    final boolean useCodeStore = codeStore != null && !env._parse_only && (!env._optimistic_types || env._lazy_compilation);
    final String cacheKey = useCodeStore ? CodeStore.getCacheKey("script", null) : null;

    if (useCodeStore) {
        storedScript = codeStore.load(source, cacheKey);
    }

    if (storedScript == null) {
        if (env._dest_dir != null) {
            source.dump(env._dest_dir);
        }

        functionNode = new Parser(env, source, errMan, strict, getLogger(Parser.class)).parse();

        if (errMan.hasErrors()) {
            return null;
        }

        if (env._print_ast || functionNode.getFlag(FunctionNode.IS_PRINT_AST)) {
            getErr().println(new ASTWriter(functionNode));
        }

        if (env._print_parse || functionNode.getFlag(FunctionNode.IS_PRINT_PARSE)) {
            getErr().println(new PrintVisitor(functionNode, true, false));
        }
    }

    if (env._parse_only) {
        return null;
    }

    final URL          url    = source.getURL();
    final ScriptLoader loader = env._loader_per_compile ? createNewLoader() : scriptLoader;
    final CodeSource   cs     = new CodeSource(url, (CodeSigner[])null);
    final CodeInstaller installer = new ContextCodeInstaller(this, loader, cs);

    if (storedScript == null) {
        final CompilationPhases phases = Compiler.CompilationPhases.COMPILE_ALL;

        final Compiler compiler = Compiler.forInitialCompilation(
                installer,
                source,
                errMan,
                strict | functionNode.isStrict());

        final FunctionNode compiledFunction = compiler.compile(functionNode, phases);
        if (errMan.hasErrors()) {
            return null;
        }
        script = compiledFunction.getRootClass();
        compiler.persistClassInfo(cacheKey, compiledFunction);
    } else {
        Compiler.updateCompilationId(storedScript.getCompilationId());
        script = storedScript.installScript(source, installer);
    }

    cacheClass(source, script);
    return script;
}
 
Example 14
Source File: Context.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
private synchronized Class<?> compile(final Source source, final ErrorManager errMan, final boolean strict, final boolean isEval) {
    // start with no errors, no warnings.
    errMan.reset();

    Class<?> script = findCachedClass(source);
    if (script != null) {
        final DebugLogger log = getLogger(Compiler.class);
        if (log.isEnabled()) {
            log.fine(new RuntimeEvent<>(Level.INFO, source), "Code cache hit for ", source, " avoiding recompile.");
        }
        return script;
    }

    StoredScript storedScript = null;
    FunctionNode functionNode = null;
    // Don't use code store if optimistic types is enabled but lazy compilation is not.
    // This would store a full script compilation with many wrong optimistic assumptions that would
    // do more harm than good on later runs with both optimistic types and lazy compilation enabled.
    final boolean useCodeStore = codeStore != null && !env._parse_only && (!env._optimistic_types || env._lazy_compilation);
    final String cacheKey = useCodeStore ? CodeStore.getCacheKey("script", null) : null;

    if (useCodeStore) {
        storedScript = codeStore.load(source, cacheKey);
    }

    if (storedScript == null) {
        if (env._dest_dir != null) {
            source.dump(env._dest_dir);
        }

        functionNode = new Parser(env, source, errMan, strict, getLogger(Parser.class)).parse();

        if (errMan.hasErrors()) {
            return null;
        }

        if (env._print_ast || functionNode.getDebugFlag(FunctionNode.DEBUG_PRINT_AST)) {
            getErr().println(new ASTWriter(functionNode));
        }

        if (env._print_parse || functionNode.getDebugFlag(FunctionNode.DEBUG_PRINT_PARSE)) {
            getErr().println(new PrintVisitor(functionNode, true, false));
        }
    }

    if (env._parse_only) {
        return null;
    }

    final URL          url    = source.getURL();
    final CodeSource   cs     = new CodeSource(url, (CodeSigner[])null);
    final CodeInstaller installer;
    if (!env.useAnonymousClasses(source.getLength()) || env._persistent_cache || !env._lazy_compilation) {
        // Persistent code cache and eager compilation preclude use of VM anonymous classes
        final ScriptLoader loader = env._loader_per_compile ? createNewLoader() : scriptLoader;
        installer = new NamedContextCodeInstaller(this, cs, loader);
    } else {
        installer = new AnonymousContextCodeInstaller(this, cs,
                anonymousHostClasses.getOrCreate(cs, (key) ->
                        createNewLoader().installClass(
                                // NOTE: we're defining these constants in AnonymousContextCodeInstaller so they are not
                                // initialized if we don't use AnonymousContextCodeInstaller. As this method is only ever
                                // invoked from AnonymousContextCodeInstaller, this is okay.
                                AnonymousContextCodeInstaller.ANONYMOUS_HOST_CLASS_NAME,
                                AnonymousContextCodeInstaller.ANONYMOUS_HOST_CLASS_BYTES, cs)));
    }

    if (storedScript == null) {
        final CompilationPhases phases = Compiler.CompilationPhases.COMPILE_ALL;

        final Compiler compiler = Compiler.forInitialCompilation(
                installer,
                source,
                errMan,
                strict | functionNode.isStrict());

        final FunctionNode compiledFunction = compiler.compile(functionNode, phases);
        if (errMan.hasErrors()) {
            return null;
        }
        script = compiledFunction.getRootClass();
        compiler.persistClassInfo(cacheKey, compiledFunction);
    } else {
        Compiler.updateCompilationId(storedScript.getCompilationId());
        script = storedScript.installScript(source, installer);
    }

    cacheClass(source, script);
    return script;
}
 
Example 15
Source File: Context.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 4 votes vote down vote up
private synchronized Class<?> compile(final Source source, final ErrorManager errMan, final boolean strict) {
    // start with no errors, no warnings.
    errMan.reset();

    Class<?> script = findCachedClass(source);
    if (script != null) {
        final DebugLogger log = getLogger(Compiler.class);
        if (log.isEnabled()) {
            log.fine(new RuntimeEvent<>(Level.INFO, source), "Code cache hit for ", source, " avoiding recompile.");
        }
        return script;
    }

    StoredScript storedScript = null;
    FunctionNode functionNode = null;
    // Don't use code store if optimistic types is enabled but lazy compilation is not.
    // This would store a full script compilation with many wrong optimistic assumptions that would
    // do more harm than good on later runs with both optimistic types and lazy compilation enabled.
    final boolean useCodeStore = codeStore != null && !env._parse_only && (!env._optimistic_types || env._lazy_compilation);
    final String cacheKey = useCodeStore ? CodeStore.getCacheKey("script", null) : null;

    if (useCodeStore) {
        storedScript = codeStore.load(source, cacheKey);
    }

    if (storedScript == null) {
        if (env._dest_dir != null) {
            source.dump(env._dest_dir);
        }

        functionNode = new Parser(env, source, errMan, strict, getLogger(Parser.class)).parse();

        if (errMan.hasErrors()) {
            return null;
        }

        if (env._print_ast || functionNode.getFlag(FunctionNode.IS_PRINT_AST)) {
            getErr().println(new ASTWriter(functionNode));
        }

        if (env._print_parse || functionNode.getFlag(FunctionNode.IS_PRINT_PARSE)) {
            getErr().println(new PrintVisitor(functionNode, true, false));
        }
    }

    if (env._parse_only) {
        return null;
    }

    final URL          url    = source.getURL();
    final ScriptLoader loader = env._loader_per_compile ? createNewLoader() : scriptLoader;
    final CodeSource   cs     = new CodeSource(url, (CodeSigner[])null);
    final CodeInstaller installer = new ContextCodeInstaller(this, loader, cs);

    if (storedScript == null) {
        final CompilationPhases phases = Compiler.CompilationPhases.COMPILE_ALL;

        final Compiler compiler = Compiler.forInitialCompilation(
                installer,
                source,
                errMan,
                strict | functionNode.isStrict());

        final FunctionNode compiledFunction = compiler.compile(functionNode, phases);
        if (errMan.hasErrors()) {
            return null;
        }
        script = compiledFunction.getRootClass();
        compiler.persistClassInfo(cacheKey, compiledFunction);
    } else {
        Compiler.updateCompilationId(storedScript.getCompilationId());
        script = storedScript.installScript(source, installer);
    }

    cacheClass(source, script);
    return script;
}
 
Example 16
Source File: Context.java    From openjdk-jdk8u with GNU General Public License v2.0 4 votes vote down vote up
private synchronized Class<?> compile(final Source source, final ErrorManager errMan, final boolean strict) {
    // start with no errors, no warnings.
    errMan.reset();

    Class<?> script = findCachedClass(source);
    if (script != null) {
        final DebugLogger log = getLogger(Compiler.class);
        if (log.isEnabled()) {
            log.fine(new RuntimeEvent<>(Level.INFO, source), "Code cache hit for ", source, " avoiding recompile.");
        }
        return script;
    }

    StoredScript storedScript = null;
    FunctionNode functionNode = null;
    // Don't use code store if optimistic types is enabled but lazy compilation is not.
    // This would store a full script compilation with many wrong optimistic assumptions that would
    // do more harm than good on later runs with both optimistic types and lazy compilation enabled.
    final boolean useCodeStore = codeStore != null && !env._parse_only && (!env._optimistic_types || env._lazy_compilation);
    final String cacheKey = useCodeStore ? CodeStore.getCacheKey("script", null) : null;

    if (useCodeStore) {
        storedScript = codeStore.load(source, cacheKey);
    }

    if (storedScript == null) {
        if (env._dest_dir != null) {
            source.dump(env._dest_dir);
        }

        functionNode = new Parser(env, source, errMan, strict, getLogger(Parser.class)).parse();

        if (errMan.hasErrors()) {
            return null;
        }

        if (env._print_ast || functionNode.getFlag(FunctionNode.IS_PRINT_AST)) {
            getErr().println(new ASTWriter(functionNode));
        }

        if (env._print_parse || functionNode.getFlag(FunctionNode.IS_PRINT_PARSE)) {
            getErr().println(new PrintVisitor(functionNode, true, false));
        }
    }

    if (env._parse_only) {
        return null;
    }

    final URL          url    = source.getURL();
    final ScriptLoader loader = env._loader_per_compile ? createNewLoader() : scriptLoader;
    final CodeSource   cs     = new CodeSource(url, (CodeSigner[])null);
    final CodeInstaller installer = new ContextCodeInstaller(this, loader, cs);

    if (storedScript == null) {
        final CompilationPhases phases = Compiler.CompilationPhases.COMPILE_ALL;

        final Compiler compiler = Compiler.forInitialCompilation(
                installer,
                source,
                errMan,
                strict | functionNode.isStrict());

        final FunctionNode compiledFunction = compiler.compile(functionNode, phases);
        if (errMan.hasErrors()) {
            return null;
        }
        script = compiledFunction.getRootClass();
        compiler.persistClassInfo(cacheKey, compiledFunction);
    } else {
        Compiler.updateCompilationId(storedScript.getCompilationId());
        script = storedScript.installScript(source, installer);
    }

    cacheClass(source, script);
    return script;
}
 
Example 17
Source File: Context.java    From jdk8u60 with GNU General Public License v2.0 4 votes vote down vote up
private synchronized Class<?> compile(final Source source, final ErrorManager errMan, final boolean strict) {
    // start with no errors, no warnings.
    errMan.reset();

    Class<?> script = findCachedClass(source);
    if (script != null) {
        final DebugLogger log = getLogger(Compiler.class);
        if (log.isEnabled()) {
            log.fine(new RuntimeEvent<>(Level.INFO, source), "Code cache hit for ", source, " avoiding recompile.");
        }
        return script;
    }

    StoredScript storedScript = null;
    FunctionNode functionNode = null;
    // Don't use code store if optimistic types is enabled but lazy compilation is not.
    // This would store a full script compilation with many wrong optimistic assumptions that would
    // do more harm than good on later runs with both optimistic types and lazy compilation enabled.
    final boolean useCodeStore = codeStore != null && !env._parse_only && (!env._optimistic_types || env._lazy_compilation);
    final String cacheKey = useCodeStore ? CodeStore.getCacheKey("script", null) : null;

    if (useCodeStore) {
        storedScript = codeStore.load(source, cacheKey);
    }

    if (storedScript == null) {
        if (env._dest_dir != null) {
            source.dump(env._dest_dir);
        }

        functionNode = new Parser(env, source, errMan, strict, getLogger(Parser.class)).parse();

        if (errMan.hasErrors()) {
            return null;
        }

        if (env._print_ast || functionNode.getFlag(FunctionNode.IS_PRINT_AST)) {
            getErr().println(new ASTWriter(functionNode));
        }

        if (env._print_parse || functionNode.getFlag(FunctionNode.IS_PRINT_PARSE)) {
            getErr().println(new PrintVisitor(functionNode, true, false));
        }
    }

    if (env._parse_only) {
        return null;
    }

    final URL          url    = source.getURL();
    final ScriptLoader loader = env._loader_per_compile ? createNewLoader() : scriptLoader;
    final CodeSource   cs     = new CodeSource(url, (CodeSigner[])null);
    final CodeInstaller<ScriptEnvironment> installer = new ContextCodeInstaller(this, loader, cs);

    if (storedScript == null) {
        final CompilationPhases phases = Compiler.CompilationPhases.COMPILE_ALL;

        final Compiler compiler = new Compiler(
                this,
                env,
                installer,
                source,
                errMan,
                strict | functionNode.isStrict());

        final FunctionNode compiledFunction = compiler.compile(functionNode, phases);
        if (errMan.hasErrors()) {
            return null;
        }
        script = compiledFunction.getRootClass();
        compiler.persistClassInfo(cacheKey, compiledFunction);
    } else {
        Compiler.updateCompilationId(storedScript.getCompilationId());
        script = storedScript.installScript(source, installer);
    }

    cacheClass(source, script);
    return script;
}