author | attila |
Wed, 26 Feb 2014 13:17:57 +0100 | |
changeset 24719 | f726e9d67629 |
parent 21691 | 8e284e9a6144 |
child 24727 | 611ba7e2101f |
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; |
|
24719 | 29 |
import static jdk.nashorn.internal.lookup.Lookup.MH; |
16147 | 30 |
import static jdk.nashorn.internal.runtime.ECMAErrors.typeError; |
31 |
||
19897
4235a28bb5e8
8024847: Java.to should accept mirror and external JSObjects as array-like objects as well
sundar
parents:
19892
diff
changeset
|
32 |
import java.lang.invoke.MethodHandle; |
19460
1b6d8e7b1cdf
8022782: publicLookup access failures in ScriptObject, ScriptFunction and ScriptFunction
sundar
parents:
18839
diff
changeset
|
33 |
import java.lang.invoke.MethodHandles; |
19897
4235a28bb5e8
8024847: Java.to should accept mirror and external JSObjects as array-like objects as well
sundar
parents:
19892
diff
changeset
|
34 |
import java.lang.reflect.Array; |
24719 | 35 |
import java.util.Arrays; |
36 |
import java.util.Collections; |
|
20944
a166d40ac0d4
8026113: Nashorn arrays should automatically convert to Java arrays
attila
parents:
20928
diff
changeset
|
37 |
import java.util.Deque; |
a166d40ac0d4
8026113: Nashorn arrays should automatically convert to Java arrays
attila
parents:
20928
diff
changeset
|
38 |
import java.util.List; |
24719 | 39 |
|
16234
86cb162cec6c
8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
16188
diff
changeset
|
40 |
import jdk.internal.dynalink.beans.StaticClass; |
19897
4235a28bb5e8
8024847: Java.to should accept mirror and external JSObjects as array-like objects as well
sundar
parents:
19892
diff
changeset
|
41 |
import jdk.nashorn.api.scripting.JSObject; |
16147 | 42 |
import jdk.nashorn.internal.codegen.CompilerConstants.Call; |
24719 | 43 |
import jdk.nashorn.internal.codegen.types.Type; |
16147 | 44 |
import jdk.nashorn.internal.parser.Lexer; |
19897
4235a28bb5e8
8024847: Java.to should accept mirror and external JSObjects as array-like objects as well
sundar
parents:
19892
diff
changeset
|
45 |
import jdk.nashorn.internal.runtime.arrays.ArrayLikeIterator; |
19627
90d910ec15a3
8023630: Implement Java.super() as the preferred way to call super methods
attila
parents:
19460
diff
changeset
|
46 |
import jdk.nashorn.internal.runtime.linker.Bootstrap; |
16147 | 47 |
|
48 |
/** |
|
49 |
* Representation for ECMAScript types - this maps directly to the ECMA script standard |
|
50 |
*/ |
|
51 |
public enum JSType { |
|
52 |
/** The undefined type */ |
|
19892 | 53 |
UNDEFINED("undefined"), |
16147 | 54 |
|
55 |
/** The null type */ |
|
19892 | 56 |
NULL("object"), |
16147 | 57 |
|
58 |
/** The boolean type */ |
|
19892 | 59 |
BOOLEAN("boolean"), |
16147 | 60 |
|
61 |
/** The number type */ |
|
19892 | 62 |
NUMBER("number"), |
16147 | 63 |
|
64 |
/** The string type */ |
|
19892 | 65 |
STRING("string"), |
16147 | 66 |
|
67 |
/** The object type */ |
|
19892 | 68 |
OBJECT("object"), |
16147 | 69 |
|
70 |
/** The function type */ |
|
19892 | 71 |
FUNCTION("function"); |
72 |
||
73 |
/** The type name as returned by ECMAScript "typeof" operator*/ |
|
74 |
private final String typeName; |
|
16147 | 75 |
|
76 |
/** Max value for an uint32 in JavaScript */ |
|
77 |
public static final long MAX_UINT = 0xFFFF_FFFFL; |
|
78 |
||
24719 | 79 |
private static final MethodHandles.Lookup JSTYPE_LOOKUP = MethodHandles.lookup(); |
19460
1b6d8e7b1cdf
8022782: publicLookup access failures in ScriptObject, ScriptFunction and ScriptFunction
sundar
parents:
18839
diff
changeset
|
80 |
|
16147 | 81 |
/** JavaScript compliant conversion function from Object to boolean */ |
24719 | 82 |
public static final Call TO_BOOLEAN = staticCall(JSTYPE_LOOKUP, JSType.class, "toBoolean", boolean.class, Object.class); |
16147 | 83 |
|
84 |
/** JavaScript compliant conversion function from number to boolean */ |
|
24719 | 85 |
public static final Call TO_BOOLEAN_D = staticCall(JSTYPE_LOOKUP, JSType.class, "toBoolean", boolean.class, double.class); |
16147 | 86 |
|
87 |
/** JavaScript compliant conversion function from Object to integer */ |
|
24719 | 88 |
public static final Call TO_INTEGER = staticCall(JSTYPE_LOOKUP, JSType.class, "toInteger", int.class, Object.class); |
16147 | 89 |
|
90 |
/** JavaScript compliant conversion function from Object to long */ |
|
24719 | 91 |
public static final Call TO_LONG = staticCall(JSTYPE_LOOKUP, JSType.class, "toLong", long.class, Object.class); |
92 |
||
93 |
/** JavaScript compliant conversion function from double to long */ |
|
94 |
public static final Call TO_LONG_D = staticCall(JSTYPE_LOOKUP, JSType.class, "toLong", long.class, double.class); |
|
16147 | 95 |
|
96 |
/** JavaScript compliant conversion function from Object to number */ |
|
24719 | 97 |
public static final Call TO_NUMBER = staticCall(JSTYPE_LOOKUP, JSType.class, "toNumber", double.class, Object.class); |
98 |
||
99 |
/** JavaScript compliant conversion function from Object to number with type check */ |
|
100 |
public static final Call TO_NUMBER_OPTIMISTIC = staticCall(JSTYPE_LOOKUP, JSType.class, "toNumberOptimistic", double.class, Object.class, int.class); |
|
16147 | 101 |
|
21691
8e284e9a6144
8027828: ClassCastException when converting return value of a Java method to boolean
sundar
parents:
21686
diff
changeset
|
102 |
/** JavaScript compliant conversion function from Object to String */ |
24719 | 103 |
public static final Call TO_STRING = staticCall(JSTYPE_LOOKUP, JSType.class, "toString", String.class, Object.class); |
21691
8e284e9a6144
8027828: ClassCastException when converting return value of a Java method to boolean
sundar
parents:
21686
diff
changeset
|
104 |
|
16147 | 105 |
/** JavaScript compliant conversion function from Object to int32 */ |
24719 | 106 |
public static final Call TO_INT32 = staticCall(JSTYPE_LOOKUP, JSType.class, "toInt32", int.class, Object.class); |
107 |
||
108 |
/** JavaScript compliant conversion function from Object to int32 */ |
|
109 |
public static final Call TO_INT32_L = staticCall(JSTYPE_LOOKUP, JSType.class, "toInt32", int.class, long.class); |
|
110 |
||
111 |
/** JavaScript compliant conversion function from Object to int32 with type check */ |
|
112 |
public static final Call TO_INT32_OPTIMISTIC = staticCall(JSTYPE_LOOKUP, JSType.class, "toInt32Optimistic", int.class, Object.class, int.class); |
|
16147 | 113 |
|
114 |
/** JavaScript compliant conversion function from double to int32 */ |
|
24719 | 115 |
public static final Call TO_INT32_D = staticCall(JSTYPE_LOOKUP, JSType.class, "toInt32", int.class, double.class); |
16147 | 116 |
|
117 |
/** JavaScript compliant conversion function from Object to uint32 */ |
|
24719 | 118 |
public static final Call TO_UINT32 = staticCall(JSTYPE_LOOKUP, JSType.class, "toUint32", long.class, Object.class); |
119 |
||
120 |
/** JavaScript compliant conversion function from Object to long with type check */ |
|
121 |
public static final Call TO_LONG_OPTIMISTIC = staticCall(JSTYPE_LOOKUP, JSType.class, "toLongOptimistic", long.class, Object.class, int.class); |
|
16147 | 122 |
|
123 |
/** JavaScript compliant conversion function from number to uint32 */ |
|
24719 | 124 |
public static final Call TO_UINT32_D = staticCall(JSTYPE_LOOKUP, JSType.class, "toUint32", long.class, double.class); |
16147 | 125 |
|
126 |
/** JavaScript compliant conversion function from number to String */ |
|
24719 | 127 |
public static final Call TO_STRING_D = staticCall(JSTYPE_LOOKUP, JSType.class, "toString", String.class, double.class); |
16147 | 128 |
|
20928
3ff39d5c8c08
8026137: Fix Issues with Binary Evaluation Order
lagergren
parents:
19897
diff
changeset
|
129 |
/** Combined call to toPrimitive followed by toString. */ |
24719 | 130 |
public static final Call TO_PRIMITIVE_TO_STRING = staticCall(JSTYPE_LOOKUP, JSType.class, "toPrimitiveToString", String.class, Object.class); |
131 |
||
132 |
/** Throw an unwarranted optimism exception */ |
|
133 |
public static final Call THROW_UNWARRANTED = staticCall(JSTYPE_LOOKUP, JSType.class, "throwUnwarrantedOptimismException", Object.class, Object.class, int.class); |
|
134 |
||
135 |
/** Add exact wrapper for potentially overflowing integer operations */ |
|
136 |
public static final Call ADD_EXACT = staticCall(JSTYPE_LOOKUP, JSType.class, "addExact", int.class, int.class, int.class, int.class); |
|
137 |
||
138 |
/** Sub exact wrapper for potentially overflowing integer operations */ |
|
139 |
public static final Call SUB_EXACT = staticCall(JSTYPE_LOOKUP, JSType.class, "subExact", int.class, int.class, int.class, int.class); |
|
140 |
||
141 |
/** Multiply exact wrapper for potentially overflowing integer operations */ |
|
142 |
public static final Call MUL_EXACT = staticCall(JSTYPE_LOOKUP, JSType.class, "mulExact", int.class, int.class, int.class, int.class); |
|
143 |
||
144 |
/** Div exact wrapper for potentially integer division that turns into float point */ |
|
145 |
public static final Call DIV_EXACT = staticCall(JSTYPE_LOOKUP, JSType.class, "divExact", int.class, int.class, int.class, int.class); |
|
146 |
||
147 |
/** Decrement exact wrapper for potentially overflowing integer operations */ |
|
148 |
public static final Call DECREMENT_EXACT = staticCall(JSTYPE_LOOKUP, JSType.class, "decrementExact", int.class, int.class, int.class); |
|
149 |
||
150 |
/** Increment exact wrapper for potentially overflowing integer operations */ |
|
151 |
public static final Call INCREMENT_EXACT = staticCall(JSTYPE_LOOKUP, JSType.class, "incrementExact", int.class, int.class, int.class); |
|
152 |
||
153 |
/** Negate exact exact wrapper for potentially overflowing long operations */ |
|
154 |
public static final Call NEGATE_EXACT = staticCall(JSTYPE_LOOKUP, JSType.class, "negateExact", int.class, int.class, int.class); |
|
155 |
||
156 |
/** Add exact wrapper for potentially overflowing long operations */ |
|
157 |
public static final Call ADD_EXACT_LONG = staticCall(JSTYPE_LOOKUP, JSType.class, "addExact", long.class, long.class, long.class, int.class); |
|
158 |
||
159 |
/** Sub exact wrapper for potentially overflowing long operations */ |
|
160 |
public static final Call SUB_EXACT_LONG = staticCall(JSTYPE_LOOKUP, JSType.class, "subExact", long.class, long.class, long.class, int.class); |
|
161 |
||
162 |
/** Multiply exact wrapper for potentially overflowing long operations */ |
|
163 |
public static final Call MUL_EXACT_LONG = staticCall(JSTYPE_LOOKUP, JSType.class, "mulExact", long.class, long.class, long.class, int.class); |
|
164 |
||
165 |
/** Div exact wrapper for potentially integer division that turns into float point */ |
|
166 |
public static final Call DIV_EXACT_LONG = staticCall(JSTYPE_LOOKUP, JSType.class, "divExact", long.class, long.class, long.class, int.class); |
|
167 |
||
168 |
/** Decrement exact wrapper for potentially overflowing long operations */ |
|
169 |
public static final Call DECREMENT_EXACT_LONG = staticCall(JSTYPE_LOOKUP, JSType.class, "decrementExact", long.class, long.class, int.class); |
|
170 |
||
171 |
/** Increment exact wrapper for potentially overflowing long operations */ |
|
172 |
public static final Call INCREMENT_EXACT_LONG = staticCall(JSTYPE_LOOKUP, JSType.class, "incrementExact", long.class, long.class, int.class); |
|
173 |
||
174 |
/** Negate exact exact wrapper for potentially overflowing long operations */ |
|
175 |
public static final Call NEGATE_EXACT_LONG = staticCall(JSTYPE_LOOKUP, JSType.class, "negateExact", long.class, long.class, int.class); |
|
16147 | 176 |
|
20944
a166d40ac0d4
8026113: Nashorn arrays should automatically convert to Java arrays
attila
parents:
20928
diff
changeset
|
177 |
/** Method handle to convert a JS Object to a Java array. */ |
24719 | 178 |
public static final Call TO_JAVA_ARRAY = staticCall(JSTYPE_LOOKUP, JSType.class, "toJavaArray", Object.class, Object.class, Class.class); |
20944
a166d40ac0d4
8026113: Nashorn arrays should automatically convert to Java arrays
attila
parents:
20928
diff
changeset
|
179 |
|
a166d40ac0d4
8026113: Nashorn arrays should automatically convert to Java arrays
attila
parents:
20928
diff
changeset
|
180 |
/** Method handle to convert a JS Object to a Java List. */ |
24719 | 181 |
public static final Call TO_JAVA_LIST = staticCall(JSTYPE_LOOKUP, JSType.class, "toJavaList", List.class, Object.class); |
20944
a166d40ac0d4
8026113: Nashorn arrays should automatically convert to Java arrays
attila
parents:
20928
diff
changeset
|
182 |
|
a166d40ac0d4
8026113: Nashorn arrays should automatically convert to Java arrays
attila
parents:
20928
diff
changeset
|
183 |
/** Method handle to convert a JS Object to a Java deque. */ |
24719 | 184 |
public static final Call TO_JAVA_DEQUE = staticCall(JSTYPE_LOOKUP, JSType.class, "toJavaDeque", Deque.class, Object.class); |
185 |
||
186 |
/** Method handle for void returns. */ |
|
187 |
public static final Call VOID_RETURN = staticCall(JSTYPE_LOOKUP, JSType.class, "voidReturn", void.class); |
|
188 |
||
189 |
||
190 |
/** |
|
191 |
* The list of available accessor types in width order. This order is used for type guesses narrow{@literal ->} wide |
|
192 |
* in the dual--fields world |
|
193 |
*/ |
|
194 |
private static final List<Type> ACCESSOR_TYPES = Collections.unmodifiableList( |
|
195 |
Arrays.asList( |
|
196 |
Type.INT, |
|
197 |
Type.LONG, |
|
198 |
Type.NUMBER, |
|
199 |
Type.OBJECT)); |
|
200 |
||
201 |
/** table index for undefined type - hard coded so it can be used in switches at compile time */ |
|
202 |
public static final int TYPE_UNDEFINED_INDEX = -1; |
|
203 |
/** table index for integer type - hard coded so it can be used in switches at compile time */ |
|
204 |
public static final int TYPE_INT_INDEX = 0; //getAccessorTypeIndex(int.class); |
|
205 |
/** table index for long type - hard coded so it can be used in switches at compile time */ |
|
206 |
public static final int TYPE_LONG_INDEX = 1; //getAccessorTypeIndex(long.class); |
|
207 |
/** table index for double type - hard coded so it can be used in switches at compile time */ |
|
208 |
public static final int TYPE_DOUBLE_INDEX = 2; //getAccessorTypeIndex(double.class); |
|
209 |
/** table index for object type - hard coded so it can be used in switches at compile time */ |
|
210 |
public static final int TYPE_OBJECT_INDEX = 3; //getAccessorTypeIndex(Object.class); |
|
211 |
||
212 |
/** object conversion quickies with JS semantics - used for return value and parameter filter */ |
|
213 |
public static final MethodHandle[] CONVERT_OBJECT = { |
|
214 |
JSType.TO_INT32.methodHandle(), |
|
215 |
JSType.TO_UINT32.methodHandle(), |
|
216 |
JSType.TO_NUMBER.methodHandle(), |
|
217 |
null |
|
218 |
}; |
|
219 |
||
220 |
/** |
|
221 |
* object conversion quickies with JS semantics - used for return value and parameter filter, optimistic |
|
222 |
* throws exception upon incompatible type (asking for a narrower one than the storage) |
|
223 |
*/ |
|
224 |
public static final MethodHandle[] CONVERT_OBJECT_OPTIMISTIC = { |
|
225 |
JSType.TO_INT32_OPTIMISTIC.methodHandle(), |
|
226 |
JSType.TO_LONG_OPTIMISTIC.methodHandle(), |
|
227 |
JSType.TO_NUMBER_OPTIMISTIC.methodHandle(), |
|
228 |
null |
|
229 |
}; |
|
230 |
||
231 |
/** The value of Undefined cast to an int32 */ |
|
232 |
public static final int UNDEFINED_INT = 0; |
|
233 |
/** The value of Undefined cast to a long */ |
|
234 |
public static final long UNDEFINED_LONG = 0L; |
|
235 |
/** The value of Undefined cast to a double */ |
|
236 |
public static final double UNDEFINED_DOUBLE = Double.NaN; |
|
237 |
||
238 |
/** |
|
239 |
* Method handles for getters that return undefined coerced |
|
240 |
* to the appropriate type |
|
241 |
*/ |
|
242 |
public static final MethodHandle[] GET_UNDEFINED = new MethodHandle[] { |
|
243 |
MH.constant(int.class, UNDEFINED_INT), |
|
244 |
MH.constant(long.class, UNDEFINED_LONG), |
|
245 |
MH.constant(double.class, UNDEFINED_DOUBLE), |
|
246 |
MH.constant(Object.class, Undefined.getUndefined()), |
|
247 |
}; |
|
20944
a166d40ac0d4
8026113: Nashorn arrays should automatically convert to Java arrays
attila
parents:
20928
diff
changeset
|
248 |
|
17241
c337fefb8c84
8012334: ToUint32, ToInt32, and ToUint16 don't conform to spec
hannesw
parents:
16256
diff
changeset
|
249 |
private static final double INT32_LIMIT = 4294967296.0; |
c337fefb8c84
8012334: ToUint32, ToInt32, and ToUint16 don't conform to spec
hannesw
parents:
16256
diff
changeset
|
250 |
|
16147 | 251 |
/** |
19892 | 252 |
* Constructor |
253 |
* |
|
254 |
* @param typeName the type name |
|
255 |
*/ |
|
256 |
private JSType(final String typeName) { |
|
257 |
this.typeName = typeName; |
|
258 |
} |
|
259 |
||
260 |
/** |
|
16147 | 261 |
* The external type name as returned by ECMAScript "typeof" operator |
262 |
* |
|
263 |
* @return type name for this type |
|
264 |
*/ |
|
265 |
public final String typeName() { |
|
19892 | 266 |
return this.typeName; |
16147 | 267 |
} |
268 |
||
269 |
/** |
|
270 |
* Return the JSType for a given object |
|
271 |
* |
|
272 |
* @param obj an object |
|
273 |
* |
|
274 |
* @return the JSType for the object |
|
275 |
*/ |
|
276 |
public static JSType of(final Object obj) { |
|
19892 | 277 |
// Order of these statements is tuned for performance (see JDK-8024476) |
278 |
if (obj == null) { |
|
279 |
return JSType.NULL; |
|
16147 | 280 |
} |
281 |
||
19892 | 282 |
if (obj instanceof ScriptObject) { |
283 |
return (obj instanceof ScriptFunction) ? JSType.FUNCTION : JSType.OBJECT; |
|
16147 | 284 |
} |
285 |
||
286 |
if (obj instanceof Boolean) { |
|
287 |
return JSType.BOOLEAN; |
|
288 |
} |
|
289 |
||
19892 | 290 |
if (obj instanceof String || obj instanceof ConsString) { |
291 |
return JSType.STRING; |
|
292 |
} |
|
293 |
||
16147 | 294 |
if (obj instanceof Number) { |
295 |
return JSType.NUMBER; |
|
296 |
} |
|
297 |
||
19892 | 298 |
if (obj == ScriptRuntime.UNDEFINED) { |
299 |
return JSType.UNDEFINED; |
|
16147 | 300 |
} |
301 |
||
19892 | 302 |
return Bootstrap.isCallable(obj) ? JSType.FUNCTION : JSType.OBJECT; |
16147 | 303 |
} |
304 |
||
305 |
/** |
|
24719 | 306 |
* Void return method handle glue |
307 |
*/ |
|
308 |
public static void voidReturn() { |
|
309 |
//empty |
|
310 |
//TODO: fix up SetMethodCreator better so we don't need this stupid thing |
|
311 |
} |
|
312 |
||
313 |
/** |
|
16147 | 314 |
* Returns true if double number can be represented as an int |
315 |
* |
|
316 |
* @param number a long to inspect |
|
317 |
* |
|
318 |
* @return true for int representable longs |
|
319 |
*/ |
|
320 |
public static boolean isRepresentableAsInt(final long number) { |
|
321 |
return (int)number == number; |
|
322 |
} |
|
323 |
||
324 |
/** |
|
24719 | 325 |
* Returns true if double number can be represented as an int. Note that it returns true for negative zero. If you |
326 |
* need to exclude negative zero, combine this check with {@link #isNegativeZero(double)}. |
|
16147 | 327 |
* |
328 |
* @param number a double to inspect |
|
329 |
* |
|
330 |
* @return true for int representable doubles |
|
331 |
*/ |
|
332 |
public static boolean isRepresentableAsInt(final double number) { |
|
333 |
return (int)number == number; |
|
334 |
} |
|
335 |
||
336 |
/** |
|
24719 | 337 |
* Returns true if Object can be represented as an int |
338 |
* |
|
339 |
* @param obj an object to inspect |
|
340 |
* |
|
341 |
* @return true for int representable objects |
|
342 |
*/ |
|
343 |
public static boolean isRepresentableAsInt(final Object obj) { |
|
344 |
if (obj instanceof Number) { |
|
345 |
return isRepresentableAsInt(((Number)obj).doubleValue()); |
|
346 |
} |
|
347 |
return false; |
|
348 |
} |
|
349 |
||
350 |
/** |
|
351 |
* Returns true if double number can be represented as a long. Note that it returns true for negative zero. If you |
|
352 |
* need to exclude negative zero, combine this check with {@link #isNegativeZero(double)}. |
|
16147 | 353 |
* |
354 |
* @param number a double to inspect |
|
355 |
* @return true for long representable doubles |
|
356 |
*/ |
|
357 |
public static boolean isRepresentableAsLong(final double number) { |
|
358 |
return (long)number == number; |
|
359 |
} |
|
360 |
||
361 |
/** |
|
24719 | 362 |
* Returns true if Object can be represented as a long |
363 |
* |
|
364 |
* @param obj an object to inspect |
|
365 |
* |
|
366 |
* @return true for long representable objects |
|
367 |
*/ |
|
368 |
public static boolean isRepresentableAsLong(final Object obj) { |
|
369 |
if (obj instanceof Number) { |
|
370 |
return isRepresentableAsLong(((Number)obj).doubleValue()); |
|
371 |
} |
|
372 |
return false; |
|
373 |
} |
|
374 |
||
375 |
/** |
|
376 |
* Returns true if the number is the negative zero ({@code -0.0d}). |
|
377 |
* @param number the number to test |
|
378 |
* @return true if it is the negative zero, false otherwise. |
|
379 |
*/ |
|
380 |
public static boolean isNegativeZero(final double number) { |
|
381 |
return number == 0.0d && Double.doubleToRawLongBits(number) == 0x8000000000000000L; |
|
382 |
} |
|
383 |
||
384 |
/** |
|
16147 | 385 |
* Check whether an object is primitive |
386 |
* |
|
387 |
* @param obj an object |
|
388 |
* |
|
389 |
* @return true if object is primitive (includes null and undefined) |
|
390 |
*/ |
|
24719 | 391 |
public static boolean isPrimitive(final Object obj) { |
16147 | 392 |
return obj == null || |
393 |
obj == ScriptRuntime.UNDEFINED || |
|
394 |
obj instanceof Boolean || |
|
395 |
obj instanceof Number || |
|
396 |
obj instanceof String || |
|
397 |
obj instanceof ConsString; |
|
398 |
} |
|
399 |
||
400 |
/** |
|
401 |
* Primitive converter for an object |
|
402 |
* |
|
403 |
* @param obj an object |
|
404 |
* |
|
405 |
* @return primitive form of the object |
|
406 |
*/ |
|
407 |
public static Object toPrimitive(final Object obj) { |
|
408 |
return toPrimitive(obj, null); |
|
409 |
} |
|
410 |
||
411 |
/** |
|
412 |
* Primitive converter for an object including type hint |
|
413 |
* See ECMA 9.1 ToPrimitive |
|
414 |
* |
|
415 |
* @param obj an object |
|
416 |
* @param hint a type hint |
|
417 |
* |
|
418 |
* @return the primitive form of the object |
|
419 |
*/ |
|
420 |
public static Object toPrimitive(final Object obj, final Class<?> hint) { |
|
21449 | 421 |
return obj instanceof ScriptObject ? toPrimitive((ScriptObject)obj, hint) : obj; |
422 |
} |
|
16147 | 423 |
|
21449 | 424 |
private static Object toPrimitive(final ScriptObject sobj, final Class<?> hint) { |
425 |
final Object result = sobj.getDefaultValue(hint); |
|
16147 | 426 |
|
427 |
if (!isPrimitive(result)) { |
|
16256
f2d9a0c49914
8007002: Replace implicit exception throwing methods with explicit throws - simplify control flow and remove useless code
lagergren
parents:
16234
diff
changeset
|
428 |
throw typeError("bad.default.value", result.toString()); |
16147 | 429 |
} |
430 |
||
431 |
return result; |
|
432 |
} |
|
433 |
||
434 |
/** |
|
20928
3ff39d5c8c08
8026137: Fix Issues with Binary Evaluation Order
lagergren
parents:
19897
diff
changeset
|
435 |
* Combines a hintless toPrimitive and a toString call. |
3ff39d5c8c08
8026137: Fix Issues with Binary Evaluation Order
lagergren
parents:
19897
diff
changeset
|
436 |
* |
3ff39d5c8c08
8026137: Fix Issues with Binary Evaluation Order
lagergren
parents:
19897
diff
changeset
|
437 |
* @param obj an object |
3ff39d5c8c08
8026137: Fix Issues with Binary Evaluation Order
lagergren
parents:
19897
diff
changeset
|
438 |
* |
3ff39d5c8c08
8026137: Fix Issues with Binary Evaluation Order
lagergren
parents:
19897
diff
changeset
|
439 |
* @return the string form of the primitive form of the object |
3ff39d5c8c08
8026137: Fix Issues with Binary Evaluation Order
lagergren
parents:
19897
diff
changeset
|
440 |
*/ |
3ff39d5c8c08
8026137: Fix Issues with Binary Evaluation Order
lagergren
parents:
19897
diff
changeset
|
441 |
public static String toPrimitiveToString(Object obj) { |
3ff39d5c8c08
8026137: Fix Issues with Binary Evaluation Order
lagergren
parents:
19897
diff
changeset
|
442 |
return toString(toPrimitive(obj)); |
3ff39d5c8c08
8026137: Fix Issues with Binary Evaluation Order
lagergren
parents:
19897
diff
changeset
|
443 |
} |
3ff39d5c8c08
8026137: Fix Issues with Binary Evaluation Order
lagergren
parents:
19897
diff
changeset
|
444 |
|
3ff39d5c8c08
8026137: Fix Issues with Binary Evaluation Order
lagergren
parents:
19897
diff
changeset
|
445 |
/** |
16147 | 446 |
* JavaScript compliant conversion of number to boolean |
447 |
* |
|
448 |
* @param num a number |
|
449 |
* |
|
450 |
* @return a boolean |
|
451 |
*/ |
|
452 |
public static boolean toBoolean(final double num) { |
|
453 |
return num != 0 && !Double.isNaN(num); |
|
454 |
} |
|
455 |
||
456 |
/** |
|
457 |
* JavaScript compliant conversion of Object to boolean |
|
458 |
* See ECMA 9.2 ToBoolean |
|
459 |
* |
|
460 |
* @param obj an object |
|
461 |
* |
|
462 |
* @return a boolean |
|
463 |
*/ |
|
464 |
public static boolean toBoolean(final Object obj) { |
|
465 |
if (obj instanceof Boolean) { |
|
466 |
return (Boolean)obj; |
|
467 |
} |
|
468 |
||
469 |
if (nullOrUndefined(obj)) { |
|
470 |
return false; |
|
471 |
} |
|
472 |
||
473 |
if (obj instanceof Number) { |
|
474 |
final double num = ((Number)obj).doubleValue(); |
|
475 |
return num != 0 && !Double.isNaN(num); |
|
476 |
} |
|
477 |
||
478 |
if (obj instanceof String || obj instanceof ConsString) { |
|
479 |
return ((CharSequence)obj).length() > 0; |
|
480 |
} |
|
481 |
||
482 |
return true; |
|
483 |
} |
|
484 |
||
485 |
||
486 |
/** |
|
487 |
* JavaScript compliant converter of Object to String |
|
488 |
* See ECMA 9.8 ToString |
|
489 |
* |
|
490 |
* @param obj an object |
|
491 |
* |
|
492 |
* @return a string |
|
493 |
*/ |
|
494 |
public static String toString(final Object obj) { |
|
495 |
return toStringImpl(obj, false); |
|
496 |
} |
|
497 |
||
498 |
/** |
|
499 |
* If obj is an instance of {@link ConsString} cast to CharSequence, else return |
|
500 |
* result of {@link #toString(Object)}. |
|
501 |
* |
|
502 |
* @param obj an object |
|
503 |
* @return an instance of String or ConsString |
|
504 |
*/ |
|
505 |
public static CharSequence toCharSequence(final Object obj) { |
|
506 |
if (obj instanceof ConsString) { |
|
507 |
return (CharSequence) obj; |
|
508 |
} |
|
509 |
return toString(obj); |
|
510 |
} |
|
511 |
||
512 |
/** |
|
513 |
* Check whether a string is representable as a JavaScript number |
|
514 |
* |
|
515 |
* @param str a string |
|
516 |
* |
|
517 |
* @return true if string can be represented as a number |
|
518 |
*/ |
|
519 |
public static boolean isNumber(final String str) { |
|
520 |
try { |
|
521 |
Double.parseDouble(str); |
|
522 |
return true; |
|
523 |
} catch (final NumberFormatException e) { |
|
524 |
return false; |
|
525 |
} |
|
526 |
} |
|
527 |
||
528 |
/** |
|
529 |
* JavaScript compliant conversion of integer to String |
|
530 |
* |
|
531 |
* @param num an integer |
|
532 |
* |
|
533 |
* @return a string |
|
534 |
*/ |
|
535 |
public static String toString(final int num) { |
|
536 |
return Integer.toString(num); |
|
537 |
} |
|
538 |
||
539 |
/** |
|
540 |
* JavaScript compliant conversion of number to String |
|
541 |
* See ECMA 9.8.1 |
|
542 |
* |
|
543 |
* @param num a number |
|
544 |
* |
|
545 |
* @return a string |
|
546 |
*/ |
|
547 |
public static String toString(final double num) { |
|
548 |
if (isRepresentableAsInt(num)) { |
|
549 |
return Integer.toString((int)num); |
|
550 |
} |
|
551 |
||
552 |
if (num == Double.POSITIVE_INFINITY) { |
|
553 |
return "Infinity"; |
|
554 |
} |
|
555 |
||
556 |
if (num == Double.NEGATIVE_INFINITY) { |
|
557 |
return "-Infinity"; |
|
558 |
} |
|
559 |
||
560 |
if (Double.isNaN(num)) { |
|
561 |
return "NaN"; |
|
562 |
} |
|
563 |
||
564 |
return NumberToString.stringFor(num); |
|
565 |
} |
|
566 |
||
567 |
/** |
|
568 |
* JavaScript compliant conversion of number to String |
|
569 |
* |
|
570 |
* @param num a number |
|
571 |
* @param radix a radix for the conversion |
|
572 |
* |
|
573 |
* @return a string |
|
574 |
*/ |
|
575 |
public static String toString(final double num, final int radix) { |
|
576 |
assert radix >= 2 && radix <= 36 : "invalid radix"; |
|
577 |
||
578 |
if (isRepresentableAsInt(num)) { |
|
579 |
return Integer.toString((int)num, radix); |
|
580 |
} |
|
581 |
||
582 |
if (num == Double.POSITIVE_INFINITY) { |
|
583 |
return "Infinity"; |
|
584 |
} |
|
585 |
||
586 |
if (num == Double.NEGATIVE_INFINITY) { |
|
587 |
return "-Infinity"; |
|
588 |
} |
|
589 |
||
590 |
if (Double.isNaN(num)) { |
|
591 |
return "NaN"; |
|
592 |
} |
|
593 |
||
594 |
if (num == 0.0) { |
|
595 |
return "0"; |
|
596 |
} |
|
597 |
||
598 |
final String chars = "0123456789abcdefghijklmnopqrstuvwxyz"; |
|
599 |
final StringBuilder sb = new StringBuilder(); |
|
600 |
||
601 |
final boolean negative = num < 0.0; |
|
602 |
final double signedNum = negative ? -num : num; |
|
603 |
||
604 |
double intPart = Math.floor(signedNum); |
|
605 |
double decPart = signedNum - intPart; |
|
606 |
||
607 |
// encode integer part from least significant digit, then reverse |
|
608 |
do { |
|
609 |
sb.append(chars.charAt((int) (intPart % radix))); |
|
610 |
intPart /= radix; |
|
611 |
} while (intPart >= 1.0); |
|
612 |
||
613 |
if (negative) { |
|
614 |
sb.append('-'); |
|
615 |
} |
|
616 |
sb.reverse(); |
|
617 |
||
618 |
// encode decimal part |
|
619 |
if (decPart > 0.0) { |
|
620 |
final int dot = sb.length(); |
|
621 |
sb.append('.'); |
|
622 |
do { |
|
623 |
decPart *= radix; |
|
624 |
final double d = Math.floor(decPart); |
|
625 |
sb.append(chars.charAt((int)d)); |
|
626 |
decPart -= d; |
|
627 |
} while (decPart > 0.0 && sb.length() - dot < 1100); |
|
628 |
// somewhat arbitrarily use same limit as V8 |
|
629 |
} |
|
630 |
||
631 |
return sb.toString(); |
|
632 |
} |
|
633 |
||
634 |
/** |
|
635 |
* JavaScript compliant conversion of Object to number |
|
636 |
* See ECMA 9.3 ToNumber |
|
637 |
* |
|
638 |
* @param obj an object |
|
639 |
* |
|
640 |
* @return a number |
|
641 |
*/ |
|
642 |
public static double toNumber(final Object obj) { |
|
643 |
if (obj instanceof Number) { |
|
644 |
return ((Number)obj).doubleValue(); |
|
645 |
} |
|
646 |
return toNumberGeneric(obj); |
|
647 |
} |
|
648 |
||
21449 | 649 |
|
650 |
/** |
|
651 |
* JavaScript compliant conversion of Object to number |
|
652 |
* See ECMA 9.3 ToNumber |
|
653 |
* |
|
654 |
* @param obj an object |
|
655 |
* |
|
656 |
* @return a number |
|
657 |
*/ |
|
658 |
public static double toNumber(final ScriptObject obj) { |
|
659 |
return toNumber(toPrimitive(obj, Number.class)); |
|
660 |
} |
|
661 |
||
16147 | 662 |
/** |
24719 | 663 |
* Optimistic number conversion - throws UnwarrantedOptimismException if Object |
664 |
* |
|
665 |
* @param obj object to convert |
|
666 |
* @param programPoint program point |
|
667 |
* @return double |
|
668 |
*/ |
|
669 |
public static double toNumberOptimistic(final Object obj, final int programPoint) { |
|
670 |
if (obj != null) { |
|
671 |
final Class<?> clz = obj.getClass(); |
|
672 |
if (clz == Double.class || clz == Integer.class || clz == Long.class) { |
|
673 |
return ((Number)obj).doubleValue(); |
|
674 |
} |
|
675 |
} |
|
676 |
throw new UnwarrantedOptimismException(obj, programPoint); |
|
677 |
} |
|
678 |
||
679 |
/** |
|
680 |
* Object to number conversion that delegates to either {@link #toNumber(Object)} or to |
|
681 |
* {@link #toNumberOptimistic(Object, int)} depending on whether the program point is valid or not. |
|
682 |
* @param obj the object to convert |
|
683 |
* @param programPoint the program point; can be invalid. |
|
684 |
* @return the value converted to a number |
|
685 |
* @throws UnwarrantedOptimismException if the value can't be represented as a number and the program point is valid. |
|
686 |
*/ |
|
687 |
public static double toNumberMaybeOptimistic(final Object obj, final int programPoint) { |
|
688 |
return UnwarrantedOptimismException.isValid(programPoint) ? toNumberOptimistic(obj, programPoint) : toNumber(obj); |
|
689 |
} |
|
690 |
||
691 |
/** |
|
16147 | 692 |
* Digit representation for a character |
693 |
* |
|
694 |
* @param ch a character |
|
695 |
* @param radix radix |
|
696 |
* |
|
697 |
* @return the digit for this character |
|
698 |
*/ |
|
699 |
public static int digit(final char ch, final int radix) { |
|
700 |
return digit(ch, radix, false); |
|
701 |
} |
|
702 |
||
703 |
/** |
|
704 |
* Digit representation for a character |
|
705 |
* |
|
706 |
* @param ch a character |
|
707 |
* @param radix radix |
|
708 |
* @param onlyIsoLatin1 iso latin conversion only |
|
709 |
* |
|
710 |
* @return the digit for this character |
|
711 |
*/ |
|
712 |
public static int digit(final char ch, final int radix, final boolean onlyIsoLatin1) { |
|
713 |
final char maxInRadix = (char)('a' + (radix - 1) - 10); |
|
714 |
final char c = Character.toLowerCase(ch); |
|
715 |
||
716 |
if (c >= 'a' && c <= maxInRadix) { |
|
717 |
return Character.digit(ch, radix); |
|
718 |
} |
|
719 |
||
720 |
if (Character.isDigit(ch)) { |
|
721 |
if (!onlyIsoLatin1 || ch >= '0' && ch <= '9') { |
|
722 |
return Character.digit(ch, radix); |
|
723 |
} |
|
724 |
} |
|
725 |
||
726 |
return -1; |
|
727 |
} |
|
728 |
||
729 |
/** |
|
730 |
* JavaScript compliant String to number conversion |
|
731 |
* |
|
732 |
* @param str a string |
|
733 |
* |
|
734 |
* @return a number |
|
735 |
*/ |
|
736 |
public static double toNumber(final String str) { |
|
737 |
int end = str.length(); |
|
738 |
if (end == 0) { |
|
739 |
return 0.0; // Empty string |
|
740 |
} |
|
741 |
||
742 |
int start = 0; |
|
743 |
char f = str.charAt(0); |
|
744 |
||
745 |
while (Lexer.isJSWhitespace(f)) { |
|
746 |
if (++start == end) { |
|
747 |
return 0.0d; // All whitespace string |
|
748 |
} |
|
749 |
f = str.charAt(start); |
|
750 |
} |
|
751 |
||
752 |
// Guaranteed to terminate even without start >= end check, as the previous loop found at least one |
|
753 |
// non-whitespace character. |
|
754 |
while (Lexer.isJSWhitespace(str.charAt(end - 1))) { |
|
755 |
end--; |
|
756 |
} |
|
757 |
||
758 |
final boolean negative; |
|
759 |
if (f == '-') { |
|
760 |
if(++start == end) { |
|
761 |
return Double.NaN; // Single-char "-" string |
|
762 |
} |
|
763 |
f = str.charAt(start); |
|
764 |
negative = true; |
|
765 |
} else { |
|
766 |
if (f == '+') { |
|
767 |
if (++start == end) { |
|
768 |
return Double.NaN; // Single-char "+" string |
|
769 |
} |
|
770 |
f = str.charAt(start); |
|
771 |
} |
|
772 |
negative = false; |
|
773 |
} |
|
774 |
||
775 |
final double value; |
|
776 |
if (start + 1 < end && f == '0' && Character.toLowerCase(str.charAt(start + 1)) == 'x') { |
|
777 |
//decode hex string |
|
778 |
value = parseRadix(str.toCharArray(), start + 2, end, 16); |
|
779 |
} else { |
|
780 |
// Fast (no NumberFormatException) path to NaN for non-numeric strings. We allow those starting with "I" or |
|
781 |
// "N" to allow for parsing "NaN" and "Infinity" correctly. |
|
782 |
if ((f < '0' || f > '9') && f != '.' && f != 'I' && f != 'N') { |
|
783 |
return Double.NaN; |
|
784 |
} |
|
785 |
try { |
|
786 |
value = Double.parseDouble(str.substring(start, end)); |
|
787 |
} catch (final NumberFormatException e) { |
|
788 |
return Double.NaN; |
|
789 |
} |
|
790 |
} |
|
791 |
||
792 |
return negative ? -value : value; |
|
793 |
} |
|
794 |
||
795 |
/** |
|
17764
29e6eb3bb9df
8010804: Review long and integer usage conventions
hannesw
parents:
17241
diff
changeset
|
796 |
* JavaScript compliant Object to integer conversion. See ECMA 9.4 ToInteger |
29e6eb3bb9df
8010804: Review long and integer usage conventions
hannesw
parents:
17241
diff
changeset
|
797 |
* |
29e6eb3bb9df
8010804: Review long and integer usage conventions
hannesw
parents:
17241
diff
changeset
|
798 |
* <p>Note that this returns {@link java.lang.Integer#MAX_VALUE} or {@link java.lang.Integer#MIN_VALUE} |
29e6eb3bb9df
8010804: Review long and integer usage conventions
hannesw
parents:
17241
diff
changeset
|
799 |
* for double values that exceed the int range, including positive and negative Infinity. It is the |
29e6eb3bb9df
8010804: Review long and integer usage conventions
hannesw
parents:
17241
diff
changeset
|
800 |
* caller's responsibility to handle such values correctly.</p> |
16147 | 801 |
* |
802 |
* @param obj an object |
|
803 |
* @return an integer |
|
804 |
*/ |
|
805 |
public static int toInteger(final Object obj) { |
|
806 |
return (int)toNumber(obj); |
|
807 |
} |
|
808 |
||
809 |
/** |
|
24719 | 810 |
* Converts an Object to long. |
17764
29e6eb3bb9df
8010804: Review long and integer usage conventions
hannesw
parents:
17241
diff
changeset
|
811 |
* |
29e6eb3bb9df
8010804: Review long and integer usage conventions
hannesw
parents:
17241
diff
changeset
|
812 |
* <p>Note that this returns {@link java.lang.Long#MAX_VALUE} or {@link java.lang.Long#MIN_VALUE} |
29e6eb3bb9df
8010804: Review long and integer usage conventions
hannesw
parents:
17241
diff
changeset
|
813 |
* for double values that exceed the long range, including positive and negative Infinity. It is the |
29e6eb3bb9df
8010804: Review long and integer usage conventions
hannesw
parents:
17241
diff
changeset
|
814 |
* caller's responsibility to handle such values correctly.</p> |
16147 | 815 |
* |
816 |
* @param obj an object |
|
817 |
* @return a long |
|
818 |
*/ |
|
819 |
public static long toLong(final Object obj) { |
|
24719 | 820 |
return obj instanceof Long ? ((Long)obj).longValue() : toLong(toNumber(obj)); |
821 |
} |
|
822 |
||
823 |
/** |
|
824 |
* Converts a double to long. |
|
825 |
* |
|
826 |
* @param num the double to convert |
|
827 |
* @return the converted long value |
|
828 |
*/ |
|
829 |
public static long toLong(final double num) { |
|
830 |
return (long)num; |
|
831 |
} |
|
832 |
||
833 |
/** |
|
834 |
* Optimistic long conversion - throws UnwarrantedOptimismException if double or Object |
|
835 |
* |
|
836 |
* @param obj object to convert |
|
837 |
* @param programPoint program point |
|
838 |
* @return long |
|
839 |
*/ |
|
840 |
public static long toLongOptimistic(final Object obj, final int programPoint) { |
|
841 |
if (obj != null) { |
|
842 |
final Class<?> clz = obj.getClass(); |
|
843 |
if (clz == Long.class || clz == Integer.class) { |
|
844 |
return ((Number)obj).longValue(); |
|
845 |
} |
|
846 |
} |
|
847 |
throw new UnwarrantedOptimismException(obj, programPoint); |
|
848 |
} |
|
849 |
||
850 |
/** |
|
851 |
* Object to int conversion that delegates to either {@link #toLong(Object)} or to |
|
852 |
* {@link #toLongOptimistic(Object, int)} depending on whether the program point is valid or not. |
|
853 |
* @param obj the object to convert |
|
854 |
* @param programPoint the program point; can be invalid. |
|
855 |
* @return the value converted to long |
|
856 |
* @throws UnwarrantedOptimismException if the value can't be represented as long and the program point is valid. |
|
857 |
*/ |
|
858 |
public static long toLongMaybeOptimistic(final Object obj, final int programPoint) { |
|
859 |
return UnwarrantedOptimismException.isValid(programPoint) ? toLongOptimistic(obj, programPoint) : toLong(obj); |
|
16147 | 860 |
} |
861 |
||
862 |
/** |
|
863 |
* JavaScript compliant Object to int32 conversion |
|
864 |
* See ECMA 9.5 ToInt32 |
|
865 |
* |
|
866 |
* @param obj an object |
|
867 |
* @return an int32 |
|
868 |
*/ |
|
869 |
public static int toInt32(final Object obj) { |
|
870 |
return toInt32(toNumber(obj)); |
|
871 |
} |
|
872 |
||
873 |
/** |
|
24719 | 874 |
* Optimistic int conversion - throws UnwarrantedOptimismException if double, long or Object |
875 |
* |
|
876 |
* @param obj object to convert |
|
877 |
* @param programPoint program point |
|
878 |
* @return double |
|
879 |
*/ |
|
880 |
public static int toInt32Optimistic(final Object obj, final int programPoint) { |
|
881 |
if (obj != null) { |
|
882 |
final Class<?> clz = obj.getClass(); |
|
883 |
if (clz == Integer.class) { |
|
884 |
return ((Integer)obj).intValue(); |
|
885 |
} |
|
886 |
} |
|
887 |
throw new UnwarrantedOptimismException(obj, programPoint); |
|
888 |
} |
|
889 |
||
890 |
/** |
|
891 |
* Object to int conversion that delegates to either {@link #toInt32(Object)} or to |
|
892 |
* {@link #toInt32Optimistic(Object, int)} depending on whether the program point is valid or not. |
|
893 |
* @param obj the object to convert |
|
894 |
* @param programPoint the program point; can be invalid. |
|
895 |
* @return the value converted to int |
|
896 |
* @throws UnwarrantedOptimismException if the value can't be represented as int and the program point is valid. |
|
897 |
*/ |
|
898 |
public static int toInt32MaybeOptimistic(final Object obj, final int programPoint) { |
|
899 |
return UnwarrantedOptimismException.isValid(programPoint) ? toInt32Optimistic(obj, programPoint) : toInt32(obj); |
|
900 |
} |
|
901 |
||
902 |
// Minimum and maximum range between which every long value can be precisely represented as a double. |
|
903 |
private static final long MAX_PRECISE_DOUBLE = (1L << 53); |
|
904 |
private static final long MIN_PRECISE_DOUBLE = -MAX_PRECISE_DOUBLE; |
|
905 |
||
906 |
/** |
|
16147 | 907 |
* JavaScript compliant long to int32 conversion |
908 |
* |
|
909 |
* @param num a long |
|
910 |
* @return an int32 |
|
911 |
*/ |
|
912 |
public static int toInt32(final long num) { |
|
24719 | 913 |
return (int)(num >= MIN_PRECISE_DOUBLE && num <= MAX_PRECISE_DOUBLE ? num : (long)(num % INT32_LIMIT)); |
16147 | 914 |
} |
915 |
||
24719 | 916 |
|
16147 | 917 |
/** |
918 |
* JavaScript compliant number to int32 conversion |
|
919 |
* |
|
920 |
* @param num a number |
|
921 |
* @return an int32 |
|
922 |
*/ |
|
923 |
public static int toInt32(final double num) { |
|
17241
c337fefb8c84
8012334: ToUint32, ToInt32, and ToUint16 don't conform to spec
hannesw
parents:
16256
diff
changeset
|
924 |
return (int)doubleToInt32(num); |
16147 | 925 |
} |
926 |
||
927 |
/** |
|
928 |
* JavaScript compliant Object to uint32 conversion |
|
929 |
* |
|
930 |
* @param obj an object |
|
931 |
* @return a uint32 |
|
932 |
*/ |
|
933 |
public static long toUint32(final Object obj) { |
|
934 |
return toUint32(toNumber(obj)); |
|
935 |
} |
|
936 |
||
937 |
/** |
|
938 |
* JavaScript compliant number to uint32 conversion |
|
939 |
* |
|
940 |
* @param num a number |
|
941 |
* @return a uint32 |
|
942 |
*/ |
|
943 |
public static long toUint32(final double num) { |
|
17241
c337fefb8c84
8012334: ToUint32, ToInt32, and ToUint16 don't conform to spec
hannesw
parents:
16256
diff
changeset
|
944 |
return doubleToInt32(num) & MAX_UINT; |
16147 | 945 |
} |
946 |
||
947 |
/** |
|
948 |
* JavaScript compliant Object to uint16 conversion |
|
949 |
* ECMA 9.7 ToUint16: (Unsigned 16 Bit Integer) |
|
950 |
* |
|
951 |
* @param obj an object |
|
952 |
* @return a uint16 |
|
953 |
*/ |
|
954 |
public static int toUint16(final Object obj) { |
|
955 |
return toUint16(toNumber(obj)); |
|
956 |
} |
|
957 |
||
958 |
/** |
|
959 |
* JavaScript compliant number to uint16 conversion |
|
960 |
* |
|
961 |
* @param num a number |
|
962 |
* @return a uint16 |
|
963 |
*/ |
|
964 |
public static int toUint16(final int num) { |
|
965 |
return num & 0xffff; |
|
966 |
} |
|
967 |
||
968 |
/** |
|
969 |
* JavaScript compliant number to uint16 conversion |
|
970 |
* |
|
971 |
* @param num a number |
|
972 |
* @return a uint16 |
|
973 |
*/ |
|
974 |
public static int toUint16(final long num) { |
|
975 |
return ((int)num) & 0xffff; |
|
976 |
} |
|
977 |
||
978 |
/** |
|
979 |
* JavaScript compliant number to uint16 conversion |
|
980 |
* |
|
981 |
* @param num a number |
|
982 |
* @return a uint16 |
|
983 |
*/ |
|
984 |
public static int toUint16(final double num) { |
|
17241
c337fefb8c84
8012334: ToUint32, ToInt32, and ToUint16 don't conform to spec
hannesw
parents:
16256
diff
changeset
|
985 |
return ((int)doubleToInt32(num)) & 0xffff; |
c337fefb8c84
8012334: ToUint32, ToInt32, and ToUint16 don't conform to spec
hannesw
parents:
16256
diff
changeset
|
986 |
} |
c337fefb8c84
8012334: ToUint32, ToInt32, and ToUint16 don't conform to spec
hannesw
parents:
16256
diff
changeset
|
987 |
|
c337fefb8c84
8012334: ToUint32, ToInt32, and ToUint16 don't conform to spec
hannesw
parents:
16256
diff
changeset
|
988 |
private static long doubleToInt32(final double num) { |
c337fefb8c84
8012334: ToUint32, ToInt32, and ToUint16 don't conform to spec
hannesw
parents:
16256
diff
changeset
|
989 |
final int exponent = Math.getExponent(num); |
c337fefb8c84
8012334: ToUint32, ToInt32, and ToUint16 don't conform to spec
hannesw
parents:
16256
diff
changeset
|
990 |
if (exponent < 31) { |
c337fefb8c84
8012334: ToUint32, ToInt32, and ToUint16 don't conform to spec
hannesw
parents:
16256
diff
changeset
|
991 |
return (long) num; // Fits into 32 bits |
c337fefb8c84
8012334: ToUint32, ToInt32, and ToUint16 don't conform to spec
hannesw
parents:
16256
diff
changeset
|
992 |
} |
c337fefb8c84
8012334: ToUint32, ToInt32, and ToUint16 don't conform to spec
hannesw
parents:
16256
diff
changeset
|
993 |
if (exponent >= 84) { |
c337fefb8c84
8012334: ToUint32, ToInt32, and ToUint16 don't conform to spec
hannesw
parents:
16256
diff
changeset
|
994 |
// Either infinite or NaN or so large that shift / modulo will produce 0 |
c337fefb8c84
8012334: ToUint32, ToInt32, and ToUint16 don't conform to spec
hannesw
parents:
16256
diff
changeset
|
995 |
// (52 bit mantissa + 32 bit target width). |
16147 | 996 |
return 0; |
997 |
} |
|
17241
c337fefb8c84
8012334: ToUint32, ToInt32, and ToUint16 don't conform to spec
hannesw
parents:
16256
diff
changeset
|
998 |
// This is rather slow and could probably be sped up using bit-fiddling. |
c337fefb8c84
8012334: ToUint32, ToInt32, and ToUint16 don't conform to spec
hannesw
parents:
16256
diff
changeset
|
999 |
final double d = (num >= 0) ? Math.floor(num) : Math.ceil(num); |
c337fefb8c84
8012334: ToUint32, ToInt32, and ToUint16 don't conform to spec
hannesw
parents:
16256
diff
changeset
|
1000 |
return (long)(d % INT32_LIMIT); |
16147 | 1001 |
} |
1002 |
||
1003 |
/** |
|
1004 |
* Check whether a number is finite |
|
1005 |
* |
|
1006 |
* @param num a number |
|
1007 |
* @return true if finite |
|
1008 |
*/ |
|
1009 |
public static boolean isFinite(final double num) { |
|
1010 |
return !Double.isInfinite(num) && !Double.isNaN(num); |
|
1011 |
} |
|
1012 |
||
1013 |
/** |
|
1014 |
* Convert a primitive to a double |
|
1015 |
* |
|
1016 |
* @param num a double |
|
1017 |
* @return a boxed double |
|
1018 |
*/ |
|
1019 |
public static Double toDouble(final double num) { |
|
1020 |
return num; |
|
1021 |
} |
|
1022 |
||
1023 |
/** |
|
1024 |
* Convert a primitive to a double |
|
1025 |
* |
|
1026 |
* @param num a long |
|
1027 |
* @return a boxed double |
|
1028 |
*/ |
|
1029 |
public static Double toDouble(final long num) { |
|
1030 |
return (double)num; |
|
1031 |
} |
|
1032 |
||
1033 |
/** |
|
1034 |
* Convert a primitive to a double |
|
1035 |
* |
|
1036 |
* @param num an int |
|
1037 |
* @return a boxed double |
|
1038 |
*/ |
|
1039 |
public static Double toDouble(final int num) { |
|
1040 |
return (double)num; |
|
1041 |
} |
|
1042 |
||
1043 |
/** |
|
1044 |
* Convert a boolean to an Object |
|
1045 |
* |
|
1046 |
* @param bool a boolean |
|
1047 |
* @return a boxed boolean, its Object representation |
|
1048 |
*/ |
|
1049 |
public static Object toObject(final boolean bool) { |
|
1050 |
return bool; |
|
1051 |
} |
|
1052 |
||
1053 |
/** |
|
1054 |
* Convert a number to an Object |
|
1055 |
* |
|
1056 |
* @param num an integer |
|
1057 |
* @return the boxed number |
|
1058 |
*/ |
|
1059 |
public static Object toObject(final int num) { |
|
1060 |
return num; |
|
1061 |
} |
|
1062 |
||
1063 |
/** |
|
1064 |
* Convert a number to an Object |
|
1065 |
* |
|
1066 |
* @param num a long |
|
1067 |
* @return the boxed number |
|
1068 |
*/ |
|
1069 |
public static Object toObject(final long num) { |
|
1070 |
return num; |
|
1071 |
} |
|
1072 |
||
1073 |
/** |
|
1074 |
* Convert a number to an Object |
|
1075 |
* |
|
1076 |
* @param num a double |
|
1077 |
* @return the boxed number |
|
1078 |
*/ |
|
1079 |
public static Object toObject(final double num) { |
|
1080 |
return num; |
|
1081 |
} |
|
1082 |
||
1083 |
/** |
|
1084 |
* Identity converter for objects. |
|
1085 |
* |
|
1086 |
* @param obj an object |
|
1087 |
* @return the boxed number |
|
1088 |
*/ |
|
1089 |
public static Object toObject(final Object obj) { |
|
1090 |
return obj; |
|
1091 |
} |
|
1092 |
||
1093 |
/** |
|
1094 |
* Object conversion. This is used to convert objects and numbers to their corresponding |
|
1095 |
* NativeObject type |
|
1096 |
* See ECMA 9.9 ToObject |
|
1097 |
* |
|
16188 | 1098 |
* @param obj the object to convert |
1099 |
* |
|
1100 |
* @return the wrapped object |
|
1101 |
*/ |
|
1102 |
public static Object toScriptObject(final Object obj) { |
|
1103 |
return toScriptObject(Context.getGlobalTrusted(), obj); |
|
1104 |
} |
|
1105 |
||
1106 |
/** |
|
1107 |
* Object conversion. This is used to convert objects and numbers to their corresponding |
|
1108 |
* NativeObject type |
|
1109 |
* See ECMA 9.9 ToObject |
|
1110 |
* |
|
16147 | 1111 |
* @param global the global object |
1112 |
* @param obj the object to convert |
|
1113 |
* |
|
1114 |
* @return the wrapped object |
|
1115 |
*/ |
|
16188 | 1116 |
public static Object toScriptObject(final ScriptObject global, final Object obj) { |
16147 | 1117 |
if (nullOrUndefined(obj)) { |
16256
f2d9a0c49914
8007002: Replace implicit exception throwing methods with explicit throws - simplify control flow and remove useless code
lagergren
parents:
16234
diff
changeset
|
1118 |
throw typeError(global, "not.an.object", ScriptRuntime.safeToString(obj)); |
16147 | 1119 |
} |
1120 |
||
1121 |
if (obj instanceof ScriptObject) { |
|
1122 |
return obj; |
|
1123 |
} |
|
1124 |
||
1125 |
return ((GlobalObject)global).wrapAsObject(obj); |
|
1126 |
} |
|
1127 |
||
1128 |
/** |
|
19897
4235a28bb5e8
8024847: Java.to should accept mirror and external JSObjects as array-like objects as well
sundar
parents:
19892
diff
changeset
|
1129 |
* Script object to Java array conversion. |
4235a28bb5e8
8024847: Java.to should accept mirror and external JSObjects as array-like objects as well
sundar
parents:
19892
diff
changeset
|
1130 |
* |
4235a28bb5e8
8024847: Java.to should accept mirror and external JSObjects as array-like objects as well
sundar
parents:
19892
diff
changeset
|
1131 |
* @param obj script object to be converted to Java array |
4235a28bb5e8
8024847: Java.to should accept mirror and external JSObjects as array-like objects as well
sundar
parents:
19892
diff
changeset
|
1132 |
* @param componentType component type of the destination array required |
4235a28bb5e8
8024847: Java.to should accept mirror and external JSObjects as array-like objects as well
sundar
parents:
19892
diff
changeset
|
1133 |
* @return converted Java array |
4235a28bb5e8
8024847: Java.to should accept mirror and external JSObjects as array-like objects as well
sundar
parents:
19892
diff
changeset
|
1134 |
*/ |
4235a28bb5e8
8024847: Java.to should accept mirror and external JSObjects as array-like objects as well
sundar
parents:
19892
diff
changeset
|
1135 |
public static Object toJavaArray(final Object obj, final Class<?> componentType) { |
4235a28bb5e8
8024847: Java.to should accept mirror and external JSObjects as array-like objects as well
sundar
parents:
19892
diff
changeset
|
1136 |
if (obj instanceof ScriptObject) { |
21686
5c6946f97d6f
8027236: Ensure ScriptObject and ConsString aren't visible to Java
attila
parents:
21453
diff
changeset
|
1137 |
return ((ScriptObject)obj).getArray().asArrayOfType(componentType); |
19897
4235a28bb5e8
8024847: Java.to should accept mirror and external JSObjects as array-like objects as well
sundar
parents:
19892
diff
changeset
|
1138 |
} else if (obj instanceof JSObject) { |
20928
3ff39d5c8c08
8026137: Fix Issues with Binary Evaluation Order
lagergren
parents:
19897
diff
changeset
|
1139 |
final ArrayLikeIterator<?> itr = ArrayLikeIterator.arrayLikeIterator(obj); |
19897
4235a28bb5e8
8024847: Java.to should accept mirror and external JSObjects as array-like objects as well
sundar
parents:
19892
diff
changeset
|
1140 |
final int len = (int) itr.getLength(); |
4235a28bb5e8
8024847: Java.to should accept mirror and external JSObjects as array-like objects as well
sundar
parents:
19892
diff
changeset
|
1141 |
final Object[] res = new Object[len]; |
4235a28bb5e8
8024847: Java.to should accept mirror and external JSObjects as array-like objects as well
sundar
parents:
19892
diff
changeset
|
1142 |
int idx = 0; |
4235a28bb5e8
8024847: Java.to should accept mirror and external JSObjects as array-like objects as well
sundar
parents:
19892
diff
changeset
|
1143 |
while (itr.hasNext()) { |
4235a28bb5e8
8024847: Java.to should accept mirror and external JSObjects as array-like objects as well
sundar
parents:
19892
diff
changeset
|
1144 |
res[idx++] = itr.next(); |
4235a28bb5e8
8024847: Java.to should accept mirror and external JSObjects as array-like objects as well
sundar
parents:
19892
diff
changeset
|
1145 |
} |
4235a28bb5e8
8024847: Java.to should accept mirror and external JSObjects as array-like objects as well
sundar
parents:
19892
diff
changeset
|
1146 |
return convertArray(res, componentType); |
20944
a166d40ac0d4
8026113: Nashorn arrays should automatically convert to Java arrays
attila
parents:
20928
diff
changeset
|
1147 |
} else if(obj == null) { |
a166d40ac0d4
8026113: Nashorn arrays should automatically convert to Java arrays
attila
parents:
20928
diff
changeset
|
1148 |
return null; |
19897
4235a28bb5e8
8024847: Java.to should accept mirror and external JSObjects as array-like objects as well
sundar
parents:
19892
diff
changeset
|
1149 |
} else { |
4235a28bb5e8
8024847: Java.to should accept mirror and external JSObjects as array-like objects as well
sundar
parents:
19892
diff
changeset
|
1150 |
throw new IllegalArgumentException("not a script object"); |
4235a28bb5e8
8024847: Java.to should accept mirror and external JSObjects as array-like objects as well
sundar
parents:
19892
diff
changeset
|
1151 |
} |
4235a28bb5e8
8024847: Java.to should accept mirror and external JSObjects as array-like objects as well
sundar
parents:
19892
diff
changeset
|
1152 |
} |
4235a28bb5e8
8024847: Java.to should accept mirror and external JSObjects as array-like objects as well
sundar
parents:
19892
diff
changeset
|
1153 |
|
4235a28bb5e8
8024847: Java.to should accept mirror and external JSObjects as array-like objects as well
sundar
parents:
19892
diff
changeset
|
1154 |
/** |
4235a28bb5e8
8024847: Java.to should accept mirror and external JSObjects as array-like objects as well
sundar
parents:
19892
diff
changeset
|
1155 |
* Java array to java array conversion - but using type conversions implemented by linker. |
4235a28bb5e8
8024847: Java.to should accept mirror and external JSObjects as array-like objects as well
sundar
parents:
19892
diff
changeset
|
1156 |
* |
4235a28bb5e8
8024847: Java.to should accept mirror and external JSObjects as array-like objects as well
sundar
parents:
19892
diff
changeset
|
1157 |
* @param src source array |
4235a28bb5e8
8024847: Java.to should accept mirror and external JSObjects as array-like objects as well
sundar
parents:
19892
diff
changeset
|
1158 |
* @param componentType component type of the destination array required |
4235a28bb5e8
8024847: Java.to should accept mirror and external JSObjects as array-like objects as well
sundar
parents:
19892
diff
changeset
|
1159 |
* @return converted Java array |
4235a28bb5e8
8024847: Java.to should accept mirror and external JSObjects as array-like objects as well
sundar
parents:
19892
diff
changeset
|
1160 |
*/ |
4235a28bb5e8
8024847: Java.to should accept mirror and external JSObjects as array-like objects as well
sundar
parents:
19892
diff
changeset
|
1161 |
public static Object convertArray(final Object[] src, final Class<?> componentType) { |
21686
5c6946f97d6f
8027236: Ensure ScriptObject and ConsString aren't visible to Java
attila
parents:
21453
diff
changeset
|
1162 |
if(componentType == Object.class) { |
5c6946f97d6f
8027236: Ensure ScriptObject and ConsString aren't visible to Java
attila
parents:
21453
diff
changeset
|
1163 |
for(int i = 0; i < src.length; ++i) { |
5c6946f97d6f
8027236: Ensure ScriptObject and ConsString aren't visible to Java
attila
parents:
21453
diff
changeset
|
1164 |
final Object e = src[i]; |
5c6946f97d6f
8027236: Ensure ScriptObject and ConsString aren't visible to Java
attila
parents:
21453
diff
changeset
|
1165 |
if(e instanceof ConsString) { |
5c6946f97d6f
8027236: Ensure ScriptObject and ConsString aren't visible to Java
attila
parents:
21453
diff
changeset
|
1166 |
src[i] = e.toString(); |
5c6946f97d6f
8027236: Ensure ScriptObject and ConsString aren't visible to Java
attila
parents:
21453
diff
changeset
|
1167 |
} |
5c6946f97d6f
8027236: Ensure ScriptObject and ConsString aren't visible to Java
attila
parents:
21453
diff
changeset
|
1168 |
} |
5c6946f97d6f
8027236: Ensure ScriptObject and ConsString aren't visible to Java
attila
parents:
21453
diff
changeset
|
1169 |
} |
5c6946f97d6f
8027236: Ensure ScriptObject and ConsString aren't visible to Java
attila
parents:
21453
diff
changeset
|
1170 |
|
19897
4235a28bb5e8
8024847: Java.to should accept mirror and external JSObjects as array-like objects as well
sundar
parents:
19892
diff
changeset
|
1171 |
final int l = src.length; |
4235a28bb5e8
8024847: Java.to should accept mirror and external JSObjects as array-like objects as well
sundar
parents:
19892
diff
changeset
|
1172 |
final Object dst = Array.newInstance(componentType, l); |
4235a28bb5e8
8024847: Java.to should accept mirror and external JSObjects as array-like objects as well
sundar
parents:
19892
diff
changeset
|
1173 |
final MethodHandle converter = Bootstrap.getLinkerServices().getTypeConverter(Object.class, componentType); |
4235a28bb5e8
8024847: Java.to should accept mirror and external JSObjects as array-like objects as well
sundar
parents:
19892
diff
changeset
|
1174 |
try { |
4235a28bb5e8
8024847: Java.to should accept mirror and external JSObjects as array-like objects as well
sundar
parents:
19892
diff
changeset
|
1175 |
for (int i = 0; i < src.length; i++) { |
4235a28bb5e8
8024847: Java.to should accept mirror and external JSObjects as array-like objects as well
sundar
parents:
19892
diff
changeset
|
1176 |
Array.set(dst, i, invoke(converter, src[i])); |
4235a28bb5e8
8024847: Java.to should accept mirror and external JSObjects as array-like objects as well
sundar
parents:
19892
diff
changeset
|
1177 |
} |
4235a28bb5e8
8024847: Java.to should accept mirror and external JSObjects as array-like objects as well
sundar
parents:
19892
diff
changeset
|
1178 |
} catch (final RuntimeException | Error e) { |
4235a28bb5e8
8024847: Java.to should accept mirror and external JSObjects as array-like objects as well
sundar
parents:
19892
diff
changeset
|
1179 |
throw e; |
4235a28bb5e8
8024847: Java.to should accept mirror and external JSObjects as array-like objects as well
sundar
parents:
19892
diff
changeset
|
1180 |
} catch (final Throwable t) { |
4235a28bb5e8
8024847: Java.to should accept mirror and external JSObjects as array-like objects as well
sundar
parents:
19892
diff
changeset
|
1181 |
throw new RuntimeException(t); |
4235a28bb5e8
8024847: Java.to should accept mirror and external JSObjects as array-like objects as well
sundar
parents:
19892
diff
changeset
|
1182 |
} |
4235a28bb5e8
8024847: Java.to should accept mirror and external JSObjects as array-like objects as well
sundar
parents:
19892
diff
changeset
|
1183 |
return dst; |
4235a28bb5e8
8024847: Java.to should accept mirror and external JSObjects as array-like objects as well
sundar
parents:
19892
diff
changeset
|
1184 |
} |
4235a28bb5e8
8024847: Java.to should accept mirror and external JSObjects as array-like objects as well
sundar
parents:
19892
diff
changeset
|
1185 |
|
4235a28bb5e8
8024847: Java.to should accept mirror and external JSObjects as array-like objects as well
sundar
parents:
19892
diff
changeset
|
1186 |
/** |
20944
a166d40ac0d4
8026113: Nashorn arrays should automatically convert to Java arrays
attila
parents:
20928
diff
changeset
|
1187 |
* Converts a JavaScript object to a Java List. See {@link ListAdapter} for details. |
a166d40ac0d4
8026113: Nashorn arrays should automatically convert to Java arrays
attila
parents:
20928
diff
changeset
|
1188 |
* @param obj the object to convert. Can be any array-like object. |
a166d40ac0d4
8026113: Nashorn arrays should automatically convert to Java arrays
attila
parents:
20928
diff
changeset
|
1189 |
* @return a List that is live-backed by the JavaScript object. |
a166d40ac0d4
8026113: Nashorn arrays should automatically convert to Java arrays
attila
parents:
20928
diff
changeset
|
1190 |
*/ |
a166d40ac0d4
8026113: Nashorn arrays should automatically convert to Java arrays
attila
parents:
20928
diff
changeset
|
1191 |
public static List<?> toJavaList(final Object obj) { |
a166d40ac0d4
8026113: Nashorn arrays should automatically convert to Java arrays
attila
parents:
20928
diff
changeset
|
1192 |
return ListAdapter.create(obj); |
a166d40ac0d4
8026113: Nashorn arrays should automatically convert to Java arrays
attila
parents:
20928
diff
changeset
|
1193 |
} |
a166d40ac0d4
8026113: Nashorn arrays should automatically convert to Java arrays
attila
parents:
20928
diff
changeset
|
1194 |
|
a166d40ac0d4
8026113: Nashorn arrays should automatically convert to Java arrays
attila
parents:
20928
diff
changeset
|
1195 |
/** |
a166d40ac0d4
8026113: Nashorn arrays should automatically convert to Java arrays
attila
parents:
20928
diff
changeset
|
1196 |
* Converts a JavaScript object to a Java Deque. See {@link ListAdapter} for details. |
a166d40ac0d4
8026113: Nashorn arrays should automatically convert to Java arrays
attila
parents:
20928
diff
changeset
|
1197 |
* @param obj the object to convert. Can be any array-like object. |
a166d40ac0d4
8026113: Nashorn arrays should automatically convert to Java arrays
attila
parents:
20928
diff
changeset
|
1198 |
* @return a Deque that is live-backed by the JavaScript object. |
a166d40ac0d4
8026113: Nashorn arrays should automatically convert to Java arrays
attila
parents:
20928
diff
changeset
|
1199 |
*/ |
a166d40ac0d4
8026113: Nashorn arrays should automatically convert to Java arrays
attila
parents:
20928
diff
changeset
|
1200 |
public static Deque<?> toJavaDeque(final Object obj) { |
a166d40ac0d4
8026113: Nashorn arrays should automatically convert to Java arrays
attila
parents:
20928
diff
changeset
|
1201 |
return ListAdapter.create(obj); |
a166d40ac0d4
8026113: Nashorn arrays should automatically convert to Java arrays
attila
parents:
20928
diff
changeset
|
1202 |
} |
a166d40ac0d4
8026113: Nashorn arrays should automatically convert to Java arrays
attila
parents:
20928
diff
changeset
|
1203 |
|
a166d40ac0d4
8026113: Nashorn arrays should automatically convert to Java arrays
attila
parents:
20928
diff
changeset
|
1204 |
/** |
16147 | 1205 |
* Check if an object is null or undefined |
1206 |
* |
|
1207 |
* @param obj object to check |
|
1208 |
* |
|
1209 |
* @return true if null or undefined |
|
1210 |
*/ |
|
1211 |
public static boolean nullOrUndefined(final Object obj) { |
|
1212 |
return obj == null || obj == ScriptRuntime.UNDEFINED; |
|
1213 |
} |
|
1214 |
||
1215 |
static String toStringImpl(final Object obj, final boolean safe) { |
|
1216 |
if (obj instanceof String) { |
|
1217 |
return (String)obj; |
|
1218 |
} |
|
1219 |
||
1220 |
if (obj instanceof Number) { |
|
1221 |
return toString(((Number)obj).doubleValue()); |
|
1222 |
} |
|
1223 |
||
1224 |
if (obj == ScriptRuntime.UNDEFINED) { |
|
1225 |
return "undefined"; |
|
1226 |
} |
|
1227 |
||
1228 |
if (obj == null) { |
|
1229 |
return "null"; |
|
1230 |
} |
|
1231 |
||
1232 |
if (obj instanceof ScriptObject) { |
|
1233 |
if (safe) { |
|
1234 |
final ScriptObject sobj = (ScriptObject)obj; |
|
16188 | 1235 |
final GlobalObject gobj = (GlobalObject)Context.getGlobalTrusted(); |
16147 | 1236 |
return gobj.isError(sobj) ? |
1237 |
ECMAException.safeToString(sobj) : |
|
1238 |
sobj.safeToString(); |
|
1239 |
} |
|
1240 |
||
1241 |
return toString(toPrimitive(obj, String.class)); |
|
1242 |
} |
|
1243 |
||
1244 |
if (obj instanceof StaticClass) { |
|
1245 |
return "[JavaClass " + ((StaticClass)obj).getRepresentedClass().getName() + "]"; |
|
1246 |
} |
|
1247 |
||
1248 |
return obj.toString(); |
|
1249 |
} |
|
1250 |
||
1251 |
// trim from left for JS whitespaces. |
|
1252 |
static String trimLeft(final String str) { |
|
1253 |
int start = 0; |
|
1254 |
||
1255 |
while (start < str.length() && Lexer.isJSWhitespace(str.charAt(start))) { |
|
1256 |
start++; |
|
1257 |
} |
|
1258 |
||
1259 |
return str.substring(start); |
|
1260 |
} |
|
1261 |
||
24719 | 1262 |
/** |
1263 |
* Throw an unwarranted optimism exception for a program point |
|
1264 |
* @param value real return value |
|
1265 |
* @param programPoint program point |
|
1266 |
* @return |
|
1267 |
*/ |
|
1268 |
@SuppressWarnings("unused") |
|
1269 |
private static Object throwUnwarrantedOptimismException(final Object value, final int programPoint) { |
|
1270 |
throw new UnwarrantedOptimismException(value, programPoint); |
|
1271 |
} |
|
1272 |
||
1273 |
/** |
|
1274 |
* Wrapper for addExact |
|
1275 |
* |
|
1276 |
* Catches ArithmeticException and rethrows as UnwarrantedOptimismException |
|
1277 |
* containing the result and the program point of the failure |
|
1278 |
* |
|
1279 |
* @param x first term |
|
1280 |
* @param y second term |
|
1281 |
* @param programPoint program point id |
|
1282 |
* @return the result |
|
1283 |
* @throws UnwarrantedOptimismException if overflow occurs |
|
1284 |
*/ |
|
1285 |
public static int addExact(final int x, final int y, final int programPoint) throws UnwarrantedOptimismException { |
|
1286 |
try { |
|
1287 |
return Math.addExact(x, y); |
|
1288 |
} catch (ArithmeticException e) { |
|
1289 |
throw new UnwarrantedOptimismException((long)x + (long)y, programPoint); |
|
1290 |
} |
|
1291 |
} |
|
1292 |
||
1293 |
/** |
|
1294 |
* Wrapper for addExact |
|
1295 |
* |
|
1296 |
* Catches ArithmeticException and rethrows as UnwarrantedOptimismException |
|
1297 |
* containing the result and the program point of the failure |
|
1298 |
* |
|
1299 |
* @param x first term |
|
1300 |
* @param y second term |
|
1301 |
* @param programPoint program point id |
|
1302 |
* @return the result |
|
1303 |
* @throws UnwarrantedOptimismException if overflow occurs |
|
1304 |
*/ |
|
1305 |
public static long addExact(final long x, final long y, final int programPoint) throws UnwarrantedOptimismException { |
|
1306 |
try { |
|
1307 |
return Math.addExact(x, y); |
|
1308 |
} catch (ArithmeticException e) { |
|
1309 |
throw new UnwarrantedOptimismException((double)x + (double)y, programPoint); |
|
1310 |
} |
|
1311 |
} |
|
1312 |
||
1313 |
/** |
|
1314 |
* Wrapper for subExact |
|
1315 |
* |
|
1316 |
* Catches ArithmeticException and rethrows as UnwarrantedOptimismException |
|
1317 |
* containing the result and the program point of the failure |
|
1318 |
* |
|
1319 |
* @param x first term |
|
1320 |
* @param y second term |
|
1321 |
* @param programPoint program point id |
|
1322 |
* @return the result |
|
1323 |
* @throws UnwarrantedOptimismException if overflow occurs |
|
1324 |
*/ |
|
1325 |
public static int subExact(final int x, final int y, final int programPoint) throws UnwarrantedOptimismException { |
|
1326 |
try { |
|
1327 |
return Math.subtractExact(x, y); |
|
1328 |
} catch (ArithmeticException e) { |
|
1329 |
throw new UnwarrantedOptimismException((long)x - (long)y, programPoint); |
|
1330 |
} |
|
1331 |
} |
|
1332 |
||
1333 |
/** |
|
1334 |
* Wrapper for subExact |
|
1335 |
* |
|
1336 |
* Catches ArithmeticException and rethrows as UnwarrantedOptimismException |
|
1337 |
* containing the result and the program point of the failure |
|
1338 |
* |
|
1339 |
* @param x first term |
|
1340 |
* @param y second term |
|
1341 |
* @param programPoint program point id |
|
1342 |
* @return the result |
|
1343 |
* @throws UnwarrantedOptimismException if overflow occurs |
|
1344 |
*/ |
|
1345 |
public static long subExact(final long x, final long y, final int programPoint) throws UnwarrantedOptimismException { |
|
1346 |
try { |
|
1347 |
return Math.subtractExact(x, y); |
|
1348 |
} catch (ArithmeticException e) { |
|
1349 |
throw new UnwarrantedOptimismException((double)x - (double)y, programPoint); |
|
1350 |
} |
|
1351 |
} |
|
1352 |
||
1353 |
/** |
|
1354 |
* Wrapper for mulExact |
|
1355 |
* |
|
1356 |
* Catches ArithmeticException and rethrows as UnwarrantedOptimismException |
|
1357 |
* containing the result and the program point of the failure |
|
1358 |
* |
|
1359 |
* @param x first term |
|
1360 |
* @param y second term |
|
1361 |
* @param programPoint program point id |
|
1362 |
* @return the result |
|
1363 |
* @throws UnwarrantedOptimismException if overflow occurs |
|
1364 |
*/ |
|
1365 |
public static int mulExact(final int x, final int y, final int programPoint) throws UnwarrantedOptimismException { |
|
1366 |
try { |
|
1367 |
return Math.multiplyExact(x, y); |
|
1368 |
} catch (ArithmeticException e) { |
|
1369 |
throw new UnwarrantedOptimismException((long)x * (long)y, programPoint); |
|
1370 |
} |
|
1371 |
} |
|
1372 |
||
1373 |
/** |
|
1374 |
* Wrapper for mulExact |
|
1375 |
* |
|
1376 |
* Catches ArithmeticException and rethrows as UnwarrantedOptimismException |
|
1377 |
* containing the result and the program point of the failure |
|
1378 |
* |
|
1379 |
* @param x first term |
|
1380 |
* @param y second term |
|
1381 |
* @param programPoint program point id |
|
1382 |
* @return the result |
|
1383 |
* @throws UnwarrantedOptimismException if overflow occurs |
|
1384 |
*/ |
|
1385 |
public static long mulExact(final long x, final long y, final int programPoint) throws UnwarrantedOptimismException { |
|
1386 |
try { |
|
1387 |
return Math.multiplyExact(x, y); |
|
1388 |
} catch (final ArithmeticException e) { |
|
1389 |
throw new UnwarrantedOptimismException((double)x * (double)y, programPoint); |
|
1390 |
} |
|
1391 |
} |
|
1392 |
||
1393 |
/** |
|
1394 |
* Wrapper for divExact. Throws UnwarrantedOptimismException if the result of the division can't be represented as |
|
1395 |
* int. |
|
1396 |
* |
|
1397 |
* @param x first term |
|
1398 |
* @param y second term |
|
1399 |
* @param programPoint program point id |
|
1400 |
* @return the result |
|
1401 |
* @throws UnwarrantedOptimismException if the result of the division can't be represented as int. |
|
1402 |
*/ |
|
1403 |
public static int divExact(final int x, final int y, final int programPoint) throws UnwarrantedOptimismException { |
|
1404 |
final int res; |
|
1405 |
try { |
|
1406 |
res = x / y; |
|
1407 |
} catch (final ArithmeticException e) { |
|
1408 |
assert y == 0; // Only div by zero anticipated |
|
1409 |
throw new UnwarrantedOptimismException(x > 0 ? Double.POSITIVE_INFINITY : x < 0 ? Double.NEGATIVE_INFINITY : Double.NaN, programPoint); |
|
1410 |
} |
|
1411 |
final int rem = x % y; |
|
1412 |
if (rem == 0) { |
|
1413 |
return res; |
|
1414 |
} |
|
1415 |
// go directly to double here, as anything with non zero remainder is a floating point number in JavaScript |
|
1416 |
throw new UnwarrantedOptimismException((double)x / (double)y, programPoint); |
|
1417 |
} |
|
1418 |
||
1419 |
/** |
|
1420 |
* Wrapper for divExact. Throws UnwarrantedOptimismException if the result of the division can't be represented as |
|
1421 |
* long. |
|
1422 |
* |
|
1423 |
* @param x first term |
|
1424 |
* @param y second term |
|
1425 |
* @param programPoint program point id |
|
1426 |
* @return the result |
|
1427 |
* @throws UnwarrantedOptimismException if the result of the division can't be represented as long. |
|
1428 |
*/ |
|
1429 |
public static long divExact(final long x, final long y, final int programPoint) throws UnwarrantedOptimismException { |
|
1430 |
final long res; |
|
1431 |
try { |
|
1432 |
res = x / y; |
|
1433 |
} catch (final ArithmeticException e) { |
|
1434 |
assert y == 0L; // Only div by zero anticipated |
|
1435 |
throw new UnwarrantedOptimismException(x > 0L ? Double.POSITIVE_INFINITY : x < 0L ? Double.NEGATIVE_INFINITY : Double.NaN, programPoint); |
|
1436 |
} |
|
1437 |
final long rem = x % y; |
|
1438 |
if (rem == 0L) { |
|
1439 |
return res; |
|
1440 |
} |
|
1441 |
throw new UnwarrantedOptimismException((double)x / (double)y, programPoint); |
|
1442 |
} |
|
1443 |
||
1444 |
/** |
|
1445 |
* Wrapper for decrementExact |
|
1446 |
* |
|
1447 |
* Catches ArithmeticException and rethrows as UnwarrantedOptimismException |
|
1448 |
* containing the result and the program point of the failure |
|
1449 |
* |
|
1450 |
* @param x number to negate |
|
1451 |
* @param programPoint program point id |
|
1452 |
* @return the result |
|
1453 |
* @throws UnwarrantedOptimismException if overflow occurs |
|
1454 |
*/ |
|
1455 |
public static int decrementExact(final int x, final int programPoint) throws UnwarrantedOptimismException { |
|
1456 |
try { |
|
1457 |
return Math.decrementExact(x); |
|
1458 |
} catch (final ArithmeticException e) { |
|
1459 |
throw new UnwarrantedOptimismException((long)x - 1, programPoint); |
|
1460 |
} |
|
1461 |
} |
|
1462 |
||
1463 |
/** |
|
1464 |
* Wrapper for decrementExact |
|
1465 |
* |
|
1466 |
* Catches ArithmeticException and rethrows as UnwarrantedOptimismException |
|
1467 |
* containing the result and the program point of the failure |
|
1468 |
* |
|
1469 |
* @param x number to negate |
|
1470 |
* @param programPoint program point id |
|
1471 |
* @return the result |
|
1472 |
* @throws UnwarrantedOptimismException if overflow occurs |
|
1473 |
*/ |
|
1474 |
public static long decrementExact(final long x, final int programPoint) throws UnwarrantedOptimismException { |
|
1475 |
try { |
|
1476 |
return Math.decrementExact(x); |
|
1477 |
} catch (final ArithmeticException e) { |
|
1478 |
throw new UnwarrantedOptimismException((double)x - 1L, programPoint); |
|
1479 |
} |
|
1480 |
} |
|
1481 |
||
1482 |
/** |
|
1483 |
* Wrapper for incrementExact |
|
1484 |
* |
|
1485 |
* Catches ArithmeticException and rethrows as UnwarrantedOptimismException |
|
1486 |
* containing the result and the program point of the failure |
|
1487 |
* |
|
1488 |
* @param x the number to increment |
|
1489 |
* @param programPoint program point id |
|
1490 |
* @return the result |
|
1491 |
* @throws UnwarrantedOptimismException if overflow occurs |
|
1492 |
*/ |
|
1493 |
public static int incrementExact(final int x, final int programPoint) throws UnwarrantedOptimismException { |
|
1494 |
try { |
|
1495 |
return Math.incrementExact(x); |
|
1496 |
} catch (final ArithmeticException e) { |
|
1497 |
throw new UnwarrantedOptimismException((long)x + 1, programPoint); |
|
1498 |
} |
|
1499 |
} |
|
1500 |
||
1501 |
/** |
|
1502 |
* Wrapper for incrementExact |
|
1503 |
* |
|
1504 |
* Catches ArithmeticException and rethrows as UnwarrantedOptimismException |
|
1505 |
* containing the result and the program point of the failure |
|
1506 |
* |
|
1507 |
* @param x the number to increment |
|
1508 |
* @param programPoint program point id |
|
1509 |
* @return the result |
|
1510 |
* @throws UnwarrantedOptimismException if overflow occurs |
|
1511 |
*/ |
|
1512 |
public static long incrementExact(final long x, final int programPoint) throws UnwarrantedOptimismException { |
|
1513 |
try { |
|
1514 |
return Math.incrementExact(x); |
|
1515 |
} catch (final ArithmeticException e) { |
|
1516 |
throw new UnwarrantedOptimismException((double)x + 1L, programPoint); |
|
1517 |
} |
|
1518 |
} |
|
1519 |
||
1520 |
/** |
|
1521 |
* Wrapper for negateExact |
|
1522 |
* |
|
1523 |
* Catches ArithmeticException and rethrows as UnwarrantedOptimismException |
|
1524 |
* containing the result and the program point of the failure |
|
1525 |
* |
|
1526 |
* @param x the number to negate |
|
1527 |
* @param programPoint program point id |
|
1528 |
* @return the result |
|
1529 |
* @throws UnwarrantedOptimismException if overflow occurs |
|
1530 |
*/ |
|
1531 |
public static int negateExact(final int x, final int programPoint) throws UnwarrantedOptimismException { |
|
1532 |
try { |
|
1533 |
if (x == 0) { |
|
1534 |
throw new UnwarrantedOptimismException(-0.0, programPoint); |
|
1535 |
} |
|
1536 |
return Math.negateExact(x); |
|
1537 |
} catch (final ArithmeticException e) { |
|
1538 |
throw new UnwarrantedOptimismException(-(long)x, programPoint); |
|
1539 |
} |
|
1540 |
} |
|
1541 |
||
1542 |
/** |
|
1543 |
* Wrapper for negateExact |
|
1544 |
* |
|
1545 |
* Catches ArithmeticException and rethrows as UnwarrantedOptimismException |
|
1546 |
* containing the result and the program point of the failure |
|
1547 |
* |
|
1548 |
* @param x the number to negate |
|
1549 |
* @param programPoint program point id |
|
1550 |
* @return the result |
|
1551 |
* @throws UnwarrantedOptimismException if overflow occurs |
|
1552 |
*/ |
|
1553 |
public static long negateExact(final long x, final int programPoint) throws UnwarrantedOptimismException { |
|
1554 |
try { |
|
1555 |
if (x == 0L) { |
|
1556 |
throw new UnwarrantedOptimismException(-0.0, programPoint); |
|
1557 |
} |
|
1558 |
return Math.negateExact(x); |
|
1559 |
} catch (final ArithmeticException e) { |
|
1560 |
throw new UnwarrantedOptimismException(-(double)x, programPoint); |
|
1561 |
} |
|
1562 |
} |
|
1563 |
||
1564 |
/** |
|
1565 |
* Given a type of an accessor, return its index in [0..getNumberOfAccessorTypes()) |
|
1566 |
* |
|
1567 |
* @param type the type |
|
1568 |
* |
|
1569 |
* @return the accessor index, or -1 if no accessor of this type exists |
|
1570 |
*/ |
|
1571 |
public static int getAccessorTypeIndex(final Type type) { |
|
1572 |
return getAccessorTypeIndex(type.getTypeClass()); |
|
1573 |
} |
|
1574 |
||
1575 |
/** |
|
1576 |
* Given a class of an accessor, return its index in [0..getNumberOfAccessorTypes()) |
|
1577 |
* |
|
1578 |
* Note that this is hardcoded with respect to the dynamic contents of the accessor |
|
1579 |
* types array for speed. Hotspot got stuck with this as 5% of the runtime in |
|
1580 |
* a benchmark when it looped over values and increased an index counter. :-( |
|
1581 |
* |
|
1582 |
* @param type the type |
|
1583 |
* |
|
1584 |
* @return the accessor index, or -1 if no accessor of this type exists |
|
1585 |
*/ |
|
1586 |
public static int getAccessorTypeIndex(final Class<?> type) { |
|
1587 |
if (type == null) { |
|
1588 |
return TYPE_UNDEFINED_INDEX; |
|
1589 |
} else if (type == int.class) { |
|
1590 |
return TYPE_INT_INDEX; |
|
1591 |
} else if (type == long.class) { |
|
1592 |
return TYPE_LONG_INDEX; |
|
1593 |
} else if (type == double.class) { |
|
1594 |
return TYPE_DOUBLE_INDEX; |
|
1595 |
} else if (!type.isPrimitive()) { |
|
1596 |
return TYPE_OBJECT_INDEX; |
|
1597 |
} |
|
1598 |
return -1; |
|
1599 |
} |
|
1600 |
||
1601 |
/** |
|
1602 |
* Return the accessor type based on its index in [0..getNumberOfAccessorTypes()) |
|
1603 |
* Indexes are ordered narrower{@literal ->}wider / optimistic{@literal ->}pessimistic. Invalidations always |
|
1604 |
* go to a type of higher index |
|
1605 |
* |
|
1606 |
* @param index accessor type index |
|
1607 |
* |
|
1608 |
* @return a type corresponding to the index. |
|
1609 |
*/ |
|
1610 |
||
1611 |
public static Type getAccessorType(final int index) { |
|
1612 |
return ACCESSOR_TYPES.get(index); |
|
1613 |
} |
|
1614 |
||
1615 |
/** |
|
1616 |
* Return the number of accessor types available. |
|
1617 |
* |
|
1618 |
* @return number of accessor types in system |
|
1619 |
*/ |
|
1620 |
public static int getNumberOfAccessorTypes() { |
|
1621 |
return ACCESSOR_TYPES.size(); |
|
1622 |
} |
|
1623 |
||
16147 | 1624 |
private static double parseRadix(final char chars[], final int start, final int length, final int radix) { |
1625 |
int pos = 0; |
|
1626 |
||
1627 |
for (int i = start; i < length ; i++) { |
|
1628 |
if (digit(chars[i], radix) == -1) { |
|
18627 | 1629 |
return Double.NaN; |
16147 | 1630 |
} |
1631 |
pos++; |
|
1632 |
} |
|
1633 |
||
1634 |
if (pos == 0) { |
|
1635 |
return Double.NaN; |
|
1636 |
} |
|
1637 |
||
1638 |
double value = 0.0; |
|
1639 |
for (int i = start; i < start + pos; i++) { |
|
1640 |
value *= radix; |
|
1641 |
value += digit(chars[i], radix); |
|
1642 |
} |
|
1643 |
||
1644 |
return value; |
|
1645 |
} |
|
1646 |
||
1647 |
private static double toNumberGeneric(final Object obj) { |
|
1648 |
if (obj == null) { |
|
1649 |
return +0.0; |
|
1650 |
} |
|
1651 |
||
1652 |
if (obj instanceof String) { |
|
1653 |
return toNumber((String)obj); |
|
1654 |
} |
|
1655 |
||
1656 |
if (obj instanceof ConsString) { |
|
1657 |
return toNumber(obj.toString()); |
|
1658 |
} |
|
1659 |
||
1660 |
if (obj instanceof Boolean) { |
|
1661 |
return (Boolean)obj ? 1 : +0.0; |
|
1662 |
} |
|
1663 |
||
1664 |
if (obj instanceof ScriptObject) { |
|
21449 | 1665 |
return toNumber((ScriptObject)obj); |
16147 | 1666 |
} |
1667 |
||
21453
b48953eede53
8027128: jdk.nashorn.api.scripting.JSObject should be an interface
sundar
parents:
21450
diff
changeset
|
1668 |
if (obj instanceof JSObject) { |
b48953eede53
8027128: jdk.nashorn.api.scripting.JSObject should be an interface
sundar
parents:
21450
diff
changeset
|
1669 |
return ((JSObject)obj).toNumber(); |
b48953eede53
8027128: jdk.nashorn.api.scripting.JSObject should be an interface
sundar
parents:
21450
diff
changeset
|
1670 |
} |
b48953eede53
8027128: jdk.nashorn.api.scripting.JSObject should be an interface
sundar
parents:
21450
diff
changeset
|
1671 |
|
16147 | 1672 |
return Double.NaN; |
1673 |
} |
|
1674 |
||
19897
4235a28bb5e8
8024847: Java.to should accept mirror and external JSObjects as array-like objects as well
sundar
parents:
19892
diff
changeset
|
1675 |
private static Object invoke(final MethodHandle mh, final Object arg) { |
4235a28bb5e8
8024847: Java.to should accept mirror and external JSObjects as array-like objects as well
sundar
parents:
19892
diff
changeset
|
1676 |
try { |
4235a28bb5e8
8024847: Java.to should accept mirror and external JSObjects as array-like objects as well
sundar
parents:
19892
diff
changeset
|
1677 |
return mh.invoke(arg); |
4235a28bb5e8
8024847: Java.to should accept mirror and external JSObjects as array-like objects as well
sundar
parents:
19892
diff
changeset
|
1678 |
} catch (final RuntimeException | Error e) { |
4235a28bb5e8
8024847: Java.to should accept mirror and external JSObjects as array-like objects as well
sundar
parents:
19892
diff
changeset
|
1679 |
throw e; |
4235a28bb5e8
8024847: Java.to should accept mirror and external JSObjects as array-like objects as well
sundar
parents:
19892
diff
changeset
|
1680 |
} catch (final Throwable t) { |
4235a28bb5e8
8024847: Java.to should accept mirror and external JSObjects as array-like objects as well
sundar
parents:
19892
diff
changeset
|
1681 |
throw new RuntimeException(t); |
4235a28bb5e8
8024847: Java.to should accept mirror and external JSObjects as array-like objects as well
sundar
parents:
19892
diff
changeset
|
1682 |
} |
4235a28bb5e8
8024847: Java.to should accept mirror and external JSObjects as array-like objects as well
sundar
parents:
19892
diff
changeset
|
1683 |
} |
24719 | 1684 |
|
16147 | 1685 |
} |