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.PropertyMap; 31 import jdk.nashorn.internal.runtime.ScriptRuntime; 32 import jdk.nashorn.internal.runtime.Undefined; 33 34 import static jdk.nashorn.internal.runtime.ECMAErrors.typeError; 35 36 /** 37 * ECMA6 23.2.5 Set Iterator Objects 38 */ 39 @ScriptClass("SetIterator") 40 public class SetIterator extends AbstractIterator { 41 42 // initialized by nasgen 43 private static PropertyMap $nasgenmap$; 44 45 private LinkedMap.LinkedMapIterator iterator; 46 47 private final IterationKind iterationKind; 48 49 // Cached global needed for every iteration result 50 private final Global global; 51 52 SetIterator(final NativeSet set, final IterationKind iterationKind, final Global global) { 53 super(global.getSetIteratorPrototype(), $nasgenmap$); 54 this.iterator = set.getJavaMap().getIterator(); 55 this.iterationKind = iterationKind; 56 this.global = global; 57 } 58 59 /** 60 * ES6 23.2.5.2.1 %SetIteratorPrototype%.next() 61 * 62 * @param self the self reference 63 * @param arg the argument 64 * @return the next result 65 */ 66 @Function 67 public static Object next(final Object self, final Object arg) { 68 if (!(self instanceof SetIterator)) { 69 throw typeError("not.a.set.iterator", ScriptRuntime.safeToString(self)); 70 } 71 return ((SetIterator)self).next(arg); 72 } 73 74 @Override 75 public String getClassName() { 76 return "Set Iterator"; 77 } 78 79 @Override 80 protected IteratorResult next(final Object arg) { 81 if (iterator == null) { 82 return makeResult(Undefined.getUndefined(), Boolean.TRUE, global); 83 } 84 85 final LinkedMap.Node node = iterator.next(); 86 87 if (node == null) { 88 iterator = null; 89 return makeResult(Undefined.getUndefined(), Boolean.TRUE, global); 90 } 91 92 if (iterationKind == IterationKind.KEY_VALUE) { 93 final NativeArray array = new NativeArray(new Object[] {node.getKey(), node.getKey()}); 94 return makeResult(array, Boolean.FALSE, global); 95 } 96 97 return makeResult(node.getKey(), Boolean.FALSE, global); 98 } 99 }