ArrayIterator.java revision 1626:d99fa86747ee
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
26package jdk.nashorn.internal.objects;
27
28import jdk.nashorn.internal.objects.annotations.Function;
29import jdk.nashorn.internal.objects.annotations.ScriptClass;
30import jdk.nashorn.internal.runtime.JSType;
31import jdk.nashorn.internal.runtime.PropertyMap;
32import jdk.nashorn.internal.runtime.ScriptObject;
33import jdk.nashorn.internal.runtime.ScriptRuntime;
34import jdk.nashorn.internal.runtime.Undefined;
35
36import static jdk.nashorn.internal.runtime.ECMAErrors.typeError;
37
38@ScriptClass("ArrayIterator")
39public 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    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    /**
58     * 22.1.5.2.1 %ArrayIteratorPrototype%.next()
59     *
60     * @param self the self reference
61     * @param arg the argument
62     * @return the next result
63     */
64    @Function
65    public static Object next(final Object self, final Object arg) {
66        if (!(self instanceof ArrayIterator)) {
67            throw typeError("not.a.array.iterator", ScriptRuntime.safeToString(self));
68        }
69        return ((ArrayIterator)self).next(arg);
70    }
71
72    @Override
73    public String getClassName() {
74        return "Array Iterator";
75    }
76
77    @Override
78    protected IteratorResult next(final Object arg) {
79        final long index = nextIndex;
80
81        if (iteratedObject == null || index >= JSType.toUint32(iteratedObject.getLength())) {
82            // ES6 22.1.5.2.1 step 10
83            iteratedObject = null;
84            return makeResult(Undefined.getUndefined(), Boolean.TRUE, global);
85        }
86
87        nextIndex++;
88
89        if (iterationKind == IterationKind.KEY_VALUE) {
90            final NativeArray value = new NativeArray(
91                    new Object[] {JSType.toNarrowestNumber(index), iteratedObject.get((double) index)});
92            return makeResult(value, Boolean.FALSE, global);
93        }
94
95        final Object value = iterationKind == IterationKind.KEY ?
96                JSType.toNarrowestNumber(index) : iteratedObject.get((double) index);
97        return makeResult(value, Boolean.FALSE, global);
98    }
99
100}
101