author | chegar |
Mon, 18 Aug 2014 10:59:40 +0100 | |
changeset 26068 | 5488f52c2788 |
parent 26067 | nashorn/src/jdk/nashorn/internal/runtime/WithObject.java@b32ccc3a76c9 |
parent 25865 | nashorn/src/jdk/nashorn/internal/runtime/WithObject.java@d38d876f1654 |
child 26765 | 97501edd2979 |
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 |
||
16277 | 28 |
import static jdk.nashorn.internal.lookup.Lookup.MH; |
16147 | 29 |
|
30 |
import java.lang.invoke.MethodHandle; |
|
31 |
import java.lang.invoke.MethodHandles; |
|
16780
f23743ec1a93
8011555: Invalid class name in with block with JavaImporter causes MH type mismatch
attila
parents:
16277
diff
changeset
|
32 |
import java.lang.invoke.MethodType; |
19884
1bacbaa1bfc7
8024180: Incorrect handling of expression and parent scope in 'with' statements
sundar
parents:
19621
diff
changeset
|
33 |
import java.lang.invoke.SwitchPoint; |
16234
86cb162cec6c
8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
16226
diff
changeset
|
34 |
import jdk.internal.dynalink.CallSiteDescriptor; |
86cb162cec6c
8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
16226
diff
changeset
|
35 |
import jdk.internal.dynalink.linker.GuardedInvocation; |
86cb162cec6c
8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
16226
diff
changeset
|
36 |
import jdk.internal.dynalink.linker.LinkRequest; |
86cb162cec6c
8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
16226
diff
changeset
|
37 |
import jdk.internal.dynalink.support.CallSiteDescriptorFactory; |
25821
fbb51e67d2a7
8048869: Reduce compile time by about 5% by removing the Class.casts from the AST nodes
lagergren
parents:
25247
diff
changeset
|
38 |
import jdk.nashorn.api.scripting.AbstractJSObject; |
fbb51e67d2a7
8048869: Reduce compile time by about 5% by removing the Class.casts from the AST nodes
lagergren
parents:
25247
diff
changeset
|
39 |
import jdk.nashorn.api.scripting.ScriptObjectMirror; |
16147 | 40 |
import jdk.nashorn.internal.runtime.linker.NashornCallSiteDescriptor; |
23084
6c5c02d1023a
8035948: Redesign property listeners for shared classes
hannesw
parents:
20933
diff
changeset
|
41 |
import jdk.nashorn.internal.runtime.linker.NashornGuards; |
16147 | 42 |
|
43 |
/** |
|
44 |
* This class supports the handling of scope in a with body. |
|
45 |
* |
|
46 |
*/ |
|
16226
0e4f37e6cc40
8007915: Nashorn IR, codegen, parser packages and Context instance should be inaccessible to user code
sundar
parents:
16195
diff
changeset
|
47 |
public final class WithObject extends ScriptObject implements Scope { |
19884
1bacbaa1bfc7
8024180: Incorrect handling of expression and parent scope in 'with' statements
sundar
parents:
19621
diff
changeset
|
48 |
private static final MethodHandle WITHEXPRESSIONGUARD = findOwnMH("withExpressionGuard", boolean.class, Object.class, PropertyMap.class, SwitchPoint.class); |
16780
f23743ec1a93
8011555: Invalid class name in with block with JavaImporter causes MH type mismatch
attila
parents:
16277
diff
changeset
|
49 |
private static final MethodHandle WITHEXPRESSIONFILTER = findOwnMH("withFilterExpression", Object.class, Object.class); |
f23743ec1a93
8011555: Invalid class name in with block with JavaImporter causes MH type mismatch
attila
parents:
16277
diff
changeset
|
50 |
private static final MethodHandle WITHSCOPEFILTER = findOwnMH("withFilterScope", Object.class, Object.class); |
f23743ec1a93
8011555: Invalid class name in with block with JavaImporter causes MH type mismatch
attila
parents:
16277
diff
changeset
|
51 |
private static final MethodHandle BIND_TO_EXPRESSION_OBJ = findOwnMH("bindToExpression", Object.class, Object.class, Object.class); |
f23743ec1a93
8011555: Invalid class name in with block with JavaImporter causes MH type mismatch
attila
parents:
16277
diff
changeset
|
52 |
private static final MethodHandle BIND_TO_EXPRESSION_FN = findOwnMH("bindToExpression", Object.class, ScriptFunction.class, Object.class); |
16147 | 53 |
|
54 |
/** With expression object. */ |
|
19884
1bacbaa1bfc7
8024180: Incorrect handling of expression and parent scope in 'with' statements
sundar
parents:
19621
diff
changeset
|
55 |
private final ScriptObject expression; |
16147 | 56 |
|
57 |
/** |
|
58 |
* Constructor |
|
59 |
* |
|
60 |
* @param scope scope object |
|
61 |
* @param expression with expression |
|
62 |
*/ |
|
19884
1bacbaa1bfc7
8024180: Incorrect handling of expression and parent scope in 'with' statements
sundar
parents:
19621
diff
changeset
|
63 |
WithObject(final ScriptObject scope, final ScriptObject expression) { |
19085
066c9e5afd79
8020731: Revisit checkPermission calls in Context class
sundar
parents:
17236
diff
changeset
|
64 |
super(scope, null); |
16147 | 65 |
setIsScope(); |
66 |
this.expression = expression; |
|
67 |
} |
|
68 |
||
69 |
/** |
|
70 |
* Delete a property based on a key. |
|
71 |
* @param key Any valid JavaScript value. |
|
72 |
* @param strict strict mode execution. |
|
73 |
* @return True if deleted. |
|
74 |
*/ |
|
75 |
@Override |
|
76 |
public boolean delete(final Object key, final boolean strict) { |
|
19884
1bacbaa1bfc7
8024180: Incorrect handling of expression and parent scope in 'with' statements
sundar
parents:
19621
diff
changeset
|
77 |
final ScriptObject self = expression; |
1bacbaa1bfc7
8024180: Incorrect handling of expression and parent scope in 'with' statements
sundar
parents:
19621
diff
changeset
|
78 |
final String propName = JSType.toString(key); |
16147 | 79 |
|
19884
1bacbaa1bfc7
8024180: Incorrect handling of expression and parent scope in 'with' statements
sundar
parents:
19621
diff
changeset
|
80 |
final FindProperty find = self.findProperty(propName, true); |
16147 | 81 |
|
19884
1bacbaa1bfc7
8024180: Incorrect handling of expression and parent scope in 'with' statements
sundar
parents:
19621
diff
changeset
|
82 |
if (find != null) { |
1bacbaa1bfc7
8024180: Incorrect handling of expression and parent scope in 'with' statements
sundar
parents:
19621
diff
changeset
|
83 |
return self.delete(propName, strict); |
16147 | 84 |
} |
85 |
||
86 |
return false; |
|
87 |
} |
|
88 |
||
89 |
||
90 |
@Override |
|
16195
3f6c0ab2597a
8006766: Array-like access to characters of a string is slow
hannesw
parents:
16173
diff
changeset
|
91 |
public GuardedInvocation lookup(final CallSiteDescriptor desc, final LinkRequest request) { |
23372
09707b3e5fb0
8021350: Share script classes between threads/globals within context
hannesw
parents:
23084
diff
changeset
|
92 |
if (request.isCallSiteUnstable()) { |
09707b3e5fb0
8021350: Share script classes between threads/globals within context
hannesw
parents:
23084
diff
changeset
|
93 |
// Fall back to megamorphic invocation which performs a complete lookup each time without further relinking. |
09707b3e5fb0
8021350: Share script classes between threads/globals within context
hannesw
parents:
23084
diff
changeset
|
94 |
return super.lookup(desc, request); |
09707b3e5fb0
8021350: Share script classes between threads/globals within context
hannesw
parents:
23084
diff
changeset
|
95 |
} |
09707b3e5fb0
8021350: Share script classes between threads/globals within context
hannesw
parents:
23084
diff
changeset
|
96 |
|
16147 | 97 |
// With scopes can never be observed outside of Nashorn code, so all call sites that can address it will of |
98 |
// necessity have a Nashorn descriptor - it is safe to cast. |
|
99 |
final NashornCallSiteDescriptor ndesc = (NashornCallSiteDescriptor)desc; |
|
100 |
FindProperty find = null; |
|
101 |
GuardedInvocation link = null; |
|
24719 | 102 |
ScriptObject self; |
16147 | 103 |
|
104 |
final boolean isNamedOperation; |
|
105 |
final String name; |
|
24720 | 106 |
if (desc.getNameTokenCount() > 2) { |
16147 | 107 |
isNamedOperation = true; |
108 |
name = desc.getNameToken(CallSiteDescriptor.NAME_OPERAND); |
|
109 |
} else { |
|
110 |
isNamedOperation = false; |
|
111 |
name = null; |
|
112 |
} |
|
113 |
||
19884
1bacbaa1bfc7
8024180: Incorrect handling of expression and parent scope in 'with' statements
sundar
parents:
19621
diff
changeset
|
114 |
self = expression; |
1bacbaa1bfc7
8024180: Incorrect handling of expression and parent scope in 'with' statements
sundar
parents:
19621
diff
changeset
|
115 |
if (isNamedOperation) { |
1bacbaa1bfc7
8024180: Incorrect handling of expression and parent scope in 'with' statements
sundar
parents:
19621
diff
changeset
|
116 |
find = self.findProperty(name, true); |
1bacbaa1bfc7
8024180: Incorrect handling of expression and parent scope in 'with' statements
sundar
parents:
19621
diff
changeset
|
117 |
} |
16147 | 118 |
|
19884
1bacbaa1bfc7
8024180: Incorrect handling of expression and parent scope in 'with' statements
sundar
parents:
19621
diff
changeset
|
119 |
if (find != null) { |
1bacbaa1bfc7
8024180: Incorrect handling of expression and parent scope in 'with' statements
sundar
parents:
19621
diff
changeset
|
120 |
link = self.lookup(desc, request); |
1bacbaa1bfc7
8024180: Incorrect handling of expression and parent scope in 'with' statements
sundar
parents:
19621
diff
changeset
|
121 |
if (link != null) { |
1bacbaa1bfc7
8024180: Incorrect handling of expression and parent scope in 'with' statements
sundar
parents:
19621
diff
changeset
|
122 |
return fixExpressionCallSite(ndesc, link); |
16147 | 123 |
} |
124 |
} |
|
125 |
||
126 |
final ScriptObject scope = getProto(); |
|
127 |
if (isNamedOperation) { |
|
128 |
find = scope.findProperty(name, true); |
|
129 |
} |
|
130 |
||
131 |
if (find != null) { |
|
23084
6c5c02d1023a
8035948: Redesign property listeners for shared classes
hannesw
parents:
20933
diff
changeset
|
132 |
return fixScopeCallSite(scope.lookup(desc, request), name, find.getOwner()); |
16147 | 133 |
} |
134 |
||
135 |
// the property is not found - now check for |
|
136 |
// __noSuchProperty__ and __noSuchMethod__ in expression |
|
137 |
if (self != null) { |
|
16173 | 138 |
final String fallBack; |
16147 | 139 |
|
140 |
final String operator = CallSiteDescriptorFactory.tokenizeOperators(desc).get(0); |
|
141 |
||
142 |
switch (operator) { |
|
143 |
case "callMethod": |
|
16173 | 144 |
throw new AssertionError(); // Nashorn never emits callMethod |
16147 | 145 |
case "getMethod": |
146 |
fallBack = NO_SUCH_METHOD_NAME; |
|
147 |
break; |
|
148 |
case "getProp": |
|
149 |
case "getElem": |
|
150 |
fallBack = NO_SUCH_PROPERTY_NAME; |
|
151 |
break; |
|
152 |
default: |
|
153 |
fallBack = null; |
|
154 |
break; |
|
155 |
} |
|
156 |
||
157 |
if (fallBack != null) { |
|
158 |
find = self.findProperty(fallBack, true); |
|
159 |
if (find != null) { |
|
160 |
switch (operator) { |
|
161 |
case "getMethod": |
|
16195
3f6c0ab2597a
8006766: Array-like access to characters of a string is slow
hannesw
parents:
16173
diff
changeset
|
162 |
link = self.noSuchMethod(desc, request); |
16147 | 163 |
break; |
164 |
case "getProp": |
|
165 |
case "getElem": |
|
16195
3f6c0ab2597a
8006766: Array-like access to characters of a string is slow
hannesw
parents:
16173
diff
changeset
|
166 |
link = self.noSuchProperty(desc, request); |
16147 | 167 |
break; |
168 |
default: |
|
169 |
break; |
|
170 |
} |
|
171 |
} |
|
172 |
} |
|
173 |
||
174 |
if (link != null) { |
|
175 |
return fixExpressionCallSite(ndesc, link); |
|
176 |
} |
|
177 |
} |
|
178 |
||
179 |
// still not found, may be scope can handle with it's own |
|
180 |
// __noSuchProperty__, __noSuchMethod__ etc. |
|
16195
3f6c0ab2597a
8006766: Array-like access to characters of a string is slow
hannesw
parents:
16173
diff
changeset
|
181 |
link = scope.lookup(desc, request); |
16147 | 182 |
|
183 |
if (link != null) { |
|
23084
6c5c02d1023a
8035948: Redesign property listeners for shared classes
hannesw
parents:
20933
diff
changeset
|
184 |
return fixScopeCallSite(link, name, null); |
16147 | 185 |
} |
186 |
||
187 |
return null; |
|
188 |
} |
|
189 |
||
16264
e0c3c97cd93e
8006984: Introducing local into a function inside with statement confuses its scope
attila
parents:
16234
diff
changeset
|
190 |
/** |
e0c3c97cd93e
8006984: Introducing local into a function inside with statement confuses its scope
attila
parents:
16234
diff
changeset
|
191 |
* Overridden to try to find the property first in the expression object (and its prototypes), and only then in this |
e0c3c97cd93e
8006984: Introducing local into a function inside with statement confuses its scope
attila
parents:
16234
diff
changeset
|
192 |
* object (and its prototypes). |
e0c3c97cd93e
8006984: Introducing local into a function inside with statement confuses its scope
attila
parents:
16234
diff
changeset
|
193 |
* |
e0c3c97cd93e
8006984: Introducing local into a function inside with statement confuses its scope
attila
parents:
16234
diff
changeset
|
194 |
* @param key Property key. |
e0c3c97cd93e
8006984: Introducing local into a function inside with statement confuses its scope
attila
parents:
16234
diff
changeset
|
195 |
* @param deep Whether the search should look up proto chain. |
e0c3c97cd93e
8006984: Introducing local into a function inside with statement confuses its scope
attila
parents:
16234
diff
changeset
|
196 |
* @param stopOnNonScope should a deep search stop on the first non-scope object? |
e0c3c97cd93e
8006984: Introducing local into a function inside with statement confuses its scope
attila
parents:
16234
diff
changeset
|
197 |
* @param start the object on which the lookup was originally initiated |
e0c3c97cd93e
8006984: Introducing local into a function inside with statement confuses its scope
attila
parents:
16234
diff
changeset
|
198 |
* |
e0c3c97cd93e
8006984: Introducing local into a function inside with statement confuses its scope
attila
parents:
16234
diff
changeset
|
199 |
* @return FindPropertyData or null if not found. |
e0c3c97cd93e
8006984: Introducing local into a function inside with statement confuses its scope
attila
parents:
16234
diff
changeset
|
200 |
*/ |
e0c3c97cd93e
8006984: Introducing local into a function inside with statement confuses its scope
attila
parents:
16234
diff
changeset
|
201 |
@Override |
e0c3c97cd93e
8006984: Introducing local into a function inside with statement confuses its scope
attila
parents:
16234
diff
changeset
|
202 |
FindProperty findProperty(final String key, final boolean deep, final boolean stopOnNonScope, final ScriptObject start) { |
19884
1bacbaa1bfc7
8024180: Incorrect handling of expression and parent scope in 'with' statements
sundar
parents:
19621
diff
changeset
|
203 |
final FindProperty exprProperty = expression.findProperty(key, deep, stopOnNonScope, start); |
1bacbaa1bfc7
8024180: Incorrect handling of expression and parent scope in 'with' statements
sundar
parents:
19621
diff
changeset
|
204 |
if (exprProperty != null) { |
1bacbaa1bfc7
8024180: Incorrect handling of expression and parent scope in 'with' statements
sundar
parents:
19621
diff
changeset
|
205 |
return exprProperty; |
16264
e0c3c97cd93e
8006984: Introducing local into a function inside with statement confuses its scope
attila
parents:
16234
diff
changeset
|
206 |
} |
e0c3c97cd93e
8006984: Introducing local into a function inside with statement confuses its scope
attila
parents:
16234
diff
changeset
|
207 |
return super.findProperty(key, deep, stopOnNonScope, start); |
e0c3c97cd93e
8006984: Introducing local into a function inside with statement confuses its scope
attila
parents:
16234
diff
changeset
|
208 |
} |
e0c3c97cd93e
8006984: Introducing local into a function inside with statement confuses its scope
attila
parents:
16234
diff
changeset
|
209 |
|
16147 | 210 |
@Override |
211 |
public void setSplitState(final int state) { |
|
212 |
getNonWithParent().setSplitState(state); |
|
213 |
} |
|
214 |
||
215 |
@Override |
|
216 |
public int getSplitState() { |
|
217 |
return getNonWithParent().getSplitState(); |
|
218 |
} |
|
219 |
||
220 |
/** |
|
221 |
* Get first parent scope that is not an instance of WithObject. |
|
222 |
*/ |
|
223 |
private Scope getNonWithParent() { |
|
19884
1bacbaa1bfc7
8024180: Incorrect handling of expression and parent scope in 'with' statements
sundar
parents:
19621
diff
changeset
|
224 |
ScriptObject proto = getParentScope(); |
16147 | 225 |
|
226 |
while (proto != null && proto instanceof WithObject) { |
|
19884
1bacbaa1bfc7
8024180: Incorrect handling of expression and parent scope in 'with' statements
sundar
parents:
19621
diff
changeset
|
227 |
proto = ((WithObject)proto).getParentScope(); |
16147 | 228 |
} |
229 |
||
230 |
assert proto instanceof Scope : "with scope without parent scope"; |
|
231 |
return (Scope) proto; |
|
232 |
} |
|
233 |
||
19884
1bacbaa1bfc7
8024180: Incorrect handling of expression and parent scope in 'with' statements
sundar
parents:
19621
diff
changeset
|
234 |
|
17236
75779a53c6a9
8012919: findMegaMorphicSetMethod should not cast result type
jlaskey
parents:
17234
diff
changeset
|
235 |
private static GuardedInvocation fixReceiverType(final GuardedInvocation link, final MethodHandle filter) { |
75779a53c6a9
8012919: findMegaMorphicSetMethod should not cast result type
jlaskey
parents:
17234
diff
changeset
|
236 |
// The receiver may be an Object or a ScriptObject. |
75779a53c6a9
8012919: findMegaMorphicSetMethod should not cast result type
jlaskey
parents:
17234
diff
changeset
|
237 |
final MethodType invType = link.getInvocation().type(); |
75779a53c6a9
8012919: findMegaMorphicSetMethod should not cast result type
jlaskey
parents:
17234
diff
changeset
|
238 |
final MethodType newInvType = invType.changeParameterType(0, filter.type().returnType()); |
75779a53c6a9
8012919: findMegaMorphicSetMethod should not cast result type
jlaskey
parents:
17234
diff
changeset
|
239 |
return link.asType(newInvType); |
75779a53c6a9
8012919: findMegaMorphicSetMethod should not cast result type
jlaskey
parents:
17234
diff
changeset
|
240 |
} |
75779a53c6a9
8012919: findMegaMorphicSetMethod should not cast result type
jlaskey
parents:
17234
diff
changeset
|
241 |
|
16147 | 242 |
private static GuardedInvocation fixExpressionCallSite(final NashornCallSiteDescriptor desc, final GuardedInvocation link) { |
243 |
// If it's not a getMethod, just add an expression filter that converts WithObject in "this" position to its |
|
244 |
// expression. |
|
24720 | 245 |
if (!"getMethod".equals(desc.getFirstOperator())) { |
17236
75779a53c6a9
8012919: findMegaMorphicSetMethod should not cast result type
jlaskey
parents:
17234
diff
changeset
|
246 |
return fixReceiverType(link, WITHEXPRESSIONFILTER).filterArguments(0, WITHEXPRESSIONFILTER); |
16147 | 247 |
} |
248 |
||
24720 | 249 |
final MethodHandle linkInvocation = link.getInvocation(); |
250 |
final MethodType linkType = linkInvocation.type(); |
|
251 |
final boolean linkReturnsFunction = ScriptFunction.class.isAssignableFrom(linkType.returnType()); |
|
24719 | 252 |
|
16147 | 253 |
return link.replaceMethods( |
254 |
// Make sure getMethod will bind the script functions it receives to WithObject.expression |
|
24720 | 255 |
MH.foldArguments( |
256 |
linkReturnsFunction ? |
|
257 |
BIND_TO_EXPRESSION_FN : |
|
258 |
BIND_TO_EXPRESSION_OBJ, |
|
259 |
filterReceiver( |
|
260 |
linkInvocation.asType( |
|
261 |
linkType.changeReturnType( |
|
262 |
linkReturnsFunction ? |
|
263 |
ScriptFunction.class : |
|
264 |
Object.class). |
|
265 |
changeParameterType( |
|
266 |
0, |
|
24727 | 267 |
Object.class)), |
24720 | 268 |
WITHEXPRESSIONFILTER)), |
269 |
filterGuardReceiver(link, WITHEXPRESSIONFILTER)); |
|
270 |
// No clever things for the guard -- it is still identically filtered. |
|
271 |
||
16147 | 272 |
} |
273 |
||
23084
6c5c02d1023a
8035948: Redesign property listeners for shared classes
hannesw
parents:
20933
diff
changeset
|
274 |
private GuardedInvocation fixScopeCallSite(final GuardedInvocation link, final String name, final ScriptObject owner) { |
24733
1e825be55fd1
8027043: Turn global accesses into MethodHandle.constant, with one chance of reassignment, e.g. x = value occuring once in the global scope is ok, twice is not.
lagergren
parents:
24727
diff
changeset
|
275 |
final GuardedInvocation newLink = fixReceiverType(link, WITHSCOPEFILTER); |
1e825be55fd1
8027043: Turn global accesses into MethodHandle.constant, with one chance of reassignment, e.g. x = value occuring once in the global scope is ok, twice is not.
lagergren
parents:
24727
diff
changeset
|
276 |
final MethodHandle expressionGuard = expressionGuard(name, owner); |
1e825be55fd1
8027043: Turn global accesses into MethodHandle.constant, with one chance of reassignment, e.g. x = value occuring once in the global scope is ok, twice is not.
lagergren
parents:
24727
diff
changeset
|
277 |
final MethodHandle filterGuardReceiver = filterGuardReceiver(newLink, WITHSCOPEFILTER); |
1e825be55fd1
8027043: Turn global accesses into MethodHandle.constant, with one chance of reassignment, e.g. x = value occuring once in the global scope is ok, twice is not.
lagergren
parents:
24727
diff
changeset
|
278 |
return link.replaceMethods( |
1e825be55fd1
8027043: Turn global accesses into MethodHandle.constant, with one chance of reassignment, e.g. x = value occuring once in the global scope is ok, twice is not.
lagergren
parents:
24727
diff
changeset
|
279 |
filterReceiver( |
1e825be55fd1
8027043: Turn global accesses into MethodHandle.constant, with one chance of reassignment, e.g. x = value occuring once in the global scope is ok, twice is not.
lagergren
parents:
24727
diff
changeset
|
280 |
newLink.getInvocation(), |
1e825be55fd1
8027043: Turn global accesses into MethodHandle.constant, with one chance of reassignment, e.g. x = value occuring once in the global scope is ok, twice is not.
lagergren
parents:
24727
diff
changeset
|
281 |
WITHSCOPEFILTER), |
1e825be55fd1
8027043: Turn global accesses into MethodHandle.constant, with one chance of reassignment, e.g. x = value occuring once in the global scope is ok, twice is not.
lagergren
parents:
24727
diff
changeset
|
282 |
NashornGuards.combineGuards( |
1e825be55fd1
8027043: Turn global accesses into MethodHandle.constant, with one chance of reassignment, e.g. x = value occuring once in the global scope is ok, twice is not.
lagergren
parents:
24727
diff
changeset
|
283 |
expressionGuard, |
1e825be55fd1
8027043: Turn global accesses into MethodHandle.constant, with one chance of reassignment, e.g. x = value occuring once in the global scope is ok, twice is not.
lagergren
parents:
24727
diff
changeset
|
284 |
filterGuardReceiver)); |
16147 | 285 |
} |
286 |
||
24719 | 287 |
private static MethodHandle filterGuardReceiver(final GuardedInvocation link, final MethodHandle receiverFilter) { |
16147 | 288 |
final MethodHandle test = link.getGuard(); |
24720 | 289 |
if (test == null) { |
290 |
return null; |
|
291 |
} |
|
292 |
||
293 |
final Class<?> receiverType = test.type().parameterType(0); |
|
294 |
final MethodHandle filter = MH.asType(receiverFilter, |
|
295 |
receiverFilter.type().changeParameterType(0, receiverType). |
|
296 |
changeReturnType(receiverType)); |
|
297 |
||
298 |
return filterReceiver(test, filter); |
|
16147 | 299 |
} |
300 |
||
24719 | 301 |
private static MethodHandle filterReceiver(final MethodHandle mh, final MethodHandle receiverFilter) { |
24720 | 302 |
//With expression filter == receiverFilter, i.e. receiver is cast to withobject and its expression returned |
24727 | 303 |
return MH.filterArguments(mh, 0, receiverFilter.asType(receiverFilter.type().changeReturnType(mh.type().parameterType(0)))); |
16147 | 304 |
} |
305 |
||
306 |
/** |
|
307 |
* Drops the WithObject wrapper from the expression. |
|
308 |
* @param receiver WithObject wrapper. |
|
309 |
* @return The with expression. |
|
310 |
*/ |
|
311 |
public static Object withFilterExpression(final Object receiver) { |
|
312 |
return ((WithObject)receiver).expression; |
|
313 |
} |
|
314 |
||
315 |
@SuppressWarnings("unused") |
|
316 |
private static Object bindToExpression(final Object fn, final Object receiver) { |
|
25247
c0f911459863
8046013: TypeError: Cannot apply "with" to non script object
sundar
parents:
24733
diff
changeset
|
317 |
if (fn instanceof ScriptFunction) { |
c0f911459863
8046013: TypeError: Cannot apply "with" to non script object
sundar
parents:
24733
diff
changeset
|
318 |
return bindToExpression((ScriptFunction) fn, receiver); |
c0f911459863
8046013: TypeError: Cannot apply "with" to non script object
sundar
parents:
24733
diff
changeset
|
319 |
} else if (fn instanceof ScriptObjectMirror) { |
c0f911459863
8046013: TypeError: Cannot apply "with" to non script object
sundar
parents:
24733
diff
changeset
|
320 |
final ScriptObjectMirror mirror = (ScriptObjectMirror)fn; |
c0f911459863
8046013: TypeError: Cannot apply "with" to non script object
sundar
parents:
24733
diff
changeset
|
321 |
if (mirror.isFunction()) { |
c0f911459863
8046013: TypeError: Cannot apply "with" to non script object
sundar
parents:
24733
diff
changeset
|
322 |
// We need to make sure correct 'this' is used for calls with Ident call |
c0f911459863
8046013: TypeError: Cannot apply "with" to non script object
sundar
parents:
24733
diff
changeset
|
323 |
// expressions. We do so here using an AbstractJSObject instance. |
c0f911459863
8046013: TypeError: Cannot apply "with" to non script object
sundar
parents:
24733
diff
changeset
|
324 |
return new AbstractJSObject() { |
25821
fbb51e67d2a7
8048869: Reduce compile time by about 5% by removing the Class.casts from the AST nodes
lagergren
parents:
25247
diff
changeset
|
325 |
@Override |
25247
c0f911459863
8046013: TypeError: Cannot apply "with" to non script object
sundar
parents:
24733
diff
changeset
|
326 |
public Object call(final Object thiz, final Object... args) { |
c0f911459863
8046013: TypeError: Cannot apply "with" to non script object
sundar
parents:
24733
diff
changeset
|
327 |
return mirror.call(withFilterExpression(receiver), args); |
c0f911459863
8046013: TypeError: Cannot apply "with" to non script object
sundar
parents:
24733
diff
changeset
|
328 |
} |
c0f911459863
8046013: TypeError: Cannot apply "with" to non script object
sundar
parents:
24733
diff
changeset
|
329 |
}; |
c0f911459863
8046013: TypeError: Cannot apply "with" to non script object
sundar
parents:
24733
diff
changeset
|
330 |
} |
c0f911459863
8046013: TypeError: Cannot apply "with" to non script object
sundar
parents:
24733
diff
changeset
|
331 |
} |
c0f911459863
8046013: TypeError: Cannot apply "with" to non script object
sundar
parents:
24733
diff
changeset
|
332 |
|
c0f911459863
8046013: TypeError: Cannot apply "with" to non script object
sundar
parents:
24733
diff
changeset
|
333 |
return fn; |
16780
f23743ec1a93
8011555: Invalid class name in with block with JavaImporter causes MH type mismatch
attila
parents:
16277
diff
changeset
|
334 |
} |
f23743ec1a93
8011555: Invalid class name in with block with JavaImporter causes MH type mismatch
attila
parents:
16277
diff
changeset
|
335 |
|
f23743ec1a93
8011555: Invalid class name in with block with JavaImporter causes MH type mismatch
attila
parents:
16277
diff
changeset
|
336 |
private static Object bindToExpression(final ScriptFunction fn, final Object receiver) { |
24719 | 337 |
return fn.makeBoundFunction(withFilterExpression(receiver), ScriptRuntime.EMPTY_ARRAY); |
16147 | 338 |
} |
339 |
||
23084
6c5c02d1023a
8035948: Redesign property listeners for shared classes
hannesw
parents:
20933
diff
changeset
|
340 |
private MethodHandle expressionGuard(final String name, final ScriptObject owner) { |
19884
1bacbaa1bfc7
8024180: Incorrect handling of expression and parent scope in 'with' statements
sundar
parents:
19621
diff
changeset
|
341 |
final PropertyMap map = expression.getMap(); |
23084
6c5c02d1023a
8035948: Redesign property listeners for shared classes
hannesw
parents:
20933
diff
changeset
|
342 |
final SwitchPoint sp = expression.getProtoSwitchPoint(name, owner); |
19884
1bacbaa1bfc7
8024180: Incorrect handling of expression and parent scope in 'with' statements
sundar
parents:
19621
diff
changeset
|
343 |
return MH.insertArguments(WITHEXPRESSIONGUARD, 1, map, sp); |
1bacbaa1bfc7
8024180: Incorrect handling of expression and parent scope in 'with' statements
sundar
parents:
19621
diff
changeset
|
344 |
} |
1bacbaa1bfc7
8024180: Incorrect handling of expression and parent scope in 'with' statements
sundar
parents:
19621
diff
changeset
|
345 |
|
1bacbaa1bfc7
8024180: Incorrect handling of expression and parent scope in 'with' statements
sundar
parents:
19621
diff
changeset
|
346 |
@SuppressWarnings("unused") |
1bacbaa1bfc7
8024180: Incorrect handling of expression and parent scope in 'with' statements
sundar
parents:
19621
diff
changeset
|
347 |
private static boolean withExpressionGuard(final Object receiver, final PropertyMap map, final SwitchPoint sp) { |
1bacbaa1bfc7
8024180: Incorrect handling of expression and parent scope in 'with' statements
sundar
parents:
19621
diff
changeset
|
348 |
return ((WithObject)receiver).expression.getMap() == map && (sp == null || !sp.hasBeenInvalidated()); |
1bacbaa1bfc7
8024180: Incorrect handling of expression and parent scope in 'with' statements
sundar
parents:
19621
diff
changeset
|
349 |
} |
1bacbaa1bfc7
8024180: Incorrect handling of expression and parent scope in 'with' statements
sundar
parents:
19621
diff
changeset
|
350 |
|
16147 | 351 |
/** |
352 |
* Drops the WithObject wrapper from the scope. |
|
353 |
* @param receiver WithObject wrapper. |
|
354 |
* @return The with scope. |
|
355 |
*/ |
|
356 |
public static Object withFilterScope(final Object receiver) { |
|
357 |
return ((WithObject)receiver).getProto(); |
|
358 |
} |
|
359 |
||
360 |
/** |
|
361 |
* Get the with expression for this {@code WithObject} |
|
362 |
* @return the with expression |
|
363 |
*/ |
|
19884
1bacbaa1bfc7
8024180: Incorrect handling of expression and parent scope in 'with' statements
sundar
parents:
19621
diff
changeset
|
364 |
public ScriptObject getExpression() { |
16147 | 365 |
return expression; |
366 |
} |
|
367 |
||
20933
89748612fd1d
8026250: Logging nullpointer bugfix and javadoc warnings
lagergren
parents:
19884
diff
changeset
|
368 |
/** |
89748612fd1d
8026250: Logging nullpointer bugfix and javadoc warnings
lagergren
parents:
19884
diff
changeset
|
369 |
* Get the parent scope for this {@code WithObject} |
89748612fd1d
8026250: Logging nullpointer bugfix and javadoc warnings
lagergren
parents:
19884
diff
changeset
|
370 |
* @return the parent scope |
89748612fd1d
8026250: Logging nullpointer bugfix and javadoc warnings
lagergren
parents:
19884
diff
changeset
|
371 |
*/ |
19884
1bacbaa1bfc7
8024180: Incorrect handling of expression and parent scope in 'with' statements
sundar
parents:
19621
diff
changeset
|
372 |
public ScriptObject getParentScope() { |
1bacbaa1bfc7
8024180: Incorrect handling of expression and parent scope in 'with' statements
sundar
parents:
19621
diff
changeset
|
373 |
return getProto(); |
1bacbaa1bfc7
8024180: Incorrect handling of expression and parent scope in 'with' statements
sundar
parents:
19621
diff
changeset
|
374 |
} |
1bacbaa1bfc7
8024180: Incorrect handling of expression and parent scope in 'with' statements
sundar
parents:
19621
diff
changeset
|
375 |
|
16147 | 376 |
private static MethodHandle findOwnMH(final String name, final Class<?> rtype, final Class<?>... types) { |
377 |
return MH.findStatic(MethodHandles.lookup(), WithObject.class, name, MH.type(rtype, types)); |
|
378 |
} |
|
379 |
} |