16147
|
1 |
/*
|
|
2 |
* Copyright (c) 2010, 2012, 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.linker;
|
|
27 |
|
|
28 |
import static jdk.nashorn.internal.runtime.ECMAErrors.typeError;
|
|
29 |
import static jdk.nashorn.internal.runtime.ScriptRuntime.UNDEFINED;
|
|
30 |
import static jdk.nashorn.internal.runtime.linker.Lookup.MH;
|
|
31 |
|
|
32 |
import java.lang.invoke.MethodHandle;
|
|
33 |
import jdk.nashorn.internal.runtime.Context;
|
|
34 |
import jdk.nashorn.internal.runtime.ScriptObject;
|
|
35 |
import jdk.nashorn.internal.runtime.ScriptRuntime;
|
|
36 |
import org.dynalang.dynalink.CallSiteDescriptor;
|
|
37 |
import org.dynalang.dynalink.linker.GuardedInvocation;
|
|
38 |
import org.dynalang.dynalink.linker.GuardingDynamicLinker;
|
|
39 |
import org.dynalang.dynalink.linker.LinkRequest;
|
|
40 |
import org.dynalang.dynalink.linker.LinkerServices;
|
|
41 |
import org.dynalang.dynalink.support.Guards;
|
|
42 |
|
|
43 |
/**
|
|
44 |
* Nashorn bottom linker; used as a last-resort catch-all linker for all linking requests that fall through all other
|
|
45 |
* linkers (see how {@link Bootstrap} class configures the dynamic linker in its static initializer). It will throw
|
|
46 |
* appropriate ECMAScript errors for attempts to invoke operations on {@code null}, link no-op property getters and
|
|
47 |
* setters for Java objects that couldn't be linked by any other linker, and throw appropriate ECMAScript errors for
|
|
48 |
* attempts to invoke arbitrary Java objects as functions or constructors.
|
|
49 |
*/
|
|
50 |
class NashornBottomLinker implements GuardingDynamicLinker {
|
|
51 |
|
|
52 |
@Override
|
|
53 |
public GuardedInvocation getGuardedInvocation(final LinkRequest linkRequest, final LinkerServices linkerServices)
|
|
54 |
throws Exception {
|
|
55 |
final Object self = linkRequest.getReceiver();
|
|
56 |
|
|
57 |
if (self == null) {
|
|
58 |
return linkNull(linkRequest);
|
|
59 |
}
|
|
60 |
|
|
61 |
// None of the objects that can be linked by NashornLinker should ever reach here. Basically, anything below
|
|
62 |
// this point is a generic Java bean. Therefore, reaching here with a ScriptObject is a Nashorn bug.
|
|
63 |
assert isExpectedObject(self) : "Couldn't link " + linkRequest.getCallSiteDescriptor() + " for " + self.getClass().getName();
|
|
64 |
|
|
65 |
return linkBean(linkRequest, linkerServices);
|
|
66 |
}
|
|
67 |
|
|
68 |
private static final MethodHandle EMPTY_PROP_GETTER =
|
|
69 |
MH.dropArguments(MH.constant(Object.class, UNDEFINED), 0, Object.class);
|
|
70 |
private static final MethodHandle EMPTY_ELEM_GETTER =
|
|
71 |
MH.dropArguments(EMPTY_PROP_GETTER, 0, Object.class);
|
|
72 |
private static final MethodHandle EMPTY_PROP_SETTER =
|
|
73 |
MH.asType(EMPTY_ELEM_GETTER, EMPTY_ELEM_GETTER.type().changeReturnType(void.class));
|
|
74 |
private static final MethodHandle EMPTY_ELEM_SETTER =
|
|
75 |
MH.dropArguments(EMPTY_PROP_SETTER, 0, Object.class);
|
|
76 |
|
|
77 |
private static GuardedInvocation linkBean(final LinkRequest linkRequest, final LinkerServices linkerServices) {
|
|
78 |
final NashornCallSiteDescriptor desc = (NashornCallSiteDescriptor)linkRequest.getCallSiteDescriptor();
|
|
79 |
final Object self = linkRequest.getReceiver();
|
|
80 |
final String operator = desc.getFirstOperator();
|
|
81 |
switch (operator) {
|
|
82 |
case "new":
|
|
83 |
if(isJavaDynamicMethod(self)) {
|
|
84 |
typeError(Context.getGlobal(), "method.not.constructor", ScriptRuntime.safeToString(self));
|
|
85 |
} else {
|
|
86 |
typeError(Context.getGlobal(), "not.a.function", ScriptRuntime.safeToString(self));
|
|
87 |
}
|
|
88 |
break;
|
|
89 |
case "call":
|
|
90 |
if(isJavaDynamicMethod(self)) {
|
|
91 |
typeError(Context.getGlobal(), "no.method.matches.args", ScriptRuntime.safeToString(self));
|
|
92 |
} else {
|
|
93 |
typeError(Context.getGlobal(), "not.a.function", ScriptRuntime.safeToString(self));
|
|
94 |
}
|
|
95 |
break;
|
|
96 |
case "callMethod":
|
|
97 |
case "getMethod":
|
|
98 |
typeError(Context.getGlobal(), "no.such.function", getArgument(linkRequest), ScriptRuntime.safeToString(self));
|
|
99 |
break;
|
|
100 |
case "getProp":
|
|
101 |
case "getElem":
|
|
102 |
if (desc.getOperand() != null) {
|
|
103 |
return getInvocation(EMPTY_PROP_GETTER, self, linkerServices, desc);
|
|
104 |
}
|
|
105 |
return getInvocation(EMPTY_ELEM_GETTER, self, linkerServices, desc);
|
|
106 |
case "setProp":
|
|
107 |
case "setElem":
|
|
108 |
if (desc.getOperand() != null) {
|
|
109 |
return getInvocation(EMPTY_PROP_SETTER, self, linkerServices, desc);
|
|
110 |
}
|
|
111 |
return getInvocation(EMPTY_ELEM_SETTER, self, linkerServices, desc);
|
|
112 |
default:
|
|
113 |
break;
|
|
114 |
}
|
|
115 |
throw new AssertionError("unknown call type " + desc);
|
|
116 |
}
|
|
117 |
|
|
118 |
/**
|
|
119 |
* Returns true if the object is a Dynalink dynamic method. Unfortunately, the dynamic method classes are package
|
|
120 |
* private in Dynalink, so this is the closest we can get to determining it.
|
|
121 |
* @param obj the object we want to test for being a dynamic method
|
|
122 |
* @return true if it is a dynamic method, false otherwise.
|
|
123 |
*/
|
|
124 |
private static boolean isJavaDynamicMethod(Object obj) {
|
|
125 |
return obj.getClass().getName().endsWith("DynamicMethod");
|
|
126 |
}
|
|
127 |
|
|
128 |
private static GuardedInvocation getInvocation(final MethodHandle handle, final Object self, final LinkerServices linkerServices, final CallSiteDescriptor desc) {
|
|
129 |
return Bootstrap.asType(new GuardedInvocation(handle, Guards.getClassGuard(self.getClass())), linkerServices, desc);
|
|
130 |
}
|
|
131 |
|
|
132 |
// Used solely in an assertion to figure out if the object we get here is something we in fact expect. Objects
|
|
133 |
// linked by NashornLinker should never reach here.
|
|
134 |
private static boolean isExpectedObject(final Object obj) {
|
|
135 |
return !(NashornLinker.canLinkTypeStatic(obj.getClass()));
|
|
136 |
}
|
|
137 |
|
|
138 |
private static GuardedInvocation linkNull(final LinkRequest linkRequest) {
|
|
139 |
final ScriptObject global = Context.getGlobal();
|
|
140 |
final NashornCallSiteDescriptor desc = (NashornCallSiteDescriptor)linkRequest.getCallSiteDescriptor();
|
|
141 |
final String operator = desc.getFirstOperator();
|
|
142 |
switch (operator) {
|
|
143 |
case "new":
|
|
144 |
case "call":
|
|
145 |
typeError(global, "not.a.function", "null");
|
|
146 |
break;
|
|
147 |
case "callMethod":
|
|
148 |
case "getMethod":
|
|
149 |
typeError(global, "no.such.function", getArgument(linkRequest), "null");
|
|
150 |
break;
|
|
151 |
case "getProp":
|
|
152 |
case "getElem":
|
|
153 |
typeError(global, "cant.get.property", getArgument(linkRequest), "null");
|
|
154 |
break;
|
|
155 |
case "setProp":
|
|
156 |
case "setElem":
|
|
157 |
typeError(global, "cant.set.property", getArgument(linkRequest), "null");
|
|
158 |
break;
|
|
159 |
default:
|
|
160 |
break;
|
|
161 |
}
|
|
162 |
throw new AssertionError("unknown call type " + desc);
|
|
163 |
}
|
|
164 |
|
|
165 |
private static String getArgument(final LinkRequest linkRequest) {
|
|
166 |
final CallSiteDescriptor desc = linkRequest.getCallSiteDescriptor();
|
|
167 |
if (desc.getNameTokenCount() > 2) {
|
|
168 |
return desc.getNameToken(2);
|
|
169 |
}
|
|
170 |
return ScriptRuntime.safeToString(linkRequest.getArguments()[1]);
|
|
171 |
}
|
|
172 |
}
|