OLD | NEW |
1 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2016, 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 js_backend.serialization; | 5 library js_backend.serialization; |
6 | 6 |
7 import '../common/backend_api.dart' show BackendSerialization; | 7 import '../common/backend_api.dart' show BackendSerialization; |
| 8 import '../dart_types.dart'; |
8 import '../elements/elements.dart'; | 9 import '../elements/elements.dart'; |
| 10 import '../js/js.dart' as js; |
| 11 import '../native/native.dart'; |
9 import '../serialization/serialization.dart' | 12 import '../serialization/serialization.dart' |
10 show DeserializerPlugin, ObjectDecoder, ObjectEncoder, SerializerPlugin; | 13 show DeserializerPlugin, ObjectDecoder, ObjectEncoder, SerializerPlugin; |
11 import '../serialization/keys.dart'; | 14 import '../serialization/keys.dart'; |
| 15 import '../universe/side_effects.dart'; |
12 import 'js_backend.dart'; | 16 import 'js_backend.dart'; |
13 | 17 |
14 const String _BACKEND_DATA_TAG = 'jsBackendData'; | 18 const String _BACKEND_DATA_TAG = 'jsBackendData'; |
| 19 const Key DART_TYPES_RETURNED = const Key('dartTypesReturned'); |
| 20 const Key SPECIAL_TYPES_RETURNED = const Key('specialTypesReturned'); |
| 21 const Key DART_TYPES_INSTANTIATED = const Key('dartTypesInstantiated'); |
| 22 const Key SPECIAL_TYPES_INSTANTIATED = const Key('specialTypesInstantiated'); |
| 23 const Key CODE_TEMPLATE = const Key('codeTemplate'); |
| 24 const Key SIDE_EFFECTS = const Key('sideEffects'); |
| 25 const Key THROW_BEHAVIOR = const Key('throwBehavior'); |
| 26 const Key IS_ALLOCATION = const Key('isAllocation'); |
| 27 const Key USE_GVN = const Key('useGvn'); |
15 | 28 |
16 class JavaScriptBackendSerialization implements BackendSerialization { | 29 class JavaScriptBackendSerialization implements BackendSerialization { |
17 final JavaScriptBackendSerializer serializer; | 30 final JavaScriptBackendSerializer serializer; |
18 final JavaScriptBackendDeserializer deserializer; | 31 final JavaScriptBackendDeserializer deserializer; |
19 | 32 |
20 JavaScriptBackendSerialization(JavaScriptBackend backend) | 33 JavaScriptBackendSerialization(JavaScriptBackend backend) |
21 : serializer = new JavaScriptBackendSerializer(backend), | 34 : serializer = new JavaScriptBackendSerializer(backend), |
22 deserializer = new JavaScriptBackendDeserializer(backend); | 35 deserializer = new JavaScriptBackendDeserializer(backend); |
23 } | 36 } |
24 | 37 |
25 class JavaScriptBackendSerializer implements SerializerPlugin { | 38 class JavaScriptBackendSerializer implements SerializerPlugin { |
26 final JavaScriptBackend backend; | 39 final JavaScriptBackend backend; |
27 | 40 |
28 JavaScriptBackendSerializer(this.backend); | 41 JavaScriptBackendSerializer(this.backend); |
29 | 42 |
30 @override | 43 @override |
31 void onElement(Element element, ObjectEncoder createEncoder(String tag)) { | 44 void onElement(Element element, ObjectEncoder createEncoder(String tag)) { |
32 // TODO(johnniwinther): Add more data, e.g. js-interop names, native tags, | 45 // TODO(johnniwinther): Add more data, e.g. js-interop names, native tags, |
33 // etc. | 46 // etc. |
34 String nativeName = backend.nativeData.nativeMemberName[element]; | 47 String nativeName = backend.nativeData.nativeMemberName[element]; |
35 if (nativeName != null) { | 48 if (nativeName != null) { |
36 ObjectEncoder encoder = createEncoder(_BACKEND_DATA_TAG); | 49 ObjectEncoder encoder = createEncoder(_BACKEND_DATA_TAG); |
37 encoder.setString(Key.NAME, nativeName); | 50 encoder.setString(Key.NAME, nativeName); |
38 } | 51 } |
39 } | 52 } |
| 53 |
| 54 /// Returns a list of the [DartType]s in [types]. |
| 55 static List<DartType> filterDartTypes(List types) { |
| 56 return types.where((type) => type is DartType).toList(); |
| 57 } |
| 58 |
| 59 /// Returns a list of the names of the [SpecialType]s in [types]. |
| 60 static List<String> filterSpecialTypes(List types) { |
| 61 return types |
| 62 .where((type) => type is SpecialType) |
| 63 .map((SpecialType type) => type.name) |
| 64 .toList(); |
| 65 } |
| 66 |
| 67 @override |
| 68 void onData(NativeBehavior behavior, ObjectEncoder encoder) { |
| 69 encoder.setTypes( |
| 70 DART_TYPES_RETURNED, filterDartTypes(behavior.typesReturned)); |
| 71 encoder.setStrings( |
| 72 SPECIAL_TYPES_RETURNED, filterSpecialTypes(behavior.typesReturned)); |
| 73 |
| 74 encoder.setTypes( |
| 75 DART_TYPES_INSTANTIATED, filterDartTypes(behavior.typesInstantiated)); |
| 76 encoder.setStrings(SPECIAL_TYPES_INSTANTIATED, |
| 77 filterSpecialTypes(behavior.typesInstantiated)); |
| 78 |
| 79 if (behavior.codeTemplateText != null) { |
| 80 encoder.setString(CODE_TEMPLATE, behavior.codeTemplateText); |
| 81 } |
| 82 |
| 83 encoder.setInt(SIDE_EFFECTS, behavior.sideEffects.flags); |
| 84 encoder.setEnum(THROW_BEHAVIOR, behavior.throwBehavior); |
| 85 encoder.setBool(IS_ALLOCATION, behavior.isAllocation); |
| 86 encoder.setBool(USE_GVN, behavior.useGvn); |
| 87 } |
40 } | 88 } |
41 | 89 |
42 class JavaScriptBackendDeserializer implements DeserializerPlugin { | 90 class JavaScriptBackendDeserializer implements DeserializerPlugin { |
43 final JavaScriptBackend backend; | 91 final JavaScriptBackend backend; |
44 | 92 |
45 JavaScriptBackendDeserializer(this.backend); | 93 JavaScriptBackendDeserializer(this.backend); |
46 | 94 |
47 @override | 95 @override |
48 void onElement(Element element, ObjectDecoder getDecoder(String tag)) { | 96 void onElement(Element element, ObjectDecoder getDecoder(String tag)) { |
49 ObjectDecoder decoder = getDecoder(_BACKEND_DATA_TAG); | 97 ObjectDecoder decoder = getDecoder(_BACKEND_DATA_TAG); |
50 if (decoder != null) { | 98 if (decoder != null) { |
51 String nativeName = decoder.getString(Key.NAME); | 99 String nativeName = decoder.getString(Key.NAME); |
52 backend.nativeData.nativeMemberName[element] = nativeName; | 100 backend.nativeData.nativeMemberName[element] = nativeName; |
53 } | 101 } |
54 } | 102 } |
| 103 |
| 104 @override |
| 105 NativeBehavior onData(ObjectDecoder decoder) { |
| 106 SideEffects sideEffects = |
| 107 new SideEffects.fromFlags(decoder.getInt(SIDE_EFFECTS)); |
| 108 NativeBehavior behavior = new NativeBehavior.internal(sideEffects); |
| 109 |
| 110 behavior.typesReturned |
| 111 .addAll(decoder.getTypes(DART_TYPES_RETURNED, isOptional: true)); |
| 112 behavior.typesReturned.addAll(decoder |
| 113 .getStrings(SPECIAL_TYPES_RETURNED, isOptional: true) |
| 114 .map(SpecialType.fromName)); |
| 115 |
| 116 behavior.typesReturned |
| 117 .addAll(decoder.getTypes(DART_TYPES_INSTANTIATED, isOptional: true)); |
| 118 behavior.typesReturned.addAll(decoder |
| 119 .getStrings(SPECIAL_TYPES_INSTANTIATED, isOptional: true) |
| 120 .map(SpecialType.fromName)); |
| 121 |
| 122 behavior.codeTemplateText = |
| 123 decoder.getString(CODE_TEMPLATE, isOptional: true); |
| 124 if (behavior.codeTemplateText != null) { |
| 125 behavior.codeTemplate = js.js.parseForeignJS(behavior.codeTemplateText); |
| 126 } |
| 127 |
| 128 behavior.throwBehavior = |
| 129 decoder.getEnum(THROW_BEHAVIOR, NativeThrowBehavior.values); |
| 130 behavior.isAllocation = decoder.getBool(IS_ALLOCATION); |
| 131 behavior.useGvn = decoder.getBool(USE_GVN); |
| 132 return behavior; |
| 133 } |
55 } | 134 } |
OLD | NEW |