src/jdk/nashorn/internal/objects/NativeArray.java

Print this page




  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.objects;
  27 
  28 import static jdk.nashorn.internal.runtime.ECMAErrors.rangeError;
  29 import static jdk.nashorn.internal.runtime.ECMAErrors.typeError;
  30 import static jdk.nashorn.internal.runtime.PropertyDescriptor.VALUE;
  31 import static jdk.nashorn.internal.runtime.PropertyDescriptor.WRITABLE;
  32 import static jdk.nashorn.internal.runtime.arrays.ArrayLikeIterator.arrayLikeIterator;
  33 import static jdk.nashorn.internal.runtime.arrays.ArrayLikeIterator.reverseArrayLikeIterator;

  34 
  35 import java.lang.invoke.MethodHandle;
  36 import java.util.ArrayList;
  37 import java.util.Arrays;
  38 import java.util.Collections;
  39 import java.util.Comparator;
  40 import java.util.Iterator;
  41 import java.util.List;
  42 import java.util.concurrent.Callable;
  43 
  44 import jdk.nashorn.api.scripting.JSObject;
  45 import jdk.nashorn.internal.objects.annotations.Attribute;
  46 import jdk.nashorn.internal.objects.annotations.Constructor;
  47 import jdk.nashorn.internal.objects.annotations.Function;
  48 import jdk.nashorn.internal.objects.annotations.Getter;
  49 import jdk.nashorn.internal.objects.annotations.ScriptClass;
  50 import jdk.nashorn.internal.objects.annotations.Setter;
  51 import jdk.nashorn.internal.objects.annotations.SpecializedConstructor;
  52 import jdk.nashorn.internal.objects.annotations.Where;
  53 import jdk.nashorn.internal.runtime.JSType;


 330             // Step 4d
 331             if (!succeeded) {
 332                 if (reject) {
 333                     throw typeError("cant.redefine.property", key, ScriptRuntime.safeToString(this));
 334                 }
 335                 return false;
 336             }
 337 
 338             // Step 4e -- adjust new length based on new element index that is set
 339             if (longIndex >= oldLen) {
 340                 oldLenDesc.setValue(longIndex + 1);
 341                 super.defineOwnProperty("length", oldLenDesc, false);
 342             }
 343 
 344             // Step 4f
 345             return true;
 346         }
 347 
 348         // not an index property
 349         return super.defineOwnProperty(key, desc, reject);





















 350     }
 351 
 352     /**
 353      * Return the array contents upcasted as an ObjectArray, regardless of
 354      * representation
 355      *
 356      * @return an object array
 357      */
 358     public Object[] asObjectArray() {
 359         return getArray().asObjectArray();
 360     }
 361 
 362     /**
 363      * ECMA 15.4.3.2 Array.isArray ( arg )
 364      *
 365      * @param self self reference
 366      * @param arg  argument - object to check
 367      * @return true if argument is an array
 368      */
 369     @Function(attributes = Attribute.NOT_ENUMERABLE, where = Where.CONSTRUCTOR)




  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.objects;
  27 
  28 import static jdk.nashorn.internal.runtime.ECMAErrors.rangeError;
  29 import static jdk.nashorn.internal.runtime.ECMAErrors.typeError;
  30 import static jdk.nashorn.internal.runtime.PropertyDescriptor.VALUE;
  31 import static jdk.nashorn.internal.runtime.PropertyDescriptor.WRITABLE;
  32 import static jdk.nashorn.internal.runtime.arrays.ArrayLikeIterator.arrayLikeIterator;
  33 import static jdk.nashorn.internal.runtime.arrays.ArrayLikeIterator.reverseArrayLikeIterator;
  34 import static jdk.nashorn.internal.runtime.arrays.ArrayIndex.isValidArrayIndex;
  35 
  36 import java.lang.invoke.MethodHandle;
  37 import java.util.ArrayList;
  38 import java.util.Arrays;
  39 import java.util.Collections;
  40 import java.util.Comparator;
  41 import java.util.Iterator;
  42 import java.util.List;
  43 import java.util.concurrent.Callable;
  44 
  45 import jdk.nashorn.api.scripting.JSObject;
  46 import jdk.nashorn.internal.objects.annotations.Attribute;
  47 import jdk.nashorn.internal.objects.annotations.Constructor;
  48 import jdk.nashorn.internal.objects.annotations.Function;
  49 import jdk.nashorn.internal.objects.annotations.Getter;
  50 import jdk.nashorn.internal.objects.annotations.ScriptClass;
  51 import jdk.nashorn.internal.objects.annotations.Setter;
  52 import jdk.nashorn.internal.objects.annotations.SpecializedConstructor;
  53 import jdk.nashorn.internal.objects.annotations.Where;
  54 import jdk.nashorn.internal.runtime.JSType;


 331             // Step 4d
 332             if (!succeeded) {
 333                 if (reject) {
 334                     throw typeError("cant.redefine.property", key, ScriptRuntime.safeToString(this));
 335                 }
 336                 return false;
 337             }
 338 
 339             // Step 4e -- adjust new length based on new element index that is set
 340             if (longIndex >= oldLen) {
 341                 oldLenDesc.setValue(longIndex + 1);
 342                 super.defineOwnProperty("length", oldLenDesc, false);
 343             }
 344 
 345             // Step 4f
 346             return true;
 347         }
 348 
 349         // not an index property
 350         return super.defineOwnProperty(key, desc, reject);
 351     }
 352 
 353     /**
 354      * Spec. mentions use of [[DefineOwnProperty]] for indexed properties in
 355      * certain places (eg. Array.prototype.map, filter). We can not use ScriptObject.set
 356      * method in such cases. This is because set method uses inherited setters (if any)
 357      * from any object in proto chain such as Array.prototype, Object.prototype.
 358      * This method directly sets a particular element value in the current object.
 359      *
 360      * @param index key for property
 361      * @param value value to define
 362      */
 363     @Override
 364     public final void defineOwnProperty(final int index, final Object value) {
 365         assert isValidArrayIndex(index) : "invalid array index";
 366         final long longIndex = ArrayIndex.toLongIndex(index);
 367         if (longIndex >= getArray().length()) {
 368             // make array big enough to hold..
 369             setArray(getArray().ensure(longIndex));
 370         }
 371         setArray(getArray().set(index, value, false));
 372     }
 373 
 374     /**
 375      * Return the array contents upcasted as an ObjectArray, regardless of
 376      * representation
 377      *
 378      * @return an object array
 379      */
 380     public Object[] asObjectArray() {
 381         return getArray().asObjectArray();
 382     }
 383 
 384     /**
 385      * ECMA 15.4.3.2 Array.isArray ( arg )
 386      *
 387      * @param self self reference
 388      * @param arg  argument - object to check
 389      * @return true if argument is an array
 390      */
 391     @Function(attributes = Attribute.NOT_ENUMERABLE, where = Where.CONSTRUCTOR)