/**
 * Copyright (c) 2012-2013 André Bargull
 * Alle Rechte vorbehalten / All Rights Reserved.  Use is subject to license terms.
 *
 * <https://github.com/anba/es6draft>
 */
package com.github.anba.es6draft.runtime.objects.internal;

import static com.github.anba.es6draft.runtime.objects.iteration.IterationAbstractOperations.IteratorComplete;
import static com.github.anba.es6draft.runtime.objects.iteration.IterationAbstractOperations.IteratorNext;
import static com.github.anba.es6draft.runtime.objects.iteration.IterationAbstractOperations.IteratorValue;

import java.util.Iterator;

import com.github.anba.es6draft.runtime.ExecutionContext;
import com.github.anba.es6draft.runtime.Realm;
import com.github.anba.es6draft.runtime.internal.SimpleIterator;
import com.github.anba.es6draft.runtime.types.Intrinsics;
import com.github.anba.es6draft.runtime.types.ScriptObject;
import com.github.anba.es6draft.runtime.types.builtins.OrdinaryObject;

/**
 *
 */
public class ListIterator<T> extends OrdinaryObject {
    private Iterator<T> iterator;

    public ListIterator(Realm realm, Iterator<T> iterator) {
        super(realm);
        this.iterator = iterator;
    }

    public Iterator<T> getIterator() {
        return iterator;
    }

    public static <T> ListIterator<T> MakeListIterator(ExecutionContext cx, Iterator<T> iterator) {
        ListIterator<T> itr = new ListIterator<>(cx.getRealm(), iterator);
        itr.setPrototype(cx.getIntrinsic(Intrinsics.ListIteratorPrototype));
        itr.preventExtensions(cx);
        return itr;
    }

    public static Iterator<?> FromListIterator(ExecutionContext cx, ScriptObject obj) {
        if (obj instanceof ListIterator) {
            return ((ListIterator<?>) obj).iterator;
        }
        return new IteratorWrapper(cx, obj);
    }

    private static class IteratorWrapper extends SimpleIterator<Object> {
        private ExecutionContext cx;
        private ScriptObject object;

        IteratorWrapper(ExecutionContext cx, ScriptObject object) {
            this.cx = cx;
            this.object = object;
        }

        @Override
        protected Object tryNext() {
            ScriptObject next = IteratorNext(cx, object);
            boolean done = IteratorComplete(cx, next);
            if (done) {
                return null;
            }
            return IteratorValue(cx, next);
        }
    }
}

@@ -948,17 +948,58 @@
 %0A%0A/**%0A *
+ Object to iterate over an internal list.
 %0A
-
  */%0Apubl
@@ -1270,32 +1270,136 @@
 terator;%0A    %7D%0A%0A
+    /**%0A     * Returns a new %7B@link ListIterator%7D object for the internal list %7B@code iterator%7D%0A     */%0A
     public stati
@@ -1688,32 +1688,178 @@
 urn itr;%0A    %7D%0A%0A
+    /**%0A     * Returns an iterator for %7B@code obj%7D which is expected to comply to the%0A     * %3Ccode%3E%2225.1.2 The Iterator Interface%22%3C/code%3E%0A     */%0A
     public stati
@@ -2016,24 +2016,29 @@
 %3E) obj).
-i
+getI
 terator
+()
 ;%0A      

Add comments