1 /* 2 * Copyright (c) 2010, 2014, 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 java.lang.invoke.MethodHandle; 29 import jdk.nashorn.internal.objects.annotations.SpecializedFunction; 30 import jdk.nashorn.internal.objects.annotations.SpecializedFunction.LinkLogic; 31 32 /** 33 * Specialization info for a {@link SpecializedFunction} 34 */ 35 public final class Specialization { 36 private final MethodHandle mh; 37 private final Class<? extends LinkLogic> linkLogicClass; 38 private final boolean isOptimistic; 39 private final boolean convertsNumericArgs; 40 41 /** 42 * Constructor 43 * 44 * @param mh invoker method handler 45 */ 46 public Specialization(final MethodHandle mh) { 47 this(mh, false, true); 48 } 49 50 /** 51 * Constructor 52 * 53 * @param mh invoker method handler 54 * @param isOptimistic is this an optimistic native method, i.e. can it throw {@link UnwarrantedOptimismException} 55 * which would have to lead to a relink and return value processing 56 * @param convertsNumericArgs true if it is safe to convert arguments to numbers 57 */ 58 public Specialization(final MethodHandle mh, final boolean isOptimistic, final boolean convertsNumericArgs) { 59 this(mh, null, isOptimistic, convertsNumericArgs); 60 } 61 62 /** 63 * Constructor 64 * 65 * @param mh invoker method handler 66 * @param linkLogicClass extra link logic needed for this function. Instances of this class also contains logic for checking 67 * if this can be linked on its first encounter, which is needed as per our standard linker semantics 68 * @param isOptimistic is this an optimistic native method, i.e. can it throw {@link UnwarrantedOptimismException} 69 * which would have to lead to a relink and return value processing 70 * @param convertsNumericArgs true if it is safe to convert arguments to numbers 71 */ 72 public Specialization(final MethodHandle mh, final Class<? extends LinkLogic> linkLogicClass, 73 final boolean isOptimistic, final boolean convertsNumericArgs) { 74 this.mh = mh; 75 this.isOptimistic = isOptimistic; 76 this.convertsNumericArgs = convertsNumericArgs; 77 if (linkLogicClass != null) { 78 //null out the "empty" link logic class for optimization purposes 79 //we only use the empty instance because we can't default class annotations 80 //to null 81 this.linkLogicClass = LinkLogic.isEmpty(linkLogicClass) ? null : linkLogicClass; 82 } else { 83 this.linkLogicClass = null; 84 } 85 } 86 87 /** 88 * Get the method handle for the invoker of this ScriptFunction 89 * @return the method handle 90 */ 91 public MethodHandle getMethodHandle() { 92 return mh; 93 } 94 95 /** 96 * Get the link logic class for this ScriptFunction 97 * @return link logic class info, i.e. one whose instance contains stuff like 98 * "do we need exception check for every call", and logic to check if we may link 99 */ 100 public Class<? extends LinkLogic> getLinkLogicClass() { 101 return linkLogicClass; 102 } 103 104 /** 105 * An optimistic specialization is one that can throw UnwarrantedOptimismException. 106 * This is allowed for native methods, as long as they are functional, i.e. don't change 107 * any state between entering and throwing the UOE. Then we can re-execute a wider version 108 * of the method in the continuation. Rest-of method generation for optimistic builtins is 109 * of course not possible, but this approach works and fits into the same relinking 110 * framework 111 * 112 * @return true if optimistic 113 */ 114 public boolean isOptimistic() { 115 return isOptimistic; 116 } 117 118 /** 119 * Check if this function converts arguments for numeric parameters to numbers 120 * so it's safe to pass booleans as 0 and 1 121 * 122 * @return true if it is safe to convert arguments to numbers 123 */ 124 public boolean convertsNumericArgs() { 125 return convertsNumericArgs; 126 } 127 128 } 129