< prev index next >

src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/runtime/JSType.java

Print this page




  25 
  26 package jdk.nashorn.internal.runtime;
  27 
  28 import static jdk.nashorn.internal.codegen.CompilerConstants.staticCall;
  29 import static jdk.nashorn.internal.lookup.Lookup.MH;
  30 import static jdk.nashorn.internal.runtime.ECMAErrors.typeError;
  31 
  32 import java.lang.invoke.MethodHandle;
  33 import java.lang.invoke.MethodHandles;
  34 import java.lang.reflect.Array;
  35 import java.util.Arrays;
  36 import java.util.Collections;
  37 import java.util.List;
  38 import jdk.internal.dynalink.beans.StaticClass;
  39 import jdk.nashorn.api.scripting.JSObject;
  40 import jdk.nashorn.internal.codegen.CompilerConstants.Call;
  41 import jdk.nashorn.internal.codegen.types.Type;
  42 import jdk.nashorn.internal.objects.Global;
  43 import jdk.nashorn.internal.parser.Lexer;
  44 import jdk.nashorn.internal.runtime.arrays.ArrayLikeIterator;

  45 import jdk.nashorn.internal.runtime.linker.Bootstrap;
  46 
  47 /**
  48  * Representation for ECMAScript types - this maps directly to the ECMA script standard
  49  */
  50 public enum JSType {
  51     /** The undefined type */
  52     UNDEFINED("undefined"),
  53 
  54     /** The null type */
  55     NULL("object"),
  56 
  57     /** The boolean type */
  58     BOOLEAN("boolean"),
  59 
  60     /** The number type */
  61     NUMBER("number"),
  62 
  63     /** The string type */
  64     STRING("string"),


 670      *
 671      * @return a string
 672      */
 673     public static String toString(final double num) {
 674         if (isRepresentableAsInt(num)) {
 675             return Integer.toString((int)num);
 676         }
 677 
 678         if (num == Double.POSITIVE_INFINITY) {
 679             return "Infinity";
 680         }
 681 
 682         if (num == Double.NEGATIVE_INFINITY) {
 683             return "-Infinity";
 684         }
 685 
 686         if (Double.isNaN(num)) {
 687             return "NaN";
 688         }
 689 
 690         return NumberToString.stringFor(num);
 691     }
 692 
 693     /**
 694      * JavaScript compliant conversion of number to String
 695      *
 696      * @param num   a number
 697      * @param radix a radix for the conversion
 698      *
 699      * @return a string
 700      */
 701     public static String toString(final double num, final int radix) {
 702         assert radix >= 2 && radix <= 36 : "invalid radix";
 703 
 704         if (isRepresentableAsInt(num)) {
 705             return Integer.toString((int)num, radix);
 706         }
 707 
 708         if (num == Double.POSITIVE_INFINITY) {
 709             return "Infinity";
 710         }




  25 
  26 package jdk.nashorn.internal.runtime;
  27 
  28 import static jdk.nashorn.internal.codegen.CompilerConstants.staticCall;
  29 import static jdk.nashorn.internal.lookup.Lookup.MH;
  30 import static jdk.nashorn.internal.runtime.ECMAErrors.typeError;
  31 
  32 import java.lang.invoke.MethodHandle;
  33 import java.lang.invoke.MethodHandles;
  34 import java.lang.reflect.Array;
  35 import java.util.Arrays;
  36 import java.util.Collections;
  37 import java.util.List;
  38 import jdk.internal.dynalink.beans.StaticClass;
  39 import jdk.nashorn.api.scripting.JSObject;
  40 import jdk.nashorn.internal.codegen.CompilerConstants.Call;
  41 import jdk.nashorn.internal.codegen.types.Type;
  42 import jdk.nashorn.internal.objects.Global;
  43 import jdk.nashorn.internal.parser.Lexer;
  44 import jdk.nashorn.internal.runtime.arrays.ArrayLikeIterator;
  45 import jdk.nashorn.internal.runtime.doubleconv.DoubleConversion;
  46 import jdk.nashorn.internal.runtime.linker.Bootstrap;
  47 
  48 /**
  49  * Representation for ECMAScript types - this maps directly to the ECMA script standard
  50  */
  51 public enum JSType {
  52     /** The undefined type */
  53     UNDEFINED("undefined"),
  54 
  55     /** The null type */
  56     NULL("object"),
  57 
  58     /** The boolean type */
  59     BOOLEAN("boolean"),
  60 
  61     /** The number type */
  62     NUMBER("number"),
  63 
  64     /** The string type */
  65     STRING("string"),


 671      *
 672      * @return a string
 673      */
 674     public static String toString(final double num) {
 675         if (isRepresentableAsInt(num)) {
 676             return Integer.toString((int)num);
 677         }
 678 
 679         if (num == Double.POSITIVE_INFINITY) {
 680             return "Infinity";
 681         }
 682 
 683         if (num == Double.NEGATIVE_INFINITY) {
 684             return "-Infinity";
 685         }
 686 
 687         if (Double.isNaN(num)) {
 688             return "NaN";
 689         }
 690 
 691         return DoubleConversion.toShortestString(num);
 692     }
 693 
 694     /**
 695      * JavaScript compliant conversion of number to String
 696      *
 697      * @param num   a number
 698      * @param radix a radix for the conversion
 699      *
 700      * @return a string
 701      */
 702     public static String toString(final double num, final int radix) {
 703         assert radix >= 2 && radix <= 36 : "invalid radix";
 704 
 705         if (isRepresentableAsInt(num)) {
 706             return Integer.toString((int)num, radix);
 707         }
 708 
 709         if (num == Double.POSITIVE_INFINITY) {
 710             return "Infinity";
 711         }


< prev index next >