| 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 // Factory class constructing mutable List and Map objects from parser generated | 5 // Factory class constructing mutable List and Map objects from parser generated |
| 6 // list and map literals. | 6 // list and map literals. |
| 7 | 7 |
| 8 class _LiteralFactory { | 8 class _LiteralFactory { |
| 9 // [elements] contains elements that are not yet type checked. | 9 // [elements] contains elements that are already type checked. |
| 10 factory List<E>.fromLiteral(int location, | 10 factory List<E>.fromLiteral(List elements) { |
| 11 String element_type, | 11 var list = new GrowableObjectArray<E>(); |
| 12 List elements) { | 12 if (elements.length > 0) { |
| 13 var len = elements.length; | 13 list.backingArray = elements; |
| 14 var list = new GrowableObjectArray<E>.withCapacity(len); | 14 list.length = elements.length; |
| 15 for (int i = 0; i < len; i++) { | |
| 16 // In checked mode only, rethrow a potential type error with a more user | |
| 17 // friendly error message. | |
| 18 try { | |
| 19 list.backingArray[i] = elements[i]; | |
| 20 } catch (TypeError error) { | |
| 21 TypeError._throwNew(location, | |
| 22 elements[i], | |
| 23 element_type, | |
| 24 "list literal element at index ${i}"); | |
| 25 } | |
| 26 } | 15 } |
| 27 list.length = len; | |
| 28 return list; | 16 return list; |
| 29 } | 17 } |
| 30 | 18 |
| 31 // [elements] contains n key-value pairs. | 19 // [elements] contains n key-value pairs. |
| 32 // The keys are at position 2*n and are already type checked by the parser | 20 // The keys are at position 2*n and are already type checked by the parser |
| 33 // in checked mode. | 21 // in checked mode. |
| 34 // The values are at position 2*n+1 and are not yet type checked. | 22 // The values are at position 2*n+1 and are not yet type checked. |
| 35 factory Map<K, V>.fromLiteral(int location, | 23 factory Map<K, V>.fromLiteral(List elements) { |
| 36 String value_type, | |
| 37 List elements) { | |
| 38 var map = new LinkedHashMap<String, V>(); | 24 var map = new LinkedHashMap<String, V>(); |
| 39 var len = elements.length; | 25 var len = elements.length; |
| 40 for (int i = 1; i < len; i += 2) { | 26 for (int i = 1; i < len; i += 2) { |
| 41 // The type of the key has been checked in the parser already. | 27 map[elements[i - 1]] = elements[i]; |
| 42 // In checked mode only, rethrow a potential type error with a more user | |
| 43 // friendly error message. | |
| 44 try { | |
| 45 map[elements[i - 1]] = elements[i]; | |
| 46 } catch (TypeError error) { | |
| 47 TypeError._throwNew(location, | |
| 48 elements[i], | |
| 49 value_type, | |
| 50 "map literal value at index ${i ~/ 2}"); | |
| 51 } | |
| 52 } | 28 } |
| 53 return map; | 29 return map; |
| 54 } | 30 } |
| 55 } | 31 } |
| OLD | NEW |