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