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'; |
| 9 import 'package:compiler/src/dart2jslib.dart'; |
8 import 'package:expect/expect.dart'; | 10 import 'package:expect/expect.dart'; |
9 import "package:async_helper/async_helper.dart"; | 11 import 'memory_compiler.dart'; |
10 import 'memory_source_file_helper.dart'; | 12 import 'output_collector.dart'; |
11 import "dart:async"; | |
12 | |
13 import 'package:compiler/src/dart2jslib.dart' | |
14 as dart2js; | |
15 | |
16 class MemoryOutputSink extends EventSink<String> { | |
17 StringBuffer mem = new StringBuffer(); | |
18 void add(String event) { | |
19 mem.write(event); | |
20 } | |
21 void addError(String event, [StackTrace stackTrace]) { | |
22 Expect.isTrue(false); | |
23 } | |
24 void close() {} | |
25 } | |
26 | 13 |
27 void main() { | 14 void main() { |
28 Uri script = currentDirectory.resolveUri(Platform.script); | 15 OutputCollector collector = new OutputCollector(); |
29 Uri libraryRoot = script.resolve('../../../sdk/'); | 16 Compiler compiler = compilerFor( |
30 Uri packageRoot = script.resolve('./packages/'); | 17 MEMORY_SOURCE_FILES, |
31 | 18 outputProvider: collector); |
32 var provider = new MemorySourceFileProvider(MEMORY_SOURCE_FILES); | |
33 var handler = new FormattingDiagnosticHandler(provider); | |
34 | |
35 Map<String, MemoryOutputSink> outputs = new Map<String, MemoryOutputSink>(); | |
36 | |
37 MemoryOutputSink outputSaver(name, extension) { | |
38 if (name == '') { | |
39 name = 'main'; | |
40 } | |
41 return outputs.putIfAbsent("$name.$extension", () { | |
42 return new MemoryOutputSink(); | |
43 }); | |
44 } | |
45 | |
46 Compiler compiler = new Compiler(provider.readStringFromUri, | |
47 outputSaver, | |
48 handler.diagnosticHandler, | |
49 libraryRoot, | |
50 packageRoot, | |
51 [], | |
52 {}); | |
53 asyncTest(() => compiler.run(Uri.parse('memory:main.dart')).then((_) { | 19 asyncTest(() => compiler.run(Uri.parse('memory:main.dart')).then((_) { |
54 lookupLibrary(name) { | 20 lookupLibrary(name) { |
55 return compiler.libraryLoader.lookupLibrary(Uri.parse(name)); | 21 return compiler.libraryLoader.lookupLibrary(Uri.parse(name)); |
56 } | 22 } |
57 | 23 |
58 var outputUnitForElement = compiler.deferredLoadTask.outputUnitForElement; | 24 var outputUnitForElement = compiler.deferredLoadTask.outputUnitForElement; |
59 | 25 |
60 var lib1 = lookupLibrary("memory:lib1.dart"); | 26 var lib1 = lookupLibrary("memory:lib1.dart"); |
61 var inlineMeAway = lib1.find("inlineMeAway"); | 27 var inlineMeAway = lib1.find("inlineMeAway"); |
62 var ou_lib1 = outputUnitForElement(inlineMeAway); | 28 var ou_lib1 = outputUnitForElement(inlineMeAway); |
63 | 29 |
64 var lib3 = lookupLibrary("memory:lib3.dart"); | 30 var lib3 = lookupLibrary("memory:lib3.dart"); |
65 var sameContextInline = lib3.find("sameContextInline"); | 31 var sameContextInline = lib3.find("sameContextInline"); |
66 var ou_lib3 = outputUnitForElement(sameContextInline); | 32 var ou_lib3 = outputUnitForElement(sameContextInline); |
67 | 33 |
68 // Test that we actually got differnt output units. | 34 // Test that we actually got differnt output units. |
69 Expect.notEquals(ou_lib1.name, ou_lib3.name); | 35 Expect.notEquals(ou_lib1.name, ou_lib3.name); |
70 | 36 |
71 String mainOutput = outputs["main.js"].mem.toString(); | 37 String mainOutput = collector.getOutput("", "js"); |
72 String lib1Output = outputs["out_${ou_lib1.name}.part.js"].mem.toString(); | 38 String lib1Output = collector.getOutput("out_${ou_lib1.name}", "part.js"); |
73 String lib3Output = outputs["out_${ou_lib3.name}.part.js"].mem.toString(); | 39 String lib3Output = collector.getOutput("out_${ou_lib3.name}", "part.js"); |
74 | 40 |
75 RegExp re1 = new RegExp(r"inlined as empty"); | 41 RegExp re1 = new RegExp(r"inlined as empty"); |
76 RegExp re2 = new RegExp(r"inlined from main"); | 42 RegExp re2 = new RegExp(r"inlined from main"); |
77 RegExp re3 = new RegExp(r"inlined from lib1"); | 43 RegExp re3 = new RegExp(r"inlined from lib1"); |
78 RegExp re4 = new RegExp(r"inline same context"); | 44 RegExp re4 = new RegExp(r"inline same context"); |
79 | 45 |
80 // Test that inlineMeAway was inlined and its argument thus dropped. | 46 // Test that inlineMeAway was inlined and its argument thus dropped. |
81 Expect.isFalse(re1.hasMatch(mainOutput)); | 47 Expect.isFalse(re1.hasMatch(mainOutput)); |
82 | 48 |
83 // Test that inlineFromMain was inlined and thus the string moved to lib1. | 49 // Test that inlineFromMain was inlined and thus the string moved to lib1. |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
128 } | 94 } |
129 """, "lib2.dart": """ | 95 """, "lib2.dart": """ |
130 import "lib3.dart" as lib3; | 96 import "lib3.dart" as lib3; |
131 | 97 |
132 test() { | 98 test() { |
133 print(lib3.sameContextInline("should be inlined")); | 99 print(lib3.sameContextInline("should be inlined")); |
134 } | 100 } |
135 """, "lib3.dart": """ | 101 """, "lib3.dart": """ |
136 sameContextInline(x) => "inline same context" + x; | 102 sameContextInline(x) => "inline same context" + x; |
137 """}; | 103 """}; |
OLD | NEW |