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