|
| 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 | + * Adapter that wraps any iterator to inherit from Iterator.prototype. Used by Iterator.from() to |
| 11 | + * ensure returned iterators have proper prototype chain. All operations are delegated to the |
| 12 | + * wrapped iterator. |
| 13 | + */ |
| 14 | +public class ES6IteratorAdapter extends ScriptableObject { |
| 15 | + |
| 16 | + private static final long serialVersionUID = 1L; |
| 17 | + |
| 18 | + private final Scriptable wrappedIterator; |
| 19 | + private final Callable nextMethod; |
| 20 | + private Callable returnMethod; |
| 21 | + private Callable throwMethod; |
| 22 | + |
| 23 | + /** |
| 24 | + * Creates an adapter that wraps an iterator to inherit from Iterator.prototype. |
| 25 | + * |
| 26 | + * @param cx Current context |
| 27 | + * @param scope Current scope |
| 28 | + * @param iterator The iterator to wrap |
| 29 | + */ |
| 30 | + public ES6IteratorAdapter(Context cx, Scriptable scope, Object iterator) { |
| 31 | + if (!(iterator instanceof Scriptable)) { |
| 32 | + throw ScriptRuntime.typeError("Iterator must be an object"); |
| 33 | + } |
| 34 | + |
| 35 | + this.wrappedIterator = (Scriptable) iterator; |
| 36 | + |
| 37 | + // Get the next method (required) |
| 38 | + Object next = ScriptableObject.getProperty(wrappedIterator, ES6Iterator.NEXT_METHOD); |
| 39 | + if (!(next instanceof Callable)) { |
| 40 | + throw ScriptRuntime.typeError("Iterator missing next method"); |
| 41 | + } |
| 42 | + this.nextMethod = (Callable) next; |
| 43 | + |
| 44 | + // Get optional return method |
| 45 | + Object ret = ScriptableObject.getProperty(wrappedIterator, ES6Iterator.RETURN_METHOD); |
| 46 | + if (ret instanceof Callable) { |
| 47 | + this.returnMethod = (Callable) ret; |
| 48 | + } |
| 49 | + |
| 50 | + // Get optional throw method |
| 51 | + Object thr = ScriptableObject.getProperty(wrappedIterator, "throw"); |
| 52 | + if (thr instanceof Callable) { |
| 53 | + this.throwMethod = (Callable) thr; |
| 54 | + } |
| 55 | + |
| 56 | + // Set up prototype chain to inherit from Iterator.prototype |
| 57 | + setupPrototype(cx, scope); |
| 58 | + } |
| 59 | + |
| 60 | + private void setupPrototype(Context cx, Scriptable scope) { |
| 61 | + Scriptable topScope = ScriptableObject.getTopLevelScope(scope); |
| 62 | + |
| 63 | + // Try to find Iterator.prototype |
| 64 | + Object iteratorCtor = ScriptableObject.getProperty(topScope, "Iterator"); |
| 65 | + if (iteratorCtor instanceof Scriptable) { |
| 66 | + Object proto = ScriptableObject.getProperty((Scriptable) iteratorCtor, "prototype"); |
| 67 | + if (proto instanceof Scriptable) { |
| 68 | + this.setPrototype((Scriptable) proto); |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + this.setParentScope(scope); |
| 73 | + } |
| 74 | + |
| 75 | + @Override |
| 76 | + public String getClassName() { |
| 77 | + return "Iterator"; |
| 78 | + } |
| 79 | + |
| 80 | + @Override |
| 81 | + public Object get(String name, Scriptable start) { |
| 82 | + // First check if we have the property |
| 83 | + Object result = super.get(name, start); |
| 84 | + if (result != NOT_FOUND) { |
| 85 | + return result; |
| 86 | + } |
| 87 | + |
| 88 | + // Delegate property access to wrapped iterator |
| 89 | + return ScriptableObject.getProperty(wrappedIterator, name); |
| 90 | + } |
| 91 | + |
| 92 | + @Override |
| 93 | + public Object get(Symbol key, Scriptable start) { |
| 94 | + // First check if we have the property |
| 95 | + Object result = super.get(key, start); |
| 96 | + if (result != NOT_FOUND) { |
| 97 | + return result; |
| 98 | + } |
| 99 | + |
| 100 | + // Delegate symbol property access to wrapped iterator |
| 101 | + if (wrappedIterator instanceof SymbolScriptable) { |
| 102 | + return ((SymbolScriptable) wrappedIterator).get(key, start); |
| 103 | + } |
| 104 | + return NOT_FOUND; |
| 105 | + } |
| 106 | + |
| 107 | + @Override |
| 108 | + public boolean has(String name, Scriptable start) { |
| 109 | + // Check both our properties and wrapped iterator's properties |
| 110 | + return super.has(name, start) || ScriptableObject.hasProperty(wrappedIterator, name); |
| 111 | + } |
| 112 | + |
| 113 | + @Override |
| 114 | + public boolean has(Symbol key, Scriptable start) { |
| 115 | + // Check both our properties and wrapped iterator's properties |
| 116 | + if (super.has(key, start)) { |
| 117 | + return true; |
| 118 | + } |
| 119 | + |
| 120 | + if (wrappedIterator instanceof SymbolScriptable) { |
| 121 | + return ((SymbolScriptable) wrappedIterator).has(key, start); |
| 122 | + } |
| 123 | + return false; |
| 124 | + } |
| 125 | + |
| 126 | + /** |
| 127 | + * Calls the next() method on the wrapped iterator. |
| 128 | + * |
| 129 | + * @param cx Current context |
| 130 | + * @param scope Current scope |
| 131 | + * @return Iterator result object |
| 132 | + */ |
| 133 | + public Object next(Context cx, Scriptable scope) { |
| 134 | + return nextMethod.call(cx, scope, wrappedIterator, ScriptRuntime.emptyArgs); |
| 135 | + } |
| 136 | + |
| 137 | + /** |
| 138 | + * Calls the return() method on the wrapped iterator if it exists. |
| 139 | + * |
| 140 | + * @param cx Current context |
| 141 | + * @param scope Current scope |
| 142 | + * @param value Return value |
| 143 | + * @return Iterator result object |
| 144 | + */ |
| 145 | + public Object doReturn(Context cx, Scriptable scope, Object value) { |
| 146 | + if (returnMethod != null) { |
| 147 | + return returnMethod.call(cx, scope, wrappedIterator, new Object[] {value}); |
| 148 | + } |
| 149 | + |
| 150 | + // Default behavior if no return method |
| 151 | + return IteratorOperations.makeIteratorResult(cx, scope, true, value); |
| 152 | + } |
| 153 | + |
| 154 | + /** |
| 155 | + * Calls the throw() method on the wrapped iterator if it exists. |
| 156 | + * |
| 157 | + * @param cx Current context |
| 158 | + * @param scope Current scope |
| 159 | + * @param value Value to throw |
| 160 | + * @return Iterator result object (if throw method handles it) |
| 161 | + * @throws JavaScriptException if no throw method or it rethrows |
| 162 | + */ |
| 163 | + public Object doThrow(Context cx, Scriptable scope, Object value) { |
| 164 | + if (throwMethod != null) { |
| 165 | + return throwMethod.call(cx, scope, wrappedIterator, new Object[] {value}); |
| 166 | + } |
| 167 | + |
| 168 | + // Default behavior if no throw method - throw the value |
| 169 | + if (value instanceof JavaScriptException) { |
| 170 | + throw (JavaScriptException) value; |
| 171 | + } else if (value instanceof RhinoException) { |
| 172 | + throw (RhinoException) value; |
| 173 | + } |
| 174 | + throw ScriptRuntime.typeError(value.toString()); |
| 175 | + } |
| 176 | + |
| 177 | + /** |
| 178 | + * Gets the wrapped iterator object. |
| 179 | + * |
| 180 | + * @return The wrapped iterator |
| 181 | + */ |
| 182 | + public Scriptable getWrappedIterator() { |
| 183 | + return wrappedIterator; |
| 184 | + } |
| 185 | +} |
0 commit comments