author | hannesw |
Wed, 21 Mar 2018 16:55:34 +0100 | |
changeset 49275 | c639a6b33c5c |
parent 47216 | 71c04702a3d5 |
permissions | -rw-r--r-- |
16147 | 1 |
/* |
16151 | 2 |
* Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. |
16147 | 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.runtime.arrays; |
|
27 |
||
28 |
import java.util.Iterator; |
|
19469
a5d34b586cea
8023026: Array.prototype iterator functions like forEach, reduce should work for Java arrays, lists
sundar
parents:
18334
diff
changeset
|
29 |
import java.util.List; |
19889
63af9358d0dc
8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents:
19472
diff
changeset
|
30 |
import jdk.nashorn.api.scripting.JSObject; |
16147 | 31 |
import jdk.nashorn.internal.runtime.JSType; |
32 |
import jdk.nashorn.internal.runtime.ScriptObject; |
|
33 |
||
34 |
/** |
|
35 |
* Superclass for array iterators |
|
36 |
* TODO: rewrite these |
|
37 |
* |
|
38 |
* @param <T> element type |
|
39 |
*/ |
|
40 |
abstract public class ArrayLikeIterator<T> implements Iterator<T> { |
|
41 |
||
42 |
/** current element index in iteration */ |
|
17983
4b1e3d565f9b
8015350: Array.prototype.reduceRight issue with large length and index
hannesw
parents:
17523
diff
changeset
|
43 |
protected long index; |
16147 | 44 |
|
45 |
/** should undefined elements be included in the iteration? */ |
|
46 |
protected final boolean includeUndefined; |
|
47 |
||
48 |
/** |
|
49 |
* Constructor |
|
50 |
* |
|
51 |
* @param includeUndefined should undefined elements be included in the iteration? |
|
52 |
*/ |
|
19469
a5d34b586cea
8023026: Array.prototype iterator functions like forEach, reduce should work for Java arrays, lists
sundar
parents:
18334
diff
changeset
|
53 |
ArrayLikeIterator(final boolean includeUndefined) { |
16147 | 54 |
this.includeUndefined = includeUndefined; |
55 |
this.index = 0; |
|
56 |
} |
|
57 |
||
58 |
/** |
|
59 |
* Is this a reverse order iteration? |
|
60 |
* @return true if reverse |
|
61 |
*/ |
|
62 |
public boolean isReverse() { |
|
63 |
return false; |
|
64 |
} |
|
65 |
||
66 |
/** |
|
67 |
* Go the the next valid element index of the iterator |
|
68 |
* @return next index |
|
69 |
*/ |
|
17983
4b1e3d565f9b
8015350: Array.prototype.reduceRight issue with large length and index
hannesw
parents:
17523
diff
changeset
|
70 |
protected long bumpIndex() { |
16147 | 71 |
return index++; |
72 |
} |
|
73 |
||
74 |
/** |
|
75 |
* Return the next valid element index of the iterator |
|
76 |
* @return next index |
|
77 |
*/ |
|
17983
4b1e3d565f9b
8015350: Array.prototype.reduceRight issue with large length and index
hannesw
parents:
17523
diff
changeset
|
78 |
public long nextIndex() { |
16147 | 79 |
return index; |
80 |
} |
|
81 |
||
82 |
@Override |
|
83 |
public void remove() { |
|
84 |
throw new UnsupportedOperationException("remove"); |
|
85 |
} |
|
86 |
||
87 |
/** |
|
88 |
* Get the length of the iteration |
|
89 |
* @return length |
|
90 |
*/ |
|
17983
4b1e3d565f9b
8015350: Array.prototype.reduceRight issue with large length and index
hannesw
parents:
17523
diff
changeset
|
91 |
public abstract long getLength(); |
16147 | 92 |
|
93 |
/** |
|
94 |
* ArrayLikeIterator factory |
|
95 |
* |
|
96 |
* @param object object over which to do element iteration |
|
97 |
* @return iterator |
|
98 |
*/ |
|
99 |
public static ArrayLikeIterator<Object> arrayLikeIterator(final Object object) { |
|
100 |
return arrayLikeIterator(object, false); |
|
101 |
} |
|
102 |
||
103 |
/** |
|
104 |
* ArrayLikeIterator factory (reverse order) |
|
105 |
* @param object object over which to do reverse element iteration |
|
106 |
* @return iterator |
|
107 |
*/ |
|
108 |
public static ArrayLikeIterator<Object> reverseArrayLikeIterator(final Object object) { |
|
109 |
return reverseArrayLikeIterator(object, false); |
|
110 |
} |
|
111 |
||
112 |
/** |
|
113 |
* ArrayLikeIterator factory |
|
114 |
* @param object object over which to do reverse element iteration |
|
115 |
* @param includeUndefined should undefined elements be included in the iteration |
|
116 |
* @return iterator |
|
117 |
*/ |
|
118 |
public static ArrayLikeIterator<Object> arrayLikeIterator(final Object object, final boolean includeUndefined) { |
|
119 |
Object obj = object; |
|
120 |
||
121 |
if (ScriptObject.isArray(obj)) { |
|
19469
a5d34b586cea
8023026: Array.prototype iterator functions like forEach, reduce should work for Java arrays, lists
sundar
parents:
18334
diff
changeset
|
122 |
return new ScriptArrayIterator((ScriptObject) obj, includeUndefined); |
16147 | 123 |
} |
124 |
||
16188 | 125 |
obj = JSType.toScriptObject(obj); |
16147 | 126 |
if (obj instanceof ScriptObject) { |
19469
a5d34b586cea
8023026: Array.prototype iterator functions like forEach, reduce should work for Java arrays, lists
sundar
parents:
18334
diff
changeset
|
127 |
return new ScriptObjectIterator((ScriptObject)obj, includeUndefined); |
16147 | 128 |
} |
129 |
||
19889
63af9358d0dc
8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents:
19472
diff
changeset
|
130 |
if (obj instanceof JSObject) { |
63af9358d0dc
8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents:
19472
diff
changeset
|
131 |
return new JSObjectIterator((JSObject)obj, includeUndefined); |
18334
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17983
diff
changeset
|
132 |
} |
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17983
diff
changeset
|
133 |
|
19469
a5d34b586cea
8023026: Array.prototype iterator functions like forEach, reduce should work for Java arrays, lists
sundar
parents:
18334
diff
changeset
|
134 |
if (obj instanceof List) { |
19472
9476460521b3
8023017: SUB missing for widest op == number for BinaryNode
lagergren
parents:
19469
diff
changeset
|
135 |
return new JavaListIterator((List<?>)obj, includeUndefined); |
19469
a5d34b586cea
8023026: Array.prototype iterator functions like forEach, reduce should work for Java arrays, lists
sundar
parents:
18334
diff
changeset
|
136 |
} |
a5d34b586cea
8023026: Array.prototype iterator functions like forEach, reduce should work for Java arrays, lists
sundar
parents:
18334
diff
changeset
|
137 |
|
a5d34b586cea
8023026: Array.prototype iterator functions like forEach, reduce should work for Java arrays, lists
sundar
parents:
18334
diff
changeset
|
138 |
if (obj != null && obj.getClass().isArray()) { |
a5d34b586cea
8023026: Array.prototype iterator functions like forEach, reduce should work for Java arrays, lists
sundar
parents:
18334
diff
changeset
|
139 |
return new JavaArrayIterator(obj, includeUndefined); |
a5d34b586cea
8023026: Array.prototype iterator functions like forEach, reduce should work for Java arrays, lists
sundar
parents:
18334
diff
changeset
|
140 |
} |
a5d34b586cea
8023026: Array.prototype iterator functions like forEach, reduce should work for Java arrays, lists
sundar
parents:
18334
diff
changeset
|
141 |
|
16147 | 142 |
return new EmptyArrayLikeIterator(); |
143 |
} |
|
144 |
||
145 |
/** |
|
146 |
* ArrayLikeIterator factory (reverse order) |
|
147 |
* @param object object over which to do reverse element iteration |
|
148 |
* @param includeUndefined should undefined elements be included in the iteration |
|
149 |
* @return iterator |
|
150 |
*/ |
|
151 |
public static ArrayLikeIterator<Object> reverseArrayLikeIterator(final Object object, final boolean includeUndefined) { |
|
152 |
Object obj = object; |
|
153 |
||
154 |
if (ScriptObject.isArray(obj)) { |
|
19469
a5d34b586cea
8023026: Array.prototype iterator functions like forEach, reduce should work for Java arrays, lists
sundar
parents:
18334
diff
changeset
|
155 |
return new ReverseScriptArrayIterator((ScriptObject) obj, includeUndefined); |
16147 | 156 |
} |
157 |
||
16188 | 158 |
obj = JSType.toScriptObject(obj); |
16147 | 159 |
if (obj instanceof ScriptObject) { |
19469
a5d34b586cea
8023026: Array.prototype iterator functions like forEach, reduce should work for Java arrays, lists
sundar
parents:
18334
diff
changeset
|
160 |
return new ReverseScriptObjectIterator((ScriptObject)obj, includeUndefined); |
16147 | 161 |
} |
162 |
||
19889
63af9358d0dc
8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents:
19472
diff
changeset
|
163 |
if (obj instanceof JSObject) { |
63af9358d0dc
8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents:
19472
diff
changeset
|
164 |
return new ReverseJSObjectIterator((JSObject)obj, includeUndefined); |
18334
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17983
diff
changeset
|
165 |
} |
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17983
diff
changeset
|
166 |
|
19469
a5d34b586cea
8023026: Array.prototype iterator functions like forEach, reduce should work for Java arrays, lists
sundar
parents:
18334
diff
changeset
|
167 |
if (obj instanceof List) { |
19472
9476460521b3
8023017: SUB missing for widest op == number for BinaryNode
lagergren
parents:
19469
diff
changeset
|
168 |
return new ReverseJavaListIterator((List<?>)obj, includeUndefined); |
19469
a5d34b586cea
8023026: Array.prototype iterator functions like forEach, reduce should work for Java arrays, lists
sundar
parents:
18334
diff
changeset
|
169 |
} |
a5d34b586cea
8023026: Array.prototype iterator functions like forEach, reduce should work for Java arrays, lists
sundar
parents:
18334
diff
changeset
|
170 |
|
a5d34b586cea
8023026: Array.prototype iterator functions like forEach, reduce should work for Java arrays, lists
sundar
parents:
18334
diff
changeset
|
171 |
if (obj != null && obj.getClass().isArray()) { |
a5d34b586cea
8023026: Array.prototype iterator functions like forEach, reduce should work for Java arrays, lists
sundar
parents:
18334
diff
changeset
|
172 |
return new ReverseJavaArrayIterator(obj, includeUndefined); |
a5d34b586cea
8023026: Array.prototype iterator functions like forEach, reduce should work for Java arrays, lists
sundar
parents:
18334
diff
changeset
|
173 |
} |
16147 | 174 |
|
175 |
return new EmptyArrayLikeIterator(); |
|
176 |
} |
|
177 |
||
178 |
} |