Fri, 13 Sep 2013 16:45:11 +0530
8024619: JDBC java.sql.DriverManager is not usable from JS script
Reviewed-by: jlaskey, lagergren, attila
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.Modifier;
40 import java.util.concurrent.atomic.AtomicLong;
41 import java.net.MalformedURLException;
42 import java.net.URL;
43 import java.security.AccessControlContext;
44 import java.security.AccessController;
45 import java.security.CodeSigner;
46 import java.security.CodeSource;
47 import java.security.Permissions;
48 import java.security.PrivilegedAction;
49 import java.security.ProtectionDomain;
50 import java.util.Map;
52 import jdk.internal.org.objectweb.asm.ClassReader;
53 import jdk.internal.org.objectweb.asm.util.CheckClassAdapter;
54 import jdk.nashorn.api.scripting.ScriptObjectMirror;
55 import jdk.nashorn.internal.codegen.Compiler;
56 import jdk.nashorn.internal.codegen.ObjectClassGenerator;
57 import jdk.nashorn.internal.ir.FunctionNode;
58 import jdk.nashorn.internal.ir.debug.ASTWriter;
59 import jdk.nashorn.internal.ir.debug.PrintVisitor;
60 import jdk.nashorn.internal.objects.Global;
61 import jdk.nashorn.internal.parser.Parser;
62 import jdk.nashorn.internal.runtime.options.Options;
64 /**
65 * This class manages the global state of execution. Context is immutable.
66 */
67 public final class Context {
68 // nashorn specific security runtime access permission names
69 /**
70 * Permission needed to pass arbitrary nashorn command line options when creating Context.
71 */
72 public static final String NASHORN_SET_CONFIG = "nashorn.setConfig";
74 /**
75 * Permission needed to create Nashorn Context instance.
76 */
77 public static final String NASHORN_CREATE_CONTEXT = "nashorn.createContext";
79 /**
80 * Permission needed to create Nashorn Global instance.
81 */
82 public static final String NASHORN_CREATE_GLOBAL = "nashorn.createGlobal";
84 /**
85 * Permission to get current Nashorn Context from thread local storage.
86 */
87 public static final String NASHORN_GET_CONTEXT = "nashorn.getContext";
89 /**
90 * Permission to use Java reflection/jsr292 from script code.
91 */
92 public static final String NASHORN_JAVA_REFLECTION = "nashorn.JavaReflection";
94 /* Force DebuggerSupport to be loaded. */
95 static {
96 DebuggerSupport.FORCELOAD = true;
97 }
99 /**
100 * ContextCodeInstaller that has the privilege of installing classes in the Context.
101 * Can only be instantiated from inside the context and is opaque to other classes
102 */
103 public static class ContextCodeInstaller implements CodeInstaller<ScriptEnvironment> {
104 private final Context context;
105 private final ScriptLoader loader;
106 private final CodeSource codeSource;
108 private ContextCodeInstaller(final Context context, final ScriptLoader loader, final CodeSource codeSource) {
109 this.context = context;
110 this.loader = loader;
111 this.codeSource = codeSource;
112 }
114 /**
115 * Return the context for this installer
116 * @return ScriptEnvironment
117 */
118 @Override
119 public ScriptEnvironment getOwner() {
120 return context.env;
121 }
123 @Override
124 public Class<?> install(final String className, final byte[] bytecode) {
125 return loader.installClass(className, bytecode, codeSource);
126 }
128 @Override
129 public void verify(final byte[] code) {
130 context.verify(code);
131 }
133 @Override
134 public long getUniqueScriptId() {
135 return context.getUniqueScriptId();
136 }
137 }
139 /** Is Context global debug mode enabled ? */
140 public static final boolean DEBUG = Options.getBooleanProperty("nashorn.debug");
142 private static final ThreadLocal<ScriptObject> currentGlobal = new ThreadLocal<>();
144 /**
145 * Get the current global scope
146 * @return the current global scope
147 */
148 public static ScriptObject getGlobal() {
149 // This class in a package.access protected package.
150 // Trusted code only can call this method.
151 return getGlobalTrusted();
152 }
154 /**
155 * Set the current global scope
156 * @param global the global scope
157 */
158 public static void setGlobal(final ScriptObject global) {
159 if (global != null && !(global instanceof Global)) {
160 throw new IllegalArgumentException("global is not an instance of Global!");
161 }
163 setGlobalTrusted(global);
164 }
166 /**
167 * Get context of the current global
168 * @return current global scope's context.
169 */
170 public static Context getContext() {
171 final SecurityManager sm = System.getSecurityManager();
172 if (sm != null) {
173 sm.checkPermission(new RuntimePermission(NASHORN_GET_CONTEXT));
174 }
175 return getContextTrusted();
176 }
178 /**
179 * Get current context's error writer
180 *
181 * @return error writer of the current context
182 */
183 public static PrintWriter getCurrentErr() {
184 final ScriptObject global = getGlobalTrusted();
185 return (global != null)? global.getContext().getErr() : new PrintWriter(System.err);
186 }
188 /**
189 * Output text to this Context's error stream
190 * @param str text to write
191 */
192 public static void err(final String str) {
193 err(str, true);
194 }
196 /**
197 * Output text to this Context's error stream, optionally with
198 * a newline afterwards
199 *
200 * @param str text to write
201 * @param crlf write a carriage return/new line after text
202 */
203 @SuppressWarnings("resource")
204 public static void err(final String str, final boolean crlf) {
205 final PrintWriter err = Context.getCurrentErr();
206 if (err != null) {
207 if (crlf) {
208 err.println(str);
209 } else {
210 err.print(str);
211 }
212 }
213 }
215 /** Current environment. */
216 private final ScriptEnvironment env;
218 /** is this context in strict mode? Cached from env. as this is used heavily. */
219 final boolean _strict;
221 /** class loader to resolve classes from script. */
222 private final ClassLoader appLoader;
224 /** Class loader to load classes from -classpath option, if set. */
225 private final ClassLoader classPathLoader;
227 /** Class loader to load classes compiled from scripts. */
228 private final ScriptLoader scriptLoader;
230 /** Current error manager. */
231 private final ErrorManager errors;
233 /** Unique id for script. Used only when --loader-per-compile=false */
234 private final AtomicLong uniqueScriptId;
236 private static final ClassLoader myLoader = Context.class.getClassLoader();
237 private static final StructureLoader sharedLoader;
239 /*package-private*/ ClassLoader getSharedLoader() {
240 return sharedLoader;
241 }
243 private static AccessControlContext createNoPermAccCtxt() {
244 return new AccessControlContext(new ProtectionDomain[] { new ProtectionDomain(null, new Permissions()) });
245 }
247 private static AccessControlContext createPermAccCtxt(final String permName) {
248 final Permissions perms = new Permissions();
249 perms.add(new RuntimePermission(permName));
250 return new AccessControlContext(new ProtectionDomain[] { new ProtectionDomain(null, perms) });
251 }
253 private static final AccessControlContext NO_PERMISSIONS_ACC_CTXT = createNoPermAccCtxt();
254 private static final AccessControlContext CREATE_LOADER_ACC_CTXT = createPermAccCtxt("createClassLoader");
255 private static final AccessControlContext CREATE_GLOBAL_ACC_CTXT = createPermAccCtxt(NASHORN_CREATE_GLOBAL);
257 static {
258 sharedLoader = AccessController.doPrivileged(new PrivilegedAction<StructureLoader>() {
259 @Override
260 public StructureLoader run() {
261 return new StructureLoader(myLoader);
262 }
263 }, CREATE_LOADER_ACC_CTXT);
264 }
266 /**
267 * ThrowErrorManager that throws ParserException upon error conditions.
268 */
269 public static class ThrowErrorManager extends ErrorManager {
270 @Override
271 public void error(final String message) {
272 throw new ParserException(message);
273 }
275 @Override
276 public void error(final ParserException e) {
277 throw e;
278 }
279 }
281 /**
282 * Constructor
283 *
284 * @param options options from command line or Context creator
285 * @param errors error manger
286 * @param appLoader application class loader
287 */
288 public Context(final Options options, final ErrorManager errors, final ClassLoader appLoader) {
289 this(options, errors, new PrintWriter(System.out, true), new PrintWriter(System.err, true), appLoader);
290 }
292 /**
293 * Constructor
294 *
295 * @param options options from command line or Context creator
296 * @param errors error manger
297 * @param out output writer for this Context
298 * @param err error writer for this Context
299 * @param appLoader application class loader
300 */
301 public Context(final Options options, final ErrorManager errors, final PrintWriter out, final PrintWriter err, final ClassLoader appLoader) {
302 final SecurityManager sm = System.getSecurityManager();
303 if (sm != null) {
304 sm.checkPermission(new RuntimePermission(NASHORN_CREATE_CONTEXT));
305 }
307 this.env = new ScriptEnvironment(options, out, err);
308 this._strict = env._strict;
309 this.appLoader = appLoader;
310 if (env._loader_per_compile) {
311 this.scriptLoader = null;
312 this.uniqueScriptId = null;
313 } else {
314 this.scriptLoader = createNewLoader();
315 this.uniqueScriptId = new AtomicLong();
316 }
317 this.errors = errors;
319 // if user passed -classpath option, make a class loader with that and set it as
320 // thread context class loader so that script can access classes from that path.
321 final String classPath = options.getString("classpath");
322 if (! env._compile_only && classPath != null && !classPath.isEmpty()) {
323 // make sure that caller can create a class loader.
324 if (sm != null) {
325 sm.checkPermission(new RuntimePermission("createClassLoader"));
326 }
327 this.classPathLoader = NashornLoader.createClassLoader(classPath);
328 } else {
329 this.classPathLoader = null;
330 }
332 // print version info if asked.
333 if (env._version) {
334 getErr().println("nashorn " + Version.version());
335 }
337 if (env._fullversion) {
338 getErr().println("nashorn full version " + Version.fullVersion());
339 }
340 }
342 /**
343 * Get the error manager for this context
344 * @return error manger
345 */
346 public ErrorManager getErrorManager() {
347 return errors;
348 }
350 /**
351 * Get the script environment for this context
352 * @return script environment
353 */
354 public ScriptEnvironment getEnv() {
355 return env;
356 }
358 /**
359 * Get the output stream for this context
360 * @return output print writer
361 */
362 public PrintWriter getOut() {
363 return env.getOut();
364 }
366 /**
367 * Get the error stream for this context
368 * @return error print writer
369 */
370 public PrintWriter getErr() {
371 return env.getErr();
372 }
374 /**
375 * Get the PropertyMap of the current global scope
376 * @return the property map of the current global scope
377 */
378 public static PropertyMap getGlobalMap() {
379 return Context.getGlobalTrusted().getMap();
380 }
382 /**
383 * Compile a top level script.
384 *
385 * @param source the source
386 * @param scope the scope
387 *
388 * @return top level function for script
389 */
390 public ScriptFunction compileScript(final Source source, final ScriptObject scope) {
391 return compileScript(source, scope, this.errors);
392 }
394 /**
395 * Entry point for {@code eval}
396 *
397 * @param initialScope The scope of this eval call
398 * @param string Evaluated code as a String
399 * @param callThis "this" to be passed to the evaluated code
400 * @param location location of the eval call
401 * @param strict is this {@code eval} call from a strict mode code?
402 *
403 * @return the return value of the {@code eval}
404 */
405 public Object eval(final ScriptObject initialScope, final String string, final Object callThis, final Object location, final boolean strict) {
406 final String file = (location == UNDEFINED || location == null) ? "<eval>" : location.toString();
407 final Source source = new Source(file, string);
408 final boolean directEval = location != UNDEFINED; // is this direct 'eval' call or indirectly invoked eval?
409 final ScriptObject global = Context.getGlobalTrusted();
411 ScriptObject scope = initialScope;
413 // ECMA section 10.1.1 point 2 says eval code is strict if it begins
414 // with "use strict" directive or eval direct call itself is made
415 // from from strict mode code. We are passed with caller's strict mode.
416 boolean strictFlag = directEval && strict;
418 Class<?> clazz = null;
419 try {
420 clazz = compile(source, new ThrowErrorManager(), strictFlag);
421 } catch (final ParserException e) {
422 e.throwAsEcmaException(global);
423 return null;
424 }
426 if (!strictFlag) {
427 // We need to get strict mode flag from compiled class. This is
428 // because eval code may start with "use strict" directive.
429 try {
430 strictFlag = clazz.getField(STRICT_MODE.symbolName()).getBoolean(null);
431 } catch (final NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException e) {
432 //ignored
433 strictFlag = false;
434 }
435 }
437 // In strict mode, eval does not instantiate variables and functions
438 // in the caller's environment. A new environment is created!
439 if (strictFlag) {
440 // Create a new scope object
441 final ScriptObject strictEvalScope = ((GlobalObject)global).newObject();
443 // bless it as a "scope"
444 strictEvalScope.setIsScope();
446 // set given scope to be it's proto so that eval can still
447 // access caller environment vars in the new environment.
448 strictEvalScope.setProto(scope);
449 scope = strictEvalScope;
450 }
452 ScriptFunction func = getRunScriptFunction(clazz, scope);
453 Object evalThis;
454 if (directEval) {
455 evalThis = (callThis instanceof ScriptObject || strictFlag) ? callThis : global;
456 } else {
457 evalThis = global;
458 }
460 return ScriptRuntime.apply(func, evalThis);
461 }
463 private static Source loadInternal(final String srcStr, final String prefix, final String resourcePath) {
464 if (srcStr.startsWith(prefix)) {
465 final String resource = resourcePath + srcStr.substring(prefix.length());
466 // NOTE: even sandbox scripts should be able to load scripts in nashorn: scheme
467 // These scripts are always available and are loaded from nashorn.jar's resources.
468 return AccessController.doPrivileged(
469 new PrivilegedAction<Source>() {
470 @Override
471 public Source run() {
472 try {
473 final URL resURL = Context.class.getResource(resource);
474 return (resURL != null)? new Source(srcStr, resURL) : null;
475 } catch (final IOException exp) {
476 return null;
477 }
478 }
479 });
480 }
482 return null;
483 }
485 /**
486 * Implementation of {@code load} Nashorn extension. Load a script file from a source
487 * expression
488 *
489 * @param scope the scope
490 * @param from source expression for script
491 *
492 * @return return value for load call (undefined)
493 *
494 * @throws IOException if source cannot be found or loaded
495 */
496 public Object load(final ScriptObject scope, final Object from) throws IOException {
497 final Object src = (from instanceof ConsString)? from.toString() : from;
498 Source source = null;
500 // load accepts a String (which could be a URL or a file name), a File, a URL
501 // or a ScriptObject that has "name" and "source" (string valued) properties.
502 if (src instanceof String) {
503 final String srcStr = (String)src;
504 final File file = new File(srcStr);
505 if (srcStr.indexOf(':') != -1) {
506 if ((source = loadInternal(srcStr, "nashorn:", "resources/")) == null &&
507 (source = loadInternal(srcStr, "fx:", "resources/fx/")) == null) {
508 URL url;
509 try {
510 //check for malformed url. if malformed, it may still be a valid file
511 url = new URL(srcStr);
512 } catch (final MalformedURLException e) {
513 url = file.toURI().toURL();
514 }
515 source = new Source(url.toString(), url);
516 }
517 } else if (file.isFile()) {
518 source = new Source(srcStr, file);
519 }
520 } else if (src instanceof File && ((File)src).isFile()) {
521 final File file = (File)src;
522 source = new Source(file.getName(), file);
523 } else if (src instanceof URL) {
524 final URL url = (URL)src;
525 source = new Source(url.toString(), url);
526 } else if (src instanceof ScriptObject) {
527 final ScriptObject sobj = (ScriptObject)src;
528 if (sobj.has("script") && sobj.has("name")) {
529 final String script = JSType.toString(sobj.get("script"));
530 final String name = JSType.toString(sobj.get("name"));
531 source = new Source(name, script);
532 }
533 } else if (src instanceof Map) {
534 final Map<?,?> map = (Map<?,?>)src;
535 if (map.containsKey("script") && map.containsKey("name")) {
536 final String script = JSType.toString(map.get("script"));
537 final String name = JSType.toString(map.get("name"));
538 source = new Source(name, script);
539 }
540 }
542 if (source != null) {
543 return evaluateSource(source, scope, scope);
544 }
546 throw typeError("cant.load.script", ScriptRuntime.safeToString(from));
547 }
549 /**
550 * Implementation of {@code loadWithNewGlobal} Nashorn extension. Load a script file from a source
551 * expression, after creating a new global scope.
552 *
553 * @param from source expression for script
554 * @param args (optional) arguments to be passed to the loaded script
555 *
556 * @return return value for load call (undefined)
557 *
558 * @throws IOException if source cannot be found or loaded
559 */
560 public Object loadWithNewGlobal(final Object from, final Object...args) throws IOException {
561 final ScriptObject oldGlobal = getGlobalTrusted();
562 final ScriptObject newGlobal = AccessController.doPrivileged(new PrivilegedAction<ScriptObject>() {
563 @Override
564 public ScriptObject run() {
565 try {
566 return newGlobal();
567 } catch (final RuntimeException e) {
568 if (Context.DEBUG) {
569 e.printStackTrace();
570 }
571 throw e;
572 }
573 }
574 }, CREATE_GLOBAL_ACC_CTXT);
575 // initialize newly created Global instance
576 initGlobal(newGlobal);
577 setGlobalTrusted(newGlobal);
579 final Object[] wrapped = args == null? ScriptRuntime.EMPTY_ARRAY : ScriptObjectMirror.wrapArray(args, oldGlobal);
580 newGlobal.put("arguments", ((GlobalObject)newGlobal).wrapAsObject(wrapped), env._strict);
582 try {
583 // wrap objects from newGlobal's world as mirrors - but if result
584 // is from oldGlobal's world, unwrap it!
585 return ScriptObjectMirror.unwrap(ScriptObjectMirror.wrap(load(newGlobal, from), newGlobal), oldGlobal);
586 } finally {
587 setGlobalTrusted(oldGlobal);
588 }
589 }
591 /**
592 * Load or get a structure class. Structure class names are based on the number of parameter fields
593 * and {@link AccessorProperty} fields in them. Structure classes are used to represent ScriptObjects
594 *
595 * @see ObjectClassGenerator
596 * @see AccessorProperty
597 * @see ScriptObject
598 *
599 * @param fullName full name of class, e.g. jdk.nashorn.internal.objects.JO2P1 contains 2 fields and 1 parameter.
600 *
601 * @return the {@code Class<?>} for this structure
602 *
603 * @throws ClassNotFoundException if structure class cannot be resolved
604 */
605 public static Class<?> forStructureClass(final String fullName) throws ClassNotFoundException {
606 if (System.getSecurityManager() != null && !StructureLoader.isStructureClass(fullName)) {
607 throw new ClassNotFoundException(fullName);
608 }
609 return Class.forName(fullName, true, sharedLoader);
610 }
612 /**
613 * Checks that the given package can be accessed from no permissions context.
614 *
615 * @param fullName fully qualified package name
616 * @throw SecurityException if not accessible
617 */
618 public static void checkPackageAccess(final String fullName) {
619 final int index = fullName.lastIndexOf('.');
620 if (index != -1) {
621 final SecurityManager sm = System.getSecurityManager();
622 if (sm != null) {
623 AccessController.doPrivileged(new PrivilegedAction<Void>() {
624 @Override
625 public Void run() {
626 sm.checkPackageAccess(fullName.substring(0, index));
627 return null;
628 }
629 }, NO_PERMISSIONS_ACC_CTXT);
630 }
631 }
632 }
634 /**
635 * Checks that the given package can be accessed from no permissions context.
636 *
637 * @param fullName fully qualified package name
638 * @return true if package is accessible, false otherwise
639 */
640 public static boolean isAccessiblePackage(final String fullName) {
641 try {
642 checkPackageAccess(fullName);
643 return true;
644 } catch (final SecurityException se) {
645 return false;
646 }
647 }
649 /**
650 * Checks that the given Class is public and it can be accessed from no permissions context.
651 *
652 * @param clazz Class object to check
653 * @return true if Class is accessible, false otherwise
654 */
655 public static boolean isAccessibleClass(final Class<?> clazz) {
656 return Modifier.isPublic(clazz.getModifiers()) && Context.isAccessiblePackage(clazz.getName());
657 }
659 /**
660 * Lookup a Java class. This is used for JSR-223 stuff linking in from
661 * {@code jdk.nashorn.internal.objects.NativeJava} and {@code jdk.nashorn.internal.runtime.NativeJavaPackage}
662 *
663 * @param fullName full name of class to load
664 *
665 * @return the {@code Class<?>} for the name
666 *
667 * @throws ClassNotFoundException if class cannot be resolved
668 */
669 public Class<?> findClass(final String fullName) throws ClassNotFoundException {
670 // check package access as soon as possible!
671 checkPackageAccess(fullName);
673 // try the script -classpath loader, if that is set
674 if (classPathLoader != null) {
675 try {
676 return Class.forName(fullName, true, classPathLoader);
677 } catch (final ClassNotFoundException ignored) {
678 // ignore, continue search
679 }
680 }
682 // Try finding using the "app" loader.
683 return Class.forName(fullName, true, appLoader);
684 }
686 /**
687 * Hook to print stack trace for a {@link Throwable} that occurred during
688 * execution
689 *
690 * @param t throwable for which to dump stack
691 */
692 public static void printStackTrace(final Throwable t) {
693 if (Context.DEBUG) {
694 t.printStackTrace(Context.getCurrentErr());
695 }
696 }
698 /**
699 * Verify generated bytecode before emission. This is called back from the
700 * {@link ObjectClassGenerator} or the {@link Compiler}. If the "--verify-code" parameter
701 * hasn't been given, this is a nop
702 *
703 * Note that verification may load classes -- we don't want to do that unless
704 * user specified verify option. We check it here even though caller
705 * may have already checked that flag
706 *
707 * @param bytecode bytecode to verify
708 */
709 public void verify(final byte[] bytecode) {
710 if (env._verify_code) {
711 // No verification when security manager is around as verifier
712 // may load further classes - which should be avoided.
713 if (System.getSecurityManager() == null) {
714 CheckClassAdapter.verify(new ClassReader(bytecode), sharedLoader, false, new PrintWriter(System.err, true));
715 }
716 }
717 }
719 /**
720 * Create and initialize a new global scope object.
721 *
722 * @return the initialized global scope object.
723 */
724 public ScriptObject createGlobal() {
725 return initGlobal(newGlobal());
726 }
728 /**
729 * Create a new uninitialized global scope object
730 * @return the global script object
731 */
732 public ScriptObject newGlobal() {
733 return new Global(this);
734 }
736 /**
737 * Initialize given global scope object.
738 *
739 * @param global the global
740 * @return the initialized global scope object.
741 */
742 public ScriptObject initGlobal(final ScriptObject global) {
743 if (! (global instanceof GlobalObject)) {
744 throw new IllegalArgumentException("not a global object!");
745 }
747 // Need only minimal global object, if we are just compiling.
748 if (!env._compile_only) {
749 final ScriptObject oldGlobal = Context.getGlobalTrusted();
750 try {
751 Context.setGlobalTrusted(global);
752 // initialize global scope with builtin global objects
753 ((GlobalObject)global).initBuiltinObjects();
754 } finally {
755 Context.setGlobalTrusted(oldGlobal);
756 }
757 }
759 return global;
760 }
762 /**
763 * Trusted variants - package-private
764 */
766 /**
767 * Return the current global scope
768 * @return current global scope
769 */
770 static ScriptObject getGlobalTrusted() {
771 return currentGlobal.get();
772 }
774 /**
775 * Set the current global scope
776 */
777 static void setGlobalTrusted(ScriptObject global) {
778 currentGlobal.set(global);
779 }
781 /**
782 * Return the current global's context
783 * @return current global's context
784 */
785 static Context getContextTrusted() {
786 return Context.getGlobalTrusted().getContext();
787 }
789 /**
790 * Try to infer Context instance from the Class. If we cannot,
791 * then get it from the thread local variable.
792 *
793 * @param clazz the class
794 * @return context
795 */
796 static Context fromClass(final Class<?> clazz) {
797 final ClassLoader loader = clazz.getClassLoader();
799 if (loader instanceof ScriptLoader) {
800 return ((ScriptLoader)loader).getContext();
801 }
803 return Context.getContextTrusted();
804 }
806 private Object evaluateSource(final Source source, final ScriptObject scope, final ScriptObject thiz) {
807 ScriptFunction script = null;
809 try {
810 script = compileScript(source, scope, new Context.ThrowErrorManager());
811 } catch (final ParserException e) {
812 e.throwAsEcmaException();
813 }
815 return ScriptRuntime.apply(script, thiz);
816 }
818 private static ScriptFunction getRunScriptFunction(final Class<?> script, final ScriptObject scope) {
819 if (script == null) {
820 return null;
821 }
823 // Get run method - the entry point to the script
824 final MethodHandle runMethodHandle =
825 MH.findStatic(
826 MethodHandles.lookup(),
827 script,
828 RUN_SCRIPT.symbolName(),
829 MH.type(
830 Object.class,
831 ScriptFunction.class,
832 Object.class));
834 boolean strict;
836 try {
837 strict = script.getField(STRICT_MODE.symbolName()).getBoolean(null);
838 } catch (final NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException e) {
839 strict = false;
840 }
842 // Package as a JavaScript function and pass function back to shell.
843 return ((GlobalObject)Context.getGlobalTrusted()).newScriptFunction(RUN_SCRIPT.symbolName(), runMethodHandle, scope, strict);
844 }
846 private ScriptFunction compileScript(final Source source, final ScriptObject scope, final ErrorManager errMan) {
847 return getRunScriptFunction(compile(source, errMan, this._strict), scope);
848 }
850 private synchronized Class<?> compile(final Source source, final ErrorManager errMan, final boolean strict) {
851 // start with no errors, no warnings.
852 errMan.reset();
854 GlobalObject global = null;
855 Class<?> script;
857 if (env._class_cache_size > 0) {
858 global = (GlobalObject)Context.getGlobalTrusted();
859 script = global.findCachedClass(source);
860 if (script != null) {
861 Compiler.LOG.fine("Code cache hit for ", source, " avoiding recompile.");
862 return script;
863 }
864 }
866 final FunctionNode functionNode = new Parser(env, source, errMan, strict).parse();
867 if (errors.hasErrors()) {
868 return null;
869 }
871 if (env._print_ast) {
872 getErr().println(new ASTWriter(functionNode));
873 }
875 if (env._print_parse) {
876 getErr().println(new PrintVisitor(functionNode));
877 }
879 if (env._parse_only) {
880 return null;
881 }
883 final URL url = source.getURL();
884 final ScriptLoader loader = env._loader_per_compile ? createNewLoader() : scriptLoader;
885 final CodeSource cs = url == null ? null : new CodeSource(url, (CodeSigner[])null);
886 final CodeInstaller<ScriptEnvironment> installer = new ContextCodeInstaller(this, loader, cs);
888 final Compiler compiler = new Compiler(installer, strict);
890 final FunctionNode newFunctionNode = compiler.compile(functionNode);
891 script = compiler.install(newFunctionNode);
893 if (global != null) {
894 global.cacheClass(source, script);
895 }
897 return script;
898 }
900 private ScriptLoader createNewLoader() {
901 return AccessController.doPrivileged(
902 new PrivilegedAction<ScriptLoader>() {
903 @Override
904 public ScriptLoader run() {
905 return new ScriptLoader(appLoader, Context.this);
906 }
907 }, CREATE_LOADER_ACC_CTXT);
908 }
910 private long getUniqueScriptId() {
911 return uniqueScriptId.getAndIncrement();
912 }
913 }