Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(202)

Side by Side Diff: tests/compiler/dart2js/deferred_dont_inline_deferred_constants_test.dart

Issue 256453004: Avoid inlining constants that are used via a deferred import. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Use intermediate constants. Created 6 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
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
3 // BSD-style license that can be found in the LICENSE file.
4
5 // Test that the additional runtime type support is output to the right
6 // Files when using deferred loading.
7
8 import 'package:expect/expect.dart';
9 import "package:async_helper/async_helper.dart";
10 import 'memory_source_file_helper.dart';
11 import "dart:async";
12
13 import '../../../sdk/lib/_internal/compiler/implementation/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
27 void main() {
28 Uri script = currentDirectory.resolveUri(Platform.script);
29 Uri libraryRoot = script.resolve('../../../sdk/');
30 Uri packageRoot = script.resolve('./packages/');
31
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((_) {
54 String mainOutput = outputs['main.js'].mem.toString();
55 String lib1Output = outputs['out_lib1.part.js'].mem.toString();
56 String lib2Output = outputs['out_lib2.part.js'].mem.toString();
57 String lib12Output = outputs['out_lib1_lib2.part.js'].mem.toString();
58 // Test that the deferred constants are not inlined into the main file.
59 RegExp re1 = new RegExp(r"= .string1");
60 RegExp re2 = new RegExp(r"= .string2");
61 RegExp re3 = new RegExp(r"= 1010");
62 Expect.isTrue(re1.hasMatch(lib1Output));
63 Expect.isTrue(re2.hasMatch(lib1Output));
64 Expect.isTrue(re3.hasMatch(lib1Output));
65 Expect.isFalse(re1.hasMatch(mainOutput));
66 Expect.isFalse(re2.hasMatch(mainOutput));
67 Expect.isFalse(re3.hasMatch(mainOutput));
68 // Test that the non-deferred constant is inlined.
69 Expect.isTrue(new RegExp(r"print\(.string3.\)").hasMatch(mainOutput));
70 Expect.isFalse(new RegExp(r"= .string3").hasMatch(mainOutput));
71 Expect.isTrue(new RegExp(r"print\(.string4.\)").hasMatch(mainOutput));
72
73 // C(1) is shared between main, lib1 and lib2. Test that lib1 and lib2 each
74 // has a reference to it. It is defined in the main output file.
75 Expect.isTrue(new RegExp(r"C.C_1 =").hasMatch(mainOutput));
76 Expect.isFalse(new RegExp(r"= C.C_1").hasMatch(mainOutput));
77
78 Expect.isTrue(new RegExp(r"= C.C_1").hasMatch(lib1Output));
79 Expect.isTrue(new RegExp(r"= C.C_1").hasMatch(lib2Output));
80
81 // C(2) is shared between lib1 and lib2, each of them has their own
82 // reference to it.
83 Expect.isFalse(new RegExp(r"= C.C_2").hasMatch(mainOutput));
84
85 Expect.isTrue(new RegExp(r"= C.C_2").hasMatch(lib1Output));
86 Expect.isTrue(new RegExp(r"= C.C_2").hasMatch(lib2Output));
87 Expect.isTrue(new RegExp(r"C.C_2 =").hasMatch(lib12Output));
88
89 // "string4" is shared between lib1 and lib2, but it can be inlined.
90 Expect.isTrue(new RegExp(r"= .string4").hasMatch(lib1Output));
91 Expect.isTrue(new RegExp(r"= .string4").hasMatch(lib2Output));
92 Expect.isFalse(new RegExp(r"= .string4").hasMatch(lib12Output));
93 }));
94 }
95
96 // Make sure that deferred constants are not inlined into the main hunk.
97 const Map MEMORY_SOURCE_FILES = const {"main.dart": """
98 import "dart:async";
99
100 import 'lib1.dart' deferred as lib1;
101 import 'lib2.dart' deferred as lib2;
102
103 const c = "string3";
104
105 class C {
106 final p;
107 const C(this.p);
108 }
109
110 void main() {
111 lib1.loadLibrary().then((_) {
112 lib2.loadLibrary().then((_) {
113 print(lib1.C1);
114 print(lib1.C2);
115 print(lib1.C.C3);
116 print(c);
117 print(lib1.C4);
118 print(lib2.C4);
119 print(lib1.C5);
120 print(lib2.C5);
121 print(lib1.C6);
122 print(lib2.C6);
123 print("string4");
124 print(const C(1));
125 });
126 });
127 }
128 """, "lib1.dart": """
129 import "main.dart" as main;
130 const C1 = "string1";
131 const C2 = 1010;
132 class C {
133 static const C3 = "string2";
134 }
135 const C4 = "string4";
136 const C5 = const main.C(1);
137 const C6 = const main.C(2);
138 """, "lib2.dart": """
139 import "main.dart" as main;
140 const C4 = "string4";
141 const C5 = const main.C(1);
142 const C6 = const main.C(2);
143 """};
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698