OLD | NEW |
| (Empty) |
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 | |
3 // BSD-style license that can be found in the LICENSE file. | |
4 | |
5 library dart2js.serialization_library_test; | |
6 | |
7 import 'dart:io'; | |
8 import 'memory_compiler.dart'; | |
9 import 'package:async_helper/async_helper.dart'; | |
10 import 'package:compiler/src/commandline_options.dart'; | |
11 import 'package:compiler/src/common/names.dart'; | |
12 import 'package:compiler/src/constants/constructors.dart'; | |
13 import 'package:compiler/src/constants/expressions.dart'; | |
14 import 'package:compiler/src/dart_types.dart'; | |
15 import 'package:compiler/src/compiler.dart'; | |
16 import 'package:compiler/src/diagnostics/invariant.dart'; | |
17 import 'package:compiler/src/elements/elements.dart'; | |
18 import 'package:compiler/src/elements/visitor.dart'; | |
19 import 'package:compiler/src/ordered_typeset.dart'; | |
20 import 'package:compiler/src/serialization/element_serialization.dart'; | |
21 import 'package:compiler/src/serialization/json_serializer.dart'; | |
22 import 'package:compiler/src/serialization/serialization.dart'; | |
23 | |
24 import 'serialization_test.dart'; | |
25 | |
26 main(List<String> arguments) { | |
27 // Ensure that we can print out constant expressions. | |
28 DEBUG_MODE = true; | |
29 | |
30 Uri entryPoint; | |
31 String outPath; | |
32 int shardCount = 3; | |
33 bool prettyPrint = false; | |
34 for (String arg in arguments) { | |
35 if (arg.startsWith('--')) { | |
36 if (arg.startsWith('--out=')) { | |
37 outPath = arg.substring('--out='.length); | |
38 } else if (arg == '--pretty-print') { | |
39 prettyPrint = true; | |
40 } else if (arg.startsWith('--shards=')) { | |
41 shardCount = int.parse(arg.substring('--shards='.length)); | |
42 } else { | |
43 print("Unknown option $arg"); | |
44 } | |
45 } else { | |
46 if (entryPoint != null) { | |
47 print("Multiple entrypoints are not supported."); | |
48 } | |
49 entryPoint = Uri.parse(arg); | |
50 } | |
51 } | |
52 if (entryPoint == null) { | |
53 entryPoint = Uris.dart_core; | |
54 } | |
55 asyncTest(() async { | |
56 CompilationResult result = await runCompiler( | |
57 entryPoint: entryPoint, options: [Flags.analyzeAll]); | |
58 Compiler compiler = result.compiler; | |
59 testSerialization(compiler.libraryLoader.libraries, | |
60 outPath: outPath, | |
61 prettyPrint: prettyPrint, | |
62 shardCount: shardCount); | |
63 }); | |
64 } | |
65 | |
66 void testSerialization(Iterable<LibraryElement> libraries1, | |
67 {String outPath, | |
68 bool prettyPrint, | |
69 int shardCount: 3}) { | |
70 if (shardCount < 1 || shardCount > libraries1.length) { | |
71 shardCount = libraries1.length; | |
72 } | |
73 List<List<LibraryElement>> librarySplits = <List<LibraryElement>>[]; | |
74 int offset = 0; | |
75 int shardSize = (libraries1.length / shardCount).ceil(); | |
76 for (int shard = 0; shard < shardCount; shard++) { | |
77 List<LibraryElement> libraries = <LibraryElement>[]; | |
78 for (int index = 0; index < shardSize; index++) { | |
79 if (offset + index < libraries1.length) { | |
80 libraries.add(libraries1.elementAt(offset + index)); | |
81 } | |
82 } | |
83 librarySplits.add(libraries); | |
84 offset += shardSize; | |
85 } | |
86 print(librarySplits.join('\n')); | |
87 List<String> texts = <String>[]; | |
88 for (int shard = 0; shard < shardCount; shard++) { | |
89 List<LibraryElement> libraries = librarySplits[shard]; | |
90 Serializer serializer = new Serializer( | |
91 shouldInclude: (e) => libraries.contains(e.library)); | |
92 for (LibraryElement library in libraries) { | |
93 serializer.serialize(library); | |
94 } | |
95 String text = serializer.toText(const JsonSerializationEncoder()); | |
96 String outText = text; | |
97 if (prettyPrint) { | |
98 outText = serializer.prettyPrint(); | |
99 } | |
100 if (outPath != null) { | |
101 String name = outPath; | |
102 String ext = ''; | |
103 int dotPos = outPath.lastIndexOf('.'); | |
104 if (dotPos != -1) { | |
105 name = outPath.substring(0, dotPos); | |
106 ext = outPath.substring(dotPos); | |
107 } | |
108 new File('$name$shard$ext').writeAsStringSync(outText); | |
109 } else if (prettyPrint) { | |
110 print(outText); | |
111 } | |
112 texts.add(text); | |
113 } | |
114 DeserializationContext deserializationContext = | |
115 new DeserializationContext(); | |
116 for (int shard = 0; shard < shardCount; shard++) { | |
117 new Deserializer.fromText( | |
118 deserializationContext, texts[shard], const JsonSerializationDecoder()); | |
119 } | |
120 List<LibraryElement> libraries2 = <LibraryElement>[]; | |
121 for (LibraryElement library1 in libraries1) { | |
122 LibraryElement library2 = | |
123 deserializationContext.lookupLibrary(library1.canonicalUri); | |
124 if (library2 == null) { | |
125 throw new ArgumentError('No library ${library1.canonicalUri} found.'); | |
126 } | |
127 checkLibraryContent('library1', 'library2', 'library', library1, library2); | |
128 libraries2.add(library2); | |
129 } | |
130 } | |
OLD | NEW |