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 part of serialization; | 5 part of serialization; |
6 | 6 |
7 // TODO(alanknight): Figure out how to reasonably separate out the things | 7 // TODO(alanknight): Figure out how to reasonably separate out the things |
8 // that require reflection without making the API more awkward. Or if that is | 8 // that require reflection without making the API more awkward. Or if that is |
9 // in fact necessary. Maybe the tree-shaking will just remove it if unused. | 9 // in fact necessary. Maybe the tree-shaking will just remove it if unused. |
10 | 10 |
(...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
123 /** | 123 /** |
124 * Configure this instance to use lists accessing fields by index as its | 124 * Configure this instance to use lists accessing fields by index as its |
125 * output. Instances can either produce maps or lists. The list representation | 125 * output. Instances can either produce maps or lists. The list representation |
126 * is much more compact and used by default. The map representation is | 126 * is much more compact and used by default. The map representation is |
127 * much easier to debug. The default is to use lists. | 127 * much easier to debug. The default is to use lists. |
128 */ | 128 */ |
129 configureForLists() { | 129 configureForLists() { |
130 useMaps = false; | 130 useMaps = false; |
131 } | 131 } |
132 | 132 |
133 /** Create either a list or a map to hold the object's state, depending | 133 /** |
| 134 * Create either a list or a map to hold the object's state, depending |
134 * on the [useMaps] variable. If using a Map, we wrap it in order to keep | 135 * on the [useMaps] variable. If using a Map, we wrap it in order to keep |
135 * the protocol compatible. See [configureForLists]/[configureForMaps]. | 136 * the protocol compatible. See [configureForLists]/[configureForMaps]. |
| 137 * |
| 138 * If a list is returned, it is growable. |
136 */ | 139 */ |
137 createStateHolder() => | 140 createStateHolder() { |
138 useMaps ? new _MapWrapper(fields.contents) : new List(fields.length); | 141 if (useMaps) return new _MapWrapper(fields.contents); |
| 142 List list = []; |
| 143 list.length = fields.length; |
| 144 return list; |
| 145 } |
139 | 146 |
140 /** | 147 /** |
141 * Wrap the state if it's passed in as a map, and if the keys are references, | 148 * Wrap the state if it's passed in as a map, and if the keys are references, |
142 * resolve them to the strings we expect. We leave the previous keys in there | 149 * resolve them to the strings we expect. We leave the previous keys in there |
143 * as well, as they shouldn't be harmful, and it costs more to remove them. | 150 * as well, as they shouldn't be harmful, and it costs more to remove them. |
144 */ | 151 */ |
145 makeIndexableByNumber(state) { | 152 makeIndexableByNumber(state) { |
146 if (!(state is Map)) return state; | 153 if (!(state is Map)) return state; |
147 // TODO(alanknight): This is quite inefficient, and we do it twice per | 154 // TODO(alanknight): This is quite inefficient, and we do it twice per |
148 // instance. If the keys are references, we need to turn them into strings | 155 // instance. If the keys are references, we need to turn them into strings |
(...skipping 474 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
623 _MapWrapper(this.fieldList) : _map = new Map(); | 630 _MapWrapper(this.fieldList) : _map = new Map(); |
624 _MapWrapper.fromMap(this._map, this.fieldList); | 631 _MapWrapper.fromMap(this._map, this.fieldList); |
625 | 632 |
626 operator [](key) => _map[fieldList[key].name]; | 633 operator [](key) => _map[fieldList[key].name]; |
627 | 634 |
628 operator []=(key, value) { _map[fieldList[key].name] = value; } | 635 operator []=(key, value) { _map[fieldList[key].name] = value; } |
629 get length => _map.length; | 636 get length => _map.length; |
630 | 637 |
631 asMap() => _map; | 638 asMap() => _map; |
632 } | 639 } |
OLD | NEW |