| 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, |
| 57 bool useMock: false, | 58 bool useMock: false, |
| 58 void check(String generated)}) async { | 59 void check(String generatedEntry), |
| 60 bool returnAll: false}) async { |
| 61 OuputCollector outputCollector = returnAll ? new OutputCollector() : null; |
| 59 if (useMock) { | 62 if (useMock) { |
| 60 // TODO(johnniwinther): Remove this when no longer needed by | 63 // TODO(johnniwinther): Remove this when no longer needed by |
| 61 // `arithmetic_simplication_test.dart`. | 64 // `arithmetic_simplication_test.dart`. |
| 62 MockCompiler compiler = new MockCompiler.internal( | 65 MockCompiler compiler = new MockCompiler.internal( |
| 63 enableTypeAssertions: enableTypeAssertions, | 66 enableTypeAssertions: enableTypeAssertions, |
| 64 // Type inference does not run when manually | 67 // Type inference does not run when manually |
| 65 // compiling a method. | 68 // compiling a method. |
| 66 disableTypeInference: true, | 69 disableTypeInference: true, |
| 67 enableMinification: minify, | 70 enableMinification: minify, |
| 68 disableInlining: disableInlining); | 71 disableInlining: disableInlining, |
| 72 outputProvider: outputCollector); |
| 69 await compiler.init(); | 73 await compiler.init(); |
| 70 compiler.parseScript(code); | 74 compiler.parseScript(code); |
| 71 lego.Element element = compiler.mainApp.find(entry); | 75 lego.Element element = compiler.mainApp.find(entry); |
| 72 if (element == null) return null; | 76 if (element == null) return null; |
| 73 compiler.phase = Compiler.PHASE_RESOLVING; | 77 compiler.phase = Compiler.PHASE_RESOLVING; |
| 74 compiler.backend.enqueueHelpers(compiler.enqueuer.resolution, | 78 compiler.backend.enqueueHelpers(compiler.enqueuer.resolution, |
| 75 compiler.globalDependencies); | 79 compiler.globalDependencies); |
| 76 compiler.processQueue(compiler.enqueuer.resolution, element); | 80 compiler.processQueue(compiler.enqueuer.resolution, element); |
| 77 compiler.world.populate(); | 81 compiler.world.populate(); |
| 78 compiler.backend.onResolutionComplete(); | 82 compiler.backend.onResolutionComplete(); |
| 79 var context = new js.JavaScriptItemCompilationContext(); | 83 var context = new js.JavaScriptItemCompilationContext(); |
| 80 ResolutionWorkItem resolutionWork = | 84 ResolutionWorkItem resolutionWork = |
| 81 new ResolutionWorkItem(element, context); | 85 new ResolutionWorkItem(element, context); |
| 82 resolutionWork.run(compiler, compiler.enqueuer.resolution); | 86 resolutionWork.run(compiler, compiler.enqueuer.resolution); |
| 83 CodegenWorkItem work = | 87 CodegenWorkItem work = |
| 84 new CodegenWorkItem(compiler, element, context); | 88 new CodegenWorkItem(compiler, element, context); |
| 85 compiler.phase = Compiler.PHASE_COMPILING; | 89 compiler.phase = Compiler.PHASE_COMPILING; |
| 86 work.run(compiler, compiler.enqueuer.codegen); | 90 work.run(compiler, compiler.enqueuer.codegen); |
| 87 js.JavaScriptBackend backend = compiler.backend; | 91 js.JavaScriptBackend backend = compiler.backend; |
| 88 String generated = backend.getGeneratedCode(element); | 92 String generated = backend.getGeneratedCode(element); |
| 89 if (check != null) { | 93 if (check != null) { |
| 90 check(generated); | 94 check(generated); |
| 91 } | 95 } |
| 92 return generated; | 96 return returnAll ? outputCollector.getOutput('', 'js') : generated; |
| 93 } else { | 97 } else { |
| 94 List<String> options = <String>[ | 98 List<String> options = <String>[ |
| 95 Flags.disableTypeInference]; | 99 Flags.disableTypeInference]; |
| 96 if (enableTypeAssertions) { | 100 if (enableTypeAssertions) { |
| 97 options.add(Flags.enableCheckedMode); | 101 options.add(Flags.enableCheckedMode); |
| 98 } | 102 } |
| 99 if (minify) { | 103 if (minify) { |
| 100 options.add(Flags.minify); | 104 options.add(Flags.minify); |
| 101 } | 105 } |
| 102 if (analyzeAll) { | 106 if (analyzeAll) { |
| 103 options.add(Flags.analyzeAll); | 107 options.add(Flags.analyzeAll); |
| 104 } | 108 } |
| 105 | 109 |
| 106 Map<String, String> source; | 110 Map<String, String> source; |
| 107 if (entry != 'main') { | 111 if (entry != 'main') { |
| 108 source = {'main.dart': "$code\n\nmain() => $entry;" }; | 112 source = {'main.dart': "$code\n\nmain() => $entry;" }; |
| 109 } else { | 113 } else { |
| 110 source = {'main.dart': code}; | 114 source = {'main.dart': code}; |
| 111 } | 115 } |
| 112 | 116 |
| 113 CompilationResult result = await runCompiler( | 117 CompilationResult result = await runCompiler( |
| 114 memorySourceFiles: source, | 118 memorySourceFiles: source, |
| 115 options: options, | 119 options: options, |
| 120 outputProvider: outputCollector, |
| 116 beforeRun: (compiler) { | 121 beforeRun: (compiler) { |
| 117 if (disableInlining) { | 122 if (disableInlining) { |
| 118 compiler.disableInlining = true; | 123 compiler.disableInlining = true; |
| 119 } | 124 } |
| 120 }); | 125 }); |
| 121 Expect.isTrue(result.isSuccess); | 126 Expect.isTrue(result.isSuccess); |
| 122 Compiler compiler = result.compiler; | 127 Compiler compiler = result.compiler; |
| 123 lego.Element element = compiler.mainApp.find(entry); | 128 lego.Element element = compiler.mainApp.find(entry); |
| 124 js.JavaScriptBackend backend = compiler.backend; | 129 js.JavaScriptBackend backend = compiler.backend; |
| 125 String generated = backend.getGeneratedCode(element); | 130 String generated = backend.getGeneratedCode(element); |
| 126 if (check != null) { | 131 if (check != null) { |
| 127 check(generated); | 132 check(generated); |
| 128 } | 133 } |
| 129 return generated; | 134 return returnAll ? outputCollector.getOutput('', 'js') : generated; |
| 130 } | 135 } |
| 131 } | 136 } |
| 132 | 137 |
| 133 Future<String> compileAll(String code, | 138 Future<String> compileAll(String code, |
| 134 {Map<String, String> coreSource, | 139 {Map<String, String> coreSource, |
| 135 bool disableInlining: true, | 140 bool disableInlining: true, |
| 136 bool trustTypeAnnotations: false, | 141 bool trustTypeAnnotations: false, |
| 137 bool minify: false, | 142 bool minify: false, |
| 138 int expectedErrors, | 143 int expectedErrors, |
| 139 int expectedWarnings}) { | 144 int expectedWarnings}) { |
| (...skipping 141 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 281 regexp = regexp.replaceAll(xRe, '(?:$anyIdentifier)'); | 286 regexp = regexp.replaceAll(xRe, '(?:$anyIdentifier)'); |
| 282 final spaceRe = new RegExp('\\s+'); | 287 final spaceRe = new RegExp('\\s+'); |
| 283 regexp = regexp.replaceAll(spaceRe, '(?:\\s*)'); | 288 regexp = regexp.replaceAll(spaceRe, '(?:\\s*)'); |
| 284 if (shouldMatch) { | 289 if (shouldMatch) { |
| 285 Expect.isTrue(new RegExp(regexp).hasMatch(generated)); | 290 Expect.isTrue(new RegExp(regexp).hasMatch(generated)); |
| 286 } else { | 291 } else { |
| 287 Expect.isFalse(new RegExp(regexp).hasMatch(generated)); | 292 Expect.isFalse(new RegExp(regexp).hasMatch(generated)); |
| 288 } | 293 } |
| 289 }); | 294 }); |
| 290 } | 295 } |
| OLD | NEW |