Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1)

Side by Side Diff: tests/compiler/dart2js/serialization_analysis_test.dart

Issue 1809533004: Support serialization of WorldImpact (Closed) Base URL: https://github.com/dart-lang/sdk.git@master
Patch Set: Updated cf. comments + cleanup. Created 4 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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_analysis_test; 5 library dart2js.serialization_analysis_test;
6 6
7 import 'dart:async'; 7 import 'dart:async';
8 import 'package:async_helper/async_helper.dart'; 8 import 'package:async_helper/async_helper.dart';
9 import 'package:expect/expect.dart'; 9 import 'package:expect/expect.dart';
10 import 'package:compiler/src/commandline_options.dart'; 10 import 'package:compiler/src/commandline_options.dart';
11 import 'package:compiler/src/common/resolution.dart';
12 import 'package:compiler/src/compiler.dart';
11 import 'package:compiler/src/elements/elements.dart'; 13 import 'package:compiler/src/elements/elements.dart';
12 import 'package:compiler/src/compiler.dart';
13 import 'package:compiler/src/filenames.dart'; 14 import 'package:compiler/src/filenames.dart';
15 import 'package:compiler/src/serialization/element_serialization.dart';
16 import 'package:compiler/src/serialization/impact_serialization.dart';
17 import 'package:compiler/src/serialization/json_serializer.dart';
14 import 'package:compiler/src/serialization/serialization.dart'; 18 import 'package:compiler/src/serialization/serialization.dart';
15 import 'package:compiler/src/serialization/json_serializer.dart';
16 import 'package:compiler/src/serialization/task.dart'; 19 import 'package:compiler/src/serialization/task.dart';
17 import 'package:compiler/src/universe/world_impact.dart'; 20 import 'package:compiler/src/universe/world_impact.dart';
18 import 'memory_compiler.dart'; 21 import 'memory_compiler.dart';
19 22
20 const List<Test> TESTS = const <Test>[ 23 const List<Test> TESTS = const <Test>[
21 const Test(const { 24 const Test(const {
22 'main.dart': 'main() => print("Hello World");' 25 'main.dart': 'main() => print("Hello World");'
23 }), 26 }),
24 27
25 const Test(const { 28 const Test(const {
(...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after
135 'main.dart': r''' 138 'main.dart': r'''
136 class Class implements Comparable { 139 class Class implements Comparable {
137 bool compareTo(a, b) => true; 140 bool compareTo(a, b) => true;
138 } 141 }
139 main() { 142 main() {
140 print(new Class().compareTo(null, null)); 143 print(new Class().compareTo(null, null));
141 }''' 144 }'''
142 }, 145 },
143 expectedWarningCount: 1, 146 expectedWarningCount: 1,
144 expectedInfoCount: 1), 147 expectedInfoCount: 1),
148
149 const Test(const {
150 'main.dart': r'''
151 import 'dart:math';
152
153 class MyRandom implements Random {
154 int nextInt(int max) {
155 return max.length;
156 }
157 bool nextBool() => true;
158 double nextDouble() => 0.0;
159 }
160 main() {
161 new MyRandom().nextInt(0);
162 }'''
163 },
164 expectedWarningCount: 1,
165 expectedInfoCount: 0),
166
167 const Test(const {
168 'main.dart': r'''
169 import 'dart:math';
170
171 class MyRandom implements Random {
172 int nextInt(int max) {
173 return max.length;
174 }
175 bool nextBool() => true;
176 double nextDouble() => 0.0;
177 }
178 main() {
179 new MyRandom();
180 }'''
181 }),
182
183 const Test(const {
184 'main.dart': r'''
185 import 'dart:math';
186
187 class MyRandom implements Random {
188 int nextInt(int max) {
189 return max.length;
190 }
191 bool nextBool() => true;
192 double nextDouble() => 0.0;
193 }
194 main() {
195 // Invocation of `MyRandom.nextInt` is only detected knowing the actual
196 // implementation class for `List` and the world impact of its `shuffle`
197 // method.
198 [].shuffle(new MyRandom());
199 }'''
200 },
201 expectedWarningCount: 1,
202 expectedInfoCount: 0),
145 ]; 203 ];
146 204
147 main(List<String> arguments) { 205 main(List<String> arguments) {
148 asyncTest(() async { 206 asyncTest(() async {
149 String serializedData = await serializeDartCore(); 207 String serializedData = await serializeDartCore();
150 208
151 if (arguments.isNotEmpty) { 209 if (arguments.isNotEmpty) {
152 Uri entryPoint = Uri.base.resolve(nativeToUriPath(arguments.last)); 210 Uri entryPoint = Uri.base.resolve(nativeToUriPath(arguments.last));
153 await analyze(serializedData, entryPoint, null); 211 await analyze(serializedData, entryPoint, null);
154 } else { 212 } else {
(...skipping 13 matching lines...) Expand all
168 final int expectedInfoCount; 226 final int expectedInfoCount;
169 227
170 const Test(this.sourceFiles, { 228 const Test(this.sourceFiles, {
171 this.expectedErrorCount: 0, 229 this.expectedErrorCount: 0,
172 this.expectedWarningCount: 0, 230 this.expectedWarningCount: 0,
173 this.expectedHintCount: 0, 231 this.expectedHintCount: 0,
174 this.expectedInfoCount: 0}); 232 this.expectedInfoCount: 0});
175 } 233 }
176 234
177 Future analyze(String serializedData, Uri entryPoint, Test test) async { 235 Future analyze(String serializedData, Uri entryPoint, Test test) async {
178 Deserializer deserializer = new Deserializer.fromText(
179 serializedData, const JsonSerializationDecoder());
180 DiagnosticCollector diagnosticCollector = new DiagnosticCollector(); 236 DiagnosticCollector diagnosticCollector = new DiagnosticCollector();
181 await runCompiler( 237 await runCompiler(
182 entryPoint: entryPoint, 238 entryPoint: entryPoint,
183 memorySourceFiles: test != null ? test.sourceFiles : const {}, 239 memorySourceFiles: test != null ? test.sourceFiles : const {},
184 options: [Flags.analyzeOnly], 240 options: [Flags.analyzeOnly],
185 diagnosticHandler: diagnosticCollector, 241 diagnosticHandler: diagnosticCollector,
186 beforeRun: (Compiler compiler) { 242 beforeRun: (Compiler compiler) {
243 Deserializer deserializer = new Deserializer.fromText(
244 serializedData,
245 const JsonSerializationDecoder());
246 deserializer.plugins.add(compiler.backend.serialization.deserializer);
187 compiler.serialization.deserializer = 247 compiler.serialization.deserializer =
188 new _DeserializerSystem(deserializer); 248 new _DeserializerSystem(deserializer);
189 }); 249 });
190 if (test != null) { 250 if (test != null) {
191 Expect.equals(test.expectedErrorCount, diagnosticCollector.errors.length, 251 Expect.equals(test.expectedErrorCount, diagnosticCollector.errors.length,
192 "Unexpected error count."); 252 "Unexpected error count.");
193 Expect.equals( 253 Expect.equals(
194 test.expectedWarningCount, 254 test.expectedWarningCount,
195 diagnosticCollector.warnings.length, 255 diagnosticCollector.warnings.length,
196 "Unexpected warning count."); 256 "Unexpected warning count.");
197 Expect.equals(test.expectedHintCount, diagnosticCollector.hints.length, 257 Expect.equals(test.expectedHintCount, diagnosticCollector.hints.length,
198 "Unexpected hint count."); 258 "Unexpected hint count.");
199 Expect.equals(test.expectedInfoCount, diagnosticCollector.infos.length, 259 Expect.equals(test.expectedInfoCount, diagnosticCollector.infos.length,
200 "Unexpected info count."); 260 "Unexpected info count.");
201 } 261 }
202 } 262 }
203 263
204 Future<String> serializeDartCore() async { 264 Future<String> serializeDartCore() async {
205 Compiler compiler = compilerFor( 265 Compiler compiler = compilerFor(
206 options: ['--analyze-all']); 266 options: ['--analyze-all']);
267 compiler.serialization.supportSerialization = true;
207 await compiler.run(Uri.parse('dart:core')); 268 await compiler.run(Uri.parse('dart:core'));
208 return serialize(compiler.libraryLoader.libraries); 269 return serialize(compiler);
209 } 270 }
210 271
211 String serialize(Iterable<LibraryElement> libraries) { 272 String serialize(Compiler compiler) {
212 Serializer serializer = new Serializer(const JsonSerializationEncoder()); 273 Serializer serializer = new Serializer(const JsonSerializationEncoder());
213 for (LibraryElement library in libraries) { 274 serializer.plugins.add(compiler.backend.serialization.serializer);
275 serializer.plugins.add(new WorldImpactSerializer(compiler.resolution));
276
277 for (LibraryElement library in compiler.libraryLoader.libraries) {
214 serializer.serialize(library); 278 serializer.serialize(library);
215 } 279 }
216 return serializer.toText(); 280 return serializer.toText();
217 } 281 }
218 282
283 const String WORLD_IMPACT_TAG = 'worldImpact';
284
285 class WorldImpactSerializer extends SerializerPlugin {
286 final Resolution resolution;
287
288 WorldImpactSerializer(this.resolution);
289
290 @override
291 void onElement(Element element, ObjectEncoder createEncoder(String tag)) {
292 if (resolution.hasBeenResolved(element)) {
293 WorldImpact impact = resolution.getWorldImpact(element);
294 ObjectEncoder encoder = createEncoder(WORLD_IMPACT_TAG);
295 impact.apply(new ImpactSerializer(encoder));
296 }
297 }
298 }
299
300 class WorldImpactDeserializer extends DeserializerPlugin {
301 Map<Element, WorldImpact> impactMap = <Element, WorldImpact>{};
302
303 @override
304 void onElement(Element element, ObjectDecoder getDecoder(String tag)) {
305 ObjectDecoder decoder = getDecoder(WORLD_IMPACT_TAG);
306 if (decoder != null) {
307 impactMap[element] = ImpactDeserializer.deserializeImpact(decoder);
308 }
309 }
310 }
311
219 class _DeserializerSystem extends DeserializerSystem { 312 class _DeserializerSystem extends DeserializerSystem {
220 final Deserializer _deserializer; 313 final Deserializer _deserializer;
221 final List<LibraryElement> deserializedLibraries = <LibraryElement>[]; 314 final List<LibraryElement> deserializedLibraries = <LibraryElement>[];
315 final WorldImpactDeserializer _worldImpactDeserializer =
316 new WorldImpactDeserializer();
222 317
223 _DeserializerSystem(this._deserializer); 318 _DeserializerSystem(this._deserializer) {
319 _deserializer.plugins.add(_worldImpactDeserializer);
320 }
224 321
225 LibraryElement readLibrary(Uri resolvedUri) { 322 LibraryElement readLibrary(Uri resolvedUri) {
226 LibraryElement library = _deserializer.lookupLibrary(resolvedUri); 323 LibraryElement library = _deserializer.lookupLibrary(resolvedUri);
227 if (library != null) { 324 if (library != null) {
228 deserializedLibraries.add(library); 325 deserializedLibraries.add(library);
229 } 326 }
230 return library; 327 return library;
231 } 328 }
232 329
233 @override 330 @override
234 WorldImpact computeWorldImpact(Element element) { 331 WorldImpact computeWorldImpact(Element element) {
235 return const WorldImpact(); 332 WorldImpact impact = _worldImpactDeserializer.impactMap[element];
333 if (impact == null) {
334 print('No impact found for $element (${element.library})');
335 impact = const WorldImpact();
336 }
337 return impact;
236 } 338 }
237 339
238 @override 340 @override
239 bool isDeserialized(Element element) { 341 bool isDeserialized(Element element) {
240 return deserializedLibraries.contains(element.library); 342 return deserializedLibraries.contains(element.library);
241 } 343 }
242 } 344 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698