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 flags     {@link ScriptFunctionData} flags


  45      */
  46     FinalScriptFunctionData(final String name, final int arity, final CompiledFunctions functions, final int flags) {
  47         super(name, arity, flags);
  48         code.addAll(functions);
  49     }
  50 
  51     /**
  52      * Constructor - used from ScriptFunction. This assumes that we have code already for the
  53      * method (typically a native method) and possibly specializations.
  54      *
  55      * @param name  name
  56      * @param mh    method handle for generic version of method
  57      * @param specs specializations
  58      * @param flags {@link ScriptFunctionData} flags


  59      */
  60     FinalScriptFunctionData(final String name, final MethodHandle mh, final MethodHandle[] specs, final int flags) {
  61         super(name, arity(mh), flags);
  62 
  63         addInvoker(mh);
  64         if (specs != null) {
  65             for (final MethodHandle spec : specs) {
  66                 addInvoker(spec);
  67             }
  68         }
  69     }
  70 
  71     private void addInvoker(final MethodHandle mh) {
  72         if (isConstructor(mh)) {
  73             // only nasgen constructors: (boolean, self, args) are subject to binding a boolean newObj. isConstructor
  74             // is too conservative a check. However, isConstructor(mh) always implies isConstructor param
  75             assert isConstructor();
  76             final MethodHandle invoker = MH.insertArguments(mh, 0, false);
  77             final MethodHandle constructor = composeConstructor(MH.insertArguments(mh, 0, true));
  78             code.add(new CompiledFunction(mh.type(), invoker, constructor));
  79         } else {
  80             code.add(new CompiledFunction(mh.type(), mh));
  81         }
  82     }
  83 
  84     private static int arity(final MethodHandle mh) {
  85         if (isVarArg(mh)) {
  86             return -1;
  87         }
  88 
  89         //drop self, callee and boolean constructor flag to get real arity
  90         return mh.type().parameterCount() - 1 - (needsCallee(mh) ? 1 : 0) - (isConstructor(mh) ? 1 : 0);
  91     }
  92 
  93     private static boolean isConstructor(final MethodHandle mh) {
  94         return mh.type().parameterCount() >= 1 && mh.type().parameterType(0) == boolean.class;
  95     }
  96 
  97 }
--- EOF ---