| OLD | NEW |
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 part of dart._interceptors; | 5 part of dart._interceptors; |
| 6 | 6 |
| 7 /** | 7 /** |
| 8 * The interceptor class for [List]. The compiler recognizes this | 8 * The interceptor class for [List]. The compiler recognizes this |
| 9 * class as an interceptor, and changes references to [:this:] to | 9 * class as an interceptor, and changes references to [:this:] to |
| 10 * actually use the receiver of the method, which is generated as an extra | 10 * actually use the receiver of the method, which is generated as an extra |
| 11 * argument added to each member. | 11 * argument added to each member. |
| 12 */ | 12 */ |
| 13 @JsPeerInterface(name: 'Array') | 13 @JsPeerInterface(name: 'Array') |
| 14 class JSArray<E> implements List<E>, JSIndexable { | 14 class JSArray<E> implements List<E>, JSIndexable { |
| 15 | 15 |
| 16 const JSArray(); | 16 const JSArray(); |
| 17 | 17 |
| 18 /** | 18 /** |
| 19 * Constructor for adding type parameters to an existing JavaScript Array. | 19 * Constructor for adding type parameters to an existing JavaScript Array. |
| 20 */ | 20 */ |
| 21 factory JSArray.typed(allocation) => | 21 factory JSArray.typed(allocation) => |
| 22 // TODO(jmesserly): skip this when E is dynamic and Object. | 22 // TODO(jmesserly): skip this when E is dynamic and Object. |
| 23 JS('-dynamic', 'dart.list(#, #)', allocation, E); | 23 JS('-dynamic', 'dart.list(#, #)', allocation, E); |
| 24 | 24 |
| 25 // TODO(jmesserly): consider a fixed array subclass instead. | 25 // TODO(jmesserly): consider a fixed array subclass instead. |
| 26 factory JSArray.markFixed(allocation) => | 26 factory JSArray.markFixed(allocation) => |
| 27 new JSArray<E>.typed(markFixedList(allocation)); | 27 new JSArray<E>.typed(markFixedList(allocation)); |
| 28 | 28 |
| 29 factory JSArray.markGrowable(allocation) = JSArray.typed; | 29 factory JSArray.markGrowable(allocation) = JSArray<E>.typed; |
| 30 | 30 |
| 31 static List markFixedList(List list) { | 31 static List markFixedList(List list) { |
| 32 // Functions are stored in the hidden class and not as properties in | 32 // Functions are stored in the hidden class and not as properties in |
| 33 // the object. We never actually look at the value, but only want | 33 // the object. We never actually look at the value, but only want |
| 34 // to know if the property exists. | 34 // to know if the property exists. |
| 35 JS('void', r'#.fixed$length = Array', list); | 35 JS('void', r'#.fixed$length = Array', list); |
| 36 return list; | 36 return list; |
| 37 } | 37 } |
| 38 | 38 |
| 39 checkGrowable(reason) { | 39 checkGrowable(reason) { |
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 98 | 98 |
| 99 void retainWhere(bool test(E element)) { | 99 void retainWhere(bool test(E element)) { |
| 100 IterableMixinWorkaround.removeWhereList(this, | 100 IterableMixinWorkaround.removeWhereList(this, |
| 101 (E element) => !test(element)); | 101 (E element) => !test(element)); |
| 102 } | 102 } |
| 103 | 103 |
| 104 Iterable<E> where(bool f(E element)) { | 104 Iterable<E> where(bool f(E element)) { |
| 105 return new IterableMixinWorkaround<E>().where(this, f); | 105 return new IterableMixinWorkaround<E>().where(this, f); |
| 106 } | 106 } |
| 107 | 107 |
| 108 Iterable expand(Iterable f(E element)) { | 108 Iterable/*<T>*/ expand/*<T>*/(Iterable/*<E>*/ /*=Iterable<T>*/f(E element)) { |
| 109 return IterableMixinWorkaround.expand(this, f); | 109 return IterableMixinWorkaround.expand(this, f); |
| 110 } | 110 } |
| 111 | 111 |
| 112 void addAll(Iterable<E> collection) { | 112 void addAll(Iterable<E> collection) { |
| 113 for (E e in collection) { | 113 for (E e in collection) { |
| 114 this.add(e); | 114 this.add(e); |
| 115 } | 115 } |
| 116 } | 116 } |
| 117 | 117 |
| 118 void clear() { | 118 void clear() { |
| 119 length = 0; | 119 length = 0; |
| 120 } | 120 } |
| 121 | 121 |
| 122 void forEach(void f(E element)) { | 122 void forEach(void f(E element)) { |
| 123 int length = this.length; | 123 int length = this.length; |
| 124 for (int i = 0; i < length; i++) { | 124 for (int i = 0; i < length; i++) { |
| 125 f(JS('', '#[#]', this, i)); | 125 f(JS('', '#[#]', this, i)); |
| 126 if (length != this.length) { | 126 if (length != this.length) { |
| 127 throw new ConcurrentModificationError(this); | 127 throw new ConcurrentModificationError(this); |
| 128 } | 128 } |
| 129 } | 129 } |
| 130 } | 130 } |
| 131 | 131 |
| 132 Iterable map(f(E element)) { | 132 Iterable/*<T>*/ map/*<T>*/(/*=T*/ f(E element)) { |
| 133 return IterableMixinWorkaround.mapList(this, f); | 133 return IterableMixinWorkaround.mapList(this, f); |
| 134 } | 134 } |
| 135 | 135 |
| 136 String join([String separator = ""]) { | 136 String join([String separator = ""]) { |
| 137 var list = new List(this.length); | 137 var list = new List(this.length); |
| 138 for (int i = 0; i < this.length; i++) { | 138 for (int i = 0; i < this.length; i++) { |
| 139 list[i] = "${this[i]}"; | 139 list[i] = "${this[i]}"; |
| 140 } | 140 } |
| 141 return JS('String', "#.join(#)", list, separator); | 141 return JS('String', "#.join(#)", list, separator); |
| 142 } | 142 } |
| (...skipping 11 matching lines...) Expand all Loading... |
| 154 } | 154 } |
| 155 | 155 |
| 156 Iterable<E> skipWhile(bool test(E value)) { | 156 Iterable<E> skipWhile(bool test(E value)) { |
| 157 return new IterableMixinWorkaround<E>().skipWhile(this, test); | 157 return new IterableMixinWorkaround<E>().skipWhile(this, test); |
| 158 } | 158 } |
| 159 | 159 |
| 160 E reduce(E combine(E value, E element)) { | 160 E reduce(E combine(E value, E element)) { |
| 161 return IterableMixinWorkaround.reduce(this, combine); | 161 return IterableMixinWorkaround.reduce(this, combine); |
| 162 } | 162 } |
| 163 | 163 |
| 164 fold(initialValue, combine(previousValue, E element)) { | 164 /*=T*/ fold/*<T>*/(/*=T*/ initialValue, /*=T*/ combine(/*=T*/ previousValue, E
element)) { |
| 165 return IterableMixinWorkaround.fold(this, initialValue, combine); | 165 return IterableMixinWorkaround.fold(this, initialValue, combine); |
| 166 } | 166 } |
| 167 | 167 |
| 168 E firstWhere(bool test(E value), {E orElse()}) { | 168 E firstWhere(bool test(E value), {E orElse()}) { |
| 169 return IterableMixinWorkaround.firstWhere(this, test, orElse); | 169 return IterableMixinWorkaround.firstWhere(this, test, orElse); |
| 170 } | 170 } |
| 171 | 171 |
| 172 E lastWhere(bool test(E value), {E orElse()}) { | 172 E lastWhere(bool test(E value), {E orElse()}) { |
| 173 return IterableMixinWorkaround.lastWhereList(this, test, orElse); | 173 return IterableMixinWorkaround.lastWhereList(this, test, orElse); |
| 174 } | 174 } |
| (...skipping 150 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 325 } | 325 } |
| 326 | 326 |
| 327 /** | 327 /** |
| 328 * Dummy subclasses that allow the backend to track more precise | 328 * Dummy subclasses that allow the backend to track more precise |
| 329 * information about arrays through their type. The CPA type inference | 329 * information about arrays through their type. The CPA type inference |
| 330 * relies on the fact that these classes do not override [] nor []=. | 330 * relies on the fact that these classes do not override [] nor []=. |
| 331 */ | 331 */ |
| 332 class JSMutableArray<E> extends JSArray<E> implements JSMutableIndexable {} | 332 class JSMutableArray<E> extends JSArray<E> implements JSMutableIndexable {} |
| 333 class JSFixedArray<E> extends JSMutableArray<E> {} | 333 class JSFixedArray<E> extends JSMutableArray<E> {} |
| 334 class JSExtendableArray<E> extends JSMutableArray<E> {} | 334 class JSExtendableArray<E> extends JSMutableArray<E> {} |
| OLD | NEW |