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

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

Issue 220203004: Improve the mirror support for deferred loading. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 6 years, 8 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 | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2014, 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 // Test of the graph segmentation algorithm used by deferred loading 5 // Test of the graph segmentation algorithm used by deferred loading
6 // to determine which elements can be deferred and which libraries 6 // to determine which elements can be deferred and which libraries
7 // much be included in the initial download (loaded eagerly). 7 // much be included in the initial download (loaded eagerly).
8 8
9 import 'package:expect/expect.dart'; 9 import 'package:expect/expect.dart';
10 import "package:async_helper/async_helper.dart"; 10 import "package:async_helper/async_helper.dart";
11 import 'memory_source_file_helper.dart'; 11 import 'memory_source_file_helper.dart';
12 import "dart:async"; 12 import "dart:async";
13 import "memory_compiler.dart";
13 14
14 import '../../../sdk/lib/_internal/compiler/implementation/dart2jslib.dart' 15 import '../../../sdk/lib/_internal/compiler/implementation/dart2jslib.dart'
15 as dart2js; 16 as dart2js;
16 17
17 class FakeOutputStream<T> extends EventSink<T> { 18 runTest(String mainScript, test) {
18 void add(T event) {} 19 Compiler compiler = compilerFor(MEMORY_SOURCE_FILES,
19 void addError(T event, [StackTrace stackTrace]) {} 20 outputProvider: new OutputCollector());
20 void close() {} 21 asyncTest(() => compiler.run(Uri.parse(mainScript))
22 .then((_) => test(compiler)));
21 } 23 }
22 24
23 void main() { 25 void main() {
24 Uri script = currentDirectory.resolveUri(Platform.script); 26 runTest('memory:main.dart', (compiler) {
25 Uri libraryRoot = script.resolve('../../../sdk/');
26 Uri packageRoot = script.resolve('./packages/');
27
28 var provider = new MemorySourceFileProvider(MEMORY_SOURCE_FILES);
29 var handler = new FormattingDiagnosticHandler(provider);
30
31 Compiler compiler = new Compiler(provider.readStringFromUri,
32 (name, extension) => new FakeOutputStream(),
33 handler.diagnosticHandler,
34 libraryRoot,
35 packageRoot,
36 [],
37 {});
38 asyncTest(() => compiler.run(Uri.parse('memory:main.dart')).then((_) {
39 var main = compiler.mainApp.find(dart2js.Compiler.MAIN); 27 var main = compiler.mainApp.find(dart2js.Compiler.MAIN);
28 Expect.isNotNull(main, "Could not find 'main'");
29 compiler.deferredLoadTask.onResolutionComplete(main);
40 var outputUnitForElement = compiler.deferredLoadTask.outputUnitForElement; 30 var outputUnitForElement = compiler.deferredLoadTask.outputUnitForElement;
41 31
42 var mainOutputUnit = compiler.deferredLoadTask.mainOutputUnit;
43 var classes = compiler.backend.emitter.neededClasses;
44 var lib1 = compiler.libraries["memory:lib1.dart"]; 32 var lib1 = compiler.libraries["memory:lib1.dart"];
45 var lib2 = compiler.libraries["memory:lib2.dart"]; 33 var lib2 = compiler.libraries["memory:lib2.dart"];
46 var mathLib = compiler.libraries["dart:math"]; 34 var mathLib = compiler.libraries["dart:math"];
47 var sin = mathLib.find('sin'); 35 var sin = mathLib.find('sin');
48 var foo1 = lib1.find("foo1"); 36 var foo1 = lib1.find("foo1");
49 var foo2 = lib2.find("foo2"); 37 var foo2 = lib2.find("foo2");
Johnni Winther 2014/04/01 13:20:00 Shouldn't this be checked?
sigurdm 2014/04/01 13:57:44 Done.
50 38
51 var outputClassLists = compiler.backend.emitter.outputClassLists; 39 Expect.notEquals(outputUnitForElement(main), outputUnitForElement(foo1));
40 Expect.equals(outputUnitForElement(foo1), outputUnitForElement(sin));
41 });
42 runTest('memory:main2.dart', (compiler) {
43 // Just check that the compile runs.
44 // This is a regression test.
45 Expect.isTrue(true);
46 });
47 runTest('memory:main3.dart', (compiler) {
48 var main = compiler.mainApp.find(dart2js.Compiler.MAIN);
49 Expect.isNotNull(main, "Could not find 'main'");
50 compiler.deferredLoadTask.onResolutionComplete(main);
51 var outputUnitForElement = compiler.deferredLoadTask.outputUnitForElement;
52 52
53 Expect.notEquals(mainOutputUnit, outputUnitForElement(foo1)); 53 var mainLib = compiler.libraries["memory:main3.dart"];
54 Expect.equals(outputUnitForElement(foo1), outputUnitForElement(sin)); 54 var lib3 = compiler.libraries["memory:lib3.dart"];
55 })); 55 var C = mainLib.find("C");
56 var foo = lib3.find("foo");
57
58 Expect.notEquals(outputUnitForElement(main), outputUnitForElement(foo));
59 Expect.equals(outputUnitForElement(foo), outputUnitForElement(C));
60 });
56 } 61 }
57 62
58 // "lib1.dart" uses mirrors without a MirrorsUsed annotation, so everything 63 // "lib1.dart" uses mirrors without a MirrorsUsed annotation, so everything
59 // should be put in the "lib1" output unit. 64 // should be put in the "lib1" output unit.
60 const Map MEMORY_SOURCE_FILES = const { 65 const Map MEMORY_SOURCE_FILES = const {
61 "main.dart":""" 66 "main.dart": """
62 import "dart:async";
63 import "dart:math"; 67 import "dart:math";
64 68
65 @def1 import 'lib1.dart' as lib1; 69 import 'lib1.dart' deferred as lib1;
66 @def2 import 'lib2.dart' as lib2; 70 import 'lib2.dart' deferred as lib2;
67
68 const def1 = const DeferredLibrary("lib1");
69 const def2 = const DeferredLibrary("lib2");
70 71
71 void main() { 72 void main() {
72 def1.load().then((_) { 73 lib1.loadLibrary().then((_) {
73 lib1.foo1(); 74 lib1.foo1();
74 }); 75 });
75 def2.load().then((_) { 76 lib2.loadLibrary().then((_) {
76 lib2.foo2(); 77 lib2.foo2();
77 }); 78 });
78 } 79 }
79 """, 80 """,
80 "lib1.dart":""" 81 "lib1.dart":"""
81 library lib1; 82 library lib1;
82 import "dart:mirrors"; 83 import "dart:mirrors";
83 84
84 const field1 = 42; 85 const field1 = 42;
85 86
86 void foo1() { 87 void foo1() {
87 var mirror = reflect(field1); 88 var mirror = reflect(field1);
88 mirror.invoke(null, null); 89 mirror.invoke(null, null);
89 } 90 }
90 """, 91 """,
91 "lib2.dart":""" 92 "lib2.dart":"""
92 library lib2; 93 library lib2;
93 @MirrorsUsed(targets: "field2") import "dart:mirrors"; 94 @MirrorsUsed(targets: "field2") import "dart:mirrors";
94 95
95 const field2 = 42; 96 const field2 = 42;
96 97
97 void foo2() { 98 void foo2() {
98 var mirror = reflect(field2); 99 var mirror = reflect(field2);
99 mirror.invoke(null, null); 100 mirror.invoke(null, null);
100 } 101 }
101 """, 102 """,
103 // The elements C and f are named as targets, but there is no actual use of
104 // mirrors.
105 "main2.dart": """
106 import "lib.dart" deferred as lib;
107
108 @MirrorsUsed(targets: const ["C", "f"])
109 import "dart:mirrors";
110
111 class C {}
112
113 var f = 3;
114
115 void main() {
116
117 }
118 """,
119 "lib.dart": """ """,
120 // Lib3 has a MirrorsUsed annotation with a library.
121 // Check that that is handled correctly.
122 "main3.dart": """
123 library main3;
124
125 import "lib3.dart" deferred as lib;
126
127 class C {}
128
129 class D {}
130
131 f() {}
132
133 void main() {
134 lib.loadLibrary().then((_) {
135 lib.foo();
136 });
137 }
138 """,
139 "lib3.dart": """
140 @MirrorsUsed(targets: const ["main3.C"])
141 import "dart:mirrors";
142
143 foo() {
144 currentMirrorSystem().findLibrary(#main3);
145 }
146 """,
102 }; 147 };
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698