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 30 import java.lang.invoke.MethodHandle; 31 32 /** 33 * This is a subclass that represents a script function that may not be regenerated. 34 * This is used for example for bound functions and builtins. 35 */ 36 final class FinalScriptFunctionData extends ScriptFunctionData { 37 38 /** 39 * Constructor - used for bind 40 * 41 * @param name name 42 * @param arity arity 43 * @param functions precompiled code 44 * @param isStrict strict 45 * @param isBuiltin builtin 46 * @param isConstructor constructor 47 */ 48 FinalScriptFunctionData(final String name, int arity, CompiledFunctions functions, final boolean isStrict, final boolean isBuiltin, final boolean isConstructor) { 49 super(name, arity, isStrict, isBuiltin, isConstructor); 50 code.addAll(functions); 51 } 52 53 /** 54 * Constructor - used from ScriptFunction. This assumes that we have code alraedy 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 isStrict strict 61 * @param isBuiltin builtin 62 * @param isConstructor constructor 63 */ 64 FinalScriptFunctionData(final String name, final MethodHandle mh, final MethodHandle[] specs, final boolean isStrict, final boolean isBuiltin, final boolean isConstructor) { 65 super(name, arity(mh), isStrict, isBuiltin, isConstructor); 66 67 addInvoker(mh); 68 if (specs != null) { 69 for (final MethodHandle spec : specs) { 70 addInvoker(spec); 71 } 72 } 73 } 74 75 private void addInvoker(final MethodHandle mh) { 76 if (isConstructor(mh)) { 77 // only nasgen constructors: (boolean, self, args) are subject to binding a boolean newObj. isConstructor 78 // is too conservative a check. However, isConstructor(mh) always implies isConstructor param 79 assert isConstructor(); 80 final MethodHandle invoker = MH.insertArguments(mh, 0, false); 81 final MethodHandle constructor = composeConstructor(MH.insertArguments(mh, 0, true)); 82 code.add(new CompiledFunction(mh.type(), invoker, constructor)); 83 } else { 84 code.add(new CompiledFunction(mh.type(), mh)); 85 } 86 } 87 88 private static int arity(final MethodHandle mh) { 89 if (isVarArg(mh)) { 90 return -1; 91 } 92 93 //drop self, callee and boolean constructor flag to get real arity 94 return mh.type().parameterCount() - 1 - (needsCallee(mh) ? 1 : 0) - (isConstructor(mh) ? 1 : 0); 95 } 96 97 private static boolean isConstructor(final MethodHandle mh) { 98 return mh.type().parameterCount() >= 1 && mh.type().parameterType(0) == boolean.class; 99 } 100 101 }