| 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 /** | 5 /** |
| 6 * This file contains code to generate serialization/deserialization logic for | 6 * This file contains code to generate serialization/deserialization logic for |
| 7 * summaries based on an "IDL" description of the summary format (written in | 7 * summaries based on an "IDL" description of the summary format (written in |
| 8 * stylized Dart). | 8 * stylized Dart). |
| 9 * | 9 * |
| 10 * For each class in the "IDL" input, two corresponding classes are generated: | 10 * For each class in the "IDL" input, two corresponding classes are generated: |
| (...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 128 } else { | 128 } else { |
| 129 return null; | 129 return null; |
| 130 } | 130 } |
| 131 } | 131 } |
| 132 | 132 |
| 133 /** | 133 /** |
| 134 * Generate a string representing the Dart type which should be used to | 134 * Generate a string representing the Dart type which should be used to |
| 135 * represent [type] while building a serialized data structure. | 135 * represent [type] while building a serialized data structure. |
| 136 */ | 136 */ |
| 137 String encodedType(idlModel.FieldType type) { | 137 String encodedType(idlModel.FieldType type) { |
| 138 String typeStr; |
| 139 if (_idl.classes.containsKey(type.typeName)) { |
| 140 typeStr = '${type.typeName}Builder'; |
| 141 } else { |
| 142 typeStr = type.typeName; |
| 143 } |
| 138 if (type.isList) { | 144 if (type.isList) { |
| 139 if (type.typeName == 'int') { | 145 return 'List<$typeStr>'; |
| 140 return 'List<int>'; | |
| 141 } else { | |
| 142 return 'List<Object>'; | |
| 143 } | |
| 144 } else if (_idl.classes.containsKey(type.typeName)) { | |
| 145 return 'Object'; | |
| 146 } else { | 146 } else { |
| 147 return dartType(type); | 147 return typeStr; |
| 148 } | 148 } |
| 149 } | 149 } |
| 150 | 150 |
| 151 /** | 151 /** |
| 152 * Process the AST in [idlParsed] and store the resulting semantic model in | 152 * Process the AST in [idlParsed] and store the resulting semantic model in |
| 153 * [_idl]. Also perform some error checking. | 153 * [_idl]. Also perform some error checking. |
| 154 */ | 154 */ |
| 155 void extractIdl(CompilationUnit idlParsed) { | 155 void extractIdl(CompilationUnit idlParsed) { |
| 156 _idl = new idlModel.Idl(); | 156 _idl = new idlModel.Idl(); |
| 157 for (CompilationUnitMember decl in idlParsed.declarations) { | 157 for (CompilationUnitMember decl in idlParsed.declarations) { |
| (...skipping 148 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 306 out('class ${name}Builder {'); | 306 out('class ${name}Builder {'); |
| 307 indent(() { | 307 indent(() { |
| 308 out('final Map _json = {};'); | 308 out('final Map _json = {};'); |
| 309 out(); | 309 out(); |
| 310 out('${name}Builder(builder.BuilderContext context);'); | 310 out('${name}Builder(builder.BuilderContext context);'); |
| 311 cls.fields.forEach((String fieldName, idlModel.FieldType type) { | 311 cls.fields.forEach((String fieldName, idlModel.FieldType type) { |
| 312 out(); | 312 out(); |
| 313 String conversion = '_value'; | 313 String conversion = '_value'; |
| 314 String condition = ''; | 314 String condition = ''; |
| 315 if (type.isList) { | 315 if (type.isList) { |
| 316 conversion = '$conversion.toList()'; | 316 if (_idl.classes.containsKey(type.typeName)) { |
| 317 conversion = '$conversion.map((b) => b.finish()).toList()'; |
| 318 } else { |
| 319 conversion = '$conversion.toList()'; |
| 320 } |
| 317 condition = ' || _value.isEmpty'; | 321 condition = ' || _value.isEmpty'; |
| 318 } else if (_idl.enums.containsKey(type.typeName)) { | 322 } else if (_idl.enums.containsKey(type.typeName)) { |
| 319 conversion = '$conversion.index'; | 323 conversion = '$conversion.index'; |
| 320 condition = ' || _value == ${defaultValue(type)}'; | 324 condition = ' || _value == ${defaultValue(type)}'; |
| 325 } else if (_idl.classes.containsKey(type.typeName)) { |
| 326 conversion = '$conversion.finish()'; |
| 321 } | 327 } |
| 322 builderParams.add('${encodedType(type)} $fieldName'); | 328 builderParams.add('${encodedType(type)} $fieldName'); |
| 323 out('void set $fieldName(${encodedType(type)} _value) {'); | 329 out('void set $fieldName(${encodedType(type)} _value) {'); |
| 324 indent(() { | 330 indent(() { |
| 325 out('assert(!_json.containsKey(${quoted(fieldName)}));'); | 331 out('assert(!_json.containsKey(${quoted(fieldName)}));'); |
| 326 out('if (_value != null$condition) {'); | 332 out('if (_value != null$condition) {'); |
| 327 indent(() { | 333 indent(() { |
| 328 out('_json[${quoted(fieldName)}] = $conversion;'); | 334 out('_json[${quoted(fieldName)}] = $conversion;'); |
| 329 }); | 335 }); |
| 330 out('}'); | 336 out('}'); |
| 331 }); | 337 }); |
| 332 out('}'); | 338 out('}'); |
| 333 }); | 339 }); |
| 340 if (cls.isTopLevel) { |
| 341 out(); |
| 342 out('List<int> toBuffer() => UTF8.encode(JSON.encode(finish()));'); |
| 343 } |
| 334 out(); | 344 out(); |
| 335 out('Object finish() => _json;'); | 345 out('Map finish() => _json;'); |
| 336 }); | 346 }); |
| 337 out('}'); | 347 out('}'); |
| 338 out(); | 348 out(); |
| 339 out('Object encode$name(builder.BuilderContext builderContext, {${builderP
arams.join(', ')}}) {'); | 349 out('${name}Builder encode$name(builder.BuilderContext builderContext, {${
builderParams.join(', ')}}) {'); |
| 340 indent(() { | 350 indent(() { |
| 341 out('${name}Builder builder = new ${name}Builder(builderContext);'); | 351 out('${name}Builder builder = new ${name}Builder(builderContext);'); |
| 342 cls.fields.forEach((String fieldName, idlModel.FieldType type) { | 352 cls.fields.forEach((String fieldName, idlModel.FieldType type) { |
| 343 out('builder.$fieldName = $fieldName;'); | 353 out('builder.$fieldName = $fieldName;'); |
| 344 }); | 354 }); |
| 345 out('return builder.finish();'); | 355 out('return builder;'); |
| 346 }); | 356 }); |
| 347 out('}'); | 357 out('}'); |
| 348 out(); | 358 out(); |
| 349 }); | 359 }); |
| 350 } | 360 } |
| 351 | 361 |
| 352 /** | 362 /** |
| 353 * Enclose [s] in quotes, escaping as necessary. | 363 * Enclose [s] in quotes, escaping as necessary. |
| 354 */ | 364 */ |
| 355 String quoted(String s) { | 365 String quoted(String s) { |
| 356 return JSON.encode(s); | 366 return JSON.encode(s); |
| 357 } | 367 } |
| 358 } | 368 } |
| OLD | NEW |