| 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 // Note that the optimizing compiler depends on the algorithm which | 5 // The _GrowableArrayMarker class is used to signal to the List() factory |
| 6 // returns a _GrowableObjectArray if length is null, otherwise returns | 6 // whether a parameter was passed. |
| 7 // fixed size array. | 7 class _GrowableArrayMarker implements int { |
| 8 const _GrowableArrayMarker(); |
| 9 } |
| 10 |
| 11 const _GROWABLE_ARRAY_MARKER = const _GrowableArrayMarker(); |
| 12 |
| 8 patch class List<E> { | 13 patch class List<E> { |
| 9 /* patch */ factory List([int length]) { | 14 /* patch */ factory List([int length = _GROWABLE_ARRAY_MARKER]) { |
| 10 if (!?length) return new _GrowableObjectArray<E>(0); | 15 if (identical(length, _GROWABLE_ARRAY_MARKER)) { |
| 11 if ((length is! int) || (length < 0)) { | 16 return new _GrowableObjectArray<E>(0); |
| 12 _throwArgumentError(length); | |
| 13 } | 17 } |
| 14 _ObjectArray<E> result = new _ObjectArray<E>(length); | 18 // All error handling on the length parameter is done at the implementation |
| 15 return result; | 19 // of new _ObjectArray. |
| 20 return new _ObjectArray<E>(length); |
| 16 } | 21 } |
| 17 | 22 |
| 18 /* patch */ factory List.filled(int length, E fill) { | 23 /* patch */ factory List.filled(int length, E fill) { |
| 19 if ((length is! int) || (length < 0)) { | 24 // All error handling on the length parameter is done at the implementation |
| 20 _throwArgumentError(length); | 25 // of new _ObjectArray. |
| 21 } | 26 var result = new _ObjectArray<E>(length); |
| 22 _ObjectArray<E> result = new _ObjectArray<E>(length); | |
| 23 if (fill != null) { | 27 if (fill != null) { |
| 24 for (int i = 0; i < length; i++) { | 28 for (int i = 0; i < length; i++) { |
| 25 result[i] = fill; | 29 result[i] = fill; |
| 26 } | 30 } |
| 27 } | 31 } |
| 28 return result; | 32 return result; |
| 29 } | 33 } |
| 30 | 34 |
| 31 // Factory constructing a mutable List from a parser generated List literal. | 35 // Factory constructing a mutable List from a parser generated List literal. |
| 32 // [elements] contains elements that are already type checked. | 36 // [elements] contains elements that are already type checked. |
| 33 factory List._fromLiteral(List elements) { | 37 factory List._fromLiteral(List elements) { |
| 34 if (elements.isEmpty) { | 38 if (elements.isEmpty) { |
| 35 return new _GrowableObjectArray<E>(0); | 39 return new _GrowableObjectArray<E>(0); |
| 36 } | 40 } |
| 37 var result = new _GrowableObjectArray<E>.withData(elements); | 41 var result = new _GrowableObjectArray<E>.withData(elements); |
| 38 result._setLength(elements.length); | 42 result._setLength(elements.length); |
| 39 return result; | 43 return result; |
| 40 } | 44 } |
| 41 | |
| 42 static void _throwArgumentError(int length) { | |
| 43 throw new ArgumentError("Length must be a positive integer: $length."); | |
| 44 } | |
| 45 } | 45 } |
| OLD | NEW |