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