Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(156)

Side by Side Diff: pkg/serialization/lib/src/basic_rule.dart

Issue 12091026: Serialization shouldn't modify the keys of a map it's iterating over (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 7 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 124 matching lines...) Expand 10 before | Expand all | Expand 10 after
135 * the protocol compatible. See [configureForLists]/[configureForMaps]. 135 * the protocol compatible. See [configureForLists]/[configureForMaps].
136 */ 136 */
137 createStateHolder() => 137 createStateHolder() =>
138 useMaps ? new _MapWrapper(fields.contents) : new List(fields.length); 138 useMaps ? new _MapWrapper(fields.contents) : new List(fields.length);
139 139
140 /** 140 /**
141 * Wrap the state if it's passed in as a map, and if the keys are references, 141 * 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 142 * 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. 143 * as well, as they shouldn't be harmful, and it costs more to remove them.
144 */ 144 */
145 makeIndexableByNumber(state) { 145 makeIndexableByNumber(state) {
146 if (!(state is Map)) return state; 146 if (!(state is Map)) return state;
147 // TODO(alanknight): This is quite inefficient, and we do it twice per 147 // 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 148 // instance. If the keys are references, we need to turn them into strings
149 // before we can look at indexing them by field position. It's also eager, 149 // before we can look at indexing them by field position. It's also eager,
150 // but we know our keys are always primitives, so we don't have to worry 150 // but we know our keys are always primitives, so we don't have to worry
151 // about their instances not having been created yet. 151 // about their instances not having been created yet.
152 for (var each in state.keys) { 152 var newState = new Map();
Jennifer Messerly 2013/01/28 19:45:00 one idea that might help, if the perf here is noti
153 if (each is Reference) { 153 for (var each in state.keys) {
154 var inflated = each.inflated(); 154 var newKey = (each is Reference) ? each.inflated() : each;
155 state[inflated] = state[each]; 155 newState[newKey] = state[each];
156 } 156 }
157 } 157 return new _MapWrapper.fromMap(newState, fields.contents);
158 return new _MapWrapper.fromMap(state, fields.contents); 158 }
159 }
160 159
161 /** 160 /**
162 * Extract the state from [object] using an instanceMirror and the field 161 * Extract the state from [object] using an instanceMirror and the field
163 * names in [fields]. Call the function [callback] on each value. 162 * names in [fields]. Call the function [callback] on each value.
164 */ 163 */
165 extractState(object, Function callback) { 164 extractState(object, Function callback) {
166 var result = createStateHolder(); 165 var result = createStateHolder();
167 var mirror = reflect(object); 166 var mirror = reflect(object);
168 167
169 keysAndValues(fields).forEach( 168 keysAndValues(fields).forEach(
(...skipping 454 matching lines...) Expand 10 before | Expand all | Expand 10 after
624 _MapWrapper(this.fieldList) : _map = new Map(); 623 _MapWrapper(this.fieldList) : _map = new Map();
625 _MapWrapper.fromMap(this._map, this.fieldList); 624 _MapWrapper.fromMap(this._map, this.fieldList);
626 625
627 operator [](key) => _map[fieldList[key].name]; 626 operator [](key) => _map[fieldList[key].name];
628 627
629 operator []=(key, value) { _map[fieldList[key].name] = value; } 628 operator []=(key, value) { _map[fieldList[key].name] = value; }
630 get length => _map.length; 629 get length => _map.length;
631 630
632 asMap() => _map; 631 asMap() => _map;
633 } 632 }
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698