| 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 library dart2js.serialization_test; | 5 library dart2js.serialization_test; |
| 6 | 6 |
| 7 import 'dart:io'; | 7 import 'dart:io'; |
| 8 import 'memory_compiler.dart'; | 8 import 'memory_compiler.dart'; |
| 9 import 'package:async_helper/async_helper.dart'; | 9 import 'package:async_helper/async_helper.dart'; |
| 10 import 'package:compiler/src/commandline_options.dart'; |
| 10 import 'package:compiler/src/constants/constructors.dart'; | 11 import 'package:compiler/src/constants/constructors.dart'; |
| 11 import 'package:compiler/src/constants/expressions.dart'; | 12 import 'package:compiler/src/constants/expressions.dart'; |
| 12 import 'package:compiler/src/dart_types.dart'; | 13 import 'package:compiler/src/dart_types.dart'; |
| 13 import 'package:compiler/src/compiler.dart'; | 14 import 'package:compiler/src/compiler.dart'; |
| 14 import 'package:compiler/src/diagnostics/invariant.dart'; | 15 import 'package:compiler/src/diagnostics/invariant.dart'; |
| 15 import 'package:compiler/src/elements/elements.dart'; | 16 import 'package:compiler/src/elements/elements.dart'; |
| 16 import 'package:compiler/src/elements/visitor.dart'; | 17 import 'package:compiler/src/elements/visitor.dart'; |
| 17 import 'package:compiler/src/ordered_typeset.dart'; | 18 import 'package:compiler/src/ordered_typeset.dart'; |
| 18 import 'package:compiler/src/serialization/element_serialization.dart'; | 19 import 'package:compiler/src/serialization/element_serialization.dart'; |
| 19 import 'package:compiler/src/serialization/json_serializer.dart'; | 20 import 'package:compiler/src/serialization/json_serializer.dart'; |
| (...skipping 20 matching lines...) Expand all Loading... |
| 40 print("Multiple entrypoints is not supported."); | 41 print("Multiple entrypoints is not supported."); |
| 41 } | 42 } |
| 42 entryPoint = Uri.parse(arg); | 43 entryPoint = Uri.parse(arg); |
| 43 } | 44 } |
| 44 } | 45 } |
| 45 if (entryPoint == null) { | 46 if (entryPoint == null) { |
| 46 entryPoint = Uri.parse('dart:core'); | 47 entryPoint = Uri.parse('dart:core'); |
| 47 } | 48 } |
| 48 asyncTest(() async { | 49 asyncTest(() async { |
| 49 CompilationResult result = await runCompiler( | 50 CompilationResult result = await runCompiler( |
| 50 entryPoint: entryPoint, options: ['--analyze-all']); | 51 entryPoint: entryPoint, options: [Flags.analyzeAll]); |
| 51 Compiler compiler = result.compiler; | 52 Compiler compiler = result.compiler; |
| 52 testSerialization(compiler.libraryLoader.libraries, | 53 testSerialization(compiler.libraryLoader.libraries, |
| 53 outPath: outPath, | 54 outPath: outPath, |
| 54 prettyPrint: prettyPrint); | 55 prettyPrint: prettyPrint); |
| 55 }); | 56 }); |
| 56 } | 57 } |
| 57 | 58 |
| 58 void testSerialization(Iterable<LibraryElement> libraries1, | 59 void testSerialization(Iterable<LibraryElement> libraries1, |
| 59 {String outPath, | 60 {String outPath, |
| 60 bool prettyPrint}) { | 61 bool prettyPrint}) { |
| 61 Serializer serializer = new Serializer(const JsonSerializationEncoder()); | 62 Serializer serializer = new Serializer(); |
| 62 for (LibraryElement library1 in libraries1) { | 63 for (LibraryElement library1 in libraries1) { |
| 63 serializer.serialize(library1); | 64 serializer.serialize(library1); |
| 64 } | 65 } |
| 65 String text = serializer.toText(); | 66 String text = serializer.toText(const JsonSerializationEncoder()); |
| 66 String outText = text; | 67 String outText = text; |
| 67 if (prettyPrint) { | 68 if (prettyPrint) { |
| 68 outText = serializer.prettyPrint(); | 69 outText = serializer.prettyPrint(); |
| 69 } | 70 } |
| 70 if (outPath != null) { | 71 if (outPath != null) { |
| 71 new File(outPath).writeAsStringSync(outText); | 72 new File(outPath).writeAsStringSync(outText); |
| 72 } else if (prettyPrint) { | 73 } else if (prettyPrint) { |
| 73 print(outText); | 74 print(outText); |
| 74 } | 75 } |
| 75 | 76 |
| 76 Deserializer deserializer = new Deserializer.fromText( | 77 Deserializer deserializer = new Deserializer.fromText( |
| 78 new DeserializationContext(), |
| 77 text, const JsonSerializationDecoder()); | 79 text, const JsonSerializationDecoder()); |
| 78 List<LibraryElement> libraries2 = <LibraryElement>[]; | 80 List<LibraryElement> libraries2 = <LibraryElement>[]; |
| 79 for (LibraryElement library1 in libraries1) { | 81 for (LibraryElement library1 in libraries1) { |
| 80 LibraryElement library2 = | 82 LibraryElement library2 = |
| 81 deserializer.lookupLibrary(library1.canonicalUri); | 83 deserializer.lookupLibrary(library1.canonicalUri); |
| 82 if (library2 == null) { | 84 if (library2 == null) { |
| 83 throw new ArgumentError('No library ${library1.canonicalUri} found.'); | 85 throw new ArgumentError('No library ${library1.canonicalUri} found.'); |
| 84 } | 86 } |
| 85 checkLibraryContent('library1', 'library2', 'library', library1, library2); | 87 checkLibraryContent('library1', 'library2', 'library', library1, library2); |
| 86 libraries2.add(library2); | 88 libraries2.add(library2); |
| 87 } | 89 } |
| 88 | 90 |
| 89 Serializer serializer2 = new Serializer(const JsonSerializationEncoder()); | 91 Serializer serializer2 = new Serializer(); |
| 90 for (LibraryElement library2 in libraries2) { | 92 for (LibraryElement library2 in libraries2) { |
| 91 serializer2.serialize(library2); | 93 serializer2.serialize(library2); |
| 92 } | 94 } |
| 93 String text2 = serializer2.toText(); | 95 String text2 = serializer2.toText(const JsonSerializationEncoder()); |
| 94 | 96 |
| 95 Deserializer deserializer3 = new Deserializer.fromText( | 97 Deserializer deserializer3 = new Deserializer.fromText( |
| 98 new DeserializationContext(), |
| 96 text2, const JsonSerializationDecoder()); | 99 text2, const JsonSerializationDecoder()); |
| 97 for (LibraryElement library1 in libraries1) { | 100 for (LibraryElement library1 in libraries1) { |
| 98 LibraryElement library2 = | 101 LibraryElement library2 = |
| 99 deserializer.lookupLibrary(library1.canonicalUri); | 102 deserializer.lookupLibrary(library1.canonicalUri); |
| 100 if (library2 == null) { | 103 if (library2 == null) { |
| 101 throw new ArgumentError('No library ${library1.canonicalUri} found.'); | 104 throw new ArgumentError('No library ${library1.canonicalUri} found.'); |
| 102 } | 105 } |
| 103 LibraryElement library3 = | 106 LibraryElement library3 = |
| 104 deserializer3.lookupLibrary(library1.canonicalUri); | 107 deserializer3.lookupLibrary(library1.canonicalUri); |
| 105 if (library3 == null) { | 108 if (library3 == null) { |
| (...skipping 435 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 541 } | 544 } |
| 542 for (Element member in members2) { | 545 for (Element member in members2) { |
| 543 names.add(member.name); | 546 names.add(member.name); |
| 544 } | 547 } |
| 545 element1 = element1.implementation; | 548 element1 = element1.implementation; |
| 546 element2 = element2.implementation; | 549 element2 = element2.implementation; |
| 547 for (String name in names) { | 550 for (String name in names) { |
| 548 Element member1 = element1.localLookup(name); | 551 Element member1 = element1.localLookup(name); |
| 549 Element member2 = element2.localLookup(name); | 552 Element member2 = element2.localLookup(name); |
| 550 if (member1 == null) { | 553 if (member1 == null) { |
| 551 print('Missing member for $member2 in\n ${members1.join('\n ')}'); | 554 // TODO(johnniwinther): Ensure abstract fields are handled correctly. |
| 552 continue; | 555 String message = |
| 556 'Missing member for $member2 in\n ${members1.join('\n ')}'; |
| 557 if (member2.isAbstractField) { |
| 558 print(message); |
| 559 continue; |
| 560 } else { |
| 561 throw message; |
| 562 } |
| 553 } | 563 } |
| 554 if (member2 == null) { | 564 if (member2 == null) { |
| 555 print('Missing member for $member1 in\n ${members2.join('\n ')}'); | 565 // TODO(johnniwinther): Ensure abstract fields are handled correctly. |
| 556 continue; | 566 String message = |
| 567 'Missing member for $member1 in\n ${members2.join('\n ')}'; |
| 568 if (member1.isAbstractField) { |
| 569 print(message); |
| 570 continue; |
| 571 } else { |
| 572 throw message; |
| 573 } |
| 557 } | 574 } |
| 575 //print('Checking member ${member1} against ${member2}'); |
| 558 visit(member1, member2); | 576 visit(member1, member2); |
| 559 } | 577 } |
| 560 } | 578 } |
| 561 | 579 |
| 562 @override | 580 @override |
| 563 void visitClassElement(ClassElement element1, ClassElement element2) { | 581 void visitClassElement(ClassElement element1, ClassElement element2) { |
| 564 checkElementIdentities(null, null, null, element1, element2); | 582 checkElementIdentities(null, null, null, element1, element2); |
| 565 check(element1, element2, 'name', | 583 check(element1, element2, 'name', |
| 566 element1.name, element2.name); | 584 element1.name, element2.name); |
| 567 check(element1, element2, 'sourcePosition', | 585 check(element1, element2, 'sourcePosition', |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 614 checkTypeLists( | 632 checkTypeLists( |
| 615 element1, element2, 'types', | 633 element1, element2, 'types', |
| 616 typeSet1.types.toList(), | 634 typeSet1.types.toList(), |
| 617 typeSet2.types.toList()); | 635 typeSet2.types.toList()); |
| 618 | 636 |
| 619 checkTypeLists( | 637 checkTypeLists( |
| 620 element1, element2, 'interfaces', | 638 element1, element2, 'interfaces', |
| 621 element1.interfaces.toList(), | 639 element1.interfaces.toList(), |
| 622 element2.interfaces.toList()); | 640 element2.interfaces.toList()); |
| 623 | 641 |
| 642 List<ConstructorElement> getConstructors(ClassElement cls) { |
| 643 return cls.implementation.constructors.map((c) => c.declaration).toList(); |
| 644 } |
| 645 |
| 646 checkElementLists( |
| 647 element1, element2, 'constructors', |
| 648 getConstructors(element1), |
| 649 getConstructors(element2)); |
| 650 |
| 624 visitMembers(element1, element2); | 651 visitMembers(element1, element2); |
| 625 } | 652 } |
| 626 | 653 |
| 627 @override | 654 @override |
| 628 void visitFieldElement(FieldElement element1, FieldElement element2) { | 655 void visitFieldElement(FieldElement element1, FieldElement element2) { |
| 629 checkElementIdentities(null, null, null, element1, element2); | 656 checkElementIdentities(null, null, null, element1, element2); |
| 630 check(element1, element2, 'name', | 657 check(element1, element2, 'name', |
| 631 element1.name, element2.name); | 658 element1.name, element2.name); |
| 632 check(element1, element2, 'sourcePosition', | 659 check(element1, element2, 'sourcePosition', |
| 633 element1.sourcePosition, element2.sourcePosition); | 660 element1.sourcePosition, element2.sourcePosition); |
| (...skipping 466 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1100 exp1, exp2, 'expression', | 1127 exp1, exp2, 'expression', |
| 1101 exp1.expression, exp2.expression); | 1128 exp1.expression, exp2.expression); |
| 1102 } | 1129 } |
| 1103 | 1130 |
| 1104 @override | 1131 @override |
| 1105 visitDeferred(DeferredConstantExpression exp1, | 1132 visitDeferred(DeferredConstantExpression exp1, |
| 1106 DeferredConstantExpression exp2) { | 1133 DeferredConstantExpression exp2) { |
| 1107 // TODO: implement visitDeferred | 1134 // TODO: implement visitDeferred |
| 1108 } | 1135 } |
| 1109 } | 1136 } |
| OLD | NEW |