| OLD | NEW |
| (Empty) | |
| 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 |
| 3 // BSD-style license that can be found in the LICENSE file. |
| 4 |
| 5 library dart2js.serialization.impact; |
| 6 |
| 7 import '../dart_types.dart'; |
| 8 import '../elements/elements.dart'; |
| 9 import '../universe/call_structure.dart'; |
| 10 import '../universe/selector.dart'; |
| 11 import '../universe/world_impact.dart'; |
| 12 import '../universe/use.dart'; |
| 13 |
| 14 import 'keys.dart'; |
| 15 import 'serialization.dart'; |
| 16 |
| 17 /// Visitor that serializes a [WorldImpact] object using an [ObjectEncoder]. |
| 18 class ImpactSerializer implements WorldImpactVisitor { |
| 19 final ListEncoder staticUses; |
| 20 final ListEncoder dynamicUses; |
| 21 final ListEncoder typeUses; |
| 22 |
| 23 ImpactSerializer(ObjectEncoder objectEncoder) |
| 24 : staticUses = objectEncoder.createList(Key.STATIC_USES), |
| 25 dynamicUses = objectEncoder.createList(Key.DYNAMIC_USES), |
| 26 typeUses = objectEncoder.createList(Key.TYPE_USES); |
| 27 |
| 28 @override |
| 29 void visitDynamicUse(DynamicUse dynamicUse) { |
| 30 ObjectEncoder object = dynamicUses.createObject(); |
| 31 object.setEnum(Key.KIND, dynamicUse.selector.kind); |
| 32 |
| 33 object.setInt(Key.ARGUMENTS, |
| 34 dynamicUse.selector.callStructure.argumentCount); |
| 35 object.setStrings(Key.NAMED_ARGUMENTS, |
| 36 dynamicUse.selector.callStructure.namedArguments); |
| 37 |
| 38 object.setString(Key.NAME, |
| 39 dynamicUse.selector.memberName.text); |
| 40 object.setBool(Key.IS_SETTER, |
| 41 dynamicUse.selector.memberName.isSetter); |
| 42 if (dynamicUse.selector.memberName.library != null) { |
| 43 object.setElement(Key.LIBRARY, |
| 44 dynamicUse.selector.memberName.library); |
| 45 } |
| 46 } |
| 47 |
| 48 @override |
| 49 void visitStaticUse(StaticUse staticUse) { |
| 50 ObjectEncoder object = staticUses.createObject(); |
| 51 object.setEnum(Key.KIND, staticUse.kind); |
| 52 object.setElement(Key.ELEMENT, staticUse.element); |
| 53 } |
| 54 |
| 55 @override |
| 56 void visitTypeUse(TypeUse typeUse) { |
| 57 ObjectEncoder object = typeUses.createObject(); |
| 58 object.setEnum(Key.KIND, typeUse.kind); |
| 59 object.setType(Key.TYPE, typeUse.type); |
| 60 } |
| 61 } |
| 62 |
| 63 /// A deserialized [WorldImpact] object. |
| 64 class DeserializedWorldImpact extends WorldImpact with WorldImpactBuilder {} |
| 65 |
| 66 class ImpactDeserializer { |
| 67 /// Deserializes a [WorldImpact] from [objectDecoder]. |
| 68 static WorldImpact deserializeImpact(ObjectDecoder objectDecoder) { |
| 69 DeserializedWorldImpact worldImpact = new DeserializedWorldImpact(); |
| 70 ListDecoder staticUses = objectDecoder.getList(Key.STATIC_USES); |
| 71 for (int index = 0; index < staticUses.length; index++) { |
| 72 ObjectDecoder object = staticUses.getObject(index); |
| 73 StaticUseKind kind = object.getEnum(Key.KIND, StaticUseKind.values); |
| 74 Element element = object.getElement(Key.ELEMENT); |
| 75 worldImpact.registerStaticUse(new StaticUse.internal(element, kind)); |
| 76 } |
| 77 ListDecoder dynamicUses = objectDecoder.getList(Key.DYNAMIC_USES); |
| 78 for (int index = 0; index < dynamicUses.length; index++) { |
| 79 ObjectDecoder object = dynamicUses.getObject(index); |
| 80 SelectorKind kind = object.getEnum(Key.KIND, SelectorKind.values); |
| 81 int argumentCount = object.getInt(Key.ARGUMENTS); |
| 82 List<String> namedArguments = |
| 83 object.getStrings(Key.NAMED_ARGUMENTS, isOptional: true); |
| 84 String name = object.getString(Key.NAME); |
| 85 bool isSetter = object.getBool(Key.IS_SETTER); |
| 86 LibraryElement library = object.getElement(Key.LIBRARY, isOptional: true); |
| 87 worldImpact.registerDynamicUse( |
| 88 new DynamicUse( |
| 89 new Selector( |
| 90 kind, |
| 91 new Name(name, library, isSetter: isSetter), |
| 92 new CallStructure(argumentCount, namedArguments)), |
| 93 null)); |
| 94 } |
| 95 ListDecoder typeUses = objectDecoder.getList(Key.TYPE_USES); |
| 96 for (int index = 0; index < typeUses.length; index++) { |
| 97 ObjectDecoder object = typeUses.getObject(index); |
| 98 TypeUseKind kind = object.getEnum(Key.KIND, TypeUseKind.values); |
| 99 DartType type = object.getType(Key.TYPE); |
| 100 worldImpact.registerTypeUse(new TypeUse.internal(type, kind)); |
| 101 } |
| 102 return worldImpact; |
| 103 } |
| 104 } |
| OLD | NEW |