Context.java revision 1709:39dececd7338
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
26package jdk.nashorn.internal.runtime;
27
28import static jdk.internal.org.objectweb.asm.Opcodes.V1_7;
29import static jdk.nashorn.internal.codegen.CompilerConstants.CONSTANTS;
30import static jdk.nashorn.internal.codegen.CompilerConstants.CREATE_PROGRAM_FUNCTION;
31import static jdk.nashorn.internal.codegen.CompilerConstants.SOURCE;
32import static jdk.nashorn.internal.codegen.CompilerConstants.STRICT_MODE;
33import static jdk.nashorn.internal.runtime.CodeStore.newCodeStore;
34import static jdk.nashorn.internal.runtime.ECMAErrors.typeError;
35import static jdk.nashorn.internal.runtime.ScriptRuntime.UNDEFINED;
36import static jdk.nashorn.internal.runtime.Source.sourceFor;
37
38import java.io.File;
39import java.io.InputStream;
40import java.io.IOException;
41import java.io.PrintWriter;
42import java.io.UncheckedIOException;
43import java.lang.invoke.MethodHandle;
44import java.lang.invoke.MethodHandles;
45import java.lang.invoke.MethodType;
46import java.lang.invoke.SwitchPoint;
47import java.lang.ref.ReferenceQueue;
48import java.lang.ref.SoftReference;
49import java.lang.module.Configuration;
50import java.lang.module.ModuleDescriptor;
51import java.lang.module.ModuleFinder;
52import java.lang.module.ModuleReference;
53import java.lang.reflect.Field;
54import java.lang.reflect.Layer;
55import java.lang.reflect.Modifier;
56import java.lang.reflect.Module;
57import java.net.MalformedURLException;
58import java.net.URL;
59import java.security.AccessControlContext;
60import java.security.AccessController;
61import java.security.CodeSigner;
62import java.security.CodeSource;
63import java.security.Permissions;
64import java.security.PrivilegedAction;
65import java.security.PrivilegedActionException;
66import java.security.PrivilegedExceptionAction;
67import java.security.ProtectionDomain;
68import java.util.Collection;
69import java.util.HashMap;
70import java.util.HashSet;
71import java.util.LinkedHashMap;
72import java.util.Map;
73import java.util.Objects;
74import java.util.Optional;
75import java.util.Set;
76import java.util.concurrent.ConcurrentHashMap;
77import java.util.concurrent.ConcurrentMap;
78import java.util.concurrent.atomic.AtomicLong;
79import java.util.concurrent.atomic.AtomicReference;
80import java.util.concurrent.atomic.LongAdder;
81import java.util.function.Consumer;
82import java.util.function.Supplier;
83import java.util.logging.Level;
84import javax.script.ScriptEngine;
85import jdk.dynalink.DynamicLinker;
86import jdk.internal.org.objectweb.asm.ClassReader;
87import jdk.internal.org.objectweb.asm.ClassWriter;
88import jdk.internal.org.objectweb.asm.Opcodes;
89import jdk.internal.org.objectweb.asm.util.CheckClassAdapter;
90import jdk.nashorn.api.scripting.ClassFilter;
91import jdk.nashorn.api.scripting.ScriptObjectMirror;
92import jdk.nashorn.internal.WeakValueCache;
93import jdk.nashorn.internal.codegen.Compiler;
94import jdk.nashorn.internal.codegen.Compiler.CompilationPhases;
95import jdk.nashorn.internal.codegen.ObjectClassGenerator;
96import jdk.nashorn.internal.ir.FunctionNode;
97import jdk.nashorn.internal.ir.debug.ASTWriter;
98import jdk.nashorn.internal.ir.debug.PrintVisitor;
99import jdk.nashorn.internal.lookup.MethodHandleFactory;
100import jdk.nashorn.internal.objects.Global;
101import jdk.nashorn.internal.parser.Parser;
102import jdk.nashorn.internal.runtime.events.RuntimeEvent;
103import jdk.nashorn.internal.runtime.linker.Bootstrap;
104import jdk.nashorn.internal.runtime.logging.DebugLogger;
105import jdk.nashorn.internal.runtime.logging.Loggable;
106import jdk.nashorn.internal.runtime.logging.Logger;
107import jdk.nashorn.internal.runtime.options.LoggingOption.LoggerInfo;
108import jdk.nashorn.internal.runtime.options.Options;
109import jdk.internal.misc.Unsafe;
110
111/**
112 * This class manages the global state of execution. Context is immutable.
113 */
114public final class Context {
115    // nashorn specific security runtime access permission names
116    /**
117     * Permission needed to pass arbitrary nashorn command line options when creating Context.
118     */
119    public static final String NASHORN_SET_CONFIG      = "nashorn.setConfig";
120
121    /**
122     * Permission needed to create Nashorn Context instance.
123     */
124    public static final String NASHORN_CREATE_CONTEXT  = "nashorn.createContext";
125
126    /**
127     * Permission needed to create Nashorn Global instance.
128     */
129    public static final String NASHORN_CREATE_GLOBAL   = "nashorn.createGlobal";
130
131    /**
132     * Permission to get current Nashorn Context from thread local storage.
133     */
134    public static final String NASHORN_GET_CONTEXT     = "nashorn.getContext";
135
136    /**
137     * Permission to use Java reflection/jsr292 from script code.
138     */
139    public static final String NASHORN_JAVA_REFLECTION = "nashorn.JavaReflection";
140
141    /**
142     * Permission to create a new Module
143     */
144    public static final String NASHORN_CREATE_MODULE = "nashorn.createModule";
145
146    /**
147     * Permission to enable nashorn debug mode.
148     */
149    public static final String NASHORN_DEBUG_MODE = "nashorn.debugMode";
150
151    // nashorn load psuedo URL prefixes
152    private static final String LOAD_CLASSPATH = "classpath:";
153    private static final String LOAD_FX = "fx:";
154    private static final String LOAD_NASHORN = "nashorn:";
155
156    private static final MethodHandles.Lookup LOOKUP = MethodHandles.lookup();
157    private static final MethodType CREATE_PROGRAM_FUNCTION_TYPE = MethodType.methodType(ScriptFunction.class, ScriptObject.class);
158
159    private static final LongAdder NAMED_INSTALLED_SCRIPT_COUNT = new LongAdder();
160    private static final LongAdder ANONYMOUS_INSTALLED_SCRIPT_COUNT = new LongAdder();
161
162    /**
163     * Should scripts use only object slots for fields, or dual long/object slots? The default
164     * behaviour is to couple this to optimistic types, using dual representation if optimistic types are enabled
165     * and single field representation otherwise. This can be overridden by setting either the "nashorn.fields.objects"
166     * or "nashorn.fields.dual" system property.
167     */
168    private final FieldMode fieldMode;
169
170    private static enum FieldMode {
171        /** Value for automatic field representation depending on optimistic types setting */
172        AUTO,
173        /** Value for object field representation regardless of optimistic types setting */
174        OBJECTS,
175        /** Value for dual primitive/object field representation regardless of optimistic types setting */
176        DUAL
177    }
178
179    /**
180     * Keeps track of which builtin prototypes and properties have been relinked
181     * Currently we are conservative and associate the name of a builtin class with all
182     * its properties, so it's enough to invalidate a property to break all assumptions
183     * about a prototype. This can be changed to a more fine grained approach, but no one
184     * ever needs this, given the very rare occurrence of swapping out only parts of
185     * a builtin v.s. the entire builtin object
186     */
187    private final Map<String, SwitchPoint> builtinSwitchPoints = new HashMap<>();
188
189    /* Force DebuggerSupport to be loaded. */
190    static {
191        DebuggerSupport.FORCELOAD = true;
192    }
193
194    static long getNamedInstalledScriptCount() {
195        return NAMED_INSTALLED_SCRIPT_COUNT.sum();
196    }
197
198    static long getAnonymousInstalledScriptCount() {
199        return ANONYMOUS_INSTALLED_SCRIPT_COUNT.sum();
200    }
201
202    /**
203     * ContextCodeInstaller that has the privilege of installing classes in the Context.
204     * Can only be instantiated from inside the context and is opaque to other classes
205     */
206    private abstract static class ContextCodeInstaller implements CodeInstaller {
207        final Context context;
208        final CodeSource codeSource;
209
210        ContextCodeInstaller(final Context context, final CodeSource codeSource) {
211            this.context = context;
212            this.codeSource = codeSource;
213        }
214
215        @Override
216        public Context getContext() {
217            return context;
218        }
219
220        @Override
221        public void initialize(final Collection<Class<?>> classes, final Source source, final Object[] constants) {
222            try {
223                AccessController.doPrivileged(new PrivilegedExceptionAction<Void>() {
224                    @Override
225                    public Void run() throws Exception {
226                        for (final Class<?> clazz : classes) {
227                            //use reflection to write source and constants table to installed classes
228                            final Field sourceField = clazz.getDeclaredField(SOURCE.symbolName());
229                            sourceField.setAccessible(true);
230                            sourceField.set(null, source);
231
232                            final Field constantsField = clazz.getDeclaredField(CONSTANTS.symbolName());
233                            constantsField.setAccessible(true);
234                            constantsField.set(null, constants);
235                        }
236                        return null;
237                    }
238                });
239            } catch (final PrivilegedActionException e) {
240                throw new RuntimeException(e);
241            }
242        }
243
244        @Override
245        public void verify(final byte[] code) {
246            context.verify(code);
247        }
248
249        @Override
250        public long getUniqueScriptId() {
251            return context.getUniqueScriptId();
252        }
253
254        @Override
255        public void storeScript(final String cacheKey, final Source source, final String mainClassName,
256                                final Map<String,byte[]> classBytes, final Map<Integer, FunctionInitializer> initializers,
257                                final Object[] constants, final int compilationId) {
258            if (context.codeStore != null) {
259                context.codeStore.store(cacheKey, source, mainClassName, classBytes, initializers, constants, compilationId);
260            }
261        }
262
263        @Override
264        public StoredScript loadScript(final Source source, final String functionKey) {
265            if (context.codeStore != null) {
266                return context.codeStore.load(source, functionKey);
267            }
268            return null;
269        }
270
271        @Override
272        public boolean isCompatibleWith(final CodeInstaller other) {
273            if (other instanceof ContextCodeInstaller) {
274                final ContextCodeInstaller cci = (ContextCodeInstaller)other;
275                return cci.context == context && cci.codeSource == codeSource;
276            }
277            return false;
278        }
279    }
280
281    private static class NamedContextCodeInstaller extends ContextCodeInstaller {
282        private final ScriptLoader loader;
283        private int usageCount = 0;
284        private int bytesDefined = 0;
285
286        // We reuse this installer for 10 compilations or 200000 defined bytes. Usually the first condition
287        // will occur much earlier, the second is a safety measure for very large scripts/functions.
288        private final static int MAX_USAGES = 10;
289        private final static int MAX_BYTES_DEFINED = 200_000;
290
291        private NamedContextCodeInstaller(final Context context, final CodeSource codeSource, final ScriptLoader loader) {
292            super(context, codeSource);
293            this.loader = loader;
294        }
295
296        @Override
297        public Class<?> install(final String className, final byte[] bytecode) {
298            usageCount++;
299            bytesDefined += bytecode.length;
300            NAMED_INSTALLED_SCRIPT_COUNT.increment();
301            return loader.installClass(Compiler.binaryName(className), bytecode, codeSource);
302        }
303
304        @Override
305        public CodeInstaller getOnDemandCompilationInstaller() {
306            // Reuse this installer if we're within our limits.
307            if (usageCount < MAX_USAGES && bytesDefined < MAX_BYTES_DEFINED) {
308                return this;
309            }
310            return new NamedContextCodeInstaller(context, codeSource, context.createNewLoader());
311        }
312
313        @Override
314        public CodeInstaller getMultiClassCodeInstaller() {
315            // This installer is perfectly suitable for installing multiple classes that reference each other
316            // as it produces classes with resolvable names, all defined in a single class loader.
317            return this;
318        }
319    }
320
321    private final WeakValueCache<CodeSource, Class<?>> anonymousHostClasses = new WeakValueCache<>();
322
323    private static final class AnonymousContextCodeInstaller extends ContextCodeInstaller {
324        private static final Unsafe UNSAFE = getUnsafe();
325        private static final String ANONYMOUS_HOST_CLASS_NAME = Compiler.SCRIPTS_PACKAGE.replace('/', '.') + ".AnonymousHost";
326        private static final byte[] ANONYMOUS_HOST_CLASS_BYTES = getAnonymousHostClassBytes();
327
328        private final Class<?> hostClass;
329
330        private AnonymousContextCodeInstaller(final Context context, final CodeSource codeSource, final Class<?> hostClass) {
331            super(context, codeSource);
332            this.hostClass = hostClass;
333        }
334
335        @Override
336        public Class<?> install(final String className, final byte[] bytecode) {
337            ANONYMOUS_INSTALLED_SCRIPT_COUNT.increment();
338            return UNSAFE.defineAnonymousClass(hostClass, bytecode, null);
339        }
340
341        @Override
342        public CodeInstaller getOnDemandCompilationInstaller() {
343            // This code loader can be indefinitely reused for on-demand recompilations for the same code source.
344            return this;
345        }
346
347        @Override
348        public CodeInstaller getMultiClassCodeInstaller() {
349            // This code loader can not be used to install multiple classes that reference each other, as they
350            // would have no resolvable names. Therefore, in such situation we must revert to an installer that
351            // produces named classes.
352            return new NamedContextCodeInstaller(context, codeSource, context.createNewLoader());
353        }
354
355        private static byte[] getAnonymousHostClassBytes() {
356            final ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_FRAMES | ClassWriter.COMPUTE_MAXS);
357            cw.visit(V1_7, Opcodes.ACC_INTERFACE | Opcodes.ACC_ABSTRACT, ANONYMOUS_HOST_CLASS_NAME.replace('.', '/'), null, "java/lang/Object", null);
358            cw.visitEnd();
359            return cw.toByteArray();
360        }
361
362        private static Unsafe getUnsafe() {
363            return AccessController.doPrivileged(new PrivilegedAction<Unsafe>() {
364                @Override
365                public Unsafe run() {
366                    try {
367                        final Field theUnsafeField = Unsafe.class.getDeclaredField("theUnsafe");
368                        theUnsafeField.setAccessible(true);
369                        return (Unsafe)theUnsafeField.get(null);
370                    } catch (final ReflectiveOperationException e) {
371                        throw new RuntimeException(e);
372                    }
373                }
374            });
375        }
376    }
377
378    /** Is Context global debug mode enabled ? */
379    public static final boolean DEBUG = Options.getBooleanProperty("nashorn.debug");
380
381    private static final ThreadLocal<Global> currentGlobal = new ThreadLocal<>();
382
383    // in-memory cache for loaded classes
384    private ClassCache classCache;
385
386    // persistent code store
387    private CodeStore codeStore;
388
389    // A factory for linking global properties as constant method handles. It is created when the first Global
390    // is created, and invalidated forever once the second global is created.
391    private final AtomicReference<GlobalConstants> globalConstantsRef = new AtomicReference<>();
392
393    /**
394     * Get the current global scope
395     * @return the current global scope
396     */
397    public static Global getGlobal() {
398        // This class in a package.access protected package.
399        // Trusted code only can call this method.
400        return currentGlobal.get();
401    }
402
403    /**
404     * Set the current global scope
405     * @param global the global scope
406     */
407    public static void setGlobal(final ScriptObject global) {
408        if (global != null && !(global instanceof Global)) {
409            throw new IllegalArgumentException("not a global!");
410        }
411        setGlobal((Global)global);
412    }
413
414    /**
415     * Set the current global scope
416     * @param global the global scope
417     */
418    public static void setGlobal(final Global global) {
419        // This class in a package.access protected package.
420        // Trusted code only can call this method.
421        assert getGlobal() != global;
422        //same code can be cached between globals, then we need to invalidate method handle constants
423        if (global != null) {
424            final GlobalConstants globalConstants = getContext(global).getGlobalConstants();
425            if (globalConstants != null) {
426                globalConstants.invalidateAll();
427            }
428        }
429        currentGlobal.set(global);
430    }
431
432    /**
433     * Get context of the current global
434     * @return current global scope's context.
435     */
436    public static Context getContext() {
437        final SecurityManager sm = System.getSecurityManager();
438        if (sm != null) {
439            sm.checkPermission(new RuntimePermission(NASHORN_GET_CONTEXT));
440        }
441        return getContextTrusted();
442    }
443
444    /**
445     * Get current context's error writer
446     *
447     * @return error writer of the current context
448     */
449    public static PrintWriter getCurrentErr() {
450        final ScriptObject global = getGlobal();
451        return (global != null)? global.getContext().getErr() : new PrintWriter(System.err);
452    }
453
454    /**
455     * Output text to this Context's error stream
456     * @param str text to write
457     */
458    public static void err(final String str) {
459        err(str, true);
460    }
461
462    /**
463     * Output text to this Context's error stream, optionally with
464     * a newline afterwards
465     *
466     * @param str  text to write
467     * @param crlf write a carriage return/new line after text
468     */
469    public static void err(final String str, final boolean crlf) {
470        final PrintWriter err = Context.getCurrentErr();
471        if (err != null) {
472            if (crlf) {
473                err.println(str);
474            } else {
475                err.print(str);
476            }
477        }
478    }
479
480    /** Current environment. */
481    private final ScriptEnvironment env;
482
483    /** is this context in strict mode? Cached from env. as this is used heavily. */
484    final boolean _strict;
485
486    /** class loader to resolve classes from script. */
487    private final ClassLoader appLoader;
488
489    /** Class loader to load classes compiled from scripts. */
490    private final ScriptLoader scriptLoader;
491
492    /** Dynamic linker for linking call sites in script code loaded by this context */
493    private final DynamicLinker dynamicLinker;
494
495    /** Current error manager. */
496    private final ErrorManager errors;
497
498    /** Unique id for script. Used only when --loader-per-compile=false */
499    private final AtomicLong uniqueScriptId;
500
501    /** Optional class filter to use for Java classes. Can be null. */
502    private final ClassFilter classFilter;
503
504    private static final StructureLoader sharedLoader;
505    private static final ConcurrentMap<String, Class<?>> structureClasses = new ConcurrentHashMap<>();
506
507    /*package-private*/ @SuppressWarnings("static-method")
508    StructureLoader getSharedLoader() {
509        return sharedLoader;
510    }
511
512    private static AccessControlContext createNoPermAccCtxt() {
513        return new AccessControlContext(new ProtectionDomain[] { new ProtectionDomain(null, new Permissions()) });
514    }
515
516    private static AccessControlContext createPermAccCtxt(final String permName) {
517        final Permissions perms = new Permissions();
518        perms.add(new RuntimePermission(permName));
519        return new AccessControlContext(new ProtectionDomain[] { new ProtectionDomain(null, perms) });
520    }
521
522    private static final AccessControlContext NO_PERMISSIONS_ACC_CTXT = createNoPermAccCtxt();
523    private static final AccessControlContext CREATE_LOADER_ACC_CTXT  = createPermAccCtxt("createClassLoader");
524    private static final AccessControlContext CREATE_GLOBAL_ACC_CTXT  = createPermAccCtxt(NASHORN_CREATE_GLOBAL);
525    private static final AccessControlContext GET_LOADER_ACC_CTXT     = createPermAccCtxt("getClassLoader");
526
527    static {
528        final ClassLoader myLoader = Context.class.getClassLoader();
529        sharedLoader = AccessController.doPrivileged(new PrivilegedAction<StructureLoader>() {
530            @Override
531            public StructureLoader run() {
532                return new StructureLoader(myLoader);
533            }
534        }, CREATE_LOADER_ACC_CTXT);
535    }
536
537    /**
538     * ThrowErrorManager that throws ParserException upon error conditions.
539     */
540    public static class ThrowErrorManager extends ErrorManager {
541        @Override
542        public void error(final String message) {
543            throw new ParserException(message);
544        }
545
546        @Override
547        public void error(final ParserException e) {
548            throw e;
549        }
550    }
551
552    /**
553     * Constructor
554     *
555     * @param options options from command line or Context creator
556     * @param errors  error manger
557     * @param appLoader application class loader
558     */
559    public Context(final Options options, final ErrorManager errors, final ClassLoader appLoader) {
560        this(options, errors, appLoader, null);
561    }
562
563    /**
564     * Constructor
565     *
566     * @param options options from command line or Context creator
567     * @param errors  error manger
568     * @param appLoader application class loader
569     * @param classFilter class filter to use
570     */
571    public Context(final Options options, final ErrorManager errors, final ClassLoader appLoader, final ClassFilter classFilter) {
572        this(options, errors, new PrintWriter(System.out, true), new PrintWriter(System.err, true), appLoader, classFilter);
573    }
574
575    /**
576     * Constructor
577     *
578     * @param options options from command line or Context creator
579     * @param errors  error manger
580     * @param out     output writer for this Context
581     * @param err     error writer for this Context
582     * @param appLoader application class loader
583     */
584    public Context(final Options options, final ErrorManager errors, final PrintWriter out, final PrintWriter err, final ClassLoader appLoader) {
585        this(options, errors, out, err, appLoader, (ClassFilter)null);
586    }
587
588    /**
589     * Constructor
590     *
591     * @param options options from command line or Context creator
592     * @param errors  error manger
593     * @param out     output writer for this Context
594     * @param err     error writer for this Context
595     * @param appLoader application class loader
596     * @param classFilter class filter to use
597     */
598    public Context(final Options options, final ErrorManager errors, final PrintWriter out, final PrintWriter err, final ClassLoader appLoader, final ClassFilter classFilter) {
599        final SecurityManager sm = System.getSecurityManager();
600        if (sm != null) {
601            sm.checkPermission(new RuntimePermission(NASHORN_CREATE_CONTEXT));
602        }
603
604        this.classFilter = classFilter;
605        this.env       = new ScriptEnvironment(options, out, err);
606        this._strict   = env._strict;
607        if (env._loader_per_compile) {
608            this.scriptLoader = null;
609            this.uniqueScriptId = null;
610        } else {
611            this.scriptLoader = createNewLoader();
612            this.uniqueScriptId = new AtomicLong();
613        }
614        this.errors    = errors;
615
616        // if user passed -classpath option, make a URLClassLoader with that and
617        // the app loader as the parent.
618        final String classPath = options.getString("classpath");
619        if (!env._compile_only && classPath != null && !classPath.isEmpty()) {
620            // make sure that caller can create a class loader.
621            if (sm != null) {
622                sm.checkCreateClassLoader();
623            }
624            this.appLoader = NashornLoader.createClassLoader(classPath, appLoader);
625        } else {
626            this.appLoader = appLoader;
627        }
628        this.dynamicLinker = Bootstrap.createDynamicLinker(this.appLoader, env._unstable_relink_threshold);
629
630        final int cacheSize = env._class_cache_size;
631        if (cacheSize > 0) {
632            classCache = new ClassCache(this, cacheSize);
633        }
634
635        if (env._persistent_cache) {
636            codeStore = newCodeStore(this);
637        }
638
639        // print version info if asked.
640        if (env._version) {
641            getErr().println("nashorn " + Version.version());
642        }
643
644        if (env._fullversion) {
645            getErr().println("nashorn full version " + Version.fullVersion());
646        }
647
648        if (Options.getBooleanProperty("nashorn.fields.dual")) {
649            fieldMode = FieldMode.DUAL;
650        } else if (Options.getBooleanProperty("nashorn.fields.objects")) {
651            fieldMode = FieldMode.OBJECTS;
652        } else {
653            fieldMode = FieldMode.AUTO;
654        }
655
656        initLoggers();
657    }
658
659
660    /**
661     * Get the class filter for this context
662     * @return class filter
663     */
664    public ClassFilter getClassFilter() {
665        return classFilter;
666    }
667
668    /**
669     * Returns the factory for constant method handles for global properties. The returned factory can be
670     * invalidated if this Context has more than one Global.
671     * @return the factory for constant method handles for global properties.
672     */
673    GlobalConstants getGlobalConstants() {
674        return globalConstantsRef.get();
675    }
676
677    /**
678     * Get the error manager for this context
679     * @return error manger
680     */
681    public ErrorManager getErrorManager() {
682        return errors;
683    }
684
685    /**
686     * Get the script environment for this context
687     * @return script environment
688     */
689    public ScriptEnvironment getEnv() {
690        return env;
691    }
692
693    /**
694     * Get the output stream for this context
695     * @return output print writer
696     */
697    public PrintWriter getOut() {
698        return env.getOut();
699    }
700
701    /**
702     * Get the error stream for this context
703     * @return error print writer
704     */
705    public PrintWriter getErr() {
706        return env.getErr();
707    }
708
709    /**
710     * Should scripts compiled by this context use dual field representation?
711     * @return true if using dual fields, false for object-only fields
712     */
713    public boolean useDualFields() {
714        return fieldMode == FieldMode.DUAL || (fieldMode == FieldMode.AUTO && env._optimistic_types);
715    }
716
717    /**
718     * Get the PropertyMap of the current global scope
719     * @return the property map of the current global scope
720     */
721    public static PropertyMap getGlobalMap() {
722        return Context.getGlobal().getMap();
723    }
724
725    /**
726     * Compile a top level script.
727     *
728     * @param source the source
729     * @param scope  the scope
730     *
731     * @return top level function for script
732     */
733    public ScriptFunction compileScript(final Source source, final ScriptObject scope) {
734        return compileScript(source, scope, this.errors);
735    }
736
737    /**
738     * Interface to represent compiled code that can be re-used across many
739     * global scope instances
740     */
741    public static interface MultiGlobalCompiledScript {
742        /**
743         * Obtain script function object for a specific global scope object.
744         *
745         * @param newGlobal global scope for which function object is obtained
746         * @return script function for script level expressions
747         */
748        public ScriptFunction getFunction(final Global newGlobal);
749    }
750
751    /**
752     * Compile a top level script.
753     *
754     * @param source the script source
755     * @return reusable compiled script across many global scopes.
756     */
757    public MultiGlobalCompiledScript compileScript(final Source source) {
758        final Class<?> clazz = compile(source, this.errors, this._strict, false);
759        final MethodHandle createProgramFunctionHandle = getCreateProgramFunctionHandle(clazz);
760
761        return new MultiGlobalCompiledScript() {
762            @Override
763            public ScriptFunction getFunction(final Global newGlobal) {
764                return invokeCreateProgramFunctionHandle(createProgramFunctionHandle, newGlobal);
765            }
766        };
767    }
768
769    /**
770     * Entry point for {@code eval}
771     *
772     * @param initialScope The scope of this eval call
773     * @param string       Evaluated code as a String
774     * @param callThis     "this" to be passed to the evaluated code
775     * @param location     location of the eval call
776     * @return the return value of the {@code eval}
777     */
778    public Object eval(final ScriptObject initialScope, final String string,
779            final Object callThis, final Object location) {
780        return eval(initialScope, string, callThis, location, false, false);
781    }
782
783    /**
784     * Entry point for {@code eval}
785     *
786     * @param initialScope The scope of this eval call
787     * @param string       Evaluated code as a String
788     * @param callThis     "this" to be passed to the evaluated code
789     * @param location     location of the eval call
790     * @param strict       is this {@code eval} call from a strict mode code?
791     * @param evalCall     is this called from "eval" builtin?
792     *
793     * @return the return value of the {@code eval}
794     */
795    public Object eval(final ScriptObject initialScope, final String string,
796            final Object callThis, final Object location, final boolean strict, final boolean evalCall) {
797        final String  file       = location == UNDEFINED || location == null ? "<eval>" : location.toString();
798        final Source  source     = sourceFor(file, string, evalCall);
799        // is this direct 'eval' builtin call?
800        final boolean directEval = evalCall && (location != UNDEFINED);
801        final Global  global = Context.getGlobal();
802        ScriptObject scope = initialScope;
803
804        // ECMA section 10.1.1 point 2 says eval code is strict if it begins
805        // with "use strict" directive or eval direct call itself is made
806        // from from strict mode code. We are passed with caller's strict mode.
807        // Nashorn extension: any 'eval' is unconditionally strict when -strict is specified.
808        boolean strictFlag = strict || this._strict;
809
810        Class<?> clazz;
811        try {
812            clazz = compile(source, new ThrowErrorManager(), strictFlag, true);
813        } catch (final ParserException e) {
814            e.throwAsEcmaException(global);
815            return null;
816        }
817
818        if (!strictFlag) {
819            // We need to get strict mode flag from compiled class. This is
820            // because eval code may start with "use strict" directive.
821            try {
822                strictFlag = clazz.getField(STRICT_MODE.symbolName()).getBoolean(null);
823            } catch (final NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException e) {
824                //ignored
825                strictFlag = false;
826            }
827        }
828
829        // In strict mode, eval does not instantiate variables and functions
830        // in the caller's environment. A new environment is created!
831        if (strictFlag) {
832            // Create a new scope object with given scope as its prototype
833            scope = newScope(scope);
834        }
835
836        final ScriptFunction func = getProgramFunction(clazz, scope);
837        Object evalThis;
838        if (directEval) {
839            evalThis = (callThis != UNDEFINED && callThis != null) || strictFlag ? callThis : global;
840        } else {
841            // either indirect evalCall or non-eval (Function, engine.eval, ScriptObjectMirror.eval..)
842            evalThis = callThis;
843        }
844
845        return ScriptRuntime.apply(func, evalThis);
846    }
847
848    private static ScriptObject newScope(final ScriptObject callerScope) {
849        return new Scope(callerScope, PropertyMap.newMap(Scope.class));
850    }
851
852    private static Source loadInternal(final String srcStr, final String prefix, final String resourcePath) {
853        if (srcStr.startsWith(prefix)) {
854            final String resource = resourcePath + srcStr.substring(prefix.length());
855            // NOTE: even sandbox scripts should be able to load scripts in nashorn: scheme
856            // These scripts are always available and are loaded from nashorn.jar's resources.
857            return AccessController.doPrivileged(
858                    new PrivilegedAction<Source>() {
859                        @Override
860                        public Source run() {
861                            try {
862                                final InputStream resStream = Context.class.getResourceAsStream(resource);
863                                return resStream != null ? sourceFor(srcStr, Source.readFully(resStream)) : null;
864                            } catch (final IOException exp) {
865                                return null;
866                            }
867                        }
868                    });
869        }
870
871        return null;
872    }
873
874    /**
875     * Implementation of {@code load} Nashorn extension. Load a script file from a source
876     * expression
877     *
878     * @param scope  the scope
879     * @param from   source expression for script
880     *
881     * @return return value for load call (undefined)
882     *
883     * @throws IOException if source cannot be found or loaded
884     */
885    public Object load(final Object scope, final Object from) throws IOException {
886        final Object src = from instanceof ConsString ? from.toString() : from;
887        Source source = null;
888
889        // load accepts a String (which could be a URL or a file name), a File, a URL
890        // or a ScriptObject that has "name" and "source" (string valued) properties.
891        if (src instanceof String) {
892            final String srcStr = (String)src;
893            if (srcStr.startsWith(LOAD_CLASSPATH)) {
894                final URL url = getResourceURL(srcStr.substring(LOAD_CLASSPATH.length()));
895                source = url != null ? sourceFor(url.toString(), url) : null;
896            } else {
897                final File file = new File(srcStr);
898                if (srcStr.indexOf(':') != -1) {
899                    if ((source = loadInternal(srcStr, LOAD_NASHORN, "resources/")) == null &&
900                        (source = loadInternal(srcStr, LOAD_FX, "resources/fx/")) == null) {
901                        URL url;
902                        try {
903                            //check for malformed url. if malformed, it may still be a valid file
904                            url = new URL(srcStr);
905                        } catch (final MalformedURLException e) {
906                            url = file.toURI().toURL();
907                        }
908                        source = sourceFor(url.toString(), url);
909                    }
910                } else if (file.isFile()) {
911                    source = sourceFor(srcStr, file);
912                }
913            }
914        } else if (src instanceof File && ((File)src).isFile()) {
915            final File file = (File)src;
916            source = sourceFor(file.getName(), file);
917        } else if (src instanceof URL) {
918            final URL url = (URL)src;
919            source = sourceFor(url.toString(), url);
920        } else if (src instanceof ScriptObject) {
921            final ScriptObject sobj = (ScriptObject)src;
922            if (sobj.has("script") && sobj.has("name")) {
923                final String script = JSType.toString(sobj.get("script"));
924                final String name   = JSType.toString(sobj.get("name"));
925                source = sourceFor(name, script);
926            }
927        } else if (src instanceof Map) {
928            final Map<?,?> map = (Map<?,?>)src;
929            if (map.containsKey("script") && map.containsKey("name")) {
930                final String script = JSType.toString(map.get("script"));
931                final String name   = JSType.toString(map.get("name"));
932                source = sourceFor(name, script);
933            }
934        }
935
936        if (source != null) {
937            if (scope instanceof ScriptObject && ((ScriptObject)scope).isScope()) {
938                final ScriptObject sobj = (ScriptObject)scope;
939                // passed object is a script object
940                // Global is the only user accessible scope ScriptObject
941                assert sobj.isGlobal() : "non-Global scope object!!";
942                return evaluateSource(source, sobj, sobj);
943            } else if (scope == null || scope == UNDEFINED) {
944                // undefined or null scope. Use current global instance.
945                final Global global = getGlobal();
946                return evaluateSource(source, global, global);
947            } else {
948                /*
949                 * Arbitrary object passed for scope.
950                 * Indirect load that is equivalent to:
951                 *
952                 *    (function(scope, source) {
953                 *        with (scope) {
954                 *            eval(<script_from_source>);
955                 *        }
956                 *    })(scope, source);
957                 */
958                final Global global = getGlobal();
959                // Create a new object. This is where all declarations
960                // (var, function) from the evaluated code go.
961                // make global to be its __proto__ so that global
962                // definitions are accessible to the evaluated code.
963                final ScriptObject evalScope = newScope(global);
964
965                // finally, make a WithObject around user supplied scope object
966                // so that it's properties are accessible as variables.
967                final ScriptObject withObj = ScriptRuntime.openWith(evalScope, scope);
968
969                // evaluate given source with 'withObj' as scope
970                // but use global object as "this".
971                return evaluateSource(source, withObj, global);
972            }
973        }
974
975        throw typeError("cant.load.script", ScriptRuntime.safeToString(from));
976    }
977
978    /**
979     * Implementation of {@code loadWithNewGlobal} Nashorn extension. Load a script file from a source
980     * expression, after creating a new global scope.
981     *
982     * @param from source expression for script
983     * @param args (optional) arguments to be passed to the loaded script
984     *
985     * @return return value for load call (undefined)
986     *
987     * @throws IOException if source cannot be found or loaded
988     */
989    public Object loadWithNewGlobal(final Object from, final Object...args) throws IOException {
990        final Global oldGlobal = getGlobal();
991        final Global newGlobal = AccessController.doPrivileged(new PrivilegedAction<Global>() {
992           @Override
993           public Global run() {
994               try {
995                   return newGlobal();
996               } catch (final RuntimeException e) {
997                   if (Context.DEBUG) {
998                       e.printStackTrace();
999                   }
1000                   throw e;
1001               }
1002           }
1003        }, CREATE_GLOBAL_ACC_CTXT);
1004        // initialize newly created Global instance
1005        initGlobal(newGlobal);
1006        setGlobal(newGlobal);
1007
1008        final Object[] wrapped = args == null? ScriptRuntime.EMPTY_ARRAY :  ScriptObjectMirror.wrapArray(args, oldGlobal);
1009        newGlobal.put("arguments", newGlobal.wrapAsObject(wrapped), env._strict);
1010
1011        try {
1012            // wrap objects from newGlobal's world as mirrors - but if result
1013            // is from oldGlobal's world, unwrap it!
1014            return ScriptObjectMirror.unwrap(ScriptObjectMirror.wrap(load(newGlobal, from), newGlobal), oldGlobal);
1015        } finally {
1016            setGlobal(oldGlobal);
1017        }
1018    }
1019
1020    /**
1021     * Load or get a structure class. Structure class names are based on the number of parameter fields
1022     * and {@link AccessorProperty} fields in them. Structure classes are used to represent ScriptObjects
1023     *
1024     * @see ObjectClassGenerator
1025     * @see AccessorProperty
1026     * @see ScriptObject
1027     *
1028     * @param fullName  full name of class, e.g. jdk.nashorn.internal.objects.JO2P1 contains 2 fields and 1 parameter.
1029     *
1030     * @return the {@code Class<?>} for this structure
1031     *
1032     * @throws ClassNotFoundException if structure class cannot be resolved
1033     */
1034    @SuppressWarnings("unchecked")
1035    public static Class<? extends ScriptObject> forStructureClass(final String fullName) throws ClassNotFoundException {
1036        if (System.getSecurityManager() != null && !StructureLoader.isStructureClass(fullName)) {
1037            throw new ClassNotFoundException(fullName);
1038        }
1039        return (Class<? extends ScriptObject>)structureClasses.computeIfAbsent(fullName, (name) -> {
1040            try {
1041                return Class.forName(name, true, sharedLoader);
1042            } catch (final ClassNotFoundException e) {
1043                throw new AssertionError(e);
1044            }
1045        });
1046    }
1047
1048    /**
1049     * Is {@code className} the name of a structure class?
1050     *
1051     * @param className a class name
1052     * @return true if className is a structure class name
1053     */
1054    public static boolean isStructureClass(final String className) {
1055        return StructureLoader.isStructureClass(className);
1056    }
1057
1058    /**
1059     * Checks that the given Class can be accessed from no permissions context.
1060     *
1061     * @param clazz Class object
1062     * @throws SecurityException if not accessible
1063     */
1064    public static void checkPackageAccess(final Class<?> clazz) {
1065        final SecurityManager sm = System.getSecurityManager();
1066        if (sm != null) {
1067            Class<?> bottomClazz = clazz;
1068            while (bottomClazz.isArray()) {
1069                bottomClazz = bottomClazz.getComponentType();
1070            }
1071            checkPackageAccess(sm, bottomClazz.getName());
1072        }
1073    }
1074
1075    /**
1076     * Checks that the given package name can be accessed from no permissions context.
1077     *
1078     * @param pkgName package name
1079     * @throws SecurityException if not accessible
1080     */
1081    public static void checkPackageAccess(final String pkgName) {
1082        final SecurityManager sm = System.getSecurityManager();
1083        if (sm != null) {
1084            checkPackageAccess(sm, pkgName.endsWith(".") ? pkgName : pkgName + ".");
1085        }
1086    }
1087
1088    /**
1089     * Checks that the given package can be accessed from no permissions context.
1090     *
1091     * @param sm current security manager instance
1092     * @param fullName fully qualified package name
1093     * @throw SecurityException if not accessible
1094     */
1095    private static void checkPackageAccess(final SecurityManager sm, final String fullName) {
1096        Objects.requireNonNull(sm);
1097        final int index = fullName.lastIndexOf('.');
1098        if (index != -1) {
1099            final String pkgName = fullName.substring(0, index);
1100            AccessController.doPrivileged(new PrivilegedAction<Void>() {
1101                @Override
1102                public Void run() {
1103                    sm.checkPackageAccess(pkgName);
1104                    return null;
1105                }
1106            }, NO_PERMISSIONS_ACC_CTXT);
1107        }
1108    }
1109
1110    /**
1111     * Checks that the given Class can be accessed from no permissions context.
1112     *
1113     * @param clazz Class object
1114     * @return true if package is accessible, false otherwise
1115     */
1116    private static boolean isAccessiblePackage(final Class<?> clazz) {
1117        try {
1118            checkPackageAccess(clazz);
1119            return true;
1120        } catch (final SecurityException se) {
1121            return false;
1122        }
1123    }
1124
1125    /**
1126     * Checks that the given Class is public and it can be accessed from no permissions context.
1127     *
1128     * @param clazz Class object to check
1129     * @return true if Class is accessible, false otherwise
1130     */
1131    public static boolean isAccessibleClass(final Class<?> clazz) {
1132        return Modifier.isPublic(clazz.getModifiers()) && Context.isAccessiblePackage(clazz);
1133    }
1134
1135    /**
1136     * Lookup a Java class. This is used for JSR-223 stuff linking in from
1137     * {@code jdk.nashorn.internal.objects.NativeJava} and {@code jdk.nashorn.internal.runtime.NativeJavaPackage}
1138     *
1139     * @param fullName full name of class to load
1140     *
1141     * @return the {@code Class<?>} for the name
1142     *
1143     * @throws ClassNotFoundException if class cannot be resolved
1144     */
1145    public Class<?> findClass(final String fullName) throws ClassNotFoundException {
1146        if (fullName.indexOf('[') != -1 || fullName.indexOf('/') != -1) {
1147            // don't allow array class names or internal names.
1148            throw new ClassNotFoundException(fullName);
1149        }
1150
1151        // give chance to ClassFilter to filter out, if present
1152        if (classFilter != null && !classFilter.exposeToScripts(fullName)) {
1153            throw new ClassNotFoundException(fullName);
1154        }
1155
1156        // check package access as soon as possible!
1157        final SecurityManager sm = System.getSecurityManager();
1158        if (sm != null) {
1159            checkPackageAccess(sm, fullName);
1160        }
1161
1162        // Try finding using the "app" loader.
1163        return Class.forName(fullName, true, appLoader);
1164    }
1165
1166    /**
1167     * Hook to print stack trace for a {@link Throwable} that occurred during
1168     * execution
1169     *
1170     * @param t throwable for which to dump stack
1171     */
1172    public static void printStackTrace(final Throwable t) {
1173        if (Context.DEBUG) {
1174            t.printStackTrace(Context.getCurrentErr());
1175        }
1176    }
1177
1178    /**
1179     * Verify generated bytecode before emission. This is called back from the
1180     * {@link ObjectClassGenerator} or the {@link Compiler}. If the "--verify-code" parameter
1181     * hasn't been given, this is a nop
1182     *
1183     * Note that verification may load classes -- we don't want to do that unless
1184     * user specified verify option. We check it here even though caller
1185     * may have already checked that flag
1186     *
1187     * @param bytecode bytecode to verify
1188     */
1189    public void verify(final byte[] bytecode) {
1190        if (env._verify_code) {
1191            // No verification when security manager is around as verifier
1192            // may load further classes - which should be avoided.
1193            if (System.getSecurityManager() == null) {
1194                CheckClassAdapter.verify(new ClassReader(bytecode), sharedLoader, false, new PrintWriter(System.err, true));
1195            }
1196        }
1197    }
1198
1199    /**
1200     * Create and initialize a new global scope object.
1201     *
1202     * @return the initialized global scope object.
1203     */
1204    public Global createGlobal() {
1205        return initGlobal(newGlobal());
1206    }
1207
1208    /**
1209     * Create a new uninitialized global scope object
1210     * @return the global script object
1211     */
1212    public Global newGlobal() {
1213        createOrInvalidateGlobalConstants();
1214        return new Global(this);
1215    }
1216
1217    private void createOrInvalidateGlobalConstants() {
1218        for (;;) {
1219            final GlobalConstants currentGlobalConstants = getGlobalConstants();
1220            if (currentGlobalConstants != null) {
1221                // Subsequent invocation; we're creating our second or later Global. GlobalConstants is not safe to use
1222                // with more than one Global, as the constant method handle linkages it creates create a coupling
1223                // between the Global and the call sites in the compiled code.
1224                currentGlobalConstants.invalidateForever();
1225                return;
1226            }
1227            final GlobalConstants newGlobalConstants = new GlobalConstants(getLogger(GlobalConstants.class));
1228            if (globalConstantsRef.compareAndSet(null, newGlobalConstants)) {
1229                // First invocation; we're creating the first Global in this Context. Create the GlobalConstants object
1230                // for this Context.
1231                return;
1232            }
1233
1234            // If we reach here, then we started out as the first invocation, but another concurrent invocation won the
1235            // CAS race. We'll just let the loop repeat and invalidate the CAS race winner.
1236        }
1237    }
1238
1239    /**
1240     * Initialize given global scope object.
1241     *
1242     * @param global the global
1243     * @param engine the associated ScriptEngine instance, can be null
1244     * @return the initialized global scope object.
1245     */
1246    public Global initGlobal(final Global global, final ScriptEngine engine) {
1247        // Need only minimal global object, if we are just compiling.
1248        if (!env._compile_only) {
1249            final Global oldGlobal = Context.getGlobal();
1250            try {
1251                Context.setGlobal(global);
1252                // initialize global scope with builtin global objects
1253                global.initBuiltinObjects(engine);
1254            } finally {
1255                Context.setGlobal(oldGlobal);
1256            }
1257        }
1258
1259        return global;
1260    }
1261
1262    /**
1263     * Initialize given global scope object.
1264     *
1265     * @param global the global
1266     * @return the initialized global scope object.
1267     */
1268    public Global initGlobal(final Global global) {
1269        return initGlobal(global, null);
1270    }
1271
1272    /**
1273     * Return the current global's context
1274     * @return current global's context
1275     */
1276    static Context getContextTrusted() {
1277        return getContext(getGlobal());
1278    }
1279
1280    /**
1281     * Gets the Nashorn dynamic linker for the specified class. If the class is
1282     * a script class, the dynamic linker associated with its context is
1283     * returned. Otherwise the dynamic linker associated with the current
1284     * context is returned.
1285     * @param clazz the class for which we want to retrieve a dynamic linker.
1286     * @return the Nashorn dynamic linker for the specified class.
1287     */
1288    public static DynamicLinker getDynamicLinker(final Class<?> clazz) {
1289        return fromClass(clazz).dynamicLinker;
1290    }
1291
1292    /**
1293     * Gets the Nashorn dynamic linker associated with the current context.
1294     * @return the Nashorn dynamic linker for the current context.
1295     */
1296    public static DynamicLinker getDynamicLinker() {
1297        return getContextTrusted().dynamicLinker;
1298    }
1299
1300    /**
1301     * Creates a module layer with one module that is defined to the given class
1302     * loader.
1303     *
1304     * @param descriptor the module descriptor for the newly created module
1305     * @param loader the class loader of the module
1306     * @return the new Module
1307     */
1308    public static Module createModule(final ModuleDescriptor descriptor, final ClassLoader loader) {
1309        final SecurityManager sm = System.getSecurityManager();
1310        if (sm != null) {
1311            sm.checkPermission(new RuntimePermission(NASHORN_CREATE_MODULE));
1312        }
1313        return createModuleTrusted(descriptor, loader);
1314    }
1315
1316    /**
1317     * Creates a module layer with one module that is defined to the given class
1318     * loader.
1319     *
1320     * @param descriptor the module descriptor for the newly created module
1321     * @param loader the class loader of the module
1322     * @return the new Module
1323     */
1324    static Module createModuleTrusted(final ModuleDescriptor descriptor, final ClassLoader loader) {
1325        return createModuleTrusted(Layer.boot(), descriptor, loader);
1326    }
1327
1328    /**
1329     * Creates a module layer with one module that is defined to the given class
1330     * loader.
1331     *
1332     * @param parent the parent layer of the new module
1333     * @param descriptor the module descriptor for the newly created module
1334     * @param loader the class loader of the module
1335     * @return the new Module
1336     */
1337    static Module createModuleTrusted(final Layer parent, final ModuleDescriptor descriptor, final ClassLoader loader) {
1338        final String mn = descriptor.name();
1339
1340        final ModuleReference mref = new ModuleReference(descriptor, null, () -> {
1341            IOException ioe = new IOException("<dynamic module>");
1342            throw new UncheckedIOException(ioe);
1343        });
1344
1345        final ModuleFinder finder = new ModuleFinder() {
1346            @Override
1347            public Optional<ModuleReference> find(String name) {
1348                if (name.equals(mn)) {
1349                    return Optional.of(mref);
1350                } else {
1351                    return Optional.empty();
1352                }
1353            }
1354            @Override
1355            public Set<ModuleReference> findAll() {
1356                return Set.of(mref);
1357            }
1358        };
1359
1360        final Configuration cf = parent.configuration()
1361                .resolveRequires(finder, ModuleFinder.of(), Set.of(mn));
1362
1363        final PrivilegedAction<Layer> pa = () -> parent.defineModules(cf, name -> loader);
1364        final Layer layer = AccessController.doPrivileged(pa, GET_LOADER_ACC_CTXT);
1365
1366        final Module m = layer.findModule(mn).get();
1367        assert m.getLayer() == layer;
1368
1369        return m;
1370    }
1371
1372    static Context getContextTrustedOrNull() {
1373        final Global global = Context.getGlobal();
1374        return global == null ? null : getContext(global);
1375    }
1376
1377    private static Context getContext(final Global global) {
1378        // We can't invoke Global.getContext() directly, as it's a protected override, and Global isn't in our package.
1379        // In order to access the method, we must cast it to ScriptObject first (which is in our package) and then let
1380        // virtual invocation do its thing.
1381        return ((ScriptObject)global).getContext();
1382    }
1383
1384    /**
1385     * Try to infer Context instance from the Class. If we cannot,
1386     * then get it from the thread local variable.
1387     *
1388     * @param clazz the class
1389     * @return context
1390     */
1391    static Context fromClass(final Class<?> clazz) {
1392        ClassLoader loader = null;
1393        try {
1394            loader = clazz.getClassLoader();
1395        } catch (SecurityException ignored) {
1396            // This could fail because of anonymous classes being used.
1397            // Accessing loader of anonymous class fails (for extension
1398            // loader class too?). In any case, for us fetching Context
1399            // from class loader is just an optimization. We can always
1400            // get Context from thread local storage (below).
1401        }
1402
1403        if (loader instanceof ScriptLoader) {
1404            return ((ScriptLoader)loader).getContext();
1405        }
1406
1407        return Context.getContextTrusted();
1408    }
1409
1410    private URL getResourceURL(final String resName) {
1411        if (appLoader != null) {
1412            return appLoader.getResource(resName);
1413        }
1414        return ClassLoader.getSystemResource(resName);
1415    }
1416
1417    private Object evaluateSource(final Source source, final ScriptObject scope, final ScriptObject thiz) {
1418        ScriptFunction script = null;
1419
1420        try {
1421            script = compileScript(source, scope, new Context.ThrowErrorManager());
1422        } catch (final ParserException e) {
1423            e.throwAsEcmaException();
1424        }
1425
1426        return ScriptRuntime.apply(script, thiz);
1427    }
1428
1429    private static ScriptFunction getProgramFunction(final Class<?> script, final ScriptObject scope) {
1430        if (script == null) {
1431            return null;
1432        }
1433        return invokeCreateProgramFunctionHandle(getCreateProgramFunctionHandle(script), scope);
1434    }
1435
1436    private static MethodHandle getCreateProgramFunctionHandle(final Class<?> script) {
1437        try {
1438            return LOOKUP.findStatic(script, CREATE_PROGRAM_FUNCTION.symbolName(), CREATE_PROGRAM_FUNCTION_TYPE);
1439        } catch (NoSuchMethodException | IllegalAccessException e) {
1440            throw new AssertionError("Failed to retrieve a handle for the program function for " + script.getName(), e);
1441        }
1442    }
1443
1444    private static ScriptFunction invokeCreateProgramFunctionHandle(final MethodHandle createProgramFunctionHandle, final ScriptObject scope) {
1445        try {
1446            return (ScriptFunction)createProgramFunctionHandle.invokeExact(scope);
1447        } catch (final RuntimeException|Error e) {
1448            throw e;
1449        } catch (final Throwable t) {
1450            throw new AssertionError("Failed to create a program function", t);
1451        }
1452    }
1453
1454    private ScriptFunction compileScript(final Source source, final ScriptObject scope, final ErrorManager errMan) {
1455        return getProgramFunction(compile(source, errMan, this._strict, false), scope);
1456    }
1457
1458    private synchronized Class<?> compile(final Source source, final ErrorManager errMan, final boolean strict, final boolean isEval) {
1459        // start with no errors, no warnings.
1460        errMan.reset();
1461
1462        Class<?> script = findCachedClass(source);
1463        if (script != null) {
1464            final DebugLogger log = getLogger(Compiler.class);
1465            if (log.isEnabled()) {
1466                log.fine(new RuntimeEvent<>(Level.INFO, source), "Code cache hit for ", source, " avoiding recompile.");
1467            }
1468            return script;
1469        }
1470
1471        StoredScript storedScript = null;
1472        FunctionNode functionNode = null;
1473        // Don't use code store if optimistic types is enabled but lazy compilation is not.
1474        // This would store a full script compilation with many wrong optimistic assumptions that would
1475        // do more harm than good on later runs with both optimistic types and lazy compilation enabled.
1476        final boolean useCodeStore = codeStore != null && !env._parse_only && (!env._optimistic_types || env._lazy_compilation);
1477        final String cacheKey = useCodeStore ? CodeStore.getCacheKey("script", null) : null;
1478
1479        if (useCodeStore) {
1480            storedScript = codeStore.load(source, cacheKey);
1481        }
1482
1483        if (storedScript == null) {
1484            if (env._dest_dir != null) {
1485                source.dump(env._dest_dir);
1486            }
1487
1488            functionNode = new Parser(env, source, errMan, strict, getLogger(Parser.class)).parse();
1489
1490            if (errMan.hasErrors()) {
1491                return null;
1492            }
1493
1494            if (env._print_ast || functionNode.getDebugFlag(FunctionNode.DEBUG_PRINT_AST)) {
1495                getErr().println(new ASTWriter(functionNode));
1496            }
1497
1498            if (env._print_parse || functionNode.getDebugFlag(FunctionNode.DEBUG_PRINT_PARSE)) {
1499                getErr().println(new PrintVisitor(functionNode, true, false));
1500            }
1501        }
1502
1503        if (env._parse_only) {
1504            return null;
1505        }
1506
1507        final URL          url    = source.getURL();
1508        final CodeSource   cs     = new CodeSource(url, (CodeSigner[])null);
1509        final CodeInstaller installer;
1510        if (!env.useAnonymousClasses(isEval) || env._persistent_cache || !env._lazy_compilation) {
1511            // Persistent code cache and eager compilation preclude use of VM anonymous classes
1512            final ScriptLoader loader = env._loader_per_compile ? createNewLoader() : scriptLoader;
1513            installer = new NamedContextCodeInstaller(this, cs, loader);
1514        } else {
1515            installer = new AnonymousContextCodeInstaller(this, cs,
1516                    anonymousHostClasses.getOrCreate(cs, (key) ->
1517                            createNewLoader().installClass(
1518                                    // NOTE: we're defining these constants in AnonymousContextCodeInstaller so they are not
1519                                    // initialized if we don't use AnonymousContextCodeInstaller. As this method is only ever
1520                                    // invoked from AnonymousContextCodeInstaller, this is okay.
1521                                    AnonymousContextCodeInstaller.ANONYMOUS_HOST_CLASS_NAME,
1522                                    AnonymousContextCodeInstaller.ANONYMOUS_HOST_CLASS_BYTES, cs)));
1523        }
1524
1525        if (storedScript == null) {
1526            final CompilationPhases phases = Compiler.CompilationPhases.COMPILE_ALL;
1527
1528            final Compiler compiler = Compiler.forInitialCompilation(
1529                    installer,
1530                    source,
1531                    errMan,
1532                    strict | functionNode.isStrict());
1533
1534            final FunctionNode compiledFunction = compiler.compile(functionNode, phases);
1535            if (errMan.hasErrors()) {
1536                return null;
1537            }
1538            script = compiledFunction.getRootClass();
1539            compiler.persistClassInfo(cacheKey, compiledFunction);
1540        } else {
1541            Compiler.updateCompilationId(storedScript.getCompilationId());
1542            script = storedScript.installScript(source, installer);
1543        }
1544
1545        cacheClass(source, script);
1546        return script;
1547    }
1548
1549    private ScriptLoader createNewLoader() {
1550        return AccessController.doPrivileged(
1551             new PrivilegedAction<ScriptLoader>() {
1552                @Override
1553                public ScriptLoader run() {
1554                    return new ScriptLoader(appLoader, Context.this);
1555                }
1556             }, CREATE_LOADER_ACC_CTXT);
1557    }
1558
1559    private long getUniqueScriptId() {
1560        return uniqueScriptId.getAndIncrement();
1561    }
1562
1563    /**
1564     * Cache for compiled script classes.
1565     */
1566    @SuppressWarnings("serial")
1567    @Logger(name="classcache")
1568    private static class ClassCache extends LinkedHashMap<Source, ClassReference> implements Loggable {
1569        private final int size;
1570        private final ReferenceQueue<Class<?>> queue;
1571        private final DebugLogger log;
1572
1573        ClassCache(final Context context, final int size) {
1574            super(size, 0.75f, true);
1575            this.size = size;
1576            this.queue = new ReferenceQueue<>();
1577            this.log   = initLogger(context);
1578        }
1579
1580        void cache(final Source source, final Class<?> clazz) {
1581            if (log.isEnabled()) {
1582                log.info("Caching ", source, " in class cache");
1583            }
1584            put(source, new ClassReference(clazz, queue, source));
1585        }
1586
1587        @Override
1588        protected boolean removeEldestEntry(final Map.Entry<Source, ClassReference> eldest) {
1589            return size() > size;
1590        }
1591
1592        @Override
1593        public ClassReference get(final Object key) {
1594            for (ClassReference ref; (ref = (ClassReference)queue.poll()) != null; ) {
1595                final Source source = ref.source;
1596                if (log.isEnabled()) {
1597                    log.info("Evicting ", source, " from class cache.");
1598                }
1599                remove(source);
1600            }
1601
1602            final ClassReference ref = super.get(key);
1603            if (ref != null && log.isEnabled()) {
1604                log.info("Retrieved class reference for ", ref.source, " from class cache");
1605            }
1606            return ref;
1607        }
1608
1609        @Override
1610        public DebugLogger initLogger(final Context context) {
1611            return context.getLogger(getClass());
1612        }
1613
1614        @Override
1615        public DebugLogger getLogger() {
1616            return log;
1617        }
1618
1619    }
1620
1621    private static class ClassReference extends SoftReference<Class<?>> {
1622        private final Source source;
1623
1624        ClassReference(final Class<?> clazz, final ReferenceQueue<Class<?>> queue, final Source source) {
1625            super(clazz, queue);
1626            this.source = source;
1627        }
1628    }
1629
1630    // Class cache management
1631    private Class<?> findCachedClass(final Source source) {
1632        final ClassReference ref = classCache == null ? null : classCache.get(source);
1633        return ref != null ? ref.get() : null;
1634    }
1635
1636    private void cacheClass(final Source source, final Class<?> clazz) {
1637        if (classCache != null) {
1638            classCache.cache(source, clazz);
1639        }
1640    }
1641
1642    // logging
1643    private final Map<String, DebugLogger> loggers = new HashMap<>();
1644
1645    private void initLoggers() {
1646        ((Loggable)MethodHandleFactory.getFunctionality()).initLogger(this);
1647    }
1648
1649    /**
1650     * Get a logger, given a loggable class
1651     * @param clazz a Loggable class
1652     * @return debuglogger associated with that class
1653     */
1654    public DebugLogger getLogger(final Class<? extends Loggable> clazz) {
1655        return getLogger(clazz, null);
1656    }
1657
1658    /**
1659     * Get a logger, given a loggable class
1660     * @param clazz a Loggable class
1661     * @param initHook an init hook - if this is the first time the logger is created in the context, run the init hook
1662     * @return debuglogger associated with that class
1663     */
1664    public DebugLogger getLogger(final Class<? extends Loggable> clazz, final Consumer<DebugLogger> initHook) {
1665        final String name = getLoggerName(clazz);
1666        DebugLogger logger = loggers.get(name);
1667        if (logger == null) {
1668            if (!env.hasLogger(name)) {
1669                return DebugLogger.DISABLED_LOGGER;
1670            }
1671            final LoggerInfo info = env._loggers.get(name);
1672            logger = new DebugLogger(name, info.getLevel(), info.isQuiet());
1673            if (initHook != null) {
1674                initHook.accept(logger);
1675            }
1676            loggers.put(name, logger);
1677        }
1678        return logger;
1679    }
1680
1681    /**
1682     * Given a Loggable class, weave debug info info a method handle for that logger.
1683     * Level.INFO is used
1684     *
1685     * @param clazz loggable
1686     * @param mh    method handle
1687     * @param text  debug printout to add
1688     *
1689     * @return instrumented method handle, or null if logger not enabled
1690     */
1691    public MethodHandle addLoggingToHandle(final Class<? extends Loggable> clazz, final MethodHandle mh, final Supplier<String> text) {
1692        return addLoggingToHandle(clazz, Level.INFO, mh, Integer.MAX_VALUE, false, text);
1693    }
1694
1695    /**
1696     * Given a Loggable class, weave debug info info a method handle for that logger.
1697     *
1698     * @param clazz            loggable
1699     * @param level            log level
1700     * @param mh               method handle
1701     * @param paramStart       first parameter to print
1702     * @param printReturnValue should we print the return value?
1703     * @param text             debug printout to add
1704     *
1705     * @return instrumented method handle, or null if logger not enabled
1706     */
1707    public MethodHandle addLoggingToHandle(final Class<? extends Loggable> clazz, final Level level, final MethodHandle mh, final int paramStart, final boolean printReturnValue, final Supplier<String> text) {
1708        final DebugLogger log = getLogger(clazz);
1709        if (log.isEnabled()) {
1710            return MethodHandleFactory.addDebugPrintout(log, level, mh, paramStart, printReturnValue, text.get());
1711        }
1712        return mh;
1713    }
1714
1715    private static String getLoggerName(final Class<?> clazz) {
1716        Class<?> current = clazz;
1717        while (current != null) {
1718            final Logger log = current.getAnnotation(Logger.class);
1719            if (log != null) {
1720                assert !"".equals(log.name());
1721                return log.name();
1722            }
1723            current = current.getSuperclass();
1724        }
1725        assert false;
1726        return null;
1727    }
1728
1729    /**
1730     * This is a special kind of switchpoint used to guard builtin
1731     * properties and prototypes. In the future it might contain
1732     * logic to e.g. multiple switchpoint classes.
1733     */
1734    public static final class BuiltinSwitchPoint extends SwitchPoint {
1735        //empty
1736    }
1737
1738    /**
1739     * Create a new builtin switchpoint and return it
1740     * @param name key name
1741     * @return new builtin switchpoint
1742     */
1743    public SwitchPoint newBuiltinSwitchPoint(final String name) {
1744        assert builtinSwitchPoints.get(name) == null;
1745        final SwitchPoint sp = new BuiltinSwitchPoint();
1746        builtinSwitchPoints.put(name, sp);
1747        return sp;
1748    }
1749
1750    /**
1751     * Return the builtin switchpoint for a particular key name
1752     * @param name key name
1753     * @return builtin switchpoint or null if none
1754     */
1755    public SwitchPoint getBuiltinSwitchPoint(final String name) {
1756        return builtinSwitchPoints.get(name);
1757    }
1758}
1759