author | sundar |
Wed, 06 Aug 2014 22:11:12 +0530 | |
changeset 26052 | 41d18e9e45a4 |
parent 25828 | 077046a5d726 |
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.codegen.CompilerConstants.staticCall; |
|
29 |
import static jdk.nashorn.internal.codegen.CompilerConstants.staticCallNoLookup; |
|
25250
b5a4e0ac31d1
8047359: large string size RangeError should be thrown rather than reporting negative length
sundar
parents:
25247
diff
changeset
|
30 |
import static jdk.nashorn.internal.runtime.ECMAErrors.rangeError; |
16147 | 31 |
import static jdk.nashorn.internal.runtime.ECMAErrors.referenceError; |
32 |
import static jdk.nashorn.internal.runtime.ECMAErrors.syntaxError; |
|
33 |
import static jdk.nashorn.internal.runtime.ECMAErrors.typeError; |
|
34 |
import static jdk.nashorn.internal.runtime.JSType.isRepresentableAsInt; |
|
35 |
||
36 |
import java.lang.invoke.MethodHandle; |
|
19460
1b6d8e7b1cdf
8022782: publicLookup access failures in ScriptObject, ScriptFunction and ScriptFunction
sundar
parents:
19097
diff
changeset
|
37 |
import java.lang.invoke.MethodHandles; |
16147 | 38 |
import java.lang.reflect.Array; |
39 |
import java.util.Collections; |
|
40 |
import java.util.Iterator; |
|
19618
6b73157185e0
8022903: Enhance for-in and for-each for Lists and Maps
attila
parents:
19460
diff
changeset
|
41 |
import java.util.List; |
17778 | 42 |
import java.util.Locale; |
19618
6b73157185e0
8022903: Enhance for-in and for-each for Lists and Maps
attila
parents:
19460
diff
changeset
|
43 |
import java.util.Map; |
16226
0e4f37e6cc40
8007915: Nashorn IR, codegen, parser packages and Context instance should be inaccessible to user code
sundar
parents:
16206
diff
changeset
|
44 |
import java.util.NoSuchElementException; |
16147 | 45 |
import java.util.Objects; |
16234
86cb162cec6c
8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
16232
diff
changeset
|
46 |
import jdk.internal.dynalink.beans.StaticClass; |
19889
63af9358d0dc
8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents:
19884
diff
changeset
|
47 |
import jdk.nashorn.api.scripting.JSObject; |
17978
750d0582d8e2
8015830: Javascript mapping of ScriptEngine bindings does not expose keys
sundar
parents:
17778
diff
changeset
|
48 |
import jdk.nashorn.api.scripting.ScriptObjectMirror; |
24719 | 49 |
import jdk.nashorn.internal.codegen.CompilerConstants; |
16147 | 50 |
import jdk.nashorn.internal.codegen.CompilerConstants.Call; |
16226
0e4f37e6cc40
8007915: Nashorn IR, codegen, parser packages and Context instance should be inaccessible to user code
sundar
parents:
16206
diff
changeset
|
51 |
import jdk.nashorn.internal.ir.debug.JSONWriter; |
21446
2fcb0f7e407f
8026955: for-in should convert primitive values to object
hannesw
parents:
19889
diff
changeset
|
52 |
import jdk.nashorn.internal.objects.Global; |
25247
c0f911459863
8046013: TypeError: Cannot apply "with" to non script object
sundar
parents:
24762
diff
changeset
|
53 |
import jdk.nashorn.internal.objects.NativeObject; |
16226
0e4f37e6cc40
8007915: Nashorn IR, codegen, parser packages and Context instance should be inaccessible to user code
sundar
parents:
16206
diff
changeset
|
54 |
import jdk.nashorn.internal.parser.Lexer; |
16147 | 55 |
import jdk.nashorn.internal.runtime.linker.Bootstrap; |
56 |
||
57 |
||
58 |
/** |
|
59 |
* Utilities to be called by JavaScript runtime API and generated classes. |
|
60 |
*/ |
|
61 |
||
62 |
public final class ScriptRuntime { |
|
63 |
private ScriptRuntime() { |
|
64 |
} |
|
65 |
||
66 |
/** Singleton representing the empty array object '[]' */ |
|
67 |
public static final Object[] EMPTY_ARRAY = new Object[0]; |
|
68 |
||
69 |
/** Unique instance of undefined. */ |
|
70 |
public static final Undefined UNDEFINED = Undefined.getUndefined(); |
|
71 |
||
72 |
/** |
|
73 |
* Unique instance of undefined used to mark empty array slots. |
|
74 |
* Can't escape the array. |
|
75 |
*/ |
|
76 |
public static final Undefined EMPTY = Undefined.getEmpty(); |
|
77 |
||
78 |
/** Method handle to generic + operator, operating on objects */ |
|
79 |
public static final Call ADD = staticCallNoLookup(ScriptRuntime.class, "ADD", Object.class, Object.class, Object.class); |
|
80 |
||
81 |
/** Method handle to generic === operator, operating on objects */ |
|
82 |
public static final Call EQ_STRICT = staticCallNoLookup(ScriptRuntime.class, "EQ_STRICT", boolean.class, Object.class, Object.class); |
|
83 |
||
16240
e1468b33e201
8008239: Unpublicized parts of the code generator package that were only package internal.
lagergren
parents:
16234
diff
changeset
|
84 |
/** Method handle used to enter a {@code with} scope at runtime. */ |
16147 | 85 |
public static final Call OPEN_WITH = staticCallNoLookup(ScriptRuntime.class, "openWith", ScriptObject.class, ScriptObject.class, Object.class); |
86 |
||
87 |
/** |
|
88 |
* Method used to place a scope's variable into the Global scope, which has to be done for the |
|
16240
e1468b33e201
8008239: Unpublicized parts of the code generator package that were only package internal.
lagergren
parents:
16234
diff
changeset
|
89 |
* properties declared at outermost script level. |
16147 | 90 |
*/ |
91 |
public static final Call MERGE_SCOPE = staticCallNoLookup(ScriptRuntime.class, "mergeScope", ScriptObject.class, ScriptObject.class); |
|
92 |
||
93 |
/** |
|
94 |
* Return an appropriate iterator for the elements in a for-in construct |
|
95 |
*/ |
|
96 |
public static final Call TO_PROPERTY_ITERATOR = staticCallNoLookup(ScriptRuntime.class, "toPropertyIterator", Iterator.class, Object.class); |
|
97 |
||
98 |
/** |
|
99 |
* Return an appropriate iterator for the elements in a for-each construct |
|
100 |
*/ |
|
101 |
public static final Call TO_VALUE_ITERATOR = staticCallNoLookup(ScriptRuntime.class, "toValueIterator", Iterator.class, Object.class); |
|
102 |
||
103 |
/** |
|
104 |
* Method handle for apply. Used from {@link ScriptFunction} for looking up calls to |
|
105 |
* call sites that are known to be megamorphic. Using an invoke dynamic here would |
|
106 |
* lead to the JVM deoptimizing itself to death |
|
107 |
*/ |
|
19460
1b6d8e7b1cdf
8022782: publicLookup access failures in ScriptObject, ScriptFunction and ScriptFunction
sundar
parents:
19097
diff
changeset
|
108 |
public static final Call APPLY = staticCall(MethodHandles.lookup(), ScriptRuntime.class, "apply", Object.class, ScriptFunction.class, Object.class, Object[].class); |
16147 | 109 |
|
110 |
/** |
|
111 |
* Converts a switch tag value to a simple integer. deflt value if it can't. |
|
112 |
* |
|
113 |
* @param tag Switch statement tag value. |
|
114 |
* @param deflt default to use if not convertible. |
|
115 |
* @return int tag value (or deflt.) |
|
116 |
*/ |
|
117 |
public static int switchTagAsInt(final Object tag, final int deflt) { |
|
118 |
if (tag instanceof Number) { |
|
119 |
final double d = ((Number)tag).doubleValue(); |
|
120 |
if (isRepresentableAsInt(d)) { |
|
121 |
return (int)d; |
|
122 |
} |
|
123 |
} |
|
124 |
return deflt; |
|
125 |
} |
|
126 |
||
127 |
/** |
|
128 |
* Converts a switch tag value to a simple integer. deflt value if it can't. |
|
129 |
* |
|
130 |
* @param tag Switch statement tag value. |
|
131 |
* @param deflt default to use if not convertible. |
|
132 |
* @return int tag value (or deflt.) |
|
133 |
*/ |
|
18628
012cd852f881
8019488: switch on literals result in NoSuchMethodError or VerifyError
sundar
parents:
18614
diff
changeset
|
134 |
public static int switchTagAsInt(final boolean tag, final int deflt) { |
012cd852f881
8019488: switch on literals result in NoSuchMethodError or VerifyError
sundar
parents:
18614
diff
changeset
|
135 |
return deflt; |
012cd852f881
8019488: switch on literals result in NoSuchMethodError or VerifyError
sundar
parents:
18614
diff
changeset
|
136 |
} |
012cd852f881
8019488: switch on literals result in NoSuchMethodError or VerifyError
sundar
parents:
18614
diff
changeset
|
137 |
|
012cd852f881
8019488: switch on literals result in NoSuchMethodError or VerifyError
sundar
parents:
18614
diff
changeset
|
138 |
/** |
012cd852f881
8019488: switch on literals result in NoSuchMethodError or VerifyError
sundar
parents:
18614
diff
changeset
|
139 |
* Converts a switch tag value to a simple integer. deflt value if it can't. |
012cd852f881
8019488: switch on literals result in NoSuchMethodError or VerifyError
sundar
parents:
18614
diff
changeset
|
140 |
* |
012cd852f881
8019488: switch on literals result in NoSuchMethodError or VerifyError
sundar
parents:
18614
diff
changeset
|
141 |
* @param tag Switch statement tag value. |
012cd852f881
8019488: switch on literals result in NoSuchMethodError or VerifyError
sundar
parents:
18614
diff
changeset
|
142 |
* @param deflt default to use if not convertible. |
012cd852f881
8019488: switch on literals result in NoSuchMethodError or VerifyError
sundar
parents:
18614
diff
changeset
|
143 |
* @return int tag value (or deflt.) |
012cd852f881
8019488: switch on literals result in NoSuchMethodError or VerifyError
sundar
parents:
18614
diff
changeset
|
144 |
*/ |
16147 | 145 |
public static int switchTagAsInt(final long tag, final int deflt) { |
146 |
return isRepresentableAsInt(tag) ? (int)tag : deflt; |
|
147 |
} |
|
148 |
||
149 |
/** |
|
150 |
* Converts a switch tag value to a simple integer. deflt value if it can't. |
|
151 |
* |
|
152 |
* @param tag Switch statement tag value. |
|
153 |
* @param deflt default to use if not convertible. |
|
154 |
* @return int tag value (or deflt.) |
|
155 |
*/ |
|
156 |
public static int switchTagAsInt(final double tag, final int deflt) { |
|
157 |
return isRepresentableAsInt(tag) ? (int)tag : deflt; |
|
158 |
} |
|
159 |
||
160 |
/** |
|
161 |
* This is the builtin implementation of {@code Object.prototype.toString} |
|
162 |
* @param self reference |
|
163 |
* @return string representation as object |
|
164 |
*/ |
|
165 |
public static String builtinObjectToString(final Object self) { |
|
166 |
String className; |
|
167 |
// Spec tells us to convert primitives by ToObject.. |
|
168 |
// But we don't need to -- all we need is the right class name |
|
169 |
// of the corresponding primitive wrapper type. |
|
170 |
||
24752
c835f368e8e0
8043002: Improve performance of Nashorn equality operators
attila
parents:
24745
diff
changeset
|
171 |
final JSType type = JSType.ofNoFunction(self); |
16147 | 172 |
|
173 |
switch (type) { |
|
174 |
case BOOLEAN: |
|
175 |
className = "Boolean"; |
|
176 |
break; |
|
177 |
case NUMBER: |
|
178 |
className = "Number"; |
|
179 |
break; |
|
180 |
case STRING: |
|
181 |
className = "String"; |
|
182 |
break; |
|
183 |
// special case of null and undefined |
|
184 |
case NULL: |
|
185 |
className = "Null"; |
|
186 |
break; |
|
187 |
case UNDEFINED: |
|
188 |
className = "Undefined"; |
|
189 |
break; |
|
190 |
case OBJECT: |
|
191 |
if (self instanceof ScriptObject) { |
|
192 |
className = ((ScriptObject)self).getClassName(); |
|
19889
63af9358d0dc
8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents:
19884
diff
changeset
|
193 |
} else if (self instanceof JSObject) { |
63af9358d0dc
8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents:
19884
diff
changeset
|
194 |
className = ((JSObject)self).getClassName(); |
16147 | 195 |
} else { |
196 |
className = self.getClass().getName(); |
|
197 |
} |
|
198 |
break; |
|
199 |
default: |
|
200 |
// Nashorn extension: use Java class name |
|
201 |
className = self.getClass().getName(); |
|
202 |
break; |
|
203 |
} |
|
204 |
||
205 |
final StringBuilder sb = new StringBuilder(); |
|
206 |
sb.append("[object "); |
|
207 |
sb.append(className); |
|
208 |
sb.append(']'); |
|
209 |
||
210 |
return sb.toString(); |
|
211 |
} |
|
212 |
||
213 |
/** |
|
214 |
* This is called whenever runtime wants to throw an error and wants to provide |
|
215 |
* meaningful information about an object. We don't want to call toString which |
|
216 |
* ends up calling "toString" from script world which may itself throw error. |
|
217 |
* When we want to throw an error, we don't additional error from script land |
|
218 |
* -- which may sometimes lead to infinite recursion. |
|
219 |
* |
|
220 |
* @param obj Object to converted to String safely (without calling user script) |
|
221 |
* @return safe String representation of the given object |
|
222 |
*/ |
|
223 |
public static String safeToString(final Object obj) { |
|
224 |
return JSType.toStringImpl(obj, true); |
|
225 |
} |
|
226 |
||
227 |
/** |
|
19618
6b73157185e0
8022903: Enhance for-in and for-each for Lists and Maps
attila
parents:
19460
diff
changeset
|
228 |
* Returns an iterator over property identifiers used in the {@code for...in} statement. Note that the ECMAScript |
6b73157185e0
8022903: Enhance for-in and for-each for Lists and Maps
attila
parents:
19460
diff
changeset
|
229 |
* 5.1 specification, chapter 12.6.4. uses the terminology "property names", which seems to imply that the property |
6b73157185e0
8022903: Enhance for-in and for-each for Lists and Maps
attila
parents:
19460
diff
changeset
|
230 |
* identifiers are expected to be strings, but this is not actually spelled out anywhere, and Nashorn will in some |
6b73157185e0
8022903: Enhance for-in and for-each for Lists and Maps
attila
parents:
19460
diff
changeset
|
231 |
* cases deviate from this. Namely, we guarantee to always return an iterator over {@link String} values for any |
6b73157185e0
8022903: Enhance for-in and for-each for Lists and Maps
attila
parents:
19460
diff
changeset
|
232 |
* built-in JavaScript object. We will however return an iterator over {@link Integer} objects for native Java |
6b73157185e0
8022903: Enhance for-in and for-each for Lists and Maps
attila
parents:
19460
diff
changeset
|
233 |
* arrays and {@link List} objects, as well as arbitrary objects representing keys of a {@link Map}. Therefore, the |
6b73157185e0
8022903: Enhance for-in and for-each for Lists and Maps
attila
parents:
19460
diff
changeset
|
234 |
* expression {@code typeof i} within a {@code for(i in obj)} statement can return something other than |
6b73157185e0
8022903: Enhance for-in and for-each for Lists and Maps
attila
parents:
19460
diff
changeset
|
235 |
* {@code string} when iterating over native Java arrays, {@code List}, and {@code Map} objects. |
6b73157185e0
8022903: Enhance for-in and for-each for Lists and Maps
attila
parents:
19460
diff
changeset
|
236 |
* @param obj object to iterate on. |
6b73157185e0
8022903: Enhance for-in and for-each for Lists and Maps
attila
parents:
19460
diff
changeset
|
237 |
* @return iterator over the object's property names. |
16147 | 238 |
*/ |
19618
6b73157185e0
8022903: Enhance for-in and for-each for Lists and Maps
attila
parents:
19460
diff
changeset
|
239 |
public static Iterator<?> toPropertyIterator(final Object obj) { |
16147 | 240 |
if (obj instanceof ScriptObject) { |
241 |
return ((ScriptObject)obj).propertyIterator(); |
|
242 |
} |
|
243 |
||
244 |
if (obj != null && obj.getClass().isArray()) { |
|
19618
6b73157185e0
8022903: Enhance for-in and for-each for Lists and Maps
attila
parents:
19460
diff
changeset
|
245 |
return new RangeIterator(Array.getLength(obj)); |
16147 | 246 |
} |
247 |
||
19889
63af9358d0dc
8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents:
19884
diff
changeset
|
248 |
if (obj instanceof JSObject) { |
63af9358d0dc
8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents:
19884
diff
changeset
|
249 |
return ((JSObject)obj).keySet().iterator(); |
17978
750d0582d8e2
8015830: Javascript mapping of ScriptEngine bindings does not expose keys
sundar
parents:
17778
diff
changeset
|
250 |
} |
750d0582d8e2
8015830: Javascript mapping of ScriptEngine bindings does not expose keys
sundar
parents:
17778
diff
changeset
|
251 |
|
19618
6b73157185e0
8022903: Enhance for-in and for-each for Lists and Maps
attila
parents:
19460
diff
changeset
|
252 |
if (obj instanceof List) { |
6b73157185e0
8022903: Enhance for-in and for-each for Lists and Maps
attila
parents:
19460
diff
changeset
|
253 |
return new RangeIterator(((List<?>)obj).size()); |
6b73157185e0
8022903: Enhance for-in and for-each for Lists and Maps
attila
parents:
19460
diff
changeset
|
254 |
} |
6b73157185e0
8022903: Enhance for-in and for-each for Lists and Maps
attila
parents:
19460
diff
changeset
|
255 |
|
6b73157185e0
8022903: Enhance for-in and for-each for Lists and Maps
attila
parents:
19460
diff
changeset
|
256 |
if (obj instanceof Map) { |
6b73157185e0
8022903: Enhance for-in and for-each for Lists and Maps
attila
parents:
19460
diff
changeset
|
257 |
return ((Map<?,?>)obj).keySet().iterator(); |
6b73157185e0
8022903: Enhance for-in and for-each for Lists and Maps
attila
parents:
19460
diff
changeset
|
258 |
} |
6b73157185e0
8022903: Enhance for-in and for-each for Lists and Maps
attila
parents:
19460
diff
changeset
|
259 |
|
21446
2fcb0f7e407f
8026955: for-in should convert primitive values to object
hannesw
parents:
19889
diff
changeset
|
260 |
final Object wrapped = Global.instance().wrapAsObject(obj); |
2fcb0f7e407f
8026955: for-in should convert primitive values to object
hannesw
parents:
19889
diff
changeset
|
261 |
if (wrapped instanceof ScriptObject) { |
2fcb0f7e407f
8026955: for-in should convert primitive values to object
hannesw
parents:
19889
diff
changeset
|
262 |
return ((ScriptObject)wrapped).propertyIterator(); |
2fcb0f7e407f
8026955: for-in should convert primitive values to object
hannesw
parents:
19889
diff
changeset
|
263 |
} |
2fcb0f7e407f
8026955: for-in should convert primitive values to object
hannesw
parents:
19889
diff
changeset
|
264 |
|
16147 | 265 |
return Collections.emptyIterator(); |
266 |
} |
|
267 |
||
19618
6b73157185e0
8022903: Enhance for-in and for-each for Lists and Maps
attila
parents:
19460
diff
changeset
|
268 |
private static final class RangeIterator implements Iterator<Integer> { |
6b73157185e0
8022903: Enhance for-in and for-each for Lists and Maps
attila
parents:
19460
diff
changeset
|
269 |
private final int length; |
6b73157185e0
8022903: Enhance for-in and for-each for Lists and Maps
attila
parents:
19460
diff
changeset
|
270 |
private int index; |
6b73157185e0
8022903: Enhance for-in and for-each for Lists and Maps
attila
parents:
19460
diff
changeset
|
271 |
|
24725
7bb1f687a852
8033334: Make sure that scope depth information is maintained in the RecompilableScriptFunctionDatas, to avoid unnecessary slow proto linkage when doing on demand compilation
lagergren
parents:
24719
diff
changeset
|
272 |
RangeIterator(final int length) { |
19618
6b73157185e0
8022903: Enhance for-in and for-each for Lists and Maps
attila
parents:
19460
diff
changeset
|
273 |
this.length = length; |
6b73157185e0
8022903: Enhance for-in and for-each for Lists and Maps
attila
parents:
19460
diff
changeset
|
274 |
} |
6b73157185e0
8022903: Enhance for-in and for-each for Lists and Maps
attila
parents:
19460
diff
changeset
|
275 |
|
6b73157185e0
8022903: Enhance for-in and for-each for Lists and Maps
attila
parents:
19460
diff
changeset
|
276 |
@Override |
6b73157185e0
8022903: Enhance for-in and for-each for Lists and Maps
attila
parents:
19460
diff
changeset
|
277 |
public boolean hasNext() { |
6b73157185e0
8022903: Enhance for-in and for-each for Lists and Maps
attila
parents:
19460
diff
changeset
|
278 |
return index < length; |
6b73157185e0
8022903: Enhance for-in and for-each for Lists and Maps
attila
parents:
19460
diff
changeset
|
279 |
} |
6b73157185e0
8022903: Enhance for-in and for-each for Lists and Maps
attila
parents:
19460
diff
changeset
|
280 |
|
6b73157185e0
8022903: Enhance for-in and for-each for Lists and Maps
attila
parents:
19460
diff
changeset
|
281 |
@Override |
6b73157185e0
8022903: Enhance for-in and for-each for Lists and Maps
attila
parents:
19460
diff
changeset
|
282 |
public Integer next() { |
6b73157185e0
8022903: Enhance for-in and for-each for Lists and Maps
attila
parents:
19460
diff
changeset
|
283 |
return index++; |
6b73157185e0
8022903: Enhance for-in and for-each for Lists and Maps
attila
parents:
19460
diff
changeset
|
284 |
} |
6b73157185e0
8022903: Enhance for-in and for-each for Lists and Maps
attila
parents:
19460
diff
changeset
|
285 |
|
6b73157185e0
8022903: Enhance for-in and for-each for Lists and Maps
attila
parents:
19460
diff
changeset
|
286 |
@Override |
6b73157185e0
8022903: Enhance for-in and for-each for Lists and Maps
attila
parents:
19460
diff
changeset
|
287 |
public void remove() { |
6b73157185e0
8022903: Enhance for-in and for-each for Lists and Maps
attila
parents:
19460
diff
changeset
|
288 |
throw new UnsupportedOperationException(); |
6b73157185e0
8022903: Enhance for-in and for-each for Lists and Maps
attila
parents:
19460
diff
changeset
|
289 |
} |
6b73157185e0
8022903: Enhance for-in and for-each for Lists and Maps
attila
parents:
19460
diff
changeset
|
290 |
} |
6b73157185e0
8022903: Enhance for-in and for-each for Lists and Maps
attila
parents:
19460
diff
changeset
|
291 |
|
16147 | 292 |
/** |
19618
6b73157185e0
8022903: Enhance for-in and for-each for Lists and Maps
attila
parents:
19460
diff
changeset
|
293 |
* Returns an iterator over property values used in the {@code for each...in} statement. Aside from built-in JS |
6b73157185e0
8022903: Enhance for-in and for-each for Lists and Maps
attila
parents:
19460
diff
changeset
|
294 |
* objects, it also operates on Java arrays, any {@link Iterable}, as well as on {@link Map} objects, iterating over |
6b73157185e0
8022903: Enhance for-in and for-each for Lists and Maps
attila
parents:
19460
diff
changeset
|
295 |
* map values. |
6b73157185e0
8022903: Enhance for-in and for-each for Lists and Maps
attila
parents:
19460
diff
changeset
|
296 |
* @param obj object to iterate on. |
6b73157185e0
8022903: Enhance for-in and for-each for Lists and Maps
attila
parents:
19460
diff
changeset
|
297 |
* @return iterator over the object's property values. |
16147 | 298 |
*/ |
299 |
public static Iterator<?> toValueIterator(final Object obj) { |
|
300 |
if (obj instanceof ScriptObject) { |
|
301 |
return ((ScriptObject)obj).valueIterator(); |
|
302 |
} |
|
303 |
||
304 |
if (obj != null && obj.getClass().isArray()) { |
|
305 |
final Object array = obj; |
|
306 |
final int length = Array.getLength(obj); |
|
307 |
||
308 |
return new Iterator<Object>() { |
|
309 |
private int index = 0; |
|
310 |
||
311 |
@Override |
|
312 |
public boolean hasNext() { |
|
313 |
return index < length; |
|
314 |
} |
|
315 |
||
316 |
@Override |
|
317 |
public Object next() { |
|
16226
0e4f37e6cc40
8007915: Nashorn IR, codegen, parser packages and Context instance should be inaccessible to user code
sundar
parents:
16206
diff
changeset
|
318 |
if (index >= length) { |
0e4f37e6cc40
8007915: Nashorn IR, codegen, parser packages and Context instance should be inaccessible to user code
sundar
parents:
16206
diff
changeset
|
319 |
throw new NoSuchElementException(); |
0e4f37e6cc40
8007915: Nashorn IR, codegen, parser packages and Context instance should be inaccessible to user code
sundar
parents:
16206
diff
changeset
|
320 |
} |
16147 | 321 |
return Array.get(array, index++); |
322 |
} |
|
323 |
||
324 |
@Override |
|
325 |
public void remove() { |
|
326 |
throw new UnsupportedOperationException(); |
|
327 |
} |
|
328 |
}; |
|
329 |
} |
|
330 |
||
19889
63af9358d0dc
8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents:
19884
diff
changeset
|
331 |
if (obj instanceof JSObject) { |
63af9358d0dc
8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents:
19884
diff
changeset
|
332 |
return ((JSObject)obj).values().iterator(); |
17978
750d0582d8e2
8015830: Javascript mapping of ScriptEngine bindings does not expose keys
sundar
parents:
17778
diff
changeset
|
333 |
} |
750d0582d8e2
8015830: Javascript mapping of ScriptEngine bindings does not expose keys
sundar
parents:
17778
diff
changeset
|
334 |
|
19618
6b73157185e0
8022903: Enhance for-in and for-each for Lists and Maps
attila
parents:
19460
diff
changeset
|
335 |
if (obj instanceof Map) { |
6b73157185e0
8022903: Enhance for-in and for-each for Lists and Maps
attila
parents:
19460
diff
changeset
|
336 |
return ((Map<?,?>)obj).values().iterator(); |
6b73157185e0
8022903: Enhance for-in and for-each for Lists and Maps
attila
parents:
19460
diff
changeset
|
337 |
} |
6b73157185e0
8022903: Enhance for-in and for-each for Lists and Maps
attila
parents:
19460
diff
changeset
|
338 |
|
16147 | 339 |
if (obj instanceof Iterable) { |
340 |
return ((Iterable<?>)obj).iterator(); |
|
341 |
} |
|
342 |
||
21446
2fcb0f7e407f
8026955: for-in should convert primitive values to object
hannesw
parents:
19889
diff
changeset
|
343 |
final Object wrapped = Global.instance().wrapAsObject(obj); |
2fcb0f7e407f
8026955: for-in should convert primitive values to object
hannesw
parents:
19889
diff
changeset
|
344 |
if (wrapped instanceof ScriptObject) { |
2fcb0f7e407f
8026955: for-in should convert primitive values to object
hannesw
parents:
19889
diff
changeset
|
345 |
return ((ScriptObject)wrapped).valueIterator(); |
2fcb0f7e407f
8026955: for-in should convert primitive values to object
hannesw
parents:
19889
diff
changeset
|
346 |
} |
2fcb0f7e407f
8026955: for-in should convert primitive values to object
hannesw
parents:
19889
diff
changeset
|
347 |
|
16147 | 348 |
return Collections.emptyIterator(); |
349 |
} |
|
350 |
||
351 |
/** |
|
352 |
* Merge a scope into its prototype's map. |
|
353 |
* Merge a scope into its prototype. |
|
354 |
* |
|
355 |
* @param scope Scope to merge. |
|
356 |
* @return prototype object after merge |
|
357 |
*/ |
|
358 |
public static ScriptObject mergeScope(final ScriptObject scope) { |
|
359 |
final ScriptObject global = scope.getProto(); |
|
360 |
global.addBoundProperties(scope); |
|
361 |
return global; |
|
362 |
} |
|
363 |
||
364 |
/** |
|
365 |
* Call a function given self and args. If the number of the arguments is known in advance, you can likely achieve |
|
366 |
* better performance by {@link Bootstrap#createDynamicInvoker(String, Class, Class...) creating a dynamic invoker} |
|
367 |
* for operation {@code "dyn:call"}, then using its {@link MethodHandle#invokeExact(Object...)} method instead. |
|
368 |
* |
|
369 |
* @param target ScriptFunction object. |
|
370 |
* @param self Receiver in call. |
|
371 |
* @param args Call arguments. |
|
372 |
* @return Call result. |
|
373 |
*/ |
|
374 |
public static Object apply(final ScriptFunction target, final Object self, final Object... args) { |
|
375 |
try { |
|
376 |
return target.invoke(self, args); |
|
377 |
} catch (final RuntimeException | Error e) { |
|
378 |
throw e; |
|
379 |
} catch (final Throwable t) { |
|
380 |
throw new RuntimeException(t); |
|
381 |
} |
|
382 |
} |
|
383 |
||
384 |
/** |
|
18614 | 385 |
* Call a script function as a constructor with given args. |
386 |
* |
|
387 |
* @param target ScriptFunction object. |
|
388 |
* @param args Call arguments. |
|
389 |
* @return Constructor call result. |
|
390 |
*/ |
|
391 |
public static Object construct(final ScriptFunction target, final Object... args) { |
|
392 |
try { |
|
393 |
return target.construct(args); |
|
394 |
} catch (final RuntimeException | Error e) { |
|
395 |
throw e; |
|
396 |
} catch (final Throwable t) { |
|
397 |
throw new RuntimeException(t); |
|
398 |
} |
|
399 |
} |
|
400 |
||
401 |
/** |
|
16147 | 402 |
* Generic implementation of ECMA 9.12 - SameValue algorithm |
403 |
* |
|
404 |
* @param x first value to compare |
|
405 |
* @param y second value to compare |
|
406 |
* |
|
407 |
* @return true if both objects have the same value |
|
408 |
*/ |
|
409 |
public static boolean sameValue(final Object x, final Object y) { |
|
24752
c835f368e8e0
8043002: Improve performance of Nashorn equality operators
attila
parents:
24745
diff
changeset
|
410 |
final JSType xType = JSType.ofNoFunction(x); |
c835f368e8e0
8043002: Improve performance of Nashorn equality operators
attila
parents:
24745
diff
changeset
|
411 |
final JSType yType = JSType.ofNoFunction(y); |
16147 | 412 |
|
413 |
if (xType != yType) { |
|
414 |
return false; |
|
415 |
} |
|
416 |
||
417 |
if (xType == JSType.UNDEFINED || xType == JSType.NULL) { |
|
418 |
return true; |
|
419 |
} |
|
420 |
||
421 |
if (xType == JSType.NUMBER) { |
|
422 |
final double xVal = ((Number)x).doubleValue(); |
|
423 |
final double yVal = ((Number)y).doubleValue(); |
|
424 |
||
425 |
if (Double.isNaN(xVal) && Double.isNaN(yVal)) { |
|
426 |
return true; |
|
427 |
} |
|
428 |
||
429 |
// checking for xVal == -0.0 and yVal == +0.0 or vice versa |
|
24725
7bb1f687a852
8033334: Make sure that scope depth information is maintained in the RecompilableScriptFunctionDatas, to avoid unnecessary slow proto linkage when doing on demand compilation
lagergren
parents:
24719
diff
changeset
|
430 |
if (xVal == 0.0 && Double.doubleToLongBits(xVal) != Double.doubleToLongBits(yVal)) { |
16147 | 431 |
return false; |
432 |
} |
|
433 |
||
434 |
return xVal == yVal; |
|
435 |
} |
|
436 |
||
437 |
if (xType == JSType.STRING || yType == JSType.BOOLEAN) { |
|
438 |
return x.equals(y); |
|
439 |
} |
|
440 |
||
24725
7bb1f687a852
8033334: Make sure that scope depth information is maintained in the RecompilableScriptFunctionDatas, to avoid unnecessary slow proto linkage when doing on demand compilation
lagergren
parents:
24719
diff
changeset
|
441 |
return x == y; |
16147 | 442 |
} |
443 |
||
444 |
/** |
|
16226
0e4f37e6cc40
8007915: Nashorn IR, codegen, parser packages and Context instance should be inaccessible to user code
sundar
parents:
16206
diff
changeset
|
445 |
* Returns AST as JSON compatible string. This is used to |
0e4f37e6cc40
8007915: Nashorn IR, codegen, parser packages and Context instance should be inaccessible to user code
sundar
parents:
16206
diff
changeset
|
446 |
* implement "parse" function in resources/parse.js script. |
0e4f37e6cc40
8007915: Nashorn IR, codegen, parser packages and Context instance should be inaccessible to user code
sundar
parents:
16206
diff
changeset
|
447 |
* |
0e4f37e6cc40
8007915: Nashorn IR, codegen, parser packages and Context instance should be inaccessible to user code
sundar
parents:
16206
diff
changeset
|
448 |
* @param code code to be parsed |
0e4f37e6cc40
8007915: Nashorn IR, codegen, parser packages and Context instance should be inaccessible to user code
sundar
parents:
16206
diff
changeset
|
449 |
* @param name name of the code source (used for location) |
0e4f37e6cc40
8007915: Nashorn IR, codegen, parser packages and Context instance should be inaccessible to user code
sundar
parents:
16206
diff
changeset
|
450 |
* @param includeLoc tells whether to include location information for nodes or not |
0e4f37e6cc40
8007915: Nashorn IR, codegen, parser packages and Context instance should be inaccessible to user code
sundar
parents:
16206
diff
changeset
|
451 |
* @return JSON string representation of AST of the supplied code |
0e4f37e6cc40
8007915: Nashorn IR, codegen, parser packages and Context instance should be inaccessible to user code
sundar
parents:
16206
diff
changeset
|
452 |
*/ |
0e4f37e6cc40
8007915: Nashorn IR, codegen, parser packages and Context instance should be inaccessible to user code
sundar
parents:
16206
diff
changeset
|
453 |
public static String parse(final String code, final String name, final boolean includeLoc) { |
24745
3a6e1477362b
8041434: Add synchronization to the common global constants structure
lagergren
parents:
24733
diff
changeset
|
454 |
return JSONWriter.parse(Context.getContextTrusted(), code, name, includeLoc); |
16226
0e4f37e6cc40
8007915: Nashorn IR, codegen, parser packages and Context instance should be inaccessible to user code
sundar
parents:
16206
diff
changeset
|
455 |
} |
0e4f37e6cc40
8007915: Nashorn IR, codegen, parser packages and Context instance should be inaccessible to user code
sundar
parents:
16206
diff
changeset
|
456 |
|
0e4f37e6cc40
8007915: Nashorn IR, codegen, parser packages and Context instance should be inaccessible to user code
sundar
parents:
16206
diff
changeset
|
457 |
/** |
0e4f37e6cc40
8007915: Nashorn IR, codegen, parser packages and Context instance should be inaccessible to user code
sundar
parents:
16206
diff
changeset
|
458 |
* Test whether a char is valid JavaScript whitespace |
0e4f37e6cc40
8007915: Nashorn IR, codegen, parser packages and Context instance should be inaccessible to user code
sundar
parents:
16206
diff
changeset
|
459 |
* @param ch a char |
0e4f37e6cc40
8007915: Nashorn IR, codegen, parser packages and Context instance should be inaccessible to user code
sundar
parents:
16206
diff
changeset
|
460 |
* @return true if valid JavaScript whitespace |
0e4f37e6cc40
8007915: Nashorn IR, codegen, parser packages and Context instance should be inaccessible to user code
sundar
parents:
16206
diff
changeset
|
461 |
*/ |
0e4f37e6cc40
8007915: Nashorn IR, codegen, parser packages and Context instance should be inaccessible to user code
sundar
parents:
16206
diff
changeset
|
462 |
public static boolean isJSWhitespace(final char ch) { |
0e4f37e6cc40
8007915: Nashorn IR, codegen, parser packages and Context instance should be inaccessible to user code
sundar
parents:
16206
diff
changeset
|
463 |
return Lexer.isJSWhitespace(ch); |
0e4f37e6cc40
8007915: Nashorn IR, codegen, parser packages and Context instance should be inaccessible to user code
sundar
parents:
16206
diff
changeset
|
464 |
} |
0e4f37e6cc40
8007915: Nashorn IR, codegen, parser packages and Context instance should be inaccessible to user code
sundar
parents:
16206
diff
changeset
|
465 |
|
0e4f37e6cc40
8007915: Nashorn IR, codegen, parser packages and Context instance should be inaccessible to user code
sundar
parents:
16206
diff
changeset
|
466 |
/** |
24719 | 467 |
* Entering a {@code with} node requires new scope. This is the implementation. When exiting the with statement, |
468 |
* use {@link ScriptObject#getProto()} on the scope. |
|
16147 | 469 |
* |
470 |
* @param scope existing scope |
|
471 |
* @param expression expression in with |
|
472 |
* |
|
473 |
* @return {@link WithObject} that is the new scope |
|
474 |
*/ |
|
475 |
public static ScriptObject openWith(final ScriptObject scope, final Object expression) { |
|
23375
a1110f2cbe75
8037400: Remove getInitialMap getters and GlobalObject interface
sundar
parents:
21446
diff
changeset
|
476 |
final Global global = Context.getGlobal(); |
16147 | 477 |
if (expression == UNDEFINED) { |
16256
f2d9a0c49914
8007002: Replace implicit exception throwing methods with explicit throws - simplify control flow and remove useless code
lagergren
parents:
16240
diff
changeset
|
478 |
throw typeError(global, "cant.apply.with.to.undefined"); |
16147 | 479 |
} else if (expression == null) { |
16256
f2d9a0c49914
8007002: Replace implicit exception throwing methods with explicit throws - simplify control flow and remove useless code
lagergren
parents:
16240
diff
changeset
|
480 |
throw typeError(global, "cant.apply.with.to.null"); |
16147 | 481 |
} |
482 |
||
25247
c0f911459863
8046013: TypeError: Cannot apply "with" to non script object
sundar
parents:
24762
diff
changeset
|
483 |
if (expression instanceof ScriptObjectMirror) { |
c0f911459863
8046013: TypeError: Cannot apply "with" to non script object
sundar
parents:
24762
diff
changeset
|
484 |
final Object unwrapped = ScriptObjectMirror.unwrap(expression, global); |
c0f911459863
8046013: TypeError: Cannot apply "with" to non script object
sundar
parents:
24762
diff
changeset
|
485 |
if (unwrapped instanceof ScriptObject) { |
c0f911459863
8046013: TypeError: Cannot apply "with" to non script object
sundar
parents:
24762
diff
changeset
|
486 |
return new WithObject(scope, (ScriptObject)unwrapped); |
c0f911459863
8046013: TypeError: Cannot apply "with" to non script object
sundar
parents:
24762
diff
changeset
|
487 |
} |
25821
fbb51e67d2a7
8048869: Reduce compile time by about 5% by removing the Class.casts from the AST nodes
lagergren
parents:
25250
diff
changeset
|
488 |
// foreign ScriptObjectMirror |
fbb51e67d2a7
8048869: Reduce compile time by about 5% by removing the Class.casts from the AST nodes
lagergren
parents:
25250
diff
changeset
|
489 |
final ScriptObject exprObj = global.newObject(); |
fbb51e67d2a7
8048869: Reduce compile time by about 5% by removing the Class.casts from the AST nodes
lagergren
parents:
25250
diff
changeset
|
490 |
NativeObject.bindAllProperties(exprObj, (ScriptObjectMirror)expression); |
fbb51e67d2a7
8048869: Reduce compile time by about 5% by removing the Class.casts from the AST nodes
lagergren
parents:
25250
diff
changeset
|
491 |
return new WithObject(scope, exprObj); |
fbb51e67d2a7
8048869: Reduce compile time by about 5% by removing the Class.casts from the AST nodes
lagergren
parents:
25250
diff
changeset
|
492 |
} |
fbb51e67d2a7
8048869: Reduce compile time by about 5% by removing the Class.casts from the AST nodes
lagergren
parents:
25250
diff
changeset
|
493 |
|
fbb51e67d2a7
8048869: Reduce compile time by about 5% by removing the Class.casts from the AST nodes
lagergren
parents:
25250
diff
changeset
|
494 |
final Object wrappedExpr = JSType.toScriptObject(global, expression); |
fbb51e67d2a7
8048869: Reduce compile time by about 5% by removing the Class.casts from the AST nodes
lagergren
parents:
25250
diff
changeset
|
495 |
if (wrappedExpr instanceof ScriptObject) { |
fbb51e67d2a7
8048869: Reduce compile time by about 5% by removing the Class.casts from the AST nodes
lagergren
parents:
25250
diff
changeset
|
496 |
return new WithObject(scope, (ScriptObject)wrappedExpr); |
19884
1bacbaa1bfc7
8024180: Incorrect handling of expression and parent scope in 'with' statements
sundar
parents:
19638
diff
changeset
|
497 |
} |
16147 | 498 |
|
19884
1bacbaa1bfc7
8024180: Incorrect handling of expression and parent scope in 'with' statements
sundar
parents:
19638
diff
changeset
|
499 |
throw typeError(global, "cant.apply.with.to.non.scriptobject"); |
16147 | 500 |
} |
501 |
||
502 |
/** |
|
503 |
* ECMA 11.6.1 - The addition operator (+) - generic implementation |
|
504 |
* Compiler specializes using {@link jdk.nashorn.internal.codegen.RuntimeCallSite} |
|
505 |
* if any type information is available for any of the operands |
|
506 |
* |
|
507 |
* @param x first term |
|
508 |
* @param y second term |
|
509 |
* |
|
510 |
* @return result of addition |
|
511 |
*/ |
|
512 |
public static Object ADD(final Object x, final Object y) { |
|
513 |
// This prefix code to handle Number special is for optimization. |
|
514 |
final boolean xIsNumber = x instanceof Number; |
|
515 |
final boolean yIsNumber = y instanceof Number; |
|
516 |
||
517 |
if (xIsNumber && yIsNumber) { |
|
518 |
return ((Number)x).doubleValue() + ((Number)y).doubleValue(); |
|
519 |
} |
|
520 |
||
521 |
final boolean xIsUndefined = x == UNDEFINED; |
|
522 |
final boolean yIsUndefined = y == UNDEFINED; |
|
523 |
||
24725
7bb1f687a852
8033334: Make sure that scope depth information is maintained in the RecompilableScriptFunctionDatas, to avoid unnecessary slow proto linkage when doing on demand compilation
lagergren
parents:
24719
diff
changeset
|
524 |
if (xIsNumber && yIsUndefined || xIsUndefined && yIsNumber || xIsUndefined && yIsUndefined) { |
16147 | 525 |
return Double.NaN; |
526 |
} |
|
527 |
||
528 |
// code below is as per the spec. |
|
529 |
final Object xPrim = JSType.toPrimitive(x); |
|
530 |
final Object yPrim = JSType.toPrimitive(y); |
|
531 |
||
532 |
if (xPrim instanceof String || yPrim instanceof String |
|
533 |
|| xPrim instanceof ConsString || yPrim instanceof ConsString) { |
|
25250
b5a4e0ac31d1
8047359: large string size RangeError should be thrown rather than reporting negative length
sundar
parents:
25247
diff
changeset
|
534 |
try { |
b5a4e0ac31d1
8047359: large string size RangeError should be thrown rather than reporting negative length
sundar
parents:
25247
diff
changeset
|
535 |
return new ConsString(JSType.toCharSequence(xPrim), JSType.toCharSequence(yPrim)); |
b5a4e0ac31d1
8047359: large string size RangeError should be thrown rather than reporting negative length
sundar
parents:
25247
diff
changeset
|
536 |
} catch (final IllegalArgumentException iae) { |
b5a4e0ac31d1
8047359: large string size RangeError should be thrown rather than reporting negative length
sundar
parents:
25247
diff
changeset
|
537 |
throw rangeError(iae, "concat.string.too.big"); |
b5a4e0ac31d1
8047359: large string size RangeError should be thrown rather than reporting negative length
sundar
parents:
25247
diff
changeset
|
538 |
} |
16147 | 539 |
} |
540 |
||
541 |
return JSType.toNumber(xPrim) + JSType.toNumber(yPrim); |
|
542 |
} |
|
543 |
||
544 |
/** |
|
545 |
* Debugger hook. |
|
546 |
* TODO: currently unimplemented |
|
547 |
* |
|
548 |
* @return undefined |
|
549 |
*/ |
|
550 |
public static Object DEBUGGER() { |
|
551 |
return UNDEFINED; |
|
552 |
} |
|
553 |
||
554 |
/** |
|
555 |
* New hook |
|
556 |
* |
|
557 |
* @param clazz type for the clss |
|
558 |
* @param args constructor arguments |
|
559 |
* |
|
560 |
* @return undefined |
|
561 |
*/ |
|
562 |
public static Object NEW(final Object clazz, final Object... args) { |
|
563 |
return UNDEFINED; |
|
564 |
} |
|
565 |
||
566 |
/** |
|
567 |
* ECMA 11.4.3 The typeof Operator - generic implementation |
|
568 |
* |
|
569 |
* @param object the object from which to retrieve property to type check |
|
570 |
* @param property property in object to check |
|
571 |
* |
|
572 |
* @return type name |
|
573 |
*/ |
|
574 |
public static Object TYPEOF(final Object object, final Object property) { |
|
575 |
Object obj = object; |
|
576 |
||
577 |
if (property != null) { |
|
578 |
if (obj instanceof ScriptObject) { |
|
579 |
obj = ((ScriptObject)obj).get(property); |
|
24719 | 580 |
if(Global.isLocationPropertyPlaceholder(obj)) { |
581 |
if(CompilerConstants.__LINE__.name().equals(property)) { |
|
582 |
obj = Integer.valueOf(0); |
|
583 |
} else { |
|
584 |
obj = ""; |
|
585 |
} |
|
586 |
} |
|
16147 | 587 |
} else if (object instanceof Undefined) { |
588 |
obj = ((Undefined)obj).get(property); |
|
589 |
} else if (object == null) { |
|
16256
f2d9a0c49914
8007002: Replace implicit exception throwing methods with explicit throws - simplify control flow and remove useless code
lagergren
parents:
16240
diff
changeset
|
590 |
throw typeError("cant.get.property", safeToString(property), "null"); |
16147 | 591 |
} else if (JSType.isPrimitive(obj)) { |
16188 | 592 |
obj = ((ScriptObject)JSType.toScriptObject(obj)).get(property); |
19889
63af9358d0dc
8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents:
19884
diff
changeset
|
593 |
} else if (obj instanceof JSObject) { |
63af9358d0dc
8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents:
19884
diff
changeset
|
594 |
obj = ((JSObject)obj).getMember(property.toString()); |
16147 | 595 |
} else { |
596 |
obj = UNDEFINED; |
|
597 |
} |
|
598 |
} |
|
599 |
||
600 |
return JSType.of(obj).typeName(); |
|
601 |
} |
|
602 |
||
603 |
/** |
|
604 |
* Throw ReferenceError when LHS of assignment or increment/decrement |
|
605 |
* operator is not an assignable node (say a literal) |
|
606 |
* |
|
607 |
* @param lhs Evaluated LHS |
|
608 |
* @param rhs Evaluated RHS |
|
609 |
* @param msg Additional LHS info for error message |
|
610 |
* @return undefined |
|
611 |
*/ |
|
612 |
public static Object REFERENCE_ERROR(final Object lhs, final Object rhs, final Object msg) { |
|
16256
f2d9a0c49914
8007002: Replace implicit exception throwing methods with explicit throws - simplify control flow and remove useless code
lagergren
parents:
16240
diff
changeset
|
613 |
throw referenceError("cant.be.used.as.lhs", Objects.toString(msg)); |
16147 | 614 |
} |
615 |
||
616 |
/** |
|
617 |
* ECMA 11.4.1 - delete operation, generic implementation |
|
618 |
* |
|
619 |
* @param obj object with property to delete |
|
620 |
* @param property property to delete |
|
621 |
* @param strict are we in strict mode |
|
622 |
* |
|
623 |
* @return true if property was successfully found and deleted |
|
624 |
*/ |
|
625 |
public static boolean DELETE(final Object obj, final Object property, final Object strict) { |
|
626 |
if (obj instanceof ScriptObject) { |
|
627 |
return ((ScriptObject)obj).delete(property, Boolean.TRUE.equals(strict)); |
|
628 |
} |
|
629 |
||
630 |
if (obj instanceof Undefined) { |
|
631 |
return ((Undefined)obj).delete(property, false); |
|
632 |
} |
|
633 |
||
634 |
if (obj == null) { |
|
16256
f2d9a0c49914
8007002: Replace implicit exception throwing methods with explicit throws - simplify control flow and remove useless code
lagergren
parents:
16240
diff
changeset
|
635 |
throw typeError("cant.delete.property", safeToString(property), "null"); |
16147 | 636 |
} |
637 |
||
17978
750d0582d8e2
8015830: Javascript mapping of ScriptEngine bindings does not expose keys
sundar
parents:
17778
diff
changeset
|
638 |
if (obj instanceof ScriptObjectMirror) { |
750d0582d8e2
8015830: Javascript mapping of ScriptEngine bindings does not expose keys
sundar
parents:
17778
diff
changeset
|
639 |
return ((ScriptObjectMirror)obj).delete(property); |
750d0582d8e2
8015830: Javascript mapping of ScriptEngine bindings does not expose keys
sundar
parents:
17778
diff
changeset
|
640 |
} |
750d0582d8e2
8015830: Javascript mapping of ScriptEngine bindings does not expose keys
sundar
parents:
17778
diff
changeset
|
641 |
|
16147 | 642 |
if (JSType.isPrimitive(obj)) { |
16188 | 643 |
return ((ScriptObject) JSType.toScriptObject(obj)).delete(property, Boolean.TRUE.equals(strict)); |
16147 | 644 |
} |
645 |
||
19889
63af9358d0dc
8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents:
19884
diff
changeset
|
646 |
if (obj instanceof JSObject) { |
63af9358d0dc
8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents:
19884
diff
changeset
|
647 |
((JSObject)obj).removeMember(Objects.toString(property)); |
63af9358d0dc
8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents:
19884
diff
changeset
|
648 |
return true; |
63af9358d0dc
8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents:
19884
diff
changeset
|
649 |
} |
63af9358d0dc
8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents:
19884
diff
changeset
|
650 |
|
16147 | 651 |
// if object is not reference type, vacuously delete is successful. |
652 |
return true; |
|
653 |
} |
|
654 |
||
655 |
/** |
|
656 |
* ECMA 11.4.1 - delete operator, special case |
|
657 |
* |
|
658 |
* This is 'delete' that always fails. We have to check strict mode and throw error. |
|
659 |
* That is why this is a runtime function. Or else we could have inlined 'false'. |
|
660 |
* |
|
661 |
* @param property property to delete |
|
662 |
* @param strict are we in strict mode |
|
663 |
* |
|
664 |
* @return false always |
|
665 |
*/ |
|
16206
83069fa0935b
8006529: Methods always get callee - it should be conditional
attila
parents:
16188
diff
changeset
|
666 |
public static boolean FAIL_DELETE(final Object property, final Object strict) { |
16147 | 667 |
if (Boolean.TRUE.equals(strict)) { |
16256
f2d9a0c49914
8007002: Replace implicit exception throwing methods with explicit throws - simplify control flow and remove useless code
lagergren
parents:
16240
diff
changeset
|
668 |
throw syntaxError("strict.cant.delete", safeToString(property)); |
16147 | 669 |
} |
670 |
return false; |
|
671 |
} |
|
672 |
||
673 |
/** |
|
674 |
* ECMA 11.9.1 - The equals operator (==) - generic implementation |
|
675 |
* |
|
676 |
* @param x first object to compare |
|
677 |
* @param y second object to compare |
|
678 |
* |
|
679 |
* @return true if type coerced versions of objects are equal |
|
680 |
*/ |
|
681 |
public static boolean EQ(final Object x, final Object y) { |
|
682 |
return equals(x, y); |
|
683 |
} |
|
684 |
||
685 |
/** |
|
686 |
* ECMA 11.9.2 - The does-not-equal operator (==) - generic implementation |
|
687 |
* |
|
688 |
* @param x first object to compare |
|
689 |
* @param y second object to compare |
|
690 |
* |
|
691 |
* @return true if type coerced versions of objects are not equal |
|
692 |
*/ |
|
693 |
public static boolean NE(final Object x, final Object y) { |
|
694 |
return !EQ(x, y); |
|
695 |
} |
|
696 |
||
697 |
/** ECMA 11.9.3 The Abstract Equality Comparison Algorithm */ |
|
698 |
private static boolean equals(final Object x, final Object y) { |
|
24762 | 699 |
if (x == y) { |
700 |
return true; |
|
701 |
} |
|
702 |
if (x instanceof ScriptObject && y instanceof ScriptObject) { |
|
703 |
return x == y; |
|
704 |
} |
|
26052
41d18e9e45a4
8053910: ScriptObjectMirror causing havoc with Invocation interface
sundar
parents:
25828
diff
changeset
|
705 |
if (x instanceof ScriptObjectMirror || y instanceof ScriptObjectMirror) { |
41d18e9e45a4
8053910: ScriptObjectMirror causing havoc with Invocation interface
sundar
parents:
25828
diff
changeset
|
706 |
return ScriptObjectMirror.identical(x, y); |
41d18e9e45a4
8053910: ScriptObjectMirror causing havoc with Invocation interface
sundar
parents:
25828
diff
changeset
|
707 |
} |
24762 | 708 |
return equalValues(x, y); |
709 |
} |
|
710 |
||
711 |
/** |
|
712 |
* Extracted portion of {@code equals()} that compares objects by value (or by reference, if no known value |
|
713 |
* comparison applies). |
|
714 |
* @param x one value |
|
715 |
* @param y another value |
|
716 |
* @return true if they're equal according to 11.9.3 |
|
717 |
*/ |
|
718 |
private static boolean equalValues(final Object x, final Object y) { |
|
24752
c835f368e8e0
8043002: Improve performance of Nashorn equality operators
attila
parents:
24745
diff
changeset
|
719 |
final JSType xType = JSType.ofNoFunction(x); |
c835f368e8e0
8043002: Improve performance of Nashorn equality operators
attila
parents:
24745
diff
changeset
|
720 |
final JSType yType = JSType.ofNoFunction(y); |
16147 | 721 |
|
722 |
if (xType == yType) { |
|
24762 | 723 |
return equalSameTypeValues(x, y, xType); |
724 |
} |
|
16147 | 725 |
|
24762 | 726 |
return equalDifferentTypeValues(x, y, xType, yType); |
727 |
} |
|
16147 | 728 |
|
24762 | 729 |
/** |
730 |
* Extracted portion of {@link #equals(Object, Object)} and {@link #strictEquals(Object, Object)} that compares |
|
731 |
* values belonging to the same JSType. |
|
732 |
* @param x one value |
|
733 |
* @param y another value |
|
734 |
* @param type the common type for the values |
|
735 |
* @return true if they're equal |
|
736 |
*/ |
|
737 |
private static boolean equalSameTypeValues(final Object x, final Object y, final JSType type) { |
|
738 |
if (type == JSType.UNDEFINED || type == JSType.NULL) { |
|
739 |
return true; |
|
740 |
} |
|
16147 | 741 |
|
24762 | 742 |
if (type == JSType.NUMBER) { |
743 |
return ((Number)x).doubleValue() == ((Number)y).doubleValue(); |
|
16147 | 744 |
} |
745 |
||
24762 | 746 |
if (type == JSType.STRING) { |
747 |
// String may be represented by ConsString |
|
748 |
return x.toString().equals(y.toString()); |
|
749 |
} |
|
750 |
||
751 |
if (type == JSType.BOOLEAN) { |
|
752 |
return ((Boolean)x).booleanValue() == ((Boolean)y).booleanValue(); |
|
753 |
} |
|
754 |
||
755 |
return x == y; |
|
756 |
} |
|
757 |
||
758 |
/** |
|
759 |
* Extracted portion of {@link #equals(Object, Object)} that compares values belonging to different JSTypes. |
|
760 |
* @param x one value |
|
761 |
* @param y another value |
|
762 |
* @param xType the type for the value x |
|
763 |
* @param yType the type for the value y |
|
764 |
* @return true if they're equal |
|
765 |
*/ |
|
766 |
private static boolean equalDifferentTypeValues(final Object x, final Object y, final JSType xType, final JSType yType) { |
|
767 |
if (xType == JSType.UNDEFINED && yType == JSType.NULL || xType == JSType.NULL && yType == JSType.UNDEFINED) { |
|
16147 | 768 |
return true; |
769 |
} |
|
770 |
||
771 |
if (xType == JSType.NUMBER && yType == JSType.STRING) { |
|
24762 | 772 |
return equals(x, JSType.toNumber(y)); |
16147 | 773 |
} |
774 |
||
775 |
if (xType == JSType.STRING && yType == JSType.NUMBER) { |
|
24762 | 776 |
return equals(JSType.toNumber(x), y); |
16147 | 777 |
} |
778 |
||
779 |
if (xType == JSType.BOOLEAN) { |
|
24762 | 780 |
return equals(JSType.toNumber(x), y); |
16147 | 781 |
} |
782 |
||
783 |
if (yType == JSType.BOOLEAN) { |
|
24762 | 784 |
return equals(x, JSType.toNumber(y)); |
16147 | 785 |
} |
786 |
||
24762 | 787 |
if ((xType == JSType.STRING || xType == JSType.NUMBER) && y instanceof ScriptObject) { |
788 |
return equals(x, JSType.toPrimitive(y)); |
|
16147 | 789 |
} |
790 |
||
24762 | 791 |
if (x instanceof ScriptObject && (yType == JSType.STRING || yType == JSType.NUMBER)) { |
792 |
return equals(JSType.toPrimitive(x), y); |
|
16147 | 793 |
} |
794 |
||
795 |
return false; |
|
796 |
} |
|
797 |
||
798 |
/** |
|
799 |
* ECMA 11.9.4 - The strict equal operator (===) - generic implementation |
|
800 |
* |
|
801 |
* @param x first object to compare |
|
802 |
* @param y second object to compare |
|
803 |
* |
|
804 |
* @return true if objects are equal |
|
805 |
*/ |
|
806 |
public static boolean EQ_STRICT(final Object x, final Object y) { |
|
807 |
return strictEquals(x, y); |
|
808 |
} |
|
809 |
||
810 |
/** |
|
811 |
* ECMA 11.9.5 - The strict non equal operator (!==) - generic implementation |
|
812 |
* |
|
813 |
* @param x first object to compare |
|
814 |
* @param y second object to compare |
|
815 |
* |
|
816 |
* @return true if objects are not equal |
|
817 |
*/ |
|
818 |
public static boolean NE_STRICT(final Object x, final Object y) { |
|
819 |
return !EQ_STRICT(x, y); |
|
820 |
} |
|
821 |
||
822 |
/** ECMA 11.9.6 The Strict Equality Comparison Algorithm */ |
|
823 |
private static boolean strictEquals(final Object x, final Object y) { |
|
25828
077046a5d726
8044786: Some tests fail with non-optimistic compilation
attila
parents:
25821
diff
changeset
|
824 |
// NOTE: you might be tempted to do a quick x == y comparison. Remember, though, that any Double object having |
077046a5d726
8044786: Some tests fail with non-optimistic compilation
attila
parents:
25821
diff
changeset
|
825 |
// NaN value is not equal to itself by value even though it is referentially. |
24762 | 826 |
|
24752
c835f368e8e0
8043002: Improve performance of Nashorn equality operators
attila
parents:
24745
diff
changeset
|
827 |
final JSType xType = JSType.ofNoFunction(x); |
c835f368e8e0
8043002: Improve performance of Nashorn equality operators
attila
parents:
24745
diff
changeset
|
828 |
final JSType yType = JSType.ofNoFunction(y); |
16147 | 829 |
|
830 |
if (xType != yType) { |
|
831 |
return false; |
|
832 |
} |
|
833 |
||
24762 | 834 |
return equalSameTypeValues(x, y, xType); |
16147 | 835 |
} |
836 |
||
837 |
/** |
|
838 |
* ECMA 11.8.6 - The in operator - generic implementation |
|
839 |
* |
|
840 |
* @param property property to check for |
|
841 |
* @param obj object in which to check for property |
|
842 |
* |
|
843 |
* @return true if objects are equal |
|
844 |
*/ |
|
845 |
public static boolean IN(final Object property, final Object obj) { |
|
24752
c835f368e8e0
8043002: Improve performance of Nashorn equality operators
attila
parents:
24745
diff
changeset
|
846 |
final JSType rvalType = JSType.ofNoFunction(obj); |
16147 | 847 |
|
24752
c835f368e8e0
8043002: Improve performance of Nashorn equality operators
attila
parents:
24745
diff
changeset
|
848 |
if (rvalType == JSType.OBJECT) { |
16147 | 849 |
if (obj instanceof ScriptObject) { |
850 |
return ((ScriptObject)obj).has(property); |
|
851 |
} |
|
852 |
||
19889
63af9358d0dc
8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents:
19884
diff
changeset
|
853 |
if (obj instanceof JSObject) { |
63af9358d0dc
8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents:
19884
diff
changeset
|
854 |
return ((JSObject)obj).hasMember(Objects.toString(property)); |
63af9358d0dc
8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents:
19884
diff
changeset
|
855 |
} |
63af9358d0dc
8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents:
19884
diff
changeset
|
856 |
|
16147 | 857 |
return false; |
858 |
} |
|
859 |
||
17778 | 860 |
throw typeError("in.with.non.object", rvalType.toString().toLowerCase(Locale.ENGLISH)); |
16147 | 861 |
} |
862 |
||
863 |
/** |
|
864 |
* ECMA 11.8.6 - The strict instanceof operator - generic implementation |
|
865 |
* |
|
866 |
* @param obj first object to compare |
|
867 |
* @param clazz type to check against |
|
868 |
* |
|
869 |
* @return true if {@code obj} is an instanceof {@code clazz} |
|
870 |
*/ |
|
871 |
public static boolean INSTANCEOF(final Object obj, final Object clazz) { |
|
872 |
if (clazz instanceof ScriptFunction) { |
|
873 |
if (obj instanceof ScriptObject) { |
|
874 |
return ((ScriptObject)clazz).isInstance((ScriptObject)obj); |
|
875 |
} |
|
876 |
return false; |
|
877 |
} |
|
878 |
||
879 |
if (clazz instanceof StaticClass) { |
|
880 |
return ((StaticClass)clazz).getRepresentedClass().isInstance(obj); |
|
881 |
} |
|
882 |
||
19889
63af9358d0dc
8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents:
19884
diff
changeset
|
883 |
if (clazz instanceof JSObject) { |
63af9358d0dc
8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents:
19884
diff
changeset
|
884 |
return ((JSObject)clazz).isInstance(obj); |
63af9358d0dc
8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents:
19884
diff
changeset
|
885 |
} |
63af9358d0dc
8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents:
19884
diff
changeset
|
886 |
|
63af9358d0dc
8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents:
19884
diff
changeset
|
887 |
// provide for reverse hook |
63af9358d0dc
8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents:
19884
diff
changeset
|
888 |
if (obj instanceof JSObject) { |
63af9358d0dc
8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents:
19884
diff
changeset
|
889 |
return ((JSObject)obj).isInstanceOf(clazz); |
18334
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17978
diff
changeset
|
890 |
} |
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17978
diff
changeset
|
891 |
|
16256
f2d9a0c49914
8007002: Replace implicit exception throwing methods with explicit throws - simplify control flow and remove useless code
lagergren
parents:
16240
diff
changeset
|
892 |
throw typeError("instanceof.on.non.object"); |
16147 | 893 |
} |
894 |
||
895 |
/** |
|
16272 | 896 |
* ECMA 11.8.1 - The less than operator ({@literal <}) - generic implementation |
16147 | 897 |
* |
898 |
* @param x first object to compare |
|
899 |
* @param y second object to compare |
|
900 |
* |
|
901 |
* @return true if x is less than y |
|
902 |
*/ |
|
903 |
public static boolean LT(final Object x, final Object y) { |
|
904 |
final Object value = lessThan(x, y, true); |
|
24725
7bb1f687a852
8033334: Make sure that scope depth information is maintained in the RecompilableScriptFunctionDatas, to avoid unnecessary slow proto linkage when doing on demand compilation
lagergren
parents:
24719
diff
changeset
|
905 |
return value == UNDEFINED ? false : (Boolean)value; |
16147 | 906 |
} |
907 |
||
908 |
/** |
|
16272 | 909 |
* ECMA 11.8.2 - The greater than operator ({@literal >}) - generic implementation |
16147 | 910 |
* |
911 |
* @param x first object to compare |
|
912 |
* @param y second object to compare |
|
913 |
* |
|
914 |
* @return true if x is greater than y |
|
915 |
*/ |
|
916 |
public static boolean GT(final Object x, final Object y) { |
|
917 |
final Object value = lessThan(y, x, false); |
|
24725
7bb1f687a852
8033334: Make sure that scope depth information is maintained in the RecompilableScriptFunctionDatas, to avoid unnecessary slow proto linkage when doing on demand compilation
lagergren
parents:
24719
diff
changeset
|
918 |
return value == UNDEFINED ? false : (Boolean)value; |
16147 | 919 |
} |
920 |
||
921 |
/** |
|
16272 | 922 |
* ECMA 11.8.3 - The less than or equal operator ({@literal <=}) - generic implementation |
16147 | 923 |
* |
924 |
* @param x first object to compare |
|
925 |
* @param y second object to compare |
|
926 |
* |
|
927 |
* @return true if x is less than or equal to y |
|
928 |
*/ |
|
929 |
public static boolean LE(final Object x, final Object y) { |
|
930 |
final Object value = lessThan(y, x, false); |
|
24725
7bb1f687a852
8033334: Make sure that scope depth information is maintained in the RecompilableScriptFunctionDatas, to avoid unnecessary slow proto linkage when doing on demand compilation
lagergren
parents:
24719
diff
changeset
|
931 |
return !(Boolean.TRUE.equals(value) || value == UNDEFINED); |
16147 | 932 |
} |
933 |
||
934 |
/** |
|
16272 | 935 |
* ECMA 11.8.4 - The greater than or equal operator ({@literal >=}) - generic implementation |
16147 | 936 |
* |
937 |
* @param x first object to compare |
|
938 |
* @param y second object to compare |
|
939 |
* |
|
940 |
* @return true if x is greater than or equal to y |
|
941 |
*/ |
|
942 |
public static boolean GE(final Object x, final Object y) { |
|
943 |
final Object value = lessThan(x, y, true); |
|
24725
7bb1f687a852
8033334: Make sure that scope depth information is maintained in the RecompilableScriptFunctionDatas, to avoid unnecessary slow proto linkage when doing on demand compilation
lagergren
parents:
24719
diff
changeset
|
944 |
return !(Boolean.TRUE.equals(value) || value == UNDEFINED); |
16147 | 945 |
} |
946 |
||
947 |
/** ECMA 11.8.5 The Abstract Relational Comparison Algorithm */ |
|
948 |
private static Object lessThan(final Object x, final Object y, final boolean leftFirst) { |
|
949 |
Object px, py; |
|
950 |
||
951 |
//support e.g. x < y should throw exception correctly if x or y are not numeric |
|
952 |
if (leftFirst) { |
|
953 |
px = JSType.toPrimitive(x, Number.class); |
|
954 |
py = JSType.toPrimitive(y, Number.class); |
|
955 |
} else { |
|
956 |
py = JSType.toPrimitive(y, Number.class); |
|
957 |
px = JSType.toPrimitive(x, Number.class); |
|
958 |
} |
|
959 |
||
24752
c835f368e8e0
8043002: Improve performance of Nashorn equality operators
attila
parents:
24745
diff
changeset
|
960 |
if (JSType.ofNoFunction(px) == JSType.STRING && JSType.ofNoFunction(py) == JSType.STRING) { |
16147 | 961 |
// May be String or ConsString |
24725
7bb1f687a852
8033334: Make sure that scope depth information is maintained in the RecompilableScriptFunctionDatas, to avoid unnecessary slow proto linkage when doing on demand compilation
lagergren
parents:
24719
diff
changeset
|
962 |
return px.toString().compareTo(py.toString()) < 0; |
16147 | 963 |
} |
964 |
||
965 |
final double nx = JSType.toNumber(px); |
|
966 |
final double ny = JSType.toNumber(py); |
|
967 |
||
968 |
if (Double.isNaN(nx) || Double.isNaN(ny)) { |
|
969 |
return UNDEFINED; |
|
970 |
} |
|
971 |
||
972 |
if (nx == ny) { |
|
973 |
return false; |
|
974 |
} |
|
975 |
||
976 |
if (nx > 0 && ny > 0 && Double.isInfinite(nx) && Double.isInfinite(ny)) { |
|
977 |
return false; |
|
978 |
} |
|
979 |
||
980 |
if (nx < 0 && ny < 0 && Double.isInfinite(nx) && Double.isInfinite(ny)) { |
|
981 |
return false; |
|
982 |
} |
|
983 |
||
984 |
return nx < ny; |
|
985 |
} |
|
986 |
||
987 |
} |