| OLD | NEW |
| 1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2015, 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 library dart2js.serialization; | 5 library dart2js.serialization; |
| 6 | 6 |
| 7 import 'package:front_end/src/scanner/token.dart' show TokenType; | 7 import 'package:front_end/src/scanner/token.dart' show TokenType; |
| 8 | 8 |
| 9 import '../common.dart'; | 9 import '../common.dart'; |
| 10 import '../common/resolution.dart'; | 10 import '../common/resolution.dart'; |
| (...skipping 244 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 255 List<Value> list = <Value>[]; | 255 List<Value> list = <Value>[]; |
| 256 _map[key] = new ListValue(list); | 256 _map[key] = new ListValue(list); |
| 257 return new ListEncoder(_serializer, list); | 257 return new ListEncoder(_serializer, list); |
| 258 } | 258 } |
| 259 | 259 |
| 260 String toString() => _map.toString(); | 260 String toString() => _map.toString(); |
| 261 } | 261 } |
| 262 | 262 |
| 263 /// [ObjectDecoder] reads serialized values from a [Map] encoded from an | 263 /// [ObjectDecoder] reads serialized values from a [Map] encoded from an |
| 264 /// [ObjectValue] where properties are stored using [Key] values as keys. | 264 /// [ObjectValue] where properties are stored using [Key] values as keys. |
| 265 class ObjectDecoder extends AbstractDecoder<Key> { | 265 class ObjectDecoder extends AbstractDecoder<dynamic, Key> { |
| 266 /// Creates an [ObjectDecoder] that decodes [map] into deserialized values | 266 /// Creates an [ObjectDecoder] that decodes [map] into deserialized values |
| 267 /// using [deserializer] to create canonicalized values. | 267 /// using [deserializer] to create canonicalized values. |
| 268 ObjectDecoder(Deserializer deserializer, Map map) : super(deserializer, map); | 268 ObjectDecoder(Deserializer deserializer, Map map) : super(deserializer, map); |
| 269 | 269 |
| 270 @override | 270 @override |
| 271 _getKeyValue(Key key) => _deserializer.decoder.getObjectPropertyValue(key); | 271 _getKeyValue(Key key) => _deserializer.decoder.getObjectPropertyValue(key); |
| 272 } | 272 } |
| 273 | 273 |
| 274 /// [MapDecoder] reads serialized values from a [Map] encoded from an | 274 /// [MapDecoder] reads serialized values from a [Map] encoded from an |
| 275 /// [MapValue] where entries are stored using [String] values as keys. | 275 /// [MapValue] where entries are stored using [String] values as keys. |
| 276 class MapDecoder extends AbstractDecoder<String> { | 276 class MapDecoder extends AbstractDecoder<String, String> { |
| 277 /// Creates an [MapDecoder] that decodes [map] into deserialized values | 277 /// Creates an [MapDecoder] that decodes [map] into deserialized values |
| 278 /// using [deserializer] to create canonicalized values. | 278 /// using [deserializer] to create canonicalized values. |
| 279 MapDecoder(Deserializer deserializer, Map<String, dynamic> map) | 279 MapDecoder(Deserializer deserializer, Map<String, dynamic> map) |
| 280 : super(deserializer, map); | 280 : super(deserializer, map); |
| 281 | 281 |
| 282 @override | 282 @override |
| 283 _getKeyValue(String key) => key; | 283 String _getKeyValue(String key) => key; |
| 284 | 284 |
| 285 /// Applies [f] to every key in the decoded [Map]. | 285 /// Applies [f] to every key in the decoded [Map]. |
| 286 void forEachKey(f(String key)) { | 286 void forEachKey(f(String key)) { |
| 287 _map.keys.forEach(f); | 287 _map.keys.forEach(f); |
| 288 } | 288 } |
| 289 } | 289 } |
| 290 | 290 |
| 291 /// [ListDecoder] reads serialized map or object values from a [List]. | 291 /// [ListDecoder] reads serialized map or object values from a [List]. |
| 292 class ListDecoder { | 292 class ListDecoder { |
| 293 final Deserializer _deserializer; | 293 final Deserializer _deserializer; |
| (...skipping 12 matching lines...) Expand all Loading... |
| 306 return new ObjectDecoder(_deserializer, _list[index]); | 306 return new ObjectDecoder(_deserializer, _list[index]); |
| 307 } | 307 } |
| 308 | 308 |
| 309 /// Returns an [MapDecoder] for the [index]th map value in the decoded list. | 309 /// Returns an [MapDecoder] for the [index]th map value in the decoded list. |
| 310 MapDecoder getMap(int index) { | 310 MapDecoder getMap(int index) { |
| 311 return new MapDecoder(_deserializer, _list[index]); | 311 return new MapDecoder(_deserializer, _list[index]); |
| 312 } | 312 } |
| 313 } | 313 } |
| 314 | 314 |
| 315 /// Abstract base implementation for [ObjectDecoder] and [MapDecoder]. | 315 /// Abstract base implementation for [ObjectDecoder] and [MapDecoder]. |
| 316 abstract class AbstractDecoder<K> { | 316 abstract class AbstractDecoder<M, K> { |
| 317 final Deserializer _deserializer; | 317 final Deserializer _deserializer; |
| 318 final Map<K, dynamic> _map; | 318 final Map<M, dynamic> _map; |
| 319 | 319 |
| 320 AbstractDecoder(this._deserializer, this._map) { | 320 AbstractDecoder(this._deserializer, this._map) { |
| 321 assert(_deserializer != null); | 321 assert(_deserializer != null); |
| 322 assert(_map != null); | 322 assert(_map != null); |
| 323 } | 323 } |
| 324 | 324 |
| 325 /// Returns the value for [key] defined by the [SerializationDecoder] in used | 325 /// Returns the value for [key] defined by the [SerializationDecoder] in used |
| 326 /// [_deserializer]. | 326 /// [_deserializer]. |
| 327 _getKeyValue(K key); | 327 M _getKeyValue(K key); |
| 328 | 328 |
| 329 /// Returns `true` if [key] has an associated value in the decoded object. | 329 /// Returns `true` if [key] has an associated value in the decoded object. |
| 330 bool containsKey(K key) => _map.containsKey(_getKeyValue(key)); | 330 bool containsKey(K key) => _map.containsKey(_getKeyValue(key)); |
| 331 | 331 |
| 332 /// Returns the enum value from the [enumValues] associated with [key] in the | 332 /// Returns the enum value from the [enumValues] associated with [key] in the |
| 333 /// decoded object. | 333 /// decoded object. |
| 334 /// | 334 /// |
| 335 /// If no value is associated with [key], then if [isOptional] is `true`, | 335 /// If no value is associated with [key], then if [isOptional] is `true`, |
| 336 /// [defaultValue] is returned, otherwise an exception is thrown. | 336 /// [defaultValue] is returned, otherwise an exception is thrown. |
| 337 getEnum(K key, List enumValues, {bool isOptional: false, defaultValue}) { | 337 getEnum(K key, List enumValues, {bool isOptional: false, defaultValue}) { |
| (...skipping 813 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1151 | 1151 |
| 1152 /// Returns the value used to store [key] as a property in the encoding an | 1152 /// Returns the value used to store [key] as a property in the encoding an |
| 1153 /// [ObjectValue]. | 1153 /// [ObjectValue]. |
| 1154 /// | 1154 /// |
| 1155 /// Different encodings have different restrictions and capabilities as how | 1155 /// Different encodings have different restrictions and capabilities as how |
| 1156 /// to store a [Key] value. For instance: A JSON encoding needs to convert | 1156 /// to store a [Key] value. For instance: A JSON encoding needs to convert |
| 1157 /// [Key] to a [String] to store it in a JSON object; a Dart encoding can | 1157 /// [Key] to a [String] to store it in a JSON object; a Dart encoding can |
| 1158 /// choose to store a [Key] as an [int] or as the [Key] itself. | 1158 /// choose to store a [Key] as an [int] or as the [Key] itself. |
| 1159 getObjectPropertyValue(Key key); | 1159 getObjectPropertyValue(Key key); |
| 1160 } | 1160 } |
| OLD | NEW |