Wed, 26 Jun 2013 15:40:52 +0200
8019157: Avoid calling ScriptObject.setProto() if possible
Reviewed-by: jlaskey, sundar
1 /*
2 * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
26 package jdk.nashorn.internal.runtime;
28 import static jdk.nashorn.internal.codegen.CompilerConstants.RUN_SCRIPT;
29 import static jdk.nashorn.internal.codegen.CompilerConstants.STRICT_MODE;
30 import static jdk.nashorn.internal.lookup.Lookup.MH;
31 import static jdk.nashorn.internal.runtime.ECMAErrors.typeError;
32 import static jdk.nashorn.internal.runtime.ScriptRuntime.UNDEFINED;
34 import java.io.File;
35 import java.io.IOException;
36 import java.io.PrintWriter;
37 import java.lang.invoke.MethodHandle;
38 import java.lang.invoke.MethodHandles;
39 import java.lang.reflect.Constructor;
40 import java.net.MalformedURLException;
41 import java.net.URL;
42 import java.security.AccessControlContext;
43 import java.security.AccessController;
44 import java.security.CodeSigner;
45 import java.security.CodeSource;
46 import java.security.Permissions;
47 import java.security.PrivilegedAction;
48 import java.security.ProtectionDomain;
49 import java.util.Map;
50 import jdk.internal.org.objectweb.asm.ClassReader;
51 import jdk.internal.org.objectweb.asm.util.CheckClassAdapter;
52 import jdk.nashorn.api.scripting.ScriptObjectMirror;
53 import jdk.nashorn.internal.codegen.Compiler;
54 import jdk.nashorn.internal.codegen.ObjectClassGenerator;
55 import jdk.nashorn.internal.ir.FunctionNode;
56 import jdk.nashorn.internal.ir.debug.ASTWriter;
57 import jdk.nashorn.internal.ir.debug.PrintVisitor;
58 import jdk.nashorn.internal.parser.Parser;
59 import jdk.nashorn.internal.runtime.options.Options;
61 /**
62 * This class manages the global state of execution. Context is immutable.
63 */
64 public final class Context {
66 /**
67 * ContextCodeInstaller that has the privilege of installing classes in the Context.
68 * Can only be instantiated from inside the context and is opaque to other classes
69 */
70 public static class ContextCodeInstaller implements CodeInstaller<ScriptEnvironment> {
71 private final Context context;
72 private final ScriptLoader loader;
73 private final CodeSource codeSource;
75 private ContextCodeInstaller(final Context context, final ScriptLoader loader, final CodeSource codeSource) {
76 this.context = context;
77 this.loader = loader;
78 this.codeSource = codeSource;
79 }
81 /**
82 * Return the context for this installer
83 * @return ScriptEnvironment
84 */
85 @Override
86 public ScriptEnvironment getOwner() {
87 return context.env;
88 }
90 @Override
91 public Class<?> install(final String className, final byte[] bytecode) {
92 return loader.installClass(className, bytecode, codeSource);
93 }
95 @Override
96 public void verify(final byte[] code) {
97 context.verify(code);
98 }
99 }
101 /** Is Context global debug mode enabled ? */
102 public static final boolean DEBUG = Options.getBooleanProperty("nashorn.debug");
104 private static final ThreadLocal<ScriptObject> currentGlobal = new ThreadLocal<>();
106 /**
107 * Get the current global scope
108 * @return the current global scope
109 */
110 public static ScriptObject getGlobal() {
111 // This class in a package.access protected package.
112 // Trusted code only can call this method.
113 return getGlobalTrusted();
114 }
116 /**
117 * Set the current global scope
118 * @param global the global scope
119 */
120 public static void setGlobal(final ScriptObject global) {
121 final SecurityManager sm = System.getSecurityManager();
122 if (sm != null) {
123 sm.checkPermission(new RuntimePermission("nashorn.setGlobal"));
124 }
126 if (global != null && !(global instanceof GlobalObject)) {
127 throw new IllegalArgumentException("global does not implement GlobalObject!");
128 }
130 setGlobalTrusted(global);
131 }
133 /**
134 * Get context of the current global
135 * @return current global scope's context.
136 */
137 public static Context getContext() {
138 final SecurityManager sm = System.getSecurityManager();
139 if (sm != null) {
140 sm.checkPermission(new RuntimePermission("nashorn.getContext"));
141 }
142 return getContextTrusted();
143 }
145 /**
146 * Get current context's error writer
147 *
148 * @return error writer of the current context
149 */
150 public static PrintWriter getCurrentErr() {
151 final ScriptObject global = getGlobalTrusted();
152 return (global != null)? global.getContext().getErr() : new PrintWriter(System.err);
153 }
155 /**
156 * Output text to this Context's error stream
157 * @param str text to write
158 */
159 public static void err(final String str) {
160 err(str, true);
161 }
163 /**
164 * Output text to this Context's error stream, optionally with
165 * a newline afterwards
166 *
167 * @param str text to write
168 * @param crlf write a carriage return/new line after text
169 */
170 @SuppressWarnings("resource")
171 public static void err(final String str, final boolean crlf) {
172 final PrintWriter err = Context.getCurrentErr();
173 if (err != null) {
174 if (crlf) {
175 err.println(str);
176 } else {
177 err.print(str);
178 }
179 }
180 }
182 /** Current environment. */
183 private final ScriptEnvironment env;
185 /** is this context in strict mode? Cached from env. as this is used heavily. */
186 final boolean _strict;
188 /** class loader to resolve classes from script. */
189 private final ClassLoader appLoader;
191 /** Class loader to load classes from -classpath option, if set. */
192 private final ClassLoader classPathLoader;
194 /** Class loader to load classes compiled from scripts. */
195 private final ScriptLoader scriptLoader;
197 /** Current error manager. */
198 private final ErrorManager errors;
200 private static final ClassLoader myLoader = Context.class.getClassLoader();
201 private static final StructureLoader sharedLoader;
203 static {
204 sharedLoader = AccessController.doPrivileged(new PrivilegedAction<StructureLoader>() {
205 @Override
206 public StructureLoader run() {
207 return new StructureLoader(myLoader, null);
208 }
209 });
210 }
212 /**
213 * ThrowErrorManager that throws ParserException upon error conditions.
214 */
215 public static class ThrowErrorManager extends ErrorManager {
216 @Override
217 public void error(final String message) {
218 throw new ParserException(message);
219 }
221 @Override
222 public void error(final ParserException e) {
223 throw e;
224 }
225 }
227 /**
228 * Constructor
229 *
230 * @param options options from command line or Context creator
231 * @param errors error manger
232 * @param appLoader application class loader
233 */
234 public Context(final Options options, final ErrorManager errors, final ClassLoader appLoader) {
235 this(options, errors, new PrintWriter(System.out, true), new PrintWriter(System.err, true), appLoader);
236 }
238 /**
239 * Constructor
240 *
241 * @param options options from command line or Context creator
242 * @param errors error manger
243 * @param out output writer for this Context
244 * @param err error writer for this Context
245 * @param appLoader application class loader
246 */
247 public Context(final Options options, final ErrorManager errors, final PrintWriter out, final PrintWriter err, final ClassLoader appLoader) {
248 final SecurityManager sm = System.getSecurityManager();
249 if (sm != null) {
250 sm.checkPermission(new RuntimePermission("nashorn.createContext"));
251 }
253 this.env = new ScriptEnvironment(options, out, err);
254 this._strict = env._strict;
255 this.appLoader = appLoader;
256 this.scriptLoader = (ScriptLoader)AccessController.doPrivileged(
257 new PrivilegedAction<ClassLoader>() {
258 @Override
259 public ClassLoader run() {
260 final StructureLoader structureLoader = new StructureLoader(sharedLoader, Context.this);
261 return new ScriptLoader(structureLoader, Context.this);
262 }
263 });
264 this.errors = errors;
266 // if user passed -classpath option, make a class loader with that and set it as
267 // thread context class loader so that script can access classes from that path.
268 final String classPath = options.getString("classpath");
269 if (! env._compile_only && classPath != null && !classPath.isEmpty()) {
270 // make sure that caller can create a class loader.
271 if (sm != null) {
272 sm.checkPermission(new RuntimePermission("createClassLoader"));
273 }
274 this.classPathLoader = NashornLoader.createClassLoader(classPath);
275 } else {
276 this.classPathLoader = null;
277 }
279 // print version info if asked.
280 if (env._version) {
281 getErr().println("nashorn " + Version.version());
282 }
284 if (env._fullversion) {
285 getErr().println("nashorn full version " + Version.fullVersion());
286 }
287 }
289 /**
290 * Get the error manager for this context
291 * @return error manger
292 */
293 public ErrorManager getErrorManager() {
294 return errors;
295 }
297 /**
298 * Get the script environment for this context
299 * @return script environment
300 */
301 public ScriptEnvironment getEnv() {
302 return env;
303 }
305 /**
306 * Get the output stream for this context
307 * @return output print writer
308 */
309 public PrintWriter getOut() {
310 return env.getOut();
311 }
313 /**
314 * Get the error stream for this context
315 * @return error print writer
316 */
317 public PrintWriter getErr() {
318 return env.getErr();
319 }
321 /**
322 * Get the PropertyMap of the current global scope
323 * @return the property map of the current global scope
324 */
325 public static PropertyMap getGlobalMap() {
326 return Context.getGlobalTrusted().getMap();
327 }
329 /**
330 * Compile a top level script.
331 *
332 * @param source the source
333 * @param scope the scope
334 *
335 * @return top level function for script
336 */
337 public ScriptFunction compileScript(final Source source, final ScriptObject scope) {
338 return compileScript(source, scope, this.errors);
339 }
341 /**
342 * Entry point for {@code eval}
343 *
344 * @param initialScope The scope of this eval call
345 * @param string Evaluated code as a String
346 * @param callThis "this" to be passed to the evaluated code
347 * @param location location of the eval call
348 * @param strict is this {@code eval} call from a strict mode code?
349 *
350 * @return the return value of the {@code eval}
351 */
352 public Object eval(final ScriptObject initialScope, final String string, final Object callThis, final Object location, final boolean strict) {
353 final String file = (location == UNDEFINED || location == null) ? "<eval>" : location.toString();
354 final Source source = new Source(file, string);
355 final boolean directEval = location != UNDEFINED; // is this direct 'eval' call or indirectly invoked eval?
356 final ScriptObject global = Context.getGlobalTrusted();
358 ScriptObject scope = initialScope;
360 // ECMA section 10.1.1 point 2 says eval code is strict if it begins
361 // with "use strict" directive or eval direct call itself is made
362 // from from strict mode code. We are passed with caller's strict mode.
363 boolean strictFlag = directEval && strict;
365 Class<?> clazz = null;
366 try {
367 clazz = compile(source, new ThrowErrorManager(), strictFlag);
368 } catch (final ParserException e) {
369 e.throwAsEcmaException(global);
370 return null;
371 }
373 if (!strictFlag) {
374 // We need to get strict mode flag from compiled class. This is
375 // because eval code may start with "use strict" directive.
376 try {
377 strictFlag = clazz.getField(STRICT_MODE.symbolName()).getBoolean(null);
378 } catch (final NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException e) {
379 //ignored
380 strictFlag = false;
381 }
382 }
384 // In strict mode, eval does not instantiate variables and functions
385 // in the caller's environment. A new environment is created!
386 if (strictFlag) {
387 // Create a new scope object
388 final ScriptObject strictEvalScope = ((GlobalObject)global).newObject();
390 // bless it as a "scope"
391 strictEvalScope.setIsScope();
393 // set given scope to be it's proto so that eval can still
394 // access caller environment vars in the new environment.
395 strictEvalScope.setProto(scope);
396 scope = strictEvalScope;
397 }
399 ScriptFunction func = getRunScriptFunction(clazz, scope);
400 Object evalThis;
401 if (directEval) {
402 evalThis = (callThis instanceof ScriptObject || strictFlag) ? callThis : global;
403 } else {
404 evalThis = global;
405 }
407 return ScriptRuntime.apply(func, evalThis);
408 }
410 private static Source loadInternal(final String srcStr, final String prefix, final String resourcePath) {
411 if (srcStr.startsWith(prefix)) {
412 final String resource = resourcePath + srcStr.substring(prefix.length());
413 // NOTE: even sandbox scripts should be able to load scripts in nashorn: scheme
414 // These scripts are always available and are loaded from nashorn.jar's resources.
415 return AccessController.doPrivileged(
416 new PrivilegedAction<Source>() {
417 @Override
418 public Source run() {
419 try {
420 final URL resURL = Context.class.getResource(resource);
421 return (resURL != null)? new Source(srcStr, resURL) : null;
422 } catch (final IOException exp) {
423 return null;
424 }
425 }
426 });
427 }
429 return null;
430 }
432 /**
433 * Implementation of {@code load} Nashorn extension. Load a script file from a source
434 * expression
435 *
436 * @param scope the scope
437 * @param from source expression for script
438 *
439 * @return return value for load call (undefined)
440 *
441 * @throws IOException if source cannot be found or loaded
442 */
443 public Object load(final ScriptObject scope, final Object from) throws IOException {
444 final Object src = (from instanceof ConsString)? from.toString() : from;
445 Source source = null;
447 // load accepts a String (which could be a URL or a file name), a File, a URL
448 // or a ScriptObject that has "name" and "source" (string valued) properties.
449 if (src instanceof String) {
450 final String srcStr = (String)src;
451 final File file = new File(srcStr);
452 if (srcStr.indexOf(':') != -1) {
453 if ((source = loadInternal(srcStr, "nashorn:", "resources/")) == null &&
454 (source = loadInternal(srcStr, "fx:", "resources/fx/")) == null) {
455 URL url;
456 try {
457 //check for malformed url. if malformed, it may still be a valid file
458 url = new URL(srcStr);
459 } catch (final MalformedURLException e) {
460 url = file.toURI().toURL();
461 }
462 source = new Source(url.toString(), url);
463 }
464 } else if (file.isFile()) {
465 source = new Source(srcStr, file);
466 }
467 } else if (src instanceof File && ((File)src).isFile()) {
468 final File file = (File)src;
469 source = new Source(file.getName(), file);
470 } else if (src instanceof URL) {
471 final URL url = (URL)src;
472 source = new Source(url.toString(), url);
473 } else if (src instanceof ScriptObject) {
474 final ScriptObject sobj = (ScriptObject)src;
475 if (sobj.has("script") && sobj.has("name")) {
476 final String script = JSType.toString(sobj.get("script"));
477 final String name = JSType.toString(sobj.get("name"));
478 source = new Source(name, script);
479 }
480 } else if (src instanceof Map) {
481 final Map map = (Map)src;
482 if (map.containsKey("script") && map.containsKey("name")) {
483 final String script = JSType.toString(map.get("script"));
484 final String name = JSType.toString(map.get("name"));
485 source = new Source(name, script);
486 }
487 }
489 if (source != null) {
490 return evaluateSource(source, scope, scope);
491 }
493 throw typeError("cant.load.script", ScriptRuntime.safeToString(from));
494 }
496 /**
497 * Implementation of {@code loadWithNewGlobal} Nashorn extension. Load a script file from a source
498 * expression, after creating a new global scope.
499 *
500 * @param from source expression for script
501 * @param args (optional) arguments to be passed to the loaded script
502 *
503 * @return return value for load call (undefined)
504 *
505 * @throws IOException if source cannot be found or loaded
506 */
507 public Object loadWithNewGlobal(final Object from, final Object...args) throws IOException {
508 final ScriptObject oldGlobal = getGlobalTrusted();
509 final ScriptObject newGlobal = AccessController.doPrivileged(new PrivilegedAction<ScriptObject>() {
510 @Override
511 public ScriptObject run() {
512 try {
513 return createGlobal();
514 } catch (final RuntimeException e) {
515 if (Context.DEBUG) {
516 e.printStackTrace();
517 }
518 throw e;
519 }
520 }
521 });
522 setGlobalTrusted(newGlobal);
524 final Object[] wrapped = args == null? ScriptRuntime.EMPTY_ARRAY : ScriptObjectMirror.wrapArray(args, newGlobal);
525 newGlobal.put("arguments", ((GlobalObject)newGlobal).wrapAsObject(wrapped));
527 try {
528 return ScriptObjectMirror.wrap(load(newGlobal, from), newGlobal);
529 } finally {
530 setGlobalTrusted(oldGlobal);
531 }
532 }
534 /**
535 * Load or get a structure class. Structure class names are based on the number of parameter fields
536 * and {@link AccessorProperty} fields in them. Structure classes are used to represent ScriptObjects
537 *
538 * @see ObjectClassGenerator
539 * @see AccessorProperty
540 * @see ScriptObject
541 *
542 * @param fullName full name of class, e.g. jdk.nashorn.internal.objects.JO2P1 contains 2 fields and 1 parameter.
543 *
544 * @return the {@code Class<?>} for this structure
545 *
546 * @throws ClassNotFoundException if structure class cannot be resolved
547 */
548 public static Class<?> forStructureClass(final String fullName) throws ClassNotFoundException {
549 return Class.forName(fullName, true, sharedLoader);
550 }
552 /**
553 * Lookup a Java class. This is used for JSR-223 stuff linking in from
554 * {@code jdk.nashorn.internal.objects.NativeJava} and {@code jdk.nashorn.internal.runtime.NativeJavaPackage}
555 *
556 * @param fullName full name of class to load
557 *
558 * @return the {@code Class<?>} for the name
559 *
560 * @throws ClassNotFoundException if class cannot be resolved
561 */
562 public Class<?> findClass(final String fullName) throws ClassNotFoundException {
563 // check package access as soon as possible!
564 final int index = fullName.lastIndexOf('.');
565 if (index != -1) {
566 final SecurityManager sm = System.getSecurityManager();
567 if (sm != null) {
568 AccessController.doPrivileged(new PrivilegedAction<Void>() {
569 @Override
570 public Void run() {
571 sm.checkPackageAccess(fullName.substring(0, index));
572 return null;
573 }
574 }, createNoPermissionsContext());
575 }
576 }
578 // try the script -classpath loader, if that is set
579 if (classPathLoader != null) {
580 try {
581 return Class.forName(fullName, true, classPathLoader);
582 } catch (final ClassNotFoundException ignored) {
583 // ignore, continue search
584 }
585 }
587 // Try finding using the "app" loader.
588 return Class.forName(fullName, true, appLoader);
589 }
591 /**
592 * Hook to print stack trace for a {@link Throwable} that occurred during
593 * execution
594 *
595 * @param t throwable for which to dump stack
596 */
597 public static void printStackTrace(final Throwable t) {
598 if (Context.DEBUG) {
599 t.printStackTrace(Context.getCurrentErr());
600 }
601 }
603 /**
604 * Verify generated bytecode before emission. This is called back from the
605 * {@link ObjectClassGenerator} or the {@link Compiler}. If the "--verify-code" parameter
606 * hasn't been given, this is a nop
607 *
608 * Note that verification may load classes -- we don't want to do that unless
609 * user specified verify option. We check it here even though caller
610 * may have already checked that flag
611 *
612 * @param bytecode bytecode to verify
613 */
614 public void verify(final byte[] bytecode) {
615 if (env._verify_code) {
616 // No verification when security manager is around as verifier
617 // may load further classes - which should be avoided.
618 if (System.getSecurityManager() == null) {
619 CheckClassAdapter.verify(new ClassReader(bytecode), scriptLoader, false, new PrintWriter(System.err, true));
620 }
621 }
622 }
624 /**
625 * Create and initialize a new global scope object.
626 *
627 * @return the initialized global scope object.
628 */
629 public ScriptObject createGlobal() {
630 return initGlobal(newGlobal());
631 }
633 /**
634 * Create a new uninitialized global scope object
635 * @return the global script object
636 */
637 public ScriptObject newGlobal() {
638 final SecurityManager sm = System.getSecurityManager();
639 if (sm != null) {
640 sm.checkPermission(new RuntimePermission("nashorn.newGlobal"));
641 }
643 return newGlobalTrusted();
644 }
646 /**
647 * Initialize given global scope object.
648 *
649 * @param global the global
650 * @return the initialized global scope object.
651 */
652 public ScriptObject initGlobal(final ScriptObject global) {
653 if (! (global instanceof GlobalObject)) {
654 throw new IllegalArgumentException("not a global object!");
655 }
657 // Need only minimal global object, if we are just compiling.
658 if (!env._compile_only) {
659 final ScriptObject oldGlobal = Context.getGlobalTrusted();
660 try {
661 Context.setGlobalTrusted(global);
662 // initialize global scope with builtin global objects
663 ((GlobalObject)global).initBuiltinObjects();
664 } finally {
665 Context.setGlobalTrusted(oldGlobal);
666 }
667 }
669 return global;
670 }
672 /**
673 * Trusted variants - package-private
674 */
676 /**
677 * Return the current global scope
678 * @return current global scope
679 */
680 static ScriptObject getGlobalTrusted() {
681 return currentGlobal.get();
682 }
684 /**
685 * Set the current global scope
686 */
687 static void setGlobalTrusted(ScriptObject global) {
688 currentGlobal.set(global);
689 }
691 /**
692 * Return the current global's context
693 * @return current global's context
694 */
695 static Context getContextTrusted() {
696 return Context.getGlobalTrusted().getContext();
697 }
699 /**
700 * Try to infer Context instance from the Class. If we cannot,
701 * then get it from the thread local variable.
702 *
703 * @param clazz the class
704 * @return context
705 */
706 static Context fromClass(final Class<?> clazz) {
707 final ClassLoader loader = clazz.getClassLoader();
709 Context context = null;
710 if (loader instanceof NashornLoader) {
711 context = ((NashornLoader)loader).getContext();
712 }
714 return (context != null) ? context : Context.getContextTrusted();
715 }
717 private static AccessControlContext createNoPermissionsContext() {
718 return new AccessControlContext(new ProtectionDomain[] { new ProtectionDomain(null, new Permissions()) });
719 }
721 private Object evaluateSource(final Source source, final ScriptObject scope, final ScriptObject thiz) {
722 ScriptFunction script = null;
724 try {
725 script = compileScript(source, scope, new Context.ThrowErrorManager());
726 } catch (final ParserException e) {
727 e.throwAsEcmaException();
728 }
730 return ScriptRuntime.apply(script, thiz);
731 }
733 private static ScriptFunction getRunScriptFunction(final Class<?> script, final ScriptObject scope) {
734 if (script == null) {
735 return null;
736 }
738 // Get run method - the entry point to the script
739 final MethodHandle runMethodHandle =
740 MH.findStatic(
741 MethodHandles.lookup(),
742 script,
743 RUN_SCRIPT.symbolName(),
744 MH.type(
745 Object.class,
746 ScriptFunction.class,
747 Object.class));
749 boolean strict;
751 try {
752 strict = script.getField(STRICT_MODE.symbolName()).getBoolean(null);
753 } catch (final NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException e) {
754 strict = false;
755 }
757 // Package as a JavaScript function and pass function back to shell.
758 return ((GlobalObject)Context.getGlobalTrusted()).newScriptFunction(RUN_SCRIPT.symbolName(), runMethodHandle, scope, strict);
759 }
761 private ScriptFunction compileScript(final Source source, final ScriptObject scope, final ErrorManager errMan) {
762 return getRunScriptFunction(compile(source, errMan, this._strict), scope);
763 }
765 private synchronized Class<?> compile(final Source source, final ErrorManager errMan, final boolean strict) {
766 // start with no errors, no warnings.
767 errMan.reset();
769 GlobalObject global = null;
770 Class<?> script;
772 if (env._class_cache_size > 0) {
773 global = (GlobalObject)Context.getGlobalTrusted();
774 script = global.findCachedClass(source);
775 if (script != null) {
776 Compiler.LOG.fine("Code cache hit for ", source, " avoiding recompile.");
777 return script;
778 }
779 }
781 final FunctionNode functionNode = new Parser(env, source, errMan, strict).parse();
782 if (errors.hasErrors()) {
783 return null;
784 }
786 if (env._print_ast) {
787 getErr().println(new ASTWriter(functionNode));
788 }
790 if (env._print_parse) {
791 getErr().println(new PrintVisitor(functionNode));
792 }
794 if (env._parse_only) {
795 return null;
796 }
798 final URL url = source.getURL();
799 final ScriptLoader loader = env._loader_per_compile ? createNewLoader() : scriptLoader;
800 final CodeSource cs = url == null ? null : new CodeSource(url, (CodeSigner[])null);
801 final CodeInstaller<ScriptEnvironment> installer = new ContextCodeInstaller(this, loader, cs);
803 final Compiler compiler = new Compiler(installer, strict);
805 final FunctionNode newFunctionNode = compiler.compile(functionNode);
806 script = compiler.install(newFunctionNode);
808 if (global != null) {
809 global.cacheClass(source, script);
810 }
812 return script;
813 }
815 private ScriptLoader createNewLoader() {
816 return AccessController.doPrivileged(
817 new PrivilegedAction<ScriptLoader>() {
818 @Override
819 public ScriptLoader run() {
820 // Generated code won't refer to any class generated by context
821 // script loader and so parent loader can be the structure
822 // loader -- which is parent of the context script loader.
823 return new ScriptLoader((StructureLoader)scriptLoader.getParent(), Context.this);
824 }
825 });
826 }
828 private ScriptObject newGlobalTrusted() {
829 try {
830 final Class<?> clazz = Class.forName("jdk.nashorn.internal.objects.Global", true, scriptLoader);
831 final Constructor<?> cstr = clazz.getConstructor(Context.class);
832 return (ScriptObject) cstr.newInstance(this);
833 } catch (final Exception e) {
834 printStackTrace(e);
835 if (e instanceof RuntimeException) {
836 throw (RuntimeException)e;
837 }
838 throw new RuntimeException(e);
839 }
840 }
841 }