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