Chromium Code Reviews| 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 |
| 45 Future<String> compile(String code, | 48 Future<String> compile(String code, |
| 46 {String entry: 'main', | 49 {String entry: 'main', |
| 47 bool enableTypeAssertions: false, | 50 bool enableTypeAssertions: false, |
| 48 bool minify: false, | 51 bool minify: false, |
| 49 bool analyzeAll: false, | 52 bool analyzeAll: false, |
| 50 bool disableInlining: true, | 53 bool disableInlining: true, |
| 51 void check(String generated)}) { | 54 bool useMock: false, |
| 52 MockCompiler compiler = new MockCompiler.internal( | 55 void check(String generated)}) async { |
|
sigurdm
2015/09/10 06:37:51
This parameter could use an explanation.
Johnni Winther
2015/09/11 10:42:06
Done.
| |
| 53 enableTypeAssertions: enableTypeAssertions, | 56 if (useMock) { |
| 54 // Type inference does not run when manually | 57 // TODO(johnniwinther): Remove this when no longer needed by |
| 55 // compiling a method. | 58 // `arithmetic_simplication_test.dart`. |
| 56 disableTypeInference: true, | 59 MockCompiler compiler = new MockCompiler.internal( |
| 57 enableMinification: minify, | 60 enableTypeAssertions: enableTypeAssertions, |
| 58 disableInlining: disableInlining); | 61 // Type inference does not run when manually |
| 59 return compiler.init().then((_) { | 62 // compiling a method. |
| 63 disableTypeInference: true, | |
| 64 enableMinification: minify, | |
| 65 disableInlining: disableInlining); | |
| 66 await compiler.init(); | |
| 60 compiler.parseScript(code); | 67 compiler.parseScript(code); |
| 61 lego.Element element = compiler.mainApp.find(entry); | 68 lego.Element element = compiler.mainApp.find(entry); |
| 62 if (element == null) return null; | 69 if (element == null) return null; |
| 63 compiler.phase = Compiler.PHASE_RESOLVING; | 70 compiler.phase = Compiler.PHASE_RESOLVING; |
| 64 compiler.backend.enqueueHelpers(compiler.enqueuer.resolution, | 71 compiler.backend.enqueueHelpers(compiler.enqueuer.resolution, |
| 65 compiler.globalDependencies); | 72 compiler.globalDependencies); |
| 66 compiler.processQueue(compiler.enqueuer.resolution, element); | 73 compiler.processQueue(compiler.enqueuer.resolution, element); |
| 67 compiler.world.populate(); | 74 compiler.world.populate(); |
| 68 compiler.backend.onResolutionComplete(); | 75 compiler.backend.onResolutionComplete(); |
| 69 var context = new js.JavaScriptItemCompilationContext(); | 76 var context = new js.JavaScriptItemCompilationContext(); |
| 70 ResolutionWorkItem resolutionWork = | 77 ResolutionWorkItem resolutionWork = |
| 71 new ResolutionWorkItem(element, context); | 78 new ResolutionWorkItem(element, context); |
| 72 resolutionWork.run(compiler, compiler.enqueuer.resolution); | 79 resolutionWork.run(compiler, compiler.enqueuer.resolution); |
| 73 CodegenWorkItem work = | 80 CodegenWorkItem work = |
| 74 new CodegenWorkItem(compiler, element, context); | 81 new CodegenWorkItem(compiler, element, context); |
| 75 compiler.phase = Compiler.PHASE_COMPILING; | 82 compiler.phase = Compiler.PHASE_COMPILING; |
| 76 work.run(compiler, compiler.enqueuer.codegen); | 83 work.run(compiler, compiler.enqueuer.codegen); |
| 77 js.JavaScriptBackend backend = compiler.backend; | 84 js.JavaScriptBackend backend = compiler.backend; |
| 78 String generated = backend.assembleCode(element); | 85 String generated = backend.assembleCode(element); |
| 79 if (check != null) { | 86 if (check != null) { |
| 80 check(generated); | 87 check(generated); |
| 81 } | 88 } |
| 82 return generated; | 89 return generated; |
| 83 }); | 90 } else { |
| 91 List<String> options = <String>[ | |
| 92 Flags.disableTypeInference]; | |
| 93 if (enableTypeAssertions) { | |
| 94 options.add(Flags.enableCheckedMode); | |
| 95 } | |
| 96 if (minify) { | |
| 97 options.add(Flags.minify); | |
| 98 } | |
| 99 if (analyzeAll) { | |
| 100 options.add(Flags.analyzeAll); | |
| 101 } | |
| 102 | |
| 103 Map<String, String> source; | |
| 104 if (entry != 'main') { | |
| 105 source = {'main.dart': "$code\n\nmain() => $entry;" }; | |
| 106 } else { | |
| 107 source = {'main.dart': code}; | |
| 108 } | |
| 109 | |
| 110 CompilationResult result = await runCompiler( | |
| 111 memorySourceFiles: source, | |
| 112 options: options, | |
| 113 beforeRun: (compiler) { | |
| 114 if (disableInlining) { | |
| 115 compiler.disableInlining = true; | |
| 116 } | |
| 117 }); | |
| 118 Expect.isTrue(result.isSuccess); | |
| 119 Compiler compiler = result.compiler; | |
| 120 lego.Element element = compiler.mainApp.find(entry); | |
| 121 js.JavaScriptBackend backend = compiler.backend; | |
|
sigurdm
2015/09/10 06:37:51
Code from here could be moved out of the if.
Johnni Winther
2015/09/11 10:42:06
They use different compilers and the true branch i
| |
| 122 String generated = backend.assembleCode(element); | |
| 123 if (check != null) { | |
| 124 check(generated); | |
| 125 } | |
| 126 return generated; | |
| 127 } | |
| 84 } | 128 } |
| 85 | 129 |
| 86 // TODO(herhut): Disallow warnings and errors during compilation by default. | 130 // TODO(herhut): Disallow warnings and errors during compilation by default. |
| 87 MockCompiler compilerFor(String code, Uri uri, | 131 MockCompiler compilerFor(String code, Uri uri, |
| 88 {bool analyzeAll: false, | 132 {bool analyzeAll: false, |
| 89 bool analyzeOnly: false, | 133 bool analyzeOnly: false, |
| 90 Map<String, String> coreSource, | 134 Map<String, String> coreSource, |
| 91 bool disableInlining: true, | 135 bool disableInlining: true, |
| 92 bool minify: false, | 136 bool minify: false, |
| 93 bool trustTypeAnnotations: false, | 137 bool trustTypeAnnotations: false, |
| (...skipping 124 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 218 | 262 |
| 219 void checkNumberOfMatches(Iterator it, int nb) { | 263 void checkNumberOfMatches(Iterator it, int nb) { |
| 220 bool hasNext = it.moveNext(); | 264 bool hasNext = it.moveNext(); |
| 221 for (int i = 0; i < nb; i++) { | 265 for (int i = 0; i < nb; i++) { |
| 222 Expect.isTrue(hasNext, "Found less than $nb matches"); | 266 Expect.isTrue(hasNext, "Found less than $nb matches"); |
| 223 hasNext = it.moveNext(); | 267 hasNext = it.moveNext(); |
| 224 } | 268 } |
| 225 Expect.isFalse(hasNext, "Found more than $nb matches"); | 269 Expect.isFalse(hasNext, "Found more than $nb matches"); |
| 226 } | 270 } |
| 227 | 271 |
| 228 Future compileAndMatch(String code, String entry, RegExp regexp) { | 272 Future compileAndMatch(String code, String entry, RegExp regexp, |
| 229 return compile(code, entry: entry, check: (String generated) { | 273 {bool useMock: false}) { |
| 274 return compile(code, entry: entry, | |
| 275 useMock: useMock, | |
| 276 check: (String generated) { | |
| 230 Expect.isTrue(regexp.hasMatch(generated), | 277 Expect.isTrue(regexp.hasMatch(generated), |
| 231 '"$generated" does not match /$regexp/'); | 278 '"$generated" does not match /$regexp/'); |
| 232 }); | 279 }); |
| 233 } | 280 } |
| 234 | 281 |
| 235 Future compileAndDoNotMatch(String code, String entry, RegExp regexp) { | 282 Future compileAndDoNotMatch(String code, String entry, RegExp regexp) { |
| 236 return compile(code, entry: entry, check: (String generated) { | 283 return compile(code, entry: entry, check: (String generated) { |
| 237 Expect.isFalse(regexp.hasMatch(generated), | 284 Expect.isFalse(regexp.hasMatch(generated), |
| 238 '"$generated" has a match in /$regexp/'); | 285 '"$generated" has a match in /$regexp/'); |
| 239 }); | 286 }); |
| (...skipping 18 matching lines...) Expand all Loading... | |
| 258 regexp = regexp.replaceAll(xRe, '(?:$anyIdentifier)'); | 305 regexp = regexp.replaceAll(xRe, '(?:$anyIdentifier)'); |
| 259 final spaceRe = new RegExp('\\s+'); | 306 final spaceRe = new RegExp('\\s+'); |
| 260 regexp = regexp.replaceAll(spaceRe, '(?:\\s*)'); | 307 regexp = regexp.replaceAll(spaceRe, '(?:\\s*)'); |
| 261 if (shouldMatch) { | 308 if (shouldMatch) { |
| 262 Expect.isTrue(new RegExp(regexp).hasMatch(generated)); | 309 Expect.isTrue(new RegExp(regexp).hasMatch(generated)); |
| 263 } else { | 310 } else { |
| 264 Expect.isFalse(new RegExp(regexp).hasMatch(generated)); | 311 Expect.isFalse(new RegExp(regexp).hasMatch(generated)); |
| 265 } | 312 } |
| 266 }); | 313 }); |
| 267 } | 314 } |
| OLD | NEW |