8049222: JSType class exposes public mutable arrays
authorattila
Thu, 03 Jul 2014 11:18:10 +0200
changeset 25253 61601aa79b10
parent 25252 e8bfc909db53
child 25254 a56c2713f1a8
8049222: JSType class exposes public mutable arrays Reviewed-by: hannesw, sundar
nashorn/src/jdk/nashorn/internal/codegen/ObjectClassGenerator.java
nashorn/src/jdk/nashorn/internal/runtime/JSType.java
nashorn/src/jdk/nashorn/internal/runtime/UserAccessorProperty.java
--- a/nashorn/src/jdk/nashorn/internal/codegen/ObjectClassGenerator.java	Wed Jul 02 18:10:31 2014 +0530
+++ b/nashorn/src/jdk/nashorn/internal/codegen/ObjectClassGenerator.java	Thu Jul 03 11:18:10 2014 +0200
@@ -550,7 +550,7 @@
     }
 
     //no optimism here. we do unconditional conversion to types
-    private static MethodHandle createGetterInner(final Class<?> forType, final Class<?> type, final MethodHandle primitiveGetter, final MethodHandle objectGetter, final MethodHandle[] converters, final int programPoint) {
+    private static MethodHandle createGetterInner(final Class<?> forType, final Class<?> type, final MethodHandle primitiveGetter, final MethodHandle objectGetter, final List<MethodHandle> converters, final int programPoint) {
         final int fti = forType == null ? TYPE_UNDEFINED_INDEX : getAccessorTypeIndex(forType);
         final int ti  = getAccessorTypeIndex(type);
         //this means fail if forType != type
@@ -564,7 +564,7 @@
             if (isOptimistic) {
                 //return undefined if asking for object. otherwise throw UnwarrantedOptimismException
                 if (ti == TYPE_OBJECT_INDEX) {
-                    return MH.dropArguments(GET_UNDEFINED[TYPE_OBJECT_INDEX], 0, Object.class);
+                    return MH.dropArguments(GET_UNDEFINED.get(TYPE_OBJECT_INDEX), 0, Object.class);
                 }
                 //throw exception
                 return MH.asType(
@@ -578,7 +578,7 @@
                     getter.type().changeReturnType(type));
             }
             //return an undefined and coerce it to the appropriate type
-            return MH.dropArguments(GET_UNDEFINED[ti], 0, Object.class);
+            return MH.dropArguments(GET_UNDEFINED.get(ti), 0, Object.class);
         }
 
         assert forType != null;
@@ -604,7 +604,7 @@
                     return MH.filterReturnValue(
                             objectGetter,
                             MH.insertArguments(
-                                    converters[ti],
+                                    converters.get(ti),
                                     1,
                                     programPoint));
                 }
@@ -631,7 +631,7 @@
         final MethodHandle tgetter = getterForType(forType, primitiveGetter, objectGetter);
         if (fti == TYPE_OBJECT_INDEX) {
             if (fti != ti) {
-                return MH.filterReturnValue(tgetter, CONVERT_OBJECT[ti]);
+                return MH.filterReturnValue(tgetter, CONVERT_OBJECT.get(ti));
             }
             return tgetter;
         }
--- a/nashorn/src/jdk/nashorn/internal/runtime/JSType.java	Wed Jul 02 18:10:31 2014 +0530
+++ b/nashorn/src/jdk/nashorn/internal/runtime/JSType.java	Thu Jul 03 11:18:10 2014 +0200
@@ -220,23 +220,23 @@
     public static final int TYPE_OBJECT_INDEX = 3; //getAccessorTypeIndex(Object.class);
 
     /** object conversion quickies with JS semantics - used for return value and parameter filter */
-    public static final MethodHandle[] CONVERT_OBJECT = {
+    public static final List<MethodHandle> CONVERT_OBJECT = toUnmodifiableList(
         JSType.TO_INT32.methodHandle(),
         JSType.TO_UINT32.methodHandle(),
         JSType.TO_NUMBER.methodHandle(),
         null
-    };
+    );
 
     /**
      * object conversion quickies with JS semantics - used for return value and parameter filter, optimistic
      * throws exception upon incompatible type (asking for a narrower one than the storage)
      */
-    public static final MethodHandle[] CONVERT_OBJECT_OPTIMISTIC = {
+    public static final List<MethodHandle> CONVERT_OBJECT_OPTIMISTIC = toUnmodifiableList(
         JSType.TO_INT32_OPTIMISTIC.methodHandle(),
         JSType.TO_LONG_OPTIMISTIC.methodHandle(),
         JSType.TO_NUMBER_OPTIMISTIC.methodHandle(),
         null
-    };
+    );
 
     /** The value of Undefined cast to an int32 */
     public static final int    UNDEFINED_INT    = 0;
@@ -249,12 +249,12 @@
      * Method handles for getters that return undefined coerced
      * to the appropriate type
      */
-    public static final MethodHandle[] GET_UNDEFINED = new MethodHandle[] {
+    public static final List<MethodHandle> GET_UNDEFINED = toUnmodifiableList(
         MH.constant(int.class, UNDEFINED_INT),
         MH.constant(long.class, UNDEFINED_LONG),
         MH.constant(double.class, UNDEFINED_DOUBLE),
-        MH.constant(Object.class, Undefined.getUndefined()),
-    };
+        MH.constant(Object.class, Undefined.getUndefined())
+    );
 
     private static final double INT32_LIMIT = 4294967296.0;
 
@@ -1820,5 +1820,7 @@
         }
     }
 
-
+    private static final List<MethodHandle> toUnmodifiableList(final MethodHandle... methodHandles) {
+        return Collections.unmodifiableList(Arrays.asList(methodHandles));
+    }
 }
--- a/nashorn/src/jdk/nashorn/internal/runtime/UserAccessorProperty.java	Wed Jul 02 18:10:31 2014 +0530
+++ b/nashorn/src/jdk/nashorn/internal/runtime/UserAccessorProperty.java	Thu Jul 03 11:18:10 2014 +0200
@@ -246,7 +246,7 @@
                             MH.filterReturnValue(
                                     getter,
                                     MH.insertArguments(
-                                            CONVERT_OBJECT_OPTIMISTIC[getAccessorTypeIndex(type)],
+                                            CONVERT_OBJECT_OPTIMISTIC.get(getAccessorTypeIndex(type)),
                                             1,
                                             programPoint)),
                                     getter.type().changeReturnType(type));