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

Print this page
rev 760 : 8037400: Remove getInitialMap getters and GlobalObject interface
Reviewed-by: lagergren, jlaskey, attila


  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.lang.invoke.MethodHandle;
  32 import java.lang.invoke.MethodHandles;
  33 import java.lang.reflect.Array;
  34 import java.util.Deque;
  35 import java.util.List;
  36 import jdk.internal.dynalink.beans.StaticClass;
  37 import jdk.nashorn.api.scripting.JSObject;
  38 import jdk.nashorn.internal.codegen.CompilerConstants.Call;

  39 import jdk.nashorn.internal.parser.Lexer;
  40 import jdk.nashorn.internal.runtime.arrays.ArrayLikeIterator;
  41 import jdk.nashorn.internal.runtime.linker.Bootstrap;
  42 
  43 /**
  44  * Representation for ECMAScript types - this maps directly to the ECMA script standard
  45  */
  46 public enum JSType {
  47     /** The undefined type */
  48     UNDEFINED("undefined"),
  49 
  50     /** The null type */
  51     NULL("object"),
  52 
  53     /** The boolean type */
  54     BOOLEAN("boolean"),
  55 
  56     /** The number type */
  57     NUMBER("number"),
  58 


 835     /**
 836      * Identity converter for objects.
 837      *
 838      * @param obj an object
 839      * @return the boxed number
 840      */
 841     public static Object toObject(final Object obj) {
 842         return obj;
 843     }
 844 
 845     /**
 846      * Object conversion. This is used to convert objects and numbers to their corresponding
 847      * NativeObject type
 848      * See ECMA 9.9 ToObject
 849      *
 850      * @param obj     the object to convert
 851      *
 852      * @return the wrapped object
 853      */
 854     public static Object toScriptObject(final Object obj) {
 855         return toScriptObject(Context.getGlobalTrusted(), obj);
 856     }
 857 
 858     /**
 859      * Object conversion. This is used to convert objects and numbers to their corresponding
 860      * NativeObject type
 861      * See ECMA 9.9 ToObject
 862      *
 863      * @param global  the global object
 864      * @param obj     the object to convert
 865      *
 866      * @return the wrapped object
 867      */
 868     public static Object toScriptObject(final ScriptObject global, final Object obj) {
 869         if (nullOrUndefined(obj)) {
 870             throw typeError(global, "not.an.object", ScriptRuntime.safeToString(obj));
 871         }
 872 
 873         if (obj instanceof ScriptObject) {
 874             return obj;
 875         }
 876 
 877         return ((GlobalObject)global).wrapAsObject(obj);
 878     }
 879 
 880     /**
 881      * Script object to Java array conversion.
 882      *
 883      * @param obj script object to be converted to Java array
 884      * @param componentType component type of the destination array required
 885      * @return converted Java array
 886      */
 887     public static Object toJavaArray(final Object obj, final Class<?> componentType) {
 888         if (obj instanceof ScriptObject) {
 889             return ((ScriptObject)obj).getArray().asArrayOfType(componentType);
 890         } else if (obj instanceof JSObject) {
 891             final ArrayLikeIterator<?> itr = ArrayLikeIterator.arrayLikeIterator(obj);
 892             final int len = (int) itr.getLength();
 893             final Object[] res = new Object[len];
 894             int idx = 0;
 895             while (itr.hasNext()) {
 896                 res[idx++] = itr.next();
 897             }


 967     static String toStringImpl(final Object obj, final boolean safe) {
 968         if (obj instanceof String) {
 969             return (String)obj;
 970         }
 971 
 972         if (obj instanceof Number) {
 973             return toString(((Number)obj).doubleValue());
 974         }
 975 
 976         if (obj == ScriptRuntime.UNDEFINED) {
 977             return "undefined";
 978         }
 979 
 980         if (obj == null) {
 981             return "null";
 982         }
 983 
 984         if (obj instanceof ScriptObject) {
 985             if (safe) {
 986                 final ScriptObject sobj = (ScriptObject)obj;
 987                 final GlobalObject gobj = (GlobalObject)Context.getGlobalTrusted();
 988                 return gobj.isError(sobj) ?
 989                     ECMAException.safeToString(sobj) :
 990                     sobj.safeToString();
 991             }
 992 
 993             return toString(toPrimitive(obj, String.class));
 994         }
 995 
 996         if (obj instanceof StaticClass) {
 997             return "[JavaClass " + ((StaticClass)obj).getRepresentedClass().getName() + "]";
 998         }
 999 
1000         return obj.toString();
1001     }
1002 
1003     // trim from left for JS whitespaces.
1004     static String trimLeft(final String str) {
1005         int start = 0;
1006 
1007         while (start < str.length() && Lexer.isJSWhitespace(str.charAt(start))) {




  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.lang.invoke.MethodHandle;
  32 import java.lang.invoke.MethodHandles;
  33 import java.lang.reflect.Array;
  34 import java.util.Deque;
  35 import java.util.List;
  36 import jdk.internal.dynalink.beans.StaticClass;
  37 import jdk.nashorn.api.scripting.JSObject;
  38 import jdk.nashorn.internal.codegen.CompilerConstants.Call;
  39 import jdk.nashorn.internal.objects.Global;
  40 import jdk.nashorn.internal.parser.Lexer;
  41 import jdk.nashorn.internal.runtime.arrays.ArrayLikeIterator;
  42 import jdk.nashorn.internal.runtime.linker.Bootstrap;
  43 
  44 /**
  45  * Representation for ECMAScript types - this maps directly to the ECMA script standard
  46  */
  47 public enum JSType {
  48     /** The undefined type */
  49     UNDEFINED("undefined"),
  50 
  51     /** The null type */
  52     NULL("object"),
  53 
  54     /** The boolean type */
  55     BOOLEAN("boolean"),
  56 
  57     /** The number type */
  58     NUMBER("number"),
  59 


 836     /**
 837      * Identity converter for objects.
 838      *
 839      * @param obj an object
 840      * @return the boxed number
 841      */
 842     public static Object toObject(final Object obj) {
 843         return obj;
 844     }
 845 
 846     /**
 847      * Object conversion. This is used to convert objects and numbers to their corresponding
 848      * NativeObject type
 849      * See ECMA 9.9 ToObject
 850      *
 851      * @param obj     the object to convert
 852      *
 853      * @return the wrapped object
 854      */
 855     public static Object toScriptObject(final Object obj) {
 856         return toScriptObject(Context.getGlobal(), obj);
 857     }
 858 
 859     /**
 860      * Object conversion. This is used to convert objects and numbers to their corresponding
 861      * NativeObject type
 862      * See ECMA 9.9 ToObject
 863      *
 864      * @param global  the global object
 865      * @param obj     the object to convert
 866      *
 867      * @return the wrapped object
 868      */
 869     public static Object toScriptObject(final Global global, final Object obj) {
 870         if (nullOrUndefined(obj)) {
 871             throw typeError(global, "not.an.object", ScriptRuntime.safeToString(obj));
 872         }
 873 
 874         if (obj instanceof ScriptObject) {
 875             return obj;
 876         }
 877 
 878         return global.wrapAsObject(obj);
 879     }
 880 
 881     /**
 882      * Script object to Java array conversion.
 883      *
 884      * @param obj script object to be converted to Java array
 885      * @param componentType component type of the destination array required
 886      * @return converted Java array
 887      */
 888     public static Object toJavaArray(final Object obj, final Class<?> componentType) {
 889         if (obj instanceof ScriptObject) {
 890             return ((ScriptObject)obj).getArray().asArrayOfType(componentType);
 891         } else if (obj instanceof JSObject) {
 892             final ArrayLikeIterator<?> itr = ArrayLikeIterator.arrayLikeIterator(obj);
 893             final int len = (int) itr.getLength();
 894             final Object[] res = new Object[len];
 895             int idx = 0;
 896             while (itr.hasNext()) {
 897                 res[idx++] = itr.next();
 898             }


 968     static String toStringImpl(final Object obj, final boolean safe) {
 969         if (obj instanceof String) {
 970             return (String)obj;
 971         }
 972 
 973         if (obj instanceof Number) {
 974             return toString(((Number)obj).doubleValue());
 975         }
 976 
 977         if (obj == ScriptRuntime.UNDEFINED) {
 978             return "undefined";
 979         }
 980 
 981         if (obj == null) {
 982             return "null";
 983         }
 984 
 985         if (obj instanceof ScriptObject) {
 986             if (safe) {
 987                 final ScriptObject sobj = (ScriptObject)obj;
 988                 final Global gobj = Context.getGlobal();
 989                 return gobj.isError(sobj) ?
 990                     ECMAException.safeToString(sobj) :
 991                     sobj.safeToString();
 992             }
 993 
 994             return toString(toPrimitive(obj, String.class));
 995         }
 996 
 997         if (obj instanceof StaticClass) {
 998             return "[JavaClass " + ((StaticClass)obj).getRepresentedClass().getName() + "]";
 999         }
1000 
1001         return obj.toString();
1002     }
1003 
1004     // trim from left for JS whitespaces.
1005     static String trimLeft(final String str) {
1006         int start = 0;
1007 
1008         while (start < str.length() && Lexer.isJSWhitespace(str.charAt(start))) {