OLD | NEW |
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, 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 library compiler_helper; | 5 library compiler_helper; |
6 | 6 |
7 import 'dart:async'; | 7 import 'dart:async'; |
8 import "package:expect/expect.dart"; | 8 import "package:expect/expect.dart"; |
9 | 9 |
10 import 'package:compiler/src/elements/elements.dart' | 10 import 'package:compiler/src/elements/elements.dart' |
(...skipping 25 matching lines...) Expand all Loading... |
36 export 'package:compiler/src/tree/tree.dart'; | 36 export 'package:compiler/src/tree/tree.dart'; |
37 | 37 |
38 import 'mock_compiler.dart'; | 38 import 'mock_compiler.dart'; |
39 export 'mock_compiler.dart'; | 39 export 'mock_compiler.dart'; |
40 | 40 |
41 import 'memory_compiler.dart' hide compilerFor; | 41 import 'memory_compiler.dart' hide compilerFor; |
42 | 42 |
43 import 'output_collector.dart'; | 43 import 'output_collector.dart'; |
44 export 'output_collector.dart'; | 44 export 'output_collector.dart'; |
45 | 45 |
46 /// Compile [code] and returns the code for [entry]. | 46 /// Compile [code] and returns either the code for [entry] or, if [returnAll] is |
| 47 /// true, the code for the entire program. |
47 /// | 48 /// |
48 /// If [check] is provided, it is executed on the code for [entry] before | 49 /// If [check] is provided, it is executed on the code for [entry] before |
49 /// returning. If [useMock] is `true` the [MockCompiler] is used for | 50 /// returning. If [useMock] is `true` the [MockCompiler] is used for |
50 /// compilation, otherwise the memory compiler is used. | 51 /// compilation, otherwise the memory compiler is used. |
51 Future<String> compile(String code, | 52 Future<String> compile(String code, |
52 {String entry: 'main', | 53 {String entry: 'main', |
53 bool enableTypeAssertions: false, | 54 bool enableTypeAssertions: false, |
54 bool minify: false, | 55 bool minify: false, |
55 bool analyzeAll: false, | 56 bool analyzeAll: false, |
56 bool disableInlining: true, | 57 bool disableInlining: true, |
| 58 bool trustJSInteropTypeAnnotations: false, |
57 bool useMock: false, | 59 bool useMock: false, |
58 void check(String generated)}) async { | 60 void check(String generatedEntry), |
| 61 bool returnAll: false}) async { |
| 62 OutputCollector outputCollector = returnAll ? new OutputCollector() : null; |
59 if (useMock) { | 63 if (useMock) { |
60 // TODO(johnniwinther): Remove this when no longer needed by | 64 // TODO(johnniwinther): Remove this when no longer needed by |
61 // `arithmetic_simplication_test.dart`. | 65 // `arithmetic_simplication_test.dart`. |
62 MockCompiler compiler = new MockCompiler.internal( | 66 MockCompiler compiler = new MockCompiler.internal( |
63 enableTypeAssertions: enableTypeAssertions, | 67 enableTypeAssertions: enableTypeAssertions, |
64 // Type inference does not run when manually | 68 // Type inference does not run when manually |
65 // compiling a method. | 69 // compiling a method. |
66 disableTypeInference: true, | 70 disableTypeInference: true, |
67 enableMinification: minify, | 71 enableMinification: minify, |
68 disableInlining: disableInlining); | 72 disableInlining: disableInlining, |
| 73 trustJSInteropTypeAnnotations: trustJSInteropTypeAnnotations, |
| 74 outputProvider: outputCollector); |
69 await compiler.init(); | 75 await compiler.init(); |
70 compiler.parseScript(code); | 76 compiler.parseScript(code); |
71 lego.Element element = compiler.mainApp.find(entry); | 77 lego.Element element = compiler.mainApp.find(entry); |
72 if (element == null) return null; | 78 if (element == null) return null; |
73 compiler.phase = Compiler.PHASE_RESOLVING; | 79 compiler.phase = Compiler.PHASE_RESOLVING; |
74 compiler.backend.enqueueHelpers(compiler.enqueuer.resolution, | 80 compiler.backend.enqueueHelpers(compiler.enqueuer.resolution, |
75 compiler.globalDependencies); | 81 compiler.globalDependencies); |
76 compiler.processQueue(compiler.enqueuer.resolution, element); | 82 compiler.processQueue(compiler.enqueuer.resolution, element); |
77 compiler.world.populate(); | 83 compiler.world.populate(); |
78 compiler.backend.onResolutionComplete(); | 84 compiler.backend.onResolutionComplete(); |
79 var context = new js.JavaScriptItemCompilationContext(); | 85 var context = new js.JavaScriptItemCompilationContext(); |
80 ResolutionWorkItem resolutionWork = | 86 ResolutionWorkItem resolutionWork = |
81 new ResolutionWorkItem(element, context); | 87 new ResolutionWorkItem(element, context); |
82 resolutionWork.run(compiler, compiler.enqueuer.resolution); | 88 resolutionWork.run(compiler, compiler.enqueuer.resolution); |
83 CodegenWorkItem work = | 89 CodegenWorkItem work = |
84 new CodegenWorkItem(compiler, element, context); | 90 new CodegenWorkItem(compiler, element, context); |
85 compiler.phase = Compiler.PHASE_COMPILING; | 91 compiler.phase = Compiler.PHASE_COMPILING; |
86 work.run(compiler, compiler.enqueuer.codegen); | 92 work.run(compiler, compiler.enqueuer.codegen); |
87 js.JavaScriptBackend backend = compiler.backend; | 93 js.JavaScriptBackend backend = compiler.backend; |
88 String generated = backend.getGeneratedCode(element); | 94 String generated = backend.getGeneratedCode(element); |
89 if (check != null) { | 95 if (check != null) { |
90 check(generated); | 96 check(generated); |
91 } | 97 } |
92 return generated; | 98 return returnAll ? outputCollector.getOutput('', 'js') : generated; |
93 } else { | 99 } else { |
94 List<String> options = <String>[ | 100 List<String> options = <String>[ |
95 Flags.disableTypeInference]; | 101 Flags.disableTypeInference]; |
96 if (enableTypeAssertions) { | 102 if (enableTypeAssertions) { |
97 options.add(Flags.enableCheckedMode); | 103 options.add(Flags.enableCheckedMode); |
98 } | 104 } |
99 if (minify) { | 105 if (minify) { |
100 options.add(Flags.minify); | 106 options.add(Flags.minify); |
101 } | 107 } |
102 if (analyzeAll) { | 108 if (analyzeAll) { |
103 options.add(Flags.analyzeAll); | 109 options.add(Flags.analyzeAll); |
104 } | 110 } |
| 111 if (trustJSInteropTypeAnnotations) { |
| 112 options.add(Flags.trustJSInteropTypeAnnotations); |
| 113 } |
105 | 114 |
106 Map<String, String> source; | 115 Map<String, String> source; |
107 if (entry != 'main') { | 116 if (entry != 'main') { |
108 source = {'main.dart': "$code\n\nmain() => $entry;" }; | 117 source = {'main.dart': "$code\n\nmain() => $entry;" }; |
109 } else { | 118 } else { |
110 source = {'main.dart': code}; | 119 source = {'main.dart': code}; |
111 } | 120 } |
112 | 121 |
113 CompilationResult result = await runCompiler( | 122 CompilationResult result = await runCompiler( |
114 memorySourceFiles: source, | 123 memorySourceFiles: source, |
115 options: options, | 124 options: options, |
| 125 outputProvider: outputCollector, |
116 beforeRun: (compiler) { | 126 beforeRun: (compiler) { |
117 if (disableInlining) { | 127 if (disableInlining) { |
118 compiler.disableInlining = true; | 128 compiler.disableInlining = true; |
119 } | 129 } |
120 }); | 130 }); |
121 Expect.isTrue(result.isSuccess); | 131 Expect.isTrue(result.isSuccess); |
122 Compiler compiler = result.compiler; | 132 Compiler compiler = result.compiler; |
123 lego.Element element = compiler.mainApp.find(entry); | 133 lego.Element element = compiler.mainApp.find(entry); |
124 js.JavaScriptBackend backend = compiler.backend; | 134 js.JavaScriptBackend backend = compiler.backend; |
125 String generated = backend.getGeneratedCode(element); | 135 String generated = backend.getGeneratedCode(element); |
126 if (check != null) { | 136 if (check != null) { |
127 check(generated); | 137 check(generated); |
128 } | 138 } |
129 return generated; | 139 return returnAll ? outputCollector.getOutput('', 'js') : generated; |
130 } | 140 } |
131 } | 141 } |
132 | 142 |
133 Future<String> compileAll(String code, | 143 Future<String> compileAll(String code, |
134 {Map<String, String> coreSource, | 144 {Map<String, String> coreSource, |
135 bool disableInlining: true, | 145 bool disableInlining: true, |
136 bool trustTypeAnnotations: false, | 146 bool trustTypeAnnotations: false, |
137 bool minify: false, | 147 bool minify: false, |
138 int expectedErrors, | 148 int expectedErrors, |
139 int expectedWarnings}) { | 149 int expectedWarnings}) { |
(...skipping 140 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
280 regexp = regexp.replaceAll(xRe, '(?:$anyIdentifier)'); | 290 regexp = regexp.replaceAll(xRe, '(?:$anyIdentifier)'); |
281 final spaceRe = new RegExp('\\s+'); | 291 final spaceRe = new RegExp('\\s+'); |
282 regexp = regexp.replaceAll(spaceRe, '(?:\\s*)'); | 292 regexp = regexp.replaceAll(spaceRe, '(?:\\s*)'); |
283 if (shouldMatch) { | 293 if (shouldMatch) { |
284 Expect.isTrue(new RegExp(regexp).hasMatch(generated)); | 294 Expect.isTrue(new RegExp(regexp).hasMatch(generated)); |
285 } else { | 295 } else { |
286 Expect.isFalse(new RegExp(regexp).hasMatch(generated)); | 296 Expect.isFalse(new RegExp(regexp).hasMatch(generated)); |
287 } | 297 } |
288 }); | 298 }); |
289 } | 299 } |
OLD | NEW |