nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/runtime/JSType.java
author attila
Thu, 05 Mar 2015 15:43:43 +0100
changeset 29283 fb47e4d25a9f
parent 29282 a8523237b66c
child 29627 03fe257ba844
permissions -rw-r--r--
8035712: Restore some of the RuntimeCallSite specializations 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
29283
fb47e4d25a9f 8035712: Restore some of the RuntimeCallSite specializations
attila
parents: 29282
diff changeset
   764
    /**
fb47e4d25a9f 8035712: Restore some of the RuntimeCallSite specializations
attila
parents: 29282
diff changeset
   765
     * Converts an object for a comparison with a number. Almost identical to {@link #toNumber(Object)} but
fb47e4d25a9f 8035712: Restore some of the RuntimeCallSite specializations
attila
parents: 29282
diff changeset
   766
     * converts {@code null} to {@code NaN} instead of zero, so it won't compare equal to zero.
fb47e4d25a9f 8035712: Restore some of the RuntimeCallSite specializations
attila
parents: 29282
diff changeset
   767
     *
fb47e4d25a9f 8035712: Restore some of the RuntimeCallSite specializations
attila
parents: 29282
diff changeset
   768
     * @param obj  an object
fb47e4d25a9f 8035712: Restore some of the RuntimeCallSite specializations
attila
parents: 29282
diff changeset
   769
     *
fb47e4d25a9f 8035712: Restore some of the RuntimeCallSite specializations
attila
parents: 29282
diff changeset
   770
     * @return a number
fb47e4d25a9f 8035712: Restore some of the RuntimeCallSite specializations
attila
parents: 29282
diff changeset
   771
     */
fb47e4d25a9f 8035712: Restore some of the RuntimeCallSite specializations
attila
parents: 29282
diff changeset
   772
    public static double toNumberForEq(final Object obj) {
fb47e4d25a9f 8035712: Restore some of the RuntimeCallSite specializations
attila
parents: 29282
diff changeset
   773
        return obj == null ? Double.NaN : toNumber(obj);
fb47e4d25a9f 8035712: Restore some of the RuntimeCallSite specializations
attila
parents: 29282
diff changeset
   774
    }
fb47e4d25a9f 8035712: Restore some of the RuntimeCallSite specializations
attila
parents: 29282
diff changeset
   775
fb47e4d25a9f 8035712: Restore some of the RuntimeCallSite specializations
attila
parents: 29282
diff changeset
   776
    /**
fb47e4d25a9f 8035712: Restore some of the RuntimeCallSite specializations
attila
parents: 29282
diff changeset
   777
     * Converts an object for strict comparison with a number. Returns {@code NaN} for any object that is not
fb47e4d25a9f 8035712: Restore some of the RuntimeCallSite specializations
attila
parents: 29282
diff changeset
   778
     * a {@link Number}, so only boxed numerics can compare strictly equal to numbers.
fb47e4d25a9f 8035712: Restore some of the RuntimeCallSite specializations
attila
parents: 29282
diff changeset
   779
     *
fb47e4d25a9f 8035712: Restore some of the RuntimeCallSite specializations
attila
parents: 29282
diff changeset
   780
     * @param obj  an object
fb47e4d25a9f 8035712: Restore some of the RuntimeCallSite specializations
attila
parents: 29282
diff changeset
   781
     *
fb47e4d25a9f 8035712: Restore some of the RuntimeCallSite specializations
attila
parents: 29282
diff changeset
   782
     * @return a number
fb47e4d25a9f 8035712: Restore some of the RuntimeCallSite specializations
attila
parents: 29282
diff changeset
   783
     */
fb47e4d25a9f 8035712: Restore some of the RuntimeCallSite specializations
attila
parents: 29282
diff changeset
   784
    public static double toNumberForStrictEq(final Object obj) {
fb47e4d25a9f 8035712: Restore some of the RuntimeCallSite specializations
attila
parents: 29282
diff changeset
   785
        if (obj instanceof Double) {
fb47e4d25a9f 8035712: Restore some of the RuntimeCallSite specializations
attila
parents: 29282
diff changeset
   786
            return (Double)obj;
fb47e4d25a9f 8035712: Restore some of the RuntimeCallSite specializations
attila
parents: 29282
diff changeset
   787
        }
fb47e4d25a9f 8035712: Restore some of the RuntimeCallSite specializations
attila
parents: 29282
diff changeset
   788
        if (obj instanceof Number) {
fb47e4d25a9f 8035712: Restore some of the RuntimeCallSite specializations
attila
parents: 29282
diff changeset
   789
            return ((Number)obj).doubleValue();
fb47e4d25a9f 8035712: Restore some of the RuntimeCallSite specializations
attila
parents: 29282
diff changeset
   790
        }
fb47e4d25a9f 8035712: Restore some of the RuntimeCallSite specializations
attila
parents: 29282
diff changeset
   791
        return Double.NaN;
fb47e4d25a9f 8035712: Restore some of the RuntimeCallSite specializations
attila
parents: 29282
diff changeset
   792
    }
fb47e4d25a9f 8035712: Restore some of the RuntimeCallSite specializations
attila
parents: 29282
diff changeset
   793
21449
72d51df5ed85 8027031: complete merging of loads and converts
attila
parents: 21437
diff changeset
   794
72d51df5ed85 8027031: complete merging of loads and converts
attila
parents: 21437
diff changeset
   795
    /**
29133
1cd7d8af99ba 8072426: Can't compare Java objects to strings or numbers
attila
parents: 28126
diff changeset
   796
     * JavaScript compliant conversion of Boolean to number
1cd7d8af99ba 8072426: Can't compare Java objects to strings or numbers
attila
parents: 28126
diff changeset
   797
     * See ECMA 9.3 ToNumber
1cd7d8af99ba 8072426: Can't compare Java objects to strings or numbers
attila
parents: 28126
diff changeset
   798
     *
1cd7d8af99ba 8072426: Can't compare Java objects to strings or numbers
attila
parents: 28126
diff changeset
   799
     * @param b a boolean
1cd7d8af99ba 8072426: Can't compare Java objects to strings or numbers
attila
parents: 28126
diff changeset
   800
     *
1cd7d8af99ba 8072426: Can't compare Java objects to strings or numbers
attila
parents: 28126
diff changeset
   801
     * @return JS numeric value of the boolean: 1.0 or 0.0
1cd7d8af99ba 8072426: Can't compare Java objects to strings or numbers
attila
parents: 28126
diff changeset
   802
     */
1cd7d8af99ba 8072426: Can't compare Java objects to strings or numbers
attila
parents: 28126
diff changeset
   803
    public static double toNumber(final Boolean b) {
1cd7d8af99ba 8072426: Can't compare Java objects to strings or numbers
attila
parents: 28126
diff changeset
   804
        return b ? 1d : +0d;
1cd7d8af99ba 8072426: Can't compare Java objects to strings or numbers
attila
parents: 28126
diff changeset
   805
    }
1cd7d8af99ba 8072426: Can't compare Java objects to strings or numbers
attila
parents: 28126
diff changeset
   806
1cd7d8af99ba 8072426: Can't compare Java objects to strings or numbers
attila
parents: 28126
diff changeset
   807
    /**
21449
72d51df5ed85 8027031: complete merging of loads and converts
attila
parents: 21437
diff changeset
   808
     * JavaScript compliant conversion of Object to number
72d51df5ed85 8027031: complete merging of loads and converts
attila
parents: 21437
diff changeset
   809
     * See ECMA 9.3 ToNumber
72d51df5ed85 8027031: complete merging of loads and converts
attila
parents: 21437
diff changeset
   810
     *
72d51df5ed85 8027031: complete merging of loads and converts
attila
parents: 21437
diff changeset
   811
     * @param obj  an object
72d51df5ed85 8027031: complete merging of loads and converts
attila
parents: 21437
diff changeset
   812
     *
72d51df5ed85 8027031: complete merging of loads and converts
attila
parents: 21437
diff changeset
   813
     * @return a number
72d51df5ed85 8027031: complete merging of loads and converts
attila
parents: 21437
diff changeset
   814
     */
72d51df5ed85 8027031: complete merging of loads and converts
attila
parents: 21437
diff changeset
   815
    public static double toNumber(final ScriptObject obj) {
72d51df5ed85 8027031: complete merging of loads and converts
attila
parents: 21437
diff changeset
   816
        return toNumber(toPrimitive(obj, Number.class));
72d51df5ed85 8027031: complete merging of loads and converts
attila
parents: 21437
diff changeset
   817
    }
72d51df5ed85 8027031: complete merging of loads and converts
attila
parents: 21437
diff changeset
   818
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   819
    /**
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
   820
     * Optimistic number conversion - throws UnwarrantedOptimismException if Object
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
   821
     *
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
   822
     * @param obj           object to convert
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
   823
     * @param programPoint  program point
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
   824
     * @return double
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
   825
     */
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
   826
    public static double toNumberOptimistic(final Object obj, final int programPoint) {
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
   827
        if (obj != null) {
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
   828
            final Class<?> clz = obj.getClass();
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
   829
            if (clz == Double.class || clz == Integer.class || clz == Long.class) {
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
   830
                return ((Number)obj).doubleValue();
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
   831
            }
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
   832
        }
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
   833
        throw new UnwarrantedOptimismException(obj, programPoint);
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
   834
    }
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
   835
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
   836
    /**
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
   837
     * Object to number conversion that delegates to either {@link #toNumber(Object)} or to
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
   838
     * {@link #toNumberOptimistic(Object, int)} depending on whether the program point is valid or not.
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
   839
     * @param obj the object to convert
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
   840
     * @param programPoint the program point; can be invalid.
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
   841
     * @return the value converted to a number
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
   842
     * @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
   843
     */
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
   844
    public static double toNumberMaybeOptimistic(final Object obj, final int programPoint) {
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
   845
        return UnwarrantedOptimismException.isValid(programPoint) ? toNumberOptimistic(obj, programPoint) : toNumber(obj);
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
   846
    }
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
   847
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
   848
    /**
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   849
     * Digit representation for a character
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   850
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   851
     * @param ch     a character
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   852
     * @param radix  radix
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   853
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   854
     * @return the digit for this character
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   855
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   856
    public static int digit(final char ch, final int radix) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   857
        return digit(ch, radix, false);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   858
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   859
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   860
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   861
     * Digit representation for a character
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   862
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   863
     * @param ch             a character
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   864
     * @param radix          radix
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   865
     * @param onlyIsoLatin1  iso latin conversion only
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   866
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   867
     * @return the digit for this character
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   868
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   869
    public static int digit(final char ch, final int radix, final boolean onlyIsoLatin1) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   870
        final char maxInRadix = (char)('a' + (radix - 1) - 10);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   871
        final char c          = Character.toLowerCase(ch);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   872
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   873
        if (c >= 'a' && c <= maxInRadix) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   874
            return Character.digit(ch, radix);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   875
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   876
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   877
        if (Character.isDigit(ch)) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   878
            if (!onlyIsoLatin1 || ch >= '0' && ch <= '9') {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   879
                return Character.digit(ch, radix);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   880
            }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   881
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   882
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   883
        return -1;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   884
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   885
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   886
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   887
     * JavaScript compliant String to number conversion
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   888
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   889
     * @param str  a string
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   890
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   891
     * @return a number
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   892
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   893
    public static double toNumber(final String str) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   894
        int end = str.length();
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   895
        if (end == 0) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   896
            return 0.0; // Empty string
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   897
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   898
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   899
        int  start = 0;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   900
        char f     = str.charAt(0);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   901
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   902
        while (Lexer.isJSWhitespace(f)) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   903
            if (++start == end) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   904
                return 0.0d; // All whitespace string
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   905
            }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   906
            f = str.charAt(start);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   907
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   908
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   909
        // 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
   910
        // non-whitespace character.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   911
        while (Lexer.isJSWhitespace(str.charAt(end - 1))) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   912
            end--;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   913
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   914
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   915
        final boolean negative;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   916
        if (f == '-') {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   917
            if(++start == end) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   918
                return Double.NaN; // Single-char "-" string
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   919
            }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   920
            f = str.charAt(start);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   921
            negative = true;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   922
        } else {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   923
            if (f == '+') {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   924
                if (++start == end) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   925
                    return Double.NaN; // Single-char "+" string
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   926
                }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   927
                f = str.charAt(start);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   928
            }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   929
            negative = false;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   930
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   931
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   932
        final double value;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   933
        if (start + 1 < end && f == '0' && Character.toLowerCase(str.charAt(start + 1)) == 'x') {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   934
            //decode hex string
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   935
            value = parseRadix(str.toCharArray(), start + 2, end, 16);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   936
        } else {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   937
            // 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
   938
            // "N" to allow for parsing "NaN" and "Infinity" correctly.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   939
            if ((f < '0' || f > '9') && f != '.' && f != 'I' && f != 'N') {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   940
                return Double.NaN;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   941
            }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   942
            try {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   943
                value = Double.parseDouble(str.substring(start, end));
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   944
            } catch (final NumberFormatException e) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   945
                return Double.NaN;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   946
            }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   947
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   948
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   949
        return negative ? -value : value;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   950
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   951
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   952
    /**
17764
29e6eb3bb9df 8010804: Review long and integer usage conventions
hannesw
parents: 17241
diff changeset
   953
     * JavaScript compliant Object to integer conversion. See ECMA 9.4 ToInteger
29e6eb3bb9df 8010804: Review long and integer usage conventions
hannesw
parents: 17241
diff changeset
   954
     *
29e6eb3bb9df 8010804: Review long and integer usage conventions
hannesw
parents: 17241
diff changeset
   955
     * <p>Note that this returns {@link java.lang.Integer#MAX_VALUE} or {@link java.lang.Integer#MIN_VALUE}
29e6eb3bb9df 8010804: Review long and integer usage conventions
hannesw
parents: 17241
diff changeset
   956
     * for double values that exceed the int range, including positive and negative Infinity. It is the
29e6eb3bb9df 8010804: Review long and integer usage conventions
hannesw
parents: 17241
diff changeset
   957
     * caller's responsibility to handle such values correctly.</p>
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   958
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   959
     * @param obj  an object
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   960
     * @return an integer
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   961
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   962
    public static int toInteger(final Object obj) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   963
        return (int)toNumber(obj);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   964
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   965
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   966
    /**
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
   967
     * Converts an Object to long.
17764
29e6eb3bb9df 8010804: Review long and integer usage conventions
hannesw
parents: 17241
diff changeset
   968
     *
29e6eb3bb9df 8010804: Review long and integer usage conventions
hannesw
parents: 17241
diff changeset
   969
     * <p>Note that this returns {@link java.lang.Long#MAX_VALUE} or {@link java.lang.Long#MIN_VALUE}
29e6eb3bb9df 8010804: Review long and integer usage conventions
hannesw
parents: 17241
diff changeset
   970
     * for double values that exceed the long range, including positive and negative Infinity. It is the
29e6eb3bb9df 8010804: Review long and integer usage conventions
hannesw
parents: 17241
diff changeset
   971
     * caller's responsibility to handle such values correctly.</p>
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   972
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   973
     * @param obj  an object
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   974
     * @return a long
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   975
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   976
    public static long toLong(final Object obj) {
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
   977
        return obj instanceof Long ? ((Long)obj).longValue() : toLong(toNumber(obj));
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
   978
    }
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
   979
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
   980
    /**
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
   981
     * Converts a double to long.
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
   982
     *
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
   983
     * @param num the double to convert
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
   984
     * @return the converted long value
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
   985
     */
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
   986
    public static long toLong(final double num) {
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
   987
        return (long)num;
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
   988
    }
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
   989
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
   990
    /**
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
   991
     * Optimistic long conversion - throws UnwarrantedOptimismException if double or Object
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
   992
     *
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
   993
     * @param obj           object to convert
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
   994
     * @param programPoint  program point
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
   995
     * @return long
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
   996
     */
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
   997
    public static long toLongOptimistic(final Object obj, final int programPoint) {
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
   998
        if (obj != null) {
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
   999
            final Class<?> clz = obj.getClass();
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1000
            if (clz == Long.class || clz == Integer.class) {
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1001
                return ((Number)obj).longValue();
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1002
            }
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1003
        }
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1004
        throw new UnwarrantedOptimismException(obj, programPoint);
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1005
    }
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1006
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1007
    /**
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1008
     * Object to int conversion that delegates to either {@link #toLong(Object)} or to
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1009
     * {@link #toLongOptimistic(Object, int)} depending on whether the program point is valid or not.
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1010
     * @param obj the object to convert
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1011
     * @param programPoint the program point; can be invalid.
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1012
     * @return the value converted to long
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1013
     * @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
  1014
     */
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1015
    public static long toLongMaybeOptimistic(final Object obj, final int programPoint) {
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1016
        return UnwarrantedOptimismException.isValid(programPoint) ? toLongOptimistic(obj, programPoint) : toLong(obj);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1017
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1018
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1019
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1020
     * JavaScript compliant Object to int32 conversion
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1021
     * See ECMA 9.5 ToInt32
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1022
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1023
     * @param obj an object
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1024
     * @return an int32
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1025
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1026
    public static int toInt32(final Object obj) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1027
        return toInt32(toNumber(obj));
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1028
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1029
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1030
    /**
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1031
     * Optimistic int conversion - throws UnwarrantedOptimismException if double, long or Object
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1032
     *
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1033
     * @param obj           object to convert
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1034
     * @param programPoint  program point
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1035
     * @return double
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1036
     */
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1037
    public static int toInt32Optimistic(final Object obj, final int programPoint) {
26378
ca7093bd5fd1 8056129: AtomicInteger is treated as primitive number with optimistic compilation
attila
parents: 25865
diff changeset
  1038
        if (obj != null && obj.getClass() == Integer.class) {
ca7093bd5fd1 8056129: AtomicInteger is treated as primitive number with optimistic compilation
attila
parents: 25865
diff changeset
  1039
            return ((Integer)obj).intValue();
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1040
        }
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1041
        throw new UnwarrantedOptimismException(obj, programPoint);
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1042
    }
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1043
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1044
    /**
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1045
     * Object to int conversion that delegates to either {@link #toInt32(Object)} or to
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1046
     * {@link #toInt32Optimistic(Object, int)} depending on whether the program point is valid or not.
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1047
     * @param obj the object to convert
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1048
     * @param programPoint the program point; can be invalid.
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1049
     * @return the value converted to int
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1050
     * @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
  1051
     */
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1052
    public static int toInt32MaybeOptimistic(final Object obj, final int programPoint) {
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1053
        return UnwarrantedOptimismException.isValid(programPoint) ? toInt32Optimistic(obj, programPoint) : toInt32(obj);
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1054
    }
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1055
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1056
    // Minimum and maximum range between which every long value can be precisely represented as a double.
24733
1e825be55fd1 8027043: Turn global accesses into MethodHandle.constant, with one chance of reassignment, e.g. x = value occuring once in the global scope is ok, twice is not.
lagergren
parents: 24727
diff changeset
  1057
    private static final long MAX_PRECISE_DOUBLE = 1L << 53;
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1058
    private static final long MIN_PRECISE_DOUBLE = -MAX_PRECISE_DOUBLE;
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1059
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1060
    /**
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1061
     * JavaScript compliant long to int32 conversion
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1062
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1063
     * @param num a long
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1064
     * @return an int32
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1065
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1066
    public static int toInt32(final long num) {
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1067
        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
  1068
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1069
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1070
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1071
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1072
     * JavaScript compliant number to int32 conversion
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1073
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1074
     * @param num a number
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1075
     * @return an int32
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1076
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1077
    public static int toInt32(final double num) {
17241
c337fefb8c84 8012334: ToUint32, ToInt32, and ToUint16 don't conform to spec
hannesw
parents: 16256
diff changeset
  1078
        return (int)doubleToInt32(num);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1079
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1080
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1081
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1082
     * JavaScript compliant Object to uint32 conversion
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1083
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1084
     * @param obj an object
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1085
     * @return a uint32
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1086
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1087
    public static long toUint32(final Object obj) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1088
        return toUint32(toNumber(obj));
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1089
    }
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
     * JavaScript compliant number to uint32 conversion
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1093
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1094
     * @param num a number
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1095
     * @return a uint32
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1096
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1097
    public static long toUint32(final double num) {
17241
c337fefb8c84 8012334: ToUint32, ToInt32, and ToUint16 don't conform to spec
hannesw
parents: 16256
diff changeset
  1098
        return doubleToInt32(num) & MAX_UINT;
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1099
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1100
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1101
    /**
27212
3361766097cd 8061955: asm.js idioms result in unnecessarily code emission
attila
parents: 27209
diff changeset
  1102
     * JavaScript compliant int to uint32 conversion
3361766097cd 8061955: asm.js idioms result in unnecessarily code emission
attila
parents: 27209
diff changeset
  1103
     *
3361766097cd 8061955: asm.js idioms result in unnecessarily code emission
attila
parents: 27209
diff changeset
  1104
     * @param num an int
3361766097cd 8061955: asm.js idioms result in unnecessarily code emission
attila
parents: 27209
diff changeset
  1105
     * @return a uint32
3361766097cd 8061955: asm.js idioms result in unnecessarily code emission
attila
parents: 27209
diff changeset
  1106
     */
3361766097cd 8061955: asm.js idioms result in unnecessarily code emission
attila
parents: 27209
diff changeset
  1107
    public static long toUint32(final int num) {
3361766097cd 8061955: asm.js idioms result in unnecessarily code emission
attila
parents: 27209
diff changeset
  1108
        return num & MAX_UINT;
3361766097cd 8061955: asm.js idioms result in unnecessarily code emission
attila
parents: 27209
diff changeset
  1109
    }
3361766097cd 8061955: asm.js idioms result in unnecessarily code emission
attila
parents: 27209
diff changeset
  1110
3361766097cd 8061955: asm.js idioms result in unnecessarily code emission
attila
parents: 27209
diff changeset
  1111
    /**
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1112
     * JavaScript compliant Object to uint16 conversion
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1113
     * ECMA 9.7 ToUint16: (Unsigned 16 Bit Integer)
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1114
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1115
     * @param obj an object
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 Object obj) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1119
        return toUint16(toNumber(obj));
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1120
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1121
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1122
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1123
     * JavaScript compliant number to uint16 conversion
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1124
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1125
     * @param num a number
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1126
     * @return a uint16
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1127
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1128
    public static int toUint16(final int num) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1129
        return num & 0xffff;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1130
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1131
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1132
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1133
     * JavaScript compliant number to uint16 conversion
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1134
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1135
     * @param num a number
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1136
     * @return a uint16
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1137
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1138
    public static int toUint16(final long num) {
24733
1e825be55fd1 8027043: Turn global accesses into MethodHandle.constant, with one chance of reassignment, e.g. x = value occuring once in the global scope is ok, twice is not.
lagergren
parents: 24727
diff changeset
  1139
        return (int)num & 0xffff;
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1140
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1141
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1142
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1143
     * JavaScript compliant number to uint16 conversion
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1144
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1145
     * @param num a number
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1146
     * @return a uint16
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1147
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1148
    public static int toUint16(final double num) {
24733
1e825be55fd1 8027043: Turn global accesses into MethodHandle.constant, with one chance of reassignment, e.g. x = value occuring once in the global scope is ok, twice is not.
lagergren
parents: 24727
diff changeset
  1149
        return (int)doubleToInt32(num) & 0xffff;
17241
c337fefb8c84 8012334: ToUint32, ToInt32, and ToUint16 don't conform to spec
hannesw
parents: 16256
diff changeset
  1150
    }
c337fefb8c84 8012334: ToUint32, ToInt32, and ToUint16 don't conform to spec
hannesw
parents: 16256
diff changeset
  1151
c337fefb8c84 8012334: ToUint32, ToInt32, and ToUint16 don't conform to spec
hannesw
parents: 16256
diff changeset
  1152
    private static long doubleToInt32(final double num) {
c337fefb8c84 8012334: ToUint32, ToInt32, and ToUint16 don't conform to spec
hannesw
parents: 16256
diff changeset
  1153
        final int exponent = Math.getExponent(num);
c337fefb8c84 8012334: ToUint32, ToInt32, and ToUint16 don't conform to spec
hannesw
parents: 16256
diff changeset
  1154
        if (exponent < 31) {
c337fefb8c84 8012334: ToUint32, ToInt32, and ToUint16 don't conform to spec
hannesw
parents: 16256
diff changeset
  1155
            return (long) num;  // Fits into 32 bits
c337fefb8c84 8012334: ToUint32, ToInt32, and ToUint16 don't conform to spec
hannesw
parents: 16256
diff changeset
  1156
        }
c337fefb8c84 8012334: ToUint32, ToInt32, and ToUint16 don't conform to spec
hannesw
parents: 16256
diff changeset
  1157
        if (exponent >= 84) {
c337fefb8c84 8012334: ToUint32, ToInt32, and ToUint16 don't conform to spec
hannesw
parents: 16256
diff changeset
  1158
            // Either infinite or NaN or so large that shift / modulo will produce 0
c337fefb8c84 8012334: ToUint32, ToInt32, and ToUint16 don't conform to spec
hannesw
parents: 16256
diff changeset
  1159
            // (52 bit mantissa + 32 bit target width).
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1160
            return 0;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1161
        }
17241
c337fefb8c84 8012334: ToUint32, ToInt32, and ToUint16 don't conform to spec
hannesw
parents: 16256
diff changeset
  1162
        // This is rather slow and could probably be sped up using bit-fiddling.
24733
1e825be55fd1 8027043: Turn global accesses into MethodHandle.constant, with one chance of reassignment, e.g. x = value occuring once in the global scope is ok, twice is not.
lagergren
parents: 24727
diff changeset
  1163
        final double d = num >= 0 ? Math.floor(num) : Math.ceil(num);
17241
c337fefb8c84 8012334: ToUint32, ToInt32, and ToUint16 don't conform to spec
hannesw
parents: 16256
diff changeset
  1164
        return (long)(d % INT32_LIMIT);
16147
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
     * Check whether a number is finite
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1169
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1170
     * @param num a number
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1171
     * @return true if finite
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1172
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1173
    public static boolean isFinite(final double num) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1174
        return !Double.isInfinite(num) && !Double.isNaN(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 primitive to a double
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1179
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1180
     * @param num a double
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1181
     * @return a boxed double
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1182
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1183
    public static Double toDouble(final double num) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1184
        return num;
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 primitive to a double
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1189
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1190
     * @param num a long
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1191
     * @return a boxed double
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1192
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1193
    public static Double toDouble(final long num) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1194
        return (double)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 primitive to a double
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1199
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1200
     * @param num an int
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1201
     * @return a boxed double
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1202
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1203
    public static Double toDouble(final int num) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1204
        return (double)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 boolean to an Object
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1209
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1210
     * @param bool a boolean
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1211
     * @return a boxed boolean, its Object representation
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 boolean bool) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1214
        return bool;
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
     * Convert a number to an Object
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1219
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1220
     * @param num an integer
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 int num) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1224
        return num;
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
     * Convert a number to an Object
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1229
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1230
     * @param num a long
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1231
     * @return the boxed number
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1232
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1233
    public static Object toObject(final long num) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1234
        return num;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1235
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1236
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1237
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1238
     * Convert a number to an Object
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1239
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1240
     * @param num a double
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1241
     * @return the boxed number
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1242
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1243
    public static Object toObject(final double num) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1244
        return num;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1245
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1246
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1247
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1248
     * Identity converter for objects.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1249
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1250
     * @param obj an object
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1251
     * @return the boxed number
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1252
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1253
    public static Object toObject(final Object obj) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1254
        return obj;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1255
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1256
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1257
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1258
     * Object conversion. This is used to convert objects and numbers to their corresponding
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1259
     * NativeObject type
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1260
     * See ECMA 9.9 ToObject
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1261
     *
16188
d6390b0ea32a 8006678: Avoid too many Context.getGlobal() calls
sundar
parents: 16151
diff changeset
  1262
     * @param obj     the object to convert
d6390b0ea32a 8006678: Avoid too many Context.getGlobal() calls
sundar
parents: 16151
diff changeset
  1263
     *
d6390b0ea32a 8006678: Avoid too many Context.getGlobal() calls
sundar
parents: 16151
diff changeset
  1264
     * @return the wrapped object
d6390b0ea32a 8006678: Avoid too many Context.getGlobal() calls
sundar
parents: 16151
diff changeset
  1265
     */
d6390b0ea32a 8006678: Avoid too many Context.getGlobal() calls
sundar
parents: 16151
diff changeset
  1266
    public static Object toScriptObject(final Object obj) {
23375
a1110f2cbe75 8037400: Remove getInitialMap getters and GlobalObject interface
sundar
parents: 21691
diff changeset
  1267
        return toScriptObject(Context.getGlobal(), obj);
16188
d6390b0ea32a 8006678: Avoid too many Context.getGlobal() calls
sundar
parents: 16151
diff changeset
  1268
    }
d6390b0ea32a 8006678: Avoid too many Context.getGlobal() calls
sundar
parents: 16151
diff changeset
  1269
d6390b0ea32a 8006678: Avoid too many Context.getGlobal() calls
sundar
parents: 16151
diff changeset
  1270
    /**
d6390b0ea32a 8006678: Avoid too many Context.getGlobal() calls
sundar
parents: 16151
diff changeset
  1271
     * 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
  1272
     * NativeObject type
d6390b0ea32a 8006678: Avoid too many Context.getGlobal() calls
sundar
parents: 16151
diff changeset
  1273
     * See ECMA 9.9 ToObject
d6390b0ea32a 8006678: Avoid too many Context.getGlobal() calls
sundar
parents: 16151
diff changeset
  1274
     *
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1275
     * @param global  the global object
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1276
     * @param obj     the object to convert
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1277
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1278
     * @return the wrapped object
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1279
     */
23375
a1110f2cbe75 8037400: Remove getInitialMap getters and GlobalObject interface
sundar
parents: 21691
diff changeset
  1280
    public static Object toScriptObject(final Global global, final Object obj) {
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1281
        if (nullOrUndefined(obj)) {
16256
f2d9a0c49914 8007002: Replace implicit exception throwing methods with explicit throws - simplify control flow and remove useless code
lagergren
parents: 16234
diff changeset
  1282
            throw typeError(global, "not.an.object", ScriptRuntime.safeToString(obj));
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1283
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1284
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1285
        if (obj instanceof ScriptObject) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1286
            return obj;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1287
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1288
23375
a1110f2cbe75 8037400: Remove getInitialMap getters and GlobalObject interface
sundar
parents: 21691
diff changeset
  1289
        return global.wrapAsObject(obj);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1290
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1291
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1292
    /**
19897
4235a28bb5e8 8024847: Java.to should accept mirror and external JSObjects as array-like objects as well
sundar
parents: 19892
diff changeset
  1293
     * Script object to Java array conversion.
4235a28bb5e8 8024847: Java.to should accept mirror and external JSObjects as array-like objects as well
sundar
parents: 19892
diff changeset
  1294
     *
4235a28bb5e8 8024847: Java.to should accept mirror and external JSObjects as array-like objects as well
sundar
parents: 19892
diff changeset
  1295
     * @param obj script object to be converted to Java array
4235a28bb5e8 8024847: Java.to should accept mirror and external JSObjects as array-like objects as well
sundar
parents: 19892
diff changeset
  1296
     * @param componentType component type of the destination array required
4235a28bb5e8 8024847: Java.to should accept mirror and external JSObjects as array-like objects as well
sundar
parents: 19892
diff changeset
  1297
     * @return converted Java array
4235a28bb5e8 8024847: Java.to should accept mirror and external JSObjects as array-like objects as well
sundar
parents: 19892
diff changeset
  1298
     */
4235a28bb5e8 8024847: Java.to should accept mirror and external JSObjects as array-like objects as well
sundar
parents: 19892
diff changeset
  1299
    public static Object toJavaArray(final Object obj, final Class<?> componentType) {
4235a28bb5e8 8024847: Java.to should accept mirror and external JSObjects as array-like objects as well
sundar
parents: 19892
diff changeset
  1300
        if (obj instanceof ScriptObject) {
21686
5c6946f97d6f 8027236: Ensure ScriptObject and ConsString aren't visible to Java
attila
parents: 21453
diff changeset
  1301
            return ((ScriptObject)obj).getArray().asArrayOfType(componentType);
19897
4235a28bb5e8 8024847: Java.to should accept mirror and external JSObjects as array-like objects as well
sundar
parents: 19892
diff changeset
  1302
        } else if (obj instanceof JSObject) {
20928
3ff39d5c8c08 8026137: Fix Issues with Binary Evaluation Order
lagergren
parents: 19897
diff changeset
  1303
            final ArrayLikeIterator<?> itr = ArrayLikeIterator.arrayLikeIterator(obj);
19897
4235a28bb5e8 8024847: Java.to should accept mirror and external JSObjects as array-like objects as well
sundar
parents: 19892
diff changeset
  1304
            final int len = (int) itr.getLength();
4235a28bb5e8 8024847: Java.to should accept mirror and external JSObjects as array-like objects as well
sundar
parents: 19892
diff changeset
  1305
            final Object[] res = new Object[len];
4235a28bb5e8 8024847: Java.to should accept mirror and external JSObjects as array-like objects as well
sundar
parents: 19892
diff changeset
  1306
            int idx = 0;
4235a28bb5e8 8024847: Java.to should accept mirror and external JSObjects as array-like objects as well
sundar
parents: 19892
diff changeset
  1307
            while (itr.hasNext()) {
4235a28bb5e8 8024847: Java.to should accept mirror and external JSObjects as array-like objects as well
sundar
parents: 19892
diff changeset
  1308
                res[idx++] = itr.next();
4235a28bb5e8 8024847: Java.to should accept mirror and external JSObjects as array-like objects as well
sundar
parents: 19892
diff changeset
  1309
            }
4235a28bb5e8 8024847: Java.to should accept mirror and external JSObjects as array-like objects as well
sundar
parents: 19892
diff changeset
  1310
            return convertArray(res, componentType);
20944
a166d40ac0d4 8026113: Nashorn arrays should automatically convert to Java arrays
attila
parents: 20928
diff changeset
  1311
        } else if(obj == null) {
a166d40ac0d4 8026113: Nashorn arrays should automatically convert to Java arrays
attila
parents: 20928
diff changeset
  1312
            return null;
19897
4235a28bb5e8 8024847: Java.to should accept mirror and external JSObjects as array-like objects as well
sundar
parents: 19892
diff changeset
  1313
        } else {
4235a28bb5e8 8024847: Java.to should accept mirror and external JSObjects as array-like objects as well
sundar
parents: 19892
diff changeset
  1314
            throw new IllegalArgumentException("not a script object");
4235a28bb5e8 8024847: Java.to should accept mirror and external JSObjects as array-like objects as well
sundar
parents: 19892
diff changeset
  1315
        }
4235a28bb5e8 8024847: Java.to should accept mirror and external JSObjects as array-like objects as well
sundar
parents: 19892
diff changeset
  1316
    }
4235a28bb5e8 8024847: Java.to should accept mirror and external JSObjects as array-like objects as well
sundar
parents: 19892
diff changeset
  1317
4235a28bb5e8 8024847: Java.to should accept mirror and external JSObjects as array-like objects as well
sundar
parents: 19892
diff changeset
  1318
    /**
4235a28bb5e8 8024847: Java.to should accept mirror and external JSObjects as array-like objects as well
sundar
parents: 19892
diff changeset
  1319
     * Java array to java array conversion - but using type conversions implemented by linker.
4235a28bb5e8 8024847: Java.to should accept mirror and external JSObjects as array-like objects as well
sundar
parents: 19892
diff changeset
  1320
     *
4235a28bb5e8 8024847: Java.to should accept mirror and external JSObjects as array-like objects as well
sundar
parents: 19892
diff changeset
  1321
     * @param src source array
4235a28bb5e8 8024847: Java.to should accept mirror and external JSObjects as array-like objects as well
sundar
parents: 19892
diff changeset
  1322
     * @param componentType component type of the destination array required
4235a28bb5e8 8024847: Java.to should accept mirror and external JSObjects as array-like objects as well
sundar
parents: 19892
diff changeset
  1323
     * @return converted Java array
4235a28bb5e8 8024847: Java.to should accept mirror and external JSObjects as array-like objects as well
sundar
parents: 19892
diff changeset
  1324
     */
4235a28bb5e8 8024847: Java.to should accept mirror and external JSObjects as array-like objects as well
sundar
parents: 19892
diff changeset
  1325
    public static Object convertArray(final Object[] src, final Class<?> componentType) {
21686
5c6946f97d6f 8027236: Ensure ScriptObject and ConsString aren't visible to Java
attila
parents: 21453
diff changeset
  1326
        if(componentType == Object.class) {
5c6946f97d6f 8027236: Ensure ScriptObject and ConsString aren't visible to Java
attila
parents: 21453
diff changeset
  1327
            for(int i = 0; i < src.length; ++i) {
5c6946f97d6f 8027236: Ensure ScriptObject and ConsString aren't visible to Java
attila
parents: 21453
diff changeset
  1328
                final Object e = src[i];
5c6946f97d6f 8027236: Ensure ScriptObject and ConsString aren't visible to Java
attila
parents: 21453
diff changeset
  1329
                if(e instanceof ConsString) {
5c6946f97d6f 8027236: Ensure ScriptObject and ConsString aren't visible to Java
attila
parents: 21453
diff changeset
  1330
                    src[i] = e.toString();
5c6946f97d6f 8027236: Ensure ScriptObject and ConsString aren't visible to Java
attila
parents: 21453
diff changeset
  1331
                }
5c6946f97d6f 8027236: Ensure ScriptObject and ConsString aren't visible to Java
attila
parents: 21453
diff changeset
  1332
            }
5c6946f97d6f 8027236: Ensure ScriptObject and ConsString aren't visible to Java
attila
parents: 21453
diff changeset
  1333
        }
5c6946f97d6f 8027236: Ensure ScriptObject and ConsString aren't visible to Java
attila
parents: 21453
diff changeset
  1334
19897
4235a28bb5e8 8024847: Java.to should accept mirror and external JSObjects as array-like objects as well
sundar
parents: 19892
diff changeset
  1335
        final int l = src.length;
4235a28bb5e8 8024847: Java.to should accept mirror and external JSObjects as array-like objects as well
sundar
parents: 19892
diff changeset
  1336
        final Object dst = Array.newInstance(componentType, l);
4235a28bb5e8 8024847: Java.to should accept mirror and external JSObjects as array-like objects as well
sundar
parents: 19892
diff changeset
  1337
        final MethodHandle converter = Bootstrap.getLinkerServices().getTypeConverter(Object.class, componentType);
4235a28bb5e8 8024847: Java.to should accept mirror and external JSObjects as array-like objects as well
sundar
parents: 19892
diff changeset
  1338
        try {
4235a28bb5e8 8024847: Java.to should accept mirror and external JSObjects as array-like objects as well
sundar
parents: 19892
diff changeset
  1339
            for (int i = 0; i < src.length; i++) {
4235a28bb5e8 8024847: Java.to should accept mirror and external JSObjects as array-like objects as well
sundar
parents: 19892
diff changeset
  1340
                Array.set(dst, i, invoke(converter, src[i]));
4235a28bb5e8 8024847: Java.to should accept mirror and external JSObjects as array-like objects as well
sundar
parents: 19892
diff changeset
  1341
            }
4235a28bb5e8 8024847: Java.to should accept mirror and external JSObjects as array-like objects as well
sundar
parents: 19892
diff changeset
  1342
        } catch (final RuntimeException | Error e) {
4235a28bb5e8 8024847: Java.to should accept mirror and external JSObjects as array-like objects as well
sundar
parents: 19892
diff changeset
  1343
            throw e;
4235a28bb5e8 8024847: Java.to should accept mirror and external JSObjects as array-like objects as well
sundar
parents: 19892
diff changeset
  1344
        } catch (final Throwable t) {
4235a28bb5e8 8024847: Java.to should accept mirror and external JSObjects as array-like objects as well
sundar
parents: 19892
diff changeset
  1345
            throw new RuntimeException(t);
4235a28bb5e8 8024847: Java.to should accept mirror and external JSObjects as array-like objects as well
sundar
parents: 19892
diff changeset
  1346
        }
4235a28bb5e8 8024847: Java.to should accept mirror and external JSObjects as array-like objects as well
sundar
parents: 19892
diff changeset
  1347
        return dst;
4235a28bb5e8 8024847: Java.to should accept mirror and external JSObjects as array-like objects as well
sundar
parents: 19892
diff changeset
  1348
    }
4235a28bb5e8 8024847: Java.to should accept mirror and external JSObjects as array-like objects as well
sundar
parents: 19892
diff changeset
  1349
4235a28bb5e8 8024847: Java.to should accept mirror and external JSObjects as array-like objects as well
sundar
parents: 19892
diff changeset
  1350
    /**
20944
a166d40ac0d4 8026113: Nashorn arrays should automatically convert to Java arrays
attila
parents: 20928
diff changeset
  1351
     * Converts a JavaScript object to a Java List. See {@link ListAdapter} for details.
a166d40ac0d4 8026113: Nashorn arrays should automatically convert to Java arrays
attila
parents: 20928
diff changeset
  1352
     * @param obj the object to convert. Can be any array-like object.
a166d40ac0d4 8026113: Nashorn arrays should automatically convert to Java arrays
attila
parents: 20928
diff changeset
  1353
     * @return a List that is live-backed by the JavaScript object.
a166d40ac0d4 8026113: Nashorn arrays should automatically convert to Java arrays
attila
parents: 20928
diff changeset
  1354
     */
a166d40ac0d4 8026113: Nashorn arrays should automatically convert to Java arrays
attila
parents: 20928
diff changeset
  1355
    public static List<?> toJavaList(final Object obj) {
a166d40ac0d4 8026113: Nashorn arrays should automatically convert to Java arrays
attila
parents: 20928
diff changeset
  1356
        return ListAdapter.create(obj);
a166d40ac0d4 8026113: Nashorn arrays should automatically convert to Java arrays
attila
parents: 20928
diff changeset
  1357
    }
a166d40ac0d4 8026113: Nashorn arrays should automatically convert to Java arrays
attila
parents: 20928
diff changeset
  1358
a166d40ac0d4 8026113: Nashorn arrays should automatically convert to Java arrays
attila
parents: 20928
diff changeset
  1359
    /**
a166d40ac0d4 8026113: Nashorn arrays should automatically convert to Java arrays
attila
parents: 20928
diff changeset
  1360
     * Converts a JavaScript object to a Java Deque. See {@link ListAdapter} for details.
a166d40ac0d4 8026113: Nashorn arrays should automatically convert to Java arrays
attila
parents: 20928
diff changeset
  1361
     * @param obj the object to convert. Can be any array-like object.
a166d40ac0d4 8026113: Nashorn arrays should automatically convert to Java arrays
attila
parents: 20928
diff changeset
  1362
     * @return a Deque that is live-backed by the JavaScript object.
a166d40ac0d4 8026113: Nashorn arrays should automatically convert to Java arrays
attila
parents: 20928
diff changeset
  1363
     */
a166d40ac0d4 8026113: Nashorn arrays should automatically convert to Java arrays
attila
parents: 20928
diff changeset
  1364
    public static Deque<?> toJavaDeque(final Object obj) {
a166d40ac0d4 8026113: Nashorn arrays should automatically convert to Java arrays
attila
parents: 20928
diff changeset
  1365
        return ListAdapter.create(obj);
a166d40ac0d4 8026113: Nashorn arrays should automatically convert to Java arrays
attila
parents: 20928
diff changeset
  1366
    }
a166d40ac0d4 8026113: Nashorn arrays should automatically convert to Java arrays
attila
parents: 20928
diff changeset
  1367
a166d40ac0d4 8026113: Nashorn arrays should automatically convert to Java arrays
attila
parents: 20928
diff changeset
  1368
    /**
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1369
     * Check if an object is null or undefined
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1370
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1371
     * @param obj object to check
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1372
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1373
     * @return true if null or undefined
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1374
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1375
    public static boolean nullOrUndefined(final Object obj) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1376
        return obj == null || obj == ScriptRuntime.UNDEFINED;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1377
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1378
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1379
    static String toStringImpl(final Object obj, final boolean safe) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1380
        if (obj instanceof String) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1381
            return (String)obj;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1382
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1383
29133
1cd7d8af99ba 8072426: Can't compare Java objects to strings or numbers
attila
parents: 28126
diff changeset
  1384
        if (obj instanceof ConsString) {
1cd7d8af99ba 8072426: Can't compare Java objects to strings or numbers
attila
parents: 28126
diff changeset
  1385
            return obj.toString();
1cd7d8af99ba 8072426: Can't compare Java objects to strings or numbers
attila
parents: 28126
diff changeset
  1386
        }
1cd7d8af99ba 8072426: Can't compare Java objects to strings or numbers
attila
parents: 28126
diff changeset
  1387
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1388
        if (obj instanceof Number) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1389
            return toString(((Number)obj).doubleValue());
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1390
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1391
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1392
        if (obj == ScriptRuntime.UNDEFINED) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1393
            return "undefined";
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1394
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1395
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1396
        if (obj == null) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1397
            return "null";
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1398
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1399
29133
1cd7d8af99ba 8072426: Can't compare Java objects to strings or numbers
attila
parents: 28126
diff changeset
  1400
        if (obj instanceof Boolean) {
1cd7d8af99ba 8072426: Can't compare Java objects to strings or numbers
attila
parents: 28126
diff changeset
  1401
            return obj.toString();
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1402
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1403
29133
1cd7d8af99ba 8072426: Can't compare Java objects to strings or numbers
attila
parents: 28126
diff changeset
  1404
        if (safe && obj instanceof ScriptObject) {
1cd7d8af99ba 8072426: Can't compare Java objects to strings or numbers
attila
parents: 28126
diff changeset
  1405
            final ScriptObject sobj = (ScriptObject)obj;
1cd7d8af99ba 8072426: Can't compare Java objects to strings or numbers
attila
parents: 28126
diff changeset
  1406
            final Global gobj = Context.getGlobal();
1cd7d8af99ba 8072426: Can't compare Java objects to strings or numbers
attila
parents: 28126
diff changeset
  1407
            return gobj.isError(sobj) ?
1cd7d8af99ba 8072426: Can't compare Java objects to strings or numbers
attila
parents: 28126
diff changeset
  1408
                ECMAException.safeToString(sobj) :
1cd7d8af99ba 8072426: Can't compare Java objects to strings or numbers
attila
parents: 28126
diff changeset
  1409
                sobj.safeToString();
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1410
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1411
29133
1cd7d8af99ba 8072426: Can't compare Java objects to strings or numbers
attila
parents: 28126
diff changeset
  1412
        return toString(toPrimitive(obj, String.class));
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1413
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1414
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1415
    // trim from left for JS whitespaces.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1416
    static String trimLeft(final String str) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1417
        int start = 0;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1418
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1419
        while (start < str.length() && Lexer.isJSWhitespace(str.charAt(start))) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1420
            start++;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1421
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1422
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1423
        return str.substring(start);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1424
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1425
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1426
    /**
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1427
     * Throw an unwarranted optimism exception for a program point
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1428
     * @param value         real return value
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1429
     * @param programPoint  program point
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1430
     * @return
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1431
     */
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1432
    @SuppressWarnings("unused")
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1433
    private static Object throwUnwarrantedOptimismException(final Object value, final int programPoint) {
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1434
        throw new UnwarrantedOptimismException(value, programPoint);
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1435
    }
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1436
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1437
    /**
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1438
     * Wrapper for addExact
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1439
     *
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1440
     * Catches ArithmeticException and rethrows as UnwarrantedOptimismException
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1441
     * containing the result and the program point of the failure
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1442
     *
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1443
     * @param x first term
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1444
     * @param y second term
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1445
     * @param programPoint program point id
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1446
     * @return the result
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1447
     * @throws UnwarrantedOptimismException if overflow occurs
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1448
     */
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1449
    public static int addExact(final int x, final int y, final int programPoint) throws UnwarrantedOptimismException {
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1450
        try {
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1451
            return Math.addExact(x, y);
24733
1e825be55fd1 8027043: Turn global accesses into MethodHandle.constant, with one chance of reassignment, e.g. x = value occuring once in the global scope is ok, twice is not.
lagergren
parents: 24727
diff changeset
  1452
        } catch (final ArithmeticException e) {
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1453
            throw new UnwarrantedOptimismException((long)x + (long)y, programPoint);
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1454
        }
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1455
    }
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1456
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1457
    /**
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1458
     * Wrapper for addExact
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1459
     *
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1460
     * Catches ArithmeticException and rethrows as UnwarrantedOptimismException
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1461
     * containing the result and the program point of the failure
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1462
     *
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1463
     * @param x first term
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1464
     * @param y second term
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1465
     * @param programPoint program point id
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1466
     * @return the result
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1467
     * @throws UnwarrantedOptimismException if overflow occurs
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1468
     */
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1469
    public static long addExact(final long x, final long y, final int programPoint) throws UnwarrantedOptimismException {
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1470
        try {
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1471
            return Math.addExact(x, y);
24733
1e825be55fd1 8027043: Turn global accesses into MethodHandle.constant, with one chance of reassignment, e.g. x = value occuring once in the global scope is ok, twice is not.
lagergren
parents: 24727
diff changeset
  1472
        } catch (final ArithmeticException e) {
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1473
            throw new UnwarrantedOptimismException((double)x + (double)y, programPoint);
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1474
        }
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1475
    }
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1476
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1477
    /**
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1478
     * Wrapper for subExact
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1479
     *
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1480
     * Catches ArithmeticException and rethrows as UnwarrantedOptimismException
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1481
     * containing the result and the program point of the failure
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1482
     *
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1483
     * @param x first term
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1484
     * @param y second term
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1485
     * @param programPoint program point id
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1486
     * @return the result
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1487
     * @throws UnwarrantedOptimismException if overflow occurs
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1488
     */
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1489
    public static int subExact(final int x, final int y, final int programPoint) throws UnwarrantedOptimismException {
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1490
        try {
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1491
            return Math.subtractExact(x, y);
24733
1e825be55fd1 8027043: Turn global accesses into MethodHandle.constant, with one chance of reassignment, e.g. x = value occuring once in the global scope is ok, twice is not.
lagergren
parents: 24727
diff changeset
  1492
        } catch (final ArithmeticException e) {
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1493
            throw new UnwarrantedOptimismException((long)x - (long)y, programPoint);
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1494
        }
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1495
    }
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1496
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1497
    /**
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1498
     * Wrapper for subExact
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1499
     *
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1500
     * Catches ArithmeticException and rethrows as UnwarrantedOptimismException
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1501
     * containing the result and the program point of the failure
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1502
     *
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1503
     * @param x first term
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1504
     * @param y second term
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1505
     * @param programPoint program point id
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1506
     * @return the result
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1507
     * @throws UnwarrantedOptimismException if overflow occurs
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1508
     */
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1509
    public static long subExact(final long x, final long y, final int programPoint) throws UnwarrantedOptimismException {
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1510
        try {
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1511
            return Math.subtractExact(x, y);
24733
1e825be55fd1 8027043: Turn global accesses into MethodHandle.constant, with one chance of reassignment, e.g. x = value occuring once in the global scope is ok, twice is not.
lagergren
parents: 24727
diff changeset
  1512
        } catch (final ArithmeticException e) {
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1513
            throw new UnwarrantedOptimismException((double)x - (double)y, programPoint);
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1514
        }
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1515
    }
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1516
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1517
    /**
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1518
     * Wrapper for mulExact
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1519
     *
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1520
     * Catches ArithmeticException and rethrows as UnwarrantedOptimismException
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1521
     * containing the result and the program point of the failure
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1522
     *
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1523
     * @param x first term
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1524
     * @param y second term
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1525
     * @param programPoint program point id
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1526
     * @return the result
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1527
     * @throws UnwarrantedOptimismException if overflow occurs
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1528
     */
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1529
    public static int mulExact(final int x, final int y, final int programPoint) throws UnwarrantedOptimismException {
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1530
        try {
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1531
            return Math.multiplyExact(x, y);
24733
1e825be55fd1 8027043: Turn global accesses into MethodHandle.constant, with one chance of reassignment, e.g. x = value occuring once in the global scope is ok, twice is not.
lagergren
parents: 24727
diff changeset
  1532
        } catch (final ArithmeticException e) {
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1533
            throw new UnwarrantedOptimismException((long)x * (long)y, programPoint);
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1534
        }
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1535
    }
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1536
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1537
    /**
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1538
     * Wrapper for mulExact
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1539
     *
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1540
     * Catches ArithmeticException and rethrows as UnwarrantedOptimismException
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1541
     * containing the result and the program point of the failure
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1542
     *
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1543
     * @param x first term
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1544
     * @param y second term
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1545
     * @param programPoint program point id
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1546
     * @return the result
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1547
     * @throws UnwarrantedOptimismException if overflow occurs
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1548
     */
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1549
    public static long mulExact(final long x, final long y, final int programPoint) throws UnwarrantedOptimismException {
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1550
        try {
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1551
            return Math.multiplyExact(x, y);
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1552
        } catch (final ArithmeticException e) {
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1553
            throw new UnwarrantedOptimismException((double)x * (double)y, programPoint);
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1554
        }
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1555
    }
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1556
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1557
    /**
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1558
     * 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
  1559
     * int.
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1560
     *
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1561
     * @param x first term
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1562
     * @param y second term
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1563
     * @param programPoint program point id
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1564
     * @return the result
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1565
     * @throws UnwarrantedOptimismException if the result of the division can't be represented as int.
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1566
     */
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1567
    public static int divExact(final int x, final int y, final int programPoint) throws UnwarrantedOptimismException {
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1568
        final int res;
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1569
        try {
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1570
            res = x / y;
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1571
        } catch (final ArithmeticException e) {
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1572
            assert y == 0; // Only div by zero anticipated
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1573
            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
  1574
        }
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1575
        final int rem = x % y;
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1576
        if (rem == 0) {
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1577
            return res;
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1578
        }
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1579
        // 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
  1580
        throw new UnwarrantedOptimismException((double)x / (double)y, programPoint);
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1581
    }
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1582
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1583
    /**
27358
8898e06e68c1 8061957: Some arithmetic operations have unnecessary widening
attila
parents: 27212
diff changeset
  1584
     * Implements int division but allows {@code x / 0} to be represented as 0. Basically equivalent to
8898e06e68c1 8061957: Some arithmetic operations have unnecessary widening
attila
parents: 27212
diff changeset
  1585
     * {@code (x / y)|0} JavaScript expression (division of two ints coerced to int).
8898e06e68c1 8061957: Some arithmetic operations have unnecessary widening
attila
parents: 27212
diff changeset
  1586
     * @param x the dividend
8898e06e68c1 8061957: Some arithmetic operations have unnecessary widening
attila
parents: 27212
diff changeset
  1587
     * @param y the divisor
8898e06e68c1 8061957: Some arithmetic operations have unnecessary widening
attila
parents: 27212
diff changeset
  1588
     * @return the result
8898e06e68c1 8061957: Some arithmetic operations have unnecessary widening
attila
parents: 27212
diff changeset
  1589
     */
8898e06e68c1 8061957: Some arithmetic operations have unnecessary widening
attila
parents: 27212
diff changeset
  1590
    public static int divZero(final int x, final int y) {
8898e06e68c1 8061957: Some arithmetic operations have unnecessary widening
attila
parents: 27212
diff changeset
  1591
        return y == 0 ? 0 : x / y;
8898e06e68c1 8061957: Some arithmetic operations have unnecessary widening
attila
parents: 27212
diff changeset
  1592
    }
8898e06e68c1 8061957: Some arithmetic operations have unnecessary widening
attila
parents: 27212
diff changeset
  1593
8898e06e68c1 8061957: Some arithmetic operations have unnecessary widening
attila
parents: 27212
diff changeset
  1594
    /**
8898e06e68c1 8061957: Some arithmetic operations have unnecessary widening
attila
parents: 27212
diff changeset
  1595
     * Implements int remainder but allows {@code x % 0} to be represented as 0. Basically equivalent to
8898e06e68c1 8061957: Some arithmetic operations have unnecessary widening
attila
parents: 27212
diff changeset
  1596
     * {@code (x % y)|0} JavaScript expression (remainder of two ints coerced to int).
8898e06e68c1 8061957: Some arithmetic operations have unnecessary widening
attila
parents: 27212
diff changeset
  1597
     * @param x the dividend
8898e06e68c1 8061957: Some arithmetic operations have unnecessary widening
attila
parents: 27212
diff changeset
  1598
     * @param y the divisor
8898e06e68c1 8061957: Some arithmetic operations have unnecessary widening
attila
parents: 27212
diff changeset
  1599
     * @return the remainder
8898e06e68c1 8061957: Some arithmetic operations have unnecessary widening
attila
parents: 27212
diff changeset
  1600
     */
8898e06e68c1 8061957: Some arithmetic operations have unnecessary widening
attila
parents: 27212
diff changeset
  1601
    public static int remZero(final int x, final int y) {
8898e06e68c1 8061957: Some arithmetic operations have unnecessary widening
attila
parents: 27212
diff changeset
  1602
        return y == 0 ? 0 : x % y;
8898e06e68c1 8061957: Some arithmetic operations have unnecessary widening
attila
parents: 27212
diff changeset
  1603
    }
8898e06e68c1 8061957: Some arithmetic operations have unnecessary widening
attila
parents: 27212
diff changeset
  1604
8898e06e68c1 8061957: Some arithmetic operations have unnecessary widening
attila
parents: 27212
diff changeset
  1605
    /**
24751
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24733
diff changeset
  1606
     * 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
  1607
     *
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24733
diff changeset
  1608
     * @param x first term
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24733
diff changeset
  1609
     * @param y second term
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24733
diff changeset
  1610
     * @param programPoint program point id
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24733
diff changeset
  1611
     * @return the result
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24733
diff changeset
  1612
     * @throws UnwarrantedOptimismException if the modulo can't be represented as int.
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24733
diff changeset
  1613
     */
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24733
diff changeset
  1614
    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
  1615
        try {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24733
diff changeset
  1616
            return x % y;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24733
diff changeset
  1617
        } catch (final ArithmeticException e) {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24733
diff changeset
  1618
            assert y == 0; // Only mod by zero anticipated
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24733
diff changeset
  1619
            throw new UnwarrantedOptimismException(Double.NaN, programPoint);
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24733
diff changeset
  1620
        }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24733
diff changeset
  1621
    }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24733
diff changeset
  1622
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24733
diff changeset
  1623
    /**
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1624
     * 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
  1625
     * long.
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1626
     *
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1627
     * @param x first term
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1628
     * @param y second term
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1629
     * @param programPoint program point id
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1630
     * @return the result
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1631
     * @throws UnwarrantedOptimismException if the result of the division can't be represented as long.
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1632
     */
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1633
    public static long divExact(final long x, final long y, final int programPoint) throws UnwarrantedOptimismException {
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1634
        final long res;
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1635
        try {
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1636
            res = x / y;
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1637
        } catch (final ArithmeticException e) {
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1638
            assert y == 0L; // Only div by zero anticipated
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1639
            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
  1640
        }
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1641
        final long rem = x % y;
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1642
        if (rem == 0L) {
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1643
            return res;
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1644
        }
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1645
        throw new UnwarrantedOptimismException((double)x / (double)y, programPoint);
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1646
    }
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1647
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1648
    /**
27358
8898e06e68c1 8061957: Some arithmetic operations have unnecessary widening
attila
parents: 27212
diff changeset
  1649
     * Implements long division but allows {@code x / 0} to be represented as 0. Useful when division of two longs
8898e06e68c1 8061957: Some arithmetic operations have unnecessary widening
attila
parents: 27212
diff changeset
  1650
     * is coerced to long.
8898e06e68c1 8061957: Some arithmetic operations have unnecessary widening
attila
parents: 27212
diff changeset
  1651
     * @param x the dividend
8898e06e68c1 8061957: Some arithmetic operations have unnecessary widening
attila
parents: 27212
diff changeset
  1652
     * @param y the divisor
8898e06e68c1 8061957: Some arithmetic operations have unnecessary widening
attila
parents: 27212
diff changeset
  1653
     * @return the result
8898e06e68c1 8061957: Some arithmetic operations have unnecessary widening
attila
parents: 27212
diff changeset
  1654
     */
8898e06e68c1 8061957: Some arithmetic operations have unnecessary widening
attila
parents: 27212
diff changeset
  1655
    public static long divZero(final long x, final long y) {
8898e06e68c1 8061957: Some arithmetic operations have unnecessary widening
attila
parents: 27212
diff changeset
  1656
        return y == 0L ? 0L : x / y;
8898e06e68c1 8061957: Some arithmetic operations have unnecessary widening
attila
parents: 27212
diff changeset
  1657
    }
8898e06e68c1 8061957: Some arithmetic operations have unnecessary widening
attila
parents: 27212
diff changeset
  1658
8898e06e68c1 8061957: Some arithmetic operations have unnecessary widening
attila
parents: 27212
diff changeset
  1659
    /**
8898e06e68c1 8061957: Some arithmetic operations have unnecessary widening
attila
parents: 27212
diff changeset
  1660
     * Implements long remainder but allows {@code x % 0} to be represented as 0. Useful when remainder of two longs
8898e06e68c1 8061957: Some arithmetic operations have unnecessary widening
attila
parents: 27212
diff changeset
  1661
     * is coerced to long.
8898e06e68c1 8061957: Some arithmetic operations have unnecessary widening
attila
parents: 27212
diff changeset
  1662
     * @param x the dividend
8898e06e68c1 8061957: Some arithmetic operations have unnecessary widening
attila
parents: 27212
diff changeset
  1663
     * @param y the divisor
8898e06e68c1 8061957: Some arithmetic operations have unnecessary widening
attila
parents: 27212
diff changeset
  1664
     * @return the remainder
8898e06e68c1 8061957: Some arithmetic operations have unnecessary widening
attila
parents: 27212
diff changeset
  1665
     */
8898e06e68c1 8061957: Some arithmetic operations have unnecessary widening
attila
parents: 27212
diff changeset
  1666
    public static long remZero(final long x, final long y) {
8898e06e68c1 8061957: Some arithmetic operations have unnecessary widening
attila
parents: 27212
diff changeset
  1667
        return y == 0L ? 0L : x % y;
8898e06e68c1 8061957: Some arithmetic operations have unnecessary widening
attila
parents: 27212
diff changeset
  1668
    }
8898e06e68c1 8061957: Some arithmetic operations have unnecessary widening
attila
parents: 27212
diff changeset
  1669
8898e06e68c1 8061957: Some arithmetic operations have unnecessary widening
attila
parents: 27212
diff changeset
  1670
    /**
24751
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24733
diff changeset
  1671
     * 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
  1672
     *
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24733
diff changeset
  1673
     * @param x first term
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24733
diff changeset
  1674
     * @param y second term
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24733
diff changeset
  1675
     * @param programPoint program point id
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24733
diff changeset
  1676
     * @return the result
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24733
diff changeset
  1677
     * @throws UnwarrantedOptimismException if the modulo can't be represented as int.
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24733
diff changeset
  1678
     */
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24733
diff changeset
  1679
    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
  1680
        try {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24733
diff changeset
  1681
            return x % y;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24733
diff changeset
  1682
        } catch (final ArithmeticException e) {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24733
diff changeset
  1683
            assert y == 0L; // Only mod by zero anticipated
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24733
diff changeset
  1684
            throw new UnwarrantedOptimismException(Double.NaN, programPoint);
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24733
diff changeset
  1685
        }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24733
diff changeset
  1686
    }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24733
diff changeset
  1687
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24733
diff changeset
  1688
    /**
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1689
     * Wrapper for decrementExact
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1690
     *
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1691
     * Catches ArithmeticException and rethrows as UnwarrantedOptimismException
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1692
     * containing the result and the program point of the failure
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1693
     *
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1694
     * @param x number to negate
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1695
     * @param programPoint program point id
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1696
     * @return the result
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1697
     * @throws UnwarrantedOptimismException if overflow occurs
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1698
     */
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1699
    public static int decrementExact(final int x, final int programPoint) throws UnwarrantedOptimismException {
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1700
        try {
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1701
            return Math.decrementExact(x);
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1702
        } catch (final ArithmeticException e) {
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1703
            throw new UnwarrantedOptimismException((long)x - 1, programPoint);
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1704
        }
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1705
    }
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1706
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1707
    /**
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1708
     * Wrapper for decrementExact
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1709
     *
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1710
     * Catches ArithmeticException and rethrows as UnwarrantedOptimismException
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1711
     * containing the result and the program point of the failure
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1712
     *
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1713
     * @param x number to negate
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1714
     * @param programPoint program point id
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1715
     * @return the result
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1716
     * @throws UnwarrantedOptimismException if overflow occurs
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1717
     */
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1718
    public static long decrementExact(final long x, final int programPoint) throws UnwarrantedOptimismException {
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1719
        try {
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1720
            return Math.decrementExact(x);
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1721
        } catch (final ArithmeticException e) {
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1722
            throw new UnwarrantedOptimismException((double)x - 1L, programPoint);
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1723
        }
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1724
    }
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1725
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1726
    /**
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1727
     * Wrapper for incrementExact
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1728
     *
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1729
     * Catches ArithmeticException and rethrows as UnwarrantedOptimismException
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1730
     * containing the result and the program point of the failure
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1731
     *
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1732
     * @param x the number to increment
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1733
     * @param programPoint program point id
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1734
     * @return the result
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1735
     * @throws UnwarrantedOptimismException if overflow occurs
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1736
     */
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1737
    public static int incrementExact(final int x, final int programPoint) throws UnwarrantedOptimismException {
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1738
        try {
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1739
            return Math.incrementExact(x);
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1740
        } catch (final ArithmeticException e) {
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1741
            throw new UnwarrantedOptimismException((long)x + 1, programPoint);
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1742
        }
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1743
    }
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1744
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1745
    /**
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1746
     * Wrapper for incrementExact
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1747
     *
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1748
     * Catches ArithmeticException and rethrows as UnwarrantedOptimismException
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1749
     * containing the result and the program point of the failure
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1750
     *
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1751
     * @param x the number to increment
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1752
     * @param programPoint program point id
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1753
     * @return the result
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1754
     * @throws UnwarrantedOptimismException if overflow occurs
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1755
     */
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1756
    public static long incrementExact(final long x, final int programPoint) throws UnwarrantedOptimismException {
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1757
        try {
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1758
            return Math.incrementExact(x);
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1759
        } catch (final ArithmeticException e) {
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1760
            throw new UnwarrantedOptimismException((double)x + 1L, programPoint);
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1761
        }
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1762
    }
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1763
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1764
    /**
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1765
     * Wrapper for negateExact
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1766
     *
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1767
     * Catches ArithmeticException and rethrows as UnwarrantedOptimismException
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1768
     * containing the result and the program point of the failure
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1769
     *
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1770
     * @param x the number to negate
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1771
     * @param programPoint program point id
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1772
     * @return the result
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1773
     * @throws UnwarrantedOptimismException if overflow occurs
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1774
     */
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1775
    public static int negateExact(final int x, final int programPoint) throws UnwarrantedOptimismException {
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1776
        try {
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1777
            if (x == 0) {
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1778
                throw new UnwarrantedOptimismException(-0.0, programPoint);
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1779
            }
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1780
            return Math.negateExact(x);
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1781
        } catch (final ArithmeticException e) {
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1782
            throw new UnwarrantedOptimismException(-(long)x, programPoint);
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1783
        }
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1784
    }
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1785
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1786
    /**
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1787
     * Wrapper for negateExact
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1788
     *
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1789
     * Catches ArithmeticException and rethrows as UnwarrantedOptimismException
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1790
     * containing the result and the program point of the failure
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1791
     *
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1792
     * @param x the number to negate
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1793
     * @param programPoint program point id
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1794
     * @return the result
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1795
     * @throws UnwarrantedOptimismException if overflow occurs
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1796
     */
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1797
    public static long negateExact(final long x, final int programPoint) throws UnwarrantedOptimismException {
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1798
        try {
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1799
            if (x == 0L) {
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1800
                throw new UnwarrantedOptimismException(-0.0, programPoint);
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1801
            }
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1802
            return Math.negateExact(x);
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1803
        } catch (final ArithmeticException e) {
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1804
            throw new UnwarrantedOptimismException(-(double)x, programPoint);
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1805
        }
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1806
    }
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1807
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1808
    /**
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1809
     * Given a type of an accessor, return its index in [0..getNumberOfAccessorTypes())
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1810
     *
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1811
     * @param type the type
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1812
     *
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1813
     * @return the accessor index, or -1 if no accessor of this type exists
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1814
     */
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1815
    public static int getAccessorTypeIndex(final Type type) {
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1816
        return getAccessorTypeIndex(type.getTypeClass());
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1817
    }
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1818
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1819
    /**
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1820
     * Given a class of an accessor, return its index in [0..getNumberOfAccessorTypes())
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1821
     *
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1822
     * Note that this is hardcoded with respect to the dynamic contents of the accessor
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1823
     * types array for speed. Hotspot got stuck with this as 5% of the runtime in
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1824
     * a benchmark when it looped over values and increased an index counter. :-(
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1825
     *
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1826
     * @param type the type
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1827
     *
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1828
     * @return the accessor index, or -1 if no accessor of this type exists
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1829
     */
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1830
    public static int getAccessorTypeIndex(final Class<?> type) {
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1831
        if (type == null) {
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1832
            return TYPE_UNDEFINED_INDEX;
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1833
        } else if (type == int.class) {
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1834
            return TYPE_INT_INDEX;
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1835
        } else if (type == long.class) {
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1836
            return TYPE_LONG_INDEX;
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1837
        } else if (type == double.class) {
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1838
            return TYPE_DOUBLE_INDEX;
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1839
        } else if (!type.isPrimitive()) {
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1840
            return TYPE_OBJECT_INDEX;
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1841
        }
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1842
        return -1;
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1843
    }
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1844
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1845
    /**
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1846
     * Return the accessor type based on its index in [0..getNumberOfAccessorTypes())
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1847
     * Indexes are ordered narrower{@literal ->}wider / optimistic{@literal ->}pessimistic. Invalidations always
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1848
     * go to a type of higher index
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1849
     *
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1850
     * @param index accessor type index
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1851
     *
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1852
     * @return a type corresponding to the index.
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1853
     */
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1854
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1855
    public static Type getAccessorType(final int index) {
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1856
        return ACCESSOR_TYPES.get(index);
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1857
    }
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1858
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1859
    /**
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1860
     * Return the number of accessor types available.
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1861
     *
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1862
     * @return number of accessor types in system
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1863
     */
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1864
    public static int getNumberOfAccessorTypes() {
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1865
        return ACCESSOR_TYPES.size();
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1866
    }
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1867
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1868
    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
  1869
        int pos = 0;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1870
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1871
        for (int i = start; i < length ; i++) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1872
            if (digit(chars[i], radix) == -1) {
18627
ef4e353a4a8f 8019482: Number("0x0.0p0") should evaluate to NaN
sundar
parents: 17778
diff changeset
  1873
                return Double.NaN;
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1874
            }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1875
            pos++;
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 (pos == 0) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1879
            return Double.NaN;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1880
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1881
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1882
        double value = 0.0;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1883
        for (int i = start; i < start + pos; i++) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1884
            value *= radix;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1885
            value += digit(chars[i], radix);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1886
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1887
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1888
        return value;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1889
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1890
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1891
    private static double toNumberGeneric(final Object obj) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1892
        if (obj == null) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1893
            return +0.0;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1894
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1895
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1896
        if (obj instanceof String) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1897
            return toNumber((String)obj);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1898
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1899
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1900
        if (obj instanceof ConsString) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1901
            return toNumber(obj.toString());
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1902
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1903
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1904
        if (obj instanceof Boolean) {
29133
1cd7d8af99ba 8072426: Can't compare Java objects to strings or numbers
attila
parents: 28126
diff changeset
  1905
            return toNumber((Boolean)obj);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1906
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1907
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1908
        if (obj instanceof ScriptObject) {
21449
72d51df5ed85 8027031: complete merging of loads and converts
attila
parents: 21437
diff changeset
  1909
            return toNumber((ScriptObject)obj);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1910
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1911
29133
1cd7d8af99ba 8072426: Can't compare Java objects to strings or numbers
attila
parents: 28126
diff changeset
  1912
        if (obj instanceof Undefined) {
1cd7d8af99ba 8072426: Can't compare Java objects to strings or numbers
attila
parents: 28126
diff changeset
  1913
            return Double.NaN;
21453
b48953eede53 8027128: jdk.nashorn.api.scripting.JSObject should be an interface
sundar
parents: 21450
diff changeset
  1914
        }
b48953eede53 8027128: jdk.nashorn.api.scripting.JSObject should be an interface
sundar
parents: 21450
diff changeset
  1915
29133
1cd7d8af99ba 8072426: Can't compare Java objects to strings or numbers
attila
parents: 28126
diff changeset
  1916
        return toNumber(toPrimitive(obj, Number.class));
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1917
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1918
19897
4235a28bb5e8 8024847: Java.to should accept mirror and external JSObjects as array-like objects as well
sundar
parents: 19892
diff changeset
  1919
    private static Object invoke(final MethodHandle mh, final Object arg) {
4235a28bb5e8 8024847: Java.to should accept mirror and external JSObjects as array-like objects as well
sundar
parents: 19892
diff changeset
  1920
        try {
4235a28bb5e8 8024847: Java.to should accept mirror and external JSObjects as array-like objects as well
sundar
parents: 19892
diff changeset
  1921
            return mh.invoke(arg);
4235a28bb5e8 8024847: Java.to should accept mirror and external JSObjects as array-like objects as well
sundar
parents: 19892
diff changeset
  1922
        } catch (final RuntimeException | Error e) {
4235a28bb5e8 8024847: Java.to should accept mirror and external JSObjects as array-like objects as well
sundar
parents: 19892
diff changeset
  1923
            throw e;
4235a28bb5e8 8024847: Java.to should accept mirror and external JSObjects as array-like objects as well
sundar
parents: 19892
diff changeset
  1924
        } catch (final Throwable t) {
4235a28bb5e8 8024847: Java.to should accept mirror and external JSObjects as array-like objects as well
sundar
parents: 19892
diff changeset
  1925
            throw new RuntimeException(t);
4235a28bb5e8 8024847: Java.to should accept mirror and external JSObjects as array-like objects as well
sundar
parents: 19892
diff changeset
  1926
        }
4235a28bb5e8 8024847: Java.to should accept mirror and external JSObjects as array-like objects as well
sundar
parents: 19892
diff changeset
  1927
    }
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21691
diff changeset
  1928
24733
1e825be55fd1 8027043: Turn global accesses into MethodHandle.constant, with one chance of reassignment, e.g. x = value occuring once in the global scope is ok, twice is not.
lagergren
parents: 24727
diff changeset
  1929
    /**
27209
30d8609b9561 8061391: concat as a builtin optimistic form, had to remove NoTypedArrayData and replace it, as we throw away a lot of optimistic link opportunities with NoTypedArrayData not being Continuous
lagergren
parents: 26378
diff changeset
  1930
     * Returns the boxed version of a primitive class
30d8609b9561 8061391: concat as a builtin optimistic form, had to remove NoTypedArrayData and replace it, as we throw away a lot of optimistic link opportunities with NoTypedArrayData not being Continuous
lagergren
parents: 26378
diff changeset
  1931
     * @param clazz the class
30d8609b9561 8061391: concat as a builtin optimistic form, had to remove NoTypedArrayData and replace it, as we throw away a lot of optimistic link opportunities with NoTypedArrayData not being Continuous
lagergren
parents: 26378
diff changeset
  1932
     * @return the boxed type of clazz, or unchanged if not primitive
30d8609b9561 8061391: concat as a builtin optimistic form, had to remove NoTypedArrayData and replace it, as we throw away a lot of optimistic link opportunities with NoTypedArrayData not being Continuous
lagergren
parents: 26378
diff changeset
  1933
     */
30d8609b9561 8061391: concat as a builtin optimistic form, had to remove NoTypedArrayData and replace it, as we throw away a lot of optimistic link opportunities with NoTypedArrayData not being Continuous
lagergren
parents: 26378
diff changeset
  1934
    public static Class<?> getBoxedClass(final Class<?> clazz) {
30d8609b9561 8061391: concat as a builtin optimistic form, had to remove NoTypedArrayData and replace it, as we throw away a lot of optimistic link opportunities with NoTypedArrayData not being Continuous
lagergren
parents: 26378
diff changeset
  1935
        if (clazz == int.class) {
30d8609b9561 8061391: concat as a builtin optimistic form, had to remove NoTypedArrayData and replace it, as we throw away a lot of optimistic link opportunities with NoTypedArrayData not being Continuous
lagergren
parents: 26378
diff changeset
  1936
            return Integer.class;
30d8609b9561 8061391: concat as a builtin optimistic form, had to remove NoTypedArrayData and replace it, as we throw away a lot of optimistic link opportunities with NoTypedArrayData not being Continuous
lagergren
parents: 26378
diff changeset
  1937
        } else if (clazz == long.class) {
30d8609b9561 8061391: concat as a builtin optimistic form, had to remove NoTypedArrayData and replace it, as we throw away a lot of optimistic link opportunities with NoTypedArrayData not being Continuous
lagergren
parents: 26378
diff changeset
  1938
            return Long.class;
30d8609b9561 8061391: concat as a builtin optimistic form, had to remove NoTypedArrayData and replace it, as we throw away a lot of optimistic link opportunities with NoTypedArrayData not being Continuous
lagergren
parents: 26378
diff changeset
  1939
        } else if (clazz == double.class) {
30d8609b9561 8061391: concat as a builtin optimistic form, had to remove NoTypedArrayData and replace it, as we throw away a lot of optimistic link opportunities with NoTypedArrayData not being Continuous
lagergren
parents: 26378
diff changeset
  1940
            return Double.class;
30d8609b9561 8061391: concat as a builtin optimistic form, had to remove NoTypedArrayData and replace it, as we throw away a lot of optimistic link opportunities with NoTypedArrayData not being Continuous
lagergren
parents: 26378
diff changeset
  1941
        }
30d8609b9561 8061391: concat as a builtin optimistic form, had to remove NoTypedArrayData and replace it, as we throw away a lot of optimistic link opportunities with NoTypedArrayData not being Continuous
lagergren
parents: 26378
diff changeset
  1942
        assert !clazz.isPrimitive();
30d8609b9561 8061391: concat as a builtin optimistic form, had to remove NoTypedArrayData and replace it, as we throw away a lot of optimistic link opportunities with NoTypedArrayData not being Continuous
lagergren
parents: 26378
diff changeset
  1943
        return clazz;
30d8609b9561 8061391: concat as a builtin optimistic form, had to remove NoTypedArrayData and replace it, as we throw away a lot of optimistic link opportunities with NoTypedArrayData not being Continuous
lagergren
parents: 26378
diff changeset
  1944
    }
30d8609b9561 8061391: concat as a builtin optimistic form, had to remove NoTypedArrayData and replace it, as we throw away a lot of optimistic link opportunities with NoTypedArrayData not being Continuous
lagergren
parents: 26378
diff changeset
  1945
30d8609b9561 8061391: concat as a builtin optimistic form, had to remove NoTypedArrayData and replace it, as we throw away a lot of optimistic link opportunities with NoTypedArrayData not being Continuous
lagergren
parents: 26378
diff changeset
  1946
    /**
24733
1e825be55fd1 8027043: Turn global accesses into MethodHandle.constant, with one chance of reassignment, e.g. x = value occuring once in the global scope is ok, twice is not.
lagergren
parents: 24727
diff changeset
  1947
     * Create a method handle constant of the correct primitive type
1e825be55fd1 8027043: Turn global accesses into MethodHandle.constant, with one chance of reassignment, e.g. x = value occuring once in the global scope is ok, twice is not.
lagergren
parents: 24727
diff changeset
  1948
     * for a constant object
1e825be55fd1 8027043: Turn global accesses into MethodHandle.constant, with one chance of reassignment, e.g. x = value occuring once in the global scope is ok, twice is not.
lagergren
parents: 24727
diff changeset
  1949
     * @param o object
1e825be55fd1 8027043: Turn global accesses into MethodHandle.constant, with one chance of reassignment, e.g. x = value occuring once in the global scope is ok, twice is not.
lagergren
parents: 24727
diff changeset
  1950
     * @return constant function that returns object
1e825be55fd1 8027043: Turn global accesses into MethodHandle.constant, with one chance of reassignment, e.g. x = value occuring once in the global scope is ok, twice is not.
lagergren
parents: 24727
diff changeset
  1951
     */
1e825be55fd1 8027043: Turn global accesses into MethodHandle.constant, with one chance of reassignment, e.g. x = value occuring once in the global scope is ok, twice is not.
lagergren
parents: 24727
diff changeset
  1952
    public static MethodHandle unboxConstant(final Object o) {
1e825be55fd1 8027043: Turn global accesses into MethodHandle.constant, with one chance of reassignment, e.g. x = value occuring once in the global scope is ok, twice is not.
lagergren
parents: 24727
diff changeset
  1953
        if (o != null) {
1e825be55fd1 8027043: Turn global accesses into MethodHandle.constant, with one chance of reassignment, e.g. x = value occuring once in the global scope is ok, twice is not.
lagergren
parents: 24727
diff changeset
  1954
            if (o.getClass() == Integer.class) {
1e825be55fd1 8027043: Turn global accesses into MethodHandle.constant, with one chance of reassignment, e.g. x = value occuring once in the global scope is ok, twice is not.
lagergren
parents: 24727
diff changeset
  1955
                return MH.constant(int.class, ((Integer)o).intValue());
1e825be55fd1 8027043: Turn global accesses into MethodHandle.constant, with one chance of reassignment, e.g. x = value occuring once in the global scope is ok, twice is not.
lagergren
parents: 24727
diff changeset
  1956
            } else if (o.getClass() == Long.class) {
1e825be55fd1 8027043: Turn global accesses into MethodHandle.constant, with one chance of reassignment, e.g. x = value occuring once in the global scope is ok, twice is not.
lagergren
parents: 24727
diff changeset
  1957
                return MH.constant(long.class, ((Long)o).longValue());
1e825be55fd1 8027043: Turn global accesses into MethodHandle.constant, with one chance of reassignment, e.g. x = value occuring once in the global scope is ok, twice is not.
lagergren
parents: 24727
diff changeset
  1958
            } else if (o.getClass() == Double.class) {
1e825be55fd1 8027043: Turn global accesses into MethodHandle.constant, with one chance of reassignment, e.g. x = value occuring once in the global scope is ok, twice is not.
lagergren
parents: 24727
diff changeset
  1959
                return MH.constant(double.class, ((Double)o).doubleValue());
1e825be55fd1 8027043: Turn global accesses into MethodHandle.constant, with one chance of reassignment, e.g. x = value occuring once in the global scope is ok, twice is not.
lagergren
parents: 24727
diff changeset
  1960
            }
1e825be55fd1 8027043: Turn global accesses into MethodHandle.constant, with one chance of reassignment, e.g. x = value occuring once in the global scope is ok, twice is not.
lagergren
parents: 24727
diff changeset
  1961
        }
1e825be55fd1 8027043: Turn global accesses into MethodHandle.constant, with one chance of reassignment, e.g. x = value occuring once in the global scope is ok, twice is not.
lagergren
parents: 24727
diff changeset
  1962
        return MH.constant(Object.class, o);
1e825be55fd1 8027043: Turn global accesses into MethodHandle.constant, with one chance of reassignment, e.g. x = value occuring once in the global scope is ok, twice is not.
lagergren
parents: 24727
diff changeset
  1963
    }
1e825be55fd1 8027043: Turn global accesses into MethodHandle.constant, with one chance of reassignment, e.g. x = value occuring once in the global scope is ok, twice is not.
lagergren
parents: 24727
diff changeset
  1964
1e825be55fd1 8027043: Turn global accesses into MethodHandle.constant, with one chance of reassignment, e.g. x = value occuring once in the global scope is ok, twice is not.
lagergren
parents: 24727
diff changeset
  1965
    /**
1e825be55fd1 8027043: Turn global accesses into MethodHandle.constant, with one chance of reassignment, e.g. x = value occuring once in the global scope is ok, twice is not.
lagergren
parents: 24727
diff changeset
  1966
     * Get the unboxed (primitive) type for an object
1e825be55fd1 8027043: Turn global accesses into MethodHandle.constant, with one chance of reassignment, e.g. x = value occuring once in the global scope is ok, twice is not.
lagergren
parents: 24727
diff changeset
  1967
     * @param o object
1e825be55fd1 8027043: Turn global accesses into MethodHandle.constant, with one chance of reassignment, e.g. x = value occuring once in the global scope is ok, twice is not.
lagergren
parents: 24727
diff changeset
  1968
     * @return primive type or Object.class if not primitive
1e825be55fd1 8027043: Turn global accesses into MethodHandle.constant, with one chance of reassignment, e.g. x = value occuring once in the global scope is ok, twice is not.
lagergren
parents: 24727
diff changeset
  1969
     */
1e825be55fd1 8027043: Turn global accesses into MethodHandle.constant, with one chance of reassignment, e.g. x = value occuring once in the global scope is ok, twice is not.
lagergren
parents: 24727
diff changeset
  1970
    public static Class<?> unboxedFieldType(final Object o) {
1e825be55fd1 8027043: Turn global accesses into MethodHandle.constant, with one chance of reassignment, e.g. x = value occuring once in the global scope is ok, twice is not.
lagergren
parents: 24727
diff changeset
  1971
        if (OBJECT_FIELDS_ONLY) {
1e825be55fd1 8027043: Turn global accesses into MethodHandle.constant, with one chance of reassignment, e.g. x = value occuring once in the global scope is ok, twice is not.
lagergren
parents: 24727
diff changeset
  1972
            return Object.class;
1e825be55fd1 8027043: Turn global accesses into MethodHandle.constant, with one chance of reassignment, e.g. x = value occuring once in the global scope is ok, twice is not.
lagergren
parents: 24727
diff changeset
  1973
        }
1e825be55fd1 8027043: Turn global accesses into MethodHandle.constant, with one chance of reassignment, e.g. x = value occuring once in the global scope is ok, twice is not.
lagergren
parents: 24727
diff changeset
  1974
1e825be55fd1 8027043: Turn global accesses into MethodHandle.constant, with one chance of reassignment, e.g. x = value occuring once in the global scope is ok, twice is not.
lagergren
parents: 24727
diff changeset
  1975
        if (o == null) {
1e825be55fd1 8027043: Turn global accesses into MethodHandle.constant, with one chance of reassignment, e.g. x = value occuring once in the global scope is ok, twice is not.
lagergren
parents: 24727
diff changeset
  1976
            return Object.class;
1e825be55fd1 8027043: Turn global accesses into MethodHandle.constant, with one chance of reassignment, e.g. x = value occuring once in the global scope is ok, twice is not.
lagergren
parents: 24727
diff changeset
  1977
        } else if (o.getClass() == Integer.class) {
1e825be55fd1 8027043: Turn global accesses into MethodHandle.constant, with one chance of reassignment, e.g. x = value occuring once in the global scope is ok, twice is not.
lagergren
parents: 24727
diff changeset
  1978
            return int.class;
1e825be55fd1 8027043: Turn global accesses into MethodHandle.constant, with one chance of reassignment, e.g. x = value occuring once in the global scope is ok, twice is not.
lagergren
parents: 24727
diff changeset
  1979
        } else if (o.getClass() == Long.class) {
1e825be55fd1 8027043: Turn global accesses into MethodHandle.constant, with one chance of reassignment, e.g. x = value occuring once in the global scope is ok, twice is not.
lagergren
parents: 24727
diff changeset
  1980
            return long.class;
1e825be55fd1 8027043: Turn global accesses into MethodHandle.constant, with one chance of reassignment, e.g. x = value occuring once in the global scope is ok, twice is not.
lagergren
parents: 24727
diff changeset
  1981
        } else if (o.getClass() == Double.class) {
1e825be55fd1 8027043: Turn global accesses into MethodHandle.constant, with one chance of reassignment, e.g. x = value occuring once in the global scope is ok, twice is not.
lagergren
parents: 24727
diff changeset
  1982
            return double.class;
1e825be55fd1 8027043: Turn global accesses into MethodHandle.constant, with one chance of reassignment, e.g. x = value occuring once in the global scope is ok, twice is not.
lagergren
parents: 24727
diff changeset
  1983
        } else {
1e825be55fd1 8027043: Turn global accesses into MethodHandle.constant, with one chance of reassignment, e.g. x = value occuring once in the global scope is ok, twice is not.
lagergren
parents: 24727
diff changeset
  1984
            return Object.class;
1e825be55fd1 8027043: Turn global accesses into MethodHandle.constant, with one chance of reassignment, e.g. x = value occuring once in the global scope is ok, twice is not.
lagergren
parents: 24727
diff changeset
  1985
        }
1e825be55fd1 8027043: Turn global accesses into MethodHandle.constant, with one chance of reassignment, e.g. x = value occuring once in the global scope is ok, twice is not.
lagergren
parents: 24727
diff changeset
  1986
    }
1e825be55fd1 8027043: Turn global accesses into MethodHandle.constant, with one chance of reassignment, e.g. x = value occuring once in the global scope is ok, twice is not.
lagergren
parents: 24727
diff changeset
  1987
25253
61601aa79b10 8049222: JSType class exposes public mutable arrays
attila
parents: 25249
diff changeset
  1988
    private static final List<MethodHandle> toUnmodifiableList(final MethodHandle... methodHandles) {
61601aa79b10 8049222: JSType class exposes public mutable arrays
attila
parents: 25249
diff changeset
  1989
        return Collections.unmodifiableList(Arrays.asList(methodHandles));
61601aa79b10 8049222: JSType class exposes public mutable arrays
attila
parents: 25249
diff changeset
  1990
    }
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1991
}