|
6 | 6 | // Files when using deferred loading.
|
7 | 7 |
|
8 | 8 | import 'package:async_helper/async_helper.dart';
|
9 |
| -import 'package:compiler/compiler_new.dart'; |
10 |
| -import 'package:compiler/src/commandline_options.dart'; |
11 |
| -import 'package:compiler/src/compiler.dart'; |
12 |
| -import 'package:compiler/src/constants/values.dart'; |
13 |
| -import 'package:compiler/src/deferred_load.dart'; |
14 |
| -import 'package:compiler/src/elements/entities.dart'; |
15 |
| -import 'package:compiler/src/js_emitter/model.dart'; |
16 |
| -import 'package:expect/expect.dart'; |
17 |
| -import '../helpers/memory_compiler.dart'; |
18 |
| -import '../helpers/output_collector.dart'; |
19 |
| -import '../helpers/program_lookup.dart'; |
| 9 | +import 'constant_emission_test_helper.dart'; |
20 | 10 |
|
21 | 11 | void main() {
|
22 | 12 | runTest({bool useCFEConstants: false}) async {
|
23 |
| - OutputCollector collector = new OutputCollector(); |
24 |
| - CompilationResult result = await runCompiler( |
25 |
| - memorySourceFiles: MEMORY_SOURCE_FILES, |
26 |
| - outputProvider: collector, |
27 |
| - options: useCFEConstants |
28 |
| - ? ['${Flags.enableLanguageExperiments}=constant-update-2018'] |
29 |
| - : ['${Flags.enableLanguageExperiments}=no-constant-update-2018']); |
30 |
| - Compiler compiler = result.compiler; |
31 |
| - ProgramLookup lookup = new ProgramLookup(compiler); |
32 |
| - var closedWorld = compiler.backendClosedWorldForTesting; |
33 |
| - var elementEnvironment = closedWorld.elementEnvironment; |
34 |
| - |
35 |
| - LibraryEntity lookupLibrary(name) { |
36 |
| - return elementEnvironment.lookupLibrary(Uri.parse(name)); |
37 |
| - } |
38 |
| - |
39 |
| - OutputUnit Function(MemberEntity) outputUnitForMember = |
40 |
| - closedWorld.outputUnitData.outputUnitForMember; |
41 |
| - |
42 |
| - LibraryEntity lib1 = lookupLibrary("memory:lib1.dart"); |
43 |
| - MemberEntity foo1 = elementEnvironment.lookupLibraryMember(lib1, "foo"); |
44 |
| - OutputUnit ou_lib1 = outputUnitForMember(foo1); |
45 |
| - |
46 |
| - LibraryEntity lib2 = lookupLibrary("memory:lib2.dart"); |
47 |
| - MemberEntity foo2 = elementEnvironment.lookupLibraryMember(lib2, "foo"); |
48 |
| - OutputUnit ou_lib2 = outputUnitForMember(foo2); |
49 |
| - |
50 |
| - LibraryEntity mainApp = elementEnvironment.mainLibrary; |
51 |
| - MemberEntity fooMain = |
52 |
| - elementEnvironment.lookupLibraryMember(mainApp, "foo"); |
53 |
| - OutputUnit ou_lib1_lib2 = outputUnitForMember(fooMain); |
54 |
| - |
55 | 13 | Map<String, Set<String>> expectedOutputUnits = {
|
56 | 14 | // Test that the deferred constants are not inlined into the main file.
|
57 |
| - 'IntConstant(1010)': {'lib1'}, |
58 |
| - 'StringConstant("string1")': {'lib1'}, |
59 |
| - 'StringConstant("string2")': {'lib1'}, |
| 15 | + 'DeferredGlobalConstant(IntConstant(1010))': {'lib1'}, |
| 16 | + 'DeferredGlobalConstant(StringConstant("string1"))': {'lib1'}, |
| 17 | + 'DeferredGlobalConstant(StringConstant("string2"))': {'lib1'}, |
60 | 18 | // "string4" is shared between lib1 and lib2, but it can be inlined.
|
61 |
| - 'StringConstant("string4")': |
| 19 | + 'DeferredGlobalConstant(StringConstant("string4"))': |
62 | 20 | // TODO(johnniwinther): Should we inline CFE constants within deferred
|
63 | 21 | // library boundaries?
|
64 | 22 | useCFEConstants ? {'lib12'} : {'lib1', 'lib2'},
|
65 | 23 | // C(1) is shared between main, lib1 and lib2. Test that lib1 and lib2
|
66 | 24 | // each has a reference to it. It is defined in the main output file.
|
67 |
| - 'ConstructedConstant(C(p=IntConstant(1)))': |
68 |
| - // With CFE constants, the references are inlined, so the constant only |
69 |
| - // occurs in main. |
70 |
| - useCFEConstants ? {'main'} : {'main', 'lib1', 'lib2'}, |
| 25 | + 'ConstructedConstant(C(p=IntConstant(1)))': {'main'}, |
| 26 | + 'DeferredGlobalConstant(ConstructedConstant(C(p=IntConstant(1))))': |
| 27 | + // With CFE constants, the references are inlined, so the constant |
| 28 | + // only occurs in main. |
| 29 | + useCFEConstants ? {} : {'lib1', 'lib2'}, |
71 | 30 | // C(2) is shared between lib1 and lib2, each of them has their own
|
72 | 31 | // reference to it.
|
73 |
| - 'ConstructedConstant(C(p=IntConstant(2)))': |
| 32 | + 'ConstructedConstant(C(p=IntConstant(2)))': {'lib12'}, |
| 33 | + 'DeferredGlobalConstant(ConstructedConstant(C(p=IntConstant(2))))': |
74 | 34 | // With CFE constants, the references are inlined, so the constant
|
75 | 35 | // occurs in lib12.
|
76 |
| - useCFEConstants ? {'lib12'} : {'lib1', 'lib2', 'lib12'}, |
| 36 | + useCFEConstants ? {'lib12'} : {'lib1', 'lib2'}, |
77 | 37 | // Test that the non-deferred constant is inlined.
|
78 | 38 | 'ConstructedConstant(C(p=IntConstant(5)))': {'main'},
|
79 | 39 | };
|
80 |
| - |
81 |
| - Map<String, Set<String>> actualOutputUnits = {}; |
82 |
| - |
83 |
| - void processFragment(Fragment fragment, String fragmentName) { |
84 |
| - for (Constant constant in fragment.constants) { |
85 |
| - String text; |
86 |
| - if (constant.value is DeferredGlobalConstantValue) { |
87 |
| - DeferredGlobalConstantValue deferred = constant.value; |
88 |
| - text = deferred.referenced.toStructuredText(); |
89 |
| - } else { |
90 |
| - text = constant.value.toStructuredText(); |
91 |
| - } |
92 |
| - Set<String> expectedConstantUnit = expectedOutputUnits[text]; |
93 |
| - if (expectedConstantUnit == null) { |
94 |
| - if (constant.value is DeferredGlobalConstantValue) { |
95 |
| - print('No expectancy for $constant found in $fragmentName'); |
96 |
| - } |
97 |
| - } else { |
98 |
| - (actualOutputUnits[text] ??= <String>{}).add(fragmentName); |
99 |
| - } |
100 |
| - } |
101 |
| - } |
102 |
| - |
103 |
| - processFragment(lookup.program.mainFragment, 'main'); |
104 |
| - processFragment(lookup.getFragment(ou_lib1), 'lib1'); |
105 |
| - processFragment(lookup.getFragment(ou_lib2), 'lib2'); |
106 |
| - processFragment(lookup.getFragment(ou_lib1_lib2), 'lib12'); |
107 |
| - |
108 |
| - expectedOutputUnits.forEach((String constant, Set<String> expectedSet) { |
109 |
| - Set<String> actualSet = actualOutputUnits[constant] ?? const <String>{}; |
110 |
| - Expect.setEquals( |
111 |
| - expectedSet, |
112 |
| - actualSet, |
113 |
| - "Constant $constant found in $actualSet, expected " |
114 |
| - "$expectedSet"); |
115 |
| - }); |
| 40 | + await run( |
| 41 | + MEMORY_SOURCE_FILES, |
| 42 | + const [ |
| 43 | + OutputUnitDescriptor('memory:lib1.dart', 'foo', 'lib1'), |
| 44 | + OutputUnitDescriptor('memory:lib2.dart', 'foo', 'lib2'), |
| 45 | + OutputUnitDescriptor('memory:main.dart', 'foo', 'lib12') |
| 46 | + ], |
| 47 | + expectedOutputUnits, |
| 48 | + useCFEConstants: useCFEConstants); |
116 | 49 | }
|
117 | 50 |
|
118 | 51 | asyncTest(() async {
|
|
0 commit comments