nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/runtime/linker/Bootstrap.java
author chegar
Sun, 17 Aug 2014 15:56:32 +0100
changeset 25865 d38d876f1654
parent 24770 nashorn/src/jdk/nashorn/internal/runtime/linker/Bootstrap.java@17f8e3b82ad3
child 26236 78b5ece438c0
permissions -rw-r--r--
8054834: Modular Source Code Reviewed-by: alanb, chegar, ihse, mduigou Contributed-by: alan.bateman@oracle.com, alex.buckley@oracle.com, chris.hegarty@oracle.com, erik.joelsson@oracle.com, jonathan.gibbons@oracle.com, karen.kinnear@oracle.com, magnus.ihse.bursie@oracle.com, mandy.chung@oracle.com, mark.reinhold@oracle.com, paul.sandoz@oracle.com
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
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    28
import static jdk.nashorn.internal.codegen.CompilerConstants.staticCallNoLookup;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    29
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    30
import java.lang.invoke.CallSite;
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22668
diff changeset
    31
import java.lang.invoke.ConstantCallSite;
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    32
import java.lang.invoke.MethodHandle;
16226
0e4f37e6cc40 8007915: Nashorn IR, codegen, parser packages and Context instance should be inaccessible to user code
sundar
parents: 16184
diff changeset
    33
import java.lang.invoke.MethodHandles;
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    34
import java.lang.invoke.MethodHandles.Lookup;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    35
import java.lang.invoke.MethodType;
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents: 16226
diff changeset
    36
import jdk.internal.dynalink.CallSiteDescriptor;
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents: 16226
diff changeset
    37
import jdk.internal.dynalink.DynamicLinker;
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents: 16226
diff changeset
    38
import jdk.internal.dynalink.DynamicLinkerFactory;
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22668
diff changeset
    39
import jdk.internal.dynalink.GuardedInvocationFilter;
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents: 16226
diff changeset
    40
import jdk.internal.dynalink.beans.BeansLinker;
19627
90d910ec15a3 8023630: Implement Java.super() as the preferred way to call super methods
attila
parents: 19455
diff changeset
    41
import jdk.internal.dynalink.beans.StaticClass;
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents: 16226
diff changeset
    42
import jdk.internal.dynalink.linker.GuardedInvocation;
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22668
diff changeset
    43
import jdk.internal.dynalink.linker.LinkRequest;
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents: 16226
diff changeset
    44
import jdk.internal.dynalink.linker.LinkerServices;
19889
63af9358d0dc 8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents: 19627
diff changeset
    45
import jdk.nashorn.api.scripting.JSObject;
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    46
import jdk.nashorn.internal.codegen.CompilerConstants.Call;
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22668
diff changeset
    47
import jdk.nashorn.internal.codegen.ObjectClassGenerator;
16226
0e4f37e6cc40 8007915: Nashorn IR, codegen, parser packages and Context instance should be inaccessible to user code
sundar
parents: 16184
diff changeset
    48
import jdk.nashorn.internal.codegen.RuntimeCallSite;
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22668
diff changeset
    49
import jdk.nashorn.internal.lookup.MethodHandleFactory;
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22668
diff changeset
    50
import jdk.nashorn.internal.lookup.MethodHandleFunctionality;
19090
b5cf0f877326 8021164: REGRESSION: test262 failures after JDK-8021122
sundar
parents: 19088
diff changeset
    51
import jdk.nashorn.internal.runtime.JSType;
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22668
diff changeset
    52
import jdk.nashorn.internal.runtime.OptimisticReturnFilters;
19088
153f268bfa72 8021122: Not all callables are handled for toString and other function valued properties
sundar
parents: 18878
diff changeset
    53
import jdk.nashorn.internal.runtime.ScriptFunction;
153f268bfa72 8021122: Not all callables are handled for toString and other function valued properties
sundar
parents: 18878
diff changeset
    54
import jdk.nashorn.internal.runtime.ScriptRuntime;
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    55
import jdk.nashorn.internal.runtime.options.Options;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    56
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    57
/**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    58
 * This class houses bootstrap method for invokedynamic instructions generated by compiler.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    59
 */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    60
public final class Bootstrap {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    61
    /** Reference to the seed boostrap function */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    62
    public static final Call BOOTSTRAP = staticCallNoLookup(Bootstrap.class, "bootstrap", CallSite.class, Lookup.class, String.class, MethodType.class, int.class);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    63
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22668
diff changeset
    64
    private static final MethodHandleFunctionality MH = MethodHandleFactory.getFunctionality();
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22668
diff changeset
    65
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22668
diff changeset
    66
    /**
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22668
diff changeset
    67
     * The default dynalink relink threshold for megamorphisism is 8. In the case
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22668
diff changeset
    68
     * of object fields only, it is fine. However, with dual fields, in order to get
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22668
diff changeset
    69
     * performance on benchmarks with a lot of object instantiation and then field
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22668
diff changeset
    70
     * reassignment, it can take slightly more relinks to become stable with type
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22668
diff changeset
    71
     * changes swapping out an entire proprety map and making a map guard fail.
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22668
diff changeset
    72
     * Therefore the relink threshold is set to 16 for dual fields (now the default).
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22668
diff changeset
    73
     * This doesn't seem to have any other negative performance implication.
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22668
diff changeset
    74
     *
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22668
diff changeset
    75
     * See for example octane.gbemu, run with --log=fields:warning to study
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22668
diff changeset
    76
     * megamorphic behavior
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22668
diff changeset
    77
     */
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22668
diff changeset
    78
    private static final int NASHORN_DEFAULT_UNSTABLE_RELINK_THRESHOLD =
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22668
diff changeset
    79
            ObjectClassGenerator.OBJECT_FIELDS_ONLY ?
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22668
diff changeset
    80
                     8 :
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22668
diff changeset
    81
                    16;
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22668
diff changeset
    82
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    83
    // do not create me!!
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    84
    private Bootstrap() {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    85
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    86
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    87
    private static final DynamicLinker dynamicLinker;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    88
    static {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    89
        final DynamicLinkerFactory factory = new DynamicLinkerFactory();
24591
67e22e0e8473 8044000: Access to undefined property yields "null" instead of "undefined"
sundar
parents: 22668
diff changeset
    90
        final NashornBeansLinker nashornBeansLinker = new NashornBeansLinker();
67e22e0e8473 8044000: Access to undefined property yields "null" instead of "undefined"
sundar
parents: 22668
diff changeset
    91
        final JSObjectLinker jsObjectLinker = new JSObjectLinker(nashornBeansLinker);
67e22e0e8473 8044000: Access to undefined property yields "null" instead of "undefined"
sundar
parents: 22668
diff changeset
    92
        factory.setPrioritizedLinkers(
67e22e0e8473 8044000: Access to undefined property yields "null" instead of "undefined"
sundar
parents: 22668
diff changeset
    93
            new NashornLinker(),
67e22e0e8473 8044000: Access to undefined property yields "null" instead of "undefined"
sundar
parents: 22668
diff changeset
    94
            new NashornPrimitiveLinker(),
67e22e0e8473 8044000: Access to undefined property yields "null" instead of "undefined"
sundar
parents: 22668
diff changeset
    95
            new NashornStaticClassLinker(),
67e22e0e8473 8044000: Access to undefined property yields "null" instead of "undefined"
sundar
parents: 22668
diff changeset
    96
            new BoundDynamicMethodLinker(),
67e22e0e8473 8044000: Access to undefined property yields "null" instead of "undefined"
sundar
parents: 22668
diff changeset
    97
            new JavaSuperAdapterLinker(),
67e22e0e8473 8044000: Access to undefined property yields "null" instead of "undefined"
sundar
parents: 22668
diff changeset
    98
            jsObjectLinker,
67e22e0e8473 8044000: Access to undefined property yields "null" instead of "undefined"
sundar
parents: 22668
diff changeset
    99
            new ReflectionCheckLinker());
67e22e0e8473 8044000: Access to undefined property yields "null" instead of "undefined"
sundar
parents: 22668
diff changeset
   100
        factory.setFallbackLinkers(nashornBeansLinker, new NashornBottomLinker());
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   101
        factory.setSyncOnRelink(true);
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22668
diff changeset
   102
        factory.setPrelinkFilter(new GuardedInvocationFilter() {
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22668
diff changeset
   103
            @Override
24738
be2026c9717c 8039746: Transform applies to calls wherever possible, for ScriptFunctions and JSObjects.
lagergren
parents: 24719
diff changeset
   104
            public GuardedInvocation filter(final GuardedInvocation inv, final LinkRequest request, final LinkerServices linkerServices) {
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22668
diff changeset
   105
                final CallSiteDescriptor desc = request.getCallSiteDescriptor();
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22668
diff changeset
   106
                return OptimisticReturnFilters.filterOptimisticReturnValue(inv, desc).asType(linkerServices, desc.getMethodType());
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22668
diff changeset
   107
            }
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22668
diff changeset
   108
        });
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22668
diff changeset
   109
        final int relinkThreshold = Options.getIntProperty("nashorn.unstable.relink.threshold", NASHORN_DEFAULT_UNSTABLE_RELINK_THRESHOLD);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   110
        if (relinkThreshold > -1) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   111
            factory.setUnstableRelinkThreshold(relinkThreshold);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   112
        }
19455
b972b61a6921 8022509: Various Dynalink security enhancements
attila
parents: 19090
diff changeset
   113
b972b61a6921 8022509: Various Dynalink security enhancements
attila
parents: 19090
diff changeset
   114
        // Linkers for any additional language runtimes deployed alongside Nashorn will be picked up by the factory.
b972b61a6921 8022509: Various Dynalink security enhancements
attila
parents: 19090
diff changeset
   115
        factory.setClassLoader(Bootstrap.class.getClassLoader());
b972b61a6921 8022509: Various Dynalink security enhancements
attila
parents: 19090
diff changeset
   116
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   117
        dynamicLinker = factory.createLinker();
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   118
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   119
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   120
    /**
19088
153f268bfa72 8021122: Not all callables are handled for toString and other function valued properties
sundar
parents: 18878
diff changeset
   121
     * Returns if the given object is a "callable"
153f268bfa72 8021122: Not all callables are handled for toString and other function valued properties
sundar
parents: 18878
diff changeset
   122
     * @param obj object to be checked for callability
153f268bfa72 8021122: Not all callables are handled for toString and other function valued properties
sundar
parents: 18878
diff changeset
   123
     * @return true if the obj is callable
153f268bfa72 8021122: Not all callables are handled for toString and other function valued properties
sundar
parents: 18878
diff changeset
   124
     */
153f268bfa72 8021122: Not all callables are handled for toString and other function valued properties
sundar
parents: 18878
diff changeset
   125
    public static boolean isCallable(final Object obj) {
153f268bfa72 8021122: Not all callables are handled for toString and other function valued properties
sundar
parents: 18878
diff changeset
   126
        if (obj == ScriptRuntime.UNDEFINED || obj == null) {
153f268bfa72 8021122: Not all callables are handled for toString and other function valued properties
sundar
parents: 18878
diff changeset
   127
            return false;
153f268bfa72 8021122: Not all callables are handled for toString and other function valued properties
sundar
parents: 18878
diff changeset
   128
        }
153f268bfa72 8021122: Not all callables are handled for toString and other function valued properties
sundar
parents: 18878
diff changeset
   129
153f268bfa72 8021122: Not all callables are handled for toString and other function valued properties
sundar
parents: 18878
diff changeset
   130
        return obj instanceof ScriptFunction ||
19889
63af9358d0dc 8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents: 19627
diff changeset
   131
            ((obj instanceof JSObject) && ((JSObject)obj).isFunction()) ||
19088
153f268bfa72 8021122: Not all callables are handled for toString and other function valued properties
sundar
parents: 18878
diff changeset
   132
            isDynamicMethod(obj) ||
19627
90d910ec15a3 8023630: Implement Java.super() as the preferred way to call super methods
attila
parents: 19455
diff changeset
   133
            isFunctionalInterfaceObject(obj) ||
90d910ec15a3 8023630: Implement Java.super() as the preferred way to call super methods
attila
parents: 19455
diff changeset
   134
            obj instanceof StaticClass;
19088
153f268bfa72 8021122: Not all callables are handled for toString and other function valued properties
sundar
parents: 18878
diff changeset
   135
    }
153f268bfa72 8021122: Not all callables are handled for toString and other function valued properties
sundar
parents: 18878
diff changeset
   136
153f268bfa72 8021122: Not all callables are handled for toString and other function valued properties
sundar
parents: 18878
diff changeset
   137
    /**
153f268bfa72 8021122: Not all callables are handled for toString and other function valued properties
sundar
parents: 18878
diff changeset
   138
     * Returns if the given object is a dynalink Dynamic method
153f268bfa72 8021122: Not all callables are handled for toString and other function valued properties
sundar
parents: 18878
diff changeset
   139
     * @param obj object to be checked
153f268bfa72 8021122: Not all callables are handled for toString and other function valued properties
sundar
parents: 18878
diff changeset
   140
     * @return true if the obj is a dynamic method
153f268bfa72 8021122: Not all callables are handled for toString and other function valued properties
sundar
parents: 18878
diff changeset
   141
     */
153f268bfa72 8021122: Not all callables are handled for toString and other function valued properties
sundar
parents: 18878
diff changeset
   142
    public static boolean isDynamicMethod(final Object obj) {
153f268bfa72 8021122: Not all callables are handled for toString and other function valued properties
sundar
parents: 18878
diff changeset
   143
        return obj instanceof BoundDynamicMethod || BeansLinker.isDynamicMethod(obj);
153f268bfa72 8021122: Not all callables are handled for toString and other function valued properties
sundar
parents: 18878
diff changeset
   144
    }
153f268bfa72 8021122: Not all callables are handled for toString and other function valued properties
sundar
parents: 18878
diff changeset
   145
153f268bfa72 8021122: Not all callables are handled for toString and other function valued properties
sundar
parents: 18878
diff changeset
   146
    /**
153f268bfa72 8021122: Not all callables are handled for toString and other function valued properties
sundar
parents: 18878
diff changeset
   147
     * Returns if the given object is an instance of an interface annotated with
153f268bfa72 8021122: Not all callables are handled for toString and other function valued properties
sundar
parents: 18878
diff changeset
   148
     * java.lang.FunctionalInterface
153f268bfa72 8021122: Not all callables are handled for toString and other function valued properties
sundar
parents: 18878
diff changeset
   149
     * @param obj object to be checked
153f268bfa72 8021122: Not all callables are handled for toString and other function valued properties
sundar
parents: 18878
diff changeset
   150
     * @return true if the obj is an instance of @FunctionalInterface interface
153f268bfa72 8021122: Not all callables are handled for toString and other function valued properties
sundar
parents: 18878
diff changeset
   151
     */
153f268bfa72 8021122: Not all callables are handled for toString and other function valued properties
sundar
parents: 18878
diff changeset
   152
    public static boolean isFunctionalInterfaceObject(final Object obj) {
19090
b5cf0f877326 8021164: REGRESSION: test262 failures after JDK-8021122
sundar
parents: 19088
diff changeset
   153
        return !JSType.isPrimitive(obj) && (NashornBottomLinker.getFunctionalInterfaceMethod(obj.getClass()) != null);
19088
153f268bfa72 8021122: Not all callables are handled for toString and other function valued properties
sundar
parents: 18878
diff changeset
   154
    }
153f268bfa72 8021122: Not all callables are handled for toString and other function valued properties
sundar
parents: 18878
diff changeset
   155
153f268bfa72 8021122: Not all callables are handled for toString and other function valued properties
sundar
parents: 18878
diff changeset
   156
    /**
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   157
     * Create a call site and link it for Nashorn. This version of the method conforms to the invokedynamic bootstrap
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   158
     * method expected signature and is referenced from Nashorn generated bytecode as the bootstrap method for all
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   159
     * invokedynamic instructions.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   160
     * @param lookup MethodHandle lookup. Ignored as Nashorn only uses public lookup.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   161
     * @param opDesc Dynalink dynamic operation descriptor.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   162
     * @param type   Method type.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   163
     * @param flags  flags for call type, trace/profile etc.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   164
     * @return CallSite with MethodHandle to appropriate method or null if not found.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   165
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   166
    public static CallSite bootstrap(final Lookup lookup, final String opDesc, final MethodType type, final int flags) {
18841
9bbc4b8832b2 8010946: AccessControl.doPrivileged is broken when called from js script
attila
parents: 16522
diff changeset
   167
        return dynamicLinker.link(LinkerCallSite.newLinkerCallSite(lookup, opDesc, type, flags));
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   168
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   169
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   170
    /**
16226
0e4f37e6cc40 8007915: Nashorn IR, codegen, parser packages and Context instance should be inaccessible to user code
sundar
parents: 16184
diff changeset
   171
     * Bootstrapper for a specialized Runtime call
0e4f37e6cc40 8007915: Nashorn IR, codegen, parser packages and Context instance should be inaccessible to user code
sundar
parents: 16184
diff changeset
   172
     *
0e4f37e6cc40 8007915: Nashorn IR, codegen, parser packages and Context instance should be inaccessible to user code
sundar
parents: 16184
diff changeset
   173
     * @param lookup       lookup
0e4f37e6cc40 8007915: Nashorn IR, codegen, parser packages and Context instance should be inaccessible to user code
sundar
parents: 16184
diff changeset
   174
     * @param initialName  initial name for callsite
0e4f37e6cc40 8007915: Nashorn IR, codegen, parser packages and Context instance should be inaccessible to user code
sundar
parents: 16184
diff changeset
   175
     * @param type         method type for call site
0e4f37e6cc40 8007915: Nashorn IR, codegen, parser packages and Context instance should be inaccessible to user code
sundar
parents: 16184
diff changeset
   176
     *
0e4f37e6cc40 8007915: Nashorn IR, codegen, parser packages and Context instance should be inaccessible to user code
sundar
parents: 16184
diff changeset
   177
     * @return callsite for a runtime node
0e4f37e6cc40 8007915: Nashorn IR, codegen, parser packages and Context instance should be inaccessible to user code
sundar
parents: 16184
diff changeset
   178
     */
0e4f37e6cc40 8007915: Nashorn IR, codegen, parser packages and Context instance should be inaccessible to user code
sundar
parents: 16184
diff changeset
   179
    public static CallSite runtimeBootstrap(final MethodHandles.Lookup lookup, final String initialName, final MethodType type) {
0e4f37e6cc40 8007915: Nashorn IR, codegen, parser packages and Context instance should be inaccessible to user code
sundar
parents: 16184
diff changeset
   180
        return new RuntimeCallSite(type, initialName);
0e4f37e6cc40 8007915: Nashorn IR, codegen, parser packages and Context instance should be inaccessible to user code
sundar
parents: 16184
diff changeset
   181
    }
0e4f37e6cc40 8007915: Nashorn IR, codegen, parser packages and Context instance should be inaccessible to user code
sundar
parents: 16184
diff changeset
   182
0e4f37e6cc40 8007915: Nashorn IR, codegen, parser packages and Context instance should be inaccessible to user code
sundar
parents: 16184
diff changeset
   183
    /**
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22668
diff changeset
   184
     * Boostrapper for math calls that may overflow
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22668
diff changeset
   185
     * @param lookup         lookup
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22668
diff changeset
   186
     * @param name           name of operation
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22668
diff changeset
   187
     * @param type           method type
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22668
diff changeset
   188
     * @param programPoint   program point to bind to callsite
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22668
diff changeset
   189
     *
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22668
diff changeset
   190
     * @return callsite for a math instrinic node
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22668
diff changeset
   191
     */
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22668
diff changeset
   192
    public static CallSite mathBootstrap(final MethodHandles.Lookup lookup, final String name, final MethodType type, final int programPoint) {
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22668
diff changeset
   193
        final MethodHandle mh;
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22668
diff changeset
   194
        switch (name) {
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22668
diff changeset
   195
        case "iadd":
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22668
diff changeset
   196
            mh = JSType.ADD_EXACT.methodHandle();
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22668
diff changeset
   197
            break;
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22668
diff changeset
   198
        case "isub":
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22668
diff changeset
   199
            mh = JSType.SUB_EXACT.methodHandle();
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22668
diff changeset
   200
            break;
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22668
diff changeset
   201
        case "imul":
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22668
diff changeset
   202
            mh = JSType.MUL_EXACT.methodHandle();
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22668
diff changeset
   203
            break;
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22668
diff changeset
   204
        case "idiv":
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22668
diff changeset
   205
            mh = JSType.DIV_EXACT.methodHandle();
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22668
diff changeset
   206
            break;
24751
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24738
diff changeset
   207
        case "irem":
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24738
diff changeset
   208
            mh = JSType.REM_EXACT.methodHandle();
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24738
diff changeset
   209
            break;
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22668
diff changeset
   210
        case "ineg":
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22668
diff changeset
   211
            mh = JSType.NEGATE_EXACT.methodHandle();
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22668
diff changeset
   212
            break;
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22668
diff changeset
   213
        case "ladd":
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22668
diff changeset
   214
            mh = JSType.ADD_EXACT_LONG.methodHandle();
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22668
diff changeset
   215
            break;
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22668
diff changeset
   216
        case "lsub":
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22668
diff changeset
   217
            mh = JSType.SUB_EXACT_LONG.methodHandle();
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22668
diff changeset
   218
            break;
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22668
diff changeset
   219
        case "lmul":
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22668
diff changeset
   220
            mh = JSType.MUL_EXACT_LONG.methodHandle();
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22668
diff changeset
   221
            break;
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22668
diff changeset
   222
        case "ldiv":
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22668
diff changeset
   223
            mh = JSType.DIV_EXACT_LONG.methodHandle();
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22668
diff changeset
   224
            break;
24751
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24738
diff changeset
   225
        case "lrem":
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24738
diff changeset
   226
            mh = JSType.REM_EXACT_LONG.methodHandle();
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24738
diff changeset
   227
            break;
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22668
diff changeset
   228
        case "lneg":
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22668
diff changeset
   229
            mh = JSType.NEGATE_EXACT_LONG.methodHandle();
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22668
diff changeset
   230
            break;
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22668
diff changeset
   231
        default:
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22668
diff changeset
   232
            throw new AssertionError("unsupported math intrinsic");
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22668
diff changeset
   233
        }
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22668
diff changeset
   234
        return new ConstantCallSite(MH.insertArguments(mh, mh.type().parameterCount() - 1, programPoint));
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22668
diff changeset
   235
    }
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22668
diff changeset
   236
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22668
diff changeset
   237
    /**
18841
9bbc4b8832b2 8010946: AccessControl.doPrivileged is broken when called from js script
attila
parents: 16522
diff changeset
   238
     * Returns a dynamic invoker for a specified dynamic operation using the public lookup. You can use this method to
9bbc4b8832b2 8010946: AccessControl.doPrivileged is broken when called from js script
attila
parents: 16522
diff changeset
   239
     * create a method handle that when invoked acts completely as if it were a Nashorn-linked call site. An overview of
9bbc4b8832b2 8010946: AccessControl.doPrivileged is broken when called from js script
attila
parents: 16522
diff changeset
   240
     * available dynamic operations can be found in the
9bbc4b8832b2 8010946: AccessControl.doPrivileged is broken when called from js script
attila
parents: 16522
diff changeset
   241
     * <a href="https://github.com/szegedi/dynalink/wiki/User-Guide-0.6">Dynalink User Guide</a>, but we'll show few
9bbc4b8832b2 8010946: AccessControl.doPrivileged is broken when called from js script
attila
parents: 16522
diff changeset
   242
     * examples here:
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   243
     * <ul>
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   244
     *   <li>Get a named property with fixed name:
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   245
     *     <pre>
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   246
     * MethodHandle getColor = Boostrap.createDynamicInvoker("dyn:getProp:color", Object.class, Object.class);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   247
     * Object obj = ...; // somehow obtain the object
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   248
     * Object color = getColor.invokeExact(obj);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   249
     *     </pre>
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   250
     *   </li>
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   251
     *   <li>Get a named property with variable name:
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   252
     *     <pre>
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   253
     * MethodHandle getProperty = Boostrap.createDynamicInvoker("dyn:getElem", Object.class, Object.class, String.class);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   254
     * Object obj = ...; // somehow obtain the object
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   255
     * Object color = getProperty.invokeExact(obj, "color");
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   256
     * Object shape = getProperty.invokeExact(obj, "shape");
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   257
     * MethodHandle getNumProperty = Boostrap.createDynamicInvoker("dyn:getElem", Object.class, Object.class, int.class);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   258
     * Object elem42 = getNumProperty.invokeExact(obj, 42);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   259
     *     </pre>
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   260
     *   </li>
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   261
     *   <li>Set a named property with fixed name:
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   262
     *     <pre>
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   263
     * MethodHandle setColor = Boostrap.createDynamicInvoker("dyn:setProp:color", void.class, Object.class, Object.class);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   264
     * Object obj = ...; // somehow obtain the object
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   265
     * setColor.invokeExact(obj, Color.BLUE);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   266
     *     </pre>
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   267
     *   </li>
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   268
     *   <li>Set a property with variable name:
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   269
     *     <pre>
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   270
     * MethodHandle setProperty = Boostrap.createDynamicInvoker("dyn:setElem", void.class, Object.class, String.class, Object.class);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   271
     * Object obj = ...; // somehow obtain the object
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   272
     * setProperty.invokeExact(obj, "color", Color.BLUE);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   273
     * setProperty.invokeExact(obj, "shape", Shape.CIRCLE);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   274
     *     </pre>
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   275
     *   </li>
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   276
     *   <li>Call a function on an object; two-step variant. This is the actual variant used by Nashorn-generated code:
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   277
     *     <pre>
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   278
     * MethodHandle findFooFunction = Boostrap.createDynamicInvoker("dyn:getMethod:foo", Object.class, Object.class);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   279
     * Object obj = ...; // somehow obtain the object
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   280
     * Object foo_fn = findFooFunction.invokeExact(obj);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   281
     * MethodHandle callFunctionWithTwoArgs = Boostrap.createDynamicInvoker("dyn:call", Object.class, Object.class, Object.class, Object.class, Object.class);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   282
     * // Note: "call" operation takes a function, then a "this" value, then the arguments:
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   283
     * Object foo_retval = callFunctionWithTwoArgs.invokeExact(foo_fn, obj, arg1, arg2);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   284
     *     </pre>
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   285
     *   </li>
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   286
     *   <li>Call a function on an object; single-step variant. Although Nashorn doesn't use this variant and never
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   287
     *   emits any INVOKEDYNAMIC instructions with {@code dyn:getMethod}, it still supports this standard Dynalink
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   288
     *   operation:
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   289
     *     <pre>
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   290
     * MethodHandle callFunctionFooWithTwoArgs = Boostrap.createDynamicInvoker("dyn:callMethod:foo", Object.class, Object.class, Object.class, Object.class);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   291
     * Object obj = ...; // somehow obtain the object
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   292
     * Object foo_retval = callFunctionFooWithTwoArgs.invokeExact(obj, arg1, arg2);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   293
     *     </pre>
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   294
     *   </li>
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   295
     * </ul>
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   296
     * Few additional remarks:
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   297
     * <ul>
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   298
     * <li>Just as Nashorn works with any Java object, the invokers returned from this method can also be applied to
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   299
     * arbitrary Java objects in addition to Nashorn JavaScript objects.</li>
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   300
     * <li>For invoking a named function on an object, you can also use the {@link InvokeByName} convenience class.</li>
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   301
     * <li>For Nashorn objects {@code getElem}, {@code getProp}, and {@code getMethod} are handled almost identically,
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   302
     * since JavaScript doesn't distinguish between different kinds of properties on an object. Either can be used with
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   303
     * fixed property name or a variable property name. The only significant difference is handling of missing
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   304
     * properties: {@code getMethod} for a missing member will link to a potential invocation of
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   305
     * {@code __noSuchMethod__} on the object, {@code getProp} for a missing member will link to a potential invocation
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   306
     * of {@code __noSuchProperty__}, while {@code getElem} for a missing member will link to an empty getter.</li>
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   307
     * <li>In similar vein, {@code setElem} and {@code setProp} are handled identically on Nashorn objects.</li>
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   308
     * <li>There's no rule that the variable property identifier has to be a {@code String} for {@code getProp/setProp}
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   309
     * and {@code int} for {@code getElem/setElem}. You can declare their type to be {@code int}, {@code double},
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   310
     * {@code Object}, and so on regardless of the kind of the operation.</li>
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   311
     * <li>You can be as specific in parameter types as you want. E.g. if you know that the receiver of the operation
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   312
     * will always be {@code ScriptObject}, you can pass {@code ScriptObject.class} as its parameter type. If you happen
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   313
     * to link to a method that expects different types, (you can use these invokers on POJOs too, after all, and end up
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   314
     * linking with their methods that have strongly-typed signatures), all necessary conversions allowed by either Java
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   315
     * or JavaScript will be applied: if invoked methods specify either primitive or wrapped Java numeric types, or
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   316
     * {@code String} or {@code boolean/Boolean}, then the parameters might be subjected to standard ECMAScript
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   317
     * {@code ToNumber}, {@code ToString}, and {@code ToBoolean} conversion, respectively. Less obviously, if the
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   318
     * expected parameter type is a SAM type, and you pass a JavaScript function, a proxy object implementing the SAM
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   319
     * type and delegating to the function will be passed. Linkage can often be optimized when linkers have more
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   320
     * specific type information than "everything can be an object".</li>
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   321
     * <li>You can also be as specific in return types as you want. For return types any necessary type conversion
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   322
     * available in either Java or JavaScript will be automatically applied, similar to the process described for
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   323
     * parameters, only in reverse direction:  if you specify any either primitive or wrapped Java numeric type, or
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   324
     * {@code String} or {@code boolean/Boolean}, then the return values will be subjected to standard ECMAScript
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   325
     * {@code ToNumber}, {@code ToString}, and {@code ToBoolean} conversion, respectively. Less obviously, if the return
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   326
     * type is a SAM type, and the return value is a JavaScript function, a proxy object implementing the SAM type and
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   327
     * delegating to the function will be returned.</li>
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   328
     * </ul>
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   329
     * @param opDesc Dynalink dynamic operation descriptor.
16173
c41d062f7d2a 8006293: Reduce ScriptObject.findCallMethodMethod
attila
parents: 16151
diff changeset
   330
     * @param rtype the return type for the operation
c41d062f7d2a 8006293: Reduce ScriptObject.findCallMethodMethod
attila
parents: 16151
diff changeset
   331
     * @param ptypes the parameter types for the operation
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   332
     * @return MethodHandle for invoking the operation.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   333
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   334
    public static MethodHandle createDynamicInvoker(final String opDesc, final Class<?> rtype, final Class<?>... ptypes) {
16173
c41d062f7d2a 8006293: Reduce ScriptObject.findCallMethodMethod
attila
parents: 16151
diff changeset
   335
        return createDynamicInvoker(opDesc, MethodType.methodType(rtype, ptypes));
c41d062f7d2a 8006293: Reduce ScriptObject.findCallMethodMethod
attila
parents: 16151
diff changeset
   336
    }
c41d062f7d2a 8006293: Reduce ScriptObject.findCallMethodMethod
attila
parents: 16151
diff changeset
   337
c41d062f7d2a 8006293: Reduce ScriptObject.findCallMethodMethod
attila
parents: 16151
diff changeset
   338
    /**
18841
9bbc4b8832b2 8010946: AccessControl.doPrivileged is broken when called from js script
attila
parents: 16522
diff changeset
   339
     * Returns a dynamic invoker for a specified dynamic operation using the public lookup. Similar to
16173
c41d062f7d2a 8006293: Reduce ScriptObject.findCallMethodMethod
attila
parents: 16151
diff changeset
   340
     * {@link #createDynamicInvoker(String, Class, Class...)} but with return and parameter types composed into a
c41d062f7d2a 8006293: Reduce ScriptObject.findCallMethodMethod
attila
parents: 16151
diff changeset
   341
     * method type in the signature. See the discussion of that method for details.
c41d062f7d2a 8006293: Reduce ScriptObject.findCallMethodMethod
attila
parents: 16151
diff changeset
   342
     * @param opDesc Dynalink dynamic operation descriptor.
c41d062f7d2a 8006293: Reduce ScriptObject.findCallMethodMethod
attila
parents: 16151
diff changeset
   343
     * @param type the method type for the operation
c41d062f7d2a 8006293: Reduce ScriptObject.findCallMethodMethod
attila
parents: 16151
diff changeset
   344
     * @return MethodHandle for invoking the operation.
c41d062f7d2a 8006293: Reduce ScriptObject.findCallMethodMethod
attila
parents: 16151
diff changeset
   345
     */
c41d062f7d2a 8006293: Reduce ScriptObject.findCallMethodMethod
attila
parents: 16151
diff changeset
   346
    public static MethodHandle createDynamicInvoker(final String opDesc, final MethodType type) {
18841
9bbc4b8832b2 8010946: AccessControl.doPrivileged is broken when called from js script
attila
parents: 16522
diff changeset
   347
        return bootstrap(MethodHandles.publicLookup(), opDesc, type, 0).dynamicInvoker();
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   348
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   349
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   350
    /**
18876
ada98218aaae 8020324: Implement Object.bindProperties(target, source) for beans
attila
parents: 18841
diff changeset
   351
     * Binds a bean dynamic method (returned by invoking {@code dyn:getMethod} on an object linked with
ada98218aaae 8020324: Implement Object.bindProperties(target, source) for beans
attila
parents: 18841
diff changeset
   352
     * {@code BeansLinker} to a receiver.
ada98218aaae 8020324: Implement Object.bindProperties(target, source) for beans
attila
parents: 18841
diff changeset
   353
     * @param dynamicMethod the dynamic method to bind
ada98218aaae 8020324: Implement Object.bindProperties(target, source) for beans
attila
parents: 18841
diff changeset
   354
     * @param boundThis the bound "this" value.
ada98218aaae 8020324: Implement Object.bindProperties(target, source) for beans
attila
parents: 18841
diff changeset
   355
     * @return a bound dynamic method.
ada98218aaae 8020324: Implement Object.bindProperties(target, source) for beans
attila
parents: 18841
diff changeset
   356
     */
24738
be2026c9717c 8039746: Transform applies to calls wherever possible, for ScriptFunctions and JSObjects.
lagergren
parents: 24719
diff changeset
   357
    public static Object bindDynamicMethod(final Object dynamicMethod, final Object boundThis) {
18876
ada98218aaae 8020324: Implement Object.bindProperties(target, source) for beans
attila
parents: 18841
diff changeset
   358
        return new BoundDynamicMethod(dynamicMethod, boundThis);
ada98218aaae 8020324: Implement Object.bindProperties(target, source) for beans
attila
parents: 18841
diff changeset
   359
    }
18878
078eb52cf5bc 8020508: Enforce reflection access restrictions on Object.bindProperties
attila
parents: 18876
diff changeset
   360
078eb52cf5bc 8020508: Enforce reflection access restrictions on Object.bindProperties
attila
parents: 18876
diff changeset
   361
    /**
19627
90d910ec15a3 8023630: Implement Java.super() as the preferred way to call super methods
attila
parents: 19455
diff changeset
   362
     * Creates a super-adapter for an adapter, that is, an adapter to the adapter that allows invocation of superclass
90d910ec15a3 8023630: Implement Java.super() as the preferred way to call super methods
attila
parents: 19455
diff changeset
   363
     * methods on it.
90d910ec15a3 8023630: Implement Java.super() as the preferred way to call super methods
attila
parents: 19455
diff changeset
   364
     * @param adapter the original adapter
90d910ec15a3 8023630: Implement Java.super() as the preferred way to call super methods
attila
parents: 19455
diff changeset
   365
     * @return a new adapter that can be used to invoke super methods on the original adapter.
90d910ec15a3 8023630: Implement Java.super() as the preferred way to call super methods
attila
parents: 19455
diff changeset
   366
     */
90d910ec15a3 8023630: Implement Java.super() as the preferred way to call super methods
attila
parents: 19455
diff changeset
   367
    public static Object createSuperAdapter(final Object adapter) {
90d910ec15a3 8023630: Implement Java.super() as the preferred way to call super methods
attila
parents: 19455
diff changeset
   368
        return new JavaSuperAdapter(adapter);
90d910ec15a3 8023630: Implement Java.super() as the preferred way to call super methods
attila
parents: 19455
diff changeset
   369
    }
90d910ec15a3 8023630: Implement Java.super() as the preferred way to call super methods
attila
parents: 19455
diff changeset
   370
90d910ec15a3 8023630: Implement Java.super() as the preferred way to call super methods
attila
parents: 19455
diff changeset
   371
    /**
18878
078eb52cf5bc 8020508: Enforce reflection access restrictions on Object.bindProperties
attila
parents: 18876
diff changeset
   372
     * If the given class is a reflection-specific class (anything in {@code java.lang.reflect} and
078eb52cf5bc 8020508: Enforce reflection access restrictions on Object.bindProperties
attila
parents: 18876
diff changeset
   373
     * {@code java.lang.invoke} package, as well a {@link Class} and any subclass of {@link ClassLoader}) and there is
078eb52cf5bc 8020508: Enforce reflection access restrictions on Object.bindProperties
attila
parents: 18876
diff changeset
   374
     * a security manager in the system, then it checks the {@code nashorn.JavaReflection} {@code RuntimePermission}.
078eb52cf5bc 8020508: Enforce reflection access restrictions on Object.bindProperties
attila
parents: 18876
diff changeset
   375
     * @param clazz the class being tested
22668
245094625886 8032948: Nashorn linkages awry
sundar
parents: 21686
diff changeset
   376
     * @param isStatic is access checked for static members (or instance members)
18878
078eb52cf5bc 8020508: Enforce reflection access restrictions on Object.bindProperties
attila
parents: 18876
diff changeset
   377
     */
24738
be2026c9717c 8039746: Transform applies to calls wherever possible, for ScriptFunctions and JSObjects.
lagergren
parents: 24719
diff changeset
   378
    public static void checkReflectionAccess(final Class<?> clazz, final boolean isStatic) {
22668
245094625886 8032948: Nashorn linkages awry
sundar
parents: 21686
diff changeset
   379
        ReflectionCheckLinker.checkReflectionAccess(clazz, isStatic);
18878
078eb52cf5bc 8020508: Enforce reflection access restrictions on Object.bindProperties
attila
parents: 18876
diff changeset
   380
    }
078eb52cf5bc 8020508: Enforce reflection access restrictions on Object.bindProperties
attila
parents: 18876
diff changeset
   381
18876
ada98218aaae 8020324: Implement Object.bindProperties(target, source) for beans
attila
parents: 18841
diff changeset
   382
    /**
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   383
     * Returns the Nashorn's internally used dynamic linker's services object. Note that in code that is processing a
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   384
     * linking request, you will normally use the {@code LinkerServices} object passed by whatever top-level linker
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   385
     * invoked the linking (if the call site is in Nashorn-generated code, you'll get this object anyway). You should
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   386
     * only resort to retrieving a linker services object using this method when you need some linker services (e.g.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   387
     * type converter method handles) outside of a code path that is linking a call site.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   388
     * @return Nashorn's internal dynamic linker's services object.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   389
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   390
    public static LinkerServices getLinkerServices() {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   391
        return dynamicLinker.getLinkerServices();
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   392
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   393
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   394
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   395
     * Takes a guarded invocation, and ensures its method and guard conform to the type of the call descriptor, using
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   396
     * all type conversions allowed by the linker's services. This method is used by Nashorn's linkers as a last step
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22668
diff changeset
   397
     * before returning guarded invocations. Most of the code used to produce the guarded invocations does not make an
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22668
diff changeset
   398
     * effort to coordinate types of the methods, and so a final type adjustment before a guarded invocation is returned
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22668
diff changeset
   399
     * to the aggregating linker is the responsibility of the linkers themselves.
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   400
     * @param inv the guarded invocation that needs to be type-converted. Can be null.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   401
     * @param linkerServices the linker services object providing the type conversions.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   402
     * @param desc the call site descriptor to whose method type the invocation needs to conform.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   403
     * @return the type-converted guarded invocation. If input is null, null is returned. If the input invocation
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   404
     * already conforms to the requested type, it is returned unchanged.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   405
     */
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22668
diff changeset
   406
    static GuardedInvocation asTypeSafeReturn(final GuardedInvocation inv, final LinkerServices linkerServices, final CallSiteDescriptor desc) {
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22668
diff changeset
   407
        return inv == null ? null : inv.asTypeSafeReturn(linkerServices, desc.getMethodType());
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   408
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   409
}