nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/runtime/Context.java
author lagergren
Fri, 26 Sep 2014 18:47:20 +0200
changeset 26886 18c744ab4df2
parent 26768 751b0f427090
child 26891 aee38d04254a
permissions -rw-r--r--
8059211: Changed ArrayData.length accessor to use the protected field and fixed javadoc warnings related to this Reviewed-by: attila, hannesw
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
     1
/*
16151
97c1e756ae1e 8005663: Update copyright year to 2013
jlaskey
parents: 16147
diff changeset
     2
 * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
     4
 *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    10
 *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    15
 * accompanied this code).
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    16
 *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    20
 *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    23
 * questions.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    24
 */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    25
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    26
package jdk.nashorn.internal.runtime;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    27
23767
7c0614b75e23 8038638: Persistent store for compiled scripts
hannesw
parents: 23375
diff changeset
    28
import static jdk.nashorn.internal.codegen.CompilerConstants.CONSTANTS;
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 23076
diff changeset
    29
import static jdk.nashorn.internal.codegen.CompilerConstants.CREATE_PROGRAM_FUNCTION;
23767
7c0614b75e23 8038638: Persistent store for compiled scripts
hannesw
parents: 23375
diff changeset
    30
import static jdk.nashorn.internal.codegen.CompilerConstants.SOURCE;
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    31
import static jdk.nashorn.internal.codegen.CompilerConstants.STRICT_MODE;
26764
c777787a937d 8046202: Make persistent code store more flexible
hannesw
parents: 26508
diff changeset
    32
import static jdk.nashorn.internal.runtime.CodeStore.newCodeStore;
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    33
import static jdk.nashorn.internal.runtime.ECMAErrors.typeError;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    34
import static jdk.nashorn.internal.runtime.ScriptRuntime.UNDEFINED;
24206
40c6d45af73f 8040078: Avoid repeated reading of source for cached loads
hannesw
parents: 23767
diff changeset
    35
import static jdk.nashorn.internal.runtime.Source.sourceFor;
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    36
import java.io.File;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    37
import java.io.IOException;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    38
import java.io.PrintWriter;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    39
import java.lang.invoke.MethodHandle;
24783
b5c31bfe1496 8046014: MultiGlobalCompiledScript should cache :createProgramFunction handle
attila
parents: 24779
diff changeset
    40
import java.lang.invoke.MethodHandles;
b5c31bfe1496 8046014: MultiGlobalCompiledScript should cache :createProgramFunction handle
attila
parents: 24779
diff changeset
    41
import java.lang.invoke.MethodType;
26768
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26764
diff changeset
    42
import java.lang.invoke.SwitchPoint;
23372
09707b3e5fb0 8021350: Share script classes between threads/globals within context
hannesw
parents: 23076
diff changeset
    43
import java.lang.ref.ReferenceQueue;
09707b3e5fb0 8021350: Share script classes between threads/globals within context
hannesw
parents: 23076
diff changeset
    44
import java.lang.ref.SoftReference;
23767
7c0614b75e23 8038638: Persistent store for compiled scripts
hannesw
parents: 23375
diff changeset
    45
import java.lang.reflect.Field;
19088
153f268bfa72 8021122: Not all callables are handled for toString and other function valued properties
sundar
parents: 19085
diff changeset
    46
import java.lang.reflect.Modifier;
16254
2ed824fc93be 8008554: load was broken for URLs
lagergren
parents: 16251
diff changeset
    47
import java.net.MalformedURLException;
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    48
import java.net.URL;
19097
f544a2ea40ef 8021262: Make nashorn access checks consistent with underlying dynalink
sundar
parents: 19088
diff changeset
    49
import java.security.AccessControlContext;
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    50
import java.security.AccessController;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    51
import java.security.CodeSigner;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    52
import java.security.CodeSource;
19097
f544a2ea40ef 8021262: Make nashorn access checks consistent with underlying dynalink
sundar
parents: 19088
diff changeset
    53
import java.security.Permissions;
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    54
import java.security.PrivilegedAction;
23767
7c0614b75e23 8038638: Persistent store for compiled scripts
hannesw
parents: 23375
diff changeset
    55
import java.security.PrivilegedActionException;
7c0614b75e23 8038638: Persistent store for compiled scripts
hannesw
parents: 23375
diff changeset
    56
import java.security.PrivilegedExceptionAction;
19097
f544a2ea40ef 8021262: Make nashorn access checks consistent with underlying dynalink
sundar
parents: 19088
diff changeset
    57
import java.security.ProtectionDomain;
24769
attila
parents: 24759 24282
diff changeset
    58
import java.util.Collection;
23767
7c0614b75e23 8038638: Persistent store for compiled scripts
hannesw
parents: 23375
diff changeset
    59
import java.util.HashMap;
23372
09707b3e5fb0 8021350: Share script classes between threads/globals within context
hannesw
parents: 23076
diff changeset
    60
import java.util.LinkedHashMap;
18334
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 18328
diff changeset
    61
import java.util.Map;
22669
75563515567f 8032681: Issues with Nashorn
attila
parents: 22389
diff changeset
    62
import java.util.concurrent.atomic.AtomicLong;
24769
attila
parents: 24759 24282
diff changeset
    63
import java.util.function.Consumer;
24745
3a6e1477362b 8041434: Add synchronization to the common global constants structure
lagergren
parents: 24744
diff changeset
    64
import java.util.function.Supplier;
24731
ab0c8fc915ae 8038406: Testability: as a first step of moving loggers away from the process global space, the Debug object now supports logging POJOs from log entries as an event queue, which can be introspected from test scripts. This is way better than screen scraping brittle and subject-to-change log output.
lagergren
parents: 24727
diff changeset
    65
import java.util.logging.Level;
25422
199a23bee487 8049524: Global object initialization via javax.script API should be minimal
sundar
parents: 25243
diff changeset
    66
import javax.script.ScriptEngine;
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    67
import jdk.internal.org.objectweb.asm.ClassReader;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    68
import jdk.internal.org.objectweb.asm.util.CheckClassAdapter;
26071
430e39f6731f 8050078: Nashorn ClassFilter Support
sundar
parents: 26068
diff changeset
    69
import jdk.nashorn.api.scripting.ClassFilter;
17979
adae4d39ee07 8015945: loadWithNewGlobal return value has to be properly wrapped
sundar
parents: 17976
diff changeset
    70
import jdk.nashorn.api.scripting.ScriptObjectMirror;
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    71
import jdk.nashorn.internal.codegen.Compiler;
24759
31aed7d9c02a 8034206: Make parts of code pipeline reusable in order to facilitate faster warmup and faster lazy compilation.
lagergren
parents: 24745
diff changeset
    72
import jdk.nashorn.internal.codegen.Compiler.CompilationPhases;
16240
e1468b33e201 8008239: Unpublicized parts of the code generator package that were only package internal.
lagergren
parents: 16233
diff changeset
    73
import jdk.nashorn.internal.codegen.ObjectClassGenerator;
16233
95d3e01c04c3 8008199: Lazy compilation and trampoline implementation
lagergren
parents: 16230
diff changeset
    74
import jdk.nashorn.internal.ir.FunctionNode;
16240
e1468b33e201 8008239: Unpublicized parts of the code generator package that were only package internal.
lagergren
parents: 16233
diff changeset
    75
import jdk.nashorn.internal.ir.debug.ASTWriter;
16233
95d3e01c04c3 8008199: Lazy compilation and trampoline implementation
lagergren
parents: 16230
diff changeset
    76
import jdk.nashorn.internal.ir.debug.PrintVisitor;
24745
3a6e1477362b 8041434: Add synchronization to the common global constants structure
lagergren
parents: 24744
diff changeset
    77
import jdk.nashorn.internal.lookup.MethodHandleFactory;
18851
bdb92c95f886 8019947: inherited property invalidation does not work with two globals in same context
sundar
parents: 18618
diff changeset
    78
import jdk.nashorn.internal.objects.Global;
16233
95d3e01c04c3 8008199: Lazy compilation and trampoline implementation
lagergren
parents: 16230
diff changeset
    79
import jdk.nashorn.internal.parser.Parser;
24742
a9afb384e654 8040655: When processing a RewriteException debug object, the return value has already been reset to null. We need to catch this value before that.
lagergren
parents: 24733
diff changeset
    80
import jdk.nashorn.internal.runtime.events.RuntimeEvent;
24744
5290da85fc3d 8038426: Move all loggers from process wide scope into Global scope
lagergren
parents: 24742
diff changeset
    81
import jdk.nashorn.internal.runtime.logging.DebugLogger;
24745
3a6e1477362b 8041434: Add synchronization to the common global constants structure
lagergren
parents: 24744
diff changeset
    82
import jdk.nashorn.internal.runtime.logging.Loggable;
3a6e1477362b 8041434: Add synchronization to the common global constants structure
lagergren
parents: 24744
diff changeset
    83
import jdk.nashorn.internal.runtime.logging.Logger;
24769
attila
parents: 24759 24282
diff changeset
    84
import jdk.nashorn.internal.runtime.options.LoggingOption.LoggerInfo;
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    85
import jdk.nashorn.internal.runtime.options.Options;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    86
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    87
/**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    88
 * This class manages the global state of execution. Context is immutable.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    89
 */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    90
public final class Context {
19459
79e75274df99 8022707: Revisit all doPrivileged blocks
sundar
parents: 19098
diff changeset
    91
    // nashorn specific security runtime access permission names
79e75274df99 8022707: Revisit all doPrivileged blocks
sundar
parents: 19098
diff changeset
    92
    /**
79e75274df99 8022707: Revisit all doPrivileged blocks
sundar
parents: 19098
diff changeset
    93
     * Permission needed to pass arbitrary nashorn command line options when creating Context.
79e75274df99 8022707: Revisit all doPrivileged blocks
sundar
parents: 19098
diff changeset
    94
     */
79e75274df99 8022707: Revisit all doPrivileged blocks
sundar
parents: 19098
diff changeset
    95
    public static final String NASHORN_SET_CONFIG      = "nashorn.setConfig";
79e75274df99 8022707: Revisit all doPrivileged blocks
sundar
parents: 19098
diff changeset
    96
79e75274df99 8022707: Revisit all doPrivileged blocks
sundar
parents: 19098
diff changeset
    97
    /**
79e75274df99 8022707: Revisit all doPrivileged blocks
sundar
parents: 19098
diff changeset
    98
     * Permission needed to create Nashorn Context instance.
79e75274df99 8022707: Revisit all doPrivileged blocks
sundar
parents: 19098
diff changeset
    99
     */
79e75274df99 8022707: Revisit all doPrivileged blocks
sundar
parents: 19098
diff changeset
   100
    public static final String NASHORN_CREATE_CONTEXT  = "nashorn.createContext";
79e75274df99 8022707: Revisit all doPrivileged blocks
sundar
parents: 19098
diff changeset
   101
79e75274df99 8022707: Revisit all doPrivileged blocks
sundar
parents: 19098
diff changeset
   102
    /**
79e75274df99 8022707: Revisit all doPrivileged blocks
sundar
parents: 19098
diff changeset
   103
     * Permission needed to create Nashorn Global instance.
79e75274df99 8022707: Revisit all doPrivileged blocks
sundar
parents: 19098
diff changeset
   104
     */
79e75274df99 8022707: Revisit all doPrivileged blocks
sundar
parents: 19098
diff changeset
   105
    public static final String NASHORN_CREATE_GLOBAL   = "nashorn.createGlobal";
79e75274df99 8022707: Revisit all doPrivileged blocks
sundar
parents: 19098
diff changeset
   106
79e75274df99 8022707: Revisit all doPrivileged blocks
sundar
parents: 19098
diff changeset
   107
    /**
79e75274df99 8022707: Revisit all doPrivileged blocks
sundar
parents: 19098
diff changeset
   108
     * Permission to get current Nashorn Context from thread local storage.
79e75274df99 8022707: Revisit all doPrivileged blocks
sundar
parents: 19098
diff changeset
   109
     */
79e75274df99 8022707: Revisit all doPrivileged blocks
sundar
parents: 19098
diff changeset
   110
    public static final String NASHORN_GET_CONTEXT     = "nashorn.getContext";
79e75274df99 8022707: Revisit all doPrivileged blocks
sundar
parents: 19098
diff changeset
   111
79e75274df99 8022707: Revisit all doPrivileged blocks
sundar
parents: 19098
diff changeset
   112
    /**
79e75274df99 8022707: Revisit all doPrivileged blocks
sundar
parents: 19098
diff changeset
   113
     * Permission to use Java reflection/jsr292 from script code.
79e75274df99 8022707: Revisit all doPrivileged blocks
sundar
parents: 19098
diff changeset
   114
     */
79e75274df99 8022707: Revisit all doPrivileged blocks
sundar
parents: 19098
diff changeset
   115
    public static final String NASHORN_JAVA_REFLECTION = "nashorn.JavaReflection";
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   116
22389
ea3dda90768c 8032060: PropertyMap of Error objects is not stable
sundar
parents: 20933
diff changeset
   117
    /**
ea3dda90768c 8032060: PropertyMap of Error objects is not stable
sundar
parents: 20933
diff changeset
   118
     * Permission to enable nashorn debug mode.
ea3dda90768c 8032060: PropertyMap of Error objects is not stable
sundar
parents: 20933
diff changeset
   119
     */
ea3dda90768c 8032060: PropertyMap of Error objects is not stable
sundar
parents: 20933
diff changeset
   120
    public static final String NASHORN_DEBUG_MODE = "nashorn.debugMode";
ea3dda90768c 8032060: PropertyMap of Error objects is not stable
sundar
parents: 20933
diff changeset
   121
20564
f353da961684 8025629: load function should support a way to load scripts from classpath
sundar
parents: 19895
diff changeset
   122
    // nashorn load psuedo URL prefixes
f353da961684 8025629: load function should support a way to load scripts from classpath
sundar
parents: 19895
diff changeset
   123
    private static final String LOAD_CLASSPATH = "classpath:";
f353da961684 8025629: load function should support a way to load scripts from classpath
sundar
parents: 19895
diff changeset
   124
    private static final String LOAD_FX = "fx:";
f353da961684 8025629: load function should support a way to load scripts from classpath
sundar
parents: 19895
diff changeset
   125
    private static final String LOAD_NASHORN = "nashorn:";
f353da961684 8025629: load function should support a way to load scripts from classpath
sundar
parents: 19895
diff changeset
   126
24783
b5c31bfe1496 8046014: MultiGlobalCompiledScript should cache :createProgramFunction handle
attila
parents: 24779
diff changeset
   127
    private static MethodHandles.Lookup LOOKUP = MethodHandles.lookup();
b5c31bfe1496 8046014: MultiGlobalCompiledScript should cache :createProgramFunction handle
attila
parents: 24779
diff changeset
   128
    private static MethodType CREATE_PROGRAM_FUNCTION_TYPE = MethodType.methodType(ScriptFunction.class, ScriptObject.class);
b5c31bfe1496 8046014: MultiGlobalCompiledScript should cache :createProgramFunction handle
attila
parents: 24779
diff changeset
   129
26768
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26764
diff changeset
   130
    /**
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26764
diff changeset
   131
     * Keeps track of which builtin prototypes and properties have been relinked
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26764
diff changeset
   132
     * Currently we are conservative and associate the name of a builtin class with all
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26764
diff changeset
   133
     * its properties, so it's enough to invalidate a property to break all assumptions
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26764
diff changeset
   134
     * about a prototype. This can be changed to a more fine grained approach, but no one
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26764
diff changeset
   135
     * ever needs this, given the very rare occurance of swapping out only parts of
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26764
diff changeset
   136
     * a builtin v.s. the entire builtin object
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26764
diff changeset
   137
     */
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26764
diff changeset
   138
    private final Map<String, SwitchPoint> builtinSwitchPoints = new HashMap<>();
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26764
diff changeset
   139
19622
b042dad0de96 8023228: Debugger information gather is too slow.
jlaskey
parents: 19472
diff changeset
   140
    /* Force DebuggerSupport to be loaded. */
b042dad0de96 8023228: Debugger information gather is too slow.
jlaskey
parents: 19472
diff changeset
   141
    static {
b042dad0de96 8023228: Debugger information gather is too slow.
jlaskey
parents: 19472
diff changeset
   142
        DebuggerSupport.FORCELOAD = true;
b042dad0de96 8023228: Debugger information gather is too slow.
jlaskey
parents: 19472
diff changeset
   143
    }
b042dad0de96 8023228: Debugger information gather is too slow.
jlaskey
parents: 19472
diff changeset
   144
16233
95d3e01c04c3 8008199: Lazy compilation and trampoline implementation
lagergren
parents: 16230
diff changeset
   145
    /**
95d3e01c04c3 8008199: Lazy compilation and trampoline implementation
lagergren
parents: 16230
diff changeset
   146
     * ContextCodeInstaller that has the privilege of installing classes in the Context.
95d3e01c04c3 8008199: Lazy compilation and trampoline implementation
lagergren
parents: 16230
diff changeset
   147
     * Can only be instantiated from inside the context and is opaque to other classes
95d3e01c04c3 8008199: Lazy compilation and trampoline implementation
lagergren
parents: 16230
diff changeset
   148
     */
16262
75513555e603 8008731: Separate configuration environment (options, error/output writer etc.) from Context
sundar
parents: 16256
diff changeset
   149
    public static class ContextCodeInstaller implements CodeInstaller<ScriptEnvironment> {
16233
95d3e01c04c3 8008199: Lazy compilation and trampoline implementation
lagergren
parents: 16230
diff changeset
   150
        private final Context      context;
95d3e01c04c3 8008199: Lazy compilation and trampoline implementation
lagergren
parents: 16230
diff changeset
   151
        private final ScriptLoader loader;
95d3e01c04c3 8008199: Lazy compilation and trampoline implementation
lagergren
parents: 16230
diff changeset
   152
        private final CodeSource   codeSource;
95d3e01c04c3 8008199: Lazy compilation and trampoline implementation
lagergren
parents: 16230
diff changeset
   153
95d3e01c04c3 8008199: Lazy compilation and trampoline implementation
lagergren
parents: 16230
diff changeset
   154
        private ContextCodeInstaller(final Context context, final ScriptLoader loader, final CodeSource codeSource) {
95d3e01c04c3 8008199: Lazy compilation and trampoline implementation
lagergren
parents: 16230
diff changeset
   155
            this.context    = context;
95d3e01c04c3 8008199: Lazy compilation and trampoline implementation
lagergren
parents: 16230
diff changeset
   156
            this.loader     = loader;
95d3e01c04c3 8008199: Lazy compilation and trampoline implementation
lagergren
parents: 16230
diff changeset
   157
            this.codeSource = codeSource;
95d3e01c04c3 8008199: Lazy compilation and trampoline implementation
lagergren
parents: 16230
diff changeset
   158
        }
95d3e01c04c3 8008199: Lazy compilation and trampoline implementation
lagergren
parents: 16230
diff changeset
   159
95d3e01c04c3 8008199: Lazy compilation and trampoline implementation
lagergren
parents: 16230
diff changeset
   160
        /**
95d3e01c04c3 8008199: Lazy compilation and trampoline implementation
lagergren
parents: 16230
diff changeset
   161
         * Return the context for this installer
16523
af8b30edebce 8009718: Lazy execution architecture continued - ScriptFunctionData is either final or recompilable. Moved ScriptFunctionData creation logic away from runtime to compile time. Prepared for method generation/specialization. Got rid of ScriptFunctionImplTrampoline whose semantics could be done as part of the relinking anyway. Merge with the lookup package change.
lagergren
parents: 16522
diff changeset
   162
         * @return ScriptEnvironment
16233
95d3e01c04c3 8008199: Lazy compilation and trampoline implementation
lagergren
parents: 16230
diff changeset
   163
         */
95d3e01c04c3 8008199: Lazy compilation and trampoline implementation
lagergren
parents: 16230
diff changeset
   164
        @Override
16262
75513555e603 8008731: Separate configuration environment (options, error/output writer etc.) from Context
sundar
parents: 16256
diff changeset
   165
        public ScriptEnvironment getOwner() {
75513555e603 8008731: Separate configuration environment (options, error/output writer etc.) from Context
sundar
parents: 16256
diff changeset
   166
            return context.env;
16233
95d3e01c04c3 8008199: Lazy compilation and trampoline implementation
lagergren
parents: 16230
diff changeset
   167
        }
95d3e01c04c3 8008199: Lazy compilation and trampoline implementation
lagergren
parents: 16230
diff changeset
   168
95d3e01c04c3 8008199: Lazy compilation and trampoline implementation
lagergren
parents: 16230
diff changeset
   169
        @Override
95d3e01c04c3 8008199: Lazy compilation and trampoline implementation
lagergren
parents: 16230
diff changeset
   170
        public Class<?> install(final String className, final byte[] bytecode) {
23767
7c0614b75e23 8038638: Persistent store for compiled scripts
hannesw
parents: 23375
diff changeset
   171
            final String   binaryName = Compiler.binaryName(className);
24769
attila
parents: 24759 24282
diff changeset
   172
            return loader.installClass(binaryName, bytecode, codeSource);
attila
parents: 24759 24282
diff changeset
   173
        }
23767
7c0614b75e23 8038638: Persistent store for compiled scripts
hannesw
parents: 23375
diff changeset
   174
24769
attila
parents: 24759 24282
diff changeset
   175
        @Override
attila
parents: 24759 24282
diff changeset
   176
        public void initialize(final Collection<Class<?>> classes, final Source source, final Object[] constants) {
26248
9e9455565f77 8055954: Do not parallelize class installation
attila
parents: 26071
diff changeset
   177
            try {
9e9455565f77 8055954: Do not parallelize class installation
attila
parents: 26071
diff changeset
   178
                AccessController.doPrivileged(new PrivilegedExceptionAction<Void>() {
23767
7c0614b75e23 8038638: Persistent store for compiled scripts
hannesw
parents: 23375
diff changeset
   179
                    @Override
26248
9e9455565f77 8055954: Do not parallelize class installation
attila
parents: 26071
diff changeset
   180
                    public Void run() throws Exception {
9e9455565f77 8055954: Do not parallelize class installation
attila
parents: 26071
diff changeset
   181
                        for (final Class<?> clazz : classes) {
9e9455565f77 8055954: Do not parallelize class installation
attila
parents: 26071
diff changeset
   182
                            //use reflection to write source and constants table to installed classes
9e9455565f77 8055954: Do not parallelize class installation
attila
parents: 26071
diff changeset
   183
                            final Field sourceField = clazz.getDeclaredField(SOURCE.symbolName());
9e9455565f77 8055954: Do not parallelize class installation
attila
parents: 26071
diff changeset
   184
                            sourceField.setAccessible(true);
9e9455565f77 8055954: Do not parallelize class installation
attila
parents: 26071
diff changeset
   185
                            sourceField.set(null, source);
24769
attila
parents: 24759 24282
diff changeset
   186
26248
9e9455565f77 8055954: Do not parallelize class installation
attila
parents: 26071
diff changeset
   187
                            final Field constantsField = clazz.getDeclaredField(CONSTANTS.symbolName());
9e9455565f77 8055954: Do not parallelize class installation
attila
parents: 26071
diff changeset
   188
                            constantsField.setAccessible(true);
9e9455565f77 8055954: Do not parallelize class installation
attila
parents: 26071
diff changeset
   189
                            constantsField.set(null, constants);
24769
attila
parents: 24759 24282
diff changeset
   190
                        }
26248
9e9455565f77 8055954: Do not parallelize class installation
attila
parents: 26071
diff changeset
   191
                        return null;
23767
7c0614b75e23 8038638: Persistent store for compiled scripts
hannesw
parents: 23375
diff changeset
   192
                    }
7c0614b75e23 8038638: Persistent store for compiled scripts
hannesw
parents: 23375
diff changeset
   193
                });
26248
9e9455565f77 8055954: Do not parallelize class installation
attila
parents: 26071
diff changeset
   194
            } catch (final PrivilegedActionException e) {
9e9455565f77 8055954: Do not parallelize class installation
attila
parents: 26071
diff changeset
   195
                throw new RuntimeException(e);
9e9455565f77 8055954: Do not parallelize class installation
attila
parents: 26071
diff changeset
   196
            }
16233
95d3e01c04c3 8008199: Lazy compilation and trampoline implementation
lagergren
parents: 16230
diff changeset
   197
        }
16262
75513555e603 8008731: Separate configuration environment (options, error/output writer etc.) from Context
sundar
parents: 16256
diff changeset
   198
75513555e603 8008731: Separate configuration environment (options, error/output writer etc.) from Context
sundar
parents: 16256
diff changeset
   199
        @Override
75513555e603 8008731: Separate configuration environment (options, error/output writer etc.) from Context
sundar
parents: 16256
diff changeset
   200
        public void verify(final byte[] code) {
75513555e603 8008731: Separate configuration environment (options, error/output writer etc.) from Context
sundar
parents: 16256
diff changeset
   201
            context.verify(code);
75513555e603 8008731: Separate configuration environment (options, error/output writer etc.) from Context
sundar
parents: 16256
diff changeset
   202
        }
18862
8b6a01b38cb8 8020224: LinkageError: attempted duplicate class definition when --loader-per-compiler=false
sundar
parents: 18860
diff changeset
   203
8b6a01b38cb8 8020224: LinkageError: attempted duplicate class definition when --loader-per-compiler=false
sundar
parents: 18860
diff changeset
   204
        @Override
8b6a01b38cb8 8020224: LinkageError: attempted duplicate class definition when --loader-per-compiler=false
sundar
parents: 18860
diff changeset
   205
        public long getUniqueScriptId() {
8b6a01b38cb8 8020224: LinkageError: attempted duplicate class definition when --loader-per-compiler=false
sundar
parents: 18860
diff changeset
   206
            return context.getUniqueScriptId();
8b6a01b38cb8 8020224: LinkageError: attempted duplicate class definition when --loader-per-compiler=false
sundar
parents: 18860
diff changeset
   207
        }
20929
f2bd18181940 8026167: Class cache/reuse of 'eval' scripts results in ClassCastException in some cases.
sundar
parents: 20928
diff changeset
   208
f2bd18181940 8026167: Class cache/reuse of 'eval' scripts results in ClassCastException in some cases.
sundar
parents: 20928
diff changeset
   209
        @Override
26508
b40ef4386b01 8057021: UserAccessorProperty guards fail with multiple globals
hannesw
parents: 26504
diff changeset
   210
        public void storeScript(final String cacheKey, final Source source, final String mainClassName,
26067
b32ccc3a76c9 8055199: Tidy up Nashorn codebase for code standards (August 2014)
attila
parents: 26065
diff changeset
   211
                                final Map<String,byte[]> classBytes, final Map<Integer, FunctionInitializer> initializers,
26055
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents: 25422
diff changeset
   212
                                final Object[] constants, final int compilationId) {
23767
7c0614b75e23 8038638: Persistent store for compiled scripts
hannesw
parents: 23375
diff changeset
   213
            if (context.codeStore != null) {
26764
c777787a937d 8046202: Make persistent code store more flexible
hannesw
parents: 26508
diff changeset
   214
                context.codeStore.store(cacheKey, source, mainClassName, classBytes, initializers, constants, compilationId);
23767
7c0614b75e23 8038638: Persistent store for compiled scripts
hannesw
parents: 23375
diff changeset
   215
            }
7c0614b75e23 8038638: Persistent store for compiled scripts
hannesw
parents: 23375
diff changeset
   216
        }
26055
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents: 25422
diff changeset
   217
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents: 25422
diff changeset
   218
        @Override
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents: 25422
diff changeset
   219
        public StoredScript loadScript(final Source source, final String functionKey) {
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents: 25422
diff changeset
   220
            if (context.codeStore != null) {
26764
c777787a937d 8046202: Make persistent code store more flexible
hannesw
parents: 26508
diff changeset
   221
                return context.codeStore.load(source, functionKey);
26055
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents: 25422
diff changeset
   222
            }
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents: 25422
diff changeset
   223
            return null;
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents: 25422
diff changeset
   224
        }
16233
95d3e01c04c3 8008199: Lazy compilation and trampoline implementation
lagergren
parents: 16230
diff changeset
   225
    }
95d3e01c04c3 8008199: Lazy compilation and trampoline implementation
lagergren
parents: 16230
diff changeset
   226
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   227
    /** Is Context global debug mode enabled ? */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   228
    public static final boolean DEBUG = Options.getBooleanProperty("nashorn.debug");
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   229
23375
a1110f2cbe75 8037400: Remove getInitialMap getters and GlobalObject interface
sundar
parents: 23374
diff changeset
   230
    private static final ThreadLocal<Global> currentGlobal = new ThreadLocal<>();
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   231
23767
7c0614b75e23 8038638: Persistent store for compiled scripts
hannesw
parents: 23375
diff changeset
   232
    // in-memory cache for loaded classes
23372
09707b3e5fb0 8021350: Share script classes between threads/globals within context
hannesw
parents: 23076
diff changeset
   233
    private ClassCache classCache;
09707b3e5fb0 8021350: Share script classes between threads/globals within context
hannesw
parents: 23076
diff changeset
   234
23767
7c0614b75e23 8038638: Persistent store for compiled scripts
hannesw
parents: 23375
diff changeset
   235
    // persistent code store
7c0614b75e23 8038638: Persistent store for compiled scripts
hannesw
parents: 23375
diff changeset
   236
    private CodeStore codeStore;
7c0614b75e23 8038638: Persistent store for compiled scripts
hannesw
parents: 23375
diff changeset
   237
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   238
    /**
16188
d6390b0ea32a 8006678: Avoid too many Context.getGlobal() calls
sundar
parents: 16185
diff changeset
   239
     * Get the current global scope
d6390b0ea32a 8006678: Avoid too many Context.getGlobal() calls
sundar
parents: 16185
diff changeset
   240
     * @return the current global scope
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   241
     */
23375
a1110f2cbe75 8037400: Remove getInitialMap getters and GlobalObject interface
sundar
parents: 23374
diff changeset
   242
    public static Global getGlobal() {
17231
734f61d5a097 8012612: Compile failed
sundar
parents: 16947
diff changeset
   243
        // This class in a package.access protected package.
734f61d5a097 8012612: Compile failed
sundar
parents: 16947
diff changeset
   244
        // Trusted code only can call this method.
23375
a1110f2cbe75 8037400: Remove getInitialMap getters and GlobalObject interface
sundar
parents: 23374
diff changeset
   245
        return currentGlobal.get();
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   246
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   247
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   248
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   249
     * Set the current global scope
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   250
     * @param global the global scope
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   251
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   252
    public static void setGlobal(final ScriptObject global) {
18851
bdb92c95f886 8019947: inherited property invalidation does not work with two globals in same context
sundar
parents: 18618
diff changeset
   253
        if (global != null && !(global instanceof Global)) {
23375
a1110f2cbe75 8037400: Remove getInitialMap getters and GlobalObject interface
sundar
parents: 23374
diff changeset
   254
            throw new IllegalArgumentException("not a global!");
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   255
        }
23375
a1110f2cbe75 8037400: Remove getInitialMap getters and GlobalObject interface
sundar
parents: 23374
diff changeset
   256
        setGlobal((Global)global);
a1110f2cbe75 8037400: Remove getInitialMap getters and GlobalObject interface
sundar
parents: 23374
diff changeset
   257
    }
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   258
23375
a1110f2cbe75 8037400: Remove getInitialMap getters and GlobalObject interface
sundar
parents: 23374
diff changeset
   259
    /**
a1110f2cbe75 8037400: Remove getInitialMap getters and GlobalObject interface
sundar
parents: 23374
diff changeset
   260
     * Set the current global scope
a1110f2cbe75 8037400: Remove getInitialMap getters and GlobalObject interface
sundar
parents: 23374
diff changeset
   261
     * @param global the global scope
a1110f2cbe75 8037400: Remove getInitialMap getters and GlobalObject interface
sundar
parents: 23374
diff changeset
   262
     */
a1110f2cbe75 8037400: Remove getInitialMap getters and GlobalObject interface
sundar
parents: 23374
diff changeset
   263
    public static void setGlobal(final Global global) {
a1110f2cbe75 8037400: Remove getInitialMap getters and GlobalObject interface
sundar
parents: 23374
diff changeset
   264
        // This class in a package.access protected package.
a1110f2cbe75 8037400: Remove getInitialMap getters and GlobalObject interface
sundar
parents: 23374
diff changeset
   265
        // Trusted code only can call this method.
24733
1e825be55fd1 8027043: Turn global accesses into MethodHandle.constant, with one chance of reassignment, e.g. x = value occuring once in the global scope is ok, twice is not.
lagergren
parents: 24731
diff changeset
   266
        assert getGlobal() != global;
1e825be55fd1 8027043: Turn global accesses into MethodHandle.constant, with one chance of reassignment, e.g. x = value occuring once in the global scope is ok, twice is not.
lagergren
parents: 24731
diff changeset
   267
        //same code can be cached between globals, then we need to invalidate method handle constants
24745
3a6e1477362b 8041434: Add synchronization to the common global constants structure
lagergren
parents: 24744
diff changeset
   268
        if (global != null) {
3a6e1477362b 8041434: Add synchronization to the common global constants structure
lagergren
parents: 24744
diff changeset
   269
            Global.getConstants().invalidateAll();
3a6e1477362b 8041434: Add synchronization to the common global constants structure
lagergren
parents: 24744
diff changeset
   270
        }
23375
a1110f2cbe75 8037400: Remove getInitialMap getters and GlobalObject interface
sundar
parents: 23374
diff changeset
   271
        currentGlobal.set(global);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   272
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   273
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   274
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   275
     * Get context of the current global
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   276
     * @return current global scope's context.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   277
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   278
    public static Context getContext() {
16185
893aabe8c800 8006635: Reduce access levels as much as possible
sundar
parents: 16155
diff changeset
   279
        final SecurityManager sm = System.getSecurityManager();
893aabe8c800 8006635: Reduce access levels as much as possible
sundar
parents: 16155
diff changeset
   280
        if (sm != null) {
19459
79e75274df99 8022707: Revisit all doPrivileged blocks
sundar
parents: 19098
diff changeset
   281
            sm.checkPermission(new RuntimePermission(NASHORN_GET_CONTEXT));
16185
893aabe8c800 8006635: Reduce access levels as much as possible
sundar
parents: 16155
diff changeset
   282
        }
893aabe8c800 8006635: Reduce access levels as much as possible
sundar
parents: 16155
diff changeset
   283
        return getContextTrusted();
893aabe8c800 8006635: Reduce access levels as much as possible
sundar
parents: 16155
diff changeset
   284
    }
893aabe8c800 8006635: Reduce access levels as much as possible
sundar
parents: 16155
diff changeset
   285
893aabe8c800 8006635: Reduce access levels as much as possible
sundar
parents: 16155
diff changeset
   286
    /**
893aabe8c800 8006635: Reduce access levels as much as possible
sundar
parents: 16155
diff changeset
   287
     * Get current context's error writer
893aabe8c800 8006635: Reduce access levels as much as possible
sundar
parents: 16155
diff changeset
   288
     *
893aabe8c800 8006635: Reduce access levels as much as possible
sundar
parents: 16155
diff changeset
   289
     * @return error writer of the current context
893aabe8c800 8006635: Reduce access levels as much as possible
sundar
parents: 16155
diff changeset
   290
     */
893aabe8c800 8006635: Reduce access levels as much as possible
sundar
parents: 16155
diff changeset
   291
    public static PrintWriter getCurrentErr() {
23375
a1110f2cbe75 8037400: Remove getInitialMap getters and GlobalObject interface
sundar
parents: 23374
diff changeset
   292
        final ScriptObject global = getGlobal();
16185
893aabe8c800 8006635: Reduce access levels as much as possible
sundar
parents: 16155
diff changeset
   293
        return (global != null)? global.getContext().getErr() : new PrintWriter(System.err);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   294
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   295
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   296
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   297
     * Output text to this Context's error stream
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   298
     * @param str text to write
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   299
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   300
    public static void err(final String str) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   301
        err(str, true);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   302
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   303
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   304
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   305
     * Output text to this Context's error stream, optionally with
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   306
     * a newline afterwards
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   307
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   308
     * @param str  text to write
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   309
     * @param crlf write a carriage return/new line after text
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   310
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   311
    public static void err(final String str, final boolean crlf) {
16185
893aabe8c800 8006635: Reduce access levels as much as possible
sundar
parents: 16155
diff changeset
   312
        final PrintWriter err = Context.getCurrentErr();
16155
a8ab83cbaa49 8005788: Loggers and their corresponding system properties not working correctly
lagergren
parents: 16151
diff changeset
   313
        if (err != null) {
a8ab83cbaa49 8005788: Loggers and their corresponding system properties not working correctly
lagergren
parents: 16151
diff changeset
   314
            if (crlf) {
a8ab83cbaa49 8005788: Loggers and their corresponding system properties not working correctly
lagergren
parents: 16151
diff changeset
   315
                err.println(str);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   316
            } else {
16155
a8ab83cbaa49 8005788: Loggers and their corresponding system properties not working correctly
lagergren
parents: 16151
diff changeset
   317
                err.print(str);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   318
            }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   319
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   320
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   321
16262
75513555e603 8008731: Separate configuration environment (options, error/output writer etc.) from Context
sundar
parents: 16256
diff changeset
   322
    /** Current environment. */
75513555e603 8008731: Separate configuration environment (options, error/output writer etc.) from Context
sundar
parents: 16256
diff changeset
   323
    private final ScriptEnvironment env;
75513555e603 8008731: Separate configuration environment (options, error/output writer etc.) from Context
sundar
parents: 16256
diff changeset
   324
75513555e603 8008731: Separate configuration environment (options, error/output writer etc.) from Context
sundar
parents: 16256
diff changeset
   325
    /** is this context in strict mode? Cached from env. as this is used heavily. */
18328
ebd24057f163 8015355: Array.prototype functions don't honour non-writable length and / or index properties
sundar
parents: 18321
diff changeset
   326
    final boolean _strict;
16262
75513555e603 8008731: Separate configuration environment (options, error/output writer etc.) from Context
sundar
parents: 16256
diff changeset
   327
16185
893aabe8c800 8006635: Reduce access levels as much as possible
sundar
parents: 16155
diff changeset
   328
    /** class loader to resolve classes from script. */
893aabe8c800 8006635: Reduce access levels as much as possible
sundar
parents: 16155
diff changeset
   329
    private final ClassLoader  appLoader;
893aabe8c800 8006635: Reduce access levels as much as possible
sundar
parents: 16155
diff changeset
   330
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   331
    /** Class loader to load classes from -classpath option, if set. */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   332
    private final ClassLoader  classPathLoader;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   333
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   334
    /** Class loader to load classes compiled from scripts. */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   335
    private final ScriptLoader scriptLoader;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   336
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   337
    /** Current error manager. */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   338
    private final ErrorManager errors;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   339
18862
8b6a01b38cb8 8020224: LinkageError: attempted duplicate class definition when --loader-per-compiler=false
sundar
parents: 18860
diff changeset
   340
    /** Unique id for script. Used only when --loader-per-compile=false */
18864
c701b823ed9e 8020276: interface checks in Invocable.getInterface implementation
sundar
parents: 18862
diff changeset
   341
    private final AtomicLong uniqueScriptId;
18862
8b6a01b38cb8 8020224: LinkageError: attempted duplicate class definition when --loader-per-compiler=false
sundar
parents: 18860
diff changeset
   342
26071
430e39f6731f 8050078: Nashorn ClassFilter Support
sundar
parents: 26068
diff changeset
   343
    /** Optional class filter to use for Java classes. Can be null. */
430e39f6731f 8050078: Nashorn ClassFilter Support
sundar
parents: 26068
diff changeset
   344
    private final ClassFilter classFilter;
430e39f6731f 8050078: Nashorn ClassFilter Support
sundar
parents: 26068
diff changeset
   345
16185
893aabe8c800 8006635: Reduce access levels as much as possible
sundar
parents: 16155
diff changeset
   346
    private static final ClassLoader myLoader = Context.class.getClassLoader();
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   347
    private static final StructureLoader sharedLoader;
19459
79e75274df99 8022707: Revisit all doPrivileged blocks
sundar
parents: 19098
diff changeset
   348
20933
89748612fd1d 8026250: Logging nullpointer bugfix and javadoc warnings
lagergren
parents: 20929
diff changeset
   349
    /*package-private*/ @SuppressWarnings("static-method")
89748612fd1d 8026250: Logging nullpointer bugfix and javadoc warnings
lagergren
parents: 20929
diff changeset
   350
    ClassLoader getSharedLoader() {
19895
965b12eb322e 8024619: JDBC java.sql.DriverManager is not usable from JS script
sundar
parents: 19884
diff changeset
   351
        return sharedLoader;
965b12eb322e 8024619: JDBC java.sql.DriverManager is not usable from JS script
sundar
parents: 19884
diff changeset
   352
    }
965b12eb322e 8024619: JDBC java.sql.DriverManager is not usable from JS script
sundar
parents: 19884
diff changeset
   353
19459
79e75274df99 8022707: Revisit all doPrivileged blocks
sundar
parents: 19098
diff changeset
   354
    private static AccessControlContext createNoPermAccCtxt() {
79e75274df99 8022707: Revisit all doPrivileged blocks
sundar
parents: 19098
diff changeset
   355
        return new AccessControlContext(new ProtectionDomain[] { new ProtectionDomain(null, new Permissions()) });
79e75274df99 8022707: Revisit all doPrivileged blocks
sundar
parents: 19098
diff changeset
   356
    }
79e75274df99 8022707: Revisit all doPrivileged blocks
sundar
parents: 19098
diff changeset
   357
79e75274df99 8022707: Revisit all doPrivileged blocks
sundar
parents: 19098
diff changeset
   358
    private static AccessControlContext createPermAccCtxt(final String permName) {
79e75274df99 8022707: Revisit all doPrivileged blocks
sundar
parents: 19098
diff changeset
   359
        final Permissions perms = new Permissions();
79e75274df99 8022707: Revisit all doPrivileged blocks
sundar
parents: 19098
diff changeset
   360
        perms.add(new RuntimePermission(permName));
79e75274df99 8022707: Revisit all doPrivileged blocks
sundar
parents: 19098
diff changeset
   361
        return new AccessControlContext(new ProtectionDomain[] { new ProtectionDomain(null, perms) });
79e75274df99 8022707: Revisit all doPrivileged blocks
sundar
parents: 19098
diff changeset
   362
    }
79e75274df99 8022707: Revisit all doPrivileged blocks
sundar
parents: 19098
diff changeset
   363
79e75274df99 8022707: Revisit all doPrivileged blocks
sundar
parents: 19098
diff changeset
   364
    private static final AccessControlContext NO_PERMISSIONS_ACC_CTXT = createNoPermAccCtxt();
79e75274df99 8022707: Revisit all doPrivileged blocks
sundar
parents: 19098
diff changeset
   365
    private static final AccessControlContext CREATE_LOADER_ACC_CTXT  = createPermAccCtxt("createClassLoader");
79e75274df99 8022707: Revisit all doPrivileged blocks
sundar
parents: 19098
diff changeset
   366
    private static final AccessControlContext CREATE_GLOBAL_ACC_CTXT  = createPermAccCtxt(NASHORN_CREATE_GLOBAL);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   367
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   368
    static {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   369
        sharedLoader = AccessController.doPrivileged(new PrivilegedAction<StructureLoader>() {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   370
            @Override
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   371
            public StructureLoader run() {
19895
965b12eb322e 8024619: JDBC java.sql.DriverManager is not usable from JS script
sundar
parents: 19884
diff changeset
   372
                return new StructureLoader(myLoader);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   373
            }
19459
79e75274df99 8022707: Revisit all doPrivileged blocks
sundar
parents: 19098
diff changeset
   374
        }, CREATE_LOADER_ACC_CTXT);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   375
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   376
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   377
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   378
     * ThrowErrorManager that throws ParserException upon error conditions.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   379
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   380
    public static class ThrowErrorManager extends ErrorManager {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   381
        @Override
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   382
        public void error(final String message) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   383
            throw new ParserException(message);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   384
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   385
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   386
        @Override
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   387
        public void error(final ParserException e) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   388
            throw e;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   389
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   390
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   391
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   392
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   393
     * Constructor
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   394
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   395
     * @param options options from command line or Context creator
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   396
     * @param errors  error manger
16185
893aabe8c800 8006635: Reduce access levels as much as possible
sundar
parents: 16155
diff changeset
   397
     * @param appLoader application class loader
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   398
     */
16185
893aabe8c800 8006635: Reduce access levels as much as possible
sundar
parents: 16155
diff changeset
   399
    public Context(final Options options, final ErrorManager errors, final ClassLoader appLoader) {
26071
430e39f6731f 8050078: Nashorn ClassFilter Support
sundar
parents: 26068
diff changeset
   400
        this(options, errors, appLoader, (ClassFilter)null);
430e39f6731f 8050078: Nashorn ClassFilter Support
sundar
parents: 26068
diff changeset
   401
    }
430e39f6731f 8050078: Nashorn ClassFilter Support
sundar
parents: 26068
diff changeset
   402
430e39f6731f 8050078: Nashorn ClassFilter Support
sundar
parents: 26068
diff changeset
   403
    /**
430e39f6731f 8050078: Nashorn ClassFilter Support
sundar
parents: 26068
diff changeset
   404
     * Constructor
430e39f6731f 8050078: Nashorn ClassFilter Support
sundar
parents: 26068
diff changeset
   405
     *
430e39f6731f 8050078: Nashorn ClassFilter Support
sundar
parents: 26068
diff changeset
   406
     * @param options options from command line or Context creator
430e39f6731f 8050078: Nashorn ClassFilter Support
sundar
parents: 26068
diff changeset
   407
     * @param errors  error manger
430e39f6731f 8050078: Nashorn ClassFilter Support
sundar
parents: 26068
diff changeset
   408
     * @param appLoader application class loader
430e39f6731f 8050078: Nashorn ClassFilter Support
sundar
parents: 26068
diff changeset
   409
     * @param classFilter class filter to use
430e39f6731f 8050078: Nashorn ClassFilter Support
sundar
parents: 26068
diff changeset
   410
     */
430e39f6731f 8050078: Nashorn ClassFilter Support
sundar
parents: 26068
diff changeset
   411
    public Context(final Options options, final ErrorManager errors, final ClassLoader appLoader, final ClassFilter classFilter) {
430e39f6731f 8050078: Nashorn ClassFilter Support
sundar
parents: 26068
diff changeset
   412
        this(options, errors, new PrintWriter(System.out, true), new PrintWriter(System.err, true), appLoader, classFilter);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   413
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   414
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   415
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   416
     * Constructor
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   417
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   418
     * @param options options from command line or Context creator
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   419
     * @param errors  error manger
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   420
     * @param out     output writer for this Context
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   421
     * @param err     error writer for this Context
16185
893aabe8c800 8006635: Reduce access levels as much as possible
sundar
parents: 16155
diff changeset
   422
     * @param appLoader application class loader
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   423
     */
16185
893aabe8c800 8006635: Reduce access levels as much as possible
sundar
parents: 16155
diff changeset
   424
    public Context(final Options options, final ErrorManager errors, final PrintWriter out, final PrintWriter err, final ClassLoader appLoader) {
26071
430e39f6731f 8050078: Nashorn ClassFilter Support
sundar
parents: 26068
diff changeset
   425
        this(options, errors, out, err, appLoader, (ClassFilter)null);
430e39f6731f 8050078: Nashorn ClassFilter Support
sundar
parents: 26068
diff changeset
   426
    }
430e39f6731f 8050078: Nashorn ClassFilter Support
sundar
parents: 26068
diff changeset
   427
430e39f6731f 8050078: Nashorn ClassFilter Support
sundar
parents: 26068
diff changeset
   428
    /**
430e39f6731f 8050078: Nashorn ClassFilter Support
sundar
parents: 26068
diff changeset
   429
     * Constructor
430e39f6731f 8050078: Nashorn ClassFilter Support
sundar
parents: 26068
diff changeset
   430
     *
430e39f6731f 8050078: Nashorn ClassFilter Support
sundar
parents: 26068
diff changeset
   431
     * @param options options from command line or Context creator
430e39f6731f 8050078: Nashorn ClassFilter Support
sundar
parents: 26068
diff changeset
   432
     * @param errors  error manger
430e39f6731f 8050078: Nashorn ClassFilter Support
sundar
parents: 26068
diff changeset
   433
     * @param out     output writer for this Context
430e39f6731f 8050078: Nashorn ClassFilter Support
sundar
parents: 26068
diff changeset
   434
     * @param err     error writer for this Context
430e39f6731f 8050078: Nashorn ClassFilter Support
sundar
parents: 26068
diff changeset
   435
     * @param appLoader application class loader
430e39f6731f 8050078: Nashorn ClassFilter Support
sundar
parents: 26068
diff changeset
   436
     * @param classFilter class filter to use
430e39f6731f 8050078: Nashorn ClassFilter Support
sundar
parents: 26068
diff changeset
   437
     */
430e39f6731f 8050078: Nashorn ClassFilter Support
sundar
parents: 26068
diff changeset
   438
    public Context(final Options options, final ErrorManager errors, final PrintWriter out, final PrintWriter err, final ClassLoader appLoader, final ClassFilter classFilter) {
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   439
        final SecurityManager sm = System.getSecurityManager();
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   440
        if (sm != null) {
19459
79e75274df99 8022707: Revisit all doPrivileged blocks
sundar
parents: 19098
diff changeset
   441
            sm.checkPermission(new RuntimePermission(NASHORN_CREATE_CONTEXT));
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   442
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   443
26071
430e39f6731f 8050078: Nashorn ClassFilter Support
sundar
parents: 26068
diff changeset
   444
        this.classFilter = classFilter;
16262
75513555e603 8008731: Separate configuration environment (options, error/output writer etc.) from Context
sundar
parents: 16256
diff changeset
   445
        this.env       = new ScriptEnvironment(options, out, err);
75513555e603 8008731: Separate configuration environment (options, error/output writer etc.) from Context
sundar
parents: 16256
diff changeset
   446
        this._strict   = env._strict;
16185
893aabe8c800 8006635: Reduce access levels as much as possible
sundar
parents: 16155
diff changeset
   447
        this.appLoader = appLoader;
18864
c701b823ed9e 8020276: interface checks in Invocable.getInterface implementation
sundar
parents: 18862
diff changeset
   448
        if (env._loader_per_compile) {
c701b823ed9e 8020276: interface checks in Invocable.getInterface implementation
sundar
parents: 18862
diff changeset
   449
            this.scriptLoader = null;
c701b823ed9e 8020276: interface checks in Invocable.getInterface implementation
sundar
parents: 18862
diff changeset
   450
            this.uniqueScriptId = null;
c701b823ed9e 8020276: interface checks in Invocable.getInterface implementation
sundar
parents: 18862
diff changeset
   451
        } else {
c701b823ed9e 8020276: interface checks in Invocable.getInterface implementation
sundar
parents: 18862
diff changeset
   452
            this.scriptLoader = createNewLoader();
c701b823ed9e 8020276: interface checks in Invocable.getInterface implementation
sundar
parents: 18862
diff changeset
   453
            this.uniqueScriptId = new AtomicLong();
c701b823ed9e 8020276: interface checks in Invocable.getInterface implementation
sundar
parents: 18862
diff changeset
   454
        }
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   455
        this.errors    = errors;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   456
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   457
        // if user passed -classpath option, make a class loader with that and set it as
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   458
        // thread context class loader so that script can access classes from that path.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   459
        final String classPath = options.getString("classpath");
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 23076
diff changeset
   460
        if (!env._compile_only && classPath != null && !classPath.isEmpty()) {
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   461
            // make sure that caller can create a class loader.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   462
            if (sm != null) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   463
                sm.checkPermission(new RuntimePermission("createClassLoader"));
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   464
            }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   465
            this.classPathLoader = NashornLoader.createClassLoader(classPath);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   466
        } else {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   467
            this.classPathLoader = null;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   468
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   469
23372
09707b3e5fb0 8021350: Share script classes between threads/globals within context
hannesw
parents: 23076
diff changeset
   470
        final int cacheSize = env._class_cache_size;
09707b3e5fb0 8021350: Share script classes between threads/globals within context
hannesw
parents: 23076
diff changeset
   471
        if (cacheSize > 0) {
09707b3e5fb0 8021350: Share script classes between threads/globals within context
hannesw
parents: 23076
diff changeset
   472
            classCache = new ClassCache(cacheSize);
09707b3e5fb0 8021350: Share script classes between threads/globals within context
hannesw
parents: 23076
diff changeset
   473
        }
09707b3e5fb0 8021350: Share script classes between threads/globals within context
hannesw
parents: 23076
diff changeset
   474
23767
7c0614b75e23 8038638: Persistent store for compiled scripts
hannesw
parents: 23375
diff changeset
   475
        if (env._persistent_cache) {
25243
7a1edca6ce94 8048079: Persistent code store is broken after optimistic types merge
hannesw
parents: 25240
diff changeset
   476
            try {
26764
c777787a937d 8046202: Make persistent code store more flexible
hannesw
parents: 26508
diff changeset
   477
                codeStore = newCodeStore(this);
25243
7a1edca6ce94 8048079: Persistent code store is broken after optimistic types merge
hannesw
parents: 25240
diff changeset
   478
            } catch (final IOException e) {
7a1edca6ce94 8048079: Persistent code store is broken after optimistic types merge
hannesw
parents: 25240
diff changeset
   479
                throw new RuntimeException("Error initializing code cache", e);
23767
7c0614b75e23 8038638: Persistent store for compiled scripts
hannesw
parents: 23375
diff changeset
   480
            }
7c0614b75e23 8038638: Persistent store for compiled scripts
hannesw
parents: 23375
diff changeset
   481
        }
7c0614b75e23 8038638: Persistent store for compiled scripts
hannesw
parents: 23375
diff changeset
   482
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   483
        // print version info if asked.
16262
75513555e603 8008731: Separate configuration environment (options, error/output writer etc.) from Context
sundar
parents: 16256
diff changeset
   484
        if (env._version) {
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   485
            getErr().println("nashorn " + Version.version());
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   486
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   487
16262
75513555e603 8008731: Separate configuration environment (options, error/output writer etc.) from Context
sundar
parents: 16256
diff changeset
   488
        if (env._fullversion) {
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   489
            getErr().println("nashorn full version " + Version.fullVersion());
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   490
        }
24745
3a6e1477362b 8041434: Add synchronization to the common global constants structure
lagergren
parents: 24744
diff changeset
   491
3a6e1477362b 8041434: Add synchronization to the common global constants structure
lagergren
parents: 24744
diff changeset
   492
        initLoggers();
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   493
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   494
26071
430e39f6731f 8050078: Nashorn ClassFilter Support
sundar
parents: 26068
diff changeset
   495
430e39f6731f 8050078: Nashorn ClassFilter Support
sundar
parents: 26068
diff changeset
   496
    /**
430e39f6731f 8050078: Nashorn ClassFilter Support
sundar
parents: 26068
diff changeset
   497
     * Get the class filter for this context
430e39f6731f 8050078: Nashorn ClassFilter Support
sundar
parents: 26068
diff changeset
   498
     * @return class filter
430e39f6731f 8050078: Nashorn ClassFilter Support
sundar
parents: 26068
diff changeset
   499
     */
430e39f6731f 8050078: Nashorn ClassFilter Support
sundar
parents: 26068
diff changeset
   500
    public ClassFilter getClassFilter() {
430e39f6731f 8050078: Nashorn ClassFilter Support
sundar
parents: 26068
diff changeset
   501
        return classFilter;
430e39f6731f 8050078: Nashorn ClassFilter Support
sundar
parents: 26068
diff changeset
   502
    }
430e39f6731f 8050078: Nashorn ClassFilter Support
sundar
parents: 26068
diff changeset
   503
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   504
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   505
     * Get the error manager for this context
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   506
     * @return error manger
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   507
     */
16185
893aabe8c800 8006635: Reduce access levels as much as possible
sundar
parents: 16155
diff changeset
   508
    public ErrorManager getErrorManager() {
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   509
        return errors;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   510
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   511
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   512
    /**
16262
75513555e603 8008731: Separate configuration environment (options, error/output writer etc.) from Context
sundar
parents: 16256
diff changeset
   513
     * Get the script environment for this context
75513555e603 8008731: Separate configuration environment (options, error/output writer etc.) from Context
sundar
parents: 16256
diff changeset
   514
     * @return script environment
75513555e603 8008731: Separate configuration environment (options, error/output writer etc.) from Context
sundar
parents: 16256
diff changeset
   515
     */
75513555e603 8008731: Separate configuration environment (options, error/output writer etc.) from Context
sundar
parents: 16256
diff changeset
   516
    public ScriptEnvironment getEnv() {
75513555e603 8008731: Separate configuration environment (options, error/output writer etc.) from Context
sundar
parents: 16256
diff changeset
   517
        return env;
75513555e603 8008731: Separate configuration environment (options, error/output writer etc.) from Context
sundar
parents: 16256
diff changeset
   518
    }
75513555e603 8008731: Separate configuration environment (options, error/output writer etc.) from Context
sundar
parents: 16256
diff changeset
   519
75513555e603 8008731: Separate configuration environment (options, error/output writer etc.) from Context
sundar
parents: 16256
diff changeset
   520
    /**
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   521
     * Get the output stream for this context
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   522
     * @return output print writer
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   523
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   524
    public PrintWriter getOut() {
16262
75513555e603 8008731: Separate configuration environment (options, error/output writer etc.) from Context
sundar
parents: 16256
diff changeset
   525
        return env.getOut();
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   526
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   527
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   528
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   529
     * Get the error stream for this context
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   530
     * @return error print writer
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   531
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   532
    public PrintWriter getErr() {
16262
75513555e603 8008731: Separate configuration environment (options, error/output writer etc.) from Context
sundar
parents: 16256
diff changeset
   533
        return env.getErr();
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   534
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   535
16201
889ddb179cdf 8007062: Split Lower up into Lower/Attr/FinalizeTypes. Integrate AccessSpecalizer into FinalizeTypes.
lagergren
parents: 16196
diff changeset
   536
    /**
16188
d6390b0ea32a 8006678: Avoid too many Context.getGlobal() calls
sundar
parents: 16185
diff changeset
   537
     * Get the PropertyMap of the current global scope
d6390b0ea32a 8006678: Avoid too many Context.getGlobal() calls
sundar
parents: 16185
diff changeset
   538
     * @return the property map of the current global scope
d6390b0ea32a 8006678: Avoid too many Context.getGlobal() calls
sundar
parents: 16185
diff changeset
   539
     */
16201
889ddb179cdf 8007062: Split Lower up into Lower/Attr/FinalizeTypes. Integrate AccessSpecalizer into FinalizeTypes.
lagergren
parents: 16196
diff changeset
   540
    public static PropertyMap getGlobalMap() {
23375
a1110f2cbe75 8037400: Remove getInitialMap getters and GlobalObject interface
sundar
parents: 23374
diff changeset
   541
        return Context.getGlobal().getMap();
16188
d6390b0ea32a 8006678: Avoid too many Context.getGlobal() calls
sundar
parents: 16185
diff changeset
   542
    }
d6390b0ea32a 8006678: Avoid too many Context.getGlobal() calls
sundar
parents: 16185
diff changeset
   543
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   544
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   545
     * Compile a top level script.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   546
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   547
     * @param source the source
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   548
     * @param scope  the scope
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   549
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   550
     * @return top level function for script
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   551
     */
16230
c38c724d82e7 8008103: Source object should maintain URL of the script source as a private field
sundar
parents: 16226
diff changeset
   552
    public ScriptFunction compileScript(final Source source, final ScriptObject scope) {
c38c724d82e7 8008103: Source object should maintain URL of the script source as a private field
sundar
parents: 16226
diff changeset
   553
        return compileScript(source, scope, this.errors);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   554
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   555
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   556
    /**
24282
2e3bd98c5664 8041697: CompiledScript slower when eval with binding
sundar
parents: 24206
diff changeset
   557
     * Interface to represent compiled code that can be re-used across many
2e3bd98c5664 8041697: CompiledScript slower when eval with binding
sundar
parents: 24206
diff changeset
   558
     * global scope instances
2e3bd98c5664 8041697: CompiledScript slower when eval with binding
sundar
parents: 24206
diff changeset
   559
     */
2e3bd98c5664 8041697: CompiledScript slower when eval with binding
sundar
parents: 24206
diff changeset
   560
    public static interface MultiGlobalCompiledScript {
2e3bd98c5664 8041697: CompiledScript slower when eval with binding
sundar
parents: 24206
diff changeset
   561
        /**
2e3bd98c5664 8041697: CompiledScript slower when eval with binding
sundar
parents: 24206
diff changeset
   562
         * Obtain script function object for a specific global scope object.
2e3bd98c5664 8041697: CompiledScript slower when eval with binding
sundar
parents: 24206
diff changeset
   563
         *
2e3bd98c5664 8041697: CompiledScript slower when eval with binding
sundar
parents: 24206
diff changeset
   564
         * @param newGlobal global scope for which function object is obtained
2e3bd98c5664 8041697: CompiledScript slower when eval with binding
sundar
parents: 24206
diff changeset
   565
         * @return script function for script level expressions
2e3bd98c5664 8041697: CompiledScript slower when eval with binding
sundar
parents: 24206
diff changeset
   566
         */
2e3bd98c5664 8041697: CompiledScript slower when eval with binding
sundar
parents: 24206
diff changeset
   567
        public ScriptFunction getFunction(final Global newGlobal);
2e3bd98c5664 8041697: CompiledScript slower when eval with binding
sundar
parents: 24206
diff changeset
   568
    }
2e3bd98c5664 8041697: CompiledScript slower when eval with binding
sundar
parents: 24206
diff changeset
   569
2e3bd98c5664 8041697: CompiledScript slower when eval with binding
sundar
parents: 24206
diff changeset
   570
    /**
2e3bd98c5664 8041697: CompiledScript slower when eval with binding
sundar
parents: 24206
diff changeset
   571
     * Compile a top level script.
2e3bd98c5664 8041697: CompiledScript slower when eval with binding
sundar
parents: 24206
diff changeset
   572
     *
2e3bd98c5664 8041697: CompiledScript slower when eval with binding
sundar
parents: 24206
diff changeset
   573
     * @param source the script source
2e3bd98c5664 8041697: CompiledScript slower when eval with binding
sundar
parents: 24206
diff changeset
   574
     * @return reusable compiled script across many global scopes.
2e3bd98c5664 8041697: CompiledScript slower when eval with binding
sundar
parents: 24206
diff changeset
   575
     */
2e3bd98c5664 8041697: CompiledScript slower when eval with binding
sundar
parents: 24206
diff changeset
   576
    public MultiGlobalCompiledScript compileScript(final Source source) {
2e3bd98c5664 8041697: CompiledScript slower when eval with binding
sundar
parents: 24206
diff changeset
   577
        final Class<?> clazz = compile(source, this.errors, this._strict);
24783
b5c31bfe1496 8046014: MultiGlobalCompiledScript should cache :createProgramFunction handle
attila
parents: 24779
diff changeset
   578
        final MethodHandle createProgramFunctionHandle = getCreateProgramFunctionHandle(clazz);
24282
2e3bd98c5664 8041697: CompiledScript slower when eval with binding
sundar
parents: 24206
diff changeset
   579
2e3bd98c5664 8041697: CompiledScript slower when eval with binding
sundar
parents: 24206
diff changeset
   580
        return new MultiGlobalCompiledScript() {
2e3bd98c5664 8041697: CompiledScript slower when eval with binding
sundar
parents: 24206
diff changeset
   581
            @Override
2e3bd98c5664 8041697: CompiledScript slower when eval with binding
sundar
parents: 24206
diff changeset
   582
            public ScriptFunction getFunction(final Global newGlobal) {
24783
b5c31bfe1496 8046014: MultiGlobalCompiledScript should cache :createProgramFunction handle
attila
parents: 24779
diff changeset
   583
                return invokeCreateProgramFunctionHandle(createProgramFunctionHandle, newGlobal);
24282
2e3bd98c5664 8041697: CompiledScript slower when eval with binding
sundar
parents: 24206
diff changeset
   584
            }
2e3bd98c5664 8041697: CompiledScript slower when eval with binding
sundar
parents: 24206
diff changeset
   585
        };
2e3bd98c5664 8041697: CompiledScript slower when eval with binding
sundar
parents: 24206
diff changeset
   586
    }
2e3bd98c5664 8041697: CompiledScript slower when eval with binding
sundar
parents: 24206
diff changeset
   587
2e3bd98c5664 8041697: CompiledScript slower when eval with binding
sundar
parents: 24206
diff changeset
   588
    /**
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   589
     * Entry point for {@code eval}
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   590
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   591
     * @param initialScope The scope of this eval call
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   592
     * @param string       Evaluated code as a String
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   593
     * @param callThis     "this" to be passed to the evaluated code
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   594
     * @param location     location of the eval call
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   595
     * @param strict       is this {@code eval} call from a strict mode code?
25240
f92c14b1ca11 8047959: bindings created for declarations in eval code are not mutable
sundar
parents: 24993
diff changeset
   596
     * @return the return value of the {@code eval}
f92c14b1ca11 8047959: bindings created for declarations in eval code are not mutable
sundar
parents: 24993
diff changeset
   597
     */
f92c14b1ca11 8047959: bindings created for declarations in eval code are not mutable
sundar
parents: 24993
diff changeset
   598
    public Object eval(final ScriptObject initialScope, final String string,
f92c14b1ca11 8047959: bindings created for declarations in eval code are not mutable
sundar
parents: 24993
diff changeset
   599
            final Object callThis, final Object location, final boolean strict) {
f92c14b1ca11 8047959: bindings created for declarations in eval code are not mutable
sundar
parents: 24993
diff changeset
   600
        return eval(initialScope, string, callThis, location, strict, false);
f92c14b1ca11 8047959: bindings created for declarations in eval code are not mutable
sundar
parents: 24993
diff changeset
   601
    }
f92c14b1ca11 8047959: bindings created for declarations in eval code are not mutable
sundar
parents: 24993
diff changeset
   602
f92c14b1ca11 8047959: bindings created for declarations in eval code are not mutable
sundar
parents: 24993
diff changeset
   603
    /**
f92c14b1ca11 8047959: bindings created for declarations in eval code are not mutable
sundar
parents: 24993
diff changeset
   604
     * Entry point for {@code eval}
f92c14b1ca11 8047959: bindings created for declarations in eval code are not mutable
sundar
parents: 24993
diff changeset
   605
     *
f92c14b1ca11 8047959: bindings created for declarations in eval code are not mutable
sundar
parents: 24993
diff changeset
   606
     * @param initialScope The scope of this eval call
f92c14b1ca11 8047959: bindings created for declarations in eval code are not mutable
sundar
parents: 24993
diff changeset
   607
     * @param string       Evaluated code as a String
f92c14b1ca11 8047959: bindings created for declarations in eval code are not mutable
sundar
parents: 24993
diff changeset
   608
     * @param callThis     "this" to be passed to the evaluated code
f92c14b1ca11 8047959: bindings created for declarations in eval code are not mutable
sundar
parents: 24993
diff changeset
   609
     * @param location     location of the eval call
f92c14b1ca11 8047959: bindings created for declarations in eval code are not mutable
sundar
parents: 24993
diff changeset
   610
     * @param strict       is this {@code eval} call from a strict mode code?
f92c14b1ca11 8047959: bindings created for declarations in eval code are not mutable
sundar
parents: 24993
diff changeset
   611
     * @param evalCall     is this called from "eval" builtin?
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   612
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   613
     * @return the return value of the {@code eval}
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   614
     */
25240
f92c14b1ca11 8047959: bindings created for declarations in eval code are not mutable
sundar
parents: 24993
diff changeset
   615
    public Object eval(final ScriptObject initialScope, final String string,
f92c14b1ca11 8047959: bindings created for declarations in eval code are not mutable
sundar
parents: 24993
diff changeset
   616
            final Object callThis, final Object location, final boolean strict, final boolean evalCall) {
24725
7bb1f687a852 8033334: Make sure that scope depth information is maintained in the RecompilableScriptFunctionDatas, to avoid unnecessary slow proto linkage when doing on demand compilation
lagergren
parents: 24719
diff changeset
   617
        final String  file       = location == UNDEFINED || location == null ? "<eval>" : location.toString();
25240
f92c14b1ca11 8047959: bindings created for declarations in eval code are not mutable
sundar
parents: 24993
diff changeset
   618
        final Source  source     = sourceFor(file, string, evalCall);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   619
        final boolean directEval = location != UNDEFINED; // is this direct 'eval' call or indirectly invoked eval?
23375
a1110f2cbe75 8037400: Remove getInitialMap getters and GlobalObject interface
sundar
parents: 23374
diff changeset
   620
        final Global  global = Context.getGlobal();
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   621
        ScriptObject scope = initialScope;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   622
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   623
        // ECMA section 10.1.1 point 2 says eval code is strict if it begins
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   624
        // with "use strict" directive or eval direct call itself is made
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   625
        // from from strict mode code. We are passed with caller's strict mode.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   626
        boolean strictFlag = directEval && strict;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   627
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   628
        Class<?> clazz = null;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   629
        try {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   630
            clazz = compile(source, new ThrowErrorManager(), strictFlag);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   631
        } catch (final ParserException e) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   632
            e.throwAsEcmaException(global);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   633
            return null;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   634
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   635
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   636
        if (!strictFlag) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   637
            // We need to get strict mode flag from compiled class. This is
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   638
            // because eval code may start with "use strict" directive.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   639
            try {
17233
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 17231
diff changeset
   640
                strictFlag = clazz.getField(STRICT_MODE.symbolName()).getBoolean(null);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   641
            } catch (final NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException e) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   642
                //ignored
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   643
                strictFlag = false;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   644
            }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   645
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   646
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   647
        // In strict mode, eval does not instantiate variables and functions
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   648
        // in the caller's environment. A new environment is created!
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   649
        if (strictFlag) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   650
            // Create a new scope object
23375
a1110f2cbe75 8037400: Remove getInitialMap getters and GlobalObject interface
sundar
parents: 23374
diff changeset
   651
            final ScriptObject strictEvalScope = global.newObject();
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   652
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   653
            // bless it as a "scope"
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   654
            strictEvalScope.setIsScope();
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   655
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   656
            // set given scope to be it's proto so that eval can still
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   657
            // access caller environment vars in the new environment.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   658
            strictEvalScope.setProto(scope);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   659
            scope = strictEvalScope;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   660
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   661
24725
7bb1f687a852 8033334: Make sure that scope depth information is maintained in the RecompilableScriptFunctionDatas, to avoid unnecessary slow proto linkage when doing on demand compilation
lagergren
parents: 24719
diff changeset
   662
        final ScriptFunction func = getProgramFunction(clazz, scope);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   663
        Object evalThis;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   664
        if (directEval) {
24725
7bb1f687a852 8033334: Make sure that scope depth information is maintained in the RecompilableScriptFunctionDatas, to avoid unnecessary slow proto linkage when doing on demand compilation
lagergren
parents: 24719
diff changeset
   665
            evalThis = callThis instanceof ScriptObject || strictFlag ? callThis : global;
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   666
        } else {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   667
            evalThis = global;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   668
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   669
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   670
        return ScriptRuntime.apply(func, evalThis);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   671
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   672
17518
2225a4f929c0 8013477: Node.setSymbol needs to be copy on write - enable IR snapshots for recompilation based on callsite type specialization. [not enabled by default, hidden by a flag for now]
lagergren
parents: 17513
diff changeset
   673
    private static Source loadInternal(final String srcStr, final String prefix, final String resourcePath) {
17244
041afba4cec5 8012251: jjs should support -fx option
jlaskey
parents: 17233
diff changeset
   674
        if (srcStr.startsWith(prefix)) {
041afba4cec5 8012251: jjs should support -fx option
jlaskey
parents: 17233
diff changeset
   675
            final String resource = resourcePath + srcStr.substring(prefix.length());
041afba4cec5 8012251: jjs should support -fx option
jlaskey
parents: 17233
diff changeset
   676
            // NOTE: even sandbox scripts should be able to load scripts in nashorn: scheme
041afba4cec5 8012251: jjs should support -fx option
jlaskey
parents: 17233
diff changeset
   677
            // These scripts are always available and are loaded from nashorn.jar's resources.
041afba4cec5 8012251: jjs should support -fx option
jlaskey
parents: 17233
diff changeset
   678
            return AccessController.doPrivileged(
041afba4cec5 8012251: jjs should support -fx option
jlaskey
parents: 17233
diff changeset
   679
                    new PrivilegedAction<Source>() {
041afba4cec5 8012251: jjs should support -fx option
jlaskey
parents: 17233
diff changeset
   680
                        @Override
041afba4cec5 8012251: jjs should support -fx option
jlaskey
parents: 17233
diff changeset
   681
                        public Source run() {
041afba4cec5 8012251: jjs should support -fx option
jlaskey
parents: 17233
diff changeset
   682
                            try {
041afba4cec5 8012251: jjs should support -fx option
jlaskey
parents: 17233
diff changeset
   683
                                final URL resURL = Context.class.getResource(resource);
24769
attila
parents: 24759 24282
diff changeset
   684
                                return resURL != null ? sourceFor(srcStr, resURL) : null;
17244
041afba4cec5 8012251: jjs should support -fx option
jlaskey
parents: 17233
diff changeset
   685
                            } catch (final IOException exp) {
041afba4cec5 8012251: jjs should support -fx option
jlaskey
parents: 17233
diff changeset
   686
                                return null;
041afba4cec5 8012251: jjs should support -fx option
jlaskey
parents: 17233
diff changeset
   687
                            }
041afba4cec5 8012251: jjs should support -fx option
jlaskey
parents: 17233
diff changeset
   688
                        }
041afba4cec5 8012251: jjs should support -fx option
jlaskey
parents: 17233
diff changeset
   689
                    });
041afba4cec5 8012251: jjs should support -fx option
jlaskey
parents: 17233
diff changeset
   690
        }
041afba4cec5 8012251: jjs should support -fx option
jlaskey
parents: 17233
diff changeset
   691
041afba4cec5 8012251: jjs should support -fx option
jlaskey
parents: 17233
diff changeset
   692
        return null;
041afba4cec5 8012251: jjs should support -fx option
jlaskey
parents: 17233
diff changeset
   693
    }
041afba4cec5 8012251: jjs should support -fx option
jlaskey
parents: 17233
diff changeset
   694
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   695
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   696
     * Implementation of {@code load} Nashorn extension. Load a script file from a source
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   697
     * expression
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   698
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   699
     * @param scope  the scope
16230
c38c724d82e7 8008103: Source object should maintain URL of the script source as a private field
sundar
parents: 16226
diff changeset
   700
     * @param from   source expression for script
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   701
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   702
     * @return return value for load call (undefined)
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   703
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   704
     * @throws IOException if source cannot be found or loaded
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   705
     */
16230
c38c724d82e7 8008103: Source object should maintain URL of the script source as a private field
sundar
parents: 16226
diff changeset
   706
    public Object load(final ScriptObject scope, final Object from) throws IOException {
24725
7bb1f687a852 8033334: Make sure that scope depth information is maintained in the RecompilableScriptFunctionDatas, to avoid unnecessary slow proto linkage when doing on demand compilation
lagergren
parents: 24719
diff changeset
   707
        final Object src = from instanceof ConsString ? from.toString() : from;
16230
c38c724d82e7 8008103: Source object should maintain URL of the script source as a private field
sundar
parents: 16226
diff changeset
   708
        Source source = null;
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   709
16230
c38c724d82e7 8008103: Source object should maintain URL of the script source as a private field
sundar
parents: 16226
diff changeset
   710
        // load accepts a String (which could be a URL or a file name), a File, a URL
c38c724d82e7 8008103: Source object should maintain URL of the script source as a private field
sundar
parents: 16226
diff changeset
   711
        // or a ScriptObject that has "name" and "source" (string valued) properties.
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   712
        if (src instanceof String) {
16251
4a5d15b2f168 8008166: URL handling was broken on windows, causing "load" to malfunction
lagergren
parents: 16245
diff changeset
   713
            final String srcStr = (String)src;
20564
f353da961684 8025629: load function should support a way to load scripts from classpath
sundar
parents: 19895
diff changeset
   714
            if (srcStr.startsWith(LOAD_CLASSPATH)) {
24725
7bb1f687a852 8033334: Make sure that scope depth information is maintained in the RecompilableScriptFunctionDatas, to avoid unnecessary slow proto linkage when doing on demand compilation
lagergren
parents: 24719
diff changeset
   715
                final URL url = getResourceURL(srcStr.substring(LOAD_CLASSPATH.length()));
24769
attila
parents: 24759 24282
diff changeset
   716
                source = url != null ? sourceFor(url.toString(), url) : null;
20564
f353da961684 8025629: load function should support a way to load scripts from classpath
sundar
parents: 19895
diff changeset
   717
            } else {
f353da961684 8025629: load function should support a way to load scripts from classpath
sundar
parents: 19895
diff changeset
   718
                final File file = new File(srcStr);
f353da961684 8025629: load function should support a way to load scripts from classpath
sundar
parents: 19895
diff changeset
   719
                if (srcStr.indexOf(':') != -1) {
f353da961684 8025629: load function should support a way to load scripts from classpath
sundar
parents: 19895
diff changeset
   720
                    if ((source = loadInternal(srcStr, LOAD_NASHORN, "resources/")) == null &&
f353da961684 8025629: load function should support a way to load scripts from classpath
sundar
parents: 19895
diff changeset
   721
                        (source = loadInternal(srcStr, LOAD_FX, "resources/fx/")) == null) {
f353da961684 8025629: load function should support a way to load scripts from classpath
sundar
parents: 19895
diff changeset
   722
                        URL url;
f353da961684 8025629: load function should support a way to load scripts from classpath
sundar
parents: 19895
diff changeset
   723
                        try {
f353da961684 8025629: load function should support a way to load scripts from classpath
sundar
parents: 19895
diff changeset
   724
                            //check for malformed url. if malformed, it may still be a valid file
f353da961684 8025629: load function should support a way to load scripts from classpath
sundar
parents: 19895
diff changeset
   725
                            url = new URL(srcStr);
f353da961684 8025629: load function should support a way to load scripts from classpath
sundar
parents: 19895
diff changeset
   726
                        } catch (final MalformedURLException e) {
f353da961684 8025629: load function should support a way to load scripts from classpath
sundar
parents: 19895
diff changeset
   727
                            url = file.toURI().toURL();
f353da961684 8025629: load function should support a way to load scripts from classpath
sundar
parents: 19895
diff changeset
   728
                        }
24206
40c6d45af73f 8040078: Avoid repeated reading of source for cached loads
hannesw
parents: 23767
diff changeset
   729
                        source = sourceFor(url.toString(), url);
16254
2ed824fc93be 8008554: load was broken for URLs
lagergren
parents: 16251
diff changeset
   730
                    }
20564
f353da961684 8025629: load function should support a way to load scripts from classpath
sundar
parents: 19895
diff changeset
   731
                } else if (file.isFile()) {
24206
40c6d45af73f 8040078: Avoid repeated reading of source for cached loads
hannesw
parents: 23767
diff changeset
   732
                    source = sourceFor(srcStr, file);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   733
                }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   734
            }
16230
c38c724d82e7 8008103: Source object should maintain URL of the script source as a private field
sundar
parents: 16226
diff changeset
   735
        } else if (src instanceof File && ((File)src).isFile()) {
c38c724d82e7 8008103: Source object should maintain URL of the script source as a private field
sundar
parents: 16226
diff changeset
   736
            final File file = (File)src;
24206
40c6d45af73f 8040078: Avoid repeated reading of source for cached loads
hannesw
parents: 23767
diff changeset
   737
            source = sourceFor(file.getName(), file);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   738
        } else if (src instanceof URL) {
16230
c38c724d82e7 8008103: Source object should maintain URL of the script source as a private field
sundar
parents: 16226
diff changeset
   739
            final URL url = (URL)src;
24206
40c6d45af73f 8040078: Avoid repeated reading of source for cached loads
hannesw
parents: 23767
diff changeset
   740
            source = sourceFor(url.toString(), url);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   741
        } else if (src instanceof ScriptObject) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   742
            final ScriptObject sobj = (ScriptObject)src;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   743
            if (sobj.has("script") && sobj.has("name")) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   744
                final String script = JSType.toString(sobj.get("script"));
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   745
                final String name   = JSType.toString(sobj.get("name"));
24206
40c6d45af73f 8040078: Avoid repeated reading of source for cached loads
hannesw
parents: 23767
diff changeset
   746
                source = sourceFor(name, script);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   747
            }
18334
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 18328
diff changeset
   748
        } else if (src instanceof Map) {
19097
f544a2ea40ef 8021262: Make nashorn access checks consistent with underlying dynalink
sundar
parents: 19088
diff changeset
   749
            final Map<?,?> map = (Map<?,?>)src;
18334
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 18328
diff changeset
   750
            if (map.containsKey("script") && map.containsKey("name")) {
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 18328
diff changeset
   751
                final String script = JSType.toString(map.get("script"));
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 18328
diff changeset
   752
                final String name   = JSType.toString(map.get("name"));
24206
40c6d45af73f 8040078: Avoid repeated reading of source for cached loads
hannesw
parents: 23767
diff changeset
   753
                source = sourceFor(name, script);
18334
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 18328
diff changeset
   754
            }
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   755
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   756
16230
c38c724d82e7 8008103: Source object should maintain URL of the script source as a private field
sundar
parents: 16226
diff changeset
   757
        if (source != null) {
c38c724d82e7 8008103: Source object should maintain URL of the script source as a private field
sundar
parents: 16226
diff changeset
   758
            return evaluateSource(source, scope, scope);
c38c724d82e7 8008103: Source object should maintain URL of the script source as a private field
sundar
parents: 16226
diff changeset
   759
        }
c38c724d82e7 8008103: Source object should maintain URL of the script source as a private field
sundar
parents: 16226
diff changeset
   760
16256
f2d9a0c49914 8007002: Replace implicit exception throwing methods with explicit throws - simplify control flow and remove useless code
lagergren
parents: 16254
diff changeset
   761
        throw typeError("cant.load.script", ScriptRuntime.safeToString(from));
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   762
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   763
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   764
    /**
17974
a2818f17324e 8015741: Need a global.load function that starts with a new global scope.
jlaskey
parents: 17524
diff changeset
   765
     * Implementation of {@code loadWithNewGlobal} Nashorn extension. Load a script file from a source
a2818f17324e 8015741: Need a global.load function that starts with a new global scope.
jlaskey
parents: 17524
diff changeset
   766
     * expression, after creating a new global scope.
a2818f17324e 8015741: Need a global.load function that starts with a new global scope.
jlaskey
parents: 17524
diff changeset
   767
     *
a2818f17324e 8015741: Need a global.load function that starts with a new global scope.
jlaskey
parents: 17524
diff changeset
   768
     * @param from source expression for script
18321
b2ceadf211cb 8016239: loadWithNewGlobal should support user supplied arguments from the caller
sundar
parents: 17982
diff changeset
   769
     * @param args (optional) arguments to be passed to the loaded script
17974
a2818f17324e 8015741: Need a global.load function that starts with a new global scope.
jlaskey
parents: 17524
diff changeset
   770
     *
a2818f17324e 8015741: Need a global.load function that starts with a new global scope.
jlaskey
parents: 17524
diff changeset
   771
     * @return return value for load call (undefined)
a2818f17324e 8015741: Need a global.load function that starts with a new global scope.
jlaskey
parents: 17524
diff changeset
   772
     *
a2818f17324e 8015741: Need a global.load function that starts with a new global scope.
jlaskey
parents: 17524
diff changeset
   773
     * @throws IOException if source cannot be found or loaded
a2818f17324e 8015741: Need a global.load function that starts with a new global scope.
jlaskey
parents: 17524
diff changeset
   774
     */
18321
b2ceadf211cb 8016239: loadWithNewGlobal should support user supplied arguments from the caller
sundar
parents: 17982
diff changeset
   775
    public Object loadWithNewGlobal(final Object from, final Object...args) throws IOException {
23375
a1110f2cbe75 8037400: Remove getInitialMap getters and GlobalObject interface
sundar
parents: 23374
diff changeset
   776
        final Global oldGlobal = getGlobal();
a1110f2cbe75 8037400: Remove getInitialMap getters and GlobalObject interface
sundar
parents: 23374
diff changeset
   777
        final Global newGlobal = AccessController.doPrivileged(new PrivilegedAction<Global>() {
17976
5615a31822a1 8015814: loadWithNewGlobal needs to wrap createGlobal in AccessController.doPrivileged
jlaskey
parents: 17974
diff changeset
   778
           @Override
23375
a1110f2cbe75 8037400: Remove getInitialMap getters and GlobalObject interface
sundar
parents: 23374
diff changeset
   779
           public Global run() {
17976
5615a31822a1 8015814: loadWithNewGlobal needs to wrap createGlobal in AccessController.doPrivileged
jlaskey
parents: 17974
diff changeset
   780
               try {
19459
79e75274df99 8022707: Revisit all doPrivileged blocks
sundar
parents: 19098
diff changeset
   781
                   return newGlobal();
17976
5615a31822a1 8015814: loadWithNewGlobal needs to wrap createGlobal in AccessController.doPrivileged
jlaskey
parents: 17974
diff changeset
   782
               } catch (final RuntimeException e) {
5615a31822a1 8015814: loadWithNewGlobal needs to wrap createGlobal in AccessController.doPrivileged
jlaskey
parents: 17974
diff changeset
   783
                   if (Context.DEBUG) {
5615a31822a1 8015814: loadWithNewGlobal needs to wrap createGlobal in AccessController.doPrivileged
jlaskey
parents: 17974
diff changeset
   784
                       e.printStackTrace();
5615a31822a1 8015814: loadWithNewGlobal needs to wrap createGlobal in AccessController.doPrivileged
jlaskey
parents: 17974
diff changeset
   785
                   }
5615a31822a1 8015814: loadWithNewGlobal needs to wrap createGlobal in AccessController.doPrivileged
jlaskey
parents: 17974
diff changeset
   786
                   throw e;
5615a31822a1 8015814: loadWithNewGlobal needs to wrap createGlobal in AccessController.doPrivileged
jlaskey
parents: 17974
diff changeset
   787
               }
5615a31822a1 8015814: loadWithNewGlobal needs to wrap createGlobal in AccessController.doPrivileged
jlaskey
parents: 17974
diff changeset
   788
           }
19459
79e75274df99 8022707: Revisit all doPrivileged blocks
sundar
parents: 19098
diff changeset
   789
        }, CREATE_GLOBAL_ACC_CTXT);
79e75274df99 8022707: Revisit all doPrivileged blocks
sundar
parents: 19098
diff changeset
   790
        // initialize newly created Global instance
79e75274df99 8022707: Revisit all doPrivileged blocks
sundar
parents: 19098
diff changeset
   791
        initGlobal(newGlobal);
23375
a1110f2cbe75 8037400: Remove getInitialMap getters and GlobalObject interface
sundar
parents: 23374
diff changeset
   792
        setGlobal(newGlobal);
17974
a2818f17324e 8015741: Need a global.load function that starts with a new global scope.
jlaskey
parents: 17524
diff changeset
   793
18874
8ba96bd382d3 8020463: Input argument array wrapping in loadWithNewGlobal is wrong
sundar
parents: 18865
diff changeset
   794
        final Object[] wrapped = args == null? ScriptRuntime.EMPTY_ARRAY :  ScriptObjectMirror.wrapArray(args, oldGlobal);
23375
a1110f2cbe75 8037400: Remove getInitialMap getters and GlobalObject interface
sundar
parents: 23374
diff changeset
   795
        newGlobal.put("arguments", newGlobal.wrapAsObject(wrapped), env._strict);
18321
b2ceadf211cb 8016239: loadWithNewGlobal should support user supplied arguments from the caller
sundar
parents: 17982
diff changeset
   796
17974
a2818f17324e 8015741: Need a global.load function that starts with a new global scope.
jlaskey
parents: 17524
diff changeset
   797
        try {
18874
8ba96bd382d3 8020463: Input argument array wrapping in loadWithNewGlobal is wrong
sundar
parents: 18865
diff changeset
   798
            // wrap objects from newGlobal's world as mirrors - but if result
8ba96bd382d3 8020463: Input argument array wrapping in loadWithNewGlobal is wrong
sundar
parents: 18865
diff changeset
   799
            // is from oldGlobal's world, unwrap it!
8ba96bd382d3 8020463: Input argument array wrapping in loadWithNewGlobal is wrong
sundar
parents: 18865
diff changeset
   800
            return ScriptObjectMirror.unwrap(ScriptObjectMirror.wrap(load(newGlobal, from), newGlobal), oldGlobal);
17974
a2818f17324e 8015741: Need a global.load function that starts with a new global scope.
jlaskey
parents: 17524
diff changeset
   801
        } finally {
23375
a1110f2cbe75 8037400: Remove getInitialMap getters and GlobalObject interface
sundar
parents: 23374
diff changeset
   802
            setGlobal(oldGlobal);
17974
a2818f17324e 8015741: Need a global.load function that starts with a new global scope.
jlaskey
parents: 17524
diff changeset
   803
        }
a2818f17324e 8015741: Need a global.load function that starts with a new global scope.
jlaskey
parents: 17524
diff changeset
   804
    }
a2818f17324e 8015741: Need a global.load function that starts with a new global scope.
jlaskey
parents: 17524
diff changeset
   805
a2818f17324e 8015741: Need a global.load function that starts with a new global scope.
jlaskey
parents: 17524
diff changeset
   806
    /**
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   807
     * Load or get a structure class. Structure class names are based on the number of parameter fields
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   808
     * and {@link AccessorProperty} fields in them. Structure classes are used to represent ScriptObjects
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   809
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   810
     * @see ObjectClassGenerator
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   811
     * @see AccessorProperty
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   812
     * @see ScriptObject
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   813
     *
16275
d5d430071b22 8009379: Remove $ from generated class names
jlaskey
parents: 16272
diff changeset
   814
     * @param fullName  full name of class, e.g. jdk.nashorn.internal.objects.JO2P1 contains 2 fields and 1 parameter.
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   815
     *
16272
675a0caf75bc 8009263: Fix all javadoc errors in nashorn code
sundar
parents: 16262
diff changeset
   816
     * @return the {@code Class<?>} for this structure
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   817
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   818
     * @throws ClassNotFoundException if structure class cannot be resolved
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   819
     */
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 23076
diff changeset
   820
    @SuppressWarnings("unchecked")
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 23076
diff changeset
   821
    public static Class<? extends ScriptObject> forStructureClass(final String fullName) throws ClassNotFoundException {
19895
965b12eb322e 8024619: JDBC java.sql.DriverManager is not usable from JS script
sundar
parents: 19884
diff changeset
   822
        if (System.getSecurityManager() != null && !StructureLoader.isStructureClass(fullName)) {
19097
f544a2ea40ef 8021262: Make nashorn access checks consistent with underlying dynalink
sundar
parents: 19088
diff changeset
   823
            throw new ClassNotFoundException(fullName);
f544a2ea40ef 8021262: Make nashorn access checks consistent with underlying dynalink
sundar
parents: 19088
diff changeset
   824
        }
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 23076
diff changeset
   825
        return (Class<? extends ScriptObject>)Class.forName(fullName, true, sharedLoader);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   826
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   827
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   828
    /**
20567
5621fc356049 8025771: Enhance Nashorn Contexts
sundar
parents: 20564
diff changeset
   829
     * Checks that the given Class can be accessed from no permissions context.
18865
8844964e5fc5 8020325: static property does not work on accessible, public classes
sundar
parents: 18864
diff changeset
   830
     *
20567
5621fc356049 8025771: Enhance Nashorn Contexts
sundar
parents: 20564
diff changeset
   831
     * @param clazz Class object
23374
f470afc89c6c 8015958: DataView constructor is not defined
sundar
parents: 23372
diff changeset
   832
     * @throws SecurityException if not accessible
18865
8844964e5fc5 8020325: static property does not work on accessible, public classes
sundar
parents: 18864
diff changeset
   833
     */
20928
3ff39d5c8c08 8026137: Fix Issues with Binary Evaluation Order
lagergren
parents: 20567
diff changeset
   834
    public static void checkPackageAccess(final Class<?> clazz) {
20567
5621fc356049 8025771: Enhance Nashorn Contexts
sundar
parents: 20564
diff changeset
   835
        final SecurityManager sm = System.getSecurityManager();
5621fc356049 8025771: Enhance Nashorn Contexts
sundar
parents: 20564
diff changeset
   836
        if (sm != null) {
20928
3ff39d5c8c08 8026137: Fix Issues with Binary Evaluation Order
lagergren
parents: 20567
diff changeset
   837
            Class<?> bottomClazz = clazz;
3ff39d5c8c08 8026137: Fix Issues with Binary Evaluation Order
lagergren
parents: 20567
diff changeset
   838
            while (bottomClazz.isArray()) {
20567
5621fc356049 8025771: Enhance Nashorn Contexts
sundar
parents: 20564
diff changeset
   839
                bottomClazz = bottomClazz.getComponentType();
18865
8844964e5fc5 8020325: static property does not work on accessible, public classes
sundar
parents: 18864
diff changeset
   840
            }
20567
5621fc356049 8025771: Enhance Nashorn Contexts
sundar
parents: 20564
diff changeset
   841
            checkPackageAccess(sm, bottomClazz.getName());
18865
8844964e5fc5 8020325: static property does not work on accessible, public classes
sundar
parents: 18864
diff changeset
   842
        }
8844964e5fc5 8020325: static property does not work on accessible, public classes
sundar
parents: 18864
diff changeset
   843
    }
8844964e5fc5 8020325: static property does not work on accessible, public classes
sundar
parents: 18864
diff changeset
   844
8844964e5fc5 8020325: static property does not work on accessible, public classes
sundar
parents: 18864
diff changeset
   845
    /**
22669
75563515567f 8032681: Issues with Nashorn
attila
parents: 22389
diff changeset
   846
     * Checks that the given package name can be accessed from no permissions context.
75563515567f 8032681: Issues with Nashorn
attila
parents: 22389
diff changeset
   847
     *
75563515567f 8032681: Issues with Nashorn
attila
parents: 22389
diff changeset
   848
     * @param pkgName package name
23374
f470afc89c6c 8015958: DataView constructor is not defined
sundar
parents: 23372
diff changeset
   849
     * @throws SecurityException if not accessible
22669
75563515567f 8032681: Issues with Nashorn
attila
parents: 22389
diff changeset
   850
     */
75563515567f 8032681: Issues with Nashorn
attila
parents: 22389
diff changeset
   851
    public static void checkPackageAccess(final String pkgName) {
75563515567f 8032681: Issues with Nashorn
attila
parents: 22389
diff changeset
   852
        final SecurityManager sm = System.getSecurityManager();
75563515567f 8032681: Issues with Nashorn
attila
parents: 22389
diff changeset
   853
        if (sm != null) {
23372
09707b3e5fb0 8021350: Share script classes between threads/globals within context
hannesw
parents: 23076
diff changeset
   854
            checkPackageAccess(sm, pkgName.endsWith(".") ? pkgName : pkgName + ".");
22669
75563515567f 8032681: Issues with Nashorn
attila
parents: 22389
diff changeset
   855
        }
75563515567f 8032681: Issues with Nashorn
attila
parents: 22389
diff changeset
   856
    }
75563515567f 8032681: Issues with Nashorn
attila
parents: 22389
diff changeset
   857
75563515567f 8032681: Issues with Nashorn
attila
parents: 22389
diff changeset
   858
    /**
19097
f544a2ea40ef 8021262: Make nashorn access checks consistent with underlying dynalink
sundar
parents: 19088
diff changeset
   859
     * Checks that the given package can be accessed from no permissions context.
19088
153f268bfa72 8021122: Not all callables are handled for toString and other function valued properties
sundar
parents: 19085
diff changeset
   860
     *
20567
5621fc356049 8025771: Enhance Nashorn Contexts
sundar
parents: 20564
diff changeset
   861
     * @param sm current security manager instance
19088
153f268bfa72 8021122: Not all callables are handled for toString and other function valued properties
sundar
parents: 19085
diff changeset
   862
     * @param fullName fully qualified package name
20567
5621fc356049 8025771: Enhance Nashorn Contexts
sundar
parents: 20564
diff changeset
   863
     * @throw SecurityException if not accessible
5621fc356049 8025771: Enhance Nashorn Contexts
sundar
parents: 20564
diff changeset
   864
     */
5621fc356049 8025771: Enhance Nashorn Contexts
sundar
parents: 20564
diff changeset
   865
    private static void checkPackageAccess(final SecurityManager sm, final String fullName) {
5621fc356049 8025771: Enhance Nashorn Contexts
sundar
parents: 20564
diff changeset
   866
        sm.getClass(); // null check
5621fc356049 8025771: Enhance Nashorn Contexts
sundar
parents: 20564
diff changeset
   867
        final int index = fullName.lastIndexOf('.');
5621fc356049 8025771: Enhance Nashorn Contexts
sundar
parents: 20564
diff changeset
   868
        if (index != -1) {
5621fc356049 8025771: Enhance Nashorn Contexts
sundar
parents: 20564
diff changeset
   869
            final String pkgName = fullName.substring(0, index);
5621fc356049 8025771: Enhance Nashorn Contexts
sundar
parents: 20564
diff changeset
   870
            AccessController.doPrivileged(new PrivilegedAction<Void>() {
5621fc356049 8025771: Enhance Nashorn Contexts
sundar
parents: 20564
diff changeset
   871
                @Override
5621fc356049 8025771: Enhance Nashorn Contexts
sundar
parents: 20564
diff changeset
   872
                public Void run() {
5621fc356049 8025771: Enhance Nashorn Contexts
sundar
parents: 20564
diff changeset
   873
                    sm.checkPackageAccess(pkgName);
5621fc356049 8025771: Enhance Nashorn Contexts
sundar
parents: 20564
diff changeset
   874
                    return null;
5621fc356049 8025771: Enhance Nashorn Contexts
sundar
parents: 20564
diff changeset
   875
                }
5621fc356049 8025771: Enhance Nashorn Contexts
sundar
parents: 20564
diff changeset
   876
            }, NO_PERMISSIONS_ACC_CTXT);
5621fc356049 8025771: Enhance Nashorn Contexts
sundar
parents: 20564
diff changeset
   877
        }
5621fc356049 8025771: Enhance Nashorn Contexts
sundar
parents: 20564
diff changeset
   878
    }
5621fc356049 8025771: Enhance Nashorn Contexts
sundar
parents: 20564
diff changeset
   879
5621fc356049 8025771: Enhance Nashorn Contexts
sundar
parents: 20564
diff changeset
   880
    /**
5621fc356049 8025771: Enhance Nashorn Contexts
sundar
parents: 20564
diff changeset
   881
     * Checks that the given Class can be accessed from no permissions context.
5621fc356049 8025771: Enhance Nashorn Contexts
sundar
parents: 20564
diff changeset
   882
     *
5621fc356049 8025771: Enhance Nashorn Contexts
sundar
parents: 20564
diff changeset
   883
     * @param clazz Class object
19088
153f268bfa72 8021122: Not all callables are handled for toString and other function valued properties
sundar
parents: 19085
diff changeset
   884
     * @return true if package is accessible, false otherwise
153f268bfa72 8021122: Not all callables are handled for toString and other function valued properties
sundar
parents: 19085
diff changeset
   885
     */
20928
3ff39d5c8c08 8026137: Fix Issues with Binary Evaluation Order
lagergren
parents: 20567
diff changeset
   886
    private static boolean isAccessiblePackage(final Class<?> clazz) {
19088
153f268bfa72 8021122: Not all callables are handled for toString and other function valued properties
sundar
parents: 19085
diff changeset
   887
        try {
20567
5621fc356049 8025771: Enhance Nashorn Contexts
sundar
parents: 20564
diff changeset
   888
            checkPackageAccess(clazz);
19088
153f268bfa72 8021122: Not all callables are handled for toString and other function valued properties
sundar
parents: 19085
diff changeset
   889
            return true;
153f268bfa72 8021122: Not all callables are handled for toString and other function valued properties
sundar
parents: 19085
diff changeset
   890
        } catch (final SecurityException se) {
153f268bfa72 8021122: Not all callables are handled for toString and other function valued properties
sundar
parents: 19085
diff changeset
   891
            return false;
153f268bfa72 8021122: Not all callables are handled for toString and other function valued properties
sundar
parents: 19085
diff changeset
   892
        }
153f268bfa72 8021122: Not all callables are handled for toString and other function valued properties
sundar
parents: 19085
diff changeset
   893
    }
153f268bfa72 8021122: Not all callables are handled for toString and other function valued properties
sundar
parents: 19085
diff changeset
   894
153f268bfa72 8021122: Not all callables are handled for toString and other function valued properties
sundar
parents: 19085
diff changeset
   895
    /**
19097
f544a2ea40ef 8021262: Make nashorn access checks consistent with underlying dynalink
sundar
parents: 19088
diff changeset
   896
     * Checks that the given Class is public and it can be accessed from no permissions context.
19088
153f268bfa72 8021122: Not all callables are handled for toString and other function valued properties
sundar
parents: 19085
diff changeset
   897
     *
153f268bfa72 8021122: Not all callables are handled for toString and other function valued properties
sundar
parents: 19085
diff changeset
   898
     * @param clazz Class object to check
153f268bfa72 8021122: Not all callables are handled for toString and other function valued properties
sundar
parents: 19085
diff changeset
   899
     * @return true if Class is accessible, false otherwise
153f268bfa72 8021122: Not all callables are handled for toString and other function valued properties
sundar
parents: 19085
diff changeset
   900
     */
153f268bfa72 8021122: Not all callables are handled for toString and other function valued properties
sundar
parents: 19085
diff changeset
   901
    public static boolean isAccessibleClass(final Class<?> clazz) {
20567
5621fc356049 8025771: Enhance Nashorn Contexts
sundar
parents: 20564
diff changeset
   902
        return Modifier.isPublic(clazz.getModifiers()) && Context.isAccessiblePackage(clazz);
19088
153f268bfa72 8021122: Not all callables are handled for toString and other function valued properties
sundar
parents: 19085
diff changeset
   903
    }
153f268bfa72 8021122: Not all callables are handled for toString and other function valued properties
sundar
parents: 19085
diff changeset
   904
153f268bfa72 8021122: Not all callables are handled for toString and other function valued properties
sundar
parents: 19085
diff changeset
   905
    /**
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   906
     * Lookup a Java class. This is used for JSR-223 stuff linking in from
17524
703643aeb0d6 8013914: Removed explicit LineNumberNodes that were too brittle when code moves around, and also introduced unnecessary footprint. Introduced the Statement node and fixed dead code elimination issues that were discovered by the absense of labels for LineNumberNodes.
lagergren
parents: 17519
diff changeset
   907
     * {@code jdk.nashorn.internal.objects.NativeJava} and {@code jdk.nashorn.internal.runtime.NativeJavaPackage}
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   908
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   909
     * @param fullName full name of class to load
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   910
     *
16272
675a0caf75bc 8009263: Fix all javadoc errors in nashorn code
sundar
parents: 16262
diff changeset
   911
     * @return the {@code Class<?>} for the name
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   912
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   913
     * @throws ClassNotFoundException if class cannot be resolved
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   914
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   915
    public Class<?> findClass(final String fullName) throws ClassNotFoundException {
20567
5621fc356049 8025771: Enhance Nashorn Contexts
sundar
parents: 20564
diff changeset
   916
        if (fullName.indexOf('[') != -1 || fullName.indexOf('/') != -1) {
5621fc356049 8025771: Enhance Nashorn Contexts
sundar
parents: 20564
diff changeset
   917
            // don't allow array class names or internal names.
5621fc356049 8025771: Enhance Nashorn Contexts
sundar
parents: 20564
diff changeset
   918
            throw new ClassNotFoundException(fullName);
5621fc356049 8025771: Enhance Nashorn Contexts
sundar
parents: 20564
diff changeset
   919
        }
5621fc356049 8025771: Enhance Nashorn Contexts
sundar
parents: 20564
diff changeset
   920
26071
430e39f6731f 8050078: Nashorn ClassFilter Support
sundar
parents: 26068
diff changeset
   921
        // give chance to ClassFilter to filter out, if present
430e39f6731f 8050078: Nashorn ClassFilter Support
sundar
parents: 26068
diff changeset
   922
        if (classFilter != null && !classFilter.exposeToScripts(fullName)) {
430e39f6731f 8050078: Nashorn ClassFilter Support
sundar
parents: 26068
diff changeset
   923
            throw new ClassNotFoundException(fullName);
430e39f6731f 8050078: Nashorn ClassFilter Support
sundar
parents: 26068
diff changeset
   924
        }
430e39f6731f 8050078: Nashorn ClassFilter Support
sundar
parents: 26068
diff changeset
   925
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   926
        // check package access as soon as possible!
20567
5621fc356049 8025771: Enhance Nashorn Contexts
sundar
parents: 20564
diff changeset
   927
        final SecurityManager sm = System.getSecurityManager();
5621fc356049 8025771: Enhance Nashorn Contexts
sundar
parents: 20564
diff changeset
   928
        if (sm != null) {
5621fc356049 8025771: Enhance Nashorn Contexts
sundar
parents: 20564
diff changeset
   929
            checkPackageAccess(sm, fullName);
5621fc356049 8025771: Enhance Nashorn Contexts
sundar
parents: 20564
diff changeset
   930
        }
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   931
16185
893aabe8c800 8006635: Reduce access levels as much as possible
sundar
parents: 16155
diff changeset
   932
        // try the script -classpath loader, if that is set
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   933
        if (classPathLoader != null) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   934
            try {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   935
                return Class.forName(fullName, true, classPathLoader);
16185
893aabe8c800 8006635: Reduce access levels as much as possible
sundar
parents: 16155
diff changeset
   936
            } catch (final ClassNotFoundException ignored) {
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   937
                // ignore, continue search
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   938
            }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   939
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   940
16185
893aabe8c800 8006635: Reduce access levels as much as possible
sundar
parents: 16155
diff changeset
   941
        // Try finding using the "app" loader.
893aabe8c800 8006635: Reduce access levels as much as possible
sundar
parents: 16155
diff changeset
   942
        return Class.forName(fullName, true, appLoader);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   943
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   944
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   945
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   946
     * Hook to print stack trace for a {@link Throwable} that occurred during
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   947
     * execution
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   948
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   949
     * @param t throwable for which to dump stack
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   950
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   951
    public static void printStackTrace(final Throwable t) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   952
        if (Context.DEBUG) {
16185
893aabe8c800 8006635: Reduce access levels as much as possible
sundar
parents: 16155
diff changeset
   953
            t.printStackTrace(Context.getCurrentErr());
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   954
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   955
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   956
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   957
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   958
     * Verify generated bytecode before emission. This is called back from the
16262
75513555e603 8008731: Separate configuration environment (options, error/output writer etc.) from Context
sundar
parents: 16256
diff changeset
   959
     * {@link ObjectClassGenerator} or the {@link Compiler}. If the "--verify-code" parameter
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   960
     * hasn't been given, this is a nop
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   961
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   962
     * Note that verification may load classes -- we don't want to do that unless
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   963
     * user specified verify option. We check it here even though caller
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   964
     * may have already checked that flag
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   965
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   966
     * @param bytecode bytecode to verify
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   967
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   968
    public void verify(final byte[] bytecode) {
16262
75513555e603 8008731: Separate configuration environment (options, error/output writer etc.) from Context
sundar
parents: 16256
diff changeset
   969
        if (env._verify_code) {
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   970
            // No verification when security manager is around as verifier
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   971
            // may load further classes - which should be avoided.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   972
            if (System.getSecurityManager() == null) {
19098
473dfe87bb7b 8021294: --verify-code option results in AnalyzerException
sundar
parents: 19097
diff changeset
   973
                CheckClassAdapter.verify(new ClassReader(bytecode), sharedLoader, false, new PrintWriter(System.err, true));
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   974
            }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   975
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   976
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   977
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   978
    /**
16211
41e031a45186 8006191: `cmd` -> exec("cmd") in script mode
jlaskey
parents: 16201
diff changeset
   979
     * Create and initialize a new global scope object.
41e031a45186 8006191: `cmd` -> exec("cmd") in script mode
jlaskey
parents: 16201
diff changeset
   980
     *
41e031a45186 8006191: `cmd` -> exec("cmd") in script mode
jlaskey
parents: 16201
diff changeset
   981
     * @return the initialized global scope object.
41e031a45186 8006191: `cmd` -> exec("cmd") in script mode
jlaskey
parents: 16201
diff changeset
   982
     */
23375
a1110f2cbe75 8037400: Remove getInitialMap getters and GlobalObject interface
sundar
parents: 23374
diff changeset
   983
    public Global createGlobal() {
16211
41e031a45186 8006191: `cmd` -> exec("cmd") in script mode
jlaskey
parents: 16201
diff changeset
   984
        return initGlobal(newGlobal());
41e031a45186 8006191: `cmd` -> exec("cmd") in script mode
jlaskey
parents: 16201
diff changeset
   985
    }
41e031a45186 8006191: `cmd` -> exec("cmd") in script mode
jlaskey
parents: 16201
diff changeset
   986
41e031a45186 8006191: `cmd` -> exec("cmd") in script mode
jlaskey
parents: 16201
diff changeset
   987
    /**
41e031a45186 8006191: `cmd` -> exec("cmd") in script mode
jlaskey
parents: 16201
diff changeset
   988
     * Create a new uninitialized global scope object
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   989
     * @return the global script object
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   990
     */
23375
a1110f2cbe75 8037400: Remove getInitialMap getters and GlobalObject interface
sundar
parents: 23374
diff changeset
   991
    public Global newGlobal() {
19085
066c9e5afd79 8020731: Revisit checkPermission calls in Context class
sundar
parents: 18874
diff changeset
   992
        return new Global(this);
16211
41e031a45186 8006191: `cmd` -> exec("cmd") in script mode
jlaskey
parents: 16201
diff changeset
   993
    }
41e031a45186 8006191: `cmd` -> exec("cmd") in script mode
jlaskey
parents: 16201
diff changeset
   994
41e031a45186 8006191: `cmd` -> exec("cmd") in script mode
jlaskey
parents: 16201
diff changeset
   995
    /**
41e031a45186 8006191: `cmd` -> exec("cmd") in script mode
jlaskey
parents: 16201
diff changeset
   996
     * Initialize given global scope object.
41e031a45186 8006191: `cmd` -> exec("cmd") in script mode
jlaskey
parents: 16201
diff changeset
   997
     *
16233
95d3e01c04c3 8008199: Lazy compilation and trampoline implementation
lagergren
parents: 16230
diff changeset
   998
     * @param global the global
25422
199a23bee487 8049524: Global object initialization via javax.script API should be minimal
sundar
parents: 25243
diff changeset
   999
     * @param engine the associated ScriptEngine instance, can be null
16211
41e031a45186 8006191: `cmd` -> exec("cmd") in script mode
jlaskey
parents: 16201
diff changeset
  1000
     * @return the initialized global scope object.
41e031a45186 8006191: `cmd` -> exec("cmd") in script mode
jlaskey
parents: 16201
diff changeset
  1001
     */
25422
199a23bee487 8049524: Global object initialization via javax.script API should be minimal
sundar
parents: 25243
diff changeset
  1002
    public Global initGlobal(final Global global, final ScriptEngine engine) {
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1003
        // Need only minimal global object, if we are just compiling.
16262
75513555e603 8008731: Separate configuration environment (options, error/output writer etc.) from Context
sundar
parents: 16256
diff changeset
  1004
        if (!env._compile_only) {
23375
a1110f2cbe75 8037400: Remove getInitialMap getters and GlobalObject interface
sundar
parents: 23374
diff changeset
  1005
            final Global oldGlobal = Context.getGlobal();
16185
893aabe8c800 8006635: Reduce access levels as much as possible
sundar
parents: 16155
diff changeset
  1006
            try {
23375
a1110f2cbe75 8037400: Remove getInitialMap getters and GlobalObject interface
sundar
parents: 23374
diff changeset
  1007
                Context.setGlobal(global);
16185
893aabe8c800 8006635: Reduce access levels as much as possible
sundar
parents: 16155
diff changeset
  1008
                // initialize global scope with builtin global objects
25422
199a23bee487 8049524: Global object initialization via javax.script API should be minimal
sundar
parents: 25243
diff changeset
  1009
                global.initBuiltinObjects(engine);
16185
893aabe8c800 8006635: Reduce access levels as much as possible
sundar
parents: 16155
diff changeset
  1010
            } finally {
23375
a1110f2cbe75 8037400: Remove getInitialMap getters and GlobalObject interface
sundar
parents: 23374
diff changeset
  1011
                Context.setGlobal(oldGlobal);
16185
893aabe8c800 8006635: Reduce access levels as much as possible
sundar
parents: 16155
diff changeset
  1012
            }
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1013
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1014
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1015
        return global;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1016
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1017
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1018
    /**
25422
199a23bee487 8049524: Global object initialization via javax.script API should be minimal
sundar
parents: 25243
diff changeset
  1019
     * Initialize given global scope object.
199a23bee487 8049524: Global object initialization via javax.script API should be minimal
sundar
parents: 25243
diff changeset
  1020
     *
199a23bee487 8049524: Global object initialization via javax.script API should be minimal
sundar
parents: 25243
diff changeset
  1021
     * @param global the global
199a23bee487 8049524: Global object initialization via javax.script API should be minimal
sundar
parents: 25243
diff changeset
  1022
     * @return the initialized global scope object.
199a23bee487 8049524: Global object initialization via javax.script API should be minimal
sundar
parents: 25243
diff changeset
  1023
     */
199a23bee487 8049524: Global object initialization via javax.script API should be minimal
sundar
parents: 25243
diff changeset
  1024
    public Global initGlobal(final Global global) {
199a23bee487 8049524: Global object initialization via javax.script API should be minimal
sundar
parents: 25243
diff changeset
  1025
        return initGlobal(global, null);
199a23bee487 8049524: Global object initialization via javax.script API should be minimal
sundar
parents: 25243
diff changeset
  1026
    }
199a23bee487 8049524: Global object initialization via javax.script API should be minimal
sundar
parents: 25243
diff changeset
  1027
199a23bee487 8049524: Global object initialization via javax.script API should be minimal
sundar
parents: 25243
diff changeset
  1028
    /**
16188
d6390b0ea32a 8006678: Avoid too many Context.getGlobal() calls
sundar
parents: 16185
diff changeset
  1029
     * Return the current global's context
d6390b0ea32a 8006678: Avoid too many Context.getGlobal() calls
sundar
parents: 16185
diff changeset
  1030
     * @return current global's context
16185
893aabe8c800 8006635: Reduce access levels as much as possible
sundar
parents: 16155
diff changeset
  1031
     */
893aabe8c800 8006635: Reduce access levels as much as possible
sundar
parents: 16155
diff changeset
  1032
    static Context getContextTrusted() {
23375
a1110f2cbe75 8037400: Remove getInitialMap getters and GlobalObject interface
sundar
parents: 23374
diff changeset
  1033
        return ((ScriptObject)Context.getGlobal()).getContext();
16185
893aabe8c800 8006635: Reduce access levels as much as possible
sundar
parents: 16155
diff changeset
  1034
    }
893aabe8c800 8006635: Reduce access levels as much as possible
sundar
parents: 16155
diff changeset
  1035
24769
attila
parents: 24759 24282
diff changeset
  1036
    static Context getContextTrustedOrNull() {
attila
parents: 24759 24282
diff changeset
  1037
        final Global global = Context.getGlobal();
attila
parents: 24759 24282
diff changeset
  1038
        return global == null ? null : ((ScriptObject)global).getContext();
attila
parents: 24759 24282
diff changeset
  1039
    }
attila
parents: 24759 24282
diff changeset
  1040
16185
893aabe8c800 8006635: Reduce access levels as much as possible
sundar
parents: 16155
diff changeset
  1041
    /**
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1042
     * Try to infer Context instance from the Class. If we cannot,
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1043
     * then get it from the thread local variable.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1044
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1045
     * @param clazz the class
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1046
     * @return context
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1047
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1048
    static Context fromClass(final Class<?> clazz) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1049
        final ClassLoader loader = clazz.getClassLoader();
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1050
19895
965b12eb322e 8024619: JDBC java.sql.DriverManager is not usable from JS script
sundar
parents: 19884
diff changeset
  1051
        if (loader instanceof ScriptLoader) {
965b12eb322e 8024619: JDBC java.sql.DriverManager is not usable from JS script
sundar
parents: 19884
diff changeset
  1052
            return ((ScriptLoader)loader).getContext();
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1053
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1054
19895
965b12eb322e 8024619: JDBC java.sql.DriverManager is not usable from JS script
sundar
parents: 19884
diff changeset
  1055
        return Context.getContextTrusted();
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1056
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1057
20928
3ff39d5c8c08 8026137: Fix Issues with Binary Evaluation Order
lagergren
parents: 20567
diff changeset
  1058
    private URL getResourceURL(final String resName) {
20564
f353da961684 8025629: load function should support a way to load scripts from classpath
sundar
parents: 19895
diff changeset
  1059
        // try the classPathLoader if we have and then
f353da961684 8025629: load function should support a way to load scripts from classpath
sundar
parents: 19895
diff changeset
  1060
        // try the appLoader if non-null.
f353da961684 8025629: load function should support a way to load scripts from classpath
sundar
parents: 19895
diff changeset
  1061
        if (classPathLoader != null) {
f353da961684 8025629: load function should support a way to load scripts from classpath
sundar
parents: 19895
diff changeset
  1062
            return classPathLoader.getResource(resName);
f353da961684 8025629: load function should support a way to load scripts from classpath
sundar
parents: 19895
diff changeset
  1063
        } else if (appLoader != null) {
f353da961684 8025629: load function should support a way to load scripts from classpath
sundar
parents: 19895
diff changeset
  1064
            return appLoader.getResource(resName);
f353da961684 8025629: load function should support a way to load scripts from classpath
sundar
parents: 19895
diff changeset
  1065
        }
f353da961684 8025629: load function should support a way to load scripts from classpath
sundar
parents: 19895
diff changeset
  1066
f353da961684 8025629: load function should support a way to load scripts from classpath
sundar
parents: 19895
diff changeset
  1067
        return null;
f353da961684 8025629: load function should support a way to load scripts from classpath
sundar
parents: 19895
diff changeset
  1068
    }
f353da961684 8025629: load function should support a way to load scripts from classpath
sundar
parents: 19895
diff changeset
  1069
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1070
    private Object evaluateSource(final Source source, final ScriptObject scope, final ScriptObject thiz) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1071
        ScriptFunction script = null;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1072
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1073
        try {
16230
c38c724d82e7 8008103: Source object should maintain URL of the script source as a private field
sundar
parents: 16226
diff changeset
  1074
            script = compileScript(source, scope, new Context.ThrowErrorManager());
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1075
        } catch (final ParserException e) {
16188
d6390b0ea32a 8006678: Avoid too many Context.getGlobal() calls
sundar
parents: 16185
diff changeset
  1076
            e.throwAsEcmaException();
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1077
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1078
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1079
        return ScriptRuntime.apply(script, thiz);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1080
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1081
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 23076
diff changeset
  1082
    private static ScriptFunction getProgramFunction(final Class<?> script, final ScriptObject scope) {
24879
d316854e4249 8046215: Running uncompilable scripts throws NullPointerException
hannesw
parents: 24783
diff changeset
  1083
        if (script == null) {
d316854e4249 8046215: Running uncompilable scripts throws NullPointerException
hannesw
parents: 24783
diff changeset
  1084
            return null;
d316854e4249 8046215: Running uncompilable scripts throws NullPointerException
hannesw
parents: 24783
diff changeset
  1085
        }
24783
b5c31bfe1496 8046014: MultiGlobalCompiledScript should cache :createProgramFunction handle
attila
parents: 24779
diff changeset
  1086
        return invokeCreateProgramFunctionHandle(getCreateProgramFunctionHandle(script), scope);
b5c31bfe1496 8046014: MultiGlobalCompiledScript should cache :createProgramFunction handle
attila
parents: 24779
diff changeset
  1087
    }
24282
2e3bd98c5664 8041697: CompiledScript slower when eval with binding
sundar
parents: 24206
diff changeset
  1088
24783
b5c31bfe1496 8046014: MultiGlobalCompiledScript should cache :createProgramFunction handle
attila
parents: 24779
diff changeset
  1089
    private static MethodHandle getCreateProgramFunctionHandle(final Class<?> script) {
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1090
        try {
24783
b5c31bfe1496 8046014: MultiGlobalCompiledScript should cache :createProgramFunction handle
attila
parents: 24779
diff changeset
  1091
            return LOOKUP.findStatic(script, CREATE_PROGRAM_FUNCTION.symbolName(), CREATE_PROGRAM_FUNCTION_TYPE);
b5c31bfe1496 8046014: MultiGlobalCompiledScript should cache :createProgramFunction handle
attila
parents: 24779
diff changeset
  1092
        } catch (NoSuchMethodException | IllegalAccessException e) {
b5c31bfe1496 8046014: MultiGlobalCompiledScript should cache :createProgramFunction handle
attila
parents: 24779
diff changeset
  1093
            throw new AssertionError("Failed to retrieve a handle for the program function for " + script.getName(), e);
b5c31bfe1496 8046014: MultiGlobalCompiledScript should cache :createProgramFunction handle
attila
parents: 24779
diff changeset
  1094
        }
b5c31bfe1496 8046014: MultiGlobalCompiledScript should cache :createProgramFunction handle
attila
parents: 24779
diff changeset
  1095
    }
b5c31bfe1496 8046014: MultiGlobalCompiledScript should cache :createProgramFunction handle
attila
parents: 24779
diff changeset
  1096
b5c31bfe1496 8046014: MultiGlobalCompiledScript should cache :createProgramFunction handle
attila
parents: 24779
diff changeset
  1097
    private static ScriptFunction invokeCreateProgramFunctionHandle(final MethodHandle createProgramFunctionHandle, final ScriptObject scope) {
b5c31bfe1496 8046014: MultiGlobalCompiledScript should cache :createProgramFunction handle
attila
parents: 24779
diff changeset
  1098
        try {
b5c31bfe1496 8046014: MultiGlobalCompiledScript should cache :createProgramFunction handle
attila
parents: 24779
diff changeset
  1099
            return (ScriptFunction)createProgramFunctionHandle.invokeExact(scope);
b5c31bfe1496 8046014: MultiGlobalCompiledScript should cache :createProgramFunction handle
attila
parents: 24779
diff changeset
  1100
        } catch (final RuntimeException|Error e) {
b5c31bfe1496 8046014: MultiGlobalCompiledScript should cache :createProgramFunction handle
attila
parents: 24779
diff changeset
  1101
            throw e;
b5c31bfe1496 8046014: MultiGlobalCompiledScript should cache :createProgramFunction handle
attila
parents: 24779
diff changeset
  1102
        } catch (final Throwable t) {
b5c31bfe1496 8046014: MultiGlobalCompiledScript should cache :createProgramFunction handle
attila
parents: 24779
diff changeset
  1103
            throw new AssertionError("Failed to create a program function", t);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1104
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1105
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1106
16230
c38c724d82e7 8008103: Source object should maintain URL of the script source as a private field
sundar
parents: 16226
diff changeset
  1107
    private ScriptFunction compileScript(final Source source, final ScriptObject scope, final ErrorManager errMan) {
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 23076
diff changeset
  1108
        return getProgramFunction(compile(source, errMan, this._strict), scope);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1109
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1110
16230
c38c724d82e7 8008103: Source object should maintain URL of the script source as a private field
sundar
parents: 16226
diff changeset
  1111
    private synchronized Class<?> compile(final Source source, final ErrorManager errMan, final boolean strict) {
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1112
        // start with no errors, no warnings.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1113
        errMan.reset();
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1114
23372
09707b3e5fb0 8021350: Share script classes between threads/globals within context
hannesw
parents: 23076
diff changeset
  1115
        Class<?> script = findCachedClass(source);
09707b3e5fb0 8021350: Share script classes between threads/globals within context
hannesw
parents: 23076
diff changeset
  1116
        if (script != null) {
24745
3a6e1477362b 8041434: Add synchronization to the common global constants structure
lagergren
parents: 24744
diff changeset
  1117
            final DebugLogger log = getLogger(Compiler.class);
24744
5290da85fc3d 8038426: Move all loggers from process wide scope into Global scope
lagergren
parents: 24742
diff changeset
  1118
            if (log.isEnabled()) {
5290da85fc3d 8038426: Move all loggers from process wide scope into Global scope
lagergren
parents: 24742
diff changeset
  1119
                log.fine(new RuntimeEvent<>(Level.INFO, source), "Code cache hit for ", source, " avoiding recompile.");
24731
ab0c8fc915ae 8038406: Testability: as a first step of moving loggers away from the process global space, the Debug object now supports logging POJOs from log entries as an event queue, which can be introspected from test scripts. This is way better than screen scraping brittle and subject-to-change log output.
lagergren
parents: 24727
diff changeset
  1120
            }
23372
09707b3e5fb0 8021350: Share script classes between threads/globals within context
hannesw
parents: 23076
diff changeset
  1121
            return script;
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1122
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1123
26055
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents: 25422
diff changeset
  1124
        StoredScript storedScript = null;
23767
7c0614b75e23 8038638: Persistent store for compiled scripts
hannesw
parents: 23375
diff changeset
  1125
        FunctionNode functionNode = null;
26055
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents: 25422
diff changeset
  1126
        final boolean useCodeStore = env._persistent_cache && !env._parse_only && !env._optimistic_types;
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents: 25422
diff changeset
  1127
        final String cacheKey = useCodeStore ? CodeStore.getCacheKey(0, null) : null;
23767
7c0614b75e23 8038638: Persistent store for compiled scripts
hannesw
parents: 23375
diff changeset
  1128
26055
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents: 25422
diff changeset
  1129
        if (useCodeStore) {
26764
c777787a937d 8046202: Make persistent code store more flexible
hannesw
parents: 26508
diff changeset
  1130
            storedScript = codeStore.load(source, cacheKey);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1131
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1132
26055
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents: 25422
diff changeset
  1133
        if (storedScript == null) {
24769
attila
parents: 24759 24282
diff changeset
  1134
            functionNode = new Parser(env, source, errMan, strict, getLogger(Parser.class)).parse();
23767
7c0614b75e23 8038638: Persistent store for compiled scripts
hannesw
parents: 23375
diff changeset
  1135
26377
028dad61662f 8051889: Implement block scoping in symbol assignment and scope computation
hannesw
parents: 26248
diff changeset
  1136
            if (errMan.hasErrors()) {
23767
7c0614b75e23 8038638: Persistent store for compiled scripts
hannesw
parents: 23375
diff changeset
  1137
                return null;
7c0614b75e23 8038638: Persistent store for compiled scripts
hannesw
parents: 23375
diff changeset
  1138
            }
16240
e1468b33e201 8008239: Unpublicized parts of the code generator package that were only package internal.
lagergren
parents: 16233
diff changeset
  1139
26065
d15adb218527 8055107: Extension directives to turn on callsite profiling, tracing, AST print and other debug features locally
sundar
parents: 26055
diff changeset
  1140
            if (env._print_ast || functionNode.getFlag(FunctionNode.IS_PRINT_AST)) {
23767
7c0614b75e23 8038638: Persistent store for compiled scripts
hannesw
parents: 23375
diff changeset
  1141
                getErr().println(new ASTWriter(functionNode));
7c0614b75e23 8038638: Persistent store for compiled scripts
hannesw
parents: 23375
diff changeset
  1142
            }
7c0614b75e23 8038638: Persistent store for compiled scripts
hannesw
parents: 23375
diff changeset
  1143
26065
d15adb218527 8055107: Extension directives to turn on callsite profiling, tracing, AST print and other debug features locally
sundar
parents: 26055
diff changeset
  1144
            if (env._print_parse || functionNode.getFlag(FunctionNode.IS_PRINT_PARSE)) {
24769
attila
parents: 24759 24282
diff changeset
  1145
                getErr().println(new PrintVisitor(functionNode, true, false));
23767
7c0614b75e23 8038638: Persistent store for compiled scripts
hannesw
parents: 23375
diff changeset
  1146
            }
16233
95d3e01c04c3 8008199: Lazy compilation and trampoline implementation
lagergren
parents: 16230
diff changeset
  1147
        }
95d3e01c04c3 8008199: Lazy compilation and trampoline implementation
lagergren
parents: 16230
diff changeset
  1148
17233
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 17231
diff changeset
  1149
        if (env._parse_only) {
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 17231
diff changeset
  1150
            return null;
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 17231
diff changeset
  1151
        }
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 17231
diff changeset
  1152
16233
95d3e01c04c3 8008199: Lazy compilation and trampoline implementation
lagergren
parents: 16230
diff changeset
  1153
        final URL          url    = source.getURL();
16262
75513555e603 8008731: Separate configuration environment (options, error/output writer etc.) from Context
sundar
parents: 16256
diff changeset
  1154
        final ScriptLoader loader = env._loader_per_compile ? createNewLoader() : scriptLoader;
23076
8660ebaaa2f2 8033924: Default permissions are not given for eval code
sundar
parents: 22669
diff changeset
  1155
        final CodeSource   cs     = new CodeSource(url, (CodeSigner[])null);
16262
75513555e603 8008731: Separate configuration environment (options, error/output writer etc.) from Context
sundar
parents: 16256
diff changeset
  1156
        final CodeInstaller<ScriptEnvironment> installer = new ContextCodeInstaller(this, loader, cs);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1157
26055
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents: 25422
diff changeset
  1158
        if (storedScript == null) {
24769
attila
parents: 24759 24282
diff changeset
  1159
            final CompilationPhases phases = Compiler.CompilationPhases.COMPILE_ALL;
24759
31aed7d9c02a 8034206: Make parts of code pipeline reusable in order to facilitate faster warmup and faster lazy compilation.
lagergren
parents: 24745
diff changeset
  1160
24769
attila
parents: 24759 24282
diff changeset
  1161
            final Compiler compiler = new Compiler(
attila
parents: 24759 24282
diff changeset
  1162
                    this,
attila
parents: 24759 24282
diff changeset
  1163
                    env,
attila
parents: 24759 24282
diff changeset
  1164
                    installer,
attila
parents: 24759 24282
diff changeset
  1165
                    source,
26377
028dad61662f 8051889: Implement block scoping in symbol assignment and scope computation
hannesw
parents: 26248
diff changeset
  1166
                    errMan,
24769
attila
parents: 24759 24282
diff changeset
  1167
                    strict | functionNode.isStrict());
16233
95d3e01c04c3 8008199: Lazy compilation and trampoline implementation
lagergren
parents: 16230
diff changeset
  1168
26055
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents: 25422
diff changeset
  1169
            final FunctionNode compiledFunction = compiler.compile(functionNode, phases);
26377
028dad61662f 8051889: Implement block scoping in symbol assignment and scope computation
hannesw
parents: 26248
diff changeset
  1170
            if (errMan.hasErrors()) {
028dad61662f 8051889: Implement block scoping in symbol assignment and scope computation
hannesw
parents: 26248
diff changeset
  1171
                return null;
028dad61662f 8051889: Implement block scoping in symbol assignment and scope computation
hannesw
parents: 26248
diff changeset
  1172
            }
26055
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents: 25422
diff changeset
  1173
            script = compiledFunction.getRootClass();
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents: 25422
diff changeset
  1174
            compiler.persistClassInfo(cacheKey, compiledFunction);
23767
7c0614b75e23 8038638: Persistent store for compiled scripts
hannesw
parents: 23375
diff changeset
  1175
        } else {
26055
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents: 25422
diff changeset
  1176
            Compiler.updateCompilationId(storedScript.getCompilationId());
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents: 25422
diff changeset
  1177
            script = install(storedScript, source, installer);
23767
7c0614b75e23 8038638: Persistent store for compiled scripts
hannesw
parents: 23375
diff changeset
  1178
        }
16233
95d3e01c04c3 8008199: Lazy compilation and trampoline implementation
lagergren
parents: 16230
diff changeset
  1179
23372
09707b3e5fb0 8021350: Share script classes between threads/globals within context
hannesw
parents: 23076
diff changeset
  1180
        cacheClass(source, script);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1181
        return script;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1182
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1183
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1184
    private ScriptLoader createNewLoader() {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1185
        return AccessController.doPrivileged(
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1186
             new PrivilegedAction<ScriptLoader>() {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1187
                @Override
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1188
                public ScriptLoader run() {
19895
965b12eb322e 8024619: JDBC java.sql.DriverManager is not usable from JS script
sundar
parents: 19884
diff changeset
  1189
                    return new ScriptLoader(appLoader, Context.this);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1190
                }
19459
79e75274df99 8022707: Revisit all doPrivileged blocks
sundar
parents: 19098
diff changeset
  1191
             }, CREATE_LOADER_ACC_CTXT);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1192
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1193
18864
c701b823ed9e 8020276: interface checks in Invocable.getInterface implementation
sundar
parents: 18862
diff changeset
  1194
    private long getUniqueScriptId() {
c701b823ed9e 8020276: interface checks in Invocable.getInterface implementation
sundar
parents: 18862
diff changeset
  1195
        return uniqueScriptId.getAndIncrement();
18862
8b6a01b38cb8 8020224: LinkageError: attempted duplicate class definition when --loader-per-compiler=false
sundar
parents: 18860
diff changeset
  1196
    }
23372
09707b3e5fb0 8021350: Share script classes between threads/globals within context
hannesw
parents: 23076
diff changeset
  1197
23767
7c0614b75e23 8038638: Persistent store for compiled scripts
hannesw
parents: 23375
diff changeset
  1198
    /**
7c0614b75e23 8038638: Persistent store for compiled scripts
hannesw
parents: 23375
diff changeset
  1199
     * Install a previously compiled class from the code cache.
7c0614b75e23 8038638: Persistent store for compiled scripts
hannesw
parents: 23375
diff changeset
  1200
     *
26055
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents: 25422
diff changeset
  1201
     * @param storedScript cached script containing class bytes and constants
23767
7c0614b75e23 8038638: Persistent store for compiled scripts
hannesw
parents: 23375
diff changeset
  1202
     * @return main script class
7c0614b75e23 8038638: Persistent store for compiled scripts
hannesw
parents: 23375
diff changeset
  1203
     */
26055
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents: 25422
diff changeset
  1204
    private static Class<?> install(final StoredScript storedScript, final Source source, final CodeInstaller<ScriptEnvironment> installer) {
23767
7c0614b75e23 8038638: Persistent store for compiled scripts
hannesw
parents: 23375
diff changeset
  1205
7c0614b75e23 8038638: Persistent store for compiled scripts
hannesw
parents: 23375
diff changeset
  1206
        final Map<String, Class<?>> installedClasses = new HashMap<>();
26764
c777787a937d 8046202: Make persistent code store more flexible
hannesw
parents: 26508
diff changeset
  1207
        final Map<String, byte[]>   classBytes       = storedScript.getClassBytes();
26055
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents: 25422
diff changeset
  1208
        final Object[] constants       = storedScript.getConstants();
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents: 25422
diff changeset
  1209
        final String   mainClassName   = storedScript.getMainClassName();
26764
c777787a937d 8046202: Make persistent code store more flexible
hannesw
parents: 26508
diff changeset
  1210
        final byte[]   mainClassBytes  = classBytes.get(mainClassName);
26055
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents: 25422
diff changeset
  1211
        final Class<?> mainClass       = installer.install(mainClassName, mainClassBytes);
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents: 25422
diff changeset
  1212
        final Map<Integer, FunctionInitializer> initialzers = storedScript.getInitializers();
23767
7c0614b75e23 8038638: Persistent store for compiled scripts
hannesw
parents: 23375
diff changeset
  1213
26055
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents: 25422
diff changeset
  1214
        installedClasses.put(mainClassName, mainClass);
23767
7c0614b75e23 8038638: Persistent store for compiled scripts
hannesw
parents: 23375
diff changeset
  1215
26764
c777787a937d 8046202: Make persistent code store more flexible
hannesw
parents: 26508
diff changeset
  1216
        for (final Map.Entry<String, byte[]> entry : classBytes.entrySet()) {
23767
7c0614b75e23 8038638: Persistent store for compiled scripts
hannesw
parents: 23375
diff changeset
  1217
            final String className = entry.getKey();
26055
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents: 25422
diff changeset
  1218
            if (className.equals(mainClassName)) {
23767
7c0614b75e23 8038638: Persistent store for compiled scripts
hannesw
parents: 23375
diff changeset
  1219
                continue;
7c0614b75e23 8038638: Persistent store for compiled scripts
hannesw
parents: 23375
diff changeset
  1220
            }
7c0614b75e23 8038638: Persistent store for compiled scripts
hannesw
parents: 23375
diff changeset
  1221
            final byte[] code = entry.getValue();
7c0614b75e23 8038638: Persistent store for compiled scripts
hannesw
parents: 23375
diff changeset
  1222
24769
attila
parents: 24759 24282
diff changeset
  1223
            installedClasses.put(className, installer.install(className, code));
23767
7c0614b75e23 8038638: Persistent store for compiled scripts
hannesw
parents: 23375
diff changeset
  1224
        }
24769
attila
parents: 24759 24282
diff changeset
  1225
attila
parents: 24759 24282
diff changeset
  1226
        installer.initialize(installedClasses.values(), source, constants);
attila
parents: 24759 24282
diff changeset
  1227
24778
2ff5d7041566 8044638: Tidy up Nashorn codebase for code standards
attila
parents: 24769
diff changeset
  1228
        for (final Object constant : constants) {
23767
7c0614b75e23 8038638: Persistent store for compiled scripts
hannesw
parents: 23375
diff changeset
  1229
            if (constant instanceof RecompilableScriptFunctionData) {
26055
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents: 25422
diff changeset
  1230
                final RecompilableScriptFunctionData data = (RecompilableScriptFunctionData) constant;
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents: 25422
diff changeset
  1231
                data.initTransients(source, installer);
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents: 25422
diff changeset
  1232
                if (initialzers != null) {
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents: 25422
diff changeset
  1233
                    final FunctionInitializer initializer = initialzers.get(data.getFunctionNodeId());
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents: 25422
diff changeset
  1234
                    initializer.setCode(installedClasses.get(initializer.getClassName()));
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents: 25422
diff changeset
  1235
                    data.initializeCode(initializer);
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents: 25422
diff changeset
  1236
                }
23767
7c0614b75e23 8038638: Persistent store for compiled scripts
hannesw
parents: 23375
diff changeset
  1237
            }
7c0614b75e23 8038638: Persistent store for compiled scripts
hannesw
parents: 23375
diff changeset
  1238
        }
7c0614b75e23 8038638: Persistent store for compiled scripts
hannesw
parents: 23375
diff changeset
  1239
26055
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents: 25422
diff changeset
  1240
        return mainClass;
23767
7c0614b75e23 8038638: Persistent store for compiled scripts
hannesw
parents: 23375
diff changeset
  1241
    }
7c0614b75e23 8038638: Persistent store for compiled scripts
hannesw
parents: 23375
diff changeset
  1242
23372
09707b3e5fb0 8021350: Share script classes between threads/globals within context
hannesw
parents: 23076
diff changeset
  1243
    /**
09707b3e5fb0 8021350: Share script classes between threads/globals within context
hannesw
parents: 23076
diff changeset
  1244
     * Cache for compiled script classes.
09707b3e5fb0 8021350: Share script classes between threads/globals within context
hannesw
parents: 23076
diff changeset
  1245
     */
09707b3e5fb0 8021350: Share script classes between threads/globals within context
hannesw
parents: 23076
diff changeset
  1246
    @SuppressWarnings("serial")
09707b3e5fb0 8021350: Share script classes between threads/globals within context
hannesw
parents: 23076
diff changeset
  1247
    private static class ClassCache extends LinkedHashMap<Source, ClassReference> {
09707b3e5fb0 8021350: Share script classes between threads/globals within context
hannesw
parents: 23076
diff changeset
  1248
        private final int size;
09707b3e5fb0 8021350: Share script classes between threads/globals within context
hannesw
parents: 23076
diff changeset
  1249
        private final ReferenceQueue<Class<?>> queue;
09707b3e5fb0 8021350: Share script classes between threads/globals within context
hannesw
parents: 23076
diff changeset
  1250
24731
ab0c8fc915ae 8038406: Testability: as a first step of moving loggers away from the process global space, the Debug object now supports logging POJOs from log entries as an event queue, which can be introspected from test scripts. This is way better than screen scraping brittle and subject-to-change log output.
lagergren
parents: 24727
diff changeset
  1251
        ClassCache(final int size) {
23372
09707b3e5fb0 8021350: Share script classes between threads/globals within context
hannesw
parents: 23076
diff changeset
  1252
            super(size, 0.75f, true);
09707b3e5fb0 8021350: Share script classes between threads/globals within context
hannesw
parents: 23076
diff changeset
  1253
            this.size = size;
09707b3e5fb0 8021350: Share script classes between threads/globals within context
hannesw
parents: 23076
diff changeset
  1254
            this.queue = new ReferenceQueue<>();
09707b3e5fb0 8021350: Share script classes between threads/globals within context
hannesw
parents: 23076
diff changeset
  1255
        }
09707b3e5fb0 8021350: Share script classes between threads/globals within context
hannesw
parents: 23076
diff changeset
  1256
09707b3e5fb0 8021350: Share script classes between threads/globals within context
hannesw
parents: 23076
diff changeset
  1257
        void cache(final Source source, final Class<?> clazz) {
09707b3e5fb0 8021350: Share script classes between threads/globals within context
hannesw
parents: 23076
diff changeset
  1258
            put(source, new ClassReference(clazz, queue, source));
09707b3e5fb0 8021350: Share script classes between threads/globals within context
hannesw
parents: 23076
diff changeset
  1259
        }
09707b3e5fb0 8021350: Share script classes between threads/globals within context
hannesw
parents: 23076
diff changeset
  1260
09707b3e5fb0 8021350: Share script classes between threads/globals within context
hannesw
parents: 23076
diff changeset
  1261
        @Override
09707b3e5fb0 8021350: Share script classes between threads/globals within context
hannesw
parents: 23076
diff changeset
  1262
        protected boolean removeEldestEntry(final Map.Entry<Source, ClassReference> eldest) {
09707b3e5fb0 8021350: Share script classes between threads/globals within context
hannesw
parents: 23076
diff changeset
  1263
            return size() > size;
09707b3e5fb0 8021350: Share script classes between threads/globals within context
hannesw
parents: 23076
diff changeset
  1264
        }
09707b3e5fb0 8021350: Share script classes between threads/globals within context
hannesw
parents: 23076
diff changeset
  1265
09707b3e5fb0 8021350: Share script classes between threads/globals within context
hannesw
parents: 23076
diff changeset
  1266
        @Override
24731
ab0c8fc915ae 8038406: Testability: as a first step of moving loggers away from the process global space, the Debug object now supports logging POJOs from log entries as an event queue, which can be introspected from test scripts. This is way better than screen scraping brittle and subject-to-change log output.
lagergren
parents: 24727
diff changeset
  1267
        public ClassReference get(final Object key) {
23372
09707b3e5fb0 8021350: Share script classes between threads/globals within context
hannesw
parents: 23076
diff changeset
  1268
            for (ClassReference ref; (ref = (ClassReference)queue.poll()) != null; ) {
09707b3e5fb0 8021350: Share script classes between threads/globals within context
hannesw
parents: 23076
diff changeset
  1269
                remove(ref.source);
09707b3e5fb0 8021350: Share script classes between threads/globals within context
hannesw
parents: 23076
diff changeset
  1270
            }
09707b3e5fb0 8021350: Share script classes between threads/globals within context
hannesw
parents: 23076
diff changeset
  1271
            return super.get(key);
09707b3e5fb0 8021350: Share script classes between threads/globals within context
hannesw
parents: 23076
diff changeset
  1272
        }
09707b3e5fb0 8021350: Share script classes between threads/globals within context
hannesw
parents: 23076
diff changeset
  1273
09707b3e5fb0 8021350: Share script classes between threads/globals within context
hannesw
parents: 23076
diff changeset
  1274
    }
09707b3e5fb0 8021350: Share script classes between threads/globals within context
hannesw
parents: 23076
diff changeset
  1275
09707b3e5fb0 8021350: Share script classes between threads/globals within context
hannesw
parents: 23076
diff changeset
  1276
    private static class ClassReference extends SoftReference<Class<?>> {
09707b3e5fb0 8021350: Share script classes between threads/globals within context
hannesw
parents: 23076
diff changeset
  1277
        private final Source source;
09707b3e5fb0 8021350: Share script classes between threads/globals within context
hannesw
parents: 23076
diff changeset
  1278
09707b3e5fb0 8021350: Share script classes between threads/globals within context
hannesw
parents: 23076
diff changeset
  1279
        ClassReference(final Class<?> clazz, final ReferenceQueue<Class<?>> queue, final Source source) {
09707b3e5fb0 8021350: Share script classes between threads/globals within context
hannesw
parents: 23076
diff changeset
  1280
            super(clazz, queue);
09707b3e5fb0 8021350: Share script classes between threads/globals within context
hannesw
parents: 23076
diff changeset
  1281
            this.source = source;
09707b3e5fb0 8021350: Share script classes between threads/globals within context
hannesw
parents: 23076
diff changeset
  1282
        }
09707b3e5fb0 8021350: Share script classes between threads/globals within context
hannesw
parents: 23076
diff changeset
  1283
    }
09707b3e5fb0 8021350: Share script classes between threads/globals within context
hannesw
parents: 23076
diff changeset
  1284
09707b3e5fb0 8021350: Share script classes between threads/globals within context
hannesw
parents: 23076
diff changeset
  1285
    // Class cache management
09707b3e5fb0 8021350: Share script classes between threads/globals within context
hannesw
parents: 23076
diff changeset
  1286
    private Class<?> findCachedClass(final Source source) {
24731
ab0c8fc915ae 8038406: Testability: as a first step of moving loggers away from the process global space, the Debug object now supports logging POJOs from log entries as an event queue, which can be introspected from test scripts. This is way better than screen scraping brittle and subject-to-change log output.
lagergren
parents: 24727
diff changeset
  1287
        final ClassReference ref = classCache == null ? null : classCache.get(source);
23372
09707b3e5fb0 8021350: Share script classes between threads/globals within context
hannesw
parents: 23076
diff changeset
  1288
        return ref != null ? ref.get() : null;
09707b3e5fb0 8021350: Share script classes between threads/globals within context
hannesw
parents: 23076
diff changeset
  1289
    }
09707b3e5fb0 8021350: Share script classes between threads/globals within context
hannesw
parents: 23076
diff changeset
  1290
09707b3e5fb0 8021350: Share script classes between threads/globals within context
hannesw
parents: 23076
diff changeset
  1291
    private void cacheClass(final Source source, final Class<?> clazz) {
09707b3e5fb0 8021350: Share script classes between threads/globals within context
hannesw
parents: 23076
diff changeset
  1292
        if (classCache != null) {
09707b3e5fb0 8021350: Share script classes between threads/globals within context
hannesw
parents: 23076
diff changeset
  1293
            classCache.cache(source, clazz);
09707b3e5fb0 8021350: Share script classes between threads/globals within context
hannesw
parents: 23076
diff changeset
  1294
        }
09707b3e5fb0 8021350: Share script classes between threads/globals within context
hannesw
parents: 23076
diff changeset
  1295
    }
09707b3e5fb0 8021350: Share script classes between threads/globals within context
hannesw
parents: 23076
diff changeset
  1296
24745
3a6e1477362b 8041434: Add synchronization to the common global constants structure
lagergren
parents: 24744
diff changeset
  1297
    // logging
3a6e1477362b 8041434: Add synchronization to the common global constants structure
lagergren
parents: 24744
diff changeset
  1298
    private final Map<String, DebugLogger> loggers = new HashMap<>();
3a6e1477362b 8041434: Add synchronization to the common global constants structure
lagergren
parents: 24744
diff changeset
  1299
3a6e1477362b 8041434: Add synchronization to the common global constants structure
lagergren
parents: 24744
diff changeset
  1300
    private void initLoggers() {
3a6e1477362b 8041434: Add synchronization to the common global constants structure
lagergren
parents: 24744
diff changeset
  1301
        ((Loggable)MethodHandleFactory.getFunctionality()).initLogger(this);
3a6e1477362b 8041434: Add synchronization to the common global constants structure
lagergren
parents: 24744
diff changeset
  1302
    }
3a6e1477362b 8041434: Add synchronization to the common global constants structure
lagergren
parents: 24744
diff changeset
  1303
3a6e1477362b 8041434: Add synchronization to the common global constants structure
lagergren
parents: 24744
diff changeset
  1304
    /**
3a6e1477362b 8041434: Add synchronization to the common global constants structure
lagergren
parents: 24744
diff changeset
  1305
     * Get a logger, given a loggable class
3a6e1477362b 8041434: Add synchronization to the common global constants structure
lagergren
parents: 24744
diff changeset
  1306
     * @param clazz a Loggable class
3a6e1477362b 8041434: Add synchronization to the common global constants structure
lagergren
parents: 24744
diff changeset
  1307
     * @return debuglogger associated with that class
3a6e1477362b 8041434: Add synchronization to the common global constants structure
lagergren
parents: 24744
diff changeset
  1308
     */
3a6e1477362b 8041434: Add synchronization to the common global constants structure
lagergren
parents: 24744
diff changeset
  1309
    public DebugLogger getLogger(final Class<? extends Loggable> clazz) {
24993
b707d46bae40 8046898: Make sure that lazy compilation is the default, remove redundant "enable lazy compilation" flags, added warning message if compile logging is enabled and lazy is switched off. Verified existing test suite code coverage equivalence between lazy and eager.
lagergren
parents: 24879
diff changeset
  1310
        return getLogger(clazz, null);
b707d46bae40 8046898: Make sure that lazy compilation is the default, remove redundant "enable lazy compilation" flags, added warning message if compile logging is enabled and lazy is switched off. Verified existing test suite code coverage equivalence between lazy and eager.
lagergren
parents: 24879
diff changeset
  1311
    }
b707d46bae40 8046898: Make sure that lazy compilation is the default, remove redundant "enable lazy compilation" flags, added warning message if compile logging is enabled and lazy is switched off. Verified existing test suite code coverage equivalence between lazy and eager.
lagergren
parents: 24879
diff changeset
  1312
b707d46bae40 8046898: Make sure that lazy compilation is the default, remove redundant "enable lazy compilation" flags, added warning message if compile logging is enabled and lazy is switched off. Verified existing test suite code coverage equivalence between lazy and eager.
lagergren
parents: 24879
diff changeset
  1313
    /**
b707d46bae40 8046898: Make sure that lazy compilation is the default, remove redundant "enable lazy compilation" flags, added warning message if compile logging is enabled and lazy is switched off. Verified existing test suite code coverage equivalence between lazy and eager.
lagergren
parents: 24879
diff changeset
  1314
     * Get a logger, given a loggable class
b707d46bae40 8046898: Make sure that lazy compilation is the default, remove redundant "enable lazy compilation" flags, added warning message if compile logging is enabled and lazy is switched off. Verified existing test suite code coverage equivalence between lazy and eager.
lagergren
parents: 24879
diff changeset
  1315
     * @param clazz a Loggable class
b707d46bae40 8046898: Make sure that lazy compilation is the default, remove redundant "enable lazy compilation" flags, added warning message if compile logging is enabled and lazy is switched off. Verified existing test suite code coverage equivalence between lazy and eager.
lagergren
parents: 24879
diff changeset
  1316
     * @param initHook an init hook - if this is the first time the logger is created in the context, run the init hook
b707d46bae40 8046898: Make sure that lazy compilation is the default, remove redundant "enable lazy compilation" flags, added warning message if compile logging is enabled and lazy is switched off. Verified existing test suite code coverage equivalence between lazy and eager.
lagergren
parents: 24879
diff changeset
  1317
     * @return debuglogger associated with that class
b707d46bae40 8046898: Make sure that lazy compilation is the default, remove redundant "enable lazy compilation" flags, added warning message if compile logging is enabled and lazy is switched off. Verified existing test suite code coverage equivalence between lazy and eager.
lagergren
parents: 24879
diff changeset
  1318
     */
b707d46bae40 8046898: Make sure that lazy compilation is the default, remove redundant "enable lazy compilation" flags, added warning message if compile logging is enabled and lazy is switched off. Verified existing test suite code coverage equivalence between lazy and eager.
lagergren
parents: 24879
diff changeset
  1319
    public DebugLogger getLogger(final Class<? extends Loggable> clazz, final Consumer<DebugLogger> initHook) {
24745
3a6e1477362b 8041434: Add synchronization to the common global constants structure
lagergren
parents: 24744
diff changeset
  1320
        final String name = getLoggerName(clazz);
3a6e1477362b 8041434: Add synchronization to the common global constants structure
lagergren
parents: 24744
diff changeset
  1321
        DebugLogger logger = loggers.get(name);
3a6e1477362b 8041434: Add synchronization to the common global constants structure
lagergren
parents: 24744
diff changeset
  1322
        if (logger == null) {
3a6e1477362b 8041434: Add synchronization to the common global constants structure
lagergren
parents: 24744
diff changeset
  1323
            if (!env.hasLogger(name)) {
3a6e1477362b 8041434: Add synchronization to the common global constants structure
lagergren
parents: 24744
diff changeset
  1324
                return DebugLogger.DISABLED_LOGGER;
3a6e1477362b 8041434: Add synchronization to the common global constants structure
lagergren
parents: 24744
diff changeset
  1325
            }
3a6e1477362b 8041434: Add synchronization to the common global constants structure
lagergren
parents: 24744
diff changeset
  1326
            final LoggerInfo info = env._loggers.get(name);
3a6e1477362b 8041434: Add synchronization to the common global constants structure
lagergren
parents: 24744
diff changeset
  1327
            logger = new DebugLogger(name, info.getLevel(), info.isQuiet());
24993
b707d46bae40 8046898: Make sure that lazy compilation is the default, remove redundant "enable lazy compilation" flags, added warning message if compile logging is enabled and lazy is switched off. Verified existing test suite code coverage equivalence between lazy and eager.
lagergren
parents: 24879
diff changeset
  1328
            if (initHook != null) {
b707d46bae40 8046898: Make sure that lazy compilation is the default, remove redundant "enable lazy compilation" flags, added warning message if compile logging is enabled and lazy is switched off. Verified existing test suite code coverage equivalence between lazy and eager.
lagergren
parents: 24879
diff changeset
  1329
                initHook.accept(logger);
b707d46bae40 8046898: Make sure that lazy compilation is the default, remove redundant "enable lazy compilation" flags, added warning message if compile logging is enabled and lazy is switched off. Verified existing test suite code coverage equivalence between lazy and eager.
lagergren
parents: 24879
diff changeset
  1330
            }
24745
3a6e1477362b 8041434: Add synchronization to the common global constants structure
lagergren
parents: 24744
diff changeset
  1331
            loggers.put(name, logger);
3a6e1477362b 8041434: Add synchronization to the common global constants structure
lagergren
parents: 24744
diff changeset
  1332
        }
3a6e1477362b 8041434: Add synchronization to the common global constants structure
lagergren
parents: 24744
diff changeset
  1333
        return logger;
3a6e1477362b 8041434: Add synchronization to the common global constants structure
lagergren
parents: 24744
diff changeset
  1334
    }
3a6e1477362b 8041434: Add synchronization to the common global constants structure
lagergren
parents: 24744
diff changeset
  1335
3a6e1477362b 8041434: Add synchronization to the common global constants structure
lagergren
parents: 24744
diff changeset
  1336
    /**
3a6e1477362b 8041434: Add synchronization to the common global constants structure
lagergren
parents: 24744
diff changeset
  1337
     * Given a Loggable class, weave debug info info a method handle for that logger.
3a6e1477362b 8041434: Add synchronization to the common global constants structure
lagergren
parents: 24744
diff changeset
  1338
     * Level.INFO is used
3a6e1477362b 8041434: Add synchronization to the common global constants structure
lagergren
parents: 24744
diff changeset
  1339
     *
3a6e1477362b 8041434: Add synchronization to the common global constants structure
lagergren
parents: 24744
diff changeset
  1340
     * @param clazz loggable
3a6e1477362b 8041434: Add synchronization to the common global constants structure
lagergren
parents: 24744
diff changeset
  1341
     * @param mh    method handle
3a6e1477362b 8041434: Add synchronization to the common global constants structure
lagergren
parents: 24744
diff changeset
  1342
     * @param text  debug printout to add
3a6e1477362b 8041434: Add synchronization to the common global constants structure
lagergren
parents: 24744
diff changeset
  1343
     *
3a6e1477362b 8041434: Add synchronization to the common global constants structure
lagergren
parents: 24744
diff changeset
  1344
     * @return instrumented method handle, or null if logger not enabled
3a6e1477362b 8041434: Add synchronization to the common global constants structure
lagergren
parents: 24744
diff changeset
  1345
     */
3a6e1477362b 8041434: Add synchronization to the common global constants structure
lagergren
parents: 24744
diff changeset
  1346
    public MethodHandle addLoggingToHandle(final Class<? extends Loggable> clazz, final MethodHandle mh, final Supplier<String> text) {
3a6e1477362b 8041434: Add synchronization to the common global constants structure
lagergren
parents: 24744
diff changeset
  1347
        return addLoggingToHandle(clazz, Level.INFO, mh, Integer.MAX_VALUE, false, text);
3a6e1477362b 8041434: Add synchronization to the common global constants structure
lagergren
parents: 24744
diff changeset
  1348
    }
3a6e1477362b 8041434: Add synchronization to the common global constants structure
lagergren
parents: 24744
diff changeset
  1349
3a6e1477362b 8041434: Add synchronization to the common global constants structure
lagergren
parents: 24744
diff changeset
  1350
    /**
3a6e1477362b 8041434: Add synchronization to the common global constants structure
lagergren
parents: 24744
diff changeset
  1351
     * Given a Loggable class, weave debug info info a method handle for that logger.
3a6e1477362b 8041434: Add synchronization to the common global constants structure
lagergren
parents: 24744
diff changeset
  1352
     *
3a6e1477362b 8041434: Add synchronization to the common global constants structure
lagergren
parents: 24744
diff changeset
  1353
     * @param clazz            loggable
3a6e1477362b 8041434: Add synchronization to the common global constants structure
lagergren
parents: 24744
diff changeset
  1354
     * @param level            log level
3a6e1477362b 8041434: Add synchronization to the common global constants structure
lagergren
parents: 24744
diff changeset
  1355
     * @param mh               method handle
3a6e1477362b 8041434: Add synchronization to the common global constants structure
lagergren
parents: 24744
diff changeset
  1356
     * @param paramStart       first parameter to print
3a6e1477362b 8041434: Add synchronization to the common global constants structure
lagergren
parents: 24744
diff changeset
  1357
     * @param printReturnValue should we print the return vaulue?
3a6e1477362b 8041434: Add synchronization to the common global constants structure
lagergren
parents: 24744
diff changeset
  1358
     * @param text             debug printout to add
3a6e1477362b 8041434: Add synchronization to the common global constants structure
lagergren
parents: 24744
diff changeset
  1359
     *
3a6e1477362b 8041434: Add synchronization to the common global constants structure
lagergren
parents: 24744
diff changeset
  1360
     * @return instrumented method handle, or null if logger not enabled
3a6e1477362b 8041434: Add synchronization to the common global constants structure
lagergren
parents: 24744
diff changeset
  1361
     */
3a6e1477362b 8041434: Add synchronization to the common global constants structure
lagergren
parents: 24744
diff changeset
  1362
    public MethodHandle addLoggingToHandle(final Class<? extends Loggable> clazz, final Level level, final MethodHandle mh, final int paramStart, final boolean printReturnValue, final Supplier<String> text) {
3a6e1477362b 8041434: Add synchronization to the common global constants structure
lagergren
parents: 24744
diff changeset
  1363
        final DebugLogger log = getLogger(clazz);
3a6e1477362b 8041434: Add synchronization to the common global constants structure
lagergren
parents: 24744
diff changeset
  1364
        if (log.isEnabled()) {
3a6e1477362b 8041434: Add synchronization to the common global constants structure
lagergren
parents: 24744
diff changeset
  1365
            return MethodHandleFactory.addDebugPrintout(log, level, mh, paramStart, printReturnValue, text.get());
3a6e1477362b 8041434: Add synchronization to the common global constants structure
lagergren
parents: 24744
diff changeset
  1366
        }
3a6e1477362b 8041434: Add synchronization to the common global constants structure
lagergren
parents: 24744
diff changeset
  1367
        return mh;
3a6e1477362b 8041434: Add synchronization to the common global constants structure
lagergren
parents: 24744
diff changeset
  1368
    }
3a6e1477362b 8041434: Add synchronization to the common global constants structure
lagergren
parents: 24744
diff changeset
  1369
3a6e1477362b 8041434: Add synchronization to the common global constants structure
lagergren
parents: 24744
diff changeset
  1370
    private static String getLoggerName(final Class<?> clazz) {
3a6e1477362b 8041434: Add synchronization to the common global constants structure
lagergren
parents: 24744
diff changeset
  1371
        Class<?> current = clazz;
3a6e1477362b 8041434: Add synchronization to the common global constants structure
lagergren
parents: 24744
diff changeset
  1372
        while (current != null) {
3a6e1477362b 8041434: Add synchronization to the common global constants structure
lagergren
parents: 24744
diff changeset
  1373
            final Logger log = current.getAnnotation(Logger.class);
3a6e1477362b 8041434: Add synchronization to the common global constants structure
lagergren
parents: 24744
diff changeset
  1374
            if (log != null) {
3a6e1477362b 8041434: Add synchronization to the common global constants structure
lagergren
parents: 24744
diff changeset
  1375
                assert !"".equals(log.name());
3a6e1477362b 8041434: Add synchronization to the common global constants structure
lagergren
parents: 24744
diff changeset
  1376
                return log.name();
3a6e1477362b 8041434: Add synchronization to the common global constants structure
lagergren
parents: 24744
diff changeset
  1377
            }
3a6e1477362b 8041434: Add synchronization to the common global constants structure
lagergren
parents: 24744
diff changeset
  1378
            current = current.getSuperclass();
3a6e1477362b 8041434: Add synchronization to the common global constants structure
lagergren
parents: 24744
diff changeset
  1379
        }
3a6e1477362b 8041434: Add synchronization to the common global constants structure
lagergren
parents: 24744
diff changeset
  1380
        assert false;
3a6e1477362b 8041434: Add synchronization to the common global constants structure
lagergren
parents: 24744
diff changeset
  1381
        return null;
3a6e1477362b 8041434: Add synchronization to the common global constants structure
lagergren
parents: 24744
diff changeset
  1382
    }
23372
09707b3e5fb0 8021350: Share script classes between threads/globals within context
hannesw
parents: 23076
diff changeset
  1383
26768
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26764
diff changeset
  1384
    /**
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26764
diff changeset
  1385
     * This is a special kind of switchpoint used to guard builtin
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26764
diff changeset
  1386
     * properties and prototypes. In the future it might contain
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26764
diff changeset
  1387
     * logic to e.g. multiple switchpoint classes.
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26764
diff changeset
  1388
     */
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26764
diff changeset
  1389
    public static final class BuiltinSwitchPoint extends SwitchPoint {
26886
18c744ab4df2 8059211: Changed ArrayData.length accessor to use the protected field and fixed javadoc warnings related to this
lagergren
parents: 26768
diff changeset
  1390
        //empty
26768
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26764
diff changeset
  1391
    }
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26764
diff changeset
  1392
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26764
diff changeset
  1393
    /**
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26764
diff changeset
  1394
     * Create a new builtin switchpoint and return it
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26764
diff changeset
  1395
     * @param name key name
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26764
diff changeset
  1396
     * @return new builtin switchpoint
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26764
diff changeset
  1397
     */
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26764
diff changeset
  1398
    public SwitchPoint newBuiltinSwitchPoint(final String name) {
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26764
diff changeset
  1399
        assert builtinSwitchPoints.get(name) == null;
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26764
diff changeset
  1400
        final SwitchPoint sp = new BuiltinSwitchPoint();
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26764
diff changeset
  1401
        builtinSwitchPoints.put(name, sp);
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26764
diff changeset
  1402
        return sp;
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26764
diff changeset
  1403
    }
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26764
diff changeset
  1404
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26764
diff changeset
  1405
    /**
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26764
diff changeset
  1406
     * Return the builtin switchpoint for a particular key name
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26764
diff changeset
  1407
     * @param name key name
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26764
diff changeset
  1408
     * @return builtin switchpoint or null if none
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26764
diff changeset
  1409
     */
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26764
diff changeset
  1410
    public SwitchPoint getBuiltinSwitchPoint(final String name) {
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26764
diff changeset
  1411
        return builtinSwitchPoints.get(name);
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26764
diff changeset
  1412
    }
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26764
diff changeset
  1413
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1414
}