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