author | sundar |
Sat, 09 Mar 2013 21:49:32 +0530 | |
changeset 16277 | fd698c5ee684 |
parent 16256 | f2d9a0c49914 |
child 19088 | 153f268bfa72 |
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; |
|
16234
86cb162cec6c
8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
16188
diff
changeset
|
33 |
import jdk.internal.dynalink.CallSiteDescriptor; |
16246
f60e04200be3
8008387: Improve code coverage tests for JSObjectLinker and NashornBottomLinker
sundar
parents:
16234
diff
changeset
|
34 |
import jdk.internal.dynalink.beans.BeansLinker; |
16234
86cb162cec6c
8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
16188
diff
changeset
|
35 |
import jdk.internal.dynalink.linker.GuardedInvocation; |
86cb162cec6c
8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
16188
diff
changeset
|
36 |
import jdk.internal.dynalink.linker.GuardingDynamicLinker; |
86cb162cec6c
8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
16188
diff
changeset
|
37 |
import jdk.internal.dynalink.linker.LinkRequest; |
86cb162cec6c
8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
16188
diff
changeset
|
38 |
import jdk.internal.dynalink.linker.LinkerServices; |
86cb162cec6c
8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
16188
diff
changeset
|
39 |
import jdk.internal.dynalink.support.Guards; |
16147 | 40 |
import jdk.nashorn.internal.runtime.ScriptRuntime; |
41 |
||
42 |
/** |
|
43 |
* Nashorn bottom linker; used as a last-resort catch-all linker for all linking requests that fall through all other |
|
44 |
* linkers (see how {@link Bootstrap} class configures the dynamic linker in its static initializer). It will throw |
|
45 |
* appropriate ECMAScript errors for attempts to invoke operations on {@code null}, link no-op property getters and |
|
46 |
* setters for Java objects that couldn't be linked by any other linker, and throw appropriate ECMAScript errors for |
|
47 |
* attempts to invoke arbitrary Java objects as functions or constructors. |
|
48 |
*/ |
|
16277 | 49 |
final class NashornBottomLinker implements GuardingDynamicLinker { |
16147 | 50 |
|
51 |
@Override |
|
52 |
public GuardedInvocation getGuardedInvocation(final LinkRequest linkRequest, final LinkerServices linkerServices) |
|
53 |
throws Exception { |
|
54 |
final Object self = linkRequest.getReceiver(); |
|
55 |
||
56 |
if (self == null) { |
|
57 |
return linkNull(linkRequest); |
|
58 |
} |
|
59 |
||
60 |
// None of the objects that can be linked by NashornLinker should ever reach here. Basically, anything below |
|
61 |
// this point is a generic Java bean. Therefore, reaching here with a ScriptObject is a Nashorn bug. |
|
62 |
assert isExpectedObject(self) : "Couldn't link " + linkRequest.getCallSiteDescriptor() + " for " + self.getClass().getName(); |
|
63 |
||
64 |
return linkBean(linkRequest, linkerServices); |
|
65 |
} |
|
66 |
||
67 |
private static final MethodHandle EMPTY_PROP_GETTER = |
|
68 |
MH.dropArguments(MH.constant(Object.class, UNDEFINED), 0, Object.class); |
|
69 |
private static final MethodHandle EMPTY_ELEM_GETTER = |
|
70 |
MH.dropArguments(EMPTY_PROP_GETTER, 0, Object.class); |
|
71 |
private static final MethodHandle EMPTY_PROP_SETTER = |
|
72 |
MH.asType(EMPTY_ELEM_GETTER, EMPTY_ELEM_GETTER.type().changeReturnType(void.class)); |
|
73 |
private static final MethodHandle EMPTY_ELEM_SETTER = |
|
74 |
MH.dropArguments(EMPTY_PROP_SETTER, 0, Object.class); |
|
75 |
||
76 |
private static GuardedInvocation linkBean(final LinkRequest linkRequest, final LinkerServices linkerServices) { |
|
77 |
final NashornCallSiteDescriptor desc = (NashornCallSiteDescriptor)linkRequest.getCallSiteDescriptor(); |
|
78 |
final Object self = linkRequest.getReceiver(); |
|
79 |
final String operator = desc.getFirstOperator(); |
|
80 |
switch (operator) { |
|
81 |
case "new": |
|
16246
f60e04200be3
8008387: Improve code coverage tests for JSObjectLinker and NashornBottomLinker
sundar
parents:
16234
diff
changeset
|
82 |
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
|
83 |
throw typeError("method.not.constructor", ScriptRuntime.safeToString(self)); |
16147 | 84 |
} |
16256
f2d9a0c49914
8007002: Replace implicit exception throwing methods with explicit throws - simplify control flow and remove useless code
lagergren
parents:
16246
diff
changeset
|
85 |
throw typeError("not.a.function", ScriptRuntime.safeToString(self)); |
16147 | 86 |
case "call": |
16246
f60e04200be3
8008387: Improve code coverage tests for JSObjectLinker and NashornBottomLinker
sundar
parents:
16234
diff
changeset
|
87 |
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
|
88 |
throw typeError("no.method.matches.args", ScriptRuntime.safeToString(self)); |
16147 | 89 |
} |
16256
f2d9a0c49914
8007002: Replace implicit exception throwing methods with explicit throws - simplify control flow and remove useless code
lagergren
parents:
16246
diff
changeset
|
90 |
throw typeError("not.a.function", ScriptRuntime.safeToString(self)); |
16147 | 91 |
case "callMethod": |
92 |
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
|
93 |
throw typeError("no.such.function", getArgument(linkRequest), ScriptRuntime.safeToString(self)); |
16147 | 94 |
case "getProp": |
95 |
case "getElem": |
|
96 |
if (desc.getOperand() != null) { |
|
97 |
return getInvocation(EMPTY_PROP_GETTER, self, linkerServices, desc); |
|
98 |
} |
|
99 |
return getInvocation(EMPTY_ELEM_GETTER, self, linkerServices, desc); |
|
100 |
case "setProp": |
|
101 |
case "setElem": |
|
102 |
if (desc.getOperand() != null) { |
|
103 |
return getInvocation(EMPTY_PROP_SETTER, self, linkerServices, desc); |
|
104 |
} |
|
105 |
return getInvocation(EMPTY_ELEM_SETTER, self, linkerServices, desc); |
|
106 |
default: |
|
107 |
break; |
|
108 |
} |
|
109 |
throw new AssertionError("unknown call type " + desc); |
|
110 |
} |
|
111 |
||
112 |
private static GuardedInvocation getInvocation(final MethodHandle handle, final Object self, final LinkerServices linkerServices, final CallSiteDescriptor desc) { |
|
113 |
return Bootstrap.asType(new GuardedInvocation(handle, Guards.getClassGuard(self.getClass())), linkerServices, desc); |
|
114 |
} |
|
115 |
||
116 |
// Used solely in an assertion to figure out if the object we get here is something we in fact expect. Objects |
|
117 |
// linked by NashornLinker should never reach here. |
|
118 |
private static boolean isExpectedObject(final Object obj) { |
|
119 |
return !(NashornLinker.canLinkTypeStatic(obj.getClass())); |
|
120 |
} |
|
121 |
||
122 |
private static GuardedInvocation linkNull(final LinkRequest linkRequest) { |
|
123 |
final NashornCallSiteDescriptor desc = (NashornCallSiteDescriptor)linkRequest.getCallSiteDescriptor(); |
|
124 |
final String operator = desc.getFirstOperator(); |
|
125 |
switch (operator) { |
|
126 |
case "new": |
|
127 |
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
|
128 |
throw typeError("not.a.function", "null"); |
16147 | 129 |
case "callMethod": |
130 |
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
|
131 |
throw typeError("no.such.function", getArgument(linkRequest), "null"); |
16147 | 132 |
case "getProp": |
133 |
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
|
134 |
throw typeError("cant.get.property", getArgument(linkRequest), "null"); |
16147 | 135 |
case "setProp": |
136 |
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
|
137 |
throw typeError("cant.set.property", getArgument(linkRequest), "null"); |
16147 | 138 |
default: |
139 |
break; |
|
140 |
} |
|
141 |
throw new AssertionError("unknown call type " + desc); |
|
142 |
} |
|
143 |
||
144 |
private static String getArgument(final LinkRequest linkRequest) { |
|
145 |
final CallSiteDescriptor desc = linkRequest.getCallSiteDescriptor(); |
|
146 |
if (desc.getNameTokenCount() > 2) { |
|
147 |
return desc.getNameToken(2); |
|
148 |
} |
|
149 |
return ScriptRuntime.safeToString(linkRequest.getArguments()[1]); |
|
150 |
} |
|
151 |
} |