src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/runtime/linker/NashornCallSiteDescriptor.java
author attila
Wed, 20 Dec 2017 17:36:50 +0100
changeset 48411 4ff5c5206427
parent 47738 e4d7a32e6a91
permissions -rw-r--r--
8193371: Use Dynalink REMOVE operation in Nashorn Reviewed-by: hannesw, sundar
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.linker;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    27
41842
50202a344d28 8168005: Introduce namespaces for GET, SET Dynalink operations
attila
parents: 36686
diff changeset
    28
import static jdk.dynalink.StandardNamespace.ELEMENT;
50202a344d28 8168005: Introduce namespaces for GET, SET Dynalink operations
attila
parents: 36686
diff changeset
    29
import static jdk.dynalink.StandardNamespace.METHOD;
50202a344d28 8168005: Introduce namespaces for GET, SET Dynalink operations
attila
parents: 36686
diff changeset
    30
import static jdk.dynalink.StandardNamespace.PROPERTY;
50202a344d28 8168005: Introduce namespaces for GET, SET Dynalink operations
attila
parents: 36686
diff changeset
    31
import static jdk.dynalink.StandardOperation.GET;
48411
4ff5c5206427 8193371: Use Dynalink REMOVE operation in Nashorn
attila
parents: 47738
diff changeset
    32
import static jdk.dynalink.StandardOperation.REMOVE;
41842
50202a344d28 8168005: Introduce namespaces for GET, SET Dynalink operations
attila
parents: 36686
diff changeset
    33
import static jdk.dynalink.StandardOperation.SET;
50202a344d28 8168005: Introduce namespaces for GET, SET Dynalink operations
attila
parents: 36686
diff changeset
    34
18841
9bbc4b8832b2 8010946: AccessControl.doPrivileged is broken when called from js script
attila
parents: 17255
diff changeset
    35
import java.lang.invoke.MethodHandles;
9bbc4b8832b2 8010946: AccessControl.doPrivileged is broken when called from js script
attila
parents: 17255
diff changeset
    36
import java.lang.invoke.MethodHandles.Lookup;
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    37
import java.lang.invoke.MethodType;
33536
3168b2a1640e 8141425: Improve caching in NashornCallSiteDescriptor
attila
parents: 33533
diff changeset
    38
import java.lang.ref.Reference;
3168b2a1640e 8141425: Improve caching in NashornCallSiteDescriptor
attila
parents: 33533
diff changeset
    39
import java.lang.ref.WeakReference;
33341
cc9fa3638714 8139905: Add a convenience AccessControlContext factory
attila
parents: 33331
diff changeset
    40
import java.security.AccessControlContext;
33331
273e6a10de22 8139435: Make sure CallSiteDescriptor.getLookup is subject to a security check
attila
parents: 33330
diff changeset
    41
import java.security.AccessController;
273e6a10de22 8139435: Make sure CallSiteDescriptor.getLookup is subject to a security check
attila
parents: 33330
diff changeset
    42
import java.security.PrivilegedAction;
33536
3168b2a1640e 8141425: Improve caching in NashornCallSiteDescriptor
attila
parents: 33533
diff changeset
    43
import java.util.Collections;
3168b2a1640e 8141425: Improve caching in NashornCallSiteDescriptor
attila
parents: 33533
diff changeset
    44
import java.util.Map;
3168b2a1640e 8141425: Improve caching in NashornCallSiteDescriptor
attila
parents: 33533
diff changeset
    45
import java.util.WeakHashMap;
18841
9bbc4b8832b2 8010946: AccessControl.doPrivileged is broken when called from js script
attila
parents: 17255
diff changeset
    46
import java.util.concurrent.ConcurrentHashMap;
9bbc4b8832b2 8010946: AccessControl.doPrivileged is broken when called from js script
attila
parents: 17255
diff changeset
    47
import java.util.concurrent.ConcurrentMap;
33536
3168b2a1640e 8141425: Improve caching in NashornCallSiteDescriptor
attila
parents: 33533
diff changeset
    48
import java.util.stream.Stream;
34447
ec4c069f9436 8141338: Move jdk.internal.dynalink package to jdk.dynalink
attila
parents: 33536
diff changeset
    49
import jdk.dynalink.CallSiteDescriptor;
ec4c069f9436 8141338: Move jdk.internal.dynalink package to jdk.dynalink
attila
parents: 33536
diff changeset
    50
import jdk.dynalink.NamedOperation;
41842
50202a344d28 8168005: Introduce namespaces for GET, SET Dynalink operations
attila
parents: 36686
diff changeset
    51
import jdk.dynalink.NamespaceOperation;
34447
ec4c069f9436 8141338: Move jdk.internal.dynalink package to jdk.dynalink
attila
parents: 33536
diff changeset
    52
import jdk.dynalink.Operation;
36686
a351eacd4c42 8150218: Autoconversion SAM adapters sometimes don't get privileges
attila
parents: 34732
diff changeset
    53
import jdk.dynalink.SecureLookupSupplier;
41842
50202a344d28 8168005: Introduce namespaces for GET, SET Dynalink operations
attila
parents: 36686
diff changeset
    54
import jdk.dynalink.StandardNamespace;
34447
ec4c069f9436 8141338: Move jdk.internal.dynalink package to jdk.dynalink
attila
parents: 33536
diff changeset
    55
import jdk.dynalink.StandardOperation;
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: 24725
diff changeset
    56
import jdk.nashorn.internal.ir.debug.NashornTextifier;
33341
cc9fa3638714 8139905: Add a convenience AccessControlContext factory
attila
parents: 33331
diff changeset
    57
import jdk.nashorn.internal.runtime.AccessControlContextFactory;
32049
af8f6292d54d 8133119: Error message associated with TypeError for call and new should include stringified Node
sundar
parents: 32048
diff changeset
    58
import jdk.nashorn.internal.runtime.ScriptRuntime;
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    59
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    60
/**
33343
23abd10384a5 8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents: 33342
diff changeset
    61
 * Nashorn-specific implementation of Dynalink's {@link CallSiteDescriptor}.
23abd10384a5 8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents: 33342
diff changeset
    62
 * The reason we have our own subclass is that we're storing flags in an
23abd10384a5 8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents: 33342
diff changeset
    63
 * additional primitive field. The class also exposes some useful utilities in
23abd10384a5 8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents: 33342
diff changeset
    64
 * form of static methods.
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    65
 */
33342
86b4260bb17a 8139919: Make CallSiteDescriptor a concrete class
attila
parents: 33341
diff changeset
    66
public final class NashornCallSiteDescriptor extends CallSiteDescriptor {
48411
4ff5c5206427 8193371: Use Dynalink REMOVE operation in Nashorn
attila
parents: 47738
diff changeset
    67
    // Lowest four bits describe the operation
33343
23abd10384a5 8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents: 33342
diff changeset
    68
    /** Property getter operation {@code obj.prop} */
23abd10384a5 8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents: 33342
diff changeset
    69
    public static final int GET_PROPERTY        = 0;
23abd10384a5 8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents: 33342
diff changeset
    70
    /** Element getter operation {@code obj[index]} */
23abd10384a5 8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents: 33342
diff changeset
    71
    public static final int GET_ELEMENT         = 1;
23abd10384a5 8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents: 33342
diff changeset
    72
    /** Property getter operation, subsequently invoked {@code obj.prop()} */
23abd10384a5 8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents: 33342
diff changeset
    73
    public static final int GET_METHOD_PROPERTY = 2;
23abd10384a5 8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents: 33342
diff changeset
    74
    /** Element getter operation, subsequently invoked {@code obj[index]()} */
23abd10384a5 8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents: 33342
diff changeset
    75
    public static final int GET_METHOD_ELEMENT  = 3;
23abd10384a5 8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents: 33342
diff changeset
    76
    /** Property setter operation {@code obj.prop = value} */
23abd10384a5 8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents: 33342
diff changeset
    77
    public static final int SET_PROPERTY        = 4;
23abd10384a5 8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents: 33342
diff changeset
    78
    /** Element setter operation {@code obj[index] = value} */
23abd10384a5 8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents: 33342
diff changeset
    79
    public static final int SET_ELEMENT         = 5;
48411
4ff5c5206427 8193371: Use Dynalink REMOVE operation in Nashorn
attila
parents: 47738
diff changeset
    80
    /** Property remove operation {@code delete obj.prop} */
4ff5c5206427 8193371: Use Dynalink REMOVE operation in Nashorn
attila
parents: 47738
diff changeset
    81
    public static final int REMOVE_PROPERTY     = 6;
4ff5c5206427 8193371: Use Dynalink REMOVE operation in Nashorn
attila
parents: 47738
diff changeset
    82
    /** Element remove operation {@code delete obj[index]} */
4ff5c5206427 8193371: Use Dynalink REMOVE operation in Nashorn
attila
parents: 47738
diff changeset
    83
    public static final int REMOVE_ELEMENT      = 7;
33343
23abd10384a5 8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents: 33342
diff changeset
    84
    /** Call operation {@code fn(args...)} */
48411
4ff5c5206427 8193371: Use Dynalink REMOVE operation in Nashorn
attila
parents: 47738
diff changeset
    85
    public static final int CALL                = 8;
33343
23abd10384a5 8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents: 33342
diff changeset
    86
    /** New operation {@code new Constructor(args...)} */
48411
4ff5c5206427 8193371: Use Dynalink REMOVE operation in Nashorn
attila
parents: 47738
diff changeset
    87
    public static final int NEW                 = 9;
33343
23abd10384a5 8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents: 33342
diff changeset
    88
48411
4ff5c5206427 8193371: Use Dynalink REMOVE operation in Nashorn
attila
parents: 47738
diff changeset
    89
    private static final int OPERATION_MASK = 15;
33343
23abd10384a5 8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents: 33342
diff changeset
    90
23abd10384a5 8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents: 33342
diff changeset
    91
    // Correspond to the operation indices above.
23abd10384a5 8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents: 33342
diff changeset
    92
    private static final Operation[] OPERATIONS = new Operation[] {
41842
50202a344d28 8168005: Introduce namespaces for GET, SET Dynalink operations
attila
parents: 36686
diff changeset
    93
        GET.withNamespaces(PROPERTY, ELEMENT, METHOD),
50202a344d28 8168005: Introduce namespaces for GET, SET Dynalink operations
attila
parents: 36686
diff changeset
    94
        GET.withNamespaces(ELEMENT, PROPERTY, METHOD),
50202a344d28 8168005: Introduce namespaces for GET, SET Dynalink operations
attila
parents: 36686
diff changeset
    95
        GET.withNamespaces(METHOD, PROPERTY, ELEMENT),
50202a344d28 8168005: Introduce namespaces for GET, SET Dynalink operations
attila
parents: 36686
diff changeset
    96
        GET.withNamespaces(METHOD, ELEMENT, PROPERTY),
50202a344d28 8168005: Introduce namespaces for GET, SET Dynalink operations
attila
parents: 36686
diff changeset
    97
        SET.withNamespaces(PROPERTY, ELEMENT),
50202a344d28 8168005: Introduce namespaces for GET, SET Dynalink operations
attila
parents: 36686
diff changeset
    98
        SET.withNamespaces(ELEMENT, PROPERTY),
48411
4ff5c5206427 8193371: Use Dynalink REMOVE operation in Nashorn
attila
parents: 47738
diff changeset
    99
        REMOVE.withNamespaces(PROPERTY, ELEMENT),
4ff5c5206427 8193371: Use Dynalink REMOVE operation in Nashorn
attila
parents: 47738
diff changeset
   100
        REMOVE.withNamespaces(ELEMENT, PROPERTY),
33343
23abd10384a5 8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents: 33342
diff changeset
   101
        StandardOperation.CALL,
23abd10384a5 8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents: 33342
diff changeset
   102
        StandardOperation.NEW
23abd10384a5 8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents: 33342
diff changeset
   103
    };
23abd10384a5 8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents: 33342
diff changeset
   104
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   105
    /** Flags that the call site references a scope variable (it's an identifier reference or a var declaration, not a
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   106
     * property access expression. */
48411
4ff5c5206427 8193371: Use Dynalink REMOVE operation in Nashorn
attila
parents: 47738
diff changeset
   107
    public static final int CALLSITE_SCOPE         = 1 << 4;
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   108
    /** Flags that the call site is in code that uses ECMAScript strict mode. */
48411
4ff5c5206427 8193371: Use Dynalink REMOVE operation in Nashorn
attila
parents: 47738
diff changeset
   109
    public static final int CALLSITE_STRICT        = 1 << 5;
17255
aa61d23e36e5 8013419: Streamline handling of with and eval
attila
parents: 16272
diff changeset
   110
    /** Flags that a property getter or setter call site references a scope variable that is located at a known distance
aa61d23e36e5 8013419: Streamline handling of with and eval
attila
parents: 16272
diff changeset
   111
     * in the scope chain. Such getters and setters can often be linked more optimally using these assumptions. */
48411
4ff5c5206427 8193371: Use Dynalink REMOVE operation in Nashorn
attila
parents: 47738
diff changeset
   112
    public static final int CALLSITE_FAST_SCOPE    = 1 << 6;
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 19105
diff changeset
   113
    /** Flags that a callsite type is optimistic, i.e. we might get back a wider return value than encoded in the
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 19105
diff changeset
   114
     * descriptor, and in that case we have to throw an UnwarrantedOptimismException */
48411
4ff5c5206427 8193371: Use Dynalink REMOVE operation in Nashorn
attila
parents: 47738
diff changeset
   115
    public static final int CALLSITE_OPTIMISTIC    = 1 << 7;
24738
be2026c9717c 8039746: Transform applies to calls wherever possible, for ScriptFunctions and JSObjects.
lagergren
parents: 24731
diff changeset
   116
    /** Is this really an apply that we try to call as a call? */
48411
4ff5c5206427 8193371: Use Dynalink REMOVE operation in Nashorn
attila
parents: 47738
diff changeset
   117
    public static final int CALLSITE_APPLY_TO_CALL = 1 << 8;
26377
028dad61662f 8051889: Implement block scoping in symbol assignment and scope computation
hannesw
parents: 25865
diff changeset
   118
    /** Does this a callsite for a variable declaration? */
48411
4ff5c5206427 8193371: Use Dynalink REMOVE operation in Nashorn
attila
parents: 47738
diff changeset
   119
    public static final int CALLSITE_DECLARE       = 1 << 9;
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   120
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   121
    /** Flags that the call site is profiled; Contexts that have {@code "profile.callsites"} boolean property set emit
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   122
     * code where call sites have this flag set. */
48411
4ff5c5206427 8193371: Use Dynalink REMOVE operation in Nashorn
attila
parents: 47738
diff changeset
   123
    public static final int CALLSITE_PROFILE         = 1 << 10;
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   124
    /** Flags that the call site is traced; Contexts that have {@code "trace.callsites"} property set emit code where
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   125
     * call sites have this flag set. */
48411
4ff5c5206427 8193371: Use Dynalink REMOVE operation in Nashorn
attila
parents: 47738
diff changeset
   126
    public static final int CALLSITE_TRACE           = 1 << 11;
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   127
    /** Flags that the call site linkage miss (and thus, relinking) is traced; Contexts that have the keyword
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   128
     * {@code "miss"} in their {@code "trace.callsites"} property emit code where call sites have this flag set. */
48411
4ff5c5206427 8193371: Use Dynalink REMOVE operation in Nashorn
attila
parents: 47738
diff changeset
   129
    public static final int CALLSITE_TRACE_MISSES    = 1 << 12;
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   130
    /** Flags that entry/exit to/from the method linked at call site are traced; Contexts that have the keyword
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 19105
diff changeset
   131
     * {@code "enterexit"} in their {@code "trace.callsites"} property emit code where call sites have this flag set. */
48411
4ff5c5206427 8193371: Use Dynalink REMOVE operation in Nashorn
attila
parents: 47738
diff changeset
   132
    public static final int CALLSITE_TRACE_ENTEREXIT = 1 << 13;
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   133
    /** Flags that values passed as arguments to and returned from the method linked at call site are traced; Contexts
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   134
     * that have the keyword {@code "values"} in their {@code "trace.callsites"} property emit code where call sites
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   135
     * have this flag set. */
48411
4ff5c5206427 8193371: Use Dynalink REMOVE operation in Nashorn
attila
parents: 47738
diff changeset
   136
    public static final int CALLSITE_TRACE_VALUES    = 1 << 14;
24738
be2026c9717c 8039746: Transform applies to calls wherever possible, for ScriptFunctions and JSObjects.
lagergren
parents: 24731
diff changeset
   137
be2026c9717c 8039746: Transform applies to calls wherever possible, for ScriptFunctions and JSObjects.
lagergren
parents: 24731
diff changeset
   138
    //we could have more tracing flags here, for example CALLSITE_TRACE_SCOPE, but bits are a bit precious
be2026c9717c 8039746: Transform applies to calls wherever possible, for ScriptFunctions and JSObjects.
lagergren
parents: 24731
diff changeset
   139
    //right now given the program points
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 19105
diff changeset
   140
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 19105
diff changeset
   141
    /**
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 19105
diff changeset
   142
     * Number of bits the program point is shifted to the left in the flags (lowest bit containing a program point).
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 19105
diff changeset
   143
     * Always one larger than the largest flag shift. Note that introducing a new flag halves the number of program
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 19105
diff changeset
   144
     * points we can have.
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 19105
diff changeset
   145
     * TODO: rethink if we need the various profile/trace flags or the linker can use the Context instead to query its
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 19105
diff changeset
   146
     * trace/profile settings.
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 19105
diff changeset
   147
     */
48411
4ff5c5206427 8193371: Use Dynalink REMOVE operation in Nashorn
attila
parents: 47738
diff changeset
   148
    public static final int CALLSITE_PROGRAM_POINT_SHIFT = 15;
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 19105
diff changeset
   149
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 19105
diff changeset
   150
    /**
48411
4ff5c5206427 8193371: Use Dynalink REMOVE operation in Nashorn
attila
parents: 47738
diff changeset
   151
     * Maximum program point value. We have 17 bits left over after flags, and
33343
23abd10384a5 8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents: 33342
diff changeset
   152
     * it should be plenty. Program points are local to a single function. Every
23abd10384a5 8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents: 33342
diff changeset
   153
     * function maps to a single JVM bytecode method that can have at most 65535
23abd10384a5 8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents: 33342
diff changeset
   154
     * bytes. (Large functions are synthetically split into smaller functions.)
23abd10384a5 8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents: 33342
diff changeset
   155
     * A single invokedynamic is 5 bytes; even if a method consists of only
23abd10384a5 8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents: 33342
diff changeset
   156
     * invokedynamic instructions that leaves us with at most 65535/5 = 13107
23abd10384a5 8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents: 33342
diff changeset
   157
     * program points for the largest single method; those can be expressed on
23abd10384a5 8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents: 33342
diff changeset
   158
     * 14 bits. It is true that numbering of program points is independent of
23abd10384a5 8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents: 33342
diff changeset
   159
     * bytecode representation, but if a function would need more than ~14 bits
23abd10384a5 8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents: 33342
diff changeset
   160
     * for the program points, then it is reasonable to presume splitter
23abd10384a5 8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents: 33342
diff changeset
   161
     * would've split it into several smaller functions already.
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 19105
diff changeset
   162
     */
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
   163
    public static final int MAX_PROGRAM_POINT_VALUE = (1 << 32 - CALLSITE_PROGRAM_POINT_SHIFT) - 1;
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 19105
diff changeset
   164
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 19105
diff changeset
   165
    /**
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 19105
diff changeset
   166
     * Flag mask to get the program point flags
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 19105
diff changeset
   167
     */
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 19105
diff changeset
   168
    public static final int FLAGS_MASK = (1 << CALLSITE_PROGRAM_POINT_SHIFT) - 1;
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   169
18841
9bbc4b8832b2 8010946: AccessControl.doPrivileged is broken when called from js script
attila
parents: 17255
diff changeset
   170
    private static final ClassValue<ConcurrentMap<NashornCallSiteDescriptor, NashornCallSiteDescriptor>> canonicals =
9bbc4b8832b2 8010946: AccessControl.doPrivileged is broken when called from js script
attila
parents: 17255
diff changeset
   171
            new ClassValue<ConcurrentMap<NashornCallSiteDescriptor,NashornCallSiteDescriptor>>() {
9bbc4b8832b2 8010946: AccessControl.doPrivileged is broken when called from js script
attila
parents: 17255
diff changeset
   172
        @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: 24725
diff changeset
   173
        protected ConcurrentMap<NashornCallSiteDescriptor, NashornCallSiteDescriptor> computeValue(final Class<?> type) {
18841
9bbc4b8832b2 8010946: AccessControl.doPrivileged is broken when called from js script
attila
parents: 17255
diff changeset
   174
            return new ConcurrentHashMap<>();
9bbc4b8832b2 8010946: AccessControl.doPrivileged is broken when called from js script
attila
parents: 17255
diff changeset
   175
        }
9bbc4b8832b2 8010946: AccessControl.doPrivileged is broken when called from js script
attila
parents: 17255
diff changeset
   176
    };
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   177
33341
cc9fa3638714 8139905: Add a convenience AccessControlContext factory
attila
parents: 33331
diff changeset
   178
    private static final AccessControlContext GET_LOOKUP_PERMISSION_CONTEXT =
36686
a351eacd4c42 8150218: Autoconversion SAM adapters sometimes don't get privileges
attila
parents: 34732
diff changeset
   179
            AccessControlContextFactory.createAccessControlContext(SecureLookupSupplier.GET_LOOKUP_PERMISSION_NAME);
33341
cc9fa3638714 8139905: Add a convenience AccessControlContext factory
attila
parents: 33331
diff changeset
   180
33536
3168b2a1640e 8141425: Improve caching in NashornCallSiteDescriptor
attila
parents: 33533
diff changeset
   181
    @SuppressWarnings("unchecked")
3168b2a1640e 8141425: Improve caching in NashornCallSiteDescriptor
attila
parents: 33533
diff changeset
   182
    private static final Map<String, Reference<NamedOperation>>[] NAMED_OPERATIONS =
3168b2a1640e 8141425: Improve caching in NashornCallSiteDescriptor
attila
parents: 33533
diff changeset
   183
            Stream.generate(() -> Collections.synchronizedMap(new WeakHashMap<>()))
3168b2a1640e 8141425: Improve caching in NashornCallSiteDescriptor
attila
parents: 33533
diff changeset
   184
            .limit(OPERATIONS.length).toArray(Map[]::new);
3168b2a1640e 8141425: Improve caching in NashornCallSiteDescriptor
attila
parents: 33533
diff changeset
   185
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   186
    private final int flags;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   187
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: 24725
diff changeset
   188
    /**
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: 24725
diff changeset
   189
     * Function used by {@link NashornTextifier} to represent call site flags in
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: 24725
diff changeset
   190
     * human readable form
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: 24725
diff changeset
   191
     * @param flags call site flags
34732
6605efbe8447 8144020: Remove long as an internal numeric type
hannesw
parents: 34447
diff changeset
   192
     * @param sb the string builder
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: 24725
diff changeset
   193
     */
34732
6605efbe8447 8144020: Remove long as an internal numeric type
hannesw
parents: 34447
diff changeset
   194
    public static void appendFlags(final int flags, final StringBuilder sb) {
6605efbe8447 8144020: Remove long as an internal numeric type
hannesw
parents: 34447
diff changeset
   195
        final int pp = flags >> CALLSITE_PROGRAM_POINT_SHIFT;
6605efbe8447 8144020: Remove long as an internal numeric type
hannesw
parents: 34447
diff changeset
   196
        if (pp != 0) {
6605efbe8447 8144020: Remove long as an internal numeric type
hannesw
parents: 34447
diff changeset
   197
            sb.append(" pp=").append(pp);
6605efbe8447 8144020: Remove long as an internal numeric type
hannesw
parents: 34447
diff changeset
   198
        }
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
   199
        if ((flags & CALLSITE_SCOPE) != 0) {
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
   200
            if ((flags & CALLSITE_FAST_SCOPE) != 0) {
34732
6605efbe8447 8144020: Remove long as an internal numeric type
hannesw
parents: 34447
diff changeset
   201
                sb.append(" fastscope");
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
   202
            } else {
34732
6605efbe8447 8144020: Remove long as an internal numeric type
hannesw
parents: 34447
diff changeset
   203
                sb.append(" scope");
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
   204
            }
26377
028dad61662f 8051889: Implement block scoping in symbol assignment and scope computation
hannesw
parents: 25865
diff changeset
   205
            if ((flags & CALLSITE_DECLARE) != 0) {
34732
6605efbe8447 8144020: Remove long as an internal numeric type
hannesw
parents: 34447
diff changeset
   206
                sb.append(" declare");
26377
028dad61662f 8051889: Implement block scoping in symbol assignment and scope computation
hannesw
parents: 25865
diff changeset
   207
            }
34732
6605efbe8447 8144020: Remove long as an internal numeric type
hannesw
parents: 34447
diff changeset
   208
        } else {
6605efbe8447 8144020: Remove long as an internal numeric type
hannesw
parents: 34447
diff changeset
   209
            assert (flags & CALLSITE_FAST_SCOPE) == 0 : "can't be fastscope without scope";
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
   210
        }
24738
be2026c9717c 8039746: Transform applies to calls wherever possible, for ScriptFunctions and JSObjects.
lagergren
parents: 24731
diff changeset
   211
        if ((flags & CALLSITE_APPLY_TO_CALL) != 0) {
34732
6605efbe8447 8144020: Remove long as an internal numeric type
hannesw
parents: 34447
diff changeset
   212
            sb.append(" apply2call");
24738
be2026c9717c 8039746: Transform applies to calls wherever possible, for ScriptFunctions and JSObjects.
lagergren
parents: 24731
diff changeset
   213
        }
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
   214
        if ((flags & CALLSITE_STRICT) != 0) {
34732
6605efbe8447 8144020: Remove long as an internal numeric type
hannesw
parents: 34447
diff changeset
   215
            sb.append(" strict");
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
   216
        }
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
   217
    }
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
   218
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   219
    /**
33343
23abd10384a5 8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents: 33342
diff changeset
   220
     * Given call site flags, returns the operation name encoded in them.
23abd10384a5 8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents: 33342
diff changeset
   221
     * @param flags flags
23abd10384a5 8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents: 33342
diff changeset
   222
     * @return the operation name
23abd10384a5 8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents: 33342
diff changeset
   223
     */
23abd10384a5 8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents: 33342
diff changeset
   224
    public static String getOperationName(final int flags) {
23abd10384a5 8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents: 33342
diff changeset
   225
        switch(flags & OPERATION_MASK) {
23abd10384a5 8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents: 33342
diff changeset
   226
        case 0: return "GET_PROPERTY";
23abd10384a5 8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents: 33342
diff changeset
   227
        case 1: return "GET_ELEMENT";
23abd10384a5 8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents: 33342
diff changeset
   228
        case 2: return "GET_METHOD_PROPERTY";
23abd10384a5 8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents: 33342
diff changeset
   229
        case 3: return "GET_METHOD_ELEMENT";
23abd10384a5 8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents: 33342
diff changeset
   230
        case 4: return "SET_PROPERTY";
23abd10384a5 8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents: 33342
diff changeset
   231
        case 5: return "SET_ELEMENT";
48411
4ff5c5206427 8193371: Use Dynalink REMOVE operation in Nashorn
attila
parents: 47738
diff changeset
   232
        case 6: return "REMOVE_PROPERTY";
4ff5c5206427 8193371: Use Dynalink REMOVE operation in Nashorn
attila
parents: 47738
diff changeset
   233
        case 7: return "REMOVE_ELEMENT";
4ff5c5206427 8193371: Use Dynalink REMOVE operation in Nashorn
attila
parents: 47738
diff changeset
   234
        case 8: return "CALL";
4ff5c5206427 8193371: Use Dynalink REMOVE operation in Nashorn
attila
parents: 47738
diff changeset
   235
        case 9: return "NEW";
33343
23abd10384a5 8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents: 33342
diff changeset
   236
        default: throw new AssertionError();
23abd10384a5 8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents: 33342
diff changeset
   237
        }
23abd10384a5 8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents: 33342
diff changeset
   238
    }
23abd10384a5 8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents: 33342
diff changeset
   239
23abd10384a5 8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents: 33342
diff changeset
   240
    /**
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   241
     * Retrieves a Nashorn call site descriptor with the specified values. Since call site descriptors are immutable
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   242
     * this method is at liberty to retrieve canonicalized instances (although it is not guaranteed it will do so).
18841
9bbc4b8832b2 8010946: AccessControl.doPrivileged is broken when called from js script
attila
parents: 17255
diff changeset
   243
     * @param lookup the lookup describing the script
33343
23abd10384a5 8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents: 33342
diff changeset
   244
     * @param name the name at the call site. Can not be null, but it can be empty.
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   245
     * @param methodType the method type at the call site
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   246
     * @param flags Nashorn-specific call site flags
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   247
     * @return a call site descriptor with the specified values.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   248
     */
18841
9bbc4b8832b2 8010946: AccessControl.doPrivileged is broken when called from js script
attila
parents: 17255
diff changeset
   249
    public static NashornCallSiteDescriptor get(final MethodHandles.Lookup lookup, final String name,
9bbc4b8832b2 8010946: AccessControl.doPrivileged is broken when called from js script
attila
parents: 17255
diff changeset
   250
            final MethodType methodType, final int flags) {
33536
3168b2a1640e 8141425: Improve caching in NashornCallSiteDescriptor
attila
parents: 33533
diff changeset
   251
        final int opIndex = flags & OPERATION_MASK;
3168b2a1640e 8141425: Improve caching in NashornCallSiteDescriptor
attila
parents: 33533
diff changeset
   252
        final Operation baseOp = OPERATIONS[opIndex];
33343
23abd10384a5 8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents: 33342
diff changeset
   253
        final String decodedName = NameCodec.decode(name);
33536
3168b2a1640e 8141425: Improve caching in NashornCallSiteDescriptor
attila
parents: 33533
diff changeset
   254
        final Operation op = decodedName.isEmpty() ? baseOp : getNamedOperation(decodedName, opIndex, baseOp);
33343
23abd10384a5 8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents: 33342
diff changeset
   255
        return get(lookup, op, methodType, flags);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   256
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   257
33536
3168b2a1640e 8141425: Improve caching in NashornCallSiteDescriptor
attila
parents: 33533
diff changeset
   258
    private static NamedOperation getNamedOperation(final String name, final int opIndex, final Operation baseOp) {
3168b2a1640e 8141425: Improve caching in NashornCallSiteDescriptor
attila
parents: 33533
diff changeset
   259
        final Map<String, Reference<NamedOperation>> namedOps = NAMED_OPERATIONS[opIndex];
3168b2a1640e 8141425: Improve caching in NashornCallSiteDescriptor
attila
parents: 33533
diff changeset
   260
        final Reference<NamedOperation> ref = namedOps.get(name);
3168b2a1640e 8141425: Improve caching in NashornCallSiteDescriptor
attila
parents: 33533
diff changeset
   261
        if (ref != null) {
3168b2a1640e 8141425: Improve caching in NashornCallSiteDescriptor
attila
parents: 33533
diff changeset
   262
            final NamedOperation existing = ref.get();
3168b2a1640e 8141425: Improve caching in NashornCallSiteDescriptor
attila
parents: 33533
diff changeset
   263
            if (existing != null) {
3168b2a1640e 8141425: Improve caching in NashornCallSiteDescriptor
attila
parents: 33533
diff changeset
   264
                return existing;
3168b2a1640e 8141425: Improve caching in NashornCallSiteDescriptor
attila
parents: 33533
diff changeset
   265
            }
3168b2a1640e 8141425: Improve caching in NashornCallSiteDescriptor
attila
parents: 33533
diff changeset
   266
        }
41842
50202a344d28 8168005: Introduce namespaces for GET, SET Dynalink operations
attila
parents: 36686
diff changeset
   267
        final NamedOperation newOp = baseOp.named(name);
33536
3168b2a1640e 8141425: Improve caching in NashornCallSiteDescriptor
attila
parents: 33533
diff changeset
   268
        namedOps.put(name, new WeakReference<>(newOp));
3168b2a1640e 8141425: Improve caching in NashornCallSiteDescriptor
attila
parents: 33533
diff changeset
   269
        return newOp;
3168b2a1640e 8141425: Improve caching in NashornCallSiteDescriptor
attila
parents: 33533
diff changeset
   270
    }
3168b2a1640e 8141425: Improve caching in NashornCallSiteDescriptor
attila
parents: 33533
diff changeset
   271
33343
23abd10384a5 8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents: 33342
diff changeset
   272
    private static NashornCallSiteDescriptor get(final MethodHandles.Lookup lookup, final Operation operation, final MethodType methodType, final int flags) {
23abd10384a5 8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents: 33342
diff changeset
   273
        final NashornCallSiteDescriptor csd = new NashornCallSiteDescriptor(lookup, operation, methodType, flags);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   274
        // Many of these call site descriptors are identical (e.g. every getter for a property color will be
33536
3168b2a1640e 8141425: Improve caching in NashornCallSiteDescriptor
attila
parents: 33533
diff changeset
   275
        // "GET_PROPERTY:color(Object)Object", so it makes sense canonicalizing them. Make an exception for
3168b2a1640e 8141425: Improve caching in NashornCallSiteDescriptor
attila
parents: 33533
diff changeset
   276
        // optimistic call site descriptors, as they also carry a program point making them unique.
3168b2a1640e 8141425: Improve caching in NashornCallSiteDescriptor
attila
parents: 33533
diff changeset
   277
        if (csd.isOptimistic()) {
3168b2a1640e 8141425: Improve caching in NashornCallSiteDescriptor
attila
parents: 33533
diff changeset
   278
            return csd;
3168b2a1640e 8141425: Improve caching in NashornCallSiteDescriptor
attila
parents: 33533
diff changeset
   279
        }
3168b2a1640e 8141425: Improve caching in NashornCallSiteDescriptor
attila
parents: 33533
diff changeset
   280
        final NashornCallSiteDescriptor canonical = canonicals.get(lookup.lookupClass()).putIfAbsent(csd, csd);
18841
9bbc4b8832b2 8010946: AccessControl.doPrivileged is broken when called from js script
attila
parents: 17255
diff changeset
   281
        return canonical != null ? canonical : csd;
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   282
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   283
33343
23abd10384a5 8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents: 33342
diff changeset
   284
    private NashornCallSiteDescriptor(final MethodHandles.Lookup lookup, final Operation operation, final MethodType methodType, final int flags) {
23abd10384a5 8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents: 33342
diff changeset
   285
        super(lookup, operation, methodType);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   286
        this.flags = flags;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   287
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   288
33342
86b4260bb17a 8139919: Make CallSiteDescriptor a concrete class
attila
parents: 33341
diff changeset
   289
    static Lookup getLookupInternal(final CallSiteDescriptor csd) {
33331
273e6a10de22 8139435: Make sure CallSiteDescriptor.getLookup is subject to a security check
attila
parents: 33330
diff changeset
   290
        if (csd instanceof NashornCallSiteDescriptor) {
33342
86b4260bb17a 8139919: Make CallSiteDescriptor a concrete class
attila
parents: 33341
diff changeset
   291
            return ((NashornCallSiteDescriptor)csd).getLookupPrivileged();
33331
273e6a10de22 8139435: Make sure CallSiteDescriptor.getLookup is subject to a security check
attila
parents: 33330
diff changeset
   292
        }
33342
86b4260bb17a 8139919: Make CallSiteDescriptor a concrete class
attila
parents: 33341
diff changeset
   293
        return AccessController.doPrivileged((PrivilegedAction<Lookup>)()->csd.getLookup(), GET_LOOKUP_PERMISSION_CONTEXT);
18841
9bbc4b8832b2 8010946: AccessControl.doPrivileged is broken when called from js script
attila
parents: 17255
diff changeset
   294
    }
9bbc4b8832b2 8010946: AccessControl.doPrivileged is broken when called from js script
attila
parents: 17255
diff changeset
   295
9bbc4b8832b2 8010946: AccessControl.doPrivileged is broken when called from js script
attila
parents: 17255
diff changeset
   296
    @Override
33342
86b4260bb17a 8139919: Make CallSiteDescriptor a concrete class
attila
parents: 33341
diff changeset
   297
    public boolean equals(final Object obj) {
86b4260bb17a 8139919: Make CallSiteDescriptor a concrete class
attila
parents: 33341
diff changeset
   298
        return super.equals(obj) && flags == ((NashornCallSiteDescriptor)obj).flags;
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   299
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   300
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   301
    @Override
33342
86b4260bb17a 8139919: Make CallSiteDescriptor a concrete class
attila
parents: 33341
diff changeset
   302
    public int hashCode() {
86b4260bb17a 8139919: Make CallSiteDescriptor a concrete class
attila
parents: 33341
diff changeset
   303
        return super.hashCode() ^ flags;
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   304
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   305
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   306
    /**
33343
23abd10384a5 8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents: 33342
diff changeset
   307
     * Returns the named operand in the passed descriptor's operation.
23abd10384a5 8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents: 33342
diff changeset
   308
     * Equivalent to
23abd10384a5 8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents: 33342
diff changeset
   309
     * {@code ((NamedOperation)desc.getOperation()).getName().toString()} for
23abd10384a5 8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents: 33342
diff changeset
   310
     * descriptors with a named operand. For descriptors without named operands
23abd10384a5 8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents: 33342
diff changeset
   311
     * returns null.
23abd10384a5 8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents: 33342
diff changeset
   312
     * @param desc the call site descriptors
23abd10384a5 8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents: 33342
diff changeset
   313
     * @return the named operand in this descriptor's operation.
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   314
     */
33343
23abd10384a5 8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents: 33342
diff changeset
   315
    public static String getOperand(final CallSiteDescriptor desc) {
23abd10384a5 8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents: 33342
diff changeset
   316
        final Operation operation = desc.getOperation();
23abd10384a5 8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents: 33342
diff changeset
   317
        return operation instanceof NamedOperation ? ((NamedOperation)operation).getName().toString() : null;
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   318
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   319
41842
50202a344d28 8168005: Introduce namespaces for GET, SET Dynalink operations
attila
parents: 36686
diff changeset
   320
    private static StandardNamespace findFirstStandardNamespace(final CallSiteDescriptor desc) {
50202a344d28 8168005: Introduce namespaces for GET, SET Dynalink operations
attila
parents: 36686
diff changeset
   321
        return StandardNamespace.findFirst(desc.getOperation());
50202a344d28 8168005: Introduce namespaces for GET, SET Dynalink operations
attila
parents: 36686
diff changeset
   322
    }
50202a344d28 8168005: Introduce namespaces for GET, SET Dynalink operations
attila
parents: 36686
diff changeset
   323
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   324
    /**
41842
50202a344d28 8168005: Introduce namespaces for GET, SET Dynalink operations
attila
parents: 36686
diff changeset
   325
     * Returns true if the operation of the call descriptor is operating on the method namespace first.
50202a344d28 8168005: Introduce namespaces for GET, SET Dynalink operations
attila
parents: 36686
diff changeset
   326
     * @param desc the call descriptor in question.
50202a344d28 8168005: Introduce namespaces for GET, SET Dynalink operations
attila
parents: 36686
diff changeset
   327
     * @return true if the operation of the call descriptor is operating on the method namespace first.
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   328
     */
41842
50202a344d28 8168005: Introduce namespaces for GET, SET Dynalink operations
attila
parents: 36686
diff changeset
   329
    public static boolean isMethodFirstOperation(final CallSiteDescriptor desc) {
50202a344d28 8168005: Introduce namespaces for GET, SET Dynalink operations
attila
parents: 36686
diff changeset
   330
        return findFirstStandardNamespace(desc) == StandardNamespace.METHOD;
50202a344d28 8168005: Introduce namespaces for GET, SET Dynalink operations
attila
parents: 36686
diff changeset
   331
    }
50202a344d28 8168005: Introduce namespaces for GET, SET Dynalink operations
attila
parents: 36686
diff changeset
   332
50202a344d28 8168005: Introduce namespaces for GET, SET Dynalink operations
attila
parents: 36686
diff changeset
   333
    /**
50202a344d28 8168005: Introduce namespaces for GET, SET Dynalink operations
attila
parents: 36686
diff changeset
   334
     * Returns true if there's a namespace operation in the call descriptor and it is operating on at least
50202a344d28 8168005: Introduce namespaces for GET, SET Dynalink operations
attila
parents: 36686
diff changeset
   335
     * one {@link StandardNamespace}. This method is only needed for exported linkers, since internal linkers
50202a344d28 8168005: Introduce namespaces for GET, SET Dynalink operations
attila
parents: 36686
diff changeset
   336
     * always operate on Nashorn-generated call sites, and they always operate on standard namespaces only.
50202a344d28 8168005: Introduce namespaces for GET, SET Dynalink operations
attila
parents: 36686
diff changeset
   337
     * @param desc the call descriptor in question.
50202a344d28 8168005: Introduce namespaces for GET, SET Dynalink operations
attila
parents: 36686
diff changeset
   338
     * @return true if the operation of the call descriptor is operating on at least one standard namespace.
50202a344d28 8168005: Introduce namespaces for GET, SET Dynalink operations
attila
parents: 36686
diff changeset
   339
     */
50202a344d28 8168005: Introduce namespaces for GET, SET Dynalink operations
attila
parents: 36686
diff changeset
   340
    public static boolean hasStandardNamespace(final CallSiteDescriptor desc) {
50202a344d28 8168005: Introduce namespaces for GET, SET Dynalink operations
attila
parents: 36686
diff changeset
   341
        return findFirstStandardNamespace(desc) != null;
32049
af8f6292d54d 8133119: Error message associated with TypeError for call and new should include stringified Node
sundar
parents: 32048
diff changeset
   342
    }
af8f6292d54d 8133119: Error message associated with TypeError for call and new should include stringified Node
sundar
parents: 32048
diff changeset
   343
af8f6292d54d 8133119: Error message associated with TypeError for call and new should include stringified Node
sundar
parents: 32048
diff changeset
   344
    /**
41842
50202a344d28 8168005: Introduce namespaces for GET, SET Dynalink operations
attila
parents: 36686
diff changeset
   345
     * Returns the base operation in this call site descriptor after unwrapping it from both a named operation
50202a344d28 8168005: Introduce namespaces for GET, SET Dynalink operations
attila
parents: 36686
diff changeset
   346
     * and a namespace operation.
33343
23abd10384a5 8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents: 33342
diff changeset
   347
     * @param desc the call site descriptor.
41842
50202a344d28 8168005: Introduce namespaces for GET, SET Dynalink operations
attila
parents: 36686
diff changeset
   348
     * @return the base operation in this call site descriptor.
32049
af8f6292d54d 8133119: Error message associated with TypeError for call and new should include stringified Node
sundar
parents: 32048
diff changeset
   349
     */
41842
50202a344d28 8168005: Introduce namespaces for GET, SET Dynalink operations
attila
parents: 36686
diff changeset
   350
    public static Operation getBaseOperation(final CallSiteDescriptor desc) {
50202a344d28 8168005: Introduce namespaces for GET, SET Dynalink operations
attila
parents: 36686
diff changeset
   351
        return NamespaceOperation.getBaseOperation(NamedOperation.getBaseOperation(desc.getOperation()));
32049
af8f6292d54d 8133119: Error message associated with TypeError for call and new should include stringified Node
sundar
parents: 32048
diff changeset
   352
    }
af8f6292d54d 8133119: Error message associated with TypeError for call and new should include stringified Node
sundar
parents: 32048
diff changeset
   353
af8f6292d54d 8133119: Error message associated with TypeError for call and new should include stringified Node
sundar
parents: 32048
diff changeset
   354
    /**
41842
50202a344d28 8168005: Introduce namespaces for GET, SET Dynalink operations
attila
parents: 36686
diff changeset
   355
     * Returns the standard operation that is the base operation in this call site descriptor.
50202a344d28 8168005: Introduce namespaces for GET, SET Dynalink operations
attila
parents: 36686
diff changeset
   356
     * @param desc the call site descriptor.
50202a344d28 8168005: Introduce namespaces for GET, SET Dynalink operations
attila
parents: 36686
diff changeset
   357
     * @return the standard operation that is the base operation in this call site descriptor.
50202a344d28 8168005: Introduce namespaces for GET, SET Dynalink operations
attila
parents: 36686
diff changeset
   358
     * @throws ClassCastException if the base operation is not a standard operation. This method is only
50202a344d28 8168005: Introduce namespaces for GET, SET Dynalink operations
attila
parents: 36686
diff changeset
   359
     * safe to use when the base operation is known to be a standard operation (e.g. all Nashorn call sites
50202a344d28 8168005: Introduce namespaces for GET, SET Dynalink operations
attila
parents: 36686
diff changeset
   360
     * are such, so it's safe to use from internal linkers).
50202a344d28 8168005: Introduce namespaces for GET, SET Dynalink operations
attila
parents: 36686
diff changeset
   361
     */
50202a344d28 8168005: Introduce namespaces for GET, SET Dynalink operations
attila
parents: 36686
diff changeset
   362
    public static StandardOperation getStandardOperation(final CallSiteDescriptor desc) {
50202a344d28 8168005: Introduce namespaces for GET, SET Dynalink operations
attila
parents: 36686
diff changeset
   363
        return (StandardOperation)getBaseOperation(desc);
50202a344d28 8168005: Introduce namespaces for GET, SET Dynalink operations
attila
parents: 36686
diff changeset
   364
    }
50202a344d28 8168005: Introduce namespaces for GET, SET Dynalink operations
attila
parents: 36686
diff changeset
   365
50202a344d28 8168005: Introduce namespaces for GET, SET Dynalink operations
attila
parents: 36686
diff changeset
   366
    /**
50202a344d28 8168005: Introduce namespaces for GET, SET Dynalink operations
attila
parents: 36686
diff changeset
   367
     * Returns true if the passed call site descriptor contains the specified standard operation on the
50202a344d28 8168005: Introduce namespaces for GET, SET Dynalink operations
attila
parents: 36686
diff changeset
   368
     * specified standard namespace.
33345
ef8c859f7992 8140273: restore use of CompositeOperation.contains where it is needed
attila
parents: 33343
diff changeset
   369
     * @param desc the call site descriptor.
ef8c859f7992 8140273: restore use of CompositeOperation.contains where it is needed
attila
parents: 33343
diff changeset
   370
     * @param operation the operation whose presence is tested.
41842
50202a344d28 8168005: Introduce namespaces for GET, SET Dynalink operations
attila
parents: 36686
diff changeset
   371
     * @param namespace the namespace on which the operation operates.
50202a344d28 8168005: Introduce namespaces for GET, SET Dynalink operations
attila
parents: 36686
diff changeset
   372
     * @return Returns true if the call site descriptor contains the specified standard operation on the
50202a344d28 8168005: Introduce namespaces for GET, SET Dynalink operations
attila
parents: 36686
diff changeset
   373
     * specified standard namespace.
33345
ef8c859f7992 8140273: restore use of CompositeOperation.contains where it is needed
attila
parents: 33343
diff changeset
   374
     */
41842
50202a344d28 8168005: Introduce namespaces for GET, SET Dynalink operations
attila
parents: 36686
diff changeset
   375
    public static boolean contains(final CallSiteDescriptor desc, final StandardOperation operation, final StandardNamespace namespace) {
50202a344d28 8168005: Introduce namespaces for GET, SET Dynalink operations
attila
parents: 36686
diff changeset
   376
        return NamespaceOperation.contains(NamedOperation.getBaseOperation(desc.getOperation()), operation, namespace);
33345
ef8c859f7992 8140273: restore use of CompositeOperation.contains where it is needed
attila
parents: 33343
diff changeset
   377
    }
ef8c859f7992 8140273: restore use of CompositeOperation.contains where it is needed
attila
parents: 33343
diff changeset
   378
ef8c859f7992 8140273: restore use of CompositeOperation.contains where it is needed
attila
parents: 33343
diff changeset
   379
    /**
33343
23abd10384a5 8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents: 33342
diff changeset
   380
     * Returns the error message to be used when CALL or NEW is used on a non-function.
32049
af8f6292d54d 8133119: Error message associated with TypeError for call and new should include stringified Node
sundar
parents: 32048
diff changeset
   381
     *
33343
23abd10384a5 8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents: 33342
diff changeset
   382
     * @param obj object on which CALL or NEW is used
32049
af8f6292d54d 8133119: Error message associated with TypeError for call and new should include stringified Node
sundar
parents: 32048
diff changeset
   383
     * @return error message
af8f6292d54d 8133119: Error message associated with TypeError for call and new should include stringified Node
sundar
parents: 32048
diff changeset
   384
     */
41842
50202a344d28 8168005: Introduce namespaces for GET, SET Dynalink operations
attila
parents: 36686
diff changeset
   385
    private String getFunctionErrorMessage(final Object obj) {
50202a344d28 8168005: Introduce namespaces for GET, SET Dynalink operations
attila
parents: 36686
diff changeset
   386
        final String funcDesc = getOperand(this);
32049
af8f6292d54d 8133119: Error message associated with TypeError for call and new should include stringified Node
sundar
parents: 32048
diff changeset
   387
        return funcDesc != null? funcDesc : ScriptRuntime.safeToString(obj);
af8f6292d54d 8133119: Error message associated with TypeError for call and new should include stringified Node
sundar
parents: 32048
diff changeset
   388
    }
af8f6292d54d 8133119: Error message associated with TypeError for call and new should include stringified Node
sundar
parents: 32048
diff changeset
   389
af8f6292d54d 8133119: Error message associated with TypeError for call and new should include stringified Node
sundar
parents: 32048
diff changeset
   390
    /**
33343
23abd10384a5 8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents: 33342
diff changeset
   391
     * Returns the error message to be used when CALL or NEW is used on a non-function.
32049
af8f6292d54d 8133119: Error message associated with TypeError for call and new should include stringified Node
sundar
parents: 32048
diff changeset
   392
     *
af8f6292d54d 8133119: Error message associated with TypeError for call and new should include stringified Node
sundar
parents: 32048
diff changeset
   393
     * @param desc call site descriptor
33343
23abd10384a5 8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents: 33342
diff changeset
   394
     * @param obj object on which CALL or NEW is used
32049
af8f6292d54d 8133119: Error message associated with TypeError for call and new should include stringified Node
sundar
parents: 32048
diff changeset
   395
     * @return error message
af8f6292d54d 8133119: Error message associated with TypeError for call and new should include stringified Node
sundar
parents: 32048
diff changeset
   396
     */
af8f6292d54d 8133119: Error message associated with TypeError for call and new should include stringified Node
sundar
parents: 32048
diff changeset
   397
    public static String getFunctionErrorMessage(final CallSiteDescriptor desc, final Object obj) {
af8f6292d54d 8133119: Error message associated with TypeError for call and new should include stringified Node
sundar
parents: 32048
diff changeset
   398
        return desc instanceof NashornCallSiteDescriptor ?
af8f6292d54d 8133119: Error message associated with TypeError for call and new should include stringified Node
sundar
parents: 32048
diff changeset
   399
                ((NashornCallSiteDescriptor)desc).getFunctionErrorMessage(obj) :
af8f6292d54d 8133119: Error message associated with TypeError for call and new should include stringified Node
sundar
parents: 32048
diff changeset
   400
                ScriptRuntime.safeToString(obj);
af8f6292d54d 8133119: Error message associated with TypeError for call and new should include stringified Node
sundar
parents: 32048
diff changeset
   401
    }
af8f6292d54d 8133119: Error message associated with TypeError for call and new should include stringified Node
sundar
parents: 32048
diff changeset
   402
af8f6292d54d 8133119: Error message associated with TypeError for call and new should include stringified Node
sundar
parents: 32048
diff changeset
   403
    /**
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   404
     * Returns the Nashorn-specific flags for this call site descriptor.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   405
     * @param desc the descriptor. It can be any kind of a call site descriptor, not necessarily a
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   406
     * {@code NashornCallSiteDescriptor}. This allows for graceful interoperability when linking Nashorn with code
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   407
     * generated outside of Nashorn.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   408
     * @return the Nashorn-specific flags for the call site, or 0 if the passed descriptor is not a Nashorn call site
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   409
     * descriptor.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   410
     */
26765
97501edd2979 8047764: Indexed or polymorphic set on global affects Object.prototype
hannesw
parents: 26377
diff changeset
   411
    public static int getFlags(final CallSiteDescriptor desc) {
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   412
        return desc instanceof NashornCallSiteDescriptor ? ((NashornCallSiteDescriptor)desc).flags : 0;
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
     * Returns true if this descriptor has the specified flag set, see {@code CALLSITE_*} constants in this class.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   417
     * @param flag the tested flag
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   418
     * @return true if the flag is set, false otherwise
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   419
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   420
    private boolean isFlag(final int flag) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   421
        return (flags & flag) != 0;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   422
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   423
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   424
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   425
     * Returns true if this descriptor has the specified flag set, see {@code CALLSITE_*} constants in this class.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   426
     * @param desc the descriptor. It can be any kind of a call site descriptor, not necessarily a
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   427
     * {@code NashornCallSiteDescriptor}. This allows for graceful interoperability when linking Nashorn with code
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   428
     * generated outside of Nashorn.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   429
     * @param flag the tested flag
31738
5ad3dfcf3507 8130888: Typos in nashorn sources
sundar
parents: 26765
diff changeset
   430
     * @return true if the flag is set, false otherwise (it will be false if the descriptor is not a Nashorn call site
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   431
     * descriptor).
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   432
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   433
    private static boolean isFlag(final CallSiteDescriptor desc, final int flag) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   434
        return (getFlags(desc) & flag) != 0;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   435
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   436
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   437
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   438
     * Returns true if this descriptor is a Nashorn call site descriptor and has the {@link  #CALLSITE_SCOPE} flag set.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   439
     * @param desc the descriptor. It can be any kind of a call site descriptor, not necessarily a
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   440
     * {@code NashornCallSiteDescriptor}. This allows for graceful interoperability when linking Nashorn with code
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   441
     * generated outside of Nashorn.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   442
     * @return true if the descriptor is a Nashorn call site descriptor, and the flag is set, false otherwise.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   443
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   444
    public static boolean isScope(final CallSiteDescriptor desc) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   445
        return isFlag(desc, CALLSITE_SCOPE);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   446
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   447
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   448
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   449
     * Returns true if this descriptor is a Nashorn call site descriptor and has the {@link  #CALLSITE_FAST_SCOPE} flag set.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   450
     * @param desc the descriptor. It can be any kind of a call site descriptor, not necessarily a
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   451
     * {@code NashornCallSiteDescriptor}. This allows for graceful interoperability when linking Nashorn with code
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   452
     * generated outside of Nashorn.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   453
     * @return true if the descriptor is a Nashorn call site descriptor, and the flag is set, false otherwise.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   454
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   455
    public static boolean isFastScope(final CallSiteDescriptor desc) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   456
        return isFlag(desc, CALLSITE_FAST_SCOPE);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   457
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   458
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   459
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   460
     * Returns true if this descriptor is a Nashorn call site descriptor and has the {@link  #CALLSITE_STRICT} flag set.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   461
     * @param desc the descriptor. It can be any kind of a call site descriptor, not necessarily a
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   462
     * {@code NashornCallSiteDescriptor}. This allows for graceful interoperability when linking Nashorn with code
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   463
     * generated outside of Nashorn.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   464
     * @return true if the descriptor is a Nashorn call site descriptor, and the flag is set, false otherwise.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   465
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   466
    public static boolean isStrict(final CallSiteDescriptor desc) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   467
        return isFlag(desc, CALLSITE_STRICT);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   468
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   469
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 19105
diff changeset
   470
    /**
24738
be2026c9717c 8039746: Transform applies to calls wherever possible, for ScriptFunctions and JSObjects.
lagergren
parents: 24731
diff changeset
   471
     * Returns true if this is an apply call that we try to call as
be2026c9717c 8039746: Transform applies to calls wherever possible, for ScriptFunctions and JSObjects.
lagergren
parents: 24731
diff changeset
   472
     * a "call"
be2026c9717c 8039746: Transform applies to calls wherever possible, for ScriptFunctions and JSObjects.
lagergren
parents: 24731
diff changeset
   473
     * @param desc descriptor
be2026c9717c 8039746: Transform applies to calls wherever possible, for ScriptFunctions and JSObjects.
lagergren
parents: 24731
diff changeset
   474
     * @return true if apply to call
be2026c9717c 8039746: Transform applies to calls wherever possible, for ScriptFunctions and JSObjects.
lagergren
parents: 24731
diff changeset
   475
     */
be2026c9717c 8039746: Transform applies to calls wherever possible, for ScriptFunctions and JSObjects.
lagergren
parents: 24731
diff changeset
   476
    public static boolean isApplyToCall(final CallSiteDescriptor desc) {
be2026c9717c 8039746: Transform applies to calls wherever possible, for ScriptFunctions and JSObjects.
lagergren
parents: 24731
diff changeset
   477
        return isFlag(desc, CALLSITE_APPLY_TO_CALL);
be2026c9717c 8039746: Transform applies to calls wherever possible, for ScriptFunctions and JSObjects.
lagergren
parents: 24731
diff changeset
   478
    }
be2026c9717c 8039746: Transform applies to calls wherever possible, for ScriptFunctions and JSObjects.
lagergren
parents: 24731
diff changeset
   479
be2026c9717c 8039746: Transform applies to calls wherever possible, for ScriptFunctions and JSObjects.
lagergren
parents: 24731
diff changeset
   480
    /**
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 19105
diff changeset
   481
     * Is this an optimistic call site
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 19105
diff changeset
   482
     * @param desc descriptor
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 19105
diff changeset
   483
     * @return true if optimistic
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 19105
diff changeset
   484
     */
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 19105
diff changeset
   485
    public static boolean isOptimistic(final CallSiteDescriptor desc) {
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 19105
diff changeset
   486
        return isFlag(desc, CALLSITE_OPTIMISTIC);
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 19105
diff changeset
   487
    }
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 19105
diff changeset
   488
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 19105
diff changeset
   489
    /**
26377
028dad61662f 8051889: Implement block scoping in symbol assignment and scope computation
hannesw
parents: 25865
diff changeset
   490
     * Does this callsite contain a declaration for its target?
028dad61662f 8051889: Implement block scoping in symbol assignment and scope computation
hannesw
parents: 25865
diff changeset
   491
     * @param desc descriptor
028dad61662f 8051889: Implement block scoping in symbol assignment and scope computation
hannesw
parents: 25865
diff changeset
   492
     * @return true if contains declaration
028dad61662f 8051889: Implement block scoping in symbol assignment and scope computation
hannesw
parents: 25865
diff changeset
   493
     */
028dad61662f 8051889: Implement block scoping in symbol assignment and scope computation
hannesw
parents: 25865
diff changeset
   494
    public static boolean isDeclaration(final CallSiteDescriptor desc) {
028dad61662f 8051889: Implement block scoping in symbol assignment and scope computation
hannesw
parents: 25865
diff changeset
   495
        return isFlag(desc, CALLSITE_DECLARE);
028dad61662f 8051889: Implement block scoping in symbol assignment and scope computation
hannesw
parents: 25865
diff changeset
   496
    }
028dad61662f 8051889: Implement block scoping in symbol assignment and scope computation
hannesw
parents: 25865
diff changeset
   497
028dad61662f 8051889: Implement block scoping in symbol assignment and scope computation
hannesw
parents: 25865
diff changeset
   498
    /**
26765
97501edd2979 8047764: Indexed or polymorphic set on global affects Object.prototype
hannesw
parents: 26377
diff changeset
   499
     * Returns true if {@code flags} has the {@link  #CALLSITE_STRICT} bit set.
97501edd2979 8047764: Indexed or polymorphic set on global affects Object.prototype
hannesw
parents: 26377
diff changeset
   500
     * @param flags the flags
97501edd2979 8047764: Indexed or polymorphic set on global affects Object.prototype
hannesw
parents: 26377
diff changeset
   501
     * @return true if the flag is set, false otherwise.
97501edd2979 8047764: Indexed or polymorphic set on global affects Object.prototype
hannesw
parents: 26377
diff changeset
   502
     */
97501edd2979 8047764: Indexed or polymorphic set on global affects Object.prototype
hannesw
parents: 26377
diff changeset
   503
    public static boolean isStrictFlag(final int flags) {
97501edd2979 8047764: Indexed or polymorphic set on global affects Object.prototype
hannesw
parents: 26377
diff changeset
   504
        return (flags & CALLSITE_STRICT) != 0;
97501edd2979 8047764: Indexed or polymorphic set on global affects Object.prototype
hannesw
parents: 26377
diff changeset
   505
    }
97501edd2979 8047764: Indexed or polymorphic set on global affects Object.prototype
hannesw
parents: 26377
diff changeset
   506
97501edd2979 8047764: Indexed or polymorphic set on global affects Object.prototype
hannesw
parents: 26377
diff changeset
   507
    /**
97501edd2979 8047764: Indexed or polymorphic set on global affects Object.prototype
hannesw
parents: 26377
diff changeset
   508
     * Returns true if {@code flags} has the {@link  #CALLSITE_SCOPE} bit set.
97501edd2979 8047764: Indexed or polymorphic set on global affects Object.prototype
hannesw
parents: 26377
diff changeset
   509
     * @param flags the flags
97501edd2979 8047764: Indexed or polymorphic set on global affects Object.prototype
hannesw
parents: 26377
diff changeset
   510
     * @return true if the flag is set, false otherwise.
97501edd2979 8047764: Indexed or polymorphic set on global affects Object.prototype
hannesw
parents: 26377
diff changeset
   511
     */
97501edd2979 8047764: Indexed or polymorphic set on global affects Object.prototype
hannesw
parents: 26377
diff changeset
   512
    public static boolean isScopeFlag(final int flags) {
97501edd2979 8047764: Indexed or polymorphic set on global affects Object.prototype
hannesw
parents: 26377
diff changeset
   513
        return (flags & CALLSITE_SCOPE) != 0;
97501edd2979 8047764: Indexed or polymorphic set on global affects Object.prototype
hannesw
parents: 26377
diff changeset
   514
    }
97501edd2979 8047764: Indexed or polymorphic set on global affects Object.prototype
hannesw
parents: 26377
diff changeset
   515
97501edd2979 8047764: Indexed or polymorphic set on global affects Object.prototype
hannesw
parents: 26377
diff changeset
   516
    /**
47738
e4d7a32e6a91 8185119: Uninitialized const when using multiple threads
hannesw
parents: 47216
diff changeset
   517
     * Returns true if {@code flags} has the {@link  #CALLSITE_DECLARE} bit set.
e4d7a32e6a91 8185119: Uninitialized const when using multiple threads
hannesw
parents: 47216
diff changeset
   518
     * @param flags the flags
e4d7a32e6a91 8185119: Uninitialized const when using multiple threads
hannesw
parents: 47216
diff changeset
   519
     * @return true if the flag is set, false otherwise.
e4d7a32e6a91 8185119: Uninitialized const when using multiple threads
hannesw
parents: 47216
diff changeset
   520
     */
e4d7a32e6a91 8185119: Uninitialized const when using multiple threads
hannesw
parents: 47216
diff changeset
   521
    public static boolean isDeclaration(final int flags) {
e4d7a32e6a91 8185119: Uninitialized const when using multiple threads
hannesw
parents: 47216
diff changeset
   522
        return (flags & CALLSITE_DECLARE) != 0;
e4d7a32e6a91 8185119: Uninitialized const when using multiple threads
hannesw
parents: 47216
diff changeset
   523
    }
e4d7a32e6a91 8185119: Uninitialized const when using multiple threads
hannesw
parents: 47216
diff changeset
   524
e4d7a32e6a91 8185119: Uninitialized const when using multiple threads
hannesw
parents: 47216
diff changeset
   525
    /**
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 19105
diff changeset
   526
     * Get a program point from a descriptor (must be optimistic)
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 19105
diff changeset
   527
     * @param desc descriptor
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 19105
diff changeset
   528
     * @return program point
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 19105
diff changeset
   529
     */
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 19105
diff changeset
   530
    public static int getProgramPoint(final CallSiteDescriptor desc) {
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 19105
diff changeset
   531
        assert isOptimistic(desc) : "program point requested from non-optimistic descriptor " + desc;
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 19105
diff changeset
   532
        return getFlags(desc) >> CALLSITE_PROGRAM_POINT_SHIFT;
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 19105
diff changeset
   533
    }
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 19105
diff changeset
   534
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   535
    boolean isProfile() {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   536
        return isFlag(CALLSITE_PROFILE);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   537
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   538
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   539
    boolean isTrace() {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   540
        return isFlag(CALLSITE_TRACE);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   541
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   542
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   543
    boolean isTraceMisses() {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   544
        return isFlag(CALLSITE_TRACE_MISSES);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   545
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   546
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   547
    boolean isTraceEnterExit() {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   548
        return isFlag(CALLSITE_TRACE_ENTEREXIT);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   549
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   550
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   551
    boolean isTraceObjects() {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   552
        return isFlag(CALLSITE_TRACE_VALUES);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   553
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   554
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 19105
diff changeset
   555
    boolean isOptimistic() {
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 19105
diff changeset
   556
        return isFlag(CALLSITE_OPTIMISTIC);
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 19105
diff changeset
   557
    }
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 19105
diff changeset
   558
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   559
    @Override
33342
86b4260bb17a 8139919: Make CallSiteDescriptor a concrete class
attila
parents: 33341
diff changeset
   560
    public CallSiteDescriptor changeMethodTypeInternal(final MethodType newMethodType) {
33343
23abd10384a5 8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents: 33342
diff changeset
   561
        return get(getLookupPrivileged(), getOperation(), newMethodType, flags);
33331
273e6a10de22 8139435: Make sure CallSiteDescriptor.getLookup is subject to a security check
attila
parents: 33330
diff changeset
   562
    }
41843
40da931419f1 8169050: underscore_linker.js sample fails after dynalink changes for JDK-8168005
sundar
parents: 41842
diff changeset
   563
40da931419f1 8169050: underscore_linker.js sample fails after dynalink changes for JDK-8168005
sundar
parents: 41842
diff changeset
   564
    @Override
40da931419f1 8169050: underscore_linker.js sample fails after dynalink changes for JDK-8168005
sundar
parents: 41842
diff changeset
   565
    protected CallSiteDescriptor changeOperationInternal(final Operation newOperation) {
40da931419f1 8169050: underscore_linker.js sample fails after dynalink changes for JDK-8168005
sundar
parents: 41842
diff changeset
   566
        return get(getLookupPrivileged(), newOperation, getMethodType(), flags);
40da931419f1 8169050: underscore_linker.js sample fails after dynalink changes for JDK-8168005
sundar
parents: 41842
diff changeset
   567
    }
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   568
}