|
| 1 | +/* -*- Mode: java; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- |
| 2 | + * |
| 3 | + * This Source Code Form is subject to the terms of the Mozilla Public |
| 4 | + * License, v. 2.0. If a copy of the MPL was not distributed with this |
| 5 | + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
| 6 | + |
| 7 | +package org.mozilla.javascript; |
| 8 | + |
| 9 | +import java.util.Iterator; |
| 10 | + |
| 11 | +/** |
| 12 | + * This class implements ArrayIterator and StringIterator objects. See |
| 13 | + * http://wiki.ecmascript.org/doku.php?id=harmony:iterators |
| 14 | + */ |
| 15 | +public final class NativeElementIterator extends IdScriptableObject { |
| 16 | + private static final long serialVersionUID = 1L; |
| 17 | + private static final Object ELEMENT_ITERATOR_TAG = "ElementIterator"; |
| 18 | + |
| 19 | + static void init(ScriptableObject scope, boolean sealed) { |
| 20 | + // NativeElementIterator |
| 21 | + // Can't use "NativeElementIterator().exportAsJSClass" since we don't want |
| 22 | + // to define "NativeElementIterator" as a constructor in the top-level scope. |
| 23 | + NativeElementIterator prototype = new NativeElementIterator(); |
| 24 | + if (scope != null) { |
| 25 | + prototype.setParentScope(scope); |
| 26 | + prototype.setPrototype(getObjectPrototype(scope)); |
| 27 | + } |
| 28 | + prototype.activatePrototypeMap(MAX_PROTOTYPE_ID); |
| 29 | + if (sealed) { |
| 30 | + prototype.sealObject(); |
| 31 | + } |
| 32 | + |
| 33 | + // Need to access Iterator prototype when constructing |
| 34 | + // Iterator instances, but don't have a iterator constructor |
| 35 | + // to use to find the prototype. Use the "associateValue" |
| 36 | + // approach instead. |
| 37 | + if (scope != null) { |
| 38 | + scope.associateValue(ELEMENT_ITERATOR_TAG, prototype); |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + /** |
| 43 | + * Only for constructing the prototype object. |
| 44 | + */ |
| 45 | + private NativeElementIterator() { |
| 46 | + } |
| 47 | + |
| 48 | + NativeElementIterator(Scriptable scope, Scriptable arrayLike) { |
| 49 | + this.arrayLike = arrayLike; |
| 50 | + this.index = 0; |
| 51 | + // Set parent and prototype properties. Since we don't have a |
| 52 | + // "Iterator" constructor in the top scope, we stash the |
| 53 | + // prototype in the top scope's associated value. |
| 54 | + Scriptable top = ScriptableObject.getTopLevelScope(scope); |
| 55 | + this.setParentScope(top); |
| 56 | + NativeElementIterator prototype = (NativeElementIterator) |
| 57 | + ScriptableObject.getTopScopeValue(top, ELEMENT_ITERATOR_TAG); |
| 58 | + setPrototype(prototype); |
| 59 | + } |
| 60 | + |
| 61 | + @Override |
| 62 | + public String getClassName() { |
| 63 | + return "Iterator"; |
| 64 | + } |
| 65 | + |
| 66 | + @Override |
| 67 | + protected void initPrototypeId(int id) |
| 68 | + { |
| 69 | + String s; |
| 70 | + int arity; |
| 71 | + switch (id) { |
| 72 | + case Id_next: arity=0; s="next"; break; |
| 73 | + default: throw new IllegalArgumentException(String.valueOf(id)); |
| 74 | + } |
| 75 | + initPrototypeMethod(ELEMENT_ITERATOR_TAG, id, s, arity); |
| 76 | + } |
| 77 | + |
| 78 | + @Override |
| 79 | + public Object execIdCall(IdFunctionObject f, Context cx, Scriptable scope, |
| 80 | + Scriptable thisObj, Object[] args) |
| 81 | + { |
| 82 | + if (!f.hasTag(ELEMENT_ITERATOR_TAG)) { |
| 83 | + return super.execIdCall(f, cx, scope, thisObj, args); |
| 84 | + } |
| 85 | + int id = f.methodId(); |
| 86 | + |
| 87 | + if (!(thisObj instanceof NativeElementIterator)) |
| 88 | + throw incompatibleCallError(f); |
| 89 | + |
| 90 | + NativeElementIterator iterator = (NativeElementIterator) thisObj; |
| 91 | + |
| 92 | + switch (id) { |
| 93 | + case Id_next: |
| 94 | + return iterator.next(cx, scope); |
| 95 | + default: |
| 96 | + throw new IllegalArgumentException(String.valueOf(id)); |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + @Override |
| 101 | + protected int findPrototypeId(String s) |
| 102 | + { |
| 103 | + if (s.equals("next")) { |
| 104 | + return Id_next; |
| 105 | + } |
| 106 | + return 0; |
| 107 | + } |
| 108 | + |
| 109 | + private Object next(Context cx, Scriptable scope) { |
| 110 | + if (index >= NativeArray.getLengthProperty(cx, arrayLike)) { |
| 111 | + // Out of values. Throw StopIteration. |
| 112 | + throw new JavaScriptException( |
| 113 | + NativeIterator.getStopIterationObject(scope), null, 0); |
| 114 | + } |
| 115 | + return arrayLike.get(index++, arrayLike); |
| 116 | + } |
| 117 | + |
| 118 | + public static final String ITERATOR_FUNCTION_NAME = "iterator"; |
| 119 | + |
| 120 | + private Scriptable arrayLike; |
| 121 | + private int index; |
| 122 | + |
| 123 | + private static final int |
| 124 | + Id_next = 1, |
| 125 | + MAX_PROTOTYPE_ID = 1; |
| 126 | +} |
0 commit comments