| 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/compiler.dart' as api; | 10 import 'package:compiler/compiler.dart' as api; |
| 11 | 11 |
| 12 import 'package:compiler/src/elements/elements.dart' | 12 import 'package:compiler/src/elements/elements.dart' |
| 13 as lego; | 13 as lego; |
| 14 export 'package:compiler/src/elements/elements.dart'; | 14 export 'package:compiler/src/elements/elements.dart'; |
| 15 | 15 |
| 16 import 'package:compiler/src/js_backend/js_backend.dart' | 16 import 'package:compiler/src/js_backend/js_backend.dart' |
| 17 as js; | 17 as js; |
| 18 | 18 |
| 19 import 'package:compiler/src/commandline_options.dart'; |
| 19 import 'package:compiler/src/common/codegen.dart'; | 20 import 'package:compiler/src/common/codegen.dart'; |
| 20 import 'package:compiler/src/common/resolution.dart'; | 21 import 'package:compiler/src/common/resolution.dart'; |
| 21 | 22 |
| 22 export 'package:compiler/src/diagnostics/messages.dart'; | 23 export 'package:compiler/src/diagnostics/messages.dart'; |
| 23 export 'package:compiler/src/diagnostics/source_span.dart'; | 24 export 'package:compiler/src/diagnostics/source_span.dart'; |
| 24 export 'package:compiler/src/diagnostics/spannable.dart'; | 25 export 'package:compiler/src/diagnostics/spannable.dart'; |
| 25 | 26 |
| 26 import 'package:compiler/src/types/types.dart' | 27 import 'package:compiler/src/types/types.dart' |
| 27 as types; | 28 as types; |
| 28 export 'package:compiler/src/types/types.dart' | 29 export 'package:compiler/src/types/types.dart' |
| 29 show TypeMask; | 30 show TypeMask; |
| 30 | 31 |
| 31 import 'package:compiler/src/util/util.dart'; | 32 import 'package:compiler/src/util/util.dart'; |
| 32 export 'package:compiler/src/util/util.dart'; | 33 export 'package:compiler/src/util/util.dart'; |
| 33 | 34 |
| 34 import 'package:compiler/src/compiler.dart' | 35 import 'package:compiler/src/compiler.dart' |
| 35 show Compiler; | 36 show Compiler; |
| 36 | 37 |
| 37 export 'package:compiler/src/tree/tree.dart'; | 38 export 'package:compiler/src/tree/tree.dart'; |
| 38 | 39 |
| 39 import 'mock_compiler.dart'; | 40 import 'mock_compiler.dart'; |
| 40 export 'mock_compiler.dart'; | 41 export 'mock_compiler.dart'; |
| 41 | 42 |
| 43 import 'memory_compiler.dart'; |
| 44 |
| 42 import 'output_collector.dart'; | 45 import 'output_collector.dart'; |
| 43 export 'output_collector.dart'; | 46 export 'output_collector.dart'; |
| 44 | 47 |
| 48 /// Compile [code] and returns the code for [entry]. |
| 49 /// |
| 50 /// If [check] is provided, it is executed on the code for [entry] before |
| 51 /// returning. If [useMock] is `true` the [MockCompiler] is used for |
| 52 /// compilation, otherwise the memory compiler is used. |
| 45 Future<String> compile(String code, | 53 Future<String> compile(String code, |
| 46 {String entry: 'main', | 54 {String entry: 'main', |
| 47 bool enableTypeAssertions: false, | 55 bool enableTypeAssertions: false, |
| 48 bool minify: false, | 56 bool minify: false, |
| 49 bool analyzeAll: false, | 57 bool analyzeAll: false, |
| 50 bool disableInlining: true, | 58 bool disableInlining: true, |
| 51 void check(String generated)}) { | 59 bool useMock: false, |
| 52 MockCompiler compiler = new MockCompiler.internal( | 60 void check(String generated)}) async { |
| 53 enableTypeAssertions: enableTypeAssertions, | 61 if (useMock) { |
| 54 // Type inference does not run when manually | 62 // TODO(johnniwinther): Remove this when no longer needed by |
| 55 // compiling a method. | 63 // `arithmetic_simplication_test.dart`. |
| 56 disableTypeInference: true, | 64 MockCompiler compiler = new MockCompiler.internal( |
| 57 enableMinification: minify, | 65 enableTypeAssertions: enableTypeAssertions, |
| 58 disableInlining: disableInlining); | 66 // Type inference does not run when manually |
| 59 return compiler.init().then((_) { | 67 // compiling a method. |
| 68 disableTypeInference: true, |
| 69 enableMinification: minify, |
| 70 disableInlining: disableInlining); |
| 71 await compiler.init(); |
| 60 compiler.parseScript(code); | 72 compiler.parseScript(code); |
| 61 lego.Element element = compiler.mainApp.find(entry); | 73 lego.Element element = compiler.mainApp.find(entry); |
| 62 if (element == null) return null; | 74 if (element == null) return null; |
| 63 compiler.phase = Compiler.PHASE_RESOLVING; | 75 compiler.phase = Compiler.PHASE_RESOLVING; |
| 64 compiler.backend.enqueueHelpers(compiler.enqueuer.resolution, | 76 compiler.backend.enqueueHelpers(compiler.enqueuer.resolution, |
| 65 compiler.globalDependencies); | 77 compiler.globalDependencies); |
| 66 compiler.processQueue(compiler.enqueuer.resolution, element); | 78 compiler.processQueue(compiler.enqueuer.resolution, element); |
| 67 compiler.world.populate(); | 79 compiler.world.populate(); |
| 68 compiler.backend.onResolutionComplete(); | 80 compiler.backend.onResolutionComplete(); |
| 69 var context = new js.JavaScriptItemCompilationContext(); | 81 var context = new js.JavaScriptItemCompilationContext(); |
| 70 ResolutionWorkItem resolutionWork = | 82 ResolutionWorkItem resolutionWork = |
| 71 new ResolutionWorkItem(element, context); | 83 new ResolutionWorkItem(element, context); |
| 72 resolutionWork.run(compiler, compiler.enqueuer.resolution); | 84 resolutionWork.run(compiler, compiler.enqueuer.resolution); |
| 73 CodegenWorkItem work = | 85 CodegenWorkItem work = |
| 74 new CodegenWorkItem(compiler, element, context); | 86 new CodegenWorkItem(compiler, element, context); |
| 75 compiler.phase = Compiler.PHASE_COMPILING; | 87 compiler.phase = Compiler.PHASE_COMPILING; |
| 76 work.run(compiler, compiler.enqueuer.codegen); | 88 work.run(compiler, compiler.enqueuer.codegen); |
| 77 js.JavaScriptBackend backend = compiler.backend; | 89 js.JavaScriptBackend backend = compiler.backend; |
| 78 String generated = backend.assembleCode(element); | 90 String generated = backend.getGeneratedCode(element); |
| 79 if (check != null) { | 91 if (check != null) { |
| 80 check(generated); | 92 check(generated); |
| 81 } | 93 } |
| 82 return generated; | 94 return generated; |
| 83 }); | 95 } else { |
| 96 List<String> options = <String>[ |
| 97 Flags.disableTypeInference]; |
| 98 if (enableTypeAssertions) { |
| 99 options.add(Flags.enableCheckedMode); |
| 100 } |
| 101 if (minify) { |
| 102 options.add(Flags.minify); |
| 103 } |
| 104 if (analyzeAll) { |
| 105 options.add(Flags.analyzeAll); |
| 106 } |
| 107 |
| 108 Map<String, String> source; |
| 109 if (entry != 'main') { |
| 110 source = {'main.dart': "$code\n\nmain() => $entry;" }; |
| 111 } else { |
| 112 source = {'main.dart': code}; |
| 113 } |
| 114 |
| 115 CompilationResult result = await runCompiler( |
| 116 memorySourceFiles: source, |
| 117 options: options, |
| 118 beforeRun: (compiler) { |
| 119 if (disableInlining) { |
| 120 compiler.disableInlining = true; |
| 121 } |
| 122 }); |
| 123 Expect.isTrue(result.isSuccess); |
| 124 Compiler compiler = result.compiler; |
| 125 lego.Element element = compiler.mainApp.find(entry); |
| 126 js.JavaScriptBackend backend = compiler.backend; |
| 127 String generated = backend.getGeneratedCode(element); |
| 128 if (check != null) { |
| 129 check(generated); |
| 130 } |
| 131 return generated; |
| 132 } |
| 84 } | 133 } |
| 85 | 134 |
| 86 // TODO(herhut): Disallow warnings and errors during compilation by default. | 135 // TODO(herhut): Disallow warnings and errors during compilation by default. |
| 87 MockCompiler compilerFor(String code, Uri uri, | 136 MockCompiler compilerFor(String code, Uri uri, |
| 88 {bool analyzeAll: false, | 137 {bool analyzeAll: false, |
| 89 bool analyzeOnly: false, | 138 bool analyzeOnly: false, |
| 90 Map<String, String> coreSource, | 139 Map<String, String> coreSource, |
| 91 bool disableInlining: true, | 140 bool disableInlining: true, |
| 92 bool minify: false, | 141 bool minify: false, |
| 93 bool trustTypeAnnotations: false, | 142 bool trustTypeAnnotations: false, |
| (...skipping 124 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 218 | 267 |
| 219 void checkNumberOfMatches(Iterator it, int nb) { | 268 void checkNumberOfMatches(Iterator it, int nb) { |
| 220 bool hasNext = it.moveNext(); | 269 bool hasNext = it.moveNext(); |
| 221 for (int i = 0; i < nb; i++) { | 270 for (int i = 0; i < nb; i++) { |
| 222 Expect.isTrue(hasNext, "Found less than $nb matches"); | 271 Expect.isTrue(hasNext, "Found less than $nb matches"); |
| 223 hasNext = it.moveNext(); | 272 hasNext = it.moveNext(); |
| 224 } | 273 } |
| 225 Expect.isFalse(hasNext, "Found more than $nb matches"); | 274 Expect.isFalse(hasNext, "Found more than $nb matches"); |
| 226 } | 275 } |
| 227 | 276 |
| 228 Future compileAndMatch(String code, String entry, RegExp regexp) { | 277 Future compileAndMatch(String code, String entry, RegExp regexp, |
| 229 return compile(code, entry: entry, check: (String generated) { | 278 {bool useMock: false}) { |
| 279 return compile(code, entry: entry, |
| 280 useMock: useMock, |
| 281 check: (String generated) { |
| 230 Expect.isTrue(regexp.hasMatch(generated), | 282 Expect.isTrue(regexp.hasMatch(generated), |
| 231 '"$generated" does not match /$regexp/'); | 283 '"$generated" does not match /$regexp/'); |
| 232 }); | 284 }); |
| 233 } | 285 } |
| 234 | 286 |
| 235 Future compileAndDoNotMatch(String code, String entry, RegExp regexp) { | 287 Future compileAndDoNotMatch(String code, String entry, RegExp regexp) { |
| 236 return compile(code, entry: entry, check: (String generated) { | 288 return compile(code, entry: entry, check: (String generated) { |
| 237 Expect.isFalse(regexp.hasMatch(generated), | 289 Expect.isFalse(regexp.hasMatch(generated), |
| 238 '"$generated" has a match in /$regexp/'); | 290 '"$generated" has a match in /$regexp/'); |
| 239 }); | 291 }); |
| (...skipping 18 matching lines...) Expand all Loading... |
| 258 regexp = regexp.replaceAll(xRe, '(?:$anyIdentifier)'); | 310 regexp = regexp.replaceAll(xRe, '(?:$anyIdentifier)'); |
| 259 final spaceRe = new RegExp('\\s+'); | 311 final spaceRe = new RegExp('\\s+'); |
| 260 regexp = regexp.replaceAll(spaceRe, '(?:\\s*)'); | 312 regexp = regexp.replaceAll(spaceRe, '(?:\\s*)'); |
| 261 if (shouldMatch) { | 313 if (shouldMatch) { |
| 262 Expect.isTrue(new RegExp(regexp).hasMatch(generated)); | 314 Expect.isTrue(new RegExp(regexp).hasMatch(generated)); |
| 263 } else { | 315 } else { |
| 264 Expect.isFalse(new RegExp(regexp).hasMatch(generated)); | 316 Expect.isFalse(new RegExp(regexp).hasMatch(generated)); |
| 265 } | 317 } |
| 266 }); | 318 }); |
| 267 } | 319 } |
| OLD | NEW |