| OLD | NEW |
| 1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2011, 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 class ListFactory<E> { | 5 class ListFactory<E> { |
| 6 factory List.from(Iterable<E> other) { | 6 factory List.from(Iterable<E> other) { |
| 7 if (other == null) { | 7 if (other == null) { |
| 8 throw const NullPointerException(); | 8 throw const NullPointerException(); |
| 9 } | 9 } |
| 10 List<E> list = new List<E>(); | 10 List<E> list = new List<E>(); |
| 11 for (final e in other) { | 11 for (final e in other) { |
| 12 list.add(e); | 12 list.add(e); |
| 13 } | 13 } |
| 14 return list; | 14 return list; |
| 15 } | 15 } |
| 16 | 16 |
| 17 factory List([int length = null]) { | 17 factory List([int length = null]) { |
| 18 bool isFixed = true; | 18 bool isFixed = true; |
| 19 if (length === null) { | 19 if (length === null) { |
| 20 length = 0; | 20 length = 0; |
| 21 isFixed = false; | 21 isFixed = false; |
| 22 } else if (length < 0) { | 22 } else if (length < 0) { |
| 23 throw new IllegalArgumentException("negative length $length"); | 23 throw new IllegalArgumentException("negative length $length"); |
| 24 } | 24 } |
| 25 // TODO(floitsch): make list creation more efficient. Currently we allocate | 25 |
| 26 // a new TypeToken at every allocation. Either we can optimize them away, | 26 ListImplementation<E> list = new ListImplementation<E>(length); |
| 27 // or we need to find other ways to pass type-information from Dart to JS. | |
| 28 ListImplementation list = _new(new TypeToken<E>(), length); | |
| 29 list._isFixed = isFixed; | 27 list._isFixed = isFixed; |
| 30 return list; | 28 return list; |
| 31 } | 29 } |
| 32 | |
| 33 static ListImplementation _new(TypeToken typeToken, int length) native; | |
| 34 } | 30 } |
| 35 | 31 |
| 36 | 32 |
| 37 class ListImplementation<T> implements List<T> native "Array" { | 33 class ListImplementation<T> implements List<T> native "Array" { |
| 38 // ListImplementation maps directly to a JavaScript array. If the list is | 34 // ListImplementation maps directly to a JavaScript array. If the list is |
| 39 // constructed by the ListFactory.List constructor, it has an | 35 // constructed by the ListFactory.List constructor, it has an |
| 40 // additional named property for '_isFixed'. If it is a literal, the | 36 // additional named property for '_isFixed'. If it is a literal, the |
| 41 // code generator will not add the property. It will be 'undefined' | 37 // code generator will not add the property. It will be 'undefined' |
| 42 // and coerce to false. | 38 // and coerce to false. |
| 43 bool _isFixed; | 39 bool _isFixed; |
| 44 | 40 |
| 41 ListImplementation(int length); |
| 42 |
| 45 T operator[](int index) { | 43 T operator[](int index) { |
| 46 if (0 <= index && index < length) { | 44 if (0 <= index && index < length) { |
| 47 return _indexOperator(index); | 45 return _indexOperator(index); |
| 48 } | 46 } |
| 49 throw new IndexOutOfRangeException(index); | 47 throw new IndexOutOfRangeException(index); |
| 50 } | 48 } |
| 51 | 49 |
| 52 void operator[]=(int index, T value) { | 50 void operator[]=(int index, T value) { |
| 53 if (index < 0 || length <= index) { | 51 if (index < 0 || length <= index) { |
| 54 throw new IndexOutOfRangeException(index); | 52 throw new IndexOutOfRangeException(index); |
| (...skipping 214 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 269 } | 267 } |
| 270 | 268 |
| 271 static List _newList(int len) native { | 269 static List _newList(int len) native { |
| 272 return new List(len); | 270 return new List(len); |
| 273 } | 271 } |
| 274 | 272 |
| 275 static void _throwIndexOutOfRangeException(int index) native { | 273 static void _throwIndexOutOfRangeException(int index) native { |
| 276 throw new IndexOutOfRangeException(index); | 274 throw new IndexOutOfRangeException(index); |
| 277 } | 275 } |
| 278 } | 276 } |
| OLD | NEW |