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