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 '../elements/elements.dart'; | 7 import '../elements/elements.dart'; |
8 import '../constants/expressions.dart'; | 8 import '../constants/expressions.dart'; |
9 import '../dart_types.dart'; | 9 import '../dart_types.dart'; |
10 import '../util/enumset.dart'; | 10 import '../util/enumset.dart'; |
(...skipping 862 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
873 void onElement(Element element, ObjectDecoder getDecoder(String tag)) {} | 873 void onElement(Element element, ObjectDecoder getDecoder(String tag)) {} |
874 | 874 |
875 /// Called to deserialize custom data from [decoder]. | 875 /// Called to deserialize custom data from [decoder]. |
876 dynamic onData(ObjectDecoder decoder) {} | 876 dynamic onData(ObjectDecoder decoder) {} |
877 } | 877 } |
878 | 878 |
879 /// Context for parallel deserialization. | 879 /// Context for parallel deserialization. |
880 class DeserializationContext { | 880 class DeserializationContext { |
881 Map<Uri, LibraryElement> _uriMap = <Uri, LibraryElement>{}; | 881 Map<Uri, LibraryElement> _uriMap = <Uri, LibraryElement>{}; |
882 List<Deserializer> deserializers = <Deserializer>[]; | 882 List<Deserializer> deserializers = <Deserializer>[]; |
| 883 List<DeserializerPlugin> plugins = <DeserializerPlugin>[]; |
883 | 884 |
884 LibraryElement lookupLibrary(Uri uri) { | 885 LibraryElement lookupLibrary(Uri uri) { |
885 return _uriMap.putIfAbsent(uri, () { | 886 return _uriMap.putIfAbsent(uri, () { |
886 for (Deserializer deserializer in deserializers) { | 887 for (Deserializer deserializer in deserializers) { |
887 LibraryElement library = deserializer.lookupLibrary(uri); | 888 LibraryElement library = deserializer.lookupLibrary(uri); |
888 if (library != null) { | 889 if (library != null) { |
889 return library; | 890 return library; |
890 } | 891 } |
891 } | 892 } |
892 return null; | 893 return null; |
893 }); | 894 }); |
894 } | 895 } |
895 } | 896 } |
896 | 897 |
897 /// Deserializer for a closed collection of libraries. | 898 /// Deserializer for a closed collection of libraries. |
898 // TODO(johnniwinther): Support per-library deserialization and dependencies | 899 // TODO(johnniwinther): Support per-library deserialization and dependencies |
899 // between deserialized subcomponent. | 900 // between deserialized subcomponent. |
900 class Deserializer { | 901 class Deserializer { |
901 final DeserializationContext context; | 902 final DeserializationContext context; |
902 final SerializationDecoder decoder; | 903 final SerializationDecoder decoder; |
903 List<DeserializerPlugin> plugins = <DeserializerPlugin>[]; | |
904 ObjectDecoder _headerObject; | 904 ObjectDecoder _headerObject; |
905 ListDecoder _elementList; | 905 ListDecoder _elementList; |
906 ListDecoder _typeList; | 906 ListDecoder _typeList; |
907 ListDecoder _constantList; | 907 ListDecoder _constantList; |
908 Map<int, Element> _elementMap = {}; | 908 Map<int, Element> _elementMap = {}; |
909 Map<int, DartType> _typeMap = {}; | 909 Map<int, DartType> _typeMap = {}; |
910 Map<int, ConstantExpression> _constantMap = {}; | 910 Map<int, ConstantExpression> _constantMap = {}; |
911 | 911 |
912 Deserializer.fromText(this.context, String text, this.decoder) { | 912 Deserializer.fromText(this.context, String text, this.decoder) { |
913 _headerObject = new ObjectDecoder(this, decoder.decode(text)); | 913 _headerObject = new ObjectDecoder(this, decoder.decode(text)); |
914 context.deserializers.add(this); | |
915 } | 914 } |
916 | 915 |
917 /// Returns the [ListDecoder] for the [Element]s in this deserializer. | 916 /// Returns the [ListDecoder] for the [Element]s in this deserializer. |
918 ListDecoder get elements { | 917 ListDecoder get elements { |
919 if (_elementList == null) { | 918 if (_elementList == null) { |
920 _elementList = _headerObject.getList(Key.ELEMENTS); | 919 _elementList = _headerObject.getList(Key.ELEMENTS); |
921 } | 920 } |
922 return _elementList; | 921 return _elementList; |
923 } | 922 } |
924 | 923 |
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
993 throw new StateError("Missing constructor for $name in $cls."); | 992 throw new StateError("Missing constructor for $name in $cls."); |
994 } | 993 } |
995 } else { | 994 } else { |
996 element = ElementDeserializer.deserialize(decoder, elementKind); | 995 element = ElementDeserializer.deserialize(decoder, elementKind); |
997 } | 996 } |
998 _elementMap[id] = element; | 997 _elementMap[id] = element; |
999 | 998 |
1000 MapDecoder pluginData = decoder.getMap(Key.DATA, isOptional: true); | 999 MapDecoder pluginData = decoder.getMap(Key.DATA, isOptional: true); |
1001 // Call plugins even when there is no data, so they can take action in | 1000 // Call plugins even when there is no data, so they can take action in |
1002 // this case. | 1001 // this case. |
1003 for (DeserializerPlugin plugin in plugins) { | 1002 for (DeserializerPlugin plugin in context.plugins) { |
1004 plugin.onElement(element, | 1003 plugin.onElement(element, |
1005 (String tag) => pluginData?.getObject(tag, isOptional: true)); | 1004 (String tag) => pluginData?.getObject(tag, isOptional: true)); |
1006 } | 1005 } |
1007 } | 1006 } |
1008 return element; | 1007 return element; |
1009 } | 1008 } |
1010 | 1009 |
1011 /// Returns the deserialized [DartType] for [id]. | 1010 /// Returns the deserialized [DartType] for [id]. |
1012 DartType deserializeType(int id) { | 1011 DartType deserializeType(int id) { |
1013 if (id == null) throw new ArgumentError('Deserializer.getType(null)'); | 1012 if (id == null) throw new ArgumentError('Deserializer.getType(null)'); |
(...skipping 26 matching lines...) Expand all Loading... |
1040 | 1039 |
1041 /// Returns the value used to store [key] as a property in the encoding an | 1040 /// Returns the value used to store [key] as a property in the encoding an |
1042 /// [ObjectValue]. | 1041 /// [ObjectValue]. |
1043 /// | 1042 /// |
1044 /// Different encodings have different restrictions and capabilities as how | 1043 /// Different encodings have different restrictions and capabilities as how |
1045 /// to store a [Key] value. For instance: A JSON encoding needs to convert | 1044 /// to store a [Key] value. For instance: A JSON encoding needs to convert |
1046 /// [Key] to a [String] to store it in a JSON object; a Dart encoding can | 1045 /// [Key] to a [String] to store it in a JSON object; a Dart encoding can |
1047 /// choose to store a [Key] as an [int] or as the [Key] itself. | 1046 /// choose to store a [Key] as an [int] or as the [Key] itself. |
1048 getObjectPropertyValue(Key key); | 1047 getObjectPropertyValue(Key key); |
1049 } | 1048 } |
OLD | NEW |