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.dart'; | 7 import '../common.dart'; |
8 import '../common/backend_api.dart' show BackendSerialization; | 8 import '../common/backend_api.dart' show BackendSerialization; |
9 import '../dart_types.dart'; | 9 import '../dart_types.dart'; |
10 import '../elements/elements.dart'; | 10 import '../elements/elements.dart'; |
11 import '../js/js.dart' as js; | 11 import '../js/js.dart' as js; |
12 import '../native/native.dart'; | 12 import '../native/native.dart'; |
13 import '../serialization/serialization.dart' | 13 import '../serialization/serialization.dart' |
14 show DeserializerPlugin, ObjectDecoder, ObjectEncoder, SerializerPlugin; | 14 show DeserializerPlugin, ObjectDecoder, ObjectEncoder, SerializerPlugin; |
15 import '../serialization/keys.dart'; | 15 import '../serialization/keys.dart'; |
16 import '../universe/side_effects.dart'; | 16 import '../universe/side_effects.dart'; |
17 import 'js_backend.dart'; | 17 import 'js_backend.dart'; |
18 | 18 |
19 const String _BACKEND_DATA_TAG = 'jsBackendData'; | 19 const String _BACKEND_DATA_TAG = 'jsBackendData'; |
20 const Key DART_TYPES_RETURNED = const Key('dartTypesReturned'); | 20 const Key DART_TYPES_RETURNED = const Key('dartTypesReturned'); |
| 21 const Key THIS_TYPES_RETURNED = const Key('thisTypesReturned'); |
21 const Key SPECIAL_TYPES_RETURNED = const Key('specialTypesReturned'); | 22 const Key SPECIAL_TYPES_RETURNED = const Key('specialTypesReturned'); |
22 const Key DART_TYPES_INSTANTIATED = const Key('dartTypesInstantiated'); | 23 const Key DART_TYPES_INSTANTIATED = const Key('dartTypesInstantiated'); |
| 24 const Key THIS_TYPES_INSTANTIATED = const Key('thisTypesInstantiated'); |
23 const Key SPECIAL_TYPES_INSTANTIATED = const Key('specialTypesInstantiated'); | 25 const Key SPECIAL_TYPES_INSTANTIATED = const Key('specialTypesInstantiated'); |
24 const Key CODE_TEMPLATE = const Key('codeTemplate'); | 26 const Key CODE_TEMPLATE = const Key('codeTemplate'); |
25 const Key SIDE_EFFECTS = const Key('sideEffects'); | 27 const Key SIDE_EFFECTS = const Key('sideEffects'); |
26 const Key THROW_BEHAVIOR = const Key('throwBehavior'); | 28 const Key THROW_BEHAVIOR = const Key('throwBehavior'); |
27 const Key IS_ALLOCATION = const Key('isAllocation'); | 29 const Key IS_ALLOCATION = const Key('isAllocation'); |
28 const Key USE_GVN = const Key('useGvn'); | 30 const Key USE_GVN = const Key('useGvn'); |
29 | 31 |
30 class JavaScriptBackendSerialization implements BackendSerialization { | 32 class JavaScriptBackendSerialization implements BackendSerialization { |
31 final JavaScriptBackendSerializer serializer; | 33 final JavaScriptBackendSerializer serializer; |
32 final JavaScriptBackendDeserializer deserializer; | 34 final JavaScriptBackendDeserializer deserializer; |
(...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
143 } | 145 } |
144 } | 146 } |
145 | 147 |
146 @override | 148 @override |
147 NativeBehavior onData(ObjectDecoder decoder) { | 149 NativeBehavior onData(ObjectDecoder decoder) { |
148 return NativeBehaviorSerialization.deserializeNativeBehavior(decoder); | 150 return NativeBehaviorSerialization.deserializeNativeBehavior(decoder); |
149 } | 151 } |
150 } | 152 } |
151 | 153 |
152 class NativeBehaviorSerialization { | 154 class NativeBehaviorSerialization { |
153 /// Returns a list of the [DartType]s in [types]. | 155 static const int NORMAL_TYPE = 0; |
| 156 static const int THIS_TYPE = 1; |
| 157 static const int SPECIAL_TYPE = 2; |
| 158 |
| 159 static int getTypeKind(var type) { |
| 160 if (type is DartType) { |
| 161 // TODO(johnniwinther): Remove this when annotation are no longer resolved |
| 162 // to this-types. |
| 163 if (type is GenericType && |
| 164 type.isGeneric && |
| 165 type == type.element.thisType) { |
| 166 return THIS_TYPE; |
| 167 } |
| 168 return NORMAL_TYPE; |
| 169 } |
| 170 return SPECIAL_TYPE; |
| 171 } |
| 172 |
| 173 /// Returns a list of the non-this-type [DartType]s in [types]. |
154 static List<DartType> filterDartTypes(List types) { | 174 static List<DartType> filterDartTypes(List types) { |
155 return types.where((type) => type is DartType).toList(); | 175 return types.where((type) => getTypeKind(type) == NORMAL_TYPE).toList(); |
| 176 } |
| 177 |
| 178 // TODO(johnniwinther): Remove this when annotation are no longer resolved |
| 179 // to this-types. |
| 180 /// Returns a list of the classes of this-types in [types]. |
| 181 static List<Element> filterThisTypes(List types) { |
| 182 return types |
| 183 .where((type) => getTypeKind(type) == THIS_TYPE) |
| 184 .map((type) => type.element) |
| 185 .toList(); |
156 } | 186 } |
157 | 187 |
158 /// Returns a list of the names of the [SpecialType]s in [types]. | 188 /// Returns a list of the names of the [SpecialType]s in [types]. |
159 static List<String> filterSpecialTypes(List types) { | 189 static List<String> filterSpecialTypes(List types) { |
160 return types | 190 return types |
161 .where((type) => type is SpecialType) | 191 .where((type) => getTypeKind(type) == SPECIAL_TYPE) |
162 .map((SpecialType type) => type.name) | 192 .map((SpecialType type) => type.name) |
163 .toList(); | 193 .toList(); |
164 } | 194 } |
165 | 195 |
166 static void serializeNativeBehavior( | 196 static void serializeNativeBehavior( |
167 NativeBehavior behavior, ObjectEncoder encoder) { | 197 NativeBehavior behavior, ObjectEncoder encoder) { |
168 encoder.setTypes( | 198 encoder.setTypes( |
169 DART_TYPES_RETURNED, filterDartTypes(behavior.typesReturned)); | 199 DART_TYPES_RETURNED, filterDartTypes(behavior.typesReturned)); |
| 200 encoder.setElements( |
| 201 THIS_TYPES_RETURNED, filterThisTypes(behavior.typesReturned)); |
170 encoder.setStrings( | 202 encoder.setStrings( |
171 SPECIAL_TYPES_RETURNED, filterSpecialTypes(behavior.typesReturned)); | 203 SPECIAL_TYPES_RETURNED, filterSpecialTypes(behavior.typesReturned)); |
172 | 204 |
173 encoder.setTypes( | 205 encoder.setTypes( |
174 DART_TYPES_INSTANTIATED, filterDartTypes(behavior.typesInstantiated)); | 206 DART_TYPES_INSTANTIATED, filterDartTypes(behavior.typesInstantiated)); |
| 207 encoder.setElements( |
| 208 THIS_TYPES_INSTANTIATED, filterThisTypes(behavior.typesInstantiated)); |
175 encoder.setStrings(SPECIAL_TYPES_INSTANTIATED, | 209 encoder.setStrings(SPECIAL_TYPES_INSTANTIATED, |
176 filterSpecialTypes(behavior.typesInstantiated)); | 210 filterSpecialTypes(behavior.typesInstantiated)); |
177 | 211 |
178 if (behavior.codeTemplateText != null) { | 212 if (behavior.codeTemplateText != null) { |
179 encoder.setString(CODE_TEMPLATE, behavior.codeTemplateText); | 213 encoder.setString(CODE_TEMPLATE, behavior.codeTemplateText); |
180 } | 214 } |
181 | 215 |
182 encoder.setInt(SIDE_EFFECTS, behavior.sideEffects.flags); | 216 encoder.setInt(SIDE_EFFECTS, behavior.sideEffects.flags); |
183 encoder.setEnum(THROW_BEHAVIOR, behavior.throwBehavior); | 217 encoder.setEnum(THROW_BEHAVIOR, behavior.throwBehavior); |
184 encoder.setBool(IS_ALLOCATION, behavior.isAllocation); | 218 encoder.setBool(IS_ALLOCATION, behavior.isAllocation); |
185 encoder.setBool(USE_GVN, behavior.useGvn); | 219 encoder.setBool(USE_GVN, behavior.useGvn); |
186 } | 220 } |
187 | 221 |
188 static NativeBehavior deserializeNativeBehavior(ObjectDecoder decoder) { | 222 static NativeBehavior deserializeNativeBehavior(ObjectDecoder decoder) { |
189 SideEffects sideEffects = | 223 SideEffects sideEffects = |
190 new SideEffects.fromFlags(decoder.getInt(SIDE_EFFECTS)); | 224 new SideEffects.fromFlags(decoder.getInt(SIDE_EFFECTS)); |
191 NativeBehavior behavior = new NativeBehavior.internal(sideEffects); | 225 NativeBehavior behavior = new NativeBehavior.internal(sideEffects); |
192 | 226 |
193 behavior.typesReturned | 227 behavior.typesReturned |
194 .addAll(decoder.getTypes(DART_TYPES_RETURNED, isOptional: true)); | 228 .addAll(decoder.getTypes(DART_TYPES_RETURNED, isOptional: true)); |
195 behavior.typesReturned.addAll(decoder | 229 behavior.typesReturned.addAll(decoder |
| 230 .getElements(THIS_TYPES_RETURNED, isOptional: true) |
| 231 .map((element) => element.thisType) |
| 232 .toList()); |
| 233 behavior.typesReturned.addAll(decoder |
196 .getStrings(SPECIAL_TYPES_RETURNED, isOptional: true) | 234 .getStrings(SPECIAL_TYPES_RETURNED, isOptional: true) |
197 .map(SpecialType.fromName)); | 235 .map(SpecialType.fromName)); |
198 | 236 |
199 behavior.typesInstantiated | 237 behavior.typesInstantiated |
200 .addAll(decoder.getTypes(DART_TYPES_INSTANTIATED, isOptional: true)); | 238 .addAll(decoder.getTypes(DART_TYPES_INSTANTIATED, isOptional: true)); |
201 behavior.typesInstantiated.addAll(decoder | 239 behavior.typesInstantiated.addAll(decoder |
| 240 .getElements(THIS_TYPES_INSTANTIATED, isOptional: true) |
| 241 .map((element) => element.thisType) |
| 242 .toList()); |
| 243 behavior.typesInstantiated.addAll(decoder |
202 .getStrings(SPECIAL_TYPES_INSTANTIATED, isOptional: true) | 244 .getStrings(SPECIAL_TYPES_INSTANTIATED, isOptional: true) |
203 .map(SpecialType.fromName)); | 245 .map(SpecialType.fromName)); |
204 | 246 |
205 behavior.codeTemplateText = | 247 behavior.codeTemplateText = |
206 decoder.getString(CODE_TEMPLATE, isOptional: true); | 248 decoder.getString(CODE_TEMPLATE, isOptional: true); |
207 if (behavior.codeTemplateText != null) { | 249 if (behavior.codeTemplateText != null) { |
208 behavior.codeTemplate = js.js.parseForeignJS(behavior.codeTemplateText); | 250 behavior.codeTemplate = js.js.parseForeignJS(behavior.codeTemplateText); |
209 } | 251 } |
210 | 252 |
211 behavior.throwBehavior = | 253 behavior.throwBehavior = |
212 decoder.getEnum(THROW_BEHAVIOR, NativeThrowBehavior.values); | 254 decoder.getEnum(THROW_BEHAVIOR, NativeThrowBehavior.values); |
213 behavior.isAllocation = decoder.getBool(IS_ALLOCATION); | 255 behavior.isAllocation = decoder.getBool(IS_ALLOCATION); |
214 behavior.useGvn = decoder.getBool(USE_GVN); | 256 behavior.useGvn = decoder.getBool(USE_GVN); |
215 return behavior; | 257 return behavior; |
216 } | 258 } |
217 } | 259 } |
OLD | NEW |