OLD | NEW |
1 // Copyright (c) 2014, 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 that we do not accidentially leak code from deferred libraries but do | 5 // Test that we do not accidentially leak code from deferred libraries but do |
6 // allow inlining of empty functions and from main. | 6 // allow inlining of empty functions and from main. |
7 | 7 |
8 import 'package:async_helper/async_helper.dart'; | 8 import 'package:async_helper/async_helper.dart'; |
9 import 'package:compiler/src/compiler.dart'; | 9 import 'package:compiler/src/compiler.dart'; |
10 import 'package:expect/expect.dart'; | 10 import 'package:expect/expect.dart'; |
11 import 'memory_compiler.dart'; | 11 import 'memory_compiler.dart'; |
12 | 12 |
13 void main() { | 13 void main() { |
14 asyncTest(() async { | 14 asyncTest(() async { |
15 OutputCollector collector = new OutputCollector(); | 15 OutputCollector collector = new OutputCollector(); |
16 CompilationResult result = await runCompiler( | 16 CompilationResult result = await runCompiler( |
17 memorySourceFiles: MEMORY_SOURCE_FILES, | 17 memorySourceFiles: MEMORY_SOURCE_FILES, outputProvider: collector); |
18 outputProvider: collector); | |
19 Compiler compiler = result.compiler; | 18 Compiler compiler = result.compiler; |
20 | 19 |
21 lookupLibrary(name) { | 20 lookupLibrary(name) { |
22 return compiler.libraryLoader.lookupLibrary(Uri.parse(name)); | 21 return compiler.libraryLoader.lookupLibrary(Uri.parse(name)); |
23 } | 22 } |
24 | 23 |
25 var outputUnitForElement = compiler.deferredLoadTask.outputUnitForElement; | 24 var outputUnitForElement = compiler.deferredLoadTask.outputUnitForElement; |
26 | 25 |
27 var lib1 = lookupLibrary("memory:lib1.dart"); | 26 var lib1 = lookupLibrary("memory:lib1.dart"); |
28 var inlineMeAway = lib1.find("inlineMeAway"); | 27 var inlineMeAway = lib1.find("inlineMeAway"); |
(...skipping 27 matching lines...) Expand all Loading... |
56 Expect.isTrue(re3.hasMatch(lib1Output)); | 55 Expect.isTrue(re3.hasMatch(lib1Output)); |
57 | 56 |
58 // Test that inlineSameContext was inlined into lib1. | 57 // Test that inlineSameContext was inlined into lib1. |
59 Expect.isFalse(re4.hasMatch(lib3Output)); | 58 Expect.isFalse(re4.hasMatch(lib3Output)); |
60 Expect.isTrue(re4.hasMatch(lib1Output)); | 59 Expect.isTrue(re4.hasMatch(lib1Output)); |
61 }); | 60 }); |
62 } | 61 } |
63 | 62 |
64 // Make sure that empty functions are inlined and that functions from | 63 // Make sure that empty functions are inlined and that functions from |
65 // main also are inlined (assuming normal heuristics). | 64 // main also are inlined (assuming normal heuristics). |
66 const Map MEMORY_SOURCE_FILES = const {"main.dart": """ | 65 const Map MEMORY_SOURCE_FILES = const { |
| 66 "main.dart": """ |
67 import "dart:async"; | 67 import "dart:async"; |
68 | 68 |
69 import 'lib1.dart' deferred as lib1; | 69 import 'lib1.dart' deferred as lib1; |
70 import 'lib2.dart' deferred as lib2; | 70 import 'lib2.dart' deferred as lib2; |
71 | 71 |
72 inlineFromMain(x) => "inlined from main" + x; | 72 inlineFromMain(x) => "inlined from main" + x; |
73 | 73 |
74 void main() { | 74 void main() { |
75 lib1.loadLibrary().then((_) { | 75 lib1.loadLibrary().then((_) { |
76 lib2.loadLibrary().then((_) { | 76 lib2.loadLibrary().then((_) { |
77 lib1.test(); | 77 lib1.test(); |
78 lib2.test(); | 78 lib2.test(); |
79 print(lib1.inlineMeAway("inlined as empty")); | 79 print(lib1.inlineMeAway("inlined as empty")); |
80 print(lib1.inlineFromLib1("should stay")); | 80 print(lib1.inlineFromLib1("should stay")); |
81 }); | 81 }); |
82 }); | 82 }); |
83 } | 83 } |
84 """, "lib1.dart": """ | 84 """, |
| 85 "lib1.dart": """ |
85 import "main.dart" as main; | 86 import "main.dart" as main; |
86 import "lib3.dart" as lib3; | 87 import "lib3.dart" as lib3; |
87 | 88 |
88 inlineMeAway(x) {} | 89 inlineMeAway(x) {} |
89 | 90 |
90 inlineFromLib1(x) => "inlined from lib1" + x; | 91 inlineFromLib1(x) => "inlined from lib1" + x; |
91 | 92 |
92 test() { | 93 test() { |
93 print(main.inlineFromMain("should be inlined")); | 94 print(main.inlineFromMain("should be inlined")); |
94 print(lib3.sameContextInline("should be inlined")); | 95 print(lib3.sameContextInline("should be inlined")); |
95 } | 96 } |
96 """, "lib2.dart": """ | 97 """, |
| 98 "lib2.dart": """ |
97 import "lib3.dart" as lib3; | 99 import "lib3.dart" as lib3; |
98 | 100 |
99 test() { | 101 test() { |
100 print(lib3.sameContextInline("should be inlined")); | 102 print(lib3.sameContextInline("should be inlined")); |
101 } | 103 } |
102 """, "lib3.dart": """ | 104 """, |
| 105 "lib3.dart": """ |
103 sameContextInline(x) => "inline same context" + x; | 106 sameContextInline(x) => "inline same context" + x; |
104 """}; | 107 """ |
| 108 }; |
OLD | NEW |