Wed, 12 Mar 2014 11:26:00 +0100
8021350: Share script classes between threads/globals within context
Reviewed-by: lagergren, 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.ref.ReferenceQueue;
40 import java.lang.ref.SoftReference;
41 import java.lang.reflect.Modifier;
42 import java.util.concurrent.atomic.AtomicLong;
43 import java.net.MalformedURLException;
44 import java.net.URL;
45 import java.security.AccessControlContext;
46 import java.security.AccessController;
47 import java.security.CodeSigner;
48 import java.security.CodeSource;
49 import java.security.Permissions;
50 import java.security.PrivilegedAction;
51 import java.security.ProtectionDomain;
52 import java.util.LinkedHashMap;
53 import java.util.Map;
55 import jdk.internal.org.objectweb.asm.ClassReader;
56 import jdk.internal.org.objectweb.asm.util.CheckClassAdapter;
57 import jdk.nashorn.api.scripting.ScriptObjectMirror;
58 import jdk.nashorn.internal.codegen.Compiler;
59 import jdk.nashorn.internal.codegen.ObjectClassGenerator;
60 import jdk.nashorn.internal.ir.FunctionNode;
61 import jdk.nashorn.internal.ir.debug.ASTWriter;
62 import jdk.nashorn.internal.ir.debug.PrintVisitor;
63 import jdk.nashorn.internal.objects.Global;
64 import jdk.nashorn.internal.parser.Parser;
65 import jdk.nashorn.internal.runtime.options.Options;
67 /**
68 * This class manages the global state of execution. Context is immutable.
69 */
70 public final class Context {
71 // nashorn specific security runtime access permission names
72 /**
73 * Permission needed to pass arbitrary nashorn command line options when creating Context.
74 */
75 public static final String NASHORN_SET_CONFIG = "nashorn.setConfig";
77 /**
78 * Permission needed to create Nashorn Context instance.
79 */
80 public static final String NASHORN_CREATE_CONTEXT = "nashorn.createContext";
82 /**
83 * Permission needed to create Nashorn Global instance.
84 */
85 public static final String NASHORN_CREATE_GLOBAL = "nashorn.createGlobal";
87 /**
88 * Permission to get current Nashorn Context from thread local storage.
89 */
90 public static final String NASHORN_GET_CONTEXT = "nashorn.getContext";
92 /**
93 * Permission to use Java reflection/jsr292 from script code.
94 */
95 public static final String NASHORN_JAVA_REFLECTION = "nashorn.JavaReflection";
97 /**
98 * Permission to enable nashorn debug mode.
99 */
100 public static final String NASHORN_DEBUG_MODE = "nashorn.debugMode";
102 // nashorn load psuedo URL prefixes
103 private static final String LOAD_CLASSPATH = "classpath:";
104 private static final String LOAD_FX = "fx:";
105 private static final String LOAD_NASHORN = "nashorn:";
107 /* Force DebuggerSupport to be loaded. */
108 static {
109 DebuggerSupport.FORCELOAD = true;
110 }
112 /**
113 * ContextCodeInstaller that has the privilege of installing classes in the Context.
114 * Can only be instantiated from inside the context and is opaque to other classes
115 */
116 public static class ContextCodeInstaller implements CodeInstaller<ScriptEnvironment> {
117 private final Context context;
118 private final ScriptLoader loader;
119 private final CodeSource codeSource;
121 private ContextCodeInstaller(final Context context, final ScriptLoader loader, final CodeSource codeSource) {
122 this.context = context;
123 this.loader = loader;
124 this.codeSource = codeSource;
125 }
127 /**
128 * Return the context for this installer
129 * @return ScriptEnvironment
130 */
131 @Override
132 public ScriptEnvironment getOwner() {
133 return context.env;
134 }
136 @Override
137 public Class<?> install(final String className, final byte[] bytecode) {
138 return loader.installClass(className, bytecode, codeSource);
139 }
141 @Override
142 public void verify(final byte[] code) {
143 context.verify(code);
144 }
146 @Override
147 public long getUniqueScriptId() {
148 return context.getUniqueScriptId();
149 }
151 @Override
152 public long getUniqueEvalId() {
153 return context.getUniqueEvalId();
154 }
155 }
157 /** Is Context global debug mode enabled ? */
158 public static final boolean DEBUG = Options.getBooleanProperty("nashorn.debug");
160 private static final ThreadLocal<ScriptObject> currentGlobal = new ThreadLocal<>();
162 // class cache
163 private ClassCache classCache;
165 /**
166 * Get the current global scope
167 * @return the current global scope
168 */
169 public static ScriptObject getGlobal() {
170 // This class in a package.access protected package.
171 // Trusted code only can call this method.
172 return getGlobalTrusted();
173 }
175 /**
176 * Set the current global scope
177 * @param global the global scope
178 */
179 public static void setGlobal(final ScriptObject global) {
180 if (global != null && !(global instanceof Global)) {
181 throw new IllegalArgumentException("global is not an instance of Global!");
182 }
184 setGlobalTrusted(global);
185 }
187 /**
188 * Get context of the current global
189 * @return current global scope's context.
190 */
191 public static Context getContext() {
192 final SecurityManager sm = System.getSecurityManager();
193 if (sm != null) {
194 sm.checkPermission(new RuntimePermission(NASHORN_GET_CONTEXT));
195 }
196 return getContextTrusted();
197 }
199 /**
200 * Get current context's error writer
201 *
202 * @return error writer of the current context
203 */
204 public static PrintWriter getCurrentErr() {
205 final ScriptObject global = getGlobalTrusted();
206 return (global != null)? global.getContext().getErr() : new PrintWriter(System.err);
207 }
209 /**
210 * Output text to this Context's error stream
211 * @param str text to write
212 */
213 public static void err(final String str) {
214 err(str, true);
215 }
217 /**
218 * Output text to this Context's error stream, optionally with
219 * a newline afterwards
220 *
221 * @param str text to write
222 * @param crlf write a carriage return/new line after text
223 */
224 @SuppressWarnings("resource")
225 public static void err(final String str, final boolean crlf) {
226 final PrintWriter err = Context.getCurrentErr();
227 if (err != null) {
228 if (crlf) {
229 err.println(str);
230 } else {
231 err.print(str);
232 }
233 }
234 }
236 /** Current environment. */
237 private final ScriptEnvironment env;
239 /** is this context in strict mode? Cached from env. as this is used heavily. */
240 final boolean _strict;
242 /** class loader to resolve classes from script. */
243 private final ClassLoader appLoader;
245 /** Class loader to load classes from -classpath option, if set. */
246 private final ClassLoader classPathLoader;
248 /** Class loader to load classes compiled from scripts. */
249 private final ScriptLoader scriptLoader;
251 /** Current error manager. */
252 private final ErrorManager errors;
254 /** Unique id for script. Used only when --loader-per-compile=false */
255 private final AtomicLong uniqueScriptId;
257 /** Unique id for 'eval' */
258 private final AtomicLong uniqueEvalId;
260 private static final ClassLoader myLoader = Context.class.getClassLoader();
261 private static final StructureLoader sharedLoader;
263 /*package-private*/ @SuppressWarnings("static-method")
264 ClassLoader getSharedLoader() {
265 return sharedLoader;
266 }
268 private static AccessControlContext createNoPermAccCtxt() {
269 return new AccessControlContext(new ProtectionDomain[] { new ProtectionDomain(null, new Permissions()) });
270 }
272 private static AccessControlContext createPermAccCtxt(final String permName) {
273 final Permissions perms = new Permissions();
274 perms.add(new RuntimePermission(permName));
275 return new AccessControlContext(new ProtectionDomain[] { new ProtectionDomain(null, perms) });
276 }
278 private static final AccessControlContext NO_PERMISSIONS_ACC_CTXT = createNoPermAccCtxt();
279 private static final AccessControlContext CREATE_LOADER_ACC_CTXT = createPermAccCtxt("createClassLoader");
280 private static final AccessControlContext CREATE_GLOBAL_ACC_CTXT = createPermAccCtxt(NASHORN_CREATE_GLOBAL);
282 static {
283 sharedLoader = AccessController.doPrivileged(new PrivilegedAction<StructureLoader>() {
284 @Override
285 public StructureLoader run() {
286 return new StructureLoader(myLoader);
287 }
288 }, CREATE_LOADER_ACC_CTXT);
289 }
291 /**
292 * ThrowErrorManager that throws ParserException upon error conditions.
293 */
294 public static class ThrowErrorManager extends ErrorManager {
295 @Override
296 public void error(final String message) {
297 throw new ParserException(message);
298 }
300 @Override
301 public void error(final ParserException e) {
302 throw e;
303 }
304 }
306 /**
307 * Constructor
308 *
309 * @param options options from command line or Context creator
310 * @param errors error manger
311 * @param appLoader application class loader
312 */
313 public Context(final Options options, final ErrorManager errors, final ClassLoader appLoader) {
314 this(options, errors, new PrintWriter(System.out, true), new PrintWriter(System.err, true), appLoader);
315 }
317 /**
318 * Constructor
319 *
320 * @param options options from command line or Context creator
321 * @param errors error manger
322 * @param out output writer for this Context
323 * @param err error writer for this Context
324 * @param appLoader application class loader
325 */
326 public Context(final Options options, final ErrorManager errors, final PrintWriter out, final PrintWriter err, final ClassLoader appLoader) {
327 final SecurityManager sm = System.getSecurityManager();
328 if (sm != null) {
329 sm.checkPermission(new RuntimePermission(NASHORN_CREATE_CONTEXT));
330 }
332 this.env = new ScriptEnvironment(options, out, err);
333 this._strict = env._strict;
334 this.appLoader = appLoader;
335 if (env._loader_per_compile) {
336 this.scriptLoader = null;
337 this.uniqueScriptId = null;
338 } else {
339 this.scriptLoader = createNewLoader();
340 this.uniqueScriptId = new AtomicLong();
341 }
342 this.errors = errors;
343 this.uniqueEvalId = new AtomicLong();
345 // if user passed -classpath option, make a class loader with that and set it as
346 // thread context class loader so that script can access classes from that path.
347 final String classPath = options.getString("classpath");
348 if (! env._compile_only && classPath != null && !classPath.isEmpty()) {
349 // make sure that caller can create a class loader.
350 if (sm != null) {
351 sm.checkPermission(new RuntimePermission("createClassLoader"));
352 }
353 this.classPathLoader = NashornLoader.createClassLoader(classPath);
354 } else {
355 this.classPathLoader = null;
356 }
358 final int cacheSize = env._class_cache_size;
359 if (cacheSize > 0) {
360 classCache = new ClassCache(cacheSize);
361 }
363 // print version info if asked.
364 if (env._version) {
365 getErr().println("nashorn " + Version.version());
366 }
368 if (env._fullversion) {
369 getErr().println("nashorn full version " + Version.fullVersion());
370 }
371 }
373 /**
374 * Get the error manager for this context
375 * @return error manger
376 */
377 public ErrorManager getErrorManager() {
378 return errors;
379 }
381 /**
382 * Get the script environment for this context
383 * @return script environment
384 */
385 public ScriptEnvironment getEnv() {
386 return env;
387 }
389 /**
390 * Get the output stream for this context
391 * @return output print writer
392 */
393 public PrintWriter getOut() {
394 return env.getOut();
395 }
397 /**
398 * Get the error stream for this context
399 * @return error print writer
400 */
401 public PrintWriter getErr() {
402 return env.getErr();
403 }
405 /**
406 * Get the PropertyMap of the current global scope
407 * @return the property map of the current global scope
408 */
409 public static PropertyMap getGlobalMap() {
410 return Context.getGlobalTrusted().getMap();
411 }
413 /**
414 * Compile a top level script.
415 *
416 * @param source the source
417 * @param scope the scope
418 *
419 * @return top level function for script
420 */
421 public ScriptFunction compileScript(final Source source, final ScriptObject scope) {
422 return compileScript(source, scope, this.errors);
423 }
425 /**
426 * Entry point for {@code eval}
427 *
428 * @param initialScope The scope of this eval call
429 * @param string Evaluated code as a String
430 * @param callThis "this" to be passed to the evaluated code
431 * @param location location of the eval call
432 * @param strict is this {@code eval} call from a strict mode code?
433 *
434 * @return the return value of the {@code eval}
435 */
436 public Object eval(final ScriptObject initialScope, final String string, final Object callThis, final Object location, final boolean strict) {
437 final String file = (location == UNDEFINED || location == null) ? "<eval>" : location.toString();
438 final Source source = new Source(file, string);
439 final boolean directEval = location != UNDEFINED; // is this direct 'eval' call or indirectly invoked eval?
440 final ScriptObject global = Context.getGlobalTrusted();
442 ScriptObject scope = initialScope;
444 // ECMA section 10.1.1 point 2 says eval code is strict if it begins
445 // with "use strict" directive or eval direct call itself is made
446 // from from strict mode code. We are passed with caller's strict mode.
447 boolean strictFlag = directEval && strict;
449 Class<?> clazz = null;
450 try {
451 clazz = compile(source, new ThrowErrorManager(), strictFlag);
452 } catch (final ParserException e) {
453 e.throwAsEcmaException(global);
454 return null;
455 }
457 if (!strictFlag) {
458 // We need to get strict mode flag from compiled class. This is
459 // because eval code may start with "use strict" directive.
460 try {
461 strictFlag = clazz.getField(STRICT_MODE.symbolName()).getBoolean(null);
462 } catch (final NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException e) {
463 //ignored
464 strictFlag = false;
465 }
466 }
468 // In strict mode, eval does not instantiate variables and functions
469 // in the caller's environment. A new environment is created!
470 if (strictFlag) {
471 // Create a new scope object
472 final ScriptObject strictEvalScope = ((GlobalObject)global).newObject();
474 // bless it as a "scope"
475 strictEvalScope.setIsScope();
477 // set given scope to be it's proto so that eval can still
478 // access caller environment vars in the new environment.
479 strictEvalScope.setProto(scope);
480 scope = strictEvalScope;
481 }
483 ScriptFunction func = getRunScriptFunction(clazz, scope);
484 Object evalThis;
485 if (directEval) {
486 evalThis = (callThis instanceof ScriptObject || strictFlag) ? callThis : global;
487 } else {
488 evalThis = global;
489 }
491 return ScriptRuntime.apply(func, evalThis);
492 }
494 private static Source loadInternal(final String srcStr, final String prefix, final String resourcePath) {
495 if (srcStr.startsWith(prefix)) {
496 final String resource = resourcePath + srcStr.substring(prefix.length());
497 // NOTE: even sandbox scripts should be able to load scripts in nashorn: scheme
498 // These scripts are always available and are loaded from nashorn.jar's resources.
499 return AccessController.doPrivileged(
500 new PrivilegedAction<Source>() {
501 @Override
502 public Source run() {
503 try {
504 final URL resURL = Context.class.getResource(resource);
505 return (resURL != null)? new Source(srcStr, resURL) : null;
506 } catch (final IOException exp) {
507 return null;
508 }
509 }
510 });
511 }
513 return null;
514 }
516 /**
517 * Implementation of {@code load} Nashorn extension. Load a script file from a source
518 * expression
519 *
520 * @param scope the scope
521 * @param from source expression for script
522 *
523 * @return return value for load call (undefined)
524 *
525 * @throws IOException if source cannot be found or loaded
526 */
527 public Object load(final ScriptObject scope, final Object from) throws IOException {
528 final Object src = (from instanceof ConsString)? from.toString() : from;
529 Source source = null;
531 // load accepts a String (which could be a URL or a file name), a File, a URL
532 // or a ScriptObject that has "name" and "source" (string valued) properties.
533 if (src instanceof String) {
534 final String srcStr = (String)src;
535 if (srcStr.startsWith(LOAD_CLASSPATH)) {
536 URL url = getResourceURL(srcStr.substring(LOAD_CLASSPATH.length()));
537 source = (url != null)? new Source(url.toString(), url) : null;
538 } else {
539 final File file = new File(srcStr);
540 if (srcStr.indexOf(':') != -1) {
541 if ((source = loadInternal(srcStr, LOAD_NASHORN, "resources/")) == null &&
542 (source = loadInternal(srcStr, LOAD_FX, "resources/fx/")) == null) {
543 URL url;
544 try {
545 //check for malformed url. if malformed, it may still be a valid file
546 url = new URL(srcStr);
547 } catch (final MalformedURLException e) {
548 url = file.toURI().toURL();
549 }
550 source = new Source(url.toString(), url);
551 }
552 } else if (file.isFile()) {
553 source = new Source(srcStr, file);
554 }
555 }
556 } else if (src instanceof File && ((File)src).isFile()) {
557 final File file = (File)src;
558 source = new Source(file.getName(), file);
559 } else if (src instanceof URL) {
560 final URL url = (URL)src;
561 source = new Source(url.toString(), url);
562 } else if (src instanceof ScriptObject) {
563 final ScriptObject sobj = (ScriptObject)src;
564 if (sobj.has("script") && sobj.has("name")) {
565 final String script = JSType.toString(sobj.get("script"));
566 final String name = JSType.toString(sobj.get("name"));
567 source = new Source(name, script);
568 }
569 } else if (src instanceof Map) {
570 final Map<?,?> map = (Map<?,?>)src;
571 if (map.containsKey("script") && map.containsKey("name")) {
572 final String script = JSType.toString(map.get("script"));
573 final String name = JSType.toString(map.get("name"));
574 source = new Source(name, script);
575 }
576 }
578 if (source != null) {
579 return evaluateSource(source, scope, scope);
580 }
582 throw typeError("cant.load.script", ScriptRuntime.safeToString(from));
583 }
585 /**
586 * Implementation of {@code loadWithNewGlobal} Nashorn extension. Load a script file from a source
587 * expression, after creating a new global scope.
588 *
589 * @param from source expression for script
590 * @param args (optional) arguments to be passed to the loaded script
591 *
592 * @return return value for load call (undefined)
593 *
594 * @throws IOException if source cannot be found or loaded
595 */
596 public Object loadWithNewGlobal(final Object from, final Object...args) throws IOException {
597 final ScriptObject oldGlobal = getGlobalTrusted();
598 final ScriptObject newGlobal = AccessController.doPrivileged(new PrivilegedAction<ScriptObject>() {
599 @Override
600 public ScriptObject run() {
601 try {
602 return newGlobal();
603 } catch (final RuntimeException e) {
604 if (Context.DEBUG) {
605 e.printStackTrace();
606 }
607 throw e;
608 }
609 }
610 }, CREATE_GLOBAL_ACC_CTXT);
611 // initialize newly created Global instance
612 initGlobal(newGlobal);
613 setGlobalTrusted(newGlobal);
615 final Object[] wrapped = args == null? ScriptRuntime.EMPTY_ARRAY : ScriptObjectMirror.wrapArray(args, oldGlobal);
616 newGlobal.put("arguments", ((GlobalObject)newGlobal).wrapAsObject(wrapped), env._strict);
618 try {
619 // wrap objects from newGlobal's world as mirrors - but if result
620 // is from oldGlobal's world, unwrap it!
621 return ScriptObjectMirror.unwrap(ScriptObjectMirror.wrap(load(newGlobal, from), newGlobal), oldGlobal);
622 } finally {
623 setGlobalTrusted(oldGlobal);
624 }
625 }
627 /**
628 * Load or get a structure class. Structure class names are based on the number of parameter fields
629 * and {@link AccessorProperty} fields in them. Structure classes are used to represent ScriptObjects
630 *
631 * @see ObjectClassGenerator
632 * @see AccessorProperty
633 * @see ScriptObject
634 *
635 * @param fullName full name of class, e.g. jdk.nashorn.internal.objects.JO2P1 contains 2 fields and 1 parameter.
636 *
637 * @return the {@code Class<?>} for this structure
638 *
639 * @throws ClassNotFoundException if structure class cannot be resolved
640 */
641 public static Class<?> forStructureClass(final String fullName) throws ClassNotFoundException {
642 if (System.getSecurityManager() != null && !StructureLoader.isStructureClass(fullName)) {
643 throw new ClassNotFoundException(fullName);
644 }
645 return Class.forName(fullName, true, sharedLoader);
646 }
648 /**
649 * Checks that the given Class can be accessed from no permissions context.
650 *
651 * @param clazz Class object
652 * @throw SecurityException if not accessible
653 */
654 public static void checkPackageAccess(final Class<?> clazz) {
655 final SecurityManager sm = System.getSecurityManager();
656 if (sm != null) {
657 Class<?> bottomClazz = clazz;
658 while (bottomClazz.isArray()) {
659 bottomClazz = bottomClazz.getComponentType();
660 }
661 checkPackageAccess(sm, bottomClazz.getName());
662 }
663 }
665 /**
666 * Checks that the given package name can be accessed from no permissions context.
667 *
668 * @param pkgName package name
669 * @throw SecurityException if not accessible
670 */
671 public static void checkPackageAccess(final String pkgName) {
672 final SecurityManager sm = System.getSecurityManager();
673 if (sm != null) {
674 checkPackageAccess(sm, pkgName.endsWith(".") ? pkgName : pkgName + ".");
675 }
676 }
678 /**
679 * Checks that the given package can be accessed from no permissions context.
680 *
681 * @param sm current security manager instance
682 * @param fullName fully qualified package name
683 * @throw SecurityException if not accessible
684 */
685 private static void checkPackageAccess(final SecurityManager sm, final String fullName) {
686 sm.getClass(); // null check
687 final int index = fullName.lastIndexOf('.');
688 if (index != -1) {
689 final String pkgName = fullName.substring(0, index);
690 AccessController.doPrivileged(new PrivilegedAction<Void>() {
691 @Override
692 public Void run() {
693 sm.checkPackageAccess(pkgName);
694 return null;
695 }
696 }, NO_PERMISSIONS_ACC_CTXT);
697 }
698 }
700 /**
701 * Checks that the given Class can be accessed from no permissions context.
702 *
703 * @param clazz Class object
704 * @return true if package is accessible, false otherwise
705 */
706 private static boolean isAccessiblePackage(final Class<?> clazz) {
707 try {
708 checkPackageAccess(clazz);
709 return true;
710 } catch (final SecurityException se) {
711 return false;
712 }
713 }
715 /**
716 * Checks that the given Class is public and it can be accessed from no permissions context.
717 *
718 * @param clazz Class object to check
719 * @return true if Class is accessible, false otherwise
720 */
721 public static boolean isAccessibleClass(final Class<?> clazz) {
722 return Modifier.isPublic(clazz.getModifiers()) && Context.isAccessiblePackage(clazz);
723 }
725 /**
726 * Lookup a Java class. This is used for JSR-223 stuff linking in from
727 * {@code jdk.nashorn.internal.objects.NativeJava} and {@code jdk.nashorn.internal.runtime.NativeJavaPackage}
728 *
729 * @param fullName full name of class to load
730 *
731 * @return the {@code Class<?>} for the name
732 *
733 * @throws ClassNotFoundException if class cannot be resolved
734 */
735 public Class<?> findClass(final String fullName) throws ClassNotFoundException {
736 if (fullName.indexOf('[') != -1 || fullName.indexOf('/') != -1) {
737 // don't allow array class names or internal names.
738 throw new ClassNotFoundException(fullName);
739 }
741 // check package access as soon as possible!
742 final SecurityManager sm = System.getSecurityManager();
743 if (sm != null) {
744 checkPackageAccess(sm, fullName);
745 }
747 // try the script -classpath loader, if that is set
748 if (classPathLoader != null) {
749 try {
750 return Class.forName(fullName, true, classPathLoader);
751 } catch (final ClassNotFoundException ignored) {
752 // ignore, continue search
753 }
754 }
756 // Try finding using the "app" loader.
757 return Class.forName(fullName, true, appLoader);
758 }
760 /**
761 * Hook to print stack trace for a {@link Throwable} that occurred during
762 * execution
763 *
764 * @param t throwable for which to dump stack
765 */
766 public static void printStackTrace(final Throwable t) {
767 if (Context.DEBUG) {
768 t.printStackTrace(Context.getCurrentErr());
769 }
770 }
772 /**
773 * Verify generated bytecode before emission. This is called back from the
774 * {@link ObjectClassGenerator} or the {@link Compiler}. If the "--verify-code" parameter
775 * hasn't been given, this is a nop
776 *
777 * Note that verification may load classes -- we don't want to do that unless
778 * user specified verify option. We check it here even though caller
779 * may have already checked that flag
780 *
781 * @param bytecode bytecode to verify
782 */
783 public void verify(final byte[] bytecode) {
784 if (env._verify_code) {
785 // No verification when security manager is around as verifier
786 // may load further classes - which should be avoided.
787 if (System.getSecurityManager() == null) {
788 CheckClassAdapter.verify(new ClassReader(bytecode), sharedLoader, false, new PrintWriter(System.err, true));
789 }
790 }
791 }
793 /**
794 * Create and initialize a new global scope object.
795 *
796 * @return the initialized global scope object.
797 */
798 public ScriptObject createGlobal() {
799 return initGlobal(newGlobal());
800 }
802 /**
803 * Create a new uninitialized global scope object
804 * @return the global script object
805 */
806 public ScriptObject newGlobal() {
807 return new Global(this);
808 }
810 /**
811 * Initialize given global scope object.
812 *
813 * @param global the global
814 * @return the initialized global scope object.
815 */
816 public ScriptObject initGlobal(final ScriptObject global) {
817 if (! (global instanceof GlobalObject)) {
818 throw new IllegalArgumentException("not a global object!");
819 }
821 // Need only minimal global object, if we are just compiling.
822 if (!env._compile_only) {
823 final ScriptObject oldGlobal = Context.getGlobalTrusted();
824 try {
825 Context.setGlobalTrusted(global);
826 // initialize global scope with builtin global objects
827 ((GlobalObject)global).initBuiltinObjects();
828 } finally {
829 Context.setGlobalTrusted(oldGlobal);
830 }
831 }
833 return global;
834 }
836 /**
837 * Trusted variants - package-private
838 */
840 /**
841 * Return the current global scope
842 * @return current global scope
843 */
844 static ScriptObject getGlobalTrusted() {
845 return currentGlobal.get();
846 }
848 /**
849 * Set the current global scope
850 */
851 static void setGlobalTrusted(ScriptObject global) {
852 currentGlobal.set(global);
853 }
855 /**
856 * Return the current global's context
857 * @return current global's context
858 */
859 static Context getContextTrusted() {
860 return Context.getGlobalTrusted().getContext();
861 }
863 /**
864 * Try to infer Context instance from the Class. If we cannot,
865 * then get it from the thread local variable.
866 *
867 * @param clazz the class
868 * @return context
869 */
870 static Context fromClass(final Class<?> clazz) {
871 final ClassLoader loader = clazz.getClassLoader();
873 if (loader instanceof ScriptLoader) {
874 return ((ScriptLoader)loader).getContext();
875 }
877 return Context.getContextTrusted();
878 }
880 private URL getResourceURL(final String resName) {
881 // try the classPathLoader if we have and then
882 // try the appLoader if non-null.
883 if (classPathLoader != null) {
884 return classPathLoader.getResource(resName);
885 } else if (appLoader != null) {
886 return appLoader.getResource(resName);
887 }
889 return null;
890 }
892 private Object evaluateSource(final Source source, final ScriptObject scope, final ScriptObject thiz) {
893 ScriptFunction script = null;
895 try {
896 script = compileScript(source, scope, new Context.ThrowErrorManager());
897 } catch (final ParserException e) {
898 e.throwAsEcmaException();
899 }
901 return ScriptRuntime.apply(script, thiz);
902 }
904 private static ScriptFunction getRunScriptFunction(final Class<?> script, final ScriptObject scope) {
905 if (script == null) {
906 return null;
907 }
909 // Get run method - the entry point to the script
910 final MethodHandle runMethodHandle =
911 MH.findStatic(
912 MethodHandles.lookup(),
913 script,
914 RUN_SCRIPT.symbolName(),
915 MH.type(
916 Object.class,
917 ScriptFunction.class,
918 Object.class));
920 boolean strict;
922 try {
923 strict = script.getField(STRICT_MODE.symbolName()).getBoolean(null);
924 } catch (final NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException e) {
925 strict = false;
926 }
928 // Package as a JavaScript function and pass function back to shell.
929 return ((GlobalObject)Context.getGlobalTrusted()).newScriptFunction(RUN_SCRIPT.symbolName(), runMethodHandle, scope, strict);
930 }
932 private ScriptFunction compileScript(final Source source, final ScriptObject scope, final ErrorManager errMan) {
933 return getRunScriptFunction(compile(source, errMan, this._strict), scope);
934 }
936 private synchronized Class<?> compile(final Source source, final ErrorManager errMan, final boolean strict) {
937 // start with no errors, no warnings.
938 errMan.reset();
940 Class<?> script = findCachedClass(source);
941 if (script != null) {
942 Compiler.LOG.fine("Code cache hit for ", source, " avoiding recompile.");
943 return script;
944 }
946 final FunctionNode functionNode = new Parser(env, source, errMan, strict).parse();
947 if (errors.hasErrors()) {
948 return null;
949 }
951 if (env._print_ast) {
952 getErr().println(new ASTWriter(functionNode));
953 }
955 if (env._print_parse) {
956 getErr().println(new PrintVisitor(functionNode));
957 }
959 if (env._parse_only) {
960 return null;
961 }
963 final URL url = source.getURL();
964 final ScriptLoader loader = env._loader_per_compile ? createNewLoader() : scriptLoader;
965 final CodeSource cs = new CodeSource(url, (CodeSigner[])null);
966 final CodeInstaller<ScriptEnvironment> installer = new ContextCodeInstaller(this, loader, cs);
968 final Compiler compiler = new Compiler(installer, strict);
970 final FunctionNode newFunctionNode = compiler.compile(functionNode);
971 script = compiler.install(newFunctionNode);
972 cacheClass(source, script);
974 return script;
975 }
977 private ScriptLoader createNewLoader() {
978 return AccessController.doPrivileged(
979 new PrivilegedAction<ScriptLoader>() {
980 @Override
981 public ScriptLoader run() {
982 return new ScriptLoader(appLoader, Context.this);
983 }
984 }, CREATE_LOADER_ACC_CTXT);
985 }
987 private long getUniqueEvalId() {
988 return uniqueEvalId.getAndIncrement();
989 }
991 private long getUniqueScriptId() {
992 return uniqueScriptId.getAndIncrement();
993 }
995 /**
996 * Cache for compiled script classes.
997 */
998 @SuppressWarnings("serial")
999 private static class ClassCache extends LinkedHashMap<Source, ClassReference> {
1000 private final int size;
1001 private final ReferenceQueue<Class<?>> queue;
1003 ClassCache(int size) {
1004 super(size, 0.75f, true);
1005 this.size = size;
1006 this.queue = new ReferenceQueue<>();
1007 }
1009 void cache(final Source source, final Class<?> clazz) {
1010 put(source, new ClassReference(clazz, queue, source));
1011 }
1013 @Override
1014 protected boolean removeEldestEntry(final Map.Entry<Source, ClassReference> eldest) {
1015 return size() > size;
1016 }
1018 @Override
1019 public ClassReference get(Object key) {
1020 for (ClassReference ref; (ref = (ClassReference)queue.poll()) != null; ) {
1021 remove(ref.source);
1022 }
1023 return super.get(key);
1024 }
1026 }
1028 private static class ClassReference extends SoftReference<Class<?>> {
1029 private final Source source;
1031 ClassReference(final Class<?> clazz, final ReferenceQueue<Class<?>> queue, final Source source) {
1032 super(clazz, queue);
1033 this.source = source;
1034 }
1035 }
1037 // Class cache management
1038 private Class<?> findCachedClass(final Source source) {
1039 ClassReference ref = classCache == null ? null : classCache.get(source);
1040 return ref != null ? ref.get() : null;
1041 }
1043 private void cacheClass(final Source source, final Class<?> clazz) {
1044 if (classCache != null) {
1045 classCache.cache(source, clazz);
1046 }
1047 }
1050 }