nashorn/src/jdk/nashorn/internal/objects/NativeNumber.java
changeset 24719 f726e9d67629
parent 18865 8844964e5fc5
child 24720 75f8388b79df
equal deleted inserted replaced
23083:8c74590d5df1 24719:f726e9d67629
    23  * questions.
    23  * questions.
    24  */
    24  */
    25 
    25 
    26 package jdk.nashorn.internal.objects;
    26 package jdk.nashorn.internal.objects;
    27 
    27 
       
    28 import static jdk.nashorn.internal.lookup.Lookup.MH;
    28 import static jdk.nashorn.internal.runtime.ECMAErrors.rangeError;
    29 import static jdk.nashorn.internal.runtime.ECMAErrors.rangeError;
    29 import static jdk.nashorn.internal.runtime.ECMAErrors.typeError;
    30 import static jdk.nashorn.internal.runtime.ECMAErrors.typeError;
    30 import static jdk.nashorn.internal.runtime.JSType.isRepresentableAsInt;
       
    31 import static jdk.nashorn.internal.runtime.JSType.isRepresentableAsLong;
       
    32 import static jdk.nashorn.internal.runtime.ScriptRuntime.UNDEFINED;
    31 import static jdk.nashorn.internal.runtime.ScriptRuntime.UNDEFINED;
    33 import static jdk.nashorn.internal.lookup.Lookup.MH;
       
    34 
    32 
    35 import java.lang.invoke.MethodHandle;
    33 import java.lang.invoke.MethodHandle;
    36 import java.lang.invoke.MethodHandles;
    34 import java.lang.invoke.MethodHandles;
    37 import java.text.NumberFormat;
    35 import java.text.NumberFormat;
    38 import java.util.Locale;
    36 import java.util.Locale;
    78     /** ECMA 15.7.3.5 positive infinity */
    76     /** ECMA 15.7.3.5 positive infinity */
    79     @Property(attributes = Attribute.NON_ENUMERABLE_CONSTANT, where = Where.CONSTRUCTOR)
    77     @Property(attributes = Attribute.NON_ENUMERABLE_CONSTANT, where = Where.CONSTRUCTOR)
    80     public static final double POSITIVE_INFINITY = Double.POSITIVE_INFINITY;
    78     public static final double POSITIVE_INFINITY = Double.POSITIVE_INFINITY;
    81 
    79 
    82     private final double  value;
    80     private final double  value;
    83     private final boolean isInt;
       
    84     private final boolean isLong;
       
    85 
    81 
    86     // initialized by nasgen
    82     // initialized by nasgen
    87     private static PropertyMap $nasgenmap$;
    83     private static PropertyMap $nasgenmap$;
    88 
    84 
    89     static PropertyMap getInitialMap() {
    85     static PropertyMap getInitialMap() {
    91     }
    87     }
    92 
    88 
    93     private NativeNumber(final double value, final ScriptObject proto, final PropertyMap map) {
    89     private NativeNumber(final double value, final ScriptObject proto, final PropertyMap map) {
    94         super(proto, map);
    90         super(proto, map);
    95         this.value = value;
    91         this.value = value;
    96         this.isInt  = isRepresentableAsInt(value);
       
    97         this.isLong = isRepresentableAsLong(value);
       
    98     }
    92     }
    99 
    93 
   100     NativeNumber(final double value, final Global global) {
    94     NativeNumber(final double value, final Global global) {
   101         this(value, global.getNumberPrototype(), global.getNumberMap());
    95         this(value, global.getNumberPrototype(), global.getNumberMap());
   102     }
    96     }
   128      * Get the value of this Number
   122      * Get the value of this Number
   129      * @return a {@code double} representing the Number value
   123      * @return a {@code double} representing the Number value
   130      */
   124      */
   131     public double doubleValue() {
   125     public double doubleValue() {
   132         return value;
   126         return value;
   133     }
       
   134 
       
   135     /**
       
   136      * Get the value of this Number as a {@code int}
       
   137      * @return an {@code int} representing the Number value
       
   138      * @throws ClassCastException If number is not representable as an {@code int}
       
   139      */
       
   140     public int intValue() throws ClassCastException {
       
   141         if (isInt) {
       
   142             return (int)value;
       
   143         }
       
   144         throw new ClassCastException();
       
   145     }
       
   146 
       
   147     /**
       
   148      * Get the value of this Number as a {@code long}
       
   149      * @return a {@code long} representing the Number value
       
   150      * @throws ClassCastException If number is not representable as an {@code long}
       
   151      */
       
   152     public long longValue() throws ClassCastException {
       
   153         if (isLong) {
       
   154             return (long)value;
       
   155         }
       
   156         throw new ClassCastException();
       
   157     }
   127     }
   158 
   128 
   159     @Override
   129     @Override
   160     public String getClassName() {
   130     public String getClassName() {
   161         return "Number";
   131         return "Number";