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 _interceptors; | 5 part of _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 class JSArray<E> implements List<E> { | 13 class JSArray<E> implements List<E> { |
14 const JSArray(); | 14 const JSArray(); |
15 | 15 |
16 void add(E value) { | 16 void add(E value) { |
17 checkGrowable(this, 'add'); | 17 checkGrowable(this, 'add'); |
18 JS('void', r'#.push(#)', this, value); | 18 JS('void', r'#.push(#)', this, value); |
19 } | 19 } |
20 | 20 |
21 E removeAt(int index) { | 21 E removeAt(int index) { |
22 if (index is !int) throw new ArgumentError(index); | 22 if (index is !int) throw new ArgumentError(index); |
23 if (index < 0 || index >= length) { | 23 if (index < 0 || index >= length) { |
24 throw new RangeError.value(index); | 24 throw new RangeError.value(index); |
25 } | 25 } |
26 checkGrowable(this, 'removeAt'); | 26 checkGrowable(this, 'removeAt'); |
27 return JS('var', r'#.splice(#, 1)[0]', this, index); | 27 return JS('var', r'#.splice(#, 1)[0]', this, index); |
28 } | 28 } |
29 | 29 |
| 30 void insertAt(int index, E value) { |
| 31 if (index is !int) throw new ArgumentError(index); |
| 32 if (index < 0 || index > length) { |
| 33 throw new RangeError.value(index); |
| 34 } |
| 35 checkGrowable(this, 'insertAt'); |
| 36 JS('void', r'#.splice(#, 0, #)', this, index, value); |
| 37 } |
| 38 |
30 E removeLast() { | 39 E removeLast() { |
31 checkGrowable(this, 'removeLast'); | 40 checkGrowable(this, 'removeLast'); |
32 if (length == 0) throw new RangeError.value(-1); | 41 if (length == 0) throw new RangeError.value(-1); |
33 return JS('var', r'#.pop()', this); | 42 return JS('var', r'#.pop()', this); |
34 } | 43 } |
35 | 44 |
36 void remove(Object element) { | 45 void remove(Object element) { |
37 checkGrowable(this, 'remove'); | 46 checkGrowable(this, 'remove'); |
38 for (int i = 0; i < this.length; i++) { | 47 for (int i = 0; i < this.length; i++) { |
39 if (this[i] == element) { | 48 if (this[i] == element) { |
(...skipping 244 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
284 return JS('var', '#[#]', this, index); | 293 return JS('var', '#[#]', this, index); |
285 } | 294 } |
286 | 295 |
287 void operator []=(int index, E value) { | 296 void operator []=(int index, E value) { |
288 checkMutable(this, 'indexed set'); | 297 checkMutable(this, 'indexed set'); |
289 if (index is !int) throw new ArgumentError(index); | 298 if (index is !int) throw new ArgumentError(index); |
290 if (index >= length || index < 0) throw new RangeError.value(index); | 299 if (index >= length || index < 0) throw new RangeError.value(index); |
291 JS('void', r'#[#] = #', this, index, value); | 300 JS('void', r'#[#] = #', this, index, value); |
292 } | 301 } |
293 } | 302 } |
OLD | NEW |