1 /*
   2  * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  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.objects;
  27 
  28 import jdk.nashorn.internal.objects.annotations.Function;
  29 import jdk.nashorn.internal.objects.annotations.ScriptClass;
  30 import jdk.nashorn.internal.runtime.JSType;
  31 import jdk.nashorn.internal.runtime.PropertyMap;
  32 import jdk.nashorn.internal.runtime.ScriptObject;
  33 import jdk.nashorn.internal.runtime.ScriptRuntime;
  34 import jdk.nashorn.internal.runtime.Undefined;
  35 
  36 import static jdk.nashorn.internal.runtime.ECMAErrors.typeError;
  37 
  38 @ScriptClass("ArrayIterator")
  39 public class ArrayIterator extends AbstractIterator {
  40 
  41     // initialized by nasgen
  42     private static PropertyMap $nasgenmap$;
  43 
  44     private ScriptObject iteratedObject;
  45     private long nextIndex = 0L;
  46     private final IterationKind iterationKind;
  47     private final Global global;
  48 
  49 
  50     private ArrayIterator(final Object iteratedObject, final IterationKind iterationKind, final Global global) {
  51         super(global.getArrayIteratorPrototype(), $nasgenmap$);
  52         this.iteratedObject = iteratedObject instanceof ScriptObject ? (ScriptObject) iteratedObject : null;
  53         this.iterationKind = iterationKind;
  54         this.global = global;
  55     }
  56 
  57     static ArrayIterator newArrayValueIterator(final Object iteratedObject) {
  58         return new ArrayIterator(Global.toObject(iteratedObject), IterationKind.VALUE, Global.instance());
  59     }
  60 
  61     static ArrayIterator newArrayKeyIterator(final Object iteratedObject) {
  62         return new ArrayIterator(Global.toObject(iteratedObject), IterationKind.KEY, Global.instance());
  63     }
  64 
  65     static ArrayIterator newArrayKeyValueIterator(final Object iteratedObject) {
  66         return new ArrayIterator(Global.toObject(iteratedObject), IterationKind.KEY_VALUE, Global.instance());
  67     }
  68 
  69     /**
  70      * 22.1.5.2.1 %ArrayIteratorPrototype%.next()
  71      *
  72      * @param self the self reference
  73      * @param arg the argument
  74      * @return the next result
  75      */
  76     @Function
  77     public static Object next(final Object self, final Object arg) {
  78         if (!(self instanceof ArrayIterator)) {
  79             throw typeError("not.a.array.iterator", ScriptRuntime.safeToString(self));
  80         }
  81         return ((ArrayIterator)self).next(arg);
  82     }
  83 
  84     @Override
  85     public String getClassName() {
  86         return "Array Iterator";
  87     }
  88 
  89     @Override
  90     protected IteratorResult next(final Object arg) {
  91         final long index = nextIndex;
  92 
  93         if (iteratedObject == null || index >= JSType.toUint32(iteratedObject.getLength())) {
  94             // ES6 22.1.5.2.1 step 10
  95             iteratedObject = null;
  96             return makeResult(Undefined.getUndefined(), Boolean.TRUE, global);
  97         }
  98 
  99         nextIndex++;
 100 
 101         if (iterationKind == IterationKind.KEY_VALUE) {
 102             final NativeArray value = new NativeArray(
 103                     new Object[] {JSType.toNarrowestNumber(index), iteratedObject.get((double) index)});
 104             return makeResult(value, Boolean.FALSE, global);
 105         }
 106 
 107         final Object value = iterationKind == IterationKind.KEY ?
 108                 JSType.toNarrowestNumber(index) : iteratedObject.get((double) index);
 109         return makeResult(value, Boolean.FALSE, global);
 110     }
 111 
 112 }