Chromium Code Reviews| 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. |
|
floitsch
2015/05/05 17:58:15
Thanks. I was going to ask for a test like this :)
herhut
2015/05/06 08:37:09
Acknowledged.
| |
| 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 | 67 |
| 68 String mainOutput = outputs["main.js"].mem.toString(); | 68 String mainOutput = outputs["main.js"].mem.toString(); |
| 69 String lib1Output = outputs["out_${ou_lib1.name}.part.js"].mem.toString(); | 69 String lib1Output = outputs["out_${ou_lib1.name}.part.js"].mem.toString(); |
| 70 // Test that the deferred globals are not inlined into the main file. | 70 String lib3Output = outputs["out_${ou_lib3.name}.part.js"].mem.toString(); |
| 71 RegExp re1 = new RegExp(r"= .string1"); | 71 |
| 72 RegExp re2 = new RegExp(r"= .string2"); | 72 RegExp re1 = new RegExp(r"inlineMeAway"); |
|
floitsch
2015/05/05 17:58:15
I would go for the strings instead. The method nam
herhut
2015/05/06 08:37:09
Done.
| |
| 73 Expect.isTrue(re1.hasMatch(lib1Output)); | 73 RegExp re2 = new RegExp(r"inlineFromMain"); |
| 74 Expect.isTrue(re2.hasMatch(lib1Output)); | 74 RegExp re3 = new RegExp(r"inlineFromLib1"); |
| 75 | |
| 76 Expect.isFalse(re1.hasMatch(lib1Output)); | |
| 77 Expect.isFalse(re2.hasMatch(lib1Output)); | |
| 78 Expect.isTrue(re3.hasMatch(lib1Output)); | |
| 79 | |
| 75 Expect.isFalse(re1.hasMatch(mainOutput)); | 80 Expect.isFalse(re1.hasMatch(mainOutput)); |
| 76 Expect.isFalse(re2.hasMatch(mainOutput)); | 81 Expect.isFalse(re2.hasMatch(mainOutput)); |
| 82 Expect.isTrue(re3.hasMatch(mainOutput)); | |
| 83 | |
| 84 Expect.isTrue(re3.hasMatch(lib3Output)); | |
| 77 })); | 85 })); |
| 78 } | 86 } |
| 79 | 87 |
| 80 // Make sure that deferred constants are not inlined into the main hunk. | 88 // Make sure that empty functions are inlined and that functions from |
| 89 // main also are inlined (assuming normal heuristics). | |
| 81 const Map MEMORY_SOURCE_FILES = const {"main.dart": """ | 90 const Map MEMORY_SOURCE_FILES = const {"main.dart": """ |
| 82 import "dart:async"; | 91 import "dart:async"; |
| 83 | 92 |
| 84 import 'lib1.dart' deferred as lib1; | 93 import 'lib1.dart' deferred as lib1; |
| 94 import 'lib2.dart' deferred as lib2; | |
| 95 | |
| 96 inlineFromMain(x) => x; | |
| 85 | 97 |
| 86 void main() { | 98 void main() { |
| 87 lib1.loadLibrary().then((_) { | 99 lib1.loadLibrary().then((_) { |
| 88 print(lib1.finalVar); | 100 lib1.test(); |
| 89 print(lib1.globalVar); | 101 print(lib1.inlineMeAway("inlined as empty")); |
| 90 lib1.globalVar = "foobar"; | 102 print(lib1.inlineFromLib1("should stay")); |
| 91 print(lib1.globalVar); | |
| 92 }); | 103 }); |
| 93 } | 104 } |
| 94 """, "lib1.dart": """ | 105 """, "lib1.dart": """ |
| 95 import "main.dart" as main; | 106 import "main.dart" as main; |
| 96 final finalVar = "string1"; | 107 import "lib3.dart" as lib3; |
| 97 var globalVar = "string2"; | 108 |
| 109 inlineMeAway(x) {} | |
| 110 | |
| 111 inlineFromLib1(x) => x; | |
| 112 | |
| 113 test() { | |
| 114 print(main.inlineFromMain("should be inlined")); | |
| 115 print(lib3.sameContextInline("should be inlined")); | |
| 116 } | |
| 117 """, "lib2.dart": """ | |
| 118 import "lib3.dart" as lib3; | |
| 119 | |
| 120 test() { | |
| 121 print(lib3.sameContextInline("should be inlined")); | |
| 122 } | |
| 123 """, "lib3.dart": """ | |
| 124 sameContextInline(x) => x; | |
| 98 """}; | 125 """}; |
| OLD | NEW |