|
| 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 | +/** |
| 10 | + * Wrapper for iterators returned by Iterator.from(). This class wraps any iterator and ensures it |
| 11 | + * inherits from Iterator.prototype while delegating all operations to the wrapped iterator. |
| 12 | + */ |
| 13 | +public final class ES6IteratorAdapter extends ScriptableObject { |
| 14 | + private static final long serialVersionUID = 1L; |
| 15 | + |
| 16 | + private Scriptable wrappedIterator; |
| 17 | + |
| 18 | + public ES6IteratorAdapter(Scriptable wrappedIterator, Scriptable scope) { |
| 19 | + this.wrappedIterator = wrappedIterator; |
| 20 | + |
| 21 | + // Set parent scope |
| 22 | + setParentScope(scope); |
| 23 | + |
| 24 | + // Set prototype to inherit from Iterator.prototype if available |
| 25 | + // Note: Iterator.prototype setup would be handled by NativeIteratorConstructor when |
| 26 | + // available |
| 27 | + } |
| 28 | + |
| 29 | + @Override |
| 30 | + public String getClassName() { |
| 31 | + return "Iterator"; |
| 32 | + } |
| 33 | + |
| 34 | + @Override |
| 35 | + public Object get(String name, Scriptable start) { |
| 36 | + // Special handling for 'next' method - create a bound version |
| 37 | + if ("next".equals(name) && wrappedIterator != null) { |
| 38 | + Object nextMethod = ScriptableObject.getProperty(wrappedIterator, "next"); |
| 39 | + if (nextMethod instanceof Callable) { |
| 40 | + // Return a function that calls next() with the wrapped iterator as 'this' |
| 41 | + return new BaseFunction() { |
| 42 | + @Override |
| 43 | + public Object call( |
| 44 | + Context cx, Scriptable scope, Scriptable thisObj, Object[] args) { |
| 45 | + return ((Callable) nextMethod).call(cx, scope, wrappedIterator, args); |
| 46 | + } |
| 47 | + |
| 48 | + @Override |
| 49 | + public String getFunctionName() { |
| 50 | + return "next"; |
| 51 | + } |
| 52 | + }; |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + // Special handling for 'return' method |
| 57 | + if ("return".equals(name) && wrappedIterator != null) { |
| 58 | + Object returnMethod = ScriptableObject.getProperty(wrappedIterator, "return"); |
| 59 | + if (returnMethod instanceof Callable) { |
| 60 | + return new BaseFunction() { |
| 61 | + @Override |
| 62 | + public Object call( |
| 63 | + Context cx, Scriptable scope, Scriptable thisObj, Object[] args) { |
| 64 | + return ((Callable) returnMethod).call(cx, scope, wrappedIterator, args); |
| 65 | + } |
| 66 | + |
| 67 | + @Override |
| 68 | + public String getFunctionName() { |
| 69 | + return "return"; |
| 70 | + } |
| 71 | + }; |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + // Delegate other properties to wrapped iterator |
| 76 | + if (wrappedIterator != null) { |
| 77 | + Object value = ScriptableObject.getProperty(wrappedIterator, name); |
| 78 | + if (value != Scriptable.NOT_FOUND) { |
| 79 | + return value; |
| 80 | + } |
| 81 | + } |
| 82 | + // Fall back to our prototype chain |
| 83 | + return super.get(name, start); |
| 84 | + } |
| 85 | + |
| 86 | + @Override |
| 87 | + public Object get(int index, Scriptable start) { |
| 88 | + if (wrappedIterator != null) { |
| 89 | + Object value = wrappedIterator.get(index, wrappedIterator); |
| 90 | + if (value != Scriptable.NOT_FOUND) { |
| 91 | + return value; |
| 92 | + } |
| 93 | + } |
| 94 | + return super.get(index, start); |
| 95 | + } |
| 96 | + |
| 97 | + @Override |
| 98 | + public boolean has(String name, Scriptable start) { |
| 99 | + if (wrappedIterator != null) { |
| 100 | + // Check if property exists in wrapped iterator or its prototype chain |
| 101 | + Object value = ScriptableObject.getProperty(wrappedIterator, name); |
| 102 | + if (value != Scriptable.NOT_FOUND) { |
| 103 | + return true; |
| 104 | + } |
| 105 | + } |
| 106 | + return super.has(name, start); |
| 107 | + } |
| 108 | + |
| 109 | + @Override |
| 110 | + public boolean has(int index, Scriptable start) { |
| 111 | + if (wrappedIterator != null && wrappedIterator.has(index, wrappedIterator)) { |
| 112 | + return true; |
| 113 | + } |
| 114 | + return super.has(index, start); |
| 115 | + } |
| 116 | + |
| 117 | + @Override |
| 118 | + public void put(String name, Scriptable start, Object value) { |
| 119 | + if (wrappedIterator != null) { |
| 120 | + wrappedIterator.put(name, wrappedIterator, value); |
| 121 | + } else { |
| 122 | + super.put(name, start, value); |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + @Override |
| 127 | + public void put(int index, Scriptable start, Object value) { |
| 128 | + if (wrappedIterator != null) { |
| 129 | + wrappedIterator.put(index, wrappedIterator, value); |
| 130 | + } else { |
| 131 | + super.put(index, start, value); |
| 132 | + } |
| 133 | + } |
| 134 | + |
| 135 | + @Override |
| 136 | + public void delete(String name) { |
| 137 | + if (wrappedIterator != null) { |
| 138 | + wrappedIterator.delete(name); |
| 139 | + } else { |
| 140 | + super.delete(name); |
| 141 | + } |
| 142 | + } |
| 143 | + |
| 144 | + @Override |
| 145 | + public void delete(int index) { |
| 146 | + if (wrappedIterator != null) { |
| 147 | + wrappedIterator.delete(index); |
| 148 | + } else { |
| 149 | + super.delete(index); |
| 150 | + } |
| 151 | + } |
| 152 | + |
| 153 | + @Override |
| 154 | + public Object[] getIds() { |
| 155 | + if (wrappedIterator != null) { |
| 156 | + return wrappedIterator.getIds(); |
| 157 | + } |
| 158 | + return super.getIds(); |
| 159 | + } |
| 160 | +} |
0 commit comments