author | hannesw |
Fri, 25 Jan 2013 17:35:31 +0100 | |
changeset 16195 | 3f6c0ab2597a |
parent 16173 | c41d062f7d2a |
child 16226 | 0e4f37e6cc40 |
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; |
|
27 |
||
28 |
import static jdk.nashorn.internal.runtime.linker.Lookup.MH; |
|
29 |
||
30 |
import java.lang.invoke.MethodHandle; |
|
31 |
import java.lang.invoke.MethodHandles; |
|
32 |
import jdk.nashorn.internal.runtime.linker.NashornCallSiteDescriptor; |
|
33 |
import org.dynalang.dynalink.CallSiteDescriptor; |
|
34 |
import org.dynalang.dynalink.linker.GuardedInvocation; |
|
16195
3f6c0ab2597a
8006766: Array-like access to characters of a string is slow
hannesw
parents:
16173
diff
changeset
|
35 |
import org.dynalang.dynalink.linker.LinkRequest; |
16147 | 36 |
import org.dynalang.dynalink.support.CallSiteDescriptorFactory; |
37 |
||
38 |
||
39 |
/** |
|
40 |
* This class supports the handling of scope in a with body. |
|
41 |
* |
|
42 |
*/ |
|
43 |
public class WithObject extends ScriptObject implements Scope { |
|
44 |
||
45 |
private static final MethodHandle WITHEXPRESSIONFILTER = findOwnMH("withFilterExpression", Object.class, Object.class); |
|
46 |
private static final MethodHandle WITHSCOPEFILTER = findOwnMH("withFilterScope", Object.class, Object.class); |
|
47 |
private static final MethodHandle BIND_TO_EXPRESSION = findOwnMH("bindToExpression", Object.class, Object.class, Object.class); |
|
48 |
||
49 |
/** With expression object. */ |
|
50 |
private final Object expression; |
|
51 |
||
52 |
/** |
|
53 |
* Constructor |
|
54 |
* |
|
55 |
* @param scope scope object |
|
56 |
* @param expression with expression |
|
57 |
*/ |
|
58 |
public WithObject(final ScriptObject scope, final Object expression) { |
|
59 |
super(); |
|
60 |
||
61 |
setIsScope(); |
|
62 |
setProto(scope); |
|
63 |
this.expression = expression; |
|
64 |
} |
|
65 |
||
66 |
/** |
|
67 |
* Delete a property based on a key. |
|
68 |
* @param key Any valid JavaScript value. |
|
69 |
* @param strict strict mode execution. |
|
70 |
* @return True if deleted. |
|
71 |
*/ |
|
72 |
@Override |
|
73 |
public boolean delete(final Object key, final boolean strict) { |
|
74 |
if (expression instanceof ScriptObject) { |
|
75 |
final ScriptObject self = (ScriptObject)expression; |
|
76 |
final String propName = ScriptObject.convertKey(key); |
|
77 |
||
78 |
final FindProperty find = self.findProperty(propName, true); |
|
79 |
||
80 |
if (find != null) { |
|
81 |
return self.delete(propName, strict); |
|
82 |
} |
|
83 |
} |
|
84 |
||
85 |
return false; |
|
86 |
} |
|
87 |
||
88 |
||
89 |
@Override |
|
16195
3f6c0ab2597a
8006766: Array-like access to characters of a string is slow
hannesw
parents:
16173
diff
changeset
|
90 |
public GuardedInvocation lookup(final CallSiteDescriptor desc, final LinkRequest request) { |
16147 | 91 |
// With scopes can never be observed outside of Nashorn code, so all call sites that can address it will of |
92 |
// necessity have a Nashorn descriptor - it is safe to cast. |
|
93 |
final NashornCallSiteDescriptor ndesc = (NashornCallSiteDescriptor)desc; |
|
94 |
FindProperty find = null; |
|
95 |
GuardedInvocation link = null; |
|
96 |
ScriptObject self = null; |
|
97 |
||
98 |
final boolean isNamedOperation; |
|
99 |
final String name; |
|
100 |
if(desc.getNameTokenCount() > 2) { |
|
101 |
isNamedOperation = true; |
|
102 |
name = desc.getNameToken(CallSiteDescriptor.NAME_OPERAND); |
|
103 |
} else { |
|
104 |
isNamedOperation = false; |
|
105 |
name = null; |
|
106 |
} |
|
107 |
||
108 |
if (expression instanceof ScriptObject) { |
|
109 |
self = (ScriptObject)expression; |
|
110 |
if (isNamedOperation) { |
|
111 |
find = self.findProperty(name, true); |
|
112 |
} |
|
113 |
||
114 |
if (find != null) { |
|
16195
3f6c0ab2597a
8006766: Array-like access to characters of a string is slow
hannesw
parents:
16173
diff
changeset
|
115 |
link = self.lookup(desc, request); |
16147 | 116 |
|
117 |
if (link != null) { |
|
118 |
return fixExpressionCallSite(ndesc, link); |
|
119 |
} |
|
120 |
} |
|
121 |
} |
|
122 |
||
123 |
final ScriptObject scope = getProto(); |
|
124 |
if (isNamedOperation) { |
|
125 |
find = scope.findProperty(name, true); |
|
126 |
} |
|
127 |
||
128 |
if (find != null) { |
|
16195
3f6c0ab2597a
8006766: Array-like access to characters of a string is slow
hannesw
parents:
16173
diff
changeset
|
129 |
return fixScopeCallSite(scope.lookup(desc, request)); |
16147 | 130 |
} |
131 |
||
132 |
// the property is not found - now check for |
|
133 |
// __noSuchProperty__ and __noSuchMethod__ in expression |
|
134 |
if (self != null) { |
|
16173 | 135 |
final String fallBack; |
16147 | 136 |
|
137 |
final String operator = CallSiteDescriptorFactory.tokenizeOperators(desc).get(0); |
|
138 |
||
139 |
switch (operator) { |
|
140 |
case "callMethod": |
|
16173 | 141 |
throw new AssertionError(); // Nashorn never emits callMethod |
16147 | 142 |
case "getMethod": |
143 |
fallBack = NO_SUCH_METHOD_NAME; |
|
144 |
break; |
|
145 |
case "getProp": |
|
146 |
case "getElem": |
|
147 |
fallBack = NO_SUCH_PROPERTY_NAME; |
|
148 |
break; |
|
149 |
default: |
|
150 |
fallBack = null; |
|
151 |
break; |
|
152 |
} |
|
153 |
||
154 |
if (fallBack != null) { |
|
155 |
find = self.findProperty(fallBack, true); |
|
156 |
if (find != null) { |
|
157 |
switch (operator) { |
|
158 |
case "getMethod": |
|
16195
3f6c0ab2597a
8006766: Array-like access to characters of a string is slow
hannesw
parents:
16173
diff
changeset
|
159 |
link = self.noSuchMethod(desc, request); |
16147 | 160 |
break; |
161 |
case "getProp": |
|
162 |
case "getElem": |
|
16195
3f6c0ab2597a
8006766: Array-like access to characters of a string is slow
hannesw
parents:
16173
diff
changeset
|
163 |
link = self.noSuchProperty(desc, request); |
16147 | 164 |
break; |
165 |
default: |
|
166 |
break; |
|
167 |
} |
|
168 |
} |
|
169 |
} |
|
170 |
||
171 |
if (link != null) { |
|
172 |
return fixExpressionCallSite(ndesc, link); |
|
173 |
} |
|
174 |
} |
|
175 |
||
176 |
// still not found, may be scope can handle with it's own |
|
177 |
// __noSuchProperty__, __noSuchMethod__ etc. |
|
16195
3f6c0ab2597a
8006766: Array-like access to characters of a string is slow
hannesw
parents:
16173
diff
changeset
|
178 |
link = scope.lookup(desc, request); |
16147 | 179 |
|
180 |
if (link != null) { |
|
181 |
return fixScopeCallSite(link); |
|
182 |
} |
|
183 |
||
184 |
return null; |
|
185 |
} |
|
186 |
||
187 |
@Override |
|
188 |
public void setSplitState(final int state) { |
|
189 |
getNonWithParent().setSplitState(state); |
|
190 |
} |
|
191 |
||
192 |
@Override |
|
193 |
public int getSplitState() { |
|
194 |
return getNonWithParent().getSplitState(); |
|
195 |
} |
|
196 |
||
197 |
/** |
|
198 |
* Get first parent scope that is not an instance of WithObject. |
|
199 |
*/ |
|
200 |
private Scope getNonWithParent() { |
|
201 |
ScriptObject proto = getProto(); |
|
202 |
||
203 |
while (proto != null && proto instanceof WithObject) { |
|
204 |
proto = proto.getProto(); |
|
205 |
} |
|
206 |
||
207 |
assert proto instanceof Scope : "with scope without parent scope"; |
|
208 |
return (Scope) proto; |
|
209 |
} |
|
210 |
||
211 |
private static GuardedInvocation fixExpressionCallSite(final NashornCallSiteDescriptor desc, final GuardedInvocation link) { |
|
212 |
// If it's not a getMethod, just add an expression filter that converts WithObject in "this" position to its |
|
213 |
// expression. |
|
214 |
if(!"getMethod".equals(desc.getFirstOperator())) { |
|
215 |
return link.filterArguments(0, WITHEXPRESSIONFILTER); |
|
216 |
} |
|
217 |
||
218 |
return link.replaceMethods( |
|
219 |
// Make sure getMethod will bind the script functions it receives to WithObject.expression |
|
220 |
MH.foldArguments(BIND_TO_EXPRESSION, filter(link.getInvocation(), WITHEXPRESSIONFILTER)), |
|
221 |
// No clever things for the guard -- it is still identically filtered. |
|
222 |
filterGuard(link, WITHEXPRESSIONFILTER)); |
|
223 |
} |
|
224 |
||
225 |
private static GuardedInvocation fixScopeCallSite(final GuardedInvocation link) { |
|
226 |
return link.replaceMethods(filter(link.getInvocation(), WITHSCOPEFILTER), filterGuard(link, WITHSCOPEFILTER)); |
|
227 |
} |
|
228 |
||
229 |
private static MethodHandle filterGuard(final GuardedInvocation link, final MethodHandle filter) { |
|
230 |
final MethodHandle test = link.getGuard(); |
|
231 |
return test == null ? null : filter(test, filter); |
|
232 |
} |
|
233 |
||
234 |
private static MethodHandle filter(final MethodHandle mh, final MethodHandle filter) { |
|
235 |
return MH.filterArguments(mh, 0, filter); |
|
236 |
} |
|
237 |
||
238 |
/** |
|
239 |
* Drops the WithObject wrapper from the expression. |
|
240 |
* @param receiver WithObject wrapper. |
|
241 |
* @return The with expression. |
|
242 |
*/ |
|
243 |
public static Object withFilterExpression(final Object receiver) { |
|
244 |
return ((WithObject)receiver).expression; |
|
245 |
} |
|
246 |
||
247 |
||
248 |
@SuppressWarnings("unused") |
|
249 |
private static Object bindToExpression(final Object fn, final Object receiver) { |
|
250 |
return fn instanceof ScriptFunction ? ((ScriptFunction) fn).makeBoundFunction(withFilterExpression(receiver), new Object[0]) : fn; |
|
251 |
} |
|
252 |
||
253 |
/** |
|
254 |
* Drops the WithObject wrapper from the scope. |
|
255 |
* @param receiver WithObject wrapper. |
|
256 |
* @return The with scope. |
|
257 |
*/ |
|
258 |
public static Object withFilterScope(final Object receiver) { |
|
259 |
return ((WithObject)receiver).getProto(); |
|
260 |
} |
|
261 |
||
262 |
/** |
|
263 |
* Get the with expression for this {@code WithObject} |
|
264 |
* @return the with expression |
|
265 |
*/ |
|
266 |
public Object getExpression() { |
|
267 |
return expression; |
|
268 |
} |
|
269 |
||
270 |
private static MethodHandle findOwnMH(final String name, final Class<?> rtype, final Class<?>... types) { |
|
271 |
return MH.findStatic(MethodHandles.lookup(), WithObject.class, name, MH.type(rtype, types)); |
|
272 |
} |
|
273 |
} |