src/jdk/nashorn/internal/runtime/JSType.java

Print this page




  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package jdk.nashorn.internal.runtime;
  27 
  28 import static jdk.nashorn.internal.codegen.CompilerConstants.staticCall;
  29 import static jdk.nashorn.internal.runtime.ECMAErrors.typeError;
  30 

  31 import jdk.internal.dynalink.beans.StaticClass;
  32 import jdk.nashorn.internal.codegen.CompilerConstants.Call;
  33 import jdk.nashorn.internal.parser.Lexer;
  34 
  35 /**
  36  * Representation for ECMAScript types - this maps directly to the ECMA script standard
  37  */
  38 public enum JSType {
  39     /** The undefined type */
  40     UNDEFINED,
  41 
  42     /** The null type */
  43     NULL,
  44 
  45     /** The boolean type */
  46     BOOLEAN,
  47 
  48     /** The number type */
  49     NUMBER,
  50 


  94     public static final Call TO_INT64_D = staticCall(JSType.class, "toInt64", long.class, double.class);
  95 
  96     /** JavaScript compliant conversion function from Object to String */
  97     public static final Call TO_STRING = staticCall(JSType.class, "toString", String.class, Object.class);
  98 
  99     /** JavaScript compliant conversion function from number to String */
 100     public static final Call TO_STRING_D = staticCall(JSType.class, "toString", String.class, double.class);
 101 
 102     /** JavaScript compliant conversion function from Object to primitive */
 103     public static final Call TO_PRIMITIVE = staticCall(JSType.class, "toPrimitive", Object.class,  Object.class);
 104 
 105     private static final double INT32_LIMIT = 4294967296.0;
 106 
 107     /**
 108      * The external type name as returned by ECMAScript "typeof" operator
 109      *
 110      * @return type name for this type
 111      */
 112     public final String typeName() {
 113         // For NULL, "object" has to be returned!
 114         return ((this == NULL) ? OBJECT : this).name().toLowerCase();
 115     }
 116 
 117     /**
 118      * Return the JSType for a given object
 119      *
 120      * @param obj an object
 121      *
 122      * @return the JSType for the object
 123      */
 124     public static JSType of(final Object obj) {
 125         if (obj == ScriptRuntime.UNDEFINED) {
 126             return JSType.UNDEFINED;
 127         }
 128 
 129         if (obj == null) {
 130             return JSType.NULL;
 131         }
 132 
 133         if (obj instanceof Boolean) {
 134             return JSType.BOOLEAN;




  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package jdk.nashorn.internal.runtime;
  27 
  28 import static jdk.nashorn.internal.codegen.CompilerConstants.staticCall;
  29 import static jdk.nashorn.internal.runtime.ECMAErrors.typeError;
  30 
  31 import java.util.Locale;
  32 import jdk.internal.dynalink.beans.StaticClass;
  33 import jdk.nashorn.internal.codegen.CompilerConstants.Call;
  34 import jdk.nashorn.internal.parser.Lexer;
  35 
  36 /**
  37  * Representation for ECMAScript types - this maps directly to the ECMA script standard
  38  */
  39 public enum JSType {
  40     /** The undefined type */
  41     UNDEFINED,
  42 
  43     /** The null type */
  44     NULL,
  45 
  46     /** The boolean type */
  47     BOOLEAN,
  48 
  49     /** The number type */
  50     NUMBER,
  51 


  95     public static final Call TO_INT64_D = staticCall(JSType.class, "toInt64", long.class, double.class);
  96 
  97     /** JavaScript compliant conversion function from Object to String */
  98     public static final Call TO_STRING = staticCall(JSType.class, "toString", String.class, Object.class);
  99 
 100     /** JavaScript compliant conversion function from number to String */
 101     public static final Call TO_STRING_D = staticCall(JSType.class, "toString", String.class, double.class);
 102 
 103     /** JavaScript compliant conversion function from Object to primitive */
 104     public static final Call TO_PRIMITIVE = staticCall(JSType.class, "toPrimitive", Object.class,  Object.class);
 105 
 106     private static final double INT32_LIMIT = 4294967296.0;
 107 
 108     /**
 109      * The external type name as returned by ECMAScript "typeof" operator
 110      *
 111      * @return type name for this type
 112      */
 113     public final String typeName() {
 114         // For NULL, "object" has to be returned!
 115         return ((this == NULL) ? OBJECT : this).name().toLowerCase(Locale.ENGLISH);
 116     }
 117 
 118     /**
 119      * Return the JSType for a given object
 120      *
 121      * @param obj an object
 122      *
 123      * @return the JSType for the object
 124      */
 125     public static JSType of(final Object obj) {
 126         if (obj == ScriptRuntime.UNDEFINED) {
 127             return JSType.UNDEFINED;
 128         }
 129 
 130         if (obj == null) {
 131             return JSType.NULL;
 132         }
 133 
 134         if (obj instanceof Boolean) {
 135             return JSType.BOOLEAN;