492
493 // if user passed -classpath option, make a class loader with that and set it as
494 // thread context class loader so that script can access classes from that path.
495 final String classPath = options.getString("classpath");
496 if (!env._compile_only && classPath != null && !classPath.isEmpty()) {
497 // make sure that caller can create a class loader.
498 if (sm != null) {
499 sm.checkPermission(new RuntimePermission("createClassLoader"));
500 }
501 this.classPathLoader = NashornLoader.createClassLoader(classPath);
502 } else {
503 this.classPathLoader = null;
504 }
505
506 final int cacheSize = env._class_cache_size;
507 if (cacheSize > 0) {
508 classCache = new ClassCache(this, cacheSize);
509 }
510
511 if (env._persistent_cache) {
512 try {
513 codeStore = newCodeStore(this);
514 } catch (final IOException e) {
515 throw new RuntimeException("Error initializing code cache", e);
516 }
517 }
518
519 // print version info if asked.
520 if (env._version) {
521 getErr().println("nashorn " + Version.version());
522 }
523
524 if (env._fullversion) {
525 getErr().println("nashorn full version " + Version.fullVersion());
526 }
527
528 initLoggers();
529 }
530
531
532 /**
533 * Get the class filter for this context
534 * @return class filter
535 */
536 public ClassFilter getClassFilter() {
1183 return getProgramFunction(compile(source, errMan, this._strict), scope);
1184 }
1185
1186 private synchronized Class<?> compile(final Source source, final ErrorManager errMan, final boolean strict) {
1187 // start with no errors, no warnings.
1188 errMan.reset();
1189
1190 Class<?> script = findCachedClass(source);
1191 if (script != null) {
1192 final DebugLogger log = getLogger(Compiler.class);
1193 if (log.isEnabled()) {
1194 log.fine(new RuntimeEvent<>(Level.INFO, source), "Code cache hit for ", source, " avoiding recompile.");
1195 }
1196 return script;
1197 }
1198
1199 StoredScript storedScript = null;
1200 FunctionNode functionNode = null;
1201 // We only use the code store here if optimistic types are disabled. With optimistic types, initial compilation
1202 // just creates a thin wrapper, and actual code is stored per function in RecompilableScriptFunctionData.
1203 final boolean useCodeStore = env._persistent_cache && !env._parse_only && !env._optimistic_types;
1204 final String cacheKey = useCodeStore ? CodeStore.getCacheKey(0, null) : null;
1205
1206 if (useCodeStore) {
1207 storedScript = codeStore.load(source, cacheKey);
1208 }
1209
1210 if (storedScript == null) {
1211 functionNode = new Parser(env, source, errMan, strict, getLogger(Parser.class)).parse();
1212
1213 if (errMan.hasErrors()) {
1214 return null;
1215 }
1216
1217 if (env._print_ast || functionNode.getFlag(FunctionNode.IS_PRINT_AST)) {
1218 getErr().println(new ASTWriter(functionNode));
1219 }
1220
1221 if (env._print_parse || functionNode.getFlag(FunctionNode.IS_PRINT_PARSE)) {
1222 getErr().println(new PrintVisitor(functionNode, true, false));
1223 }
|
492
493 // if user passed -classpath option, make a class loader with that and set it as
494 // thread context class loader so that script can access classes from that path.
495 final String classPath = options.getString("classpath");
496 if (!env._compile_only && classPath != null && !classPath.isEmpty()) {
497 // make sure that caller can create a class loader.
498 if (sm != null) {
499 sm.checkPermission(new RuntimePermission("createClassLoader"));
500 }
501 this.classPathLoader = NashornLoader.createClassLoader(classPath);
502 } else {
503 this.classPathLoader = null;
504 }
505
506 final int cacheSize = env._class_cache_size;
507 if (cacheSize > 0) {
508 classCache = new ClassCache(this, cacheSize);
509 }
510
511 if (env._persistent_cache) {
512 codeStore = newCodeStore(this);
513 }
514
515 // print version info if asked.
516 if (env._version) {
517 getErr().println("nashorn " + Version.version());
518 }
519
520 if (env._fullversion) {
521 getErr().println("nashorn full version " + Version.fullVersion());
522 }
523
524 initLoggers();
525 }
526
527
528 /**
529 * Get the class filter for this context
530 * @return class filter
531 */
532 public ClassFilter getClassFilter() {
1179 return getProgramFunction(compile(source, errMan, this._strict), scope);
1180 }
1181
1182 private synchronized Class<?> compile(final Source source, final ErrorManager errMan, final boolean strict) {
1183 // start with no errors, no warnings.
1184 errMan.reset();
1185
1186 Class<?> script = findCachedClass(source);
1187 if (script != null) {
1188 final DebugLogger log = getLogger(Compiler.class);
1189 if (log.isEnabled()) {
1190 log.fine(new RuntimeEvent<>(Level.INFO, source), "Code cache hit for ", source, " avoiding recompile.");
1191 }
1192 return script;
1193 }
1194
1195 StoredScript storedScript = null;
1196 FunctionNode functionNode = null;
1197 // We only use the code store here if optimistic types are disabled. With optimistic types, initial compilation
1198 // just creates a thin wrapper, and actual code is stored per function in RecompilableScriptFunctionData.
1199 final boolean useCodeStore = codeStore != null && !env._parse_only && !env._optimistic_types;
1200 final String cacheKey = useCodeStore ? CodeStore.getCacheKey(0, null) : null;
1201
1202 if (useCodeStore) {
1203 storedScript = codeStore.load(source, cacheKey);
1204 }
1205
1206 if (storedScript == null) {
1207 functionNode = new Parser(env, source, errMan, strict, getLogger(Parser.class)).parse();
1208
1209 if (errMan.hasErrors()) {
1210 return null;
1211 }
1212
1213 if (env._print_ast || functionNode.getFlag(FunctionNode.IS_PRINT_AST)) {
1214 getErr().println(new ASTWriter(functionNode));
1215 }
1216
1217 if (env._print_parse || functionNode.getFlag(FunctionNode.IS_PRINT_PARSE)) {
1218 getErr().println(new PrintVisitor(functionNode, true, false));
1219 }
|