Chromium Code Reviews| 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 /// Returns a list of the non-this-type [DartType]s in [types]. |
| 154 static List<DartType> filterDartTypes(List types) { | 156 static List<DartType> filterDartTypes(List types) { |
| 155 return types.where((type) => type is DartType).toList(); | 157 return types.where((type) { |
| 158 if (type is DartType) { | |
| 159 // TODO(johnniwinther): Remove this when annotation are no longer resolv ed | |
|
Harry Terkelsen
2016/07/15 16:03:10
nit: long line
Johnni Winther
2016/07/18 10:44:17
Done.
| |
| 160 // to this-types. | |
| 161 if (type is GenericType && | |
| 162 type.isGeneric && | |
| 163 type == type.element.thisType) { | |
| 164 return false; | |
| 165 } | |
| 166 return true; | |
| 167 } | |
| 168 return false; | |
| 169 }).toList(); | |
| 170 } | |
| 171 | |
| 172 // TODO(johnniwinther): Remove this when annotation are no longer resolved | |
| 173 // to this-types. | |
| 174 /// Returns a list of the classes of this-types in [types]. | |
| 175 static List<Element> filterThisTypes(List types) { | |
| 176 return types.where((type) { | |
|
Harry Terkelsen
2016/07/15 16:03:10
maybe move this filter function to it's own local
Johnni Winther
2016/07/18 10:44:17
Done.
| |
| 177 if (type is DartType) { | |
| 178 if (type is GenericType && | |
| 179 type.isGeneric && | |
| 180 type == type.element.thisType) { | |
| 181 return true; | |
| 182 } | |
| 183 } | |
| 184 return false; | |
| 185 }).map((type) { | |
| 186 return type.element; | |
| 187 }).toList(); | |
| 156 } | 188 } |
| 157 | 189 |
| 158 /// Returns a list of the names of the [SpecialType]s in [types]. | 190 /// Returns a list of the names of the [SpecialType]s in [types]. |
| 159 static List<String> filterSpecialTypes(List types) { | 191 static List<String> filterSpecialTypes(List types) { |
| 160 return types | 192 return types |
| 161 .where((type) => type is SpecialType) | 193 .where((type) => type is SpecialType) |
| 162 .map((SpecialType type) => type.name) | 194 .map((SpecialType type) => type.name) |
| 163 .toList(); | 195 .toList(); |
| 164 } | 196 } |
| 165 | 197 |
| 166 static void serializeNativeBehavior( | 198 static void serializeNativeBehavior( |
| 167 NativeBehavior behavior, ObjectEncoder encoder) { | 199 NativeBehavior behavior, ObjectEncoder encoder) { |
| 168 encoder.setTypes( | 200 encoder.setTypes( |
| 169 DART_TYPES_RETURNED, filterDartTypes(behavior.typesReturned)); | 201 DART_TYPES_RETURNED, filterDartTypes(behavior.typesReturned)); |
| 202 encoder.setElements( | |
| 203 THIS_TYPES_RETURNED, filterThisTypes(behavior.typesReturned)); | |
| 170 encoder.setStrings( | 204 encoder.setStrings( |
| 171 SPECIAL_TYPES_RETURNED, filterSpecialTypes(behavior.typesReturned)); | 205 SPECIAL_TYPES_RETURNED, filterSpecialTypes(behavior.typesReturned)); |
| 172 | 206 |
| 173 encoder.setTypes( | 207 encoder.setTypes( |
| 174 DART_TYPES_INSTANTIATED, filterDartTypes(behavior.typesInstantiated)); | 208 DART_TYPES_INSTANTIATED, filterDartTypes(behavior.typesInstantiated)); |
| 209 encoder.setElements( | |
| 210 THIS_TYPES_INSTANTIATED, filterThisTypes(behavior.typesInstantiated)); | |
| 175 encoder.setStrings(SPECIAL_TYPES_INSTANTIATED, | 211 encoder.setStrings(SPECIAL_TYPES_INSTANTIATED, |
| 176 filterSpecialTypes(behavior.typesInstantiated)); | 212 filterSpecialTypes(behavior.typesInstantiated)); |
| 177 | 213 |
| 178 if (behavior.codeTemplateText != null) { | 214 if (behavior.codeTemplateText != null) { |
| 179 encoder.setString(CODE_TEMPLATE, behavior.codeTemplateText); | 215 encoder.setString(CODE_TEMPLATE, behavior.codeTemplateText); |
| 180 } | 216 } |
| 181 | 217 |
| 182 encoder.setInt(SIDE_EFFECTS, behavior.sideEffects.flags); | 218 encoder.setInt(SIDE_EFFECTS, behavior.sideEffects.flags); |
| 183 encoder.setEnum(THROW_BEHAVIOR, behavior.throwBehavior); | 219 encoder.setEnum(THROW_BEHAVIOR, behavior.throwBehavior); |
| 184 encoder.setBool(IS_ALLOCATION, behavior.isAllocation); | 220 encoder.setBool(IS_ALLOCATION, behavior.isAllocation); |
| 185 encoder.setBool(USE_GVN, behavior.useGvn); | 221 encoder.setBool(USE_GVN, behavior.useGvn); |
| 186 } | 222 } |
| 187 | 223 |
| 188 static NativeBehavior deserializeNativeBehavior(ObjectDecoder decoder) { | 224 static NativeBehavior deserializeNativeBehavior(ObjectDecoder decoder) { |
| 189 SideEffects sideEffects = | 225 SideEffects sideEffects = |
| 190 new SideEffects.fromFlags(decoder.getInt(SIDE_EFFECTS)); | 226 new SideEffects.fromFlags(decoder.getInt(SIDE_EFFECTS)); |
| 191 NativeBehavior behavior = new NativeBehavior.internal(sideEffects); | 227 NativeBehavior behavior = new NativeBehavior.internal(sideEffects); |
| 192 | 228 |
| 193 behavior.typesReturned | 229 behavior.typesReturned |
| 194 .addAll(decoder.getTypes(DART_TYPES_RETURNED, isOptional: true)); | 230 .addAll(decoder.getTypes(DART_TYPES_RETURNED, isOptional: true)); |
| 195 behavior.typesReturned.addAll(decoder | 231 behavior.typesReturned.addAll(decoder |
| 232 .getElements(THIS_TYPES_RETURNED, isOptional: true) | |
| 233 .map((element) => element.thisType) | |
| 234 .toList()); | |
| 235 behavior.typesReturned.addAll(decoder | |
| 196 .getStrings(SPECIAL_TYPES_RETURNED, isOptional: true) | 236 .getStrings(SPECIAL_TYPES_RETURNED, isOptional: true) |
| 197 .map(SpecialType.fromName)); | 237 .map(SpecialType.fromName)); |
| 198 | 238 |
| 199 behavior.typesInstantiated | 239 behavior.typesInstantiated |
| 200 .addAll(decoder.getTypes(DART_TYPES_INSTANTIATED, isOptional: true)); | 240 .addAll(decoder.getTypes(DART_TYPES_INSTANTIATED, isOptional: true)); |
| 201 behavior.typesInstantiated.addAll(decoder | 241 behavior.typesInstantiated.addAll(decoder |
| 242 .getElements(THIS_TYPES_INSTANTIATED, isOptional: true) | |
| 243 .map((element) => element.thisType) | |
| 244 .toList()); | |
| 245 behavior.typesInstantiated.addAll(decoder | |
| 202 .getStrings(SPECIAL_TYPES_INSTANTIATED, isOptional: true) | 246 .getStrings(SPECIAL_TYPES_INSTANTIATED, isOptional: true) |
| 203 .map(SpecialType.fromName)); | 247 .map(SpecialType.fromName)); |
| 204 | 248 |
| 205 behavior.codeTemplateText = | 249 behavior.codeTemplateText = |
| 206 decoder.getString(CODE_TEMPLATE, isOptional: true); | 250 decoder.getString(CODE_TEMPLATE, isOptional: true); |
| 207 if (behavior.codeTemplateText != null) { | 251 if (behavior.codeTemplateText != null) { |
| 208 behavior.codeTemplate = js.js.parseForeignJS(behavior.codeTemplateText); | 252 behavior.codeTemplate = js.js.parseForeignJS(behavior.codeTemplateText); |
| 209 } | 253 } |
| 210 | 254 |
| 211 behavior.throwBehavior = | 255 behavior.throwBehavior = |
| 212 decoder.getEnum(THROW_BEHAVIOR, NativeThrowBehavior.values); | 256 decoder.getEnum(THROW_BEHAVIOR, NativeThrowBehavior.values); |
| 213 behavior.isAllocation = decoder.getBool(IS_ALLOCATION); | 257 behavior.isAllocation = decoder.getBool(IS_ALLOCATION); |
| 214 behavior.useGvn = decoder.getBool(USE_GVN); | 258 behavior.useGvn = decoder.getBool(USE_GVN); |
| 215 return behavior; | 259 return behavior; |
| 216 } | 260 } |
| 217 } | 261 } |
| OLD | NEW |