OLD | NEW |
(Empty) | |
| 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 |
| 3 // BSD-style license that can be found in the LICENSE file. |
| 4 |
| 5 part of _interceptors; |
| 6 |
| 7 /** |
| 8 * The interceptor class for [List]. The compiler recognizes this |
| 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 |
| 11 * argument added to each member. |
| 12 */ |
| 13 class JSArray<E> implements List<E> { |
| 14 const JSArray(); |
| 15 |
| 16 void add(E value) { |
| 17 checkGrowable(this, 'add'); |
| 18 JS('void', r'#.push(#)', this, value); |
| 19 } |
| 20 |
| 21 E removeAt(int index) { |
| 22 if (index is !int) throw new ArgumentError(index); |
| 23 if (index < 0 || index >= length) { |
| 24 throw new RangeError.value(index); |
| 25 } |
| 26 checkGrowable(this, 'removeAt'); |
| 27 return JS('var', r'#.splice(#, 1)[0]', this, index); |
| 28 } |
| 29 |
| 30 E removeLast() { |
| 31 checkGrowable(this, 'removeLast'); |
| 32 if (length == 0) throw new RangeError.value(-1); |
| 33 return JS('var', r'#.pop()', this); |
| 34 } |
| 35 |
| 36 List<E> filter(bool f(E element)) { |
| 37 return Collections.filter(this, <E>[], f); |
| 38 } |
| 39 |
| 40 void addAll(Collection<E> collection) { |
| 41 for (Element e in collection) { |
| 42 this.add(e); |
| 43 } |
| 44 } |
| 45 |
| 46 void addLast(E value) { |
| 47 checkGrowable(this, 'addLast'); |
| 48 JS('void', r'#.push(#)', this, value); |
| 49 } |
| 50 |
| 51 void clear() { |
| 52 length = 0; |
| 53 } |
| 54 |
| 55 void forEach(void f(E element)) { |
| 56 return Collections.forEach(this, f); |
| 57 } |
| 58 |
| 59 Collection map(f(E element)) { |
| 60 return Collections.map(this, [], f); |
| 61 } |
| 62 |
| 63 reduce(initialValue, combine(previousValue, E element)) { |
| 64 return Collections.reduce(this, initialValue, combine); |
| 65 } |
| 66 |
| 67 List<E> getRange(int start, int length) { |
| 68 // TODO(ngeoffray): Parameterize the return value. |
| 69 if (0 == length) return []; |
| 70 checkNull(start); // TODO(ahe): This is not specified but co19 tests it. |
| 71 checkNull(length); // TODO(ahe): This is not specified but co19 tests it. |
| 72 if (start is !int) throw new ArgumentError(start); |
| 73 if (length is !int) throw new ArgumentError(length); |
| 74 if (length < 0) throw new ArgumentError(length); |
| 75 if (start < 0) throw new RangeError.value(start); |
| 76 int end = start + length; |
| 77 if (end > this.length) { |
| 78 throw new RangeError.value(length); |
| 79 } |
| 80 if (length < 0) throw new ArgumentError(length); |
| 81 return JS('List', r'#.slice(#, #)', this, start, end); |
| 82 } |
| 83 } |
OLD | NEW |