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  */
  25 
  26 package jdk.nashorn.internal.runtime;
  27 
  28 import static jdk.nashorn.internal.codegen.CompilerConstants.CONSTANTS;
  29 import static jdk.nashorn.internal.codegen.CompilerConstants.RUN_SCRIPT;
  30 import static jdk.nashorn.internal.codegen.CompilerConstants.SOURCE;
  31 import static jdk.nashorn.internal.codegen.CompilerConstants.STRICT_MODE;
  32 import static jdk.nashorn.internal.lookup.Lookup.MH;
  33 import static jdk.nashorn.internal.runtime.ECMAErrors.typeError;
  34 import static jdk.nashorn.internal.runtime.ScriptRuntime.UNDEFINED;
  35 import static jdk.nashorn.internal.runtime.Source.sourceFor;
  36 
  37 import java.io.File;
  38 import java.io.IOException;
  39 import java.io.PrintWriter;
  40 import java.lang.invoke.MethodHandle;
  41 import java.lang.invoke.MethodHandles;
  42 import java.lang.ref.ReferenceQueue;
  43 import java.lang.ref.SoftReference;
  44 import java.lang.reflect.Field;
  45 import java.lang.reflect.Modifier;
  46 import java.net.MalformedURLException;
  47 import java.net.URL;
  48 import java.security.AccessControlContext;
  49 import java.security.AccessController;
  50 import java.security.CodeSigner;
  51 import java.security.CodeSource;
  52 import java.security.Permissions;
  53 import java.security.PrivilegedAction;
  54 import java.security.PrivilegedActionException;
  55 import java.security.PrivilegedExceptionAction;
  56 import java.security.ProtectionDomain;
  57 import java.util.HashMap;
  58 import java.util.LinkedHashMap;
  59 import java.util.Map;
  60 import java.util.concurrent.atomic.AtomicLong;
  61 import jdk.internal.org.objectweb.asm.ClassReader;
  62 import jdk.internal.org.objectweb.asm.util.CheckClassAdapter;
  63 import jdk.nashorn.api.scripting.ScriptObjectMirror;
  64 import jdk.nashorn.internal.codegen.Compiler;
  65 import jdk.nashorn.internal.codegen.ObjectClassGenerator;
  66 import jdk.nashorn.internal.ir.FunctionNode;
  67 import jdk.nashorn.internal.ir.debug.ASTWriter;
  68 import jdk.nashorn.internal.ir.debug.PrintVisitor;
  69 import jdk.nashorn.internal.objects.Global;
  70 import jdk.nashorn.internal.parser.Parser;
  71 import jdk.nashorn.internal.runtime.options.Options;
  72 
  73 /**
  74  * This class manages the global state of execution. Context is immutable.
  75  */
  76 public final class Context {
  77     // nashorn specific security runtime access permission names
  78     /**
  79      * Permission needed to pass arbitrary nashorn command line options when creating Context.
  80      */
  81     public static final String NASHORN_SET_CONFIG      = "nashorn.setConfig";
  82 
  83     /**
  84      * Permission needed to create Nashorn Context instance.
  85      */
  86     public static final String NASHORN_CREATE_CONTEXT  = "nashorn.createContext";
  87 
  88     /**
  89      * Permission needed to create Nashorn Global instance.
  90      */
  91     public static final String NASHORN_CREATE_GLOBAL   = "nashorn.createGlobal";
  92 
  93     /**
  94      * Permission to get current Nashorn Context from thread local storage.
  95      */
  96     public static final String NASHORN_GET_CONTEXT     = "nashorn.getContext";
  97 
  98     /**
  99      * Permission to use Java reflection/jsr292 from script code.
 100      */
 101     public static final String NASHORN_JAVA_REFLECTION = "nashorn.JavaReflection";
 102 
 103     /**
 104      * Permission to enable nashorn debug mode.
 105      */
 106     public static final String NASHORN_DEBUG_MODE = "nashorn.debugMode";
 107 
 108     // nashorn load psuedo URL prefixes
 109     private static final String LOAD_CLASSPATH = "classpath:";
 110     private static final String LOAD_FX = "fx:";
 111     private static final String LOAD_NASHORN = "nashorn:";
 112 
 113     /* Force DebuggerSupport to be loaded. */
 114     static {
 115         DebuggerSupport.FORCELOAD = true;
 116     }
 117 
 118     /**
 119      * ContextCodeInstaller that has the privilege of installing classes in the Context.
 120      * Can only be instantiated from inside the context and is opaque to other classes
 121      */
 122     public static class ContextCodeInstaller implements CodeInstaller<ScriptEnvironment> {
 123         private final Context      context;
 124         private final ScriptLoader loader;
 125         private final CodeSource   codeSource;
 126 
 127         private ContextCodeInstaller(final Context context, final ScriptLoader loader, final CodeSource codeSource) {
 128             this.context    = context;
 129             this.loader     = loader;
 130             this.codeSource = codeSource;
 131         }
 132 
 133         /**
 134          * Return the context for this installer
 135          * @return ScriptEnvironment
 136          */
 137         @Override
 138         public ScriptEnvironment getOwner() {
 139             return context.env;
 140         }
 141 
 142         @Override
 143         public Class<?> install(final String className, final byte[] bytecode, final Source source, final Object[] constants) {
 144             Compiler.LOG.fine("Installing class ", className);
 145 
 146             final String   binaryName = Compiler.binaryName(className);
 147             final Class<?> clazz      = loader.installClass(binaryName, bytecode, codeSource);
 148 
 149             try {
 150                 // Need doPrivileged because these fields are private
 151                 AccessController.doPrivileged(new PrivilegedExceptionAction<Void>() {
 152                     @Override
 153                     public Void run() throws Exception {
 154                         //use reflection to write source and constants table to installed classes
 155                         final Field sourceField    = clazz.getDeclaredField(SOURCE.symbolName());
 156                         final Field constantsField = clazz.getDeclaredField(CONSTANTS.symbolName());
 157                         sourceField.setAccessible(true);
 158                         constantsField.setAccessible(true);
 159                         sourceField.set(null, source);
 160                         constantsField.set(null, constants);
 161                         return null;
 162                     }
 163                 });
 164             } catch (final PrivilegedActionException e) {
 165                 throw new RuntimeException(e);
 166             }
 167 
 168             return clazz;
 169         }
 170 
 171         @Override
 172         public void verify(final byte[] code) {
 173             context.verify(code);
 174         }
 175 
 176         @Override
 177         public long getUniqueScriptId() {
 178             return context.getUniqueScriptId();
 179         }
 180 
 181         @Override
 182         public long getUniqueEvalId() {
 183             return context.getUniqueEvalId();
 184         }
 185 
 186         @Override
 187         public void storeCompiledScript(final Source source, final String mainClassName,
 188                                         final Map<String, byte[]> classBytes, final Object[] constants) {
 189             if (context.codeStore != null) {
 190                 try {
 191                     context.codeStore.putScript(source, mainClassName, classBytes, constants);
 192                 } catch (final IOException e) {
 193                     throw new RuntimeException(e);
 194                 }
 195             }
 196         }
 197     }
 198 
 199     /** Is Context global debug mode enabled ? */
 200     public static final boolean DEBUG = Options.getBooleanProperty("nashorn.debug");
 201 
 202     private static final ThreadLocal<Global> currentGlobal = new ThreadLocal<>();
 203 
 204     // in-memory cache for loaded classes
 205     private ClassCache classCache;
 206 
 207     // persistent code store
 208     private CodeStore codeStore;
 209 
 210     /**
 211      * Get the current global scope
 212      * @return the current global scope
 213      */
 214     public static Global getGlobal() {
 215         // This class in a package.access protected package.
 216         // Trusted code only can call this method.
 217         return currentGlobal.get();
 218     }
 219 
 220     /**
 221      * Set the current global scope
 222      * @param global the global scope
 223      */
 224     public static void setGlobal(final ScriptObject global) {
 225         if (global != null && !(global instanceof Global)) {
 226             throw new IllegalArgumentException("not a global!");
 227         }
 228         setGlobal((Global)global);
 229     }
 230 
 231     /**
 232      * Set the current global scope
 233      * @param global the global scope
 234      */
 235     public static void setGlobal(final Global global) {
 236         // This class in a package.access protected package.
 237         // Trusted code only can call this method.
 238         currentGlobal.set(global);
 239     }
 240 
 241     /**
 242      * Get context of the current global
 243      * @return current global scope's context.
 244      */
 245     public static Context getContext() {
 246         final SecurityManager sm = System.getSecurityManager();
 247         if (sm != null) {
 248             sm.checkPermission(new RuntimePermission(NASHORN_GET_CONTEXT));
 249         }
 250         return getContextTrusted();
 251     }
 252 
 253     /**
 254      * Get current context's error writer
 255      *
 256      * @return error writer of the current context
 257      */
 258     public static PrintWriter getCurrentErr() {
 259         final ScriptObject global = getGlobal();
 260         return (global != null)? global.getContext().getErr() : new PrintWriter(System.err);
 261     }
 262 
 263     /**
 264      * Output text to this Context's error stream
 265      * @param str text to write
 266      */
 267     public static void err(final String str) {
 268         err(str, true);
 269     }
 270 
 271     /**
 272      * Output text to this Context's error stream, optionally with
 273      * a newline afterwards
 274      *
 275      * @param str  text to write
 276      * @param crlf write a carriage return/new line after text
 277      */
 278     @SuppressWarnings("resource")
 279     public static void err(final String str, final boolean crlf) {
 280         final PrintWriter err = Context.getCurrentErr();
 281         if (err != null) {
 282             if (crlf) {
 283                 err.println(str);
 284             } else {
 285                 err.print(str);
 286             }
 287         }
 288     }
 289 
 290     /** Current environment. */
 291     private final ScriptEnvironment env;
 292 
 293     /** is this context in strict mode? Cached from env. as this is used heavily. */
 294     final boolean _strict;
 295 
 296     /** class loader to resolve classes from script. */
 297     private final ClassLoader  appLoader;
 298 
 299     /** Class loader to load classes from -classpath option, if set. */
 300     private final ClassLoader  classPathLoader;
 301 
 302     /** Class loader to load classes compiled from scripts. */
 303     private final ScriptLoader scriptLoader;
 304 
 305     /** Current error manager. */
 306     private final ErrorManager errors;
 307 
 308     /** Unique id for script. Used only when --loader-per-compile=false */
 309     private final AtomicLong uniqueScriptId;
 310 
 311     /** Unique id for 'eval' */
 312     private final AtomicLong uniqueEvalId;
 313 
 314     private static final ClassLoader myLoader = Context.class.getClassLoader();
 315     private static final StructureLoader sharedLoader;
 316 
 317     /*package-private*/ @SuppressWarnings("static-method")
 318     ClassLoader getSharedLoader() {
 319         return sharedLoader;
 320     }
 321 
 322     private static AccessControlContext createNoPermAccCtxt() {
 323         return new AccessControlContext(new ProtectionDomain[] { new ProtectionDomain(null, new Permissions()) });
 324     }
 325 
 326     private static AccessControlContext createPermAccCtxt(final String permName) {
 327         final Permissions perms = new Permissions();
 328         perms.add(new RuntimePermission(permName));
 329         return new AccessControlContext(new ProtectionDomain[] { new ProtectionDomain(null, perms) });
 330     }
 331 
 332     private static final AccessControlContext NO_PERMISSIONS_ACC_CTXT = createNoPermAccCtxt();
 333     private static final AccessControlContext CREATE_LOADER_ACC_CTXT  = createPermAccCtxt("createClassLoader");
 334     private static final AccessControlContext CREATE_GLOBAL_ACC_CTXT  = createPermAccCtxt(NASHORN_CREATE_GLOBAL);
 335 
 336     static {
 337         sharedLoader = AccessController.doPrivileged(new PrivilegedAction<StructureLoader>() {
 338             @Override
 339             public StructureLoader run() {
 340                 return new StructureLoader(myLoader);
 341             }
 342         }, CREATE_LOADER_ACC_CTXT);
 343     }
 344 
 345     /**
 346      * ThrowErrorManager that throws ParserException upon error conditions.
 347      */
 348     public static class ThrowErrorManager extends ErrorManager {
 349         @Override
 350         public void error(final String message) {
 351             throw new ParserException(message);
 352         }
 353 
 354         @Override
 355         public void error(final ParserException e) {
 356             throw e;
 357         }
 358     }
 359 
 360     /**
 361      * Constructor
 362      *
 363      * @param options options from command line or Context creator
 364      * @param errors  error manger
 365      * @param appLoader application class loader
 366      */
 367     public Context(final Options options, final ErrorManager errors, final ClassLoader appLoader) {
 368         this(options, errors, new PrintWriter(System.out, true), new PrintWriter(System.err, true), appLoader);
 369     }
 370 
 371     /**
 372      * Constructor
 373      *
 374      * @param options options from command line or Context creator
 375      * @param errors  error manger
 376      * @param out     output writer for this Context
 377      * @param err     error writer for this Context
 378      * @param appLoader application class loader
 379      */
 380     public Context(final Options options, final ErrorManager errors, final PrintWriter out, final PrintWriter err, final ClassLoader appLoader) {
 381         final SecurityManager sm = System.getSecurityManager();
 382         if (sm != null) {
 383             sm.checkPermission(new RuntimePermission(NASHORN_CREATE_CONTEXT));
 384         }
 385 
 386         this.env       = new ScriptEnvironment(options, out, err);
 387         this._strict   = env._strict;
 388         this.appLoader = appLoader;
 389         if (env._loader_per_compile) {
 390             this.scriptLoader = null;
 391             this.uniqueScriptId = null;
 392         } else {
 393             this.scriptLoader = createNewLoader();
 394             this.uniqueScriptId = new AtomicLong();
 395         }
 396         this.errors    = errors;
 397         this.uniqueEvalId = new AtomicLong();
 398 
 399         // if user passed -classpath option, make a class loader with that and set it as
 400         // thread context class loader so that script can access classes from that path.
 401         final String classPath = options.getString("classpath");
 402         if (! env._compile_only && classPath != null && !classPath.isEmpty()) {
 403             // make sure that caller can create a class loader.
 404             if (sm != null) {
 405                 sm.checkPermission(new RuntimePermission("createClassLoader"));
 406             }
 407             this.classPathLoader = NashornLoader.createClassLoader(classPath);
 408         } else {
 409             this.classPathLoader = null;
 410         }
 411 
 412         final int cacheSize = env._class_cache_size;
 413         if (cacheSize > 0) {
 414             classCache = new ClassCache(cacheSize);
 415         }
 416 
 417         if (env._persistent_cache) {
 418             if (env._lazy_compilation || env._specialize_calls != null) {
 419                 getErr().println("Can not use persistent class caching with lazy compilation or call specialization.");
 420             } else {
 421                 try {
 422                     final String cacheDir = Options.getStringProperty("nashorn.persistent.code.cache", "nashorn_code_cache");
 423                     codeStore = new CodeStore(cacheDir);
 424                 } catch (IOException e) {
 425                     throw new RuntimeException("Error initializing code cache", e);
 426                 }
 427             }
 428         }
 429 
 430         // print version info if asked.
 431         if (env._version) {
 432             getErr().println("nashorn " + Version.version());
 433         }
 434 
 435         if (env._fullversion) {
 436             getErr().println("nashorn full version " + Version.fullVersion());
 437         }
 438     }
 439 
 440     /**
 441      * Get the error manager for this context
 442      * @return error manger
 443      */
 444     public ErrorManager getErrorManager() {
 445         return errors;
 446     }
 447 
 448     /**
 449      * Get the script environment for this context
 450      * @return script environment
 451      */
 452     public ScriptEnvironment getEnv() {
 453         return env;
 454     }
 455 
 456     /**
 457      * Get the output stream for this context
 458      * @return output print writer
 459      */
 460     public PrintWriter getOut() {
 461         return env.getOut();
 462     }
 463 
 464     /**
 465      * Get the error stream for this context
 466      * @return error print writer
 467      */
 468     public PrintWriter getErr() {
 469         return env.getErr();
 470     }
 471 
 472     /**
 473      * Get the PropertyMap of the current global scope
 474      * @return the property map of the current global scope
 475      */
 476     public static PropertyMap getGlobalMap() {
 477         return Context.getGlobal().getMap();
 478     }
 479     
 480     /**
 481      * Compile a top level script.
 482      *
 483      * @param source the source
 484      * @param scope  the scope
 485      *
 486      * @return top level function for script
 487      */
 488     public ScriptFunction compileScript(final Source source, final ScriptObject scope) {
 489         return compileScript(source, scope, this.errors);
 490     }
 491     
 492     /**
 493      * Interface to represent compiled code that can be re-used across many
 494      * global scope instances
 495      */
 496     public static interface MultiGlobalCompiledScript {
 497         /**
 498          * Obtain script function object for a specific global scope object.
 499          * 
 500          * @param newGlobal global scope for which function object is obtained
 501          * @return script function for script level expressions
 502          */
 503         public ScriptFunction getFunction(final Global newGlobal);
 504     }
 505     
 506     /**
 507      * Compile a top level script.
 508      * 
 509      * @param source the script source
 510      * @return reusable compiled script across many global scopes.
 511      */
 512     public MultiGlobalCompiledScript compileScript(final Source source) {
 513         final Class<?> clazz = compile(source, this.errors, this._strict);
 514         final MethodHandle runMethodHandle = getRunScriptHandle(clazz);
 515         final boolean strict = isStrict(clazz);
 516         
 517         return new MultiGlobalCompiledScript() {           
 518             @Override
 519             public ScriptFunction getFunction(final Global newGlobal) {
 520                 return Context.getGlobal().newScriptFunction(RUN_SCRIPT.symbolName(), runMethodHandle, newGlobal, strict);
 521             }
 522         };
 523     }    
 524     
 525     /**
 526      * Entry point for {@code eval}
 527      *
 528      * @param initialScope The scope of this eval call
 529      * @param string       Evaluated code as a String
 530      * @param callThis     "this" to be passed to the evaluated code
 531      * @param location     location of the eval call
 532      * @param strict       is this {@code eval} call from a strict mode code?
 533      *
 534      * @return the return value of the {@code eval}
 535      */
 536     public Object eval(final ScriptObject initialScope, final String string, final Object callThis, final Object location, final boolean strict) {
 537         final String  file       = (location == UNDEFINED || location == null) ? "<eval>" : location.toString();
 538         final Source  source     = sourceFor(file, string);
 539         final boolean directEval = location != UNDEFINED; // is this direct 'eval' call or indirectly invoked eval?
 540         final Global  global = Context.getGlobal();
 541 
 542         ScriptObject scope = initialScope;
 543 
 544         // ECMA section 10.1.1 point 2 says eval code is strict if it begins
 545         // with "use strict" directive or eval direct call itself is made
 546         // from from strict mode code. We are passed with caller's strict mode.
 547         boolean strictFlag = directEval && strict;
 548 
 549         Class<?> clazz = null;
 550         try {
 551             clazz = compile(source, new ThrowErrorManager(), strictFlag);
 552         } catch (final ParserException e) {
 553             e.throwAsEcmaException(global);
 554             return null;
 555         }
 556 
 557         if (!strictFlag) {
 558             // We need to get strict mode flag from compiled class. This is
 559             // because eval code may start with "use strict" directive.
 560             try {
 561                 strictFlag = clazz.getField(STRICT_MODE.symbolName()).getBoolean(null);
 562             } catch (final NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException e) {
 563                 //ignored
 564                 strictFlag = false;
 565             }
 566         }
 567 
 568         // In strict mode, eval does not instantiate variables and functions
 569         // in the caller's environment. A new environment is created!
 570         if (strictFlag) {
 571             // Create a new scope object
 572             final ScriptObject strictEvalScope = global.newObject();
 573 
 574             // bless it as a "scope"
 575             strictEvalScope.setIsScope();
 576 
 577             // set given scope to be it's proto so that eval can still
 578             // access caller environment vars in the new environment.
 579             strictEvalScope.setProto(scope);
 580             scope = strictEvalScope;
 581         }
 582 
 583         ScriptFunction func = getRunScriptFunction(clazz, scope);
 584         Object evalThis;
 585         if (directEval) {
 586             evalThis = (callThis instanceof ScriptObject || strictFlag) ? callThis : global;
 587         } else {
 588             evalThis = global;
 589         }
 590 
 591         return ScriptRuntime.apply(func, evalThis);
 592     }
 593 
 594     private static Source loadInternal(final String srcStr, final String prefix, final String resourcePath) {
 595         if (srcStr.startsWith(prefix)) {
 596             final String resource = resourcePath + srcStr.substring(prefix.length());
 597             // NOTE: even sandbox scripts should be able to load scripts in nashorn: scheme
 598             // These scripts are always available and are loaded from nashorn.jar's resources.
 599             return AccessController.doPrivileged(
 600                     new PrivilegedAction<Source>() {
 601                         @Override
 602                         public Source run() {
 603                             try {
 604                                 final URL resURL = Context.class.getResource(resource);
 605                                 return (resURL != null)? sourceFor(srcStr, resURL) : null;
 606                             } catch (final IOException exp) {
 607                                 return null;
 608                             }
 609                         }
 610                     });
 611         }
 612 
 613         return null;
 614     }
 615 
 616     /**
 617      * Implementation of {@code load} Nashorn extension. Load a script file from a source
 618      * expression
 619      *
 620      * @param scope  the scope
 621      * @param from   source expression for script
 622      *
 623      * @return return value for load call (undefined)
 624      *
 625      * @throws IOException if source cannot be found or loaded
 626      */
 627     public Object load(final ScriptObject scope, final Object from) throws IOException {
 628         final Object src = (from instanceof ConsString)?  from.toString() : from;
 629         Source source = null;
 630 
 631         // load accepts a String (which could be a URL or a file name), a File, a URL
 632         // or a ScriptObject that has "name" and "source" (string valued) properties.
 633         if (src instanceof String) {
 634             final String srcStr = (String)src;
 635             if (srcStr.startsWith(LOAD_CLASSPATH)) {
 636                 URL url = getResourceURL(srcStr.substring(LOAD_CLASSPATH.length()));
 637                 source = (url != null)? sourceFor(url.toString(), url) : null;
 638             } else {
 639                 final File file = new File(srcStr);
 640                 if (srcStr.indexOf(':') != -1) {
 641                     if ((source = loadInternal(srcStr, LOAD_NASHORN, "resources/")) == null &&
 642                         (source = loadInternal(srcStr, LOAD_FX, "resources/fx/")) == null) {
 643                         URL url;
 644                         try {
 645                             //check for malformed url. if malformed, it may still be a valid file
 646                             url = new URL(srcStr);
 647                         } catch (final MalformedURLException e) {
 648                             url = file.toURI().toURL();
 649                         }
 650                         source = sourceFor(url.toString(), url);
 651                     }
 652                 } else if (file.isFile()) {
 653                     source = sourceFor(srcStr, file);
 654                 }
 655             }
 656         } else if (src instanceof File && ((File)src).isFile()) {
 657             final File file = (File)src;
 658             source = sourceFor(file.getName(), file);
 659         } else if (src instanceof URL) {
 660             final URL url = (URL)src;
 661             source = sourceFor(url.toString(), url);
 662         } else if (src instanceof ScriptObject) {
 663             final ScriptObject sobj = (ScriptObject)src;
 664             if (sobj.has("script") && sobj.has("name")) {
 665                 final String script = JSType.toString(sobj.get("script"));
 666                 final String name   = JSType.toString(sobj.get("name"));
 667                 source = sourceFor(name, script);
 668             }
 669         } else if (src instanceof Map) {
 670             final Map<?,?> map = (Map<?,?>)src;
 671             if (map.containsKey("script") && map.containsKey("name")) {
 672                 final String script = JSType.toString(map.get("script"));
 673                 final String name   = JSType.toString(map.get("name"));
 674                 source = sourceFor(name, script);
 675             }
 676         }
 677 
 678         if (source != null) {
 679             return evaluateSource(source, scope, scope);
 680         }
 681 
 682         throw typeError("cant.load.script", ScriptRuntime.safeToString(from));
 683     }
 684 
 685     /**
 686      * Implementation of {@code loadWithNewGlobal} Nashorn extension. Load a script file from a source
 687      * expression, after creating a new global scope.
 688      *
 689      * @param from source expression for script
 690      * @param args (optional) arguments to be passed to the loaded script
 691      *
 692      * @return return value for load call (undefined)
 693      *
 694      * @throws IOException if source cannot be found or loaded
 695      */
 696     public Object loadWithNewGlobal(final Object from, final Object...args) throws IOException {
 697         final Global oldGlobal = getGlobal();
 698         final Global newGlobal = AccessController.doPrivileged(new PrivilegedAction<Global>() {
 699            @Override
 700            public Global run() {
 701                try {
 702                    return newGlobal();
 703                } catch (final RuntimeException e) {
 704                    if (Context.DEBUG) {
 705                        e.printStackTrace();
 706                    }
 707                    throw e;
 708                }
 709            }
 710         }, CREATE_GLOBAL_ACC_CTXT);
 711         // initialize newly created Global instance
 712         initGlobal(newGlobal);
 713         setGlobal(newGlobal);
 714 
 715         final Object[] wrapped = args == null? ScriptRuntime.EMPTY_ARRAY :  ScriptObjectMirror.wrapArray(args, oldGlobal);
 716         newGlobal.put("arguments", newGlobal.wrapAsObject(wrapped), env._strict);
 717 
 718         try {
 719             // wrap objects from newGlobal's world as mirrors - but if result
 720             // is from oldGlobal's world, unwrap it!
 721             return ScriptObjectMirror.unwrap(ScriptObjectMirror.wrap(load(newGlobal, from), newGlobal), oldGlobal);
 722         } finally {
 723             setGlobal(oldGlobal);
 724         }
 725     }
 726 
 727     /**
 728      * Load or get a structure class. Structure class names are based on the number of parameter fields
 729      * and {@link AccessorProperty} fields in them. Structure classes are used to represent ScriptObjects
 730      *
 731      * @see ObjectClassGenerator
 732      * @see AccessorProperty
 733      * @see ScriptObject
 734      *
 735      * @param fullName  full name of class, e.g. jdk.nashorn.internal.objects.JO2P1 contains 2 fields and 1 parameter.
 736      *
 737      * @return the {@code Class<?>} for this structure
 738      *
 739      * @throws ClassNotFoundException if structure class cannot be resolved
 740      */
 741     public static Class<?> forStructureClass(final String fullName) throws ClassNotFoundException {
 742         if (System.getSecurityManager() != null && !StructureLoader.isStructureClass(fullName)) {
 743             throw new ClassNotFoundException(fullName);
 744         }
 745         return Class.forName(fullName, true, sharedLoader);
 746     }
 747 
 748     /**
 749      * Checks that the given Class can be accessed from no permissions context.
 750      *
 751      * @param clazz Class object
 752      * @throws SecurityException if not accessible
 753      */
 754     public static void checkPackageAccess(final Class<?> clazz) {
 755         final SecurityManager sm = System.getSecurityManager();
 756         if (sm != null) {
 757             Class<?> bottomClazz = clazz;
 758             while (bottomClazz.isArray()) {
 759                 bottomClazz = bottomClazz.getComponentType();
 760             }
 761             checkPackageAccess(sm, bottomClazz.getName());
 762         }
 763     }
 764 
 765     /**
 766      * Checks that the given package name can be accessed from no permissions context.
 767      *
 768      * @param pkgName package name
 769      * @throws SecurityException if not accessible
 770      */
 771     public static void checkPackageAccess(final String pkgName) {
 772         final SecurityManager sm = System.getSecurityManager();
 773         if (sm != null) {
 774             checkPackageAccess(sm, pkgName.endsWith(".") ? pkgName : pkgName + ".");
 775         }
 776     }
 777 
 778     /**
 779      * Checks that the given package can be accessed from no permissions context.
 780      *
 781      * @param sm current security manager instance
 782      * @param fullName fully qualified package name
 783      * @throw SecurityException if not accessible
 784      */
 785     private static void checkPackageAccess(final SecurityManager sm, final String fullName) {
 786         sm.getClass(); // null check
 787         final int index = fullName.lastIndexOf('.');
 788         if (index != -1) {
 789             final String pkgName = fullName.substring(0, index);
 790             AccessController.doPrivileged(new PrivilegedAction<Void>() {
 791                 @Override
 792                 public Void run() {
 793                     sm.checkPackageAccess(pkgName);
 794                     return null;
 795                 }
 796             }, NO_PERMISSIONS_ACC_CTXT);
 797         }
 798     }
 799 
 800     /**
 801      * Checks that the given Class can be accessed from no permissions context.
 802      *
 803      * @param clazz Class object
 804      * @return true if package is accessible, false otherwise
 805      */
 806     private static boolean isAccessiblePackage(final Class<?> clazz) {
 807         try {
 808             checkPackageAccess(clazz);
 809             return true;
 810         } catch (final SecurityException se) {
 811             return false;
 812         }
 813     }
 814 
 815     /**
 816      * Checks that the given Class is public and it can be accessed from no permissions context.
 817      *
 818      * @param clazz Class object to check
 819      * @return true if Class is accessible, false otherwise
 820      */
 821     public static boolean isAccessibleClass(final Class<?> clazz) {
 822         return Modifier.isPublic(clazz.getModifiers()) && Context.isAccessiblePackage(clazz);
 823     }
 824 
 825     /**
 826      * Lookup a Java class. This is used for JSR-223 stuff linking in from
 827      * {@code jdk.nashorn.internal.objects.NativeJava} and {@code jdk.nashorn.internal.runtime.NativeJavaPackage}
 828      *
 829      * @param fullName full name of class to load
 830      *
 831      * @return the {@code Class<?>} for the name
 832      *
 833      * @throws ClassNotFoundException if class cannot be resolved
 834      */
 835     public Class<?> findClass(final String fullName) throws ClassNotFoundException {
 836         if (fullName.indexOf('[') != -1 || fullName.indexOf('/') != -1) {
 837             // don't allow array class names or internal names.
 838             throw new ClassNotFoundException(fullName);
 839         }
 840 
 841         // check package access as soon as possible!
 842         final SecurityManager sm = System.getSecurityManager();
 843         if (sm != null) {
 844             checkPackageAccess(sm, fullName);
 845         }
 846 
 847         // try the script -classpath loader, if that is set
 848         if (classPathLoader != null) {
 849             try {
 850                 return Class.forName(fullName, true, classPathLoader);
 851             } catch (final ClassNotFoundException ignored) {
 852                 // ignore, continue search
 853             }
 854         }
 855 
 856         // Try finding using the "app" loader.
 857         return Class.forName(fullName, true, appLoader);
 858     }
 859 
 860     /**
 861      * Hook to print stack trace for a {@link Throwable} that occurred during
 862      * execution
 863      *
 864      * @param t throwable for which to dump stack
 865      */
 866     public static void printStackTrace(final Throwable t) {
 867         if (Context.DEBUG) {
 868             t.printStackTrace(Context.getCurrentErr());
 869         }
 870     }
 871 
 872     /**
 873      * Verify generated bytecode before emission. This is called back from the
 874      * {@link ObjectClassGenerator} or the {@link Compiler}. If the "--verify-code" parameter
 875      * hasn't been given, this is a nop
 876      *
 877      * Note that verification may load classes -- we don't want to do that unless
 878      * user specified verify option. We check it here even though caller
 879      * may have already checked that flag
 880      *
 881      * @param bytecode bytecode to verify
 882      */
 883     public void verify(final byte[] bytecode) {
 884         if (env._verify_code) {
 885             // No verification when security manager is around as verifier
 886             // may load further classes - which should be avoided.
 887             if (System.getSecurityManager() == null) {
 888                 CheckClassAdapter.verify(new ClassReader(bytecode), sharedLoader, false, new PrintWriter(System.err, true));
 889             }
 890         }
 891     }
 892 
 893     /**
 894      * Create and initialize a new global scope object.
 895      *
 896      * @return the initialized global scope object.
 897      */
 898     public Global createGlobal() {
 899         return initGlobal(newGlobal());
 900     }
 901 
 902     /**
 903      * Create a new uninitialized global scope object
 904      * @return the global script object
 905      */
 906     public Global newGlobal() {
 907         return new Global(this);
 908     }
 909 
 910     /**
 911      * Initialize given global scope object.
 912      *
 913      * @param global the global
 914      * @return the initialized global scope object.
 915      */
 916     public Global initGlobal(final Global global) {
 917         // Need only minimal global object, if we are just compiling.
 918         if (!env._compile_only) {
 919             final Global oldGlobal = Context.getGlobal();
 920             try {
 921                 Context.setGlobal(global);
 922                 // initialize global scope with builtin global objects
 923                 global.initBuiltinObjects();
 924             } finally {
 925                 Context.setGlobal(oldGlobal);
 926             }
 927         }
 928 
 929         return global;
 930     }
 931 
 932     /**
 933      * Trusted variant - package-private
 934      */
 935 
 936     /**
 937      * Return the current global's context
 938      * @return current global's context
 939      */
 940     static Context getContextTrusted() {
 941         return ((ScriptObject)Context.getGlobal()).getContext();
 942     }
 943 
 944     /**
 945      * Try to infer Context instance from the Class. If we cannot,
 946      * then get it from the thread local variable.
 947      *
 948      * @param clazz the class
 949      * @return context
 950      */
 951     static Context fromClass(final Class<?> clazz) {
 952         final ClassLoader loader = clazz.getClassLoader();
 953 
 954         if (loader instanceof ScriptLoader) {
 955             return ((ScriptLoader)loader).getContext();
 956         }
 957 
 958         return Context.getContextTrusted();
 959     }
 960 
 961     private URL getResourceURL(final String resName) {
 962         // try the classPathLoader if we have and then
 963         // try the appLoader if non-null.
 964         if (classPathLoader != null) {
 965             return classPathLoader.getResource(resName);
 966         } else if (appLoader != null) {
 967             return appLoader.getResource(resName);
 968         }
 969 
 970         return null;
 971     }
 972 
 973     private Object evaluateSource(final Source source, final ScriptObject scope, final ScriptObject thiz) {
 974         ScriptFunction script = null;
 975 
 976         try {
 977             script = compileScript(source, scope, new Context.ThrowErrorManager());
 978         } catch (final ParserException e) {
 979             e.throwAsEcmaException();
 980         }
 981 
 982         return ScriptRuntime.apply(script, thiz);
 983     }
 984 
 985     private static MethodHandle getRunScriptHandle(final Class<?> script) {
 986         return MH.findStatic(
 987                     MethodHandles.lookup(),
 988                     script,
 989                     RUN_SCRIPT.symbolName(),
 990                     MH.type(
 991                         Object.class,
 992                         ScriptFunction.class,
 993                         Object.class));
 994     }
 995     
 996     private static boolean isStrict(final Class<?> script) {
 997         try {
 998             return script.getField(STRICT_MODE.symbolName()).getBoolean(null);
 999         } catch (final NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException e) {
1000             return false;
1001         }
1002     }
1003     
1004     private static ScriptFunction getRunScriptFunction(final Class<?> script, final ScriptObject scope) {
1005         if (script == null) {
1006             return null;
1007         }
1008 
1009         // Get run method - the entry point to the script
1010         final MethodHandle runMethodHandle = getRunScriptHandle(script);
1011         boolean strict = isStrict(script);
1012 
1013         // Package as a JavaScript function and pass function back to shell.
1014         return Context.getGlobal().newScriptFunction(RUN_SCRIPT.symbolName(), runMethodHandle, scope, strict);
1015     }
1016 
1017     private ScriptFunction compileScript(final Source source, final ScriptObject scope, final ErrorManager errMan) {
1018         return getRunScriptFunction(compile(source, errMan, this._strict), scope);
1019     }
1020 
1021     private synchronized Class<?> compile(final Source source, final ErrorManager errMan, final boolean strict) {
1022         // start with no errors, no warnings.
1023         errMan.reset();
1024 
1025         Class<?> script = findCachedClass(source);
1026         if (script != null) {
1027             Compiler.LOG.fine("Code cache hit for ", source, " avoiding recompile.");
1028             return script;
1029         }
1030 
1031         CompiledScript compiledScript = null;
1032         FunctionNode functionNode = null;
1033 
1034         if (!env._parse_only && codeStore != null) {
1035             try {
1036                 compiledScript = codeStore.getScript(source);
1037             } catch (IOException | ClassNotFoundException e) {
1038                 Compiler.LOG.warning("Error loading ", source, " from cache: ", e);
1039                 // Fall back to normal compilation
1040             }
1041         }
1042 
1043         if (compiledScript == null) {
1044             functionNode = new Parser(env, source, errMan, strict).parse();
1045 
1046             if (errors.hasErrors()) {
1047                 return null;
1048             }
1049 
1050             if (env._print_ast) {
1051                 getErr().println(new ASTWriter(functionNode));
1052             }
1053 
1054             if (env._print_parse) {
1055                 getErr().println(new PrintVisitor(functionNode));
1056             }
1057         }
1058 
1059         if (env._parse_only) {
1060             return null;
1061         }
1062 
1063         final URL          url    = source.getURL();
1064         final ScriptLoader loader = env._loader_per_compile ? createNewLoader() : scriptLoader;
1065         final CodeSource   cs     = new CodeSource(url, (CodeSigner[])null);
1066         final CodeInstaller<ScriptEnvironment> installer = new ContextCodeInstaller(this, loader, cs);
1067 
1068         if (functionNode != null) {
1069             final Compiler compiler = new Compiler(installer, strict);
1070             final FunctionNode newFunctionNode = compiler.compile(functionNode);
1071             script = compiler.install(newFunctionNode);
1072         } else {
1073             script = install(compiledScript, installer);
1074         }
1075 
1076         cacheClass(source, script);
1077         return script;
1078     }
1079 
1080     private ScriptLoader createNewLoader() {
1081         return AccessController.doPrivileged(
1082              new PrivilegedAction<ScriptLoader>() {
1083                 @Override
1084                 public ScriptLoader run() {
1085                     return new ScriptLoader(appLoader, Context.this);
1086                 }
1087              }, CREATE_LOADER_ACC_CTXT);
1088     }
1089 
1090     private long getUniqueEvalId() {
1091         return uniqueEvalId.getAndIncrement();
1092     }
1093 
1094     private long getUniqueScriptId() {
1095         return uniqueScriptId.getAndIncrement();
1096     }
1097 
1098 
1099     /**
1100      * Install a previously compiled class from the code cache.
1101      *
1102      * @param compiledScript cached script containing class bytes and constants
1103      * @return main script class
1104      */
1105     private Class<?> install(final CompiledScript compiledScript, final CodeInstaller<ScriptEnvironment> installer) {
1106 
1107         final Map<String, Class<?>> installedClasses = new HashMap<>();
1108         final Source   source        = compiledScript.getSource();
1109         final Object[] constants     = compiledScript.getConstants();
1110         final String   rootClassName = compiledScript.getMainClassName();
1111         final byte[]   rootByteCode  = compiledScript.getClassBytes().get(rootClassName);
1112         final Class<?> rootClass     = installer.install(rootClassName, rootByteCode, source, constants);
1113 
1114         installedClasses.put(rootClassName, rootClass);
1115 
1116         for (final Map.Entry<String, byte[]> entry : compiledScript.getClassBytes().entrySet()) {
1117             final String className = entry.getKey();
1118             if (className.equals(rootClassName)) {
1119                 continue;
1120             }
1121             final byte[] code = entry.getValue();
1122 
1123             installedClasses.put(className, installer.install(className, code, source, constants));
1124         }
1125         for (Object constant : constants) {
1126             if (constant instanceof RecompilableScriptFunctionData) {
1127                 ((RecompilableScriptFunctionData) constant).setCodeAndSource(installedClasses, source);
1128             }
1129         }
1130 
1131         return rootClass;
1132     }
1133 
1134     /**
1135      * Cache for compiled script classes.
1136      */
1137     @SuppressWarnings("serial")
1138     private static class ClassCache extends LinkedHashMap<Source, ClassReference> {
1139         private final int size;
1140         private final ReferenceQueue<Class<?>> queue;
1141 
1142         ClassCache(int size) {
1143             super(size, 0.75f, true);
1144             this.size = size;
1145             this.queue = new ReferenceQueue<>();
1146         }
1147 
1148         void cache(final Source source, final Class<?> clazz) {
1149             put(source, new ClassReference(clazz, queue, source));
1150         }
1151 
1152         @Override
1153         protected boolean removeEldestEntry(final Map.Entry<Source, ClassReference> eldest) {
1154             return size() > size;
1155         }
1156 
1157         @Override
1158         public ClassReference get(Object key) {
1159             for (ClassReference ref; (ref = (ClassReference)queue.poll()) != null; ) {
1160                 remove(ref.source);
1161             }
1162             return super.get(key);
1163         }
1164 
1165     }
1166 
1167     private static class ClassReference extends SoftReference<Class<?>> {
1168         private final Source source;
1169 
1170         ClassReference(final Class<?> clazz, final ReferenceQueue<Class<?>> queue, final Source source) {
1171             super(clazz, queue);
1172             this.source = source;
1173         }
1174     }
1175 
1176     // Class cache management
1177     private Class<?> findCachedClass(final Source source) {
1178         ClassReference ref = classCache == null ? null : classCache.get(source);
1179         return ref != null ? ref.get() : null;
1180     }
1181 
1182     private void cacheClass(final Source source, final Class<?> clazz) {
1183         if (classCache != null) {
1184             classCache.cache(source, clazz);
1185         }
1186     }
1187 
1188 
1189 }