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