| 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 /// Converters and codecs for converting between JSON and [Info] classes. | 5 /// Converters and codecs for converting between JSON and [Info] classes. |
| 6 part of dart2js_info.info; | 6 part of dart2js_info.info; |
| 7 | 7 |
| 8 // TODO(sigmund): add unit tests. | 8 // TODO(sigmund): add unit tests. |
| 9 class JsonToAllInfoConverter extends Converter<Map<String, dynamic>, AllInfo> { | 9 class JsonToAllInfoConverter extends Converter<Map<String, dynamic>, AllInfo> { |
| 10 Map<String, Info> registry; | 10 Map<String, Info> registry = <String, Info>{}; |
| 11 | 11 |
| 12 AllInfo convert(Map<String, dynamic> json) { | 12 AllInfo convert(Map<String, dynamic> json) { |
| 13 registry = <String, Info>{}; | 13 registry.clear(); |
| 14 | 14 |
| 15 var result = new AllInfo(); | 15 var result = new AllInfo(); |
| 16 var elements = json['elements']; | 16 var elements = json['elements']; |
| 17 result.libraries | 17 result.libraries |
| 18 .addAll((elements['library'] as Map).values.map(parseLibrary)); | 18 .addAll((elements['library'] as Map).values.map(parseLibrary)); |
| 19 result.classes.addAll((elements['class'] as Map).values.map(parseClass)); | 19 result.classes.addAll((elements['class'] as Map).values.map(parseClass)); |
| 20 result.functions | 20 result.functions |
| 21 .addAll((elements['function'] as Map).values.map(parseFunction)); | 21 .addAll((elements['function'] as Map).values.map(parseFunction)); |
| 22 result.closures |
| 23 .addAll((elements['closure'] as Map).values.map(parseClosure)); |
| 22 result.fields.addAll((elements['field'] as Map).values.map(parseField)); | 24 result.fields.addAll((elements['field'] as Map).values.map(parseField)); |
| 23 result.typedefs | 25 result.typedefs |
| 24 .addAll((elements['typedef'] as Map).values.map(parseTypedef)); | 26 .addAll((elements['typedef'] as Map).values.map(parseTypedef)); |
| 25 | 27 result.constants |
| 26 // TODO(sigmund): remove null check on next breaking version | 28 .addAll((elements['constant'] as Map).values.map(parseConstant)); |
| 27 var constants = elements['constant']; | |
| 28 if (constants != null) { | |
| 29 result.constants.addAll((constants as Map).values.map(parseConstant)); | |
| 30 } | |
| 31 | 29 |
| 32 var idMap = <String, Info>{}; | 30 var idMap = <String, Info>{}; |
| 33 for (var f in result.functions) { | 31 for (var f in result.functions) { |
| 34 idMap[f.serializedId] = f; | 32 idMap[f.serializedId] = f; |
| 35 } | 33 } |
| 36 for (var f in result.fields) { | 34 for (var f in result.fields) { |
| 37 idMap[f.serializedId] = f; | 35 idMap[f.serializedId] = f; |
| 38 } | 36 } |
| 39 | 37 |
| 40 json['holding'].forEach((k, deps) { | 38 json['holding'].forEach((k, deps) { |
| (...skipping 160 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 201 } | 199 } |
| 202 | 200 |
| 203 FunctionModifiers parseModifiers(Map<String, bool> json) { | 201 FunctionModifiers parseModifiers(Map<String, bool> json) { |
| 204 return new FunctionModifiers( | 202 return new FunctionModifiers( |
| 205 isStatic: json['static'] == true, | 203 isStatic: json['static'] == true, |
| 206 isConst: json['const'] == true, | 204 isConst: json['const'] == true, |
| 207 isFactory: json['factory'] == true, | 205 isFactory: json['factory'] == true, |
| 208 isExternal: json['external'] == true); | 206 isExternal: json['external'] == true); |
| 209 } | 207 } |
| 210 | 208 |
| 209 ClosureInfo parseClosure(Map json) { |
| 210 ClosureInfo result = parseId(json['id']); |
| 211 return result |
| 212 ..name = json['name'] |
| 213 ..parent = parseId(json['parent']) |
| 214 ..outputUnit = parseId(json['outputUnit']) |
| 215 ..size = json['size'] |
| 216 ..function = parseId(json['function']); |
| 217 } |
| 218 |
| 211 Info parseId(String serializedId) => registry.putIfAbsent(serializedId, () { | 219 Info parseId(String serializedId) => registry.putIfAbsent(serializedId, () { |
| 212 if (serializedId == null) { | 220 if (serializedId == null) { |
| 213 return null; | 221 return null; |
| 214 } else if (serializedId.startsWith('function/')) { | 222 } else if (serializedId.startsWith('function/')) { |
| 215 return new FunctionInfo._(serializedId); | 223 return new FunctionInfo._(serializedId); |
| 224 } else if (serializedId.startsWith('closure/')) { |
| 225 return new ClosureInfo._(serializedId); |
| 216 } else if (serializedId.startsWith('library/')) { | 226 } else if (serializedId.startsWith('library/')) { |
| 217 return new LibraryInfo._(serializedId); | 227 return new LibraryInfo._(serializedId); |
| 218 } else if (serializedId.startsWith('class/')) { | 228 } else if (serializedId.startsWith('class/')) { |
| 219 return new ClassInfo._(serializedId); | 229 return new ClassInfo._(serializedId); |
| 220 } else if (serializedId.startsWith('field/')) { | 230 } else if (serializedId.startsWith('field/')) { |
| 221 return new FieldInfo._(serializedId); | 231 return new FieldInfo._(serializedId); |
| 222 } else if (serializedId.startsWith('constant/')) { | 232 } else if (serializedId.startsWith('constant/')) { |
| 223 return new ConstantInfo._(serializedId); | 233 return new ConstantInfo._(serializedId); |
| 224 } else if (serializedId.startsWith('typedef/')) { | 234 } else if (serializedId.startsWith('typedef/')) { |
| 225 return new TypedefInfo._(serializedId); | 235 return new TypedefInfo._(serializedId); |
| (...skipping 16 matching lines...) Expand all Loading... |
| 242 return map; | 252 return map; |
| 243 } | 253 } |
| 244 | 254 |
| 245 Map _visitAllInfoElements(AllInfo info) { | 255 Map _visitAllInfoElements(AllInfo info) { |
| 246 var jsonLibraries = _visitList(info.libraries); | 256 var jsonLibraries = _visitList(info.libraries); |
| 247 var jsonClasses = _visitList(info.classes); | 257 var jsonClasses = _visitList(info.classes); |
| 248 var jsonFunctions = _visitList(info.functions); | 258 var jsonFunctions = _visitList(info.functions); |
| 249 var jsonTypedefs = _visitList(info.typedefs); | 259 var jsonTypedefs = _visitList(info.typedefs); |
| 250 var jsonFields = _visitList(info.fields); | 260 var jsonFields = _visitList(info.fields); |
| 251 var jsonConstants = _visitList(info.constants); | 261 var jsonConstants = _visitList(info.constants); |
| 262 var jsonClosures = _visitList(info.closures); |
| 252 return { | 263 return { |
| 253 'library': jsonLibraries, | 264 'library': jsonLibraries, |
| 254 'class': jsonClasses, | 265 'class': jsonClasses, |
| 255 'function': jsonFunctions, | 266 'function': jsonFunctions, |
| 256 'typedef': jsonTypedefs, | 267 'typedef': jsonTypedefs, |
| 257 'field': jsonFields, | 268 'field': jsonFields, |
| 258 'constant': jsonConstants | 269 'constant': jsonConstants, |
| 270 'closure': jsonClosures, |
| 259 }; | 271 }; |
| 260 } | 272 } |
| 261 | 273 |
| 262 Map _visitDependencyInfo(DependencyInfo info) => | 274 Map _visitDependencyInfo(DependencyInfo info) => |
| 263 {'id': info.target.serializedId, 'mask': info.mask}; | 275 {'id': info.target.serializedId, 'mask': info.mask}; |
| 264 | 276 |
| 265 Map _visitAllInfoHolding(AllInfo allInfo) { | 277 Map _visitAllInfoHolding(AllInfo allInfo) { |
| 266 var map = <String, List>{}; | 278 var map = <String, List>{}; |
| 267 void helper(CodeInfo info) { | 279 void helper(CodeInfo info) { |
| 268 if (info.uses.isEmpty) return; | 280 if (info.uses.isEmpty) return; |
| (...skipping 157 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 426 'sideEffects': info.sideEffects, | 438 'sideEffects': info.sideEffects, |
| 427 'inlinedCount': info.inlinedCount, | 439 'inlinedCount': info.inlinedCount, |
| 428 'code': info.code, | 440 'code': info.code, |
| 429 'type': info.type, | 441 'type': info.type, |
| 430 'measurements': _visitMeasurements(info.measurements), | 442 'measurements': _visitMeasurements(info.measurements), |
| 431 // Note: version 3.2 of dump-info serializes `uses` in a section called | 443 // Note: version 3.2 of dump-info serializes `uses` in a section called |
| 432 // `holding` at the top-level. | 444 // `holding` at the top-level. |
| 433 }); | 445 }); |
| 434 } | 446 } |
| 435 | 447 |
| 448 Map visitClosure(ClosureInfo info) { |
| 449 return _visitBasicInfo(info) |
| 450 ..addAll({'function': info.function.serializedId}); |
| 451 } |
| 452 |
| 436 visitTypedef(TypedefInfo info) => _visitBasicInfo(info)..['type'] = info.type; | 453 visitTypedef(TypedefInfo info) => _visitBasicInfo(info)..['type'] = info.type; |
| 437 | 454 |
| 438 visitOutput(OutputUnitInfo info) => | 455 visitOutput(OutputUnitInfo info) => |
| 439 _visitBasicInfo(info)..['imports'] = info.imports; | 456 _visitBasicInfo(info)..['imports'] = info.imports; |
| 440 } | 457 } |
| 441 | 458 |
| 442 class AllInfoJsonCodec extends Codec<AllInfo, Map> { | 459 class AllInfoJsonCodec extends Codec<AllInfo, Map> { |
| 443 final Converter<AllInfo, Map> encoder = new AllInfoToJsonConverter(); | 460 final Converter<AllInfo, Map> encoder = new AllInfoToJsonConverter(); |
| 444 final Converter<Map, AllInfo> decoder = new JsonToAllInfoConverter(); | 461 final Converter<Map, AllInfo> decoder = new JsonToAllInfoConverter(); |
| 445 } | 462 } |
| OLD | NEW |