nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/runtime/linker/NashornBottomLinker.java
author sundar
Tue, 16 Sep 2014 17:04:35 +0530
changeset 26649 1d7a917a35e2
parent 25865 d38d876f1654
child 28437 b9b1042592e6
permissions -rw-r--r--
8058545: With strict mode, bean property assignment of a non-existent property should result in TypeError Reviewed-by: hannesw, lagergren
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
22669
75563515567f 8032681: Issues with Nashorn
attila
parents: 21691
diff changeset
    28
import static jdk.nashorn.internal.lookup.Lookup.MH;
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    29
import static jdk.nashorn.internal.runtime.ECMAErrors.typeError;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    30
import static jdk.nashorn.internal.runtime.ScriptRuntime.UNDEFINED;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    31
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    32
import java.lang.invoke.MethodHandle;
19088
153f268bfa72 8021122: Not all callables are handled for toString and other function valued properties
sundar
parents: 16277
diff changeset
    33
import java.lang.invoke.MethodType;
153f268bfa72 8021122: Not all callables are handled for toString and other function valued properties
sundar
parents: 16277
diff changeset
    34
import java.lang.reflect.Method;
153f268bfa72 8021122: Not all callables are handled for toString and other function valued properties
sundar
parents: 16277
diff changeset
    35
import java.lang.reflect.Modifier;
22669
75563515567f 8032681: Issues with Nashorn
attila
parents: 21691
diff changeset
    36
import java.util.HashMap;
21691
8e284e9a6144 8027828: ClassCastException when converting return value of a Java method to boolean
sundar
parents: 19097
diff changeset
    37
import java.util.Map;
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents: 16188
diff changeset
    38
import jdk.internal.dynalink.CallSiteDescriptor;
16246
f60e04200be3 8008387: Improve code coverage tests for JSObjectLinker and NashornBottomLinker
sundar
parents: 16234
diff changeset
    39
import jdk.internal.dynalink.beans.BeansLinker;
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents: 16188
diff changeset
    40
import jdk.internal.dynalink.linker.GuardedInvocation;
22669
75563515567f 8032681: Issues with Nashorn
attila
parents: 21691
diff changeset
    41
import jdk.internal.dynalink.linker.GuardedTypeConversion;
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents: 16188
diff changeset
    42
import jdk.internal.dynalink.linker.GuardingDynamicLinker;
21691
8e284e9a6144 8027828: ClassCastException when converting return value of a Java method to boolean
sundar
parents: 19097
diff changeset
    43
import jdk.internal.dynalink.linker.GuardingTypeConverterFactory;
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents: 16188
diff changeset
    44
import jdk.internal.dynalink.linker.LinkRequest;
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents: 16188
diff changeset
    45
import jdk.internal.dynalink.linker.LinkerServices;
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents: 16188
diff changeset
    46
import jdk.internal.dynalink.support.Guards;
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22669
diff changeset
    47
import jdk.nashorn.internal.codegen.types.Type;
19088
153f268bfa72 8021122: Not all callables are handled for toString and other function valued properties
sundar
parents: 16277
diff changeset
    48
import jdk.nashorn.internal.runtime.Context;
21691
8e284e9a6144 8027828: ClassCastException when converting return value of a Java method to boolean
sundar
parents: 19097
diff changeset
    49
import jdk.nashorn.internal.runtime.JSType;
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    50
import jdk.nashorn.internal.runtime.ScriptRuntime;
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22669
diff changeset
    51
import jdk.nashorn.internal.runtime.UnwarrantedOptimismException;
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    52
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    53
/**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    54
 * Nashorn bottom linker; used as a last-resort catch-all linker for all linking requests that fall through all other
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    55
 * linkers (see how {@link Bootstrap} class configures the dynamic linker in its static initializer). It will throw
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    56
 * appropriate ECMAScript errors for attempts to invoke operations on {@code null}, link no-op property getters and
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    57
 * setters for Java objects that couldn't be linked by any other linker, and throw appropriate ECMAScript errors for
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    58
 * attempts to invoke arbitrary Java objects as functions or constructors.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    59
 */
21691
8e284e9a6144 8027828: ClassCastException when converting return value of a Java method to boolean
sundar
parents: 19097
diff changeset
    60
final class NashornBottomLinker implements GuardingDynamicLinker, GuardingTypeConverterFactory {
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    61
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    62
    @Override
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    63
    public GuardedInvocation getGuardedInvocation(final LinkRequest linkRequest, final LinkerServices linkerServices)
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    64
            throws Exception {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    65
        final Object self = linkRequest.getReceiver();
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    66
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    67
        if (self == null) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    68
            return linkNull(linkRequest);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    69
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    70
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    71
        // None of the objects that can be linked by NashornLinker should ever reach here. Basically, anything below
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    72
        // this point is a generic Java bean. Therefore, reaching here with a ScriptObject is a Nashorn bug.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    73
        assert isExpectedObject(self) : "Couldn't link " + linkRequest.getCallSiteDescriptor() + " for " + self.getClass().getName();
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    74
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    75
        return linkBean(linkRequest, linkerServices);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    76
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    77
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    78
    private static final MethodHandle EMPTY_PROP_GETTER =
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    79
            MH.dropArguments(MH.constant(Object.class, UNDEFINED), 0, Object.class);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    80
    private static final MethodHandle EMPTY_ELEM_GETTER =
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    81
            MH.dropArguments(EMPTY_PROP_GETTER, 0, Object.class);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    82
    private static final MethodHandle EMPTY_PROP_SETTER =
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    83
            MH.asType(EMPTY_ELEM_GETTER, EMPTY_ELEM_GETTER.type().changeReturnType(void.class));
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    84
    private static final MethodHandle EMPTY_ELEM_SETTER =
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    85
            MH.dropArguments(EMPTY_PROP_SETTER, 0, Object.class);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    86
19088
153f268bfa72 8021122: Not all callables are handled for toString and other function valued properties
sundar
parents: 16277
diff changeset
    87
    private static GuardedInvocation linkBean(final LinkRequest linkRequest, final LinkerServices linkerServices) throws Exception {
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    88
        final NashornCallSiteDescriptor desc = (NashornCallSiteDescriptor)linkRequest.getCallSiteDescriptor();
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    89
        final Object self = linkRequest.getReceiver();
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    90
        final String operator = desc.getFirstOperator();
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    91
        switch (operator) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    92
        case "new":
16246
f60e04200be3 8008387: Improve code coverage tests for JSObjectLinker and NashornBottomLinker
sundar
parents: 16234
diff changeset
    93
            if(BeansLinker.isDynamicMethod(self)) {
16256
f2d9a0c49914 8007002: Replace implicit exception throwing methods with explicit throws - simplify control flow and remove useless code
lagergren
parents: 16246
diff changeset
    94
                throw typeError("method.not.constructor", ScriptRuntime.safeToString(self));
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    95
            }
16256
f2d9a0c49914 8007002: Replace implicit exception throwing methods with explicit throws - simplify control flow and remove useless code
lagergren
parents: 16246
diff changeset
    96
            throw typeError("not.a.function", ScriptRuntime.safeToString(self));
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    97
        case "call":
19088
153f268bfa72 8021122: Not all callables are handled for toString and other function valued properties
sundar
parents: 16277
diff changeset
    98
            // Support dyn:call on any object that supports some @FunctionalInterface
153f268bfa72 8021122: Not all callables are handled for toString and other function valued properties
sundar
parents: 16277
diff changeset
    99
            // annotated interface. This way Java method, constructor references or
153f268bfa72 8021122: Not all callables are handled for toString and other function valued properties
sundar
parents: 16277
diff changeset
   100
            // implementations of java.util.function.* interfaces can be called as though
153f268bfa72 8021122: Not all callables are handled for toString and other function valued properties
sundar
parents: 16277
diff changeset
   101
            // those are script functions.
153f268bfa72 8021122: Not all callables are handled for toString and other function valued properties
sundar
parents: 16277
diff changeset
   102
            final Method m = getFunctionalInterfaceMethod(self.getClass());
153f268bfa72 8021122: Not all callables are handled for toString and other function valued properties
sundar
parents: 16277
diff changeset
   103
            if (m != null) {
153f268bfa72 8021122: Not all callables are handled for toString and other function valued properties
sundar
parents: 16277
diff changeset
   104
                final MethodType callType = desc.getMethodType();
153f268bfa72 8021122: Not all callables are handled for toString and other function valued properties
sundar
parents: 16277
diff changeset
   105
                // 'callee' and 'thiz' passed from script + actual arguments
153f268bfa72 8021122: Not all callables are handled for toString and other function valued properties
sundar
parents: 16277
diff changeset
   106
                if (callType.parameterCount() != m.getParameterCount() + 2) {
153f268bfa72 8021122: Not all callables are handled for toString and other function valued properties
sundar
parents: 16277
diff changeset
   107
                    throw typeError("no.method.matches.args", ScriptRuntime.safeToString(self));
153f268bfa72 8021122: Not all callables are handled for toString and other function valued properties
sundar
parents: 16277
diff changeset
   108
                }
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22669
diff changeset
   109
                return Bootstrap.asTypeSafeReturn(new GuardedInvocation(
19088
153f268bfa72 8021122: Not all callables are handled for toString and other function valued properties
sundar
parents: 16277
diff changeset
   110
                        // drop 'thiz' passed from the script.
153f268bfa72 8021122: Not all callables are handled for toString and other function valued properties
sundar
parents: 16277
diff changeset
   111
                        MH.dropArguments(desc.getLookup().unreflect(m), 1, callType.parameterType(1)),
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22669
diff changeset
   112
                        Guards.getInstanceOfGuard(m.getDeclaringClass())), linkerServices, desc);
19088
153f268bfa72 8021122: Not all callables are handled for toString and other function valued properties
sundar
parents: 16277
diff changeset
   113
            }
25252
e8bfc909db53 8043232: Index selection of overloaded java new constructors
sundar
parents: 24778
diff changeset
   114
            if(BeansLinker.isDynamicConstructor(self)) {
e8bfc909db53 8043232: Index selection of overloaded java new constructors
sundar
parents: 24778
diff changeset
   115
                throw typeError("constructor.requires.new", ScriptRuntime.safeToString(self));
e8bfc909db53 8043232: Index selection of overloaded java new constructors
sundar
parents: 24778
diff changeset
   116
            }
16246
f60e04200be3 8008387: Improve code coverage tests for JSObjectLinker and NashornBottomLinker
sundar
parents: 16234
diff changeset
   117
            if(BeansLinker.isDynamicMethod(self)) {
16256
f2d9a0c49914 8007002: Replace implicit exception throwing methods with explicit throws - simplify control flow and remove useless code
lagergren
parents: 16246
diff changeset
   118
                throw typeError("no.method.matches.args", ScriptRuntime.safeToString(self));
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   119
            }
16256
f2d9a0c49914 8007002: Replace implicit exception throwing methods with explicit throws - simplify control flow and remove useless code
lagergren
parents: 16246
diff changeset
   120
            throw typeError("not.a.function", ScriptRuntime.safeToString(self));
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   121
        case "callMethod":
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   122
        case "getMethod":
16256
f2d9a0c49914 8007002: Replace implicit exception throwing methods with explicit throws - simplify control flow and remove useless code
lagergren
parents: 16246
diff changeset
   123
            throw typeError("no.such.function", getArgument(linkRequest), ScriptRuntime.safeToString(self));
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   124
        case "getProp":
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   125
        case "getElem":
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22669
diff changeset
   126
            if(NashornCallSiteDescriptor.isOptimistic(desc)) {
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22669
diff changeset
   127
                throw new UnwarrantedOptimismException(UNDEFINED, NashornCallSiteDescriptor.getProgramPoint(desc), Type.OBJECT);
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22669
diff changeset
   128
            }
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   129
            if (desc.getOperand() != null) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   130
                return getInvocation(EMPTY_PROP_GETTER, self, linkerServices, desc);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   131
            }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   132
            return getInvocation(EMPTY_ELEM_GETTER, self, linkerServices, desc);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   133
        case "setProp":
26649
1d7a917a35e2 8058545: With strict mode, bean property assignment of a non-existent property should result in TypeError
sundar
parents: 25865
diff changeset
   134
        case "setElem": {
1d7a917a35e2 8058545: With strict mode, bean property assignment of a non-existent property should result in TypeError
sundar
parents: 25865
diff changeset
   135
            final boolean strict = NashornCallSiteDescriptor.isStrict(desc);
1d7a917a35e2 8058545: With strict mode, bean property assignment of a non-existent property should result in TypeError
sundar
parents: 25865
diff changeset
   136
            if (strict) {
1d7a917a35e2 8058545: With strict mode, bean property assignment of a non-existent property should result in TypeError
sundar
parents: 25865
diff changeset
   137
                throw typeError("cant.set.property", getArgument(linkRequest), ScriptRuntime.safeToString(self));
1d7a917a35e2 8058545: With strict mode, bean property assignment of a non-existent property should result in TypeError
sundar
parents: 25865
diff changeset
   138
            }
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   139
            if (desc.getOperand() != null) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   140
                return getInvocation(EMPTY_PROP_SETTER, self, linkerServices, desc);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   141
            }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   142
            return getInvocation(EMPTY_ELEM_SETTER, self, linkerServices, desc);
26649
1d7a917a35e2 8058545: With strict mode, bean property assignment of a non-existent property should result in TypeError
sundar
parents: 25865
diff changeset
   143
        }
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   144
        default:
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   145
            break;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   146
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   147
        throw new AssertionError("unknown call type " + desc);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   148
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   149
21691
8e284e9a6144 8027828: ClassCastException when converting return value of a Java method to boolean
sundar
parents: 19097
diff changeset
   150
    @Override
22669
75563515567f 8032681: Issues with Nashorn
attila
parents: 21691
diff changeset
   151
    public GuardedTypeConversion convertToType(final Class<?> sourceType, final Class<?> targetType) throws Exception {
21691
8e284e9a6144 8027828: ClassCastException when converting return value of a Java method to boolean
sundar
parents: 19097
diff changeset
   152
        final GuardedInvocation gi = convertToTypeNoCast(sourceType, targetType);
22669
75563515567f 8032681: Issues with Nashorn
attila
parents: 21691
diff changeset
   153
        return gi == null ? null : new GuardedTypeConversion(gi.asType(MH.type(targetType, sourceType)), true);
21691
8e284e9a6144 8027828: ClassCastException when converting return value of a Java method to boolean
sundar
parents: 19097
diff changeset
   154
    }
8e284e9a6144 8027828: ClassCastException when converting return value of a Java method to boolean
sundar
parents: 19097
diff changeset
   155
8e284e9a6144 8027828: ClassCastException when converting return value of a Java method to boolean
sundar
parents: 19097
diff changeset
   156
    /**
8e284e9a6144 8027828: ClassCastException when converting return value of a Java method to boolean
sundar
parents: 19097
diff changeset
   157
     * Main part of the implementation of {@link GuardingTypeConverterFactory#convertToType(Class, Class)} that doesn't
8e284e9a6144 8027828: ClassCastException when converting return value of a Java method to boolean
sundar
parents: 19097
diff changeset
   158
     * care about adapting the method signature; that's done by the invoking method. Returns conversion from Object to String/number/boolean (JS primitive types).
8e284e9a6144 8027828: ClassCastException when converting return value of a Java method to boolean
sundar
parents: 19097
diff changeset
   159
     * @param sourceType the source type
8e284e9a6144 8027828: ClassCastException when converting return value of a Java method to boolean
sundar
parents: 19097
diff changeset
   160
     * @param targetType the target type
8e284e9a6144 8027828: ClassCastException when converting return value of a Java method to boolean
sundar
parents: 19097
diff changeset
   161
     * @return a guarded invocation that converts from the source type to the target type.
8e284e9a6144 8027828: ClassCastException when converting return value of a Java method to boolean
sundar
parents: 19097
diff changeset
   162
     * @throws Exception if something goes wrong
8e284e9a6144 8027828: ClassCastException when converting return value of a Java method to boolean
sundar
parents: 19097
diff changeset
   163
     */
8e284e9a6144 8027828: ClassCastException when converting return value of a Java method to boolean
sundar
parents: 19097
diff changeset
   164
    private static GuardedInvocation convertToTypeNoCast(final Class<?> sourceType, final Class<?> targetType) throws Exception {
8e284e9a6144 8027828: ClassCastException when converting return value of a Java method to boolean
sundar
parents: 19097
diff changeset
   165
        final MethodHandle mh = CONVERTERS.get(targetType);
8e284e9a6144 8027828: ClassCastException when converting return value of a Java method to boolean
sundar
parents: 19097
diff changeset
   166
        if (mh != null) {
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22669
diff changeset
   167
            return new GuardedInvocation(mh);
21691
8e284e9a6144 8027828: ClassCastException when converting return value of a Java method to boolean
sundar
parents: 19097
diff changeset
   168
        }
8e284e9a6144 8027828: ClassCastException when converting return value of a Java method to boolean
sundar
parents: 19097
diff changeset
   169
8e284e9a6144 8027828: ClassCastException when converting return value of a Java method to boolean
sundar
parents: 19097
diff changeset
   170
        return null;
8e284e9a6144 8027828: ClassCastException when converting return value of a Java method to boolean
sundar
parents: 19097
diff changeset
   171
    }
8e284e9a6144 8027828: ClassCastException when converting return value of a Java method to boolean
sundar
parents: 19097
diff changeset
   172
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   173
    private static GuardedInvocation getInvocation(final MethodHandle handle, final Object self, final LinkerServices linkerServices, final CallSiteDescriptor desc) {
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22669
diff changeset
   174
        return Bootstrap.asTypeSafeReturn(new GuardedInvocation(handle, Guards.getClassGuard(self.getClass())), linkerServices, desc);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   175
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   176
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   177
    // Used solely in an assertion to figure out if the object we get here is something we in fact expect. Objects
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   178
    // linked by NashornLinker should never reach here.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   179
    private static boolean isExpectedObject(final Object obj) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   180
        return !(NashornLinker.canLinkTypeStatic(obj.getClass()));
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   181
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   182
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   183
    private static GuardedInvocation linkNull(final LinkRequest linkRequest) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   184
        final NashornCallSiteDescriptor desc = (NashornCallSiteDescriptor)linkRequest.getCallSiteDescriptor();
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   185
        final String operator = desc.getFirstOperator();
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   186
        switch (operator) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   187
        case "new":
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   188
        case "call":
16256
f2d9a0c49914 8007002: Replace implicit exception throwing methods with explicit throws - simplify control flow and remove useless code
lagergren
parents: 16246
diff changeset
   189
            throw typeError("not.a.function", "null");
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   190
        case "callMethod":
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   191
        case "getMethod":
16256
f2d9a0c49914 8007002: Replace implicit exception throwing methods with explicit throws - simplify control flow and remove useless code
lagergren
parents: 16246
diff changeset
   192
            throw typeError("no.such.function", getArgument(linkRequest), "null");
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   193
        case "getProp":
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   194
        case "getElem":
16256
f2d9a0c49914 8007002: Replace implicit exception throwing methods with explicit throws - simplify control flow and remove useless code
lagergren
parents: 16246
diff changeset
   195
            throw typeError("cant.get.property", getArgument(linkRequest), "null");
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   196
        case "setProp":
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   197
        case "setElem":
16256
f2d9a0c49914 8007002: Replace implicit exception throwing methods with explicit throws - simplify control flow and remove useless code
lagergren
parents: 16246
diff changeset
   198
            throw typeError("cant.set.property", getArgument(linkRequest), "null");
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   199
        default:
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   200
            break;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   201
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   202
        throw new AssertionError("unknown call type " + desc);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   203
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   204
21691
8e284e9a6144 8027828: ClassCastException when converting return value of a Java method to boolean
sundar
parents: 19097
diff changeset
   205
    private static final Map<Class<?>, MethodHandle> CONVERTERS = new HashMap<>();
8e284e9a6144 8027828: ClassCastException when converting return value of a Java method to boolean
sundar
parents: 19097
diff changeset
   206
    static {
8e284e9a6144 8027828: ClassCastException when converting return value of a Java method to boolean
sundar
parents: 19097
diff changeset
   207
        CONVERTERS.put(boolean.class, JSType.TO_BOOLEAN.methodHandle());
8e284e9a6144 8027828: ClassCastException when converting return value of a Java method to boolean
sundar
parents: 19097
diff changeset
   208
        CONVERTERS.put(double.class, JSType.TO_NUMBER.methodHandle());
8e284e9a6144 8027828: ClassCastException when converting return value of a Java method to boolean
sundar
parents: 19097
diff changeset
   209
        CONVERTERS.put(int.class, JSType.TO_INTEGER.methodHandle());
8e284e9a6144 8027828: ClassCastException when converting return value of a Java method to boolean
sundar
parents: 19097
diff changeset
   210
        CONVERTERS.put(long.class, JSType.TO_LONG.methodHandle());
8e284e9a6144 8027828: ClassCastException when converting return value of a Java method to boolean
sundar
parents: 19097
diff changeset
   211
        CONVERTERS.put(String.class, JSType.TO_STRING.methodHandle());
8e284e9a6144 8027828: ClassCastException when converting return value of a Java method to boolean
sundar
parents: 19097
diff changeset
   212
    }
8e284e9a6144 8027828: ClassCastException when converting return value of a Java method to boolean
sundar
parents: 19097
diff changeset
   213
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   214
    private static String getArgument(final LinkRequest linkRequest) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   215
        final CallSiteDescriptor desc = linkRequest.getCallSiteDescriptor();
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   216
        if (desc.getNameTokenCount() > 2) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   217
            return desc.getNameToken(2);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   218
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   219
        return ScriptRuntime.safeToString(linkRequest.getArguments()[1]);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   220
    }
19088
153f268bfa72 8021122: Not all callables are handled for toString and other function valued properties
sundar
parents: 16277
diff changeset
   221
19097
f544a2ea40ef 8021262: Make nashorn access checks consistent with underlying dynalink
sundar
parents: 19088
diff changeset
   222
    // cache of @FunctionalInterface method of implementor classes
f544a2ea40ef 8021262: Make nashorn access checks consistent with underlying dynalink
sundar
parents: 19088
diff changeset
   223
    private static final ClassValue<Method> FUNCTIONAL_IFACE_METHOD = new ClassValue<Method>() {
f544a2ea40ef 8021262: Make nashorn access checks consistent with underlying dynalink
sundar
parents: 19088
diff changeset
   224
        @Override
f544a2ea40ef 8021262: Make nashorn access checks consistent with underlying dynalink
sundar
parents: 19088
diff changeset
   225
        protected Method computeValue(final Class<?> type) {
f544a2ea40ef 8021262: Make nashorn access checks consistent with underlying dynalink
sundar
parents: 19088
diff changeset
   226
            return findFunctionalInterfaceMethod(type);
19088
153f268bfa72 8021122: Not all callables are handled for toString and other function valued properties
sundar
parents: 16277
diff changeset
   227
        }
153f268bfa72 8021122: Not all callables are handled for toString and other function valued properties
sundar
parents: 16277
diff changeset
   228
19097
f544a2ea40ef 8021262: Make nashorn access checks consistent with underlying dynalink
sundar
parents: 19088
diff changeset
   229
        private Method findFunctionalInterfaceMethod(final Class<?> clazz) {
f544a2ea40ef 8021262: Make nashorn access checks consistent with underlying dynalink
sundar
parents: 19088
diff changeset
   230
            if (clazz == null) {
f544a2ea40ef 8021262: Make nashorn access checks consistent with underlying dynalink
sundar
parents: 19088
diff changeset
   231
                return null;
19088
153f268bfa72 8021122: Not all callables are handled for toString and other function valued properties
sundar
parents: 16277
diff changeset
   232
            }
153f268bfa72 8021122: Not all callables are handled for toString and other function valued properties
sundar
parents: 16277
diff changeset
   233
24778
2ff5d7041566 8044638: Tidy up Nashorn codebase for code standards
attila
parents: 24719
diff changeset
   234
            for (final Class<?> iface : clazz.getInterfaces()) {
19097
f544a2ea40ef 8021262: Make nashorn access checks consistent with underlying dynalink
sundar
parents: 19088
diff changeset
   235
                // check accessiblity up-front
f544a2ea40ef 8021262: Make nashorn access checks consistent with underlying dynalink
sundar
parents: 19088
diff changeset
   236
                if (! Context.isAccessibleClass(iface)) {
f544a2ea40ef 8021262: Make nashorn access checks consistent with underlying dynalink
sundar
parents: 19088
diff changeset
   237
                    continue;
f544a2ea40ef 8021262: Make nashorn access checks consistent with underlying dynalink
sundar
parents: 19088
diff changeset
   238
                }
f544a2ea40ef 8021262: Make nashorn access checks consistent with underlying dynalink
sundar
parents: 19088
diff changeset
   239
f544a2ea40ef 8021262: Make nashorn access checks consistent with underlying dynalink
sundar
parents: 19088
diff changeset
   240
                // check for @FunctionalInterface
f544a2ea40ef 8021262: Make nashorn access checks consistent with underlying dynalink
sundar
parents: 19088
diff changeset
   241
                if (iface.isAnnotationPresent(FunctionalInterface.class)) {
f544a2ea40ef 8021262: Make nashorn access checks consistent with underlying dynalink
sundar
parents: 19088
diff changeset
   242
                    // return the first abstract method
f544a2ea40ef 8021262: Make nashorn access checks consistent with underlying dynalink
sundar
parents: 19088
diff changeset
   243
                    for (final Method m : iface.getMethods()) {
f544a2ea40ef 8021262: Make nashorn access checks consistent with underlying dynalink
sundar
parents: 19088
diff changeset
   244
                        if (Modifier.isAbstract(m.getModifiers())) {
f544a2ea40ef 8021262: Make nashorn access checks consistent with underlying dynalink
sundar
parents: 19088
diff changeset
   245
                            return m;
f544a2ea40ef 8021262: Make nashorn access checks consistent with underlying dynalink
sundar
parents: 19088
diff changeset
   246
                        }
19088
153f268bfa72 8021122: Not all callables are handled for toString and other function valued properties
sundar
parents: 16277
diff changeset
   247
                    }
153f268bfa72 8021122: Not all callables are handled for toString and other function valued properties
sundar
parents: 16277
diff changeset
   248
                }
153f268bfa72 8021122: Not all callables are handled for toString and other function valued properties
sundar
parents: 16277
diff changeset
   249
            }
153f268bfa72 8021122: Not all callables are handled for toString and other function valued properties
sundar
parents: 16277
diff changeset
   250
19097
f544a2ea40ef 8021262: Make nashorn access checks consistent with underlying dynalink
sundar
parents: 19088
diff changeset
   251
            // did not find here, try super class
f544a2ea40ef 8021262: Make nashorn access checks consistent with underlying dynalink
sundar
parents: 19088
diff changeset
   252
            return findFunctionalInterfaceMethod(clazz.getSuperclass());
f544a2ea40ef 8021262: Make nashorn access checks consistent with underlying dynalink
sundar
parents: 19088
diff changeset
   253
        }
f544a2ea40ef 8021262: Make nashorn access checks consistent with underlying dynalink
sundar
parents: 19088
diff changeset
   254
    };
f544a2ea40ef 8021262: Make nashorn access checks consistent with underlying dynalink
sundar
parents: 19088
diff changeset
   255
f544a2ea40ef 8021262: Make nashorn access checks consistent with underlying dynalink
sundar
parents: 19088
diff changeset
   256
    // Returns @FunctionalInterface annotated interface's single abstract
f544a2ea40ef 8021262: Make nashorn access checks consistent with underlying dynalink
sundar
parents: 19088
diff changeset
   257
    // method. If not found, returns null.
f544a2ea40ef 8021262: Make nashorn access checks consistent with underlying dynalink
sundar
parents: 19088
diff changeset
   258
    static Method getFunctionalInterfaceMethod(final Class<?> clazz) {
f544a2ea40ef 8021262: Make nashorn access checks consistent with underlying dynalink
sundar
parents: 19088
diff changeset
   259
        return FUNCTIONAL_IFACE_METHOD.get(clazz);
19088
153f268bfa72 8021122: Not all callables are handled for toString and other function valued properties
sundar
parents: 16277
diff changeset
   260
    }
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   261
}