FinalScriptFunctionData.java revision 1036:f0b5e3900a10
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;
30import java.util.List;
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 */
36final class FinalScriptFunctionData extends ScriptFunctionData {
37
38    private static final long serialVersionUID = -930632846167768864L;
39
40    /**
41     * Constructor - used for bind
42     *
43     * @param name      name
44     * @param arity     arity
45     * @param functions precompiled code
46     * @param flags     {@link ScriptFunctionData} flags
47     */
48    FinalScriptFunctionData(final String name, final int arity, final List<CompiledFunction> functions, final int flags) {
49        super(name, arity, flags);
50        code.addAll(functions);
51        assert !needsCallee();
52    }
53
54    /**
55     * Constructor - used from ScriptFunction. This assumes that we have code already for the
56     * method (typically a native method) and possibly specializations.
57     *
58     * @param name  name
59     * @param mh    method handle for generic version of method
60     * @param specs specializations
61     * @param flags {@link ScriptFunctionData} flags
62     */
63    FinalScriptFunctionData(final String name, final MethodHandle mh, final Specialization[] specs, final int flags) {
64        super(name, methodHandleArity(mh), flags);
65
66        addInvoker(mh);
67        if (specs != null) {
68            for (final Specialization spec : specs) {
69                addInvoker(spec.getMethodHandle(), spec);
70            }
71        }
72    }
73
74    @Override
75    boolean isRecompilable() {
76        return false;
77    }
78
79    @Override
80    protected boolean needsCallee() {
81        final boolean needsCallee = code.getFirst().needsCallee();
82        assert allNeedCallee(needsCallee);
83        return needsCallee;
84    }
85
86    private boolean allNeedCallee(final boolean needCallee) {
87        for (final CompiledFunction inv : code) {
88            if(inv.needsCallee() != needCallee) {
89                return false;
90            }
91        }
92        return true;
93    }
94
95    @Override
96    MethodType getGenericType() {
97        // 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
98        // actually correct for lots of built-ins. E.g. ECMAScript 5.1 section 15.5.3.2 prescribes that
99        // Script.fromCharCode([char0[, char1[, ...]]]) has a declared arity of 1 even though it's a variable arity
100        // method.
101        int max = 0;
102        for(final CompiledFunction fn: code) {
103            final MethodType t = fn.type();
104            if(ScriptFunctionData.isVarArg(t)) {
105                // 2 for (callee, this, args[])
106                return MethodType.genericMethodType(2, true);
107            }
108            final int paramCount = t.parameterCount() - (ScriptFunctionData.needsCallee(t) ? 1 : 0);
109            if(paramCount > max) {
110                max = paramCount;
111            }
112        }
113        // +1 for callee
114        return MethodType.genericMethodType(max + 1);
115    }
116
117    private CompiledFunction addInvoker(final MethodHandle mh, final Specialization specialization) {
118        assert !needsCallee(mh);
119
120        final CompiledFunction invoker;
121        if (isConstructor(mh)) {
122            // only nasgen constructors: (boolean, self, args) are subject to binding a boolean newObj. isConstructor
123            // is too conservative a check. However, isConstructor(mh) always implies isConstructor param
124            assert isConstructor();
125            invoker = CompiledFunction.createBuiltInConstructor(mh);
126        } else {
127            invoker = new CompiledFunction(mh, null, specialization);
128        }
129        code.add(invoker);
130
131        return invoker;
132    }
133
134    private CompiledFunction addInvoker(final MethodHandle mh) {
135        return addInvoker(mh, null);
136    }
137
138    private static int methodHandleArity(final MethodHandle mh) {
139        if (isVarArg(mh)) {
140            return MAX_ARITY;
141        }
142
143        //drop self, callee and boolean constructor flag to get real arity
144        return mh.type().parameterCount() - 1 - (needsCallee(mh) ? 1 : 0) - (isConstructor(mh) ? 1 : 0);
145    }
146
147    private static boolean isConstructor(final MethodHandle mh) {
148        return mh.type().parameterCount() >= 1 && mh.type().parameterType(0) == boolean.class;
149    }
150
151}
152