Chromium Code Reviews| 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 = <String, Info>{}; | 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.clear(); | 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 | 22 |
| 23 .addAll((elements['closure'] as Map).values.map(parseClosure)); | 23 // TODO(het): Revert this when the dart2js with the new codec is in stable |
| 24 if (elements['closure'] != null) { | |
| 25 result.closures | |
| 26 .addAll((elements['closure'] as Map).values.map(parseClosure)); | |
| 27 } | |
| 24 result.fields.addAll((elements['field'] as Map).values.map(parseField)); | 28 result.fields.addAll((elements['field'] as Map).values.map(parseField)); |
| 25 result.typedefs | 29 result.typedefs |
| 26 .addAll((elements['typedef'] as Map).values.map(parseTypedef)); | 30 .addAll((elements['typedef'] as Map).values.map(parseTypedef)); |
| 27 result.constants | 31 result.constants |
| 28 .addAll((elements['constant'] as Map).values.map(parseConstant)); | 32 .addAll((elements['constant'] as Map).values.map(parseConstant)); |
| 29 | 33 |
| 30 var idMap = <String, Info>{}; | 34 var idMap = <String, Info>{}; |
| 31 for (var f in result.functions) { | 35 for (var f in result.functions) { |
| 32 idMap[f.serializedId] = f; | 36 idMap[f.serializedId] = f; |
| 33 } | 37 } |
| (...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 135 | 139 |
| 136 TypedefInfo parseTypedef(Map json) { | 140 TypedefInfo parseTypedef(Map json) { |
| 137 TypedefInfo result = parseId(json['id']); | 141 TypedefInfo result = parseId(json['id']); |
| 138 return result | 142 return result |
| 139 ..name = json['name'] | 143 ..name = json['name'] |
| 140 ..parent = parseId(json['parent']) | 144 ..parent = parseId(json['parent']) |
| 141 ..type = json['type'] | 145 ..type = json['type'] |
| 142 ..size = 0; | 146 ..size = 0; |
| 143 } | 147 } |
| 144 | 148 |
| 145 ProgramInfo parseProgram(Map json) => new ProgramInfo() | 149 ProgramInfo parseProgram(Map json) { |
| 146 ..entrypoint = parseId(json['entrypoint']) | 150 var programInfo = new ProgramInfo() |
| 147 ..size = json['size'] | 151 ..entrypoint = parseId(json['entrypoint']) |
| 148 ..dart2jsVersion = json['dart2jsVersion'] | 152 ..size = json['size'] |
| 149 ..compilationMoment = DateTime.parse(json['compilationMoment']) | 153 ..dart2jsVersion = json['dart2jsVersion'] |
| 150 ..compilationDuration = | 154 ..noSuchMethodEnabled = json['noSuchMethodEnabled'] |
| 151 new Duration(microseconds: json['compilationDuration']) | 155 ..minified = json['minified']; |
| 152 ..toJsonDuration = new Duration(microseconds: json['toJsonDuration']) | 156 |
| 153 ..dumpInfoDuration = new Duration(microseconds: json['dumpInfoDuration']) | 157 // TODO(het): Revert this when the dart2js with the new codec is in stable |
| 154 ..noSuchMethodEnabled = json['noSuchMethodEnabled'] | 158 var compilationDuration = json['compilationDuration']; |
| 155 ..minified = json['minified']; | 159 if (compilationDuration is String) { |
| 160 programInfo.compilationDuration = _parseDuration(compilationDuration); | |
| 161 } else { | |
| 162 assert(compilationDuration is int); | |
| 163 programInfo.compilationDuration = | |
| 164 new Duration(microseconds: compilationDuration); | |
| 165 } | |
| 166 | |
| 167 var toJsonDuration = json['toJsonDuration']; | |
| 168 if (toJsonDuration is String) { | |
|
sra1
2016/10/27 14:19:47
You could put this repeated if-then-else in a help
| |
| 169 programInfo.toJsonDuration = _parseDuration(toJsonDuration); | |
| 170 } else { | |
| 171 assert(toJsonDuration is int); | |
| 172 programInfo.toJsonDuration = new Duration(microseconds: toJsonDuration); | |
| 173 } | |
| 174 | |
| 175 var dumpInfoDuration = json['dumpInfoDuration']; | |
| 176 if (dumpInfoDuration is String) { | |
| 177 programInfo.dumpInfoDuration = _parseDuration(dumpInfoDuration); | |
| 178 } else { | |
| 179 assert(dumpInfoDuration is int); | |
| 180 programInfo.dumpInfoDuration = | |
| 181 new Duration(microseconds: dumpInfoDuration); | |
| 182 } | |
| 183 | |
| 184 return programInfo; | |
| 185 } | |
| 186 | |
| 187 /// Parse a string formatted as "XX:YY:ZZ.ZZZZZ" into a [Duration]. | |
| 188 Duration _parseDuration(String duration) { | |
| 189 if (!duration.contains(':')) { | |
| 190 return new Duration(milliseconds: int.parse(duration)); | |
| 191 } | |
| 192 var parts = duration.split(':'); | |
| 193 var hours = double.parse(parts[0]); | |
| 194 var minutes = double.parse(parts[1]); | |
| 195 var seconds = double.parse(parts[2]); | |
| 196 const secondsInMillis = 1000; | |
| 197 const minutesInMillis = 60 * secondsInMillis; | |
| 198 const hoursInMillis = 60 * minutesInMillis; | |
| 199 var totalMillis = secondsInMillis * seconds + | |
| 200 minutesInMillis * minutes + | |
| 201 hoursInMillis * hours; | |
| 202 return new Duration(milliseconds: totalMillis.round()); | |
| 203 } | |
| 156 | 204 |
| 157 FunctionInfo parseFunction(Map json) { | 205 FunctionInfo parseFunction(Map json) { |
| 158 FunctionInfo result = parseId(json['id']); | 206 FunctionInfo result = parseId(json['id']); |
| 159 return result | 207 return result |
| 160 ..name = json['name'] | 208 ..name = json['name'] |
| 161 ..parent = parseId(json['parent']) | 209 ..parent = parseId(json['parent']) |
| 162 ..coverageId = json['coverageId'] | 210 ..coverageId = json['coverageId'] |
| 163 ..outputUnit = parseId(json['outputUnit']) | 211 ..outputUnit = parseId(json['outputUnit']) |
| 164 ..size = json['size'] | 212 ..size = json['size'] |
| 165 ..type = json['type'] | 213 ..type = json['type'] |
| (...skipping 287 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 453 visitTypedef(TypedefInfo info) => _visitBasicInfo(info)..['type'] = info.type; | 501 visitTypedef(TypedefInfo info) => _visitBasicInfo(info)..['type'] = info.type; |
| 454 | 502 |
| 455 visitOutput(OutputUnitInfo info) => | 503 visitOutput(OutputUnitInfo info) => |
| 456 _visitBasicInfo(info)..['imports'] = info.imports; | 504 _visitBasicInfo(info)..['imports'] = info.imports; |
| 457 } | 505 } |
| 458 | 506 |
| 459 class AllInfoJsonCodec extends Codec<AllInfo, Map> { | 507 class AllInfoJsonCodec extends Codec<AllInfo, Map> { |
| 460 final Converter<AllInfo, Map> encoder = new AllInfoToJsonConverter(); | 508 final Converter<AllInfo, Map> encoder = new AllInfoToJsonConverter(); |
| 461 final Converter<Map, AllInfo> decoder = new JsonToAllInfoConverter(); | 509 final Converter<Map, AllInfo> decoder = new JsonToAllInfoConverter(); |
| 462 } | 510 } |
| OLD | NEW |