nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/runtime/linker/Bootstrap.java
author mhaupt
Tue, 09 Feb 2016 14:14:06 +0100
changeset 35793 b89ddacf3104
parent 35725 c7a2c18529b1
child 36026 ad5ff5d5459b
permissions -rw-r--r--
8149462: revert changes for 8149186 Reviewed-by: jlaskey, sundar
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
     1
/*
35793
b89ddacf3104 8149462: revert changes for 8149186
mhaupt
parents: 35725
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;
27815
6ed8e97e8fb6 8051778: support bind on all Nashorn callables
attila
parents: 27360
diff changeset
    29
import static jdk.nashorn.internal.runtime.ECMAErrors.typeError;
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    30
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    31
import java.lang.invoke.CallSite;
35793
b89ddacf3104 8149462: revert changes for 8149186
mhaupt
parents: 35725
diff changeset
    32
import java.lang.invoke.ConstantCallSite;
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    33
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
    34
import java.lang.invoke.MethodHandles;
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    35
import java.lang.invoke.MethodHandles.Lookup;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    36
import java.lang.invoke.MethodType;
34447
ec4c069f9436 8141338: Move jdk.internal.dynalink package to jdk.dynalink
attila
parents: 33686
diff changeset
    37
import jdk.dynalink.CallSiteDescriptor;
ec4c069f9436 8141338: Move jdk.internal.dynalink package to jdk.dynalink
attila
parents: 33686
diff changeset
    38
import jdk.dynalink.DynamicLinker;
ec4c069f9436 8141338: Move jdk.internal.dynalink package to jdk.dynalink
attila
parents: 33686
diff changeset
    39
import jdk.dynalink.DynamicLinkerFactory;
ec4c069f9436 8141338: Move jdk.internal.dynalink package to jdk.dynalink
attila
parents: 33686
diff changeset
    40
import jdk.dynalink.beans.BeansLinker;
ec4c069f9436 8141338: Move jdk.internal.dynalink package to jdk.dynalink
attila
parents: 33686
diff changeset
    41
import jdk.dynalink.beans.StaticClass;
ec4c069f9436 8141338: Move jdk.internal.dynalink package to jdk.dynalink
attila
parents: 33686
diff changeset
    42
import jdk.dynalink.linker.GuardedInvocation;
34979
03b189baa361 8144919: Implement missing member handler for BeansLinker
attila
parents: 34732
diff changeset
    43
import jdk.dynalink.linker.GuardingDynamicLinker;
34447
ec4c069f9436 8141338: Move jdk.internal.dynalink package to jdk.dynalink
attila
parents: 33686
diff changeset
    44
import jdk.dynalink.linker.LinkRequest;
ec4c069f9436 8141338: Move jdk.internal.dynalink package to jdk.dynalink
attila
parents: 33686
diff changeset
    45
import jdk.dynalink.linker.LinkerServices;
ec4c069f9436 8141338: Move jdk.internal.dynalink package to jdk.dynalink
attila
parents: 33686
diff changeset
    46
import jdk.dynalink.linker.MethodTypeConversionStrategy;
34979
03b189baa361 8144919: Implement missing member handler for BeansLinker
attila
parents: 34732
diff changeset
    47
import jdk.dynalink.linker.TypeBasedGuardingDynamicLinker;
34447
ec4c069f9436 8141338: Move jdk.internal.dynalink package to jdk.dynalink
attila
parents: 33686
diff changeset
    48
import jdk.dynalink.linker.support.TypeUtilities;
19889
63af9358d0dc 8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents: 19627
diff changeset
    49
import jdk.nashorn.api.scripting.JSObject;
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    50
import jdk.nashorn.internal.codegen.CompilerConstants.Call;
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22668
diff changeset
    51
import jdk.nashorn.internal.lookup.MethodHandleFactory;
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22668
diff changeset
    52
import jdk.nashorn.internal.lookup.MethodHandleFunctionality;
33685
343aaf358c21 8141538: Make DynamicLinker specific to a Context in Nashorn
attila
parents: 33533
diff changeset
    53
import jdk.nashorn.internal.runtime.Context;
27815
6ed8e97e8fb6 8051778: support bind on all Nashorn callables
attila
parents: 27360
diff changeset
    54
import jdk.nashorn.internal.runtime.ECMAException;
19090
b5cf0f877326 8021164: REGRESSION: test262 failures after JDK-8021122
sundar
parents: 19088
diff changeset
    55
import jdk.nashorn.internal.runtime.JSType;
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22668
diff changeset
    56
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
    57
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
    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
/**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    61
 * This class houses bootstrap method for invokedynamic instructions generated by compiler.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    62
 */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    63
public final class Bootstrap {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    64
    /** Reference to the seed boostrap function */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    65
    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
    66
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22668
diff changeset
    67
    private static final MethodHandleFunctionality MH = MethodHandleFactory.getFunctionality();
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22668
diff changeset
    68
28438
f164fc2618a0 8068573: POJO setter using [] syntax throws an exception
attila
parents: 28437
diff changeset
    69
    private static final MethodHandle VOID_TO_OBJECT = MH.constant(Object.class, ScriptRuntime.UNDEFINED);
f164fc2618a0 8068573: POJO setter using [] syntax throws an exception
attila
parents: 28437
diff changeset
    70
34979
03b189baa361 8144919: Implement missing member handler for BeansLinker
attila
parents: 34732
diff changeset
    71
    private static final BeansLinker beansLinker = new BeansLinker(Bootstrap::createMissingMemberHandler);
03b189baa361 8144919: Implement missing member handler for BeansLinker
attila
parents: 34732
diff changeset
    72
    private static final GuardingDynamicLinker[] prioritizedLinkers;
03b189baa361 8144919: Implement missing member handler for BeansLinker
attila
parents: 34732
diff changeset
    73
    private static final GuardingDynamicLinker[] fallbackLinkers;
03b189baa361 8144919: Implement missing member handler for BeansLinker
attila
parents: 34732
diff changeset
    74
    static {
03b189baa361 8144919: Implement missing member handler for BeansLinker
attila
parents: 34732
diff changeset
    75
        final NashornBeansLinker nashornBeansLinker = new NashornBeansLinker(beansLinker);
03b189baa361 8144919: Implement missing member handler for BeansLinker
attila
parents: 34732
diff changeset
    76
        prioritizedLinkers = new GuardingDynamicLinker[] {
03b189baa361 8144919: Implement missing member handler for BeansLinker
attila
parents: 34732
diff changeset
    77
            new NashornLinker(),
03b189baa361 8144919: Implement missing member handler for BeansLinker
attila
parents: 34732
diff changeset
    78
            new NashornPrimitiveLinker(),
03b189baa361 8144919: Implement missing member handler for BeansLinker
attila
parents: 34732
diff changeset
    79
            new NashornStaticClassLinker(beansLinker),
03b189baa361 8144919: Implement missing member handler for BeansLinker
attila
parents: 34732
diff changeset
    80
            new BoundCallableLinker(),
03b189baa361 8144919: Implement missing member handler for BeansLinker
attila
parents: 34732
diff changeset
    81
            new JavaSuperAdapterLinker(beansLinker),
03b189baa361 8144919: Implement missing member handler for BeansLinker
attila
parents: 34732
diff changeset
    82
            new JSObjectLinker(nashornBeansLinker),
03b189baa361 8144919: Implement missing member handler for BeansLinker
attila
parents: 34732
diff changeset
    83
            new BrowserJSObjectLinker(nashornBeansLinker),
03b189baa361 8144919: Implement missing member handler for BeansLinker
attila
parents: 34732
diff changeset
    84
            new ReflectionCheckLinker()
03b189baa361 8144919: Implement missing member handler for BeansLinker
attila
parents: 34732
diff changeset
    85
        };
03b189baa361 8144919: Implement missing member handler for BeansLinker
attila
parents: 34732
diff changeset
    86
        fallbackLinkers = new GuardingDynamicLinker[] {nashornBeansLinker, new NashornBottomLinker() };
03b189baa361 8144919: Implement missing member handler for BeansLinker
attila
parents: 34732
diff changeset
    87
    }
03b189baa361 8144919: Implement missing member handler for BeansLinker
attila
parents: 34732
diff changeset
    88
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    89
    // do not create me!!
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    90
    private Bootstrap() {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    91
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    92
33685
343aaf358c21 8141538: Make DynamicLinker specific to a Context in Nashorn
attila
parents: 33533
diff changeset
    93
    /**
343aaf358c21 8141538: Make DynamicLinker specific to a Context in Nashorn
attila
parents: 33533
diff changeset
    94
     * Creates a Nashorn dynamic linker with the given app class loader.
343aaf358c21 8141538: Make DynamicLinker specific to a Context in Nashorn
attila
parents: 33533
diff changeset
    95
     * @param appLoader the app class loader. It will be used to discover
343aaf358c21 8141538: Make DynamicLinker specific to a Context in Nashorn
attila
parents: 33533
diff changeset
    96
     * additional language runtime linkers (if any).
33686
1391474a6405 8141550: Introduce a command line option instead of nashorn.unstable.relink.threshold system property
attila
parents: 33685
diff changeset
    97
     * @param unstableRelinkThreshold the unstable relink threshold
33685
343aaf358c21 8141538: Make DynamicLinker specific to a Context in Nashorn
attila
parents: 33533
diff changeset
    98
     * @return a newly created dynamic linker.
343aaf358c21 8141538: Make DynamicLinker specific to a Context in Nashorn
attila
parents: 33533
diff changeset
    99
     */
33686
1391474a6405 8141550: Introduce a command line option instead of nashorn.unstable.relink.threshold system property
attila
parents: 33685
diff changeset
   100
    public static DynamicLinker createDynamicLinker(final ClassLoader appLoader,
1391474a6405 8141550: Introduce a command line option instead of nashorn.unstable.relink.threshold system property
attila
parents: 33685
diff changeset
   101
            final int unstableRelinkThreshold) {
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   102
        final DynamicLinkerFactory factory = new DynamicLinkerFactory();
34979
03b189baa361 8144919: Implement missing member handler for BeansLinker
attila
parents: 34732
diff changeset
   103
        factory.setPrioritizedLinkers(prioritizedLinkers);
03b189baa361 8144919: Implement missing member handler for BeansLinker
attila
parents: 34732
diff changeset
   104
        factory.setFallbackLinkers(fallbackLinkers);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   105
        factory.setSyncOnRelink(true);
34979
03b189baa361 8144919: Implement missing member handler for BeansLinker
attila
parents: 34732
diff changeset
   106
        factory.setPrelinkTransformer((inv, request, linkerServices) -> {
03b189baa361 8144919: Implement missing member handler for BeansLinker
attila
parents: 34732
diff changeset
   107
            final CallSiteDescriptor desc = request.getCallSiteDescriptor();
03b189baa361 8144919: Implement missing member handler for BeansLinker
attila
parents: 34732
diff changeset
   108
            return OptimisticReturnFilters.filterOptimisticReturnValue(inv, desc).asType(linkerServices, desc.getMethodType());
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22668
diff changeset
   109
        });
34979
03b189baa361 8144919: Implement missing member handler for BeansLinker
attila
parents: 34732
diff changeset
   110
        factory.setAutoConversionStrategy(Bootstrap::unboxReturnType);
28881
0008daeef352 8072596: Arrays.asList results in ClassCastException with a JS array
attila
parents: 28438
diff changeset
   111
        factory.setInternalObjectsFilter(NashornBeansLinker.createHiddenObjectFilter());
33686
1391474a6405 8141550: Introduce a command line option instead of nashorn.unstable.relink.threshold system property
attila
parents: 33685
diff changeset
   112
        factory.setUnstableRelinkThreshold(unstableRelinkThreshold);
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.
33685
343aaf358c21 8141538: Make DynamicLinker specific to a Context in Nashorn
attila
parents: 33533
diff changeset
   115
        factory.setClassLoader(appLoader);
343aaf358c21 8141538: Make DynamicLinker specific to a Context in Nashorn
attila
parents: 33533
diff changeset
   116
        return factory.createLinker();
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   117
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   118
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   119
    /**
34979
03b189baa361 8144919: Implement missing member handler for BeansLinker
attila
parents: 34732
diff changeset
   120
     * Returns a dynamic linker for the specific Java class using beans semantics.
03b189baa361 8144919: Implement missing member handler for BeansLinker
attila
parents: 34732
diff changeset
   121
     * @param clazz the Java class
03b189baa361 8144919: Implement missing member handler for BeansLinker
attila
parents: 34732
diff changeset
   122
     * @return a dynamic linker for the specific Java class using beans semantics.
03b189baa361 8144919: Implement missing member handler for BeansLinker
attila
parents: 34732
diff changeset
   123
     */
03b189baa361 8144919: Implement missing member handler for BeansLinker
attila
parents: 34732
diff changeset
   124
    public static TypeBasedGuardingDynamicLinker getBeanLinkerForClass(final Class<?> clazz) {
03b189baa361 8144919: Implement missing member handler for BeansLinker
attila
parents: 34732
diff changeset
   125
        return beansLinker.getLinkerForClass(clazz);
03b189baa361 8144919: Implement missing member handler for BeansLinker
attila
parents: 34732
diff changeset
   126
    }
03b189baa361 8144919: Implement missing member handler for BeansLinker
attila
parents: 34732
diff changeset
   127
03b189baa361 8144919: Implement missing member handler for BeansLinker
attila
parents: 34732
diff changeset
   128
    /**
19088
153f268bfa72 8021122: Not all callables are handled for toString and other function valued properties
sundar
parents: 18878
diff changeset
   129
     * 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
   130
     * @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
   131
     * @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
   132
     */
153f268bfa72 8021122: Not all callables are handled for toString and other function valued properties
sundar
parents: 18878
diff changeset
   133
    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
   134
        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
   135
            return false;
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
        return obj instanceof ScriptFunction ||
27815
6ed8e97e8fb6 8051778: support bind on all Nashorn callables
attila
parents: 27360
diff changeset
   139
            isJSObjectFunction(obj) ||
6ed8e97e8fb6 8051778: support bind on all Nashorn callables
attila
parents: 27360
diff changeset
   140
            BeansLinker.isDynamicMethod(obj) ||
6ed8e97e8fb6 8051778: support bind on all Nashorn callables
attila
parents: 27360
diff changeset
   141
            obj instanceof BoundCallable ||
19627
90d910ec15a3 8023630: Implement Java.super() as the preferred way to call super methods
attila
parents: 19455
diff changeset
   142
            isFunctionalInterfaceObject(obj) ||
90d910ec15a3 8023630: Implement Java.super() as the preferred way to call super methods
attila
parents: 19455
diff changeset
   143
            obj instanceof StaticClass;
19088
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
    /**
27815
6ed8e97e8fb6 8051778: support bind on all Nashorn callables
attila
parents: 27360
diff changeset
   147
     * Returns true if the given object is a strict callable
6ed8e97e8fb6 8051778: support bind on all Nashorn callables
attila
parents: 27360
diff changeset
   148
     * @param callable the callable object to be checked for strictness
6ed8e97e8fb6 8051778: support bind on all Nashorn callables
attila
parents: 27360
diff changeset
   149
     * @return true if the obj is a strict callable, false if it is a non-strict callable.
6ed8e97e8fb6 8051778: support bind on all Nashorn callables
attila
parents: 27360
diff changeset
   150
     * @throws ECMAException with {@code TypeError} if the object is not a callable.
6ed8e97e8fb6 8051778: support bind on all Nashorn callables
attila
parents: 27360
diff changeset
   151
     */
6ed8e97e8fb6 8051778: support bind on all Nashorn callables
attila
parents: 27360
diff changeset
   152
    public static boolean isStrictCallable(final Object callable) {
6ed8e97e8fb6 8051778: support bind on all Nashorn callables
attila
parents: 27360
diff changeset
   153
        if (callable instanceof ScriptFunction) {
6ed8e97e8fb6 8051778: support bind on all Nashorn callables
attila
parents: 27360
diff changeset
   154
            return ((ScriptFunction)callable).isStrict();
6ed8e97e8fb6 8051778: support bind on all Nashorn callables
attila
parents: 27360
diff changeset
   155
        } else if (isJSObjectFunction(callable)) {
6ed8e97e8fb6 8051778: support bind on all Nashorn callables
attila
parents: 27360
diff changeset
   156
            return ((JSObject)callable).isStrictFunction();
6ed8e97e8fb6 8051778: support bind on all Nashorn callables
attila
parents: 27360
diff changeset
   157
        } else if (callable instanceof BoundCallable) {
6ed8e97e8fb6 8051778: support bind on all Nashorn callables
attila
parents: 27360
diff changeset
   158
            return isStrictCallable(((BoundCallable)callable).getCallable());
6ed8e97e8fb6 8051778: support bind on all Nashorn callables
attila
parents: 27360
diff changeset
   159
        } else if (BeansLinker.isDynamicMethod(callable) || callable instanceof StaticClass) {
6ed8e97e8fb6 8051778: support bind on all Nashorn callables
attila
parents: 27360
diff changeset
   160
            return false;
6ed8e97e8fb6 8051778: support bind on all Nashorn callables
attila
parents: 27360
diff changeset
   161
        }
6ed8e97e8fb6 8051778: support bind on all Nashorn callables
attila
parents: 27360
diff changeset
   162
        throw notFunction(callable);
6ed8e97e8fb6 8051778: support bind on all Nashorn callables
attila
parents: 27360
diff changeset
   163
    }
6ed8e97e8fb6 8051778: support bind on all Nashorn callables
attila
parents: 27360
diff changeset
   164
6ed8e97e8fb6 8051778: support bind on all Nashorn callables
attila
parents: 27360
diff changeset
   165
    private static ECMAException notFunction(final Object obj) {
6ed8e97e8fb6 8051778: support bind on all Nashorn callables
attila
parents: 27360
diff changeset
   166
        return typeError("not.a.function", ScriptRuntime.safeToString(obj));
6ed8e97e8fb6 8051778: support bind on all Nashorn callables
attila
parents: 27360
diff changeset
   167
    }
6ed8e97e8fb6 8051778: support bind on all Nashorn callables
attila
parents: 27360
diff changeset
   168
6ed8e97e8fb6 8051778: support bind on all Nashorn callables
attila
parents: 27360
diff changeset
   169
    private static boolean isJSObjectFunction(final Object obj) {
6ed8e97e8fb6 8051778: support bind on all Nashorn callables
attila
parents: 27360
diff changeset
   170
        return obj instanceof JSObject && ((JSObject)obj).isFunction();
6ed8e97e8fb6 8051778: support bind on all Nashorn callables
attila
parents: 27360
diff changeset
   171
    }
6ed8e97e8fb6 8051778: support bind on all Nashorn callables
attila
parents: 27360
diff changeset
   172
6ed8e97e8fb6 8051778: support bind on all Nashorn callables
attila
parents: 27360
diff changeset
   173
    /**
19088
153f268bfa72 8021122: Not all callables are handled for toString and other function valued properties
sundar
parents: 18878
diff changeset
   174
     * 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
   175
     * @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
   176
     * @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
   177
     */
153f268bfa72 8021122: Not all callables are handled for toString and other function valued properties
sundar
parents: 18878
diff changeset
   178
    public static boolean isDynamicMethod(final Object obj) {
27815
6ed8e97e8fb6 8051778: support bind on all Nashorn callables
attila
parents: 27360
diff changeset
   179
        return BeansLinker.isDynamicMethod(obj instanceof BoundCallable ? ((BoundCallable)obj).getCallable() : obj);
19088
153f268bfa72 8021122: Not all callables are handled for toString and other function valued properties
sundar
parents: 18878
diff changeset
   180
    }
153f268bfa72 8021122: Not all callables are handled for toString and other function valued properties
sundar
parents: 18878
diff changeset
   181
153f268bfa72 8021122: Not all callables are handled for toString and other function valued properties
sundar
parents: 18878
diff changeset
   182
    /**
153f268bfa72 8021122: Not all callables are handled for toString and other function valued properties
sundar
parents: 18878
diff changeset
   183
     * 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
   184
     * java.lang.FunctionalInterface
153f268bfa72 8021122: Not all callables are handled for toString and other function valued properties
sundar
parents: 18878
diff changeset
   185
     * @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
   186
     * @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
   187
     */
153f268bfa72 8021122: Not all callables are handled for toString and other function valued properties
sundar
parents: 18878
diff changeset
   188
    public static boolean isFunctionalInterfaceObject(final Object obj) {
32436
a6328e0a8dd6 8068901: Surprising behavior with more than one functional interface on a class
sundar
parents: 31738
diff changeset
   189
        return !JSType.isPrimitive(obj) && (NashornBeansLinker.getFunctionalInterfaceMethodName(obj.getClass()) != null);
19088
153f268bfa72 8021122: Not all callables are handled for toString and other function valued properties
sundar
parents: 18878
diff changeset
   190
    }
153f268bfa72 8021122: Not all callables are handled for toString and other function valued properties
sundar
parents: 18878
diff changeset
   191
153f268bfa72 8021122: Not all callables are handled for toString and other function valued properties
sundar
parents: 18878
diff changeset
   192
    /**
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   193
     * 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
   194
     * 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
   195
     * invokedynamic instructions.
35330
7cc832bade3c 8133299: Nashorn Java adapters should not early bind to functions
attila
parents: 34979
diff changeset
   196
     * @param lookup MethodHandle lookup.
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   197
     * @param opDesc Dynalink dynamic operation descriptor.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   198
     * @param type   Method type.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   199
     * @param flags  flags for call type, trace/profile etc.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   200
     * @return CallSite with MethodHandle to appropriate method or null if not found.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   201
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   202
    public static CallSite bootstrap(final Lookup lookup, final String opDesc, final MethodType type, final int flags) {
33685
343aaf358c21 8141538: Make DynamicLinker specific to a Context in Nashorn
attila
parents: 33533
diff changeset
   203
        return Context.getDynamicLinker(lookup.lookupClass()).link(LinkerCallSite.newLinkerCallSite(lookup, opDesc, type, flags));
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   204
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   205
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   206
    /**
35793
b89ddacf3104 8149462: revert changes for 8149186
mhaupt
parents: 35725
diff changeset
   207
     * Boostrapper for math calls that may overflow
b89ddacf3104 8149462: revert changes for 8149186
mhaupt
parents: 35725
diff changeset
   208
     * @param lookup         lookup
b89ddacf3104 8149462: revert changes for 8149186
mhaupt
parents: 35725
diff changeset
   209
     * @param name           name of operation
b89ddacf3104 8149462: revert changes for 8149186
mhaupt
parents: 35725
diff changeset
   210
     * @param type           method type
b89ddacf3104 8149462: revert changes for 8149186
mhaupt
parents: 35725
diff changeset
   211
     * @param programPoint   program point to bind to callsite
b89ddacf3104 8149462: revert changes for 8149186
mhaupt
parents: 35725
diff changeset
   212
     *
b89ddacf3104 8149462: revert changes for 8149186
mhaupt
parents: 35725
diff changeset
   213
     * @return callsite for a math intrinsic node
b89ddacf3104 8149462: revert changes for 8149186
mhaupt
parents: 35725
diff changeset
   214
     */
b89ddacf3104 8149462: revert changes for 8149186
mhaupt
parents: 35725
diff changeset
   215
    public static CallSite mathBootstrap(final Lookup lookup, final String name, final MethodType type, final int programPoint) {
b89ddacf3104 8149462: revert changes for 8149186
mhaupt
parents: 35725
diff changeset
   216
        final MethodHandle mh;
b89ddacf3104 8149462: revert changes for 8149186
mhaupt
parents: 35725
diff changeset
   217
        switch (name) {
b89ddacf3104 8149462: revert changes for 8149186
mhaupt
parents: 35725
diff changeset
   218
        case "iadd":
b89ddacf3104 8149462: revert changes for 8149186
mhaupt
parents: 35725
diff changeset
   219
            mh = JSType.ADD_EXACT.methodHandle();
b89ddacf3104 8149462: revert changes for 8149186
mhaupt
parents: 35725
diff changeset
   220
            break;
b89ddacf3104 8149462: revert changes for 8149186
mhaupt
parents: 35725
diff changeset
   221
        case "isub":
b89ddacf3104 8149462: revert changes for 8149186
mhaupt
parents: 35725
diff changeset
   222
            mh = JSType.SUB_EXACT.methodHandle();
b89ddacf3104 8149462: revert changes for 8149186
mhaupt
parents: 35725
diff changeset
   223
            break;
b89ddacf3104 8149462: revert changes for 8149186
mhaupt
parents: 35725
diff changeset
   224
        case "imul":
b89ddacf3104 8149462: revert changes for 8149186
mhaupt
parents: 35725
diff changeset
   225
            mh = JSType.MUL_EXACT.methodHandle();
b89ddacf3104 8149462: revert changes for 8149186
mhaupt
parents: 35725
diff changeset
   226
            break;
b89ddacf3104 8149462: revert changes for 8149186
mhaupt
parents: 35725
diff changeset
   227
        case "idiv":
b89ddacf3104 8149462: revert changes for 8149186
mhaupt
parents: 35725
diff changeset
   228
            mh = JSType.DIV_EXACT.methodHandle();
b89ddacf3104 8149462: revert changes for 8149186
mhaupt
parents: 35725
diff changeset
   229
            break;
b89ddacf3104 8149462: revert changes for 8149186
mhaupt
parents: 35725
diff changeset
   230
        case "irem":
b89ddacf3104 8149462: revert changes for 8149186
mhaupt
parents: 35725
diff changeset
   231
            mh = JSType.REM_EXACT.methodHandle();
b89ddacf3104 8149462: revert changes for 8149186
mhaupt
parents: 35725
diff changeset
   232
            break;
b89ddacf3104 8149462: revert changes for 8149186
mhaupt
parents: 35725
diff changeset
   233
        case "ineg":
b89ddacf3104 8149462: revert changes for 8149186
mhaupt
parents: 35725
diff changeset
   234
            mh = JSType.NEGATE_EXACT.methodHandle();
b89ddacf3104 8149462: revert changes for 8149186
mhaupt
parents: 35725
diff changeset
   235
            break;
b89ddacf3104 8149462: revert changes for 8149186
mhaupt
parents: 35725
diff changeset
   236
        default:
b89ddacf3104 8149462: revert changes for 8149186
mhaupt
parents: 35725
diff changeset
   237
            throw new AssertionError("unsupported math intrinsic");
b89ddacf3104 8149462: revert changes for 8149186
mhaupt
parents: 35725
diff changeset
   238
        }
b89ddacf3104 8149462: revert changes for 8149186
mhaupt
parents: 35725
diff changeset
   239
        return new ConstantCallSite(MH.insertArguments(mh, mh.type().parameterCount() - 1, programPoint));
b89ddacf3104 8149462: revert changes for 8149186
mhaupt
parents: 35725
diff changeset
   240
    }
b89ddacf3104 8149462: revert changes for 8149186
mhaupt
parents: 35725
diff changeset
   241
b89ddacf3104 8149462: revert changes for 8149186
mhaupt
parents: 35725
diff changeset
   242
    /**
33343
23abd10384a5 8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents: 33337
diff changeset
   243
     * Returns a dynamic invoker for a specified dynamic operation using the
23abd10384a5 8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents: 33337
diff changeset
   244
     * public lookup. You can use this method to create a method handle that
23abd10384a5 8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents: 33337
diff changeset
   245
     * when invoked acts completely as if it were a Nashorn-linked call site.
23abd10384a5 8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents: 33337
diff changeset
   246
     * Note that the available operations are encoded in the flags, see
23abd10384a5 8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents: 33337
diff changeset
   247
     * {@link NashornCallSiteDescriptor} operation constants. If the operation
23abd10384a5 8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents: 33337
diff changeset
   248
     * takes a name, it should be set otherwise empty name (not null) should be
23abd10384a5 8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents: 33337
diff changeset
   249
     * used. All names (including the empty one) should be encoded using
23abd10384a5 8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents: 33337
diff changeset
   250
     * {@link NameCodec#encode(String)}. Few examples:
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   251
     * <ul>
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   252
     *   <li>Get a named property with fixed name:
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   253
     *     <pre>
33343
23abd10384a5 8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents: 33337
diff changeset
   254
     * MethodHandle getColor = Boostrap.createDynamicInvoker(
23abd10384a5 8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents: 33337
diff changeset
   255
     *     "color",
23abd10384a5 8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents: 33337
diff changeset
   256
     *     NashornCallSiteDescriptor.GET_PROPERTY,
23abd10384a5 8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents: 33337
diff changeset
   257
     *     Object.class, Object.class);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   258
     * Object obj = ...; // somehow obtain the object
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   259
     * Object color = getColor.invokeExact(obj);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   260
     *     </pre>
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   261
     *   </li>
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   262
     *   <li>Get a named property with variable name:
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   263
     *     <pre>
33343
23abd10384a5 8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents: 33337
diff changeset
   264
     * MethodHandle getProperty = Boostrap.createDynamicInvoker(
23abd10384a5 8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents: 33337
diff changeset
   265
     *     NameCodec.encode(""),
23abd10384a5 8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents: 33337
diff changeset
   266
     *     NashornCallSiteDescriptor.GET_PROPERTY,
23abd10384a5 8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents: 33337
diff changeset
   267
     *     Object.class, Object.class, String.class);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   268
     * Object obj = ...; // somehow obtain the object
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   269
     * Object color = getProperty.invokeExact(obj, "color");
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   270
     * Object shape = getProperty.invokeExact(obj, "shape");
33343
23abd10384a5 8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents: 33337
diff changeset
   271
     *
23abd10384a5 8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents: 33337
diff changeset
   272
     * MethodHandle getNumProperty = Boostrap.createDynamicInvoker(
23abd10384a5 8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents: 33337
diff changeset
   273
     *     NameCodec.encode(""),
23abd10384a5 8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents: 33337
diff changeset
   274
     *     NashornCallSiteDescriptor.GET_ELEMENT,
23abd10384a5 8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents: 33337
diff changeset
   275
     *     Object.class, Object.class, int.class);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   276
     * Object elem42 = getNumProperty.invokeExact(obj, 42);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   277
     *     </pre>
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   278
     *   </li>
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   279
     *   <li>Set a named property with fixed name:
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   280
     *     <pre>
33343
23abd10384a5 8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents: 33337
diff changeset
   281
     * MethodHandle setColor = Boostrap.createDynamicInvoker(
23abd10384a5 8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents: 33337
diff changeset
   282
     *     "color",
23abd10384a5 8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents: 33337
diff changeset
   283
     *     NashornCallSiteDescriptor.SET_PROPERTY,
23abd10384a5 8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents: 33337
diff changeset
   284
     *     void.class, Object.class, Object.class);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   285
     * Object obj = ...; // somehow obtain the object
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   286
     * setColor.invokeExact(obj, Color.BLUE);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   287
     *     </pre>
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   288
     *   </li>
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   289
     *   <li>Set a property with variable name:
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   290
     *     <pre>
33343
23abd10384a5 8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents: 33337
diff changeset
   291
     * MethodHandle setProperty = Boostrap.createDynamicInvoker(
23abd10384a5 8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents: 33337
diff changeset
   292
     *     NameCodec.encode(""),
23abd10384a5 8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents: 33337
diff changeset
   293
     *     NashornCallSiteDescriptor.SET_PROPERTY,
23abd10384a5 8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents: 33337
diff changeset
   294
     *     void.class, Object.class, String.class, Object.class);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   295
     * Object obj = ...; // somehow obtain the object
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   296
     * setProperty.invokeExact(obj, "color", Color.BLUE);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   297
     * setProperty.invokeExact(obj, "shape", Shape.CIRCLE);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   298
     *     </pre>
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   299
     *   </li>
33343
23abd10384a5 8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents: 33337
diff changeset
   300
     *   <li>Call a function on an object; note it's a two-step process: get the
23abd10384a5 8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents: 33337
diff changeset
   301
     *   method, then invoke the method. This is the actual:
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   302
     *     <pre>
33343
23abd10384a5 8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents: 33337
diff changeset
   303
     * MethodHandle findFooFunction = Boostrap.createDynamicInvoker(
23abd10384a5 8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents: 33337
diff changeset
   304
     *     "foo",
23abd10384a5 8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents: 33337
diff changeset
   305
     *     NashornCallSiteDescriptor.GET_METHOD,
23abd10384a5 8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents: 33337
diff changeset
   306
     *     Object.class, Object.class);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   307
     * Object obj = ...; // somehow obtain the object
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   308
     * Object foo_fn = findFooFunction.invokeExact(obj);
33343
23abd10384a5 8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents: 33337
diff changeset
   309
     * MethodHandle callFunctionWithTwoArgs = Boostrap.createDynamicCallInvoker(
23abd10384a5 8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents: 33337
diff changeset
   310
     *     Object.class, Object.class, Object.class, Object.class, Object.class);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   311
     * // Note: "call" operation takes a function, then a "this" value, then the arguments:
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   312
     * Object foo_retval = callFunctionWithTwoArgs.invokeExact(foo_fn, obj, arg1, arg2);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   313
     *     </pre>
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   314
     *   </li>
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   315
     * </ul>
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   316
     * Few additional remarks:
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   317
     * <ul>
33343
23abd10384a5 8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents: 33337
diff changeset
   318
     * <li>Just as Nashorn works with any Java object, the invokers returned
23abd10384a5 8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents: 33337
diff changeset
   319
     * from this method can also be applied to arbitrary Java objects in
23abd10384a5 8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents: 33337
diff changeset
   320
     * addition to Nashorn JavaScript objects.</li>
23abd10384a5 8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents: 33337
diff changeset
   321
     * <li>For invoking a named function on an object, you can also use the
23abd10384a5 8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents: 33337
diff changeset
   322
     * {@link InvokeByName} convenience class.</li>
23abd10384a5 8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents: 33337
diff changeset
   323
     * <li>There's no rule that the variable property identifier has to be a
23abd10384a5 8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents: 33337
diff changeset
   324
     * {@code String} for {@code GET_PROPERTY/SET_PROPERTY} and {@code int} for
23abd10384a5 8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents: 33337
diff changeset
   325
     * {@code GET_ELEMENT/SET_ELEMENT}. You can declare their type to be
23abd10384a5 8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents: 33337
diff changeset
   326
     * {@code int}, {@code double}, {@code Object}, and so on regardless of the
23abd10384a5 8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents: 33337
diff changeset
   327
     * kind of the operation.</li>
23abd10384a5 8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents: 33337
diff changeset
   328
     * <li>You can be as specific in parameter types as you want. E.g. if you
23abd10384a5 8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents: 33337
diff changeset
   329
     * know that the receiver of the operation will always be
23abd10384a5 8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents: 33337
diff changeset
   330
     * {@code ScriptObject}, you can pass {@code ScriptObject.class} as its
23abd10384a5 8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents: 33337
diff changeset
   331
     * parameter type. If you happen to link to a method that expects different
23abd10384a5 8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents: 33337
diff changeset
   332
     * types, (you can use these invokers on POJOs too, after all, and end up
23abd10384a5 8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents: 33337
diff changeset
   333
     * linking with their methods that have strongly-typed signatures), all
23abd10384a5 8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents: 33337
diff changeset
   334
     * necessary conversions allowed by either Java or JavaScript will be
23abd10384a5 8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents: 33337
diff changeset
   335
     * applied: if invoked methods specify either primitive or wrapped Java
23abd10384a5 8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents: 33337
diff changeset
   336
     * numeric types, or {@code String} or {@code boolean/Boolean}, then the
23abd10384a5 8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents: 33337
diff changeset
   337
     * parameters might be subjected to standard ECMAScript {@code ToNumber},
23abd10384a5 8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents: 33337
diff changeset
   338
     * {@code ToString}, and {@code ToBoolean} conversion, respectively. Less
23abd10384a5 8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents: 33337
diff changeset
   339
     * obviously, if the expected parameter type is a SAM type, and you pass a
23abd10384a5 8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents: 33337
diff changeset
   340
     * JavaScript function, a proxy object implementing the SAM type and
23abd10384a5 8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents: 33337
diff changeset
   341
     * delegating to the function will be passed. Linkage can often be optimized
23abd10384a5 8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents: 33337
diff changeset
   342
     * when linkers have more specific type information than "everything can be
23abd10384a5 8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents: 33337
diff changeset
   343
     * an object".</li>
23abd10384a5 8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents: 33337
diff changeset
   344
     * <li>You can also be as specific in return types as you want. For return
23abd10384a5 8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents: 33337
diff changeset
   345
     * types any necessary type conversion available in either Java or
23abd10384a5 8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents: 33337
diff changeset
   346
     * JavaScript will be automatically applied, similar to the process
23abd10384a5 8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents: 33337
diff changeset
   347
     * described for parameters, only in reverse direction: if you specify any
23abd10384a5 8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents: 33337
diff changeset
   348
     * either primitive or wrapped Java numeric type, or {@code String} or
23abd10384a5 8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents: 33337
diff changeset
   349
     * {@code boolean/Boolean}, then the return values will be subjected to
23abd10384a5 8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents: 33337
diff changeset
   350
     * standard ECMAScript {@code ToNumber}, {@code ToString}, and
23abd10384a5 8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents: 33337
diff changeset
   351
     * {@code ToBoolean} conversion, respectively. Less obviously, if the return
23abd10384a5 8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents: 33337
diff changeset
   352
     * type is a SAM type, and the return value is a JavaScript function, a
23abd10384a5 8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents: 33337
diff changeset
   353
     * proxy object implementing the SAM type and delegating to the function
23abd10384a5 8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents: 33337
diff changeset
   354
     * will be returned.</li>
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   355
     * </ul>
33343
23abd10384a5 8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents: 33337
diff changeset
   356
     * @param name name at the call site. Must not be null. Must be encoded
23abd10384a5 8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents: 33337
diff changeset
   357
     * using {@link NameCodec#encode(String)}. If the operation does not take a
23abd10384a5 8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents: 33337
diff changeset
   358
     * name, use empty string (also has to be encoded).
23abd10384a5 8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents: 33337
diff changeset
   359
     * @param flags the call site flags for the operation; see
23abd10384a5 8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents: 33337
diff changeset
   360
     * {@link NashornCallSiteDescriptor} for available flags. The most important
23abd10384a5 8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents: 33337
diff changeset
   361
     * part of the flags are the ones encoding the actual operation.
16173
c41d062f7d2a 8006293: Reduce ScriptObject.findCallMethodMethod
attila
parents: 16151
diff changeset
   362
     * @param rtype the return type for the operation
c41d062f7d2a 8006293: Reduce ScriptObject.findCallMethodMethod
attila
parents: 16151
diff changeset
   363
     * @param ptypes the parameter types for the operation
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   364
     * @return MethodHandle for invoking the operation.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   365
     */
33343
23abd10384a5 8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents: 33337
diff changeset
   366
    public static MethodHandle createDynamicInvoker(final String name, final int flags, final Class<?> rtype, final Class<?>... ptypes) {
23abd10384a5 8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents: 33337
diff changeset
   367
        return bootstrap(MethodHandles.publicLookup(), name, MethodType.methodType(rtype, ptypes), flags).dynamicInvoker();
16173
c41d062f7d2a 8006293: Reduce ScriptObject.findCallMethodMethod
attila
parents: 16151
diff changeset
   368
    }
c41d062f7d2a 8006293: Reduce ScriptObject.findCallMethodMethod
attila
parents: 16151
diff changeset
   369
c41d062f7d2a 8006293: Reduce ScriptObject.findCallMethodMethod
attila
parents: 16151
diff changeset
   370
    /**
33343
23abd10384a5 8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents: 33337
diff changeset
   371
     * Returns a dynamic invoker for the {@link NashornCallSiteDescriptor#CALL}
23abd10384a5 8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents: 33337
diff changeset
   372
     * operation using the public lookup.
27307
62ed492cbe63 8062401: User accessors require boxing and do not support optimistic types
hannesw
parents: 26237
diff changeset
   373
     * @param rtype the return type for the operation
62ed492cbe63 8062401: User accessors require boxing and do not support optimistic types
hannesw
parents: 26237
diff changeset
   374
     * @param ptypes the parameter types for the operation
33343
23abd10384a5 8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents: 33337
diff changeset
   375
     * @return a dynamic invoker for the {@code CALL} operation.
27307
62ed492cbe63 8062401: User accessors require boxing and do not support optimistic types
hannesw
parents: 26237
diff changeset
   376
     */
33343
23abd10384a5 8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents: 33337
diff changeset
   377
    public static MethodHandle createDynamicCallInvoker(final Class<?> rtype, final Class<?>... ptypes) {
23abd10384a5 8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents: 33337
diff changeset
   378
        return createDynamicInvoker("", NashornCallSiteDescriptor.CALL, rtype, ptypes);
27307
62ed492cbe63 8062401: User accessors require boxing and do not support optimistic types
hannesw
parents: 26237
diff changeset
   379
    }
62ed492cbe63 8062401: User accessors require boxing and do not support optimistic types
hannesw
parents: 26237
diff changeset
   380
62ed492cbe63 8062401: User accessors require boxing and do not support optimistic types
hannesw
parents: 26237
diff changeset
   381
    /**
33343
23abd10384a5 8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents: 33337
diff changeset
   382
     * Returns a dynamic invoker for a specified dynamic operation using the
23abd10384a5 8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents: 33337
diff changeset
   383
     * public lookup. Similar to
23abd10384a5 8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents: 33337
diff changeset
   384
     * {@link #createDynamicInvoker(String, int, Class, Class...)} but with
23abd10384a5 8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents: 33337
diff changeset
   385
     * already precomposed method type.
23abd10384a5 8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents: 33337
diff changeset
   386
     * @param name name at the call site.
23abd10384a5 8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents: 33337
diff changeset
   387
     * @param flags flags at the call site
16173
c41d062f7d2a 8006293: Reduce ScriptObject.findCallMethodMethod
attila
parents: 16151
diff changeset
   388
     * @param type the method type for the operation
c41d062f7d2a 8006293: Reduce ScriptObject.findCallMethodMethod
attila
parents: 16151
diff changeset
   389
     * @return MethodHandle for invoking the operation.
c41d062f7d2a 8006293: Reduce ScriptObject.findCallMethodMethod
attila
parents: 16151
diff changeset
   390
     */
33343
23abd10384a5 8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents: 33337
diff changeset
   391
    public static MethodHandle createDynamicInvoker(final String name, final int flags, final MethodType type) {
23abd10384a5 8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents: 33337
diff changeset
   392
        return bootstrap(MethodHandles.publicLookup(), name, type, flags).dynamicInvoker();
16147
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
    /**
27815
6ed8e97e8fb6 8051778: support bind on all Nashorn callables
attila
parents: 27360
diff changeset
   396
     * Binds any object Nashorn can use as a [[Callable]] to a receiver and optionally arguments.
6ed8e97e8fb6 8051778: support bind on all Nashorn callables
attila
parents: 27360
diff changeset
   397
     * @param callable the callable to bind
18876
ada98218aaae 8020324: Implement Object.bindProperties(target, source) for beans
attila
parents: 18841
diff changeset
   398
     * @param boundThis the bound "this" value.
27815
6ed8e97e8fb6 8051778: support bind on all Nashorn callables
attila
parents: 27360
diff changeset
   399
     * @param boundArgs the bound arguments. Can be either null or empty array to signify no arguments are bound.
6ed8e97e8fb6 8051778: support bind on all Nashorn callables
attila
parents: 27360
diff changeset
   400
     * @return a bound callable.
6ed8e97e8fb6 8051778: support bind on all Nashorn callables
attila
parents: 27360
diff changeset
   401
     * @throws ECMAException with {@code TypeError} if the object is not a callable.
18876
ada98218aaae 8020324: Implement Object.bindProperties(target, source) for beans
attila
parents: 18841
diff changeset
   402
     */
27815
6ed8e97e8fb6 8051778: support bind on all Nashorn callables
attila
parents: 27360
diff changeset
   403
    public static Object bindCallable(final Object callable, final Object boundThis, final Object[] boundArgs) {
32527
b105632002c5 8027137: Merge ScriptFunction and ScriptFunctionImpl
sundar
parents: 32436
diff changeset
   404
        if (callable instanceof ScriptFunction) {
b105632002c5 8027137: Merge ScriptFunction and ScriptFunctionImpl
sundar
parents: 32436
diff changeset
   405
            return ((ScriptFunction)callable).createBound(boundThis, boundArgs);
27815
6ed8e97e8fb6 8051778: support bind on all Nashorn callables
attila
parents: 27360
diff changeset
   406
        } else if (callable instanceof BoundCallable) {
6ed8e97e8fb6 8051778: support bind on all Nashorn callables
attila
parents: 27360
diff changeset
   407
            return ((BoundCallable)callable).bind(boundArgs);
6ed8e97e8fb6 8051778: support bind on all Nashorn callables
attila
parents: 27360
diff changeset
   408
        } else if (isCallable(callable)) {
6ed8e97e8fb6 8051778: support bind on all Nashorn callables
attila
parents: 27360
diff changeset
   409
            return new BoundCallable(callable, boundThis, boundArgs);
6ed8e97e8fb6 8051778: support bind on all Nashorn callables
attila
parents: 27360
diff changeset
   410
        }
6ed8e97e8fb6 8051778: support bind on all Nashorn callables
attila
parents: 27360
diff changeset
   411
        throw notFunction(callable);
18876
ada98218aaae 8020324: Implement Object.bindProperties(target, source) for beans
attila
parents: 18841
diff changeset
   412
    }
18878
078eb52cf5bc 8020508: Enforce reflection access restrictions on Object.bindProperties
attila
parents: 18876
diff changeset
   413
078eb52cf5bc 8020508: Enforce reflection access restrictions on Object.bindProperties
attila
parents: 18876
diff changeset
   414
    /**
19627
90d910ec15a3 8023630: Implement Java.super() as the preferred way to call super methods
attila
parents: 19455
diff changeset
   415
     * 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
   416
     * methods on it.
90d910ec15a3 8023630: Implement Java.super() as the preferred way to call super methods
attila
parents: 19455
diff changeset
   417
     * @param adapter the original adapter
90d910ec15a3 8023630: Implement Java.super() as the preferred way to call super methods
attila
parents: 19455
diff changeset
   418
     * @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
   419
     */
90d910ec15a3 8023630: Implement Java.super() as the preferred way to call super methods
attila
parents: 19455
diff changeset
   420
    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
   421
        return new JavaSuperAdapter(adapter);
90d910ec15a3 8023630: Implement Java.super() as the preferred way to call super methods
attila
parents: 19455
diff changeset
   422
    }
90d910ec15a3 8023630: Implement Java.super() as the preferred way to call super methods
attila
parents: 19455
diff changeset
   423
90d910ec15a3 8023630: Implement Java.super() as the preferred way to call super methods
attila
parents: 19455
diff changeset
   424
    /**
18878
078eb52cf5bc 8020508: Enforce reflection access restrictions on Object.bindProperties
attila
parents: 18876
diff changeset
   425
     * 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
   426
     * {@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
   427
     * 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
   428
     * @param clazz the class being tested
22668
245094625886 8032948: Nashorn linkages awry
sundar
parents: 21686
diff changeset
   429
     * @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
   430
     */
24738
be2026c9717c 8039746: Transform applies to calls wherever possible, for ScriptFunctions and JSObjects.
lagergren
parents: 24719
diff changeset
   431
    public static void checkReflectionAccess(final Class<?> clazz, final boolean isStatic) {
22668
245094625886 8032948: Nashorn linkages awry
sundar
parents: 21686
diff changeset
   432
        ReflectionCheckLinker.checkReflectionAccess(clazz, isStatic);
18878
078eb52cf5bc 8020508: Enforce reflection access restrictions on Object.bindProperties
attila
parents: 18876
diff changeset
   433
    }
078eb52cf5bc 8020508: Enforce reflection access restrictions on Object.bindProperties
attila
parents: 18876
diff changeset
   434
18876
ada98218aaae 8020324: Implement Object.bindProperties(target, source) for beans
attila
parents: 18841
diff changeset
   435
    /**
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   436
     * 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
   437
     * 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
   438
     * 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
   439
     * 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
   440
     * type converter method handles) outside of a code path that is linking a call site.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   441
     * @return Nashorn's internal dynamic linker's services object.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   442
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   443
    public static LinkerServices getLinkerServices() {
33685
343aaf358c21 8141538: Make DynamicLinker specific to a Context in Nashorn
attila
parents: 33533
diff changeset
   444
        return Context.getDynamicLinker().getLinkerServices();
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   445
    }
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
     * 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
   449
     * 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
   450
     * 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
   451
     * 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
   452
     * to the aggregating linker is the responsibility of the linkers themselves.
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   453
     * @param inv the guarded invocation that needs to be type-converted. Can be null.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   454
     * @param linkerServices the linker services object providing the type conversions.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   455
     * @param desc the call site descriptor to whose method type the invocation needs to conform.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   456
     * @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
   457
     * already conforms to the requested type, it is returned unchanged.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   458
     */
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22668
diff changeset
   459
    static GuardedInvocation asTypeSafeReturn(final GuardedInvocation inv, final LinkerServices linkerServices, final CallSiteDescriptor desc) {
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22668
diff changeset
   460
        return inv == null ? null : inv.asTypeSafeReturn(linkerServices, desc.getMethodType());
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   461
    }
27360
a19c14022fa4 8059443: NPE when unboxing return values
attila
parents: 27307
diff changeset
   462
a19c14022fa4 8059443: NPE when unboxing return values
attila
parents: 27307
diff changeset
   463
    /**
a19c14022fa4 8059443: NPE when unboxing return values
attila
parents: 27307
diff changeset
   464
     * Adapts the return type of the method handle with {@code explicitCastArguments} when it is an unboxing
a19c14022fa4 8059443: NPE when unboxing return values
attila
parents: 27307
diff changeset
   465
     * conversion. This will ensure that nulls are unwrapped to false or 0.
a19c14022fa4 8059443: NPE when unboxing return values
attila
parents: 27307
diff changeset
   466
     * @param target the target method handle
a19c14022fa4 8059443: NPE when unboxing return values
attila
parents: 27307
diff changeset
   467
     * @param newType the desired new type. Note that this method does not adapt the method handle completely to the
a19c14022fa4 8059443: NPE when unboxing return values
attila
parents: 27307
diff changeset
   468
     * new type, it only adapts the return type; this is allowed as per
a19c14022fa4 8059443: NPE when unboxing return values
attila
parents: 27307
diff changeset
   469
     * {@link DynamicLinkerFactory#setAutoConversionStrategy(MethodTypeConversionStrategy)}, which is what this method
a19c14022fa4 8059443: NPE when unboxing return values
attila
parents: 27307
diff changeset
   470
     * is used for.
a19c14022fa4 8059443: NPE when unboxing return values
attila
parents: 27307
diff changeset
   471
     * @return the method handle with adapted return type, if it required an unboxing conversion.
a19c14022fa4 8059443: NPE when unboxing return values
attila
parents: 27307
diff changeset
   472
     */
a19c14022fa4 8059443: NPE when unboxing return values
attila
parents: 27307
diff changeset
   473
    private static MethodHandle unboxReturnType(final MethodHandle target, final MethodType newType) {
a19c14022fa4 8059443: NPE when unboxing return values
attila
parents: 27307
diff changeset
   474
        final MethodType targetType = target.type();
a19c14022fa4 8059443: NPE when unboxing return values
attila
parents: 27307
diff changeset
   475
        final Class<?> oldReturnType = targetType.returnType();
28438
f164fc2618a0 8068573: POJO setter using [] syntax throws an exception
attila
parents: 28437
diff changeset
   476
        final Class<?> newReturnType = newType.returnType();
27360
a19c14022fa4 8059443: NPE when unboxing return values
attila
parents: 27307
diff changeset
   477
        if (TypeUtilities.isWrapperType(oldReturnType)) {
a19c14022fa4 8059443: NPE when unboxing return values
attila
parents: 27307
diff changeset
   478
            if (newReturnType.isPrimitive()) {
a19c14022fa4 8059443: NPE when unboxing return values
attila
parents: 27307
diff changeset
   479
                // The contract of setAutoConversionStrategy is such that the difference between newType and targetType
a19c14022fa4 8059443: NPE when unboxing return values
attila
parents: 27307
diff changeset
   480
                // can only be JLS method invocation conversions.
a19c14022fa4 8059443: NPE when unboxing return values
attila
parents: 27307
diff changeset
   481
                assert TypeUtilities.isMethodInvocationConvertible(oldReturnType, newReturnType);
a19c14022fa4 8059443: NPE when unboxing return values
attila
parents: 27307
diff changeset
   482
                return MethodHandles.explicitCastArguments(target, targetType.changeReturnType(newReturnType));
a19c14022fa4 8059443: NPE when unboxing return values
attila
parents: 27307
diff changeset
   483
            }
28438
f164fc2618a0 8068573: POJO setter using [] syntax throws an exception
attila
parents: 28437
diff changeset
   484
        } else if (oldReturnType == void.class && newReturnType == Object.class) {
f164fc2618a0 8068573: POJO setter using [] syntax throws an exception
attila
parents: 28437
diff changeset
   485
            return MethodHandles.filterReturnValue(target, VOID_TO_OBJECT);
27360
a19c14022fa4 8059443: NPE when unboxing return values
attila
parents: 27307
diff changeset
   486
        }
a19c14022fa4 8059443: NPE when unboxing return values
attila
parents: 27307
diff changeset
   487
        return target;
a19c14022fa4 8059443: NPE when unboxing return values
attila
parents: 27307
diff changeset
   488
    }
34979
03b189baa361 8144919: Implement missing member handler for BeansLinker
attila
parents: 34732
diff changeset
   489
03b189baa361 8144919: Implement missing member handler for BeansLinker
attila
parents: 34732
diff changeset
   490
    private static MethodHandle createMissingMemberHandler(
03b189baa361 8144919: Implement missing member handler for BeansLinker
attila
parents: 34732
diff changeset
   491
            final LinkRequest linkRequest, final LinkerServices linkerServices) throws Exception {
03b189baa361 8144919: Implement missing member handler for BeansLinker
attila
parents: 34732
diff changeset
   492
        if (BrowserJSObjectLinker.canLinkTypeStatic(linkRequest.getReceiver().getClass())) {
03b189baa361 8144919: Implement missing member handler for BeansLinker
attila
parents: 34732
diff changeset
   493
            // Don't create missing member handlers for the browser JS objects as they
03b189baa361 8144919: Implement missing member handler for BeansLinker
attila
parents: 34732
diff changeset
   494
            // have their own logic.
03b189baa361 8144919: Implement missing member handler for BeansLinker
attila
parents: 34732
diff changeset
   495
            return null;
03b189baa361 8144919: Implement missing member handler for BeansLinker
attila
parents: 34732
diff changeset
   496
        }
03b189baa361 8144919: Implement missing member handler for BeansLinker
attila
parents: 34732
diff changeset
   497
        return NashornBottomLinker.linkMissingBeanMember(linkRequest, linkerServices);
03b189baa361 8144919: Implement missing member handler for BeansLinker
attila
parents: 34732
diff changeset
   498
    }
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   499
}