author | sundar |
Tue, 23 Jul 2013 18:17:25 +0530 | |
changeset 19088 | 153f268bfa72 |
parent 16277 | fd698c5ee684 |
child 19097 | f544a2ea40ef |
permissions | -rw-r--r-- |
16147 | 1 |
/* |
16151 | 2 |
* Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. |
16147 | 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.linker; |
|
27 |
||
28 |
import static jdk.nashorn.internal.runtime.ECMAErrors.typeError; |
|
29 |
import static jdk.nashorn.internal.runtime.ScriptRuntime.UNDEFINED; |
|
16277 | 30 |
import static jdk.nashorn.internal.lookup.Lookup.MH; |
16147 | 31 |
|
32 |
import java.lang.invoke.MethodHandle; |
|
19088
153f268bfa72
8021122: Not all callables are handled for toString and other function valued properties
sundar
parents:
16277
diff
changeset
|
33 |
import java.lang.invoke.MethodType; |
153f268bfa72
8021122: Not all callables are handled for toString and other function valued properties
sundar
parents:
16277
diff
changeset
|
34 |
import java.lang.reflect.Method; |
153f268bfa72
8021122: Not all callables are handled for toString and other function valued properties
sundar
parents:
16277
diff
changeset
|
35 |
import java.lang.reflect.Modifier; |
16234
86cb162cec6c
8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
16188
diff
changeset
|
36 |
import jdk.internal.dynalink.CallSiteDescriptor; |
16246
f60e04200be3
8008387: Improve code coverage tests for JSObjectLinker and NashornBottomLinker
sundar
parents:
16234
diff
changeset
|
37 |
import jdk.internal.dynalink.beans.BeansLinker; |
16234
86cb162cec6c
8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
16188
diff
changeset
|
38 |
import jdk.internal.dynalink.linker.GuardedInvocation; |
86cb162cec6c
8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
16188
diff
changeset
|
39 |
import jdk.internal.dynalink.linker.GuardingDynamicLinker; |
86cb162cec6c
8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
16188
diff
changeset
|
40 |
import jdk.internal.dynalink.linker.LinkRequest; |
86cb162cec6c
8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
16188
diff
changeset
|
41 |
import jdk.internal.dynalink.linker.LinkerServices; |
86cb162cec6c
8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
16188
diff
changeset
|
42 |
import jdk.internal.dynalink.support.Guards; |
19088
153f268bfa72
8021122: Not all callables are handled for toString and other function valued properties
sundar
parents:
16277
diff
changeset
|
43 |
import jdk.nashorn.internal.runtime.Context; |
16147 | 44 |
import jdk.nashorn.internal.runtime.ScriptRuntime; |
45 |
||
46 |
/** |
|
47 |
* Nashorn bottom linker; used as a last-resort catch-all linker for all linking requests that fall through all other |
|
48 |
* linkers (see how {@link Bootstrap} class configures the dynamic linker in its static initializer). It will throw |
|
49 |
* appropriate ECMAScript errors for attempts to invoke operations on {@code null}, link no-op property getters and |
|
50 |
* setters for Java objects that couldn't be linked by any other linker, and throw appropriate ECMAScript errors for |
|
51 |
* attempts to invoke arbitrary Java objects as functions or constructors. |
|
52 |
*/ |
|
16277 | 53 |
final class NashornBottomLinker implements GuardingDynamicLinker { |
16147 | 54 |
|
55 |
@Override |
|
56 |
public GuardedInvocation getGuardedInvocation(final LinkRequest linkRequest, final LinkerServices linkerServices) |
|
57 |
throws Exception { |
|
58 |
final Object self = linkRequest.getReceiver(); |
|
59 |
||
60 |
if (self == null) { |
|
61 |
return linkNull(linkRequest); |
|
62 |
} |
|
63 |
||
64 |
// None of the objects that can be linked by NashornLinker should ever reach here. Basically, anything below |
|
65 |
// this point is a generic Java bean. Therefore, reaching here with a ScriptObject is a Nashorn bug. |
|
66 |
assert isExpectedObject(self) : "Couldn't link " + linkRequest.getCallSiteDescriptor() + " for " + self.getClass().getName(); |
|
67 |
||
68 |
return linkBean(linkRequest, linkerServices); |
|
69 |
} |
|
70 |
||
71 |
private static final MethodHandle EMPTY_PROP_GETTER = |
|
72 |
MH.dropArguments(MH.constant(Object.class, UNDEFINED), 0, Object.class); |
|
73 |
private static final MethodHandle EMPTY_ELEM_GETTER = |
|
74 |
MH.dropArguments(EMPTY_PROP_GETTER, 0, Object.class); |
|
75 |
private static final MethodHandle EMPTY_PROP_SETTER = |
|
76 |
MH.asType(EMPTY_ELEM_GETTER, EMPTY_ELEM_GETTER.type().changeReturnType(void.class)); |
|
77 |
private static final MethodHandle EMPTY_ELEM_SETTER = |
|
78 |
MH.dropArguments(EMPTY_PROP_SETTER, 0, Object.class); |
|
79 |
||
19088
153f268bfa72
8021122: Not all callables are handled for toString and other function valued properties
sundar
parents:
16277
diff
changeset
|
80 |
private static GuardedInvocation linkBean(final LinkRequest linkRequest, final LinkerServices linkerServices) throws Exception { |
16147 | 81 |
final NashornCallSiteDescriptor desc = (NashornCallSiteDescriptor)linkRequest.getCallSiteDescriptor(); |
82 |
final Object self = linkRequest.getReceiver(); |
|
83 |
final String operator = desc.getFirstOperator(); |
|
84 |
switch (operator) { |
|
85 |
case "new": |
|
16246
f60e04200be3
8008387: Improve code coverage tests for JSObjectLinker and NashornBottomLinker
sundar
parents:
16234
diff
changeset
|
86 |
if(BeansLinker.isDynamicMethod(self)) { |
16256
f2d9a0c49914
8007002: Replace implicit exception throwing methods with explicit throws - simplify control flow and remove useless code
lagergren
parents:
16246
diff
changeset
|
87 |
throw typeError("method.not.constructor", ScriptRuntime.safeToString(self)); |
16147 | 88 |
} |
16256
f2d9a0c49914
8007002: Replace implicit exception throwing methods with explicit throws - simplify control flow and remove useless code
lagergren
parents:
16246
diff
changeset
|
89 |
throw typeError("not.a.function", ScriptRuntime.safeToString(self)); |
16147 | 90 |
case "call": |
19088
153f268bfa72
8021122: Not all callables are handled for toString and other function valued properties
sundar
parents:
16277
diff
changeset
|
91 |
// Support dyn:call on any object that supports some @FunctionalInterface |
153f268bfa72
8021122: Not all callables are handled for toString and other function valued properties
sundar
parents:
16277
diff
changeset
|
92 |
// annotated interface. This way Java method, constructor references or |
153f268bfa72
8021122: Not all callables are handled for toString and other function valued properties
sundar
parents:
16277
diff
changeset
|
93 |
// implementations of java.util.function.* interfaces can be called as though |
153f268bfa72
8021122: Not all callables are handled for toString and other function valued properties
sundar
parents:
16277
diff
changeset
|
94 |
// those are script functions. |
153f268bfa72
8021122: Not all callables are handled for toString and other function valued properties
sundar
parents:
16277
diff
changeset
|
95 |
final Method m = getFunctionalInterfaceMethod(self.getClass()); |
153f268bfa72
8021122: Not all callables are handled for toString and other function valued properties
sundar
parents:
16277
diff
changeset
|
96 |
if (m != null) { |
153f268bfa72
8021122: Not all callables are handled for toString and other function valued properties
sundar
parents:
16277
diff
changeset
|
97 |
final MethodType callType = desc.getMethodType(); |
153f268bfa72
8021122: Not all callables are handled for toString and other function valued properties
sundar
parents:
16277
diff
changeset
|
98 |
// 'callee' and 'thiz' passed from script + actual arguments |
153f268bfa72
8021122: Not all callables are handled for toString and other function valued properties
sundar
parents:
16277
diff
changeset
|
99 |
if (callType.parameterCount() != m.getParameterCount() + 2) { |
153f268bfa72
8021122: Not all callables are handled for toString and other function valued properties
sundar
parents:
16277
diff
changeset
|
100 |
throw typeError("no.method.matches.args", ScriptRuntime.safeToString(self)); |
153f268bfa72
8021122: Not all callables are handled for toString and other function valued properties
sundar
parents:
16277
diff
changeset
|
101 |
} |
153f268bfa72
8021122: Not all callables are handled for toString and other function valued properties
sundar
parents:
16277
diff
changeset
|
102 |
return new GuardedInvocation( |
153f268bfa72
8021122: Not all callables are handled for toString and other function valued properties
sundar
parents:
16277
diff
changeset
|
103 |
// drop 'thiz' passed from the script. |
153f268bfa72
8021122: Not all callables are handled for toString and other function valued properties
sundar
parents:
16277
diff
changeset
|
104 |
MH.dropArguments(desc.getLookup().unreflect(m), 1, callType.parameterType(1)), |
153f268bfa72
8021122: Not all callables are handled for toString and other function valued properties
sundar
parents:
16277
diff
changeset
|
105 |
Guards.getInstanceOfGuard(m.getDeclaringClass())).asType(callType); |
153f268bfa72
8021122: Not all callables are handled for toString and other function valued properties
sundar
parents:
16277
diff
changeset
|
106 |
} |
16246
f60e04200be3
8008387: Improve code coverage tests for JSObjectLinker and NashornBottomLinker
sundar
parents:
16234
diff
changeset
|
107 |
if(BeansLinker.isDynamicMethod(self)) { |
16256
f2d9a0c49914
8007002: Replace implicit exception throwing methods with explicit throws - simplify control flow and remove useless code
lagergren
parents:
16246
diff
changeset
|
108 |
throw typeError("no.method.matches.args", ScriptRuntime.safeToString(self)); |
16147 | 109 |
} |
16256
f2d9a0c49914
8007002: Replace implicit exception throwing methods with explicit throws - simplify control flow and remove useless code
lagergren
parents:
16246
diff
changeset
|
110 |
throw typeError("not.a.function", ScriptRuntime.safeToString(self)); |
16147 | 111 |
case "callMethod": |
112 |
case "getMethod": |
|
16256
f2d9a0c49914
8007002: Replace implicit exception throwing methods with explicit throws - simplify control flow and remove useless code
lagergren
parents:
16246
diff
changeset
|
113 |
throw typeError("no.such.function", getArgument(linkRequest), ScriptRuntime.safeToString(self)); |
16147 | 114 |
case "getProp": |
115 |
case "getElem": |
|
116 |
if (desc.getOperand() != null) { |
|
117 |
return getInvocation(EMPTY_PROP_GETTER, self, linkerServices, desc); |
|
118 |
} |
|
119 |
return getInvocation(EMPTY_ELEM_GETTER, self, linkerServices, desc); |
|
120 |
case "setProp": |
|
121 |
case "setElem": |
|
122 |
if (desc.getOperand() != null) { |
|
123 |
return getInvocation(EMPTY_PROP_SETTER, self, linkerServices, desc); |
|
124 |
} |
|
125 |
return getInvocation(EMPTY_ELEM_SETTER, self, linkerServices, desc); |
|
126 |
default: |
|
127 |
break; |
|
128 |
} |
|
129 |
throw new AssertionError("unknown call type " + desc); |
|
130 |
} |
|
131 |
||
132 |
private static GuardedInvocation getInvocation(final MethodHandle handle, final Object self, final LinkerServices linkerServices, final CallSiteDescriptor desc) { |
|
133 |
return Bootstrap.asType(new GuardedInvocation(handle, Guards.getClassGuard(self.getClass())), linkerServices, desc); |
|
134 |
} |
|
135 |
||
136 |
// Used solely in an assertion to figure out if the object we get here is something we in fact expect. Objects |
|
137 |
// linked by NashornLinker should never reach here. |
|
138 |
private static boolean isExpectedObject(final Object obj) { |
|
139 |
return !(NashornLinker.canLinkTypeStatic(obj.getClass())); |
|
140 |
} |
|
141 |
||
142 |
private static GuardedInvocation linkNull(final LinkRequest linkRequest) { |
|
143 |
final NashornCallSiteDescriptor desc = (NashornCallSiteDescriptor)linkRequest.getCallSiteDescriptor(); |
|
144 |
final String operator = desc.getFirstOperator(); |
|
145 |
switch (operator) { |
|
146 |
case "new": |
|
147 |
case "call": |
|
16256
f2d9a0c49914
8007002: Replace implicit exception throwing methods with explicit throws - simplify control flow and remove useless code
lagergren
parents:
16246
diff
changeset
|
148 |
throw typeError("not.a.function", "null"); |
16147 | 149 |
case "callMethod": |
150 |
case "getMethod": |
|
16256
f2d9a0c49914
8007002: Replace implicit exception throwing methods with explicit throws - simplify control flow and remove useless code
lagergren
parents:
16246
diff
changeset
|
151 |
throw typeError("no.such.function", getArgument(linkRequest), "null"); |
16147 | 152 |
case "getProp": |
153 |
case "getElem": |
|
16256
f2d9a0c49914
8007002: Replace implicit exception throwing methods with explicit throws - simplify control flow and remove useless code
lagergren
parents:
16246
diff
changeset
|
154 |
throw typeError("cant.get.property", getArgument(linkRequest), "null"); |
16147 | 155 |
case "setProp": |
156 |
case "setElem": |
|
16256
f2d9a0c49914
8007002: Replace implicit exception throwing methods with explicit throws - simplify control flow and remove useless code
lagergren
parents:
16246
diff
changeset
|
157 |
throw typeError("cant.set.property", getArgument(linkRequest), "null"); |
16147 | 158 |
default: |
159 |
break; |
|
160 |
} |
|
161 |
throw new AssertionError("unknown call type " + desc); |
|
162 |
} |
|
163 |
||
164 |
private static String getArgument(final LinkRequest linkRequest) { |
|
165 |
final CallSiteDescriptor desc = linkRequest.getCallSiteDescriptor(); |
|
166 |
if (desc.getNameTokenCount() > 2) { |
|
167 |
return desc.getNameToken(2); |
|
168 |
} |
|
169 |
return ScriptRuntime.safeToString(linkRequest.getArguments()[1]); |
|
170 |
} |
|
19088
153f268bfa72
8021122: Not all callables are handled for toString and other function valued properties
sundar
parents:
16277
diff
changeset
|
171 |
|
153f268bfa72
8021122: Not all callables are handled for toString and other function valued properties
sundar
parents:
16277
diff
changeset
|
172 |
// Returns @FunctionalInterface annotated interface's single abstract method. |
153f268bfa72
8021122: Not all callables are handled for toString and other function valued properties
sundar
parents:
16277
diff
changeset
|
173 |
// If not found, returns null |
153f268bfa72
8021122: Not all callables are handled for toString and other function valued properties
sundar
parents:
16277
diff
changeset
|
174 |
static Method getFunctionalInterfaceMethod(final Class<?> clazz) { |
153f268bfa72
8021122: Not all callables are handled for toString and other function valued properties
sundar
parents:
16277
diff
changeset
|
175 |
if (clazz == null) { |
153f268bfa72
8021122: Not all callables are handled for toString and other function valued properties
sundar
parents:
16277
diff
changeset
|
176 |
return null; |
153f268bfa72
8021122: Not all callables are handled for toString and other function valued properties
sundar
parents:
16277
diff
changeset
|
177 |
} |
153f268bfa72
8021122: Not all callables are handled for toString and other function valued properties
sundar
parents:
16277
diff
changeset
|
178 |
|
153f268bfa72
8021122: Not all callables are handled for toString and other function valued properties
sundar
parents:
16277
diff
changeset
|
179 |
for (Class<?> iface : clazz.getInterfaces()) { |
153f268bfa72
8021122: Not all callables are handled for toString and other function valued properties
sundar
parents:
16277
diff
changeset
|
180 |
// check accessiblity up-front |
153f268bfa72
8021122: Not all callables are handled for toString and other function valued properties
sundar
parents:
16277
diff
changeset
|
181 |
if (! Context.isAccessibleClass(iface)) { |
153f268bfa72
8021122: Not all callables are handled for toString and other function valued properties
sundar
parents:
16277
diff
changeset
|
182 |
continue; |
153f268bfa72
8021122: Not all callables are handled for toString and other function valued properties
sundar
parents:
16277
diff
changeset
|
183 |
} |
153f268bfa72
8021122: Not all callables are handled for toString and other function valued properties
sundar
parents:
16277
diff
changeset
|
184 |
|
153f268bfa72
8021122: Not all callables are handled for toString and other function valued properties
sundar
parents:
16277
diff
changeset
|
185 |
// check for @FunctionalInterface |
153f268bfa72
8021122: Not all callables are handled for toString and other function valued properties
sundar
parents:
16277
diff
changeset
|
186 |
if (iface.isAnnotationPresent(FunctionalInterface.class)) { |
153f268bfa72
8021122: Not all callables are handled for toString and other function valued properties
sundar
parents:
16277
diff
changeset
|
187 |
// return the first abstract method |
153f268bfa72
8021122: Not all callables are handled for toString and other function valued properties
sundar
parents:
16277
diff
changeset
|
188 |
for (final Method m : iface.getMethods()) { |
153f268bfa72
8021122: Not all callables are handled for toString and other function valued properties
sundar
parents:
16277
diff
changeset
|
189 |
if (Modifier.isAbstract(m.getModifiers())) { |
153f268bfa72
8021122: Not all callables are handled for toString and other function valued properties
sundar
parents:
16277
diff
changeset
|
190 |
return m; |
153f268bfa72
8021122: Not all callables are handled for toString and other function valued properties
sundar
parents:
16277
diff
changeset
|
191 |
} |
153f268bfa72
8021122: Not all callables are handled for toString and other function valued properties
sundar
parents:
16277
diff
changeset
|
192 |
} |
153f268bfa72
8021122: Not all callables are handled for toString and other function valued properties
sundar
parents:
16277
diff
changeset
|
193 |
} |
153f268bfa72
8021122: Not all callables are handled for toString and other function valued properties
sundar
parents:
16277
diff
changeset
|
194 |
} |
153f268bfa72
8021122: Not all callables are handled for toString and other function valued properties
sundar
parents:
16277
diff
changeset
|
195 |
|
153f268bfa72
8021122: Not all callables are handled for toString and other function valued properties
sundar
parents:
16277
diff
changeset
|
196 |
// did not find here, try super class |
153f268bfa72
8021122: Not all callables are handled for toString and other function valued properties
sundar
parents:
16277
diff
changeset
|
197 |
return getFunctionalInterfaceMethod(clazz.getSuperclass()); |
153f268bfa72
8021122: Not all callables are handled for toString and other function valued properties
sundar
parents:
16277
diff
changeset
|
198 |
} |
16147 | 199 |
} |