| OLD | NEW |
| 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"); |
| 38 var field2 = lib2.find("field2"); |
| 50 | 39 |
| 51 var outputClassLists = compiler.backend.emitter.outputClassLists; | 40 Expect.notEquals(outputUnitForElement(main), outputUnitForElement(foo1)); |
| 41 Expect.equals(outputUnitForElement(foo1), outputUnitForElement(sin)); |
| 42 Expect.equals(outputUnitForElement(foo2), outputUnitForElement(sin)); |
| 43 Expect.equals(outputUnitForElement(foo2), outputUnitForElement(field2)); |
| 44 }); |
| 45 runTest('memory:main2.dart', (compiler) { |
| 46 // Just check that the compile runs. |
| 47 // This is a regression test. |
| 48 Expect.isTrue(true); |
| 49 }); |
| 50 runTest('memory:main3.dart', (compiler) { |
| 51 var main = compiler.mainApp.find(dart2js.Compiler.MAIN); |
| 52 Expect.isNotNull(main, "Could not find 'main'"); |
| 53 compiler.deferredLoadTask.onResolutionComplete(main); |
| 54 var outputUnitForElement = compiler.deferredLoadTask.outputUnitForElement; |
| 52 | 55 |
| 53 Expect.notEquals(mainOutputUnit, outputUnitForElement(foo1)); | 56 var mainLib = compiler.libraries["memory:main3.dart"]; |
| 54 Expect.equals(outputUnitForElement(foo1), outputUnitForElement(sin)); | 57 var lib3 = compiler.libraries["memory:lib3.dart"]; |
| 55 })); | 58 var C = mainLib.find("C"); |
| 59 var foo = lib3.find("foo"); |
| 60 |
| 61 Expect.notEquals(outputUnitForElement(main), outputUnitForElement(foo)); |
| 62 Expect.equals(outputUnitForElement(foo), outputUnitForElement(C)); |
| 63 }); |
| 56 } | 64 } |
| 57 | 65 |
| 58 // "lib1.dart" uses mirrors without a MirrorsUsed annotation, so everything | 66 // "lib1.dart" uses mirrors without a MirrorsUsed annotation, so everything |
| 59 // should be put in the "lib1" output unit. | 67 // should be put in the "lib1" output unit. |
| 60 const Map MEMORY_SOURCE_FILES = const { | 68 const Map MEMORY_SOURCE_FILES = const { |
| 61 "main.dart":""" | 69 "main.dart": """ |
| 62 import "dart:async"; | |
| 63 import "dart:math"; | 70 import "dart:math"; |
| 64 | 71 |
| 65 @def1 import 'lib1.dart' as lib1; | 72 import 'lib1.dart' deferred as lib1; |
| 66 @def2 import 'lib2.dart' as lib2; | 73 import 'lib2.dart' deferred as lib2; |
| 67 | |
| 68 const def1 = const DeferredLibrary("lib1"); | |
| 69 const def2 = const DeferredLibrary("lib2"); | |
| 70 | 74 |
| 71 void main() { | 75 void main() { |
| 72 def1.load().then((_) { | 76 lib1.loadLibrary().then((_) { |
| 73 lib1.foo1(); | 77 lib1.foo1(); |
| 74 }); | 78 }); |
| 75 def2.load().then((_) { | 79 lib2.loadLibrary().then((_) { |
| 76 lib2.foo2(); | 80 lib2.foo2(); |
| 77 }); | 81 }); |
| 78 } | 82 } |
| 79 """, | 83 """, |
| 80 "lib1.dart":""" | 84 "lib1.dart":""" |
| 81 library lib1; | 85 library lib1; |
| 82 import "dart:mirrors"; | 86 import "dart:mirrors"; |
| 83 | 87 |
| 84 const field1 = 42; | 88 const field1 = 42; |
| 85 | 89 |
| 86 void foo1() { | 90 void foo1() { |
| 87 var mirror = reflect(field1); | 91 var mirror = reflect(field1); |
| 88 mirror.invoke(null, null); | 92 mirror.invoke(null, null); |
| 89 } | 93 } |
| 90 """, | 94 """, |
| 91 "lib2.dart":""" | 95 "lib2.dart":""" |
| 92 library lib2; | 96 library lib2; |
| 93 @MirrorsUsed(targets: "field2") import "dart:mirrors"; | 97 @MirrorsUsed(targets: "field2") import "dart:mirrors"; |
| 94 | 98 |
| 95 const field2 = 42; | 99 const field2 = 42; |
| 96 | 100 |
| 97 void foo2() { | 101 void foo2() { |
| 98 var mirror = reflect(field2); | 102 var mirror = reflect(field2); |
| 99 mirror.invoke(null, null); | 103 mirror.invoke(null, null); |
| 100 } | 104 } |
| 101 """, | 105 """, |
| 106 // The elements C and f are named as targets, but there is no actual use of |
| 107 // mirrors. |
| 108 "main2.dart": """ |
| 109 import "lib.dart" deferred as lib; |
| 110 |
| 111 @MirrorsUsed(targets: const ["C", "f"]) |
| 112 import "dart:mirrors"; |
| 113 |
| 114 class C {} |
| 115 |
| 116 var f = 3; |
| 117 |
| 118 void main() { |
| 119 |
| 120 } |
| 121 """, |
| 122 "lib.dart": """ """, |
| 123 // Lib3 has a MirrorsUsed annotation with a library. |
| 124 // Check that that is handled correctly. |
| 125 "main3.dart": """ |
| 126 library main3; |
| 127 |
| 128 import "lib3.dart" deferred as lib; |
| 129 |
| 130 class C {} |
| 131 |
| 132 class D {} |
| 133 |
| 134 f() {} |
| 135 |
| 136 void main() { |
| 137 lib.loadLibrary().then((_) { |
| 138 lib.foo(); |
| 139 }); |
| 140 } |
| 141 """, |
| 142 "lib3.dart": """ |
| 143 @MirrorsUsed(targets: const ["main3.C"]) |
| 144 import "dart:mirrors"; |
| 145 |
| 146 foo() { |
| 147 currentMirrorSystem().findLibrary(#main3); |
| 148 } |
| 149 """, |
| 102 }; | 150 }; |
| OLD | NEW |