| 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) native; | 43 T operator[](int index) native; |
| 46 void operator[]=(int index, T value) native; | 44 void operator[]=(int index, T value) native; |
| 47 | 45 |
| 48 Iterator<T> iterator() { | 46 Iterator<T> iterator() { |
| 49 if (_isFixed) { | 47 if (_isFixed) { |
| 50 return new FixedSizeListIterator<T>(this); | 48 return new FixedSizeListIterator<T>(this); |
| 51 } else { | 49 } else { |
| 52 return new VariableSizeListIterator<T>(this); | 50 return new VariableSizeListIterator<T>(this); |
| 53 } | 51 } |
| 54 } | 52 } |
| (...skipping 201 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 256 } | 254 } |
| 257 | 255 |
| 258 static List _newList(int len) native { | 256 static List _newList(int len) native { |
| 259 return new List(len); | 257 return new List(len); |
| 260 } | 258 } |
| 261 | 259 |
| 262 static void _throwIndexOutOfRangeException(int index) native { | 260 static void _throwIndexOutOfRangeException(int index) native { |
| 263 throw new IndexOutOfRangeException(index); | 261 throw new IndexOutOfRangeException(index); |
| 264 } | 262 } |
| 265 } | 263 } |
| OLD | NEW |