src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/runtime/OptimisticReturnFilters.java
author hannesw
Wed, 21 Mar 2018 16:55:34 +0100
changeset 49275 c639a6b33c5c
parent 47216 71c04702a3d5
permissions -rw-r--r--
8199869: Missing copyright headers in nashorn source code Reviewed-by: sundar
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
     1
/*
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
     2
 * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
     4
 *
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
    10
 *
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
    15
 * accompanied this code).
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
    16
 *
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
    20
 *
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
    23
 * questions.
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
    24
 */
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
    25
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
    26
package jdk.nashorn.internal.runtime;
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
    27
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
    28
import static jdk.nashorn.internal.lookup.Lookup.MH;
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
    29
import static jdk.nashorn.internal.runtime.JSType.getAccessorTypeIndex;
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
    30
import static jdk.nashorn.internal.runtime.UnwarrantedOptimismException.isValid;
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
    31
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
    32
import java.lang.invoke.MethodHandle;
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
    33
import java.lang.invoke.MethodHandles;
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
    34
import java.lang.invoke.MethodType;
34447
ec4c069f9436 8141338: Move jdk.internal.dynalink package to jdk.dynalink
attila
parents: 33337
diff changeset
    35
import jdk.dynalink.CallSiteDescriptor;
ec4c069f9436 8141338: Move jdk.internal.dynalink package to jdk.dynalink
attila
parents: 33337
diff changeset
    36
import jdk.dynalink.linker.GuardedInvocation;
ec4c069f9436 8141338: Move jdk.internal.dynalink package to jdk.dynalink
attila
parents: 33337
diff changeset
    37
import jdk.dynalink.linker.support.TypeUtilities;
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
    38
import jdk.nashorn.internal.codegen.types.Type;
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
    39
import jdk.nashorn.internal.runtime.linker.NashornCallSiteDescriptor;
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
    40
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
    41
/**
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
    42
 * Optimistic return value filters
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
    43
 */
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
    44
public final class OptimisticReturnFilters {
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
    45
    private static final MethodHandle[] ENSURE_INT;
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
    46
    private static final MethodHandle[] ENSURE_NUMBER;
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
    47
34732
6605efbe8447 8144020: Remove long as an internal numeric type
hannesw
parents: 34447
diff changeset
    48
    // These extend the type index constants in JSType
6605efbe8447 8144020: Remove long as an internal numeric type
hannesw
parents: 34447
diff changeset
    49
    private static final int VOID_TYPE_INDEX;
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
    50
    private static final int BOOLEAN_TYPE_INDEX;
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
    51
    private static final int CHAR_TYPE_INDEX;
34732
6605efbe8447 8144020: Remove long as an internal numeric type
hannesw
parents: 34447
diff changeset
    52
    private static final int LONG_TYPE_INDEX;
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
    53
    private static final int FLOAT_TYPE_INDEX;
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
    54
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
    55
    static {
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
    56
        final MethodHandle INT_DOUBLE = findOwnMH("ensureInt", int.class, double.class, int.class);
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
    57
        ENSURE_INT = new MethodHandle[] {
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
    58
                null,
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
    59
                INT_DOUBLE,
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
    60
                findOwnMH("ensureInt", int.class, Object.class, int.class),
24751
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24719
diff changeset
    61
                findOwnMH("ensureInt", int.class, int.class),
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
    62
                findOwnMH("ensureInt", int.class, boolean.class, int.class),
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
    63
                findOwnMH("ensureInt", int.class, char.class, int.class),
34732
6605efbe8447 8144020: Remove long as an internal numeric type
hannesw
parents: 34447
diff changeset
    64
                findOwnMH("ensureInt", int.class, long.class, int.class),
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
    65
                INT_DOUBLE.asType(INT_DOUBLE.type().changeParameterType(0, float.class)),
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
    66
        };
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
    67
34732
6605efbe8447 8144020: Remove long as an internal numeric type
hannesw
parents: 34447
diff changeset
    68
        VOID_TYPE_INDEX = ENSURE_INT.length - 5;
6605efbe8447 8144020: Remove long as an internal numeric type
hannesw
parents: 34447
diff changeset
    69
        BOOLEAN_TYPE_INDEX = ENSURE_INT.length - 4;
6605efbe8447 8144020: Remove long as an internal numeric type
hannesw
parents: 34447
diff changeset
    70
        CHAR_TYPE_INDEX = ENSURE_INT.length - 3;
6605efbe8447 8144020: Remove long as an internal numeric type
hannesw
parents: 34447
diff changeset
    71
        LONG_TYPE_INDEX = ENSURE_INT.length - 2;
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
    72
        FLOAT_TYPE_INDEX = ENSURE_INT.length - 1;
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
    73
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
    74
        ENSURE_NUMBER = new MethodHandle[] {
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
    75
                null,
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
    76
                null,
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
    77
                findOwnMH("ensureNumber", double.class, Object.class, int.class),
24751
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24719
diff changeset
    78
                ENSURE_INT[VOID_TYPE_INDEX].asType(ENSURE_INT[VOID_TYPE_INDEX].type().changeReturnType(double.class)),
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
    79
                ENSURE_INT[BOOLEAN_TYPE_INDEX].asType(ENSURE_INT[BOOLEAN_TYPE_INDEX].type().changeReturnType(double.class)),
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
    80
                ENSURE_INT[CHAR_TYPE_INDEX].asType(ENSURE_INT[CHAR_TYPE_INDEX].type().changeReturnType(double.class)),
34732
6605efbe8447 8144020: Remove long as an internal numeric type
hannesw
parents: 34447
diff changeset
    81
                findOwnMH("ensureNumber", double.class, long.class, int.class),
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
    82
                null
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
    83
        };
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
    84
    }
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
    85
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
    86
    /**
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
    87
     * Given a method handle and an expected return type, perform return value filtering
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
    88
     * according to the optimistic type coercion rules
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
    89
     * @param mh method handle
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
    90
     * @param expectedReturnType expected return type
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
    91
     * @param programPoint program point
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
    92
     * @return filtered method
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
    93
     */
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
    94
    public static MethodHandle filterOptimisticReturnValue(final MethodHandle mh, final Class<?> expectedReturnType, final int programPoint) {
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
    95
        if(!isValid(programPoint)) {
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
    96
            return mh;
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
    97
        }
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
    98
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
    99
        final MethodType type = mh.type();
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
   100
        final Class<?> actualReturnType = type.returnType();
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
   101
        if(TypeUtilities.isConvertibleWithoutLoss(actualReturnType, expectedReturnType)) {
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
   102
            return mh;
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
   103
        }
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
   104
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
   105
        final MethodHandle guard = getOptimisticTypeGuard(expectedReturnType, actualReturnType);
24751
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24719
diff changeset
   106
        return guard == null ? mh : MH.filterReturnValue(mh, MH.insertArguments(guard, guard.type().parameterCount() - 1, programPoint));
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
   107
    }
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
   108
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
   109
    /**
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
   110
     * Given a guarded invocation and a callsite descriptor, perform return value filtering
24751
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24719
diff changeset
   111
     * according to the optimistic type coercion rules, using the return value from the descriptor
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
   112
     * @param inv the invocation
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
   113
     * @param desc the descriptor
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
   114
     * @return filtered invocation
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
   115
     */
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
   116
    public static GuardedInvocation filterOptimisticReturnValue(final GuardedInvocation inv, final CallSiteDescriptor desc) {
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
   117
        if(!NashornCallSiteDescriptor.isOptimistic(desc)) {
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
   118
            return inv;
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
   119
        }
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
   120
        return inv.replaceMethods(filterOptimisticReturnValue(inv.getInvocation(), desc.getMethodType().returnType(),
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
   121
                NashornCallSiteDescriptor.getProgramPoint(desc)), inv.getGuard());
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
   122
    }
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
   123
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
   124
    private static MethodHandle getOptimisticTypeGuard(final Class<?> actual, final Class<?> provable) {
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
   125
        final MethodHandle guard;
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
   126
        final int provableTypeIndex = getProvableTypeIndex(provable);
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
   127
        if (actual == int.class) {
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
   128
            guard = ENSURE_INT[provableTypeIndex];
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
   129
        } else if (actual == double.class) {
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
   130
            guard = ENSURE_NUMBER[provableTypeIndex];
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
   131
        } else {
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
   132
            guard = null;
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
   133
            assert !actual.isPrimitive() : actual + ", " + provable;
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
   134
        }
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
   135
        if(guard != null && !(provable.isPrimitive())) {
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
   136
            // Make sure filtering a MethodHandle(...)String works with a filter MethodHandle(Object, int)... Note that
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
   137
            // if the return type of the method is incompatible with Number, then the guard will always throw an
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
   138
            // UnwarrantedOperationException when invoked, but we must link it anyway as we need the guarded function to
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
   139
            // successfully execute and return the non-convertible return value that it'll put into the thrown
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
   140
            // UnwarrantedOptimismException.
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
   141
            return guard.asType(guard.type().changeParameterType(0, provable));
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
   142
        }
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
   143
        return guard;
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
   144
    }
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
   145
24778
2ff5d7041566 8044638: Tidy up Nashorn codebase for code standards
attila
parents: 24751
diff changeset
   146
    private static int getProvableTypeIndex(final Class<?> provable) {
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
   147
        final int accTypeIndex = getAccessorTypeIndex(provable);
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
   148
        if(accTypeIndex != -1) {
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
   149
            return accTypeIndex;
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
   150
        } else if(provable == boolean.class) {
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
   151
            return BOOLEAN_TYPE_INDEX;
24751
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24719
diff changeset
   152
        } else if(provable == void.class) {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24719
diff changeset
   153
            return VOID_TYPE_INDEX;
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
   154
        } else if(provable == byte.class || provable == short.class) {
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
   155
            return 0; // never needs a guard, as it's assignable to int
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
   156
        } else if(provable == char.class) {
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
   157
            return CHAR_TYPE_INDEX;
34732
6605efbe8447 8144020: Remove long as an internal numeric type
hannesw
parents: 34447
diff changeset
   158
        } else if(provable == long.class) {
6605efbe8447 8144020: Remove long as an internal numeric type
hannesw
parents: 34447
diff changeset
   159
            return LONG_TYPE_INDEX;
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
   160
        } else if(provable == float.class) {
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
   161
            return FLOAT_TYPE_INDEX;
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
   162
        }
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
   163
        throw new AssertionError(provable.getName());
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
   164
    }
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
   165
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
   166
    //maps staticallyProvableCallSiteType to actualCallSiteType, throws exception if impossible
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
   167
    @SuppressWarnings("unused")
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
   168
    private static int ensureInt(final long arg, final int programPoint) {
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
   169
        if (JSType.isRepresentableAsInt(arg)) {
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
   170
            return (int)arg;
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
   171
        }
34732
6605efbe8447 8144020: Remove long as an internal numeric type
hannesw
parents: 34447
diff changeset
   172
        throw UnwarrantedOptimismException.createNarrowest(arg, programPoint);
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
   173
    }
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
   174
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
   175
    @SuppressWarnings("unused")
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
   176
    private static int ensureInt(final double arg, final int programPoint) {
31490
77f783445d40 8130234: Get rid of JSType.isNegativeZero
attila
parents: 26378
diff changeset
   177
        if (JSType.isStrictlyRepresentableAsInt(arg)) {
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
   178
            return (int)arg;
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
   179
        }
34732
6605efbe8447 8144020: Remove long as an internal numeric type
hannesw
parents: 34447
diff changeset
   180
        throw new UnwarrantedOptimismException(arg, programPoint, Type.NUMBER);
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
   181
    }
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
   182
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
   183
    /**
26378
ca7093bd5fd1 8056129: AtomicInteger is treated as primitive number with optimistic compilation
attila
parents: 25865
diff changeset
   184
     * Returns the argument value as an int. If the argument is not a wrapper for a primitive numeric type
ca7093bd5fd1 8056129: AtomicInteger is treated as primitive number with optimistic compilation
attila
parents: 25865
diff changeset
   185
     * with a value that can be exactly represented as an int, throw an {@link UnwarrantedOptimismException}.
ca7093bd5fd1 8056129: AtomicInteger is treated as primitive number with optimistic compilation
attila
parents: 25865
diff changeset
   186
     * This method is only public so that generated script code can use it. See {code CodeGenerator.ENSURE_INT}.
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
   187
     * @param arg the original argument.
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
   188
     * @param programPoint the program point used in the exception
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
   189
     * @return the value of the argument as an int.
26378
ca7093bd5fd1 8056129: AtomicInteger is treated as primitive number with optimistic compilation
attila
parents: 25865
diff changeset
   190
     * @throws UnwarrantedOptimismException if the argument is not a wrapper for a primitive numeric type with
ca7093bd5fd1 8056129: AtomicInteger is treated as primitive number with optimistic compilation
attila
parents: 25865
diff changeset
   191
     * a value that can be exactly represented as an int.
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
   192
     */
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
   193
    public static int ensureInt(final Object arg, final int programPoint) {
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
   194
        // NOTE: this doesn't delegate to ensureInt(double, int) as in that case if arg were a Long, it would throw a
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
   195
        // (potentially imprecise) Double in the UnwarrantedOptimismException. This way, it will put the correct valued
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
   196
        // Long into the exception.
26378
ca7093bd5fd1 8056129: AtomicInteger is treated as primitive number with optimistic compilation
attila
parents: 25865
diff changeset
   197
        if (isPrimitiveNumberWrapper(arg)) {
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
   198
            final double d = ((Number)arg).doubleValue();
31490
77f783445d40 8130234: Get rid of JSType.isNegativeZero
attila
parents: 26378
diff changeset
   199
            if (JSType.isStrictlyRepresentableAsInt(d)) {
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
   200
                return (int)d;
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
   201
            }
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
   202
        }
34732
6605efbe8447 8144020: Remove long as an internal numeric type
hannesw
parents: 34447
diff changeset
   203
        throw UnwarrantedOptimismException.createNarrowest(arg, programPoint);
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
   204
    }
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
   205
26378
ca7093bd5fd1 8056129: AtomicInteger is treated as primitive number with optimistic compilation
attila
parents: 25865
diff changeset
   206
    private static boolean isPrimitiveNumberWrapper(final Object obj) {
ca7093bd5fd1 8056129: AtomicInteger is treated as primitive number with optimistic compilation
attila
parents: 25865
diff changeset
   207
        if (obj == null) {
ca7093bd5fd1 8056129: AtomicInteger is treated as primitive number with optimistic compilation
attila
parents: 25865
diff changeset
   208
            return false;
ca7093bd5fd1 8056129: AtomicInteger is treated as primitive number with optimistic compilation
attila
parents: 25865
diff changeset
   209
        }
ca7093bd5fd1 8056129: AtomicInteger is treated as primitive number with optimistic compilation
attila
parents: 25865
diff changeset
   210
        final Class<?> c = obj.getClass();
ca7093bd5fd1 8056129: AtomicInteger is treated as primitive number with optimistic compilation
attila
parents: 25865
diff changeset
   211
        return c == Integer.class || c == Double.class || c == Long.class ||
ca7093bd5fd1 8056129: AtomicInteger is treated as primitive number with optimistic compilation
attila
parents: 25865
diff changeset
   212
               c ==   Float.class || c ==  Short.class || c == Byte.class;
ca7093bd5fd1 8056129: AtomicInteger is treated as primitive number with optimistic compilation
attila
parents: 25865
diff changeset
   213
    }
ca7093bd5fd1 8056129: AtomicInteger is treated as primitive number with optimistic compilation
attila
parents: 25865
diff changeset
   214
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
   215
    @SuppressWarnings("unused")
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
   216
    private static int ensureInt(final boolean arg, final int programPoint) {
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
   217
        throw new UnwarrantedOptimismException(arg, programPoint, Type.OBJECT);
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
   218
    }
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
   219
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
   220
    @SuppressWarnings("unused")
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
   221
    private static int ensureInt(final char arg, final int programPoint) {
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
   222
        throw new UnwarrantedOptimismException(arg, programPoint, Type.OBJECT);
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
   223
    }
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
   224
24751
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24719
diff changeset
   225
    @SuppressWarnings("unused")
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24719
diff changeset
   226
    private static int ensureInt(final int programPoint) {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24719
diff changeset
   227
        // Turns a void into UNDEFINED
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24719
diff changeset
   228
        throw new UnwarrantedOptimismException(ScriptRuntime.UNDEFINED, programPoint, Type.OBJECT);
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24719
diff changeset
   229
    }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24719
diff changeset
   230
34732
6605efbe8447 8144020: Remove long as an internal numeric type
hannesw
parents: 34447
diff changeset
   231
6605efbe8447 8144020: Remove long as an internal numeric type
hannesw
parents: 34447
diff changeset
   232
    @SuppressWarnings("unused")
6605efbe8447 8144020: Remove long as an internal numeric type
hannesw
parents: 34447
diff changeset
   233
    private static double ensureNumber(final long arg, final int programPoint) {
6605efbe8447 8144020: Remove long as an internal numeric type
hannesw
parents: 34447
diff changeset
   234
        if (JSType.isRepresentableAsDouble(arg)) {
6605efbe8447 8144020: Remove long as an internal numeric type
hannesw
parents: 34447
diff changeset
   235
            return (double) arg;
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
   236
        }
34732
6605efbe8447 8144020: Remove long as an internal numeric type
hannesw
parents: 34447
diff changeset
   237
        throw new UnwarrantedOptimismException(arg, programPoint, Type.OBJECT);
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
   238
    }
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
   239
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
   240
    /**
34732
6605efbe8447 8144020: Remove long as an internal numeric type
hannesw
parents: 34447
diff changeset
   241
     * Returns the argument value as a double. If the argument is not a wrapper for a primitive numeric type
6605efbe8447 8144020: Remove long as an internal numeric type
hannesw
parents: 34447
diff changeset
   242
     * that can be represented as double throw an {@link UnwarrantedOptimismException}.
6605efbe8447 8144020: Remove long as an internal numeric type
hannesw
parents: 34447
diff changeset
   243
     * This method is only public so that generated script code can use it. See {code CodeGenerator.ENSURE_NUMBER}.
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
   244
     * @param arg the original argument.
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
   245
     * @param programPoint the program point used in the exception
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
   246
     * @return the value of the argument as a double.
26378
ca7093bd5fd1 8056129: AtomicInteger is treated as primitive number with optimistic compilation
attila
parents: 25865
diff changeset
   247
     * @throws UnwarrantedOptimismException if the argument is not a wrapper for a primitive numeric type.
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
   248
     */
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
   249
    public static double ensureNumber(final Object arg, final int programPoint) {
34732
6605efbe8447 8144020: Remove long as an internal numeric type
hannesw
parents: 34447
diff changeset
   250
        if (isPrimitiveNumberWrapper(arg)
6605efbe8447 8144020: Remove long as an internal numeric type
hannesw
parents: 34447
diff changeset
   251
                && (arg.getClass() != Long.class || JSType.isRepresentableAsDouble((Long) arg))) {
6605efbe8447 8144020: Remove long as an internal numeric type
hannesw
parents: 34447
diff changeset
   252
            return ((Number) arg).doubleValue();
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
   253
        }
34732
6605efbe8447 8144020: Remove long as an internal numeric type
hannesw
parents: 34447
diff changeset
   254
        throw new UnwarrantedOptimismException(arg, programPoint, Type.OBJECT);
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
   255
    }
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
   256
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
   257
    private static MethodHandle findOwnMH(final String name, final Class<?> rtype, final Class<?>... types) {
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
   258
        return MH.findStatic(MethodHandles.lookup(), OptimisticReturnFilters.class, name, MH.type(rtype, types));
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
   259
    }
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
   260
}