FinalScriptFunctionData.java revision 953:221a84ef44c0
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 26package jdk.nashorn.internal.runtime; 27 28import java.lang.invoke.MethodHandle; 29import java.lang.invoke.MethodType; 30 31/** 32 * This is a subclass that represents a script function that may not be regenerated. 33 * This is used for example for bound functions and builtins. 34 */ 35final class FinalScriptFunctionData extends ScriptFunctionData { 36 37 private static final long serialVersionUID = -930632846167768864L; 38 39 /** 40 * Constructor - used for bind 41 * 42 * @param name name 43 * @param arity arity 44 * @param functions precompiled code 45 * @param flags {@link ScriptFunctionData} flags 46 */ 47 FinalScriptFunctionData(final String name, final int arity, final CompiledFunctions functions, final int flags) { 48 super(name, arity, flags); 49 assert !functions.needsCallee(); 50 code.addAll(functions); 51 } 52 53 /** 54 * Constructor - used from ScriptFunction. This assumes that we have code already for the 55 * method (typically a native method) and possibly specializations. 56 * 57 * @param name name 58 * @param mh method handle for generic version of method 59 * @param specs specializations 60 * @param flags {@link ScriptFunctionData} flags 61 */ 62 FinalScriptFunctionData(final String name, final MethodHandle mh, final MethodHandle[] specs, final int flags) { 63 super(name, methodHandleArity(mh), flags); 64 65 addInvoker(mh); 66 if (specs != null) { 67 for (final MethodHandle spec : specs) { 68 addInvoker(spec); 69 } 70 } 71 } 72 73 @Override 74 boolean isRecompilable() { 75 return false; 76 } 77 78 @Override 79 boolean needsCallee() { 80 return code.needsCallee(); 81 } 82 83 @Override 84 MethodType getGenericType() { 85 // We need to ask the code for its generic type. We can't just rely on this function data's arity, as it's not 86 // actually correct for lots of built-ins. E.g. ECMAScript 5.1 section 15.5.3.2 prescribes that 87 // Script.fromCharCode([char0[, char1[, ...]]]) has a declared arity of 1 even though it's a variable arity 88 // method. 89 return code.getFinalGenericType(); 90 } 91 92 private void addInvoker(final MethodHandle mh) { 93 assert !needsCallee(mh); 94 if (isConstructor(mh)) { 95 // only nasgen constructors: (boolean, self, args) are subject to binding a boolean newObj. isConstructor 96 // is too conservative a check. However, isConstructor(mh) always implies isConstructor param 97 assert isConstructor(); 98 code.add(CompiledFunction.createBuiltInConstructor(mh)); 99 } else { 100 code.add(new CompiledFunction(mh)); 101 } 102 } 103 104 private static int methodHandleArity(final MethodHandle mh) { 105 if (isVarArg(mh)) { 106 return MAX_ARITY; 107 } 108 109 //drop self, callee and boolean constructor flag to get real arity 110 return mh.type().parameterCount() - 1 - (needsCallee(mh) ? 1 : 0) - (isConstructor(mh) ? 1 : 0); 111 } 112 113 private static boolean isConstructor(final MethodHandle mh) { 114 return mh.type().parameterCount() >= 1 && mh.type().parameterType(0) == boolean.class; 115 } 116 117} 118