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 |
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
70 JS('JSExtendableArray', '#', new JSArray<E>.typed(allocation)); | 70 JS('JSExtendableArray', '#', new JSArray<E>.typed(allocation)); |
71 | 71 |
72 static List markFixedList(List list) { | 72 static List markFixedList(List list) { |
73 // Functions are stored in the hidden class and not as properties in | 73 // Functions are stored in the hidden class and not as properties in |
74 // the object. We never actually look at the value, but only want | 74 // the object. We never actually look at the value, but only want |
75 // to know if the property exists. | 75 // to know if the property exists. |
76 JS('void', r'#.fixed$length = Array', list); | 76 JS('void', r'#.fixed$length = Array', list); |
77 return JS('JSFixedArray', '#', list); | 77 return JS('JSFixedArray', '#', list); |
78 } | 78 } |
79 | 79 |
80 static List markUnmodifiableList(List list) { | |
81 // Functions are stored in the hidden class and not as properties in | |
82 // the object. We never actually look at the value, but only want | |
83 // to know if the property exists. | |
84 JS('void', r'#.fixed$length = Array', list); | |
85 JS('void', r'#.immutable$list = Array', list); | |
86 return JS('List', '#', list); | |
sra1
2015/04/22 21:17:31
1. Change 'List' to 'JSArray'.
Leaving it as List
| |
87 } | |
88 | |
80 checkMutable(reason) { | 89 checkMutable(reason) { |
81 if (this is !JSMutableArray) { | 90 if (this is !JSMutableArray) { |
82 throw new UnsupportedError(reason); | 91 throw new UnsupportedError(reason); |
83 } | 92 } |
84 } | 93 } |
85 | 94 |
86 checkGrowable(reason) { | 95 checkGrowable(reason) { |
87 if (this is !JSExtendableArray) { | 96 if (this is !JSExtendableArray) { |
88 throw new UnsupportedError(reason); | 97 throw new UnsupportedError(reason); |
89 } | 98 } |
90 } | 99 } |
91 | 100 |
92 void add(E value) { | 101 void add(E value) { |
93 checkGrowable('add'); | 102 checkGrowable('add'); |
94 JS('void', r'#.push(#)', this, value); | 103 JS('void', r'#.push(#)', this, value); |
95 } | 104 } |
96 | 105 |
97 E removeAt(int index) { | 106 E removeAt(int index) { |
107 checkGrowable('removeAt'); | |
98 if (index is !int) throw new ArgumentError(index); | 108 if (index is !int) throw new ArgumentError(index); |
99 if (index < 0 || index >= length) { | 109 if (index < 0 || index >= length) { |
100 throw new RangeError.value(index); | 110 throw new RangeError.value(index); |
101 } | 111 } |
102 checkGrowable('removeAt'); | |
103 return JS('var', r'#.splice(#, 1)[0]', this, index); | 112 return JS('var', r'#.splice(#, 1)[0]', this, index); |
104 } | 113 } |
105 | 114 |
106 void insert(int index, E value) { | 115 void insert(int index, E value) { |
116 checkGrowable('insert'); | |
107 if (index is !int) throw new ArgumentError(index); | 117 if (index is !int) throw new ArgumentError(index); |
108 if (index < 0 || index > length) { | 118 if (index < 0 || index > length) { |
109 throw new RangeError.value(index); | 119 throw new RangeError.value(index); |
110 } | 120 } |
111 checkGrowable('insert'); | |
112 JS('void', r'#.splice(#, 0, #)', this, index, value); | 121 JS('void', r'#.splice(#, 0, #)', this, index, value); |
113 } | 122 } |
114 | 123 |
115 void insertAll(int index, Iterable<E> iterable) { | 124 void insertAll(int index, Iterable<E> iterable) { |
116 checkGrowable('insertAll'); | 125 checkGrowable('insertAll'); |
117 RangeError.checkValueInInterval(index, 0, this.length, "index"); | 126 RangeError.checkValueInInterval(index, 0, this.length, "index"); |
118 if (iterable is! EfficientLength) { | 127 if (iterable is! EfficientLength) { |
119 iterable = iterable.toList(); | 128 iterable = iterable.toList(); |
120 } | 129 } |
121 int insertionLength = iterable.length; | 130 int insertionLength = iterable.length; |
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
192 | 201 |
193 Iterable<E> where(bool f(E element)) { | 202 Iterable<E> where(bool f(E element)) { |
194 return new WhereIterable<E>(this, f); | 203 return new WhereIterable<E>(this, f); |
195 } | 204 } |
196 | 205 |
197 Iterable expand(Iterable f(E element)) { | 206 Iterable expand(Iterable f(E element)) { |
198 return new ExpandIterable<E, dynamic>(this, f); | 207 return new ExpandIterable<E, dynamic>(this, f); |
199 } | 208 } |
200 | 209 |
201 void addAll(Iterable<E> collection) { | 210 void addAll(Iterable<E> collection) { |
211 checkGrowable('addAll'); | |
202 for (E e in collection) { | 212 for (E e in collection) { |
203 this.add(e); | 213 JS('void', r'#.push(#)', this, e); |
204 } | 214 } |
205 } | 215 } |
206 | 216 |
207 void clear() { | 217 void clear() { |
208 length = 0; | 218 length = 0; |
209 } | 219 } |
210 | 220 |
211 void forEach(void f(E element)) { | 221 void forEach(void f(E element)) { |
212 int end = this.length; | 222 int end = this.length; |
213 for (int i = 0; i < end; i++) { | 223 for (int i = 0; i < end; i++) { |
(...skipping 343 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
557 | 567 |
558 Set<E> toSet() => new Set<E>.from(this); | 568 Set<E> toSet() => new Set<E>.from(this); |
559 | 569 |
560 Iterator<E> get iterator => new ArrayIterator<E>(this); | 570 Iterator<E> get iterator => new ArrayIterator<E>(this); |
561 | 571 |
562 int get hashCode => Primitives.objectHashCode(this); | 572 int get hashCode => Primitives.objectHashCode(this); |
563 | 573 |
564 int get length => JS('JSUInt32', r'#.length', this); | 574 int get length => JS('JSUInt32', r'#.length', this); |
565 | 575 |
566 void set length(int newLength) { | 576 void set length(int newLength) { |
577 checkGrowable('set length'); | |
567 if (newLength is !int) throw new ArgumentError(newLength); | 578 if (newLength is !int) throw new ArgumentError(newLength); |
568 if (newLength < 0) throw new RangeError.value(newLength); | 579 if (newLength < 0) throw new RangeError.value(newLength); |
569 checkGrowable('set length'); | |
570 JS('void', r'#.length = #', this, newLength); | 580 JS('void', r'#.length = #', this, newLength); |
571 } | 581 } |
572 | 582 |
573 E operator [](int index) { | 583 E operator [](int index) { |
574 if (index is !int) throw new ArgumentError(index); | 584 if (index is !int) throw new ArgumentError(index); |
575 if (index >= length || index < 0) throw new RangeError.value(index); | 585 if (index >= length || index < 0) throw new RangeError.value(index); |
576 return JS('var', '#[#]', this, index); | 586 return JS('var', '#[#]', this, index); |
577 } | 587 } |
578 | 588 |
579 void operator []=(int index, E value) { | 589 void operator []=(int index, E value) { |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
629 | 639 |
630 if (_index >= length) { | 640 if (_index >= length) { |
631 _current = null; | 641 _current = null; |
632 return false; | 642 return false; |
633 } | 643 } |
634 _current = _iterable[_index]; | 644 _current = _iterable[_index]; |
635 _index++; | 645 _index++; |
636 return true; | 646 return true; |
637 } | 647 } |
638 } | 648 } |
OLD | NEW |