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 the additional runtime type support is output to the right | 5 // Test that we do not accidentially leak code from deferred libraries but do |
6 // Files when using deferred loading. | 6 // allow inlining of empty functions and from main. |
7 | 7 |
8 import 'package:expect/expect.dart'; | 8 import 'package:expect/expect.dart'; |
9 import "package:async_helper/async_helper.dart"; | 9 import "package:async_helper/async_helper.dart"; |
10 import 'memory_source_file_helper.dart'; | 10 import 'memory_source_file_helper.dart'; |
11 import "dart:async"; | 11 import "dart:async"; |
12 | 12 |
13 import 'package:compiler/src/dart2jslib.dart' | 13 import 'package:compiler/src/dart2jslib.dart' |
14 as dart2js; | 14 as dart2js; |
15 | 15 |
16 class MemoryOutputSink extends EventSink<String> { | 16 class MemoryOutputSink extends EventSink<String> { |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
48 handler.diagnosticHandler, | 48 handler.diagnosticHandler, |
49 libraryRoot, | 49 libraryRoot, |
50 packageRoot, | 50 packageRoot, |
51 [], | 51 [], |
52 {}); | 52 {}); |
53 asyncTest(() => compiler.run(Uri.parse('memory:main.dart')).then((_) { | 53 asyncTest(() => compiler.run(Uri.parse('memory:main.dart')).then((_) { |
54 lookupLibrary(name) { | 54 lookupLibrary(name) { |
55 return compiler.libraryLoader.lookupLibrary(Uri.parse(name)); | 55 return compiler.libraryLoader.lookupLibrary(Uri.parse(name)); |
56 } | 56 } |
57 | 57 |
58 var main = compiler.mainApp.find(dart2js.Compiler.MAIN); | |
59 Expect.isNotNull(main, "Could not find 'main'"); | |
60 compiler.deferredLoadTask.onResolutionComplete(main); | |
61 | |
62 var outputUnitForElement = compiler.deferredLoadTask.outputUnitForElement; | 58 var outputUnitForElement = compiler.deferredLoadTask.outputUnitForElement; |
63 | 59 |
64 var lib1 = lookupLibrary("memory:lib1.dart"); | 60 var lib1 = lookupLibrary("memory:lib1.dart"); |
65 var foo1 = lib1.find("finalVar"); | 61 var inlineMeAway = lib1.find("inlineMeAway"); |
66 var ou_lib1 = outputUnitForElement(foo1); | 62 var ou_lib1 = outputUnitForElement(inlineMeAway); |
| 63 |
| 64 var lib3 = lookupLibrary("memory:lib3.dart"); |
| 65 var sameContextInline = lib3.find("sameContextInline"); |
| 66 var ou_lib3 = outputUnitForElement(sameContextInline); |
| 67 |
| 68 // Test that we actually got differnt output units. |
| 69 Expect.notEquals(ou_lib1.name, ou_lib3.name); |
67 | 70 |
68 String mainOutput = outputs["main.js"].mem.toString(); | 71 String mainOutput = outputs["main.js"].mem.toString(); |
69 String lib1Output = outputs["out_${ou_lib1.name}.part.js"].mem.toString(); | 72 String lib1Output = outputs["out_${ou_lib1.name}.part.js"].mem.toString(); |
70 // Test that the deferred globals are not inlined into the main file. | 73 String lib3Output = outputs["out_${ou_lib3.name}.part.js"].mem.toString(); |
71 RegExp re1 = new RegExp(r"= .string1"); | 74 |
72 RegExp re2 = new RegExp(r"= .string2"); | 75 RegExp re1 = new RegExp(r"inlined as empty"); |
73 Expect.isTrue(re1.hasMatch(lib1Output)); | 76 RegExp re2 = new RegExp(r"inlined from main"); |
| 77 RegExp re3 = new RegExp(r"inlined from lib1"); |
| 78 RegExp re4 = new RegExp(r"inline same context"); |
| 79 |
| 80 // Test that inlineMeAway was inlined and its argument thus dropped. |
| 81 Expect.isFalse(re1.hasMatch(mainOutput)); |
| 82 |
| 83 // Test that inlineFromMain was inlined and thus the string moved to lib1. |
| 84 Expect.isFalse(re2.hasMatch(mainOutput)); |
74 Expect.isTrue(re2.hasMatch(lib1Output)); | 85 Expect.isTrue(re2.hasMatch(lib1Output)); |
75 Expect.isFalse(re1.hasMatch(mainOutput)); | 86 |
76 Expect.isFalse(re2.hasMatch(mainOutput)); | 87 // Test that inlineFromLib1 was not inlined into main. |
| 88 Expect.isFalse(re3.hasMatch(mainOutput)); |
| 89 Expect.isTrue(re3.hasMatch(lib1Output)); |
| 90 |
| 91 // Test that inlineSameContext was inlined into lib1. |
| 92 Expect.isFalse(re4.hasMatch(lib3Output)); |
| 93 Expect.isTrue(re4.hasMatch(lib1Output)); |
77 })); | 94 })); |
78 } | 95 } |
79 | 96 |
80 // Make sure that deferred constants are not inlined into the main hunk. | 97 // Make sure that empty functions are inlined and that functions from |
| 98 // main also are inlined (assuming normal heuristics). |
81 const Map MEMORY_SOURCE_FILES = const {"main.dart": """ | 99 const Map MEMORY_SOURCE_FILES = const {"main.dart": """ |
82 import "dart:async"; | 100 import "dart:async"; |
83 | 101 |
84 import 'lib1.dart' deferred as lib1; | 102 import 'lib1.dart' deferred as lib1; |
| 103 import 'lib2.dart' deferred as lib2; |
| 104 |
| 105 inlineFromMain(x) => "inlined from main" + x; |
85 | 106 |
86 void main() { | 107 void main() { |
87 lib1.loadLibrary().then((_) { | 108 lib1.loadLibrary().then((_) { |
88 print(lib1.finalVar); | 109 lib2.loadLibrary().then((_) { |
89 print(lib1.globalVar); | 110 lib1.test(); |
90 lib1.globalVar = "foobar"; | 111 lib2.test(); |
91 print(lib1.globalVar); | 112 print(lib1.inlineMeAway("inlined as empty")); |
| 113 print(lib1.inlineFromLib1("should stay")); |
| 114 }); |
92 }); | 115 }); |
93 } | 116 } |
94 """, "lib1.dart": """ | 117 """, "lib1.dart": """ |
95 import "main.dart" as main; | 118 import "main.dart" as main; |
96 final finalVar = "string1"; | 119 import "lib3.dart" as lib3; |
97 var globalVar = "string2"; | 120 |
| 121 inlineMeAway(x) {} |
| 122 |
| 123 inlineFromLib1(x) => "inlined from lib1" + x; |
| 124 |
| 125 test() { |
| 126 print(main.inlineFromMain("should be inlined")); |
| 127 print(lib3.sameContextInline("should be inlined")); |
| 128 } |
| 129 """, "lib2.dart": """ |
| 130 import "lib3.dart" as lib3; |
| 131 |
| 132 test() { |
| 133 print(lib3.sameContextInline("should be inlined")); |
| 134 } |
| 135 """, "lib3.dart": """ |
| 136 sameContextInline(x) => "inline same context" + x; |
98 """}; | 137 """}; |
OLD | NEW |