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