| 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 206 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 217 /** | 217 /** |
| 218 * Call the designated constructor with the appropriate fields from [state], | 218 * Call the designated constructor with the appropriate fields from [state], |
| 219 * first resolving references in the context of [reader]. | 219 * first resolving references in the context of [reader]. |
| 220 */ | 220 */ |
| 221 inflateEssential(state, Reader reader) { | 221 inflateEssential(state, Reader reader) { |
| 222 InstanceMirror mirror = constructor.constructFrom( | 222 InstanceMirror mirror = constructor.constructFrom( |
| 223 makeIndexableByNumber(state), reader); | 223 makeIndexableByNumber(state), reader); |
| 224 return mirror.reflectee; | 224 return mirror.reflectee; |
| 225 } | 225 } |
| 226 | 226 |
| 227 /** For all [state] not required in the constructor, set it in the [object], | 227 /** For all [rawState] not required in the constructor, set it in the |
| 228 * resolving references in the context of [reader]. | 228 * [object], resolving references in the context of [reader]. |
| 229 */ | 229 */ |
| 230 inflateNonEssential(rawState, object, Reader reader) { | 230 inflateNonEssential(rawState, object, Reader reader) { |
| 231 InstanceMirror mirror = reflect(object); | 231 InstanceMirror mirror = reflect(object); |
| 232 var state = makeIndexableByNumber(rawState); | 232 var state = makeIndexableByNumber(rawState); |
| 233 fields.forEachRegularField( (_Field field) { | 233 fields.forEachRegularField( (_Field field) { |
| 234 var value = reader.inflateReference(state[field.index]); | 234 var value = reader.inflateReference(state[field.index]); |
| 235 field.setValue(mirror, value); | 235 field.setValue(mirror, value); |
| 236 }); | 236 }); |
| 237 } | 237 } |
| 238 | 238 |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 277 /** | 277 /** |
| 278 * This represents a field in an object. It is intended to be used as part of | 278 * This represents a field in an object. It is intended to be used as part of |
| 279 * a [_FieldList]. | 279 * a [_FieldList]. |
| 280 */ | 280 */ |
| 281 abstract class _Field implements Comparable<_Field> { | 281 abstract class _Field implements Comparable<_Field> { |
| 282 | 282 |
| 283 /** The FieldList that contains us. */ | 283 /** The FieldList that contains us. */ |
| 284 final _FieldList fieldList; | 284 final _FieldList fieldList; |
| 285 | 285 |
| 286 /** | 286 /** |
| 287 * Our position in the [contents] collection of [fieldList]. This is used | 287 * Our position in the [fieldList._contents] collection. This is used |
| 288 * to index into the state, so it's extremely important. | 288 * to index into the state, so it's extremely important. |
| 289 */ | 289 */ |
| 290 int index; | 290 int index; |
| 291 | 291 |
| 292 /** Is this field used in the constructor? */ | 292 /** Is this field used in the constructor? */ |
| 293 bool usedInConstructor = false; | 293 bool usedInConstructor = false; |
| 294 | 294 |
| 295 /** | 295 /** |
| 296 * Create a new [_Field] instance. This will be either a [_NamedField] or a | 296 * Create a new [_Field] instance. This will be either a [_NamedField] or a |
| 297 * [_ConstantField] depending on whether or not [value] corresponds to a | 297 * [_ConstantField] depending on whether or not [value] corresponds to a |
| (...skipping 27 matching lines...) Expand all Loading... |
| 325 valueIn(InstanceMirror mirror); | 325 valueIn(InstanceMirror mirror); |
| 326 | 326 |
| 327 // TODO(alanknight): Is this the right name, or is it confusing that essential | 327 // TODO(alanknight): Is this the right name, or is it confusing that essential |
| 328 // is not the inverse of regular. | 328 // is not the inverse of regular. |
| 329 /** Return true if this is field is not used in the constructor. */ | 329 /** Return true if this is field is not used in the constructor. */ |
| 330 bool get isRegular => !usedInConstructor; | 330 bool get isRegular => !usedInConstructor; |
| 331 | 331 |
| 332 /** | 332 /** |
| 333 * Return true if this field is treated as essential state, either because | 333 * Return true if this field is treated as essential state, either because |
| 334 * it is used in the constructor, or because it's been designated | 334 * it is used in the constructor, or because it's been designated |
| 335 * using [setFieldWith]. | 335 * using [BasicRule.setFieldWith]. |
| 336 */ | 336 */ |
| 337 bool get isEssential => usedInConstructor; | 337 bool get isEssential => usedInConstructor; |
| 338 | 338 |
| 339 /** Set the [value] of our field in the given mirrored [object]. */ | 339 /** Set the [value] of our field in the given mirrored [object]. */ |
| 340 void setValue(InstanceMirror object, value); | 340 void setValue(InstanceMirror object, value); |
| 341 | 341 |
| 342 // Because [x] may not be a named field, we compare the toString. We don't | 342 // Because [x] may not be a named field, we compare the toString. We don't |
| 343 // care that much where constants come in the sort order as long as it's | 343 // care that much where constants come in the sort order as long as it's |
| 344 // consistent. | 344 // consistent. |
| 345 compareTo(_Field x) => toString().compareTo(x.toString()); | 345 compareTo(_Field x) => toString().compareTo(x.toString()); |
| (...skipping 24 matching lines...) Expand all Loading... |
| 370 | 370 |
| 371 String get name => | 371 String get name => |
| 372 _name == null ? _name = MirrorSystem.getName(nameSymbol) : _name; | 372 _name == null ? _name = MirrorSystem.getName(nameSymbol) : _name; |
| 373 | 373 |
| 374 operator ==(x) => x is _NamedField && (nameSymbol == x.nameSymbol); | 374 operator ==(x) => x is _NamedField && (nameSymbol == x.nameSymbol); |
| 375 int get hashCode => name.hashCode; | 375 int get hashCode => name.hashCode; |
| 376 | 376 |
| 377 /** | 377 /** |
| 378 * Return true if this field is treated as essential state, either because | 378 * Return true if this field is treated as essential state, either because |
| 379 * it is used in the constructor, or because it's been designated | 379 * it is used in the constructor, or because it's been designated |
| 380 * using [setFieldWith]. | 380 * using [BasicRule.setFieldWith]. |
| 381 */ | 381 */ |
| 382 bool get isEssential => super.isEssential || customSetter != null; | 382 bool get isEssential => super.isEssential || customSetter != null; |
| 383 | 383 |
| 384 /** Set the [value] of our field in the given mirrored [object]. */ | 384 /** Set the [value] of our field in the given mirrored [object]. */ |
| 385 void setValue(InstanceMirror object, value) { | 385 void setValue(InstanceMirror object, value) { |
| 386 setter(object, value); | 386 setter(object, value); |
| 387 } | 387 } |
| 388 | 388 |
| 389 valueIn(InstanceMirror mirror) => mirror.getField(nameSymbol).reflectee; | 389 valueIn(InstanceMirror mirror) => mirror.getField(nameSymbol).reflectee; |
| 390 | 390 |
| (...skipping 212 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 603 class Constructor { | 603 class Constructor { |
| 604 /** The mirror of the class we construct. */ | 604 /** The mirror of the class we construct. */ |
| 605 final ClassMirror type; | 605 final ClassMirror type; |
| 606 | 606 |
| 607 /** The name of the constructor to use, if not the default constructor.*/ | 607 /** The name of the constructor to use, if not the default constructor.*/ |
| 608 String name; | 608 String name; |
| 609 Symbol nameSymbol; | 609 Symbol nameSymbol; |
| 610 | 610 |
| 611 /** | 611 /** |
| 612 * The indices of the fields used as constructor arguments. We will look | 612 * The indices of the fields used as constructor arguments. We will look |
| 613 * these up in the state by number. These correspond to the index in the | 613 * these up in the state by number. The index is according to a list of the |
| 614 * [contents] of the FieldList, which will be alphabetically sorted. | 614 * fields in alphabetical order by name. |
| 615 */ | 615 */ |
| 616 List<int> fieldNumbers; | 616 List<int> fieldNumbers; |
| 617 | 617 |
| 618 /** | 618 /** |
| 619 * Creates a new constructor for the [type] with the constructor named [name] | 619 * Creates a new constructor for the [type] with the constructor named [name] |
| 620 * and the [fieldNumbers] of the constructor fields. | 620 * and the [fieldNumbers] of the constructor fields. |
| 621 */ | 621 */ |
| 622 Constructor(this.type, this.name, this.fieldNumbers) { | 622 Constructor(this.type, this.name, this.fieldNumbers) { |
| 623 if (name == null) name = ''; | 623 if (name == null) name = ''; |
| 624 nameSymbol = new Symbol(name); | 624 nameSymbol = new Symbol(name); |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 673 if (value is String) { | 673 if (value is String) { |
| 674 try { | 674 try { |
| 675 return new Symbol(value); | 675 return new Symbol(value); |
| 676 } on ArgumentError { | 676 } on ArgumentError { |
| 677 return null; | 677 return null; |
| 678 }; | 678 }; |
| 679 } else { | 679 } else { |
| 680 return null; | 680 return null; |
| 681 } | 681 } |
| 682 } | 682 } |
| OLD | NEW |