| 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 // Test constant folding. | 4 // Test constant folding. |
| 5 | 5 |
| 6 library compiler_helper; | 6 library compiler_helper; |
| 7 | 7 |
| 8 import 'dart:async'; | 8 import 'dart:async'; |
| 9 import "package:expect/expect.dart"; | 9 import "package:expect/expect.dart"; |
| 10 | 10 |
| (...skipping 27 matching lines...) Expand all Loading... |
| 38 import '../../../sdk/lib/_internal/compiler/implementation/source_file.dart'; | 38 import '../../../sdk/lib/_internal/compiler/implementation/source_file.dart'; |
| 39 | 39 |
| 40 import '../../../sdk/lib/_internal/compiler/implementation/dart2jslib.dart' | 40 import '../../../sdk/lib/_internal/compiler/implementation/dart2jslib.dart' |
| 41 show Compiler; | 41 show Compiler; |
| 42 | 42 |
| 43 export '../../../sdk/lib/_internal/compiler/implementation/tree/tree.dart'; | 43 export '../../../sdk/lib/_internal/compiler/implementation/tree/tree.dart'; |
| 44 | 44 |
| 45 import 'mock_compiler.dart'; | 45 import 'mock_compiler.dart'; |
| 46 export 'mock_compiler.dart'; | 46 export 'mock_compiler.dart'; |
| 47 | 47 |
| 48 import 'parser_helper.dart'; | 48 Future<String> compile(String code, |
| 49 | 49 {String entry: 'main', |
| 50 String compile(String code, {String entry: 'main', | 50 String coreSource: DEFAULT_CORELIB, |
| 51 String coreSource: DEFAULT_CORELIB, | 51 String interceptorsSource: DEFAULT_INTERCEPTORSLIB, |
| 52 String interceptorsSource: DEFAULT_INTERCEPTORSLIB, | 52 bool enableTypeAssertions: false, |
| 53 bool enableTypeAssertions: false, | 53 bool minify: false, |
| 54 bool minify: false, | 54 bool analyzeAll: false, |
| 55 bool analyzeAll: false, | 55 bool disableInlining: true, |
| 56 bool disableInlining: true}) { | 56 void check(String generated)}) { |
| 57 MockCompiler compiler = | 57 MockCompiler compiler = new MockCompiler.internal( |
| 58 new MockCompiler(enableTypeAssertions: enableTypeAssertions, | 58 enableTypeAssertions: enableTypeAssertions, |
| 59 coreSource: coreSource, | 59 coreSource: coreSource, |
| 60 // Type inference does not run when manually | 60 // Type inference does not run when manually |
| 61 // compiling a method. | 61 // compiling a method. |
| 62 disableTypeInference: true, | 62 disableTypeInference: true, |
| 63 interceptorsSource: interceptorsSource, | 63 interceptorsSource: interceptorsSource, |
| 64 enableMinification: minify, | 64 enableMinification: minify, |
| 65 disableInlining: disableInlining); | 65 disableInlining: disableInlining); |
| 66 compiler.parseScript(code); | 66 return compiler.init().then((_) { |
| 67 lego.Element element = compiler.mainApp.find(entry); | 67 compiler.parseScript(code); |
| 68 if (element == null) return null; | 68 lego.Element element = compiler.mainApp.find(entry); |
| 69 compiler.phase = Compiler.PHASE_RESOLVING; | 69 if (element == null) return null; |
| 70 compiler.backend.enqueueHelpers(compiler.enqueuer.resolution, | 70 compiler.phase = Compiler.PHASE_RESOLVING; |
| 71 compiler.globalDependencies); | 71 compiler.backend.enqueueHelpers(compiler.enqueuer.resolution, |
| 72 compiler.processQueue(compiler.enqueuer.resolution, element); | 72 compiler.globalDependencies); |
| 73 compiler.world.populate(); | 73 compiler.processQueue(compiler.enqueuer.resolution, element); |
| 74 var context = new js.JavaScriptItemCompilationContext(); | 74 compiler.world.populate(); |
| 75 leg.ResolutionWorkItem resolutionWork = | 75 var context = new js.JavaScriptItemCompilationContext(); |
| 76 new leg.ResolutionWorkItem(element, context); | 76 leg.ResolutionWorkItem resolutionWork = |
| 77 resolutionWork.run(compiler, compiler.enqueuer.resolution); | 77 new leg.ResolutionWorkItem(element, context); |
| 78 leg.CodegenWorkItem work = | 78 resolutionWork.run(compiler, compiler.enqueuer.resolution); |
| 79 new leg.CodegenWorkItem(element, context); | 79 leg.CodegenWorkItem work = |
| 80 compiler.phase = Compiler.PHASE_COMPILING; | 80 new leg.CodegenWorkItem(element, context); |
| 81 work.run(compiler, compiler.enqueuer.codegen); | 81 compiler.phase = Compiler.PHASE_COMPILING; |
| 82 js.JavaScriptBackend backend = compiler.backend; | 82 work.run(compiler, compiler.enqueuer.codegen); |
| 83 return backend.assembleCode(element); | 83 js.JavaScriptBackend backend = compiler.backend; |
| 84 String generated = backend.assembleCode(element); |
| 85 if (check != null) { |
| 86 check(generated); |
| 87 } |
| 88 return generated; |
| 89 }); |
| 84 } | 90 } |
| 85 | 91 |
| 86 // TODO(herhut): Disallow warnings and errors during compilation by default. | 92 // TODO(herhut): Disallow warnings and errors during compilation by default. |
| 87 MockCompiler compilerFor(String code, Uri uri, | 93 MockCompiler compilerFor(String code, Uri uri, |
| 88 {bool analyzeAll: false, | 94 {bool analyzeAll: false, |
| 89 bool analyzeOnly: false, | 95 bool analyzeOnly: false, |
| 90 String coreSource: DEFAULT_CORELIB, | 96 String coreSource: DEFAULT_CORELIB, |
| 91 bool disableInlining: true, | 97 bool disableInlining: true, |
| 92 bool minify: false, | 98 bool minify: false, |
| 93 int expectedErrors, | 99 int expectedErrors, |
| 94 int expectedWarnings}) { | 100 int expectedWarnings}) { |
| 95 MockCompiler compiler = new MockCompiler( | 101 MockCompiler compiler = new MockCompiler.internal( |
| 96 analyzeAll: analyzeAll, | 102 analyzeAll: analyzeAll, |
| 97 analyzeOnly: analyzeOnly, | 103 analyzeOnly: analyzeOnly, |
| 98 coreSource: coreSource, | 104 coreSource: coreSource, |
| 99 disableInlining: disableInlining, | 105 disableInlining: disableInlining, |
| 100 enableMinification: minify, | 106 enableMinification: minify, |
| 101 expectedErrors: expectedErrors, | 107 expectedErrors: expectedErrors, |
| 102 expectedWarnings: expectedWarnings); | 108 expectedWarnings: expectedWarnings); |
| 103 compiler.sourceFiles[uri.toString()] = | 109 compiler.sourceFiles[uri.toString()] = |
| 104 new StringSourceFile(uri.toString(), code); | 110 new StringSourceFile(uri.toString(), code); |
| 105 return compiler; | 111 return compiler; |
| (...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 186 | 192 |
| 187 String getIntTypeCheck(String variable) { | 193 String getIntTypeCheck(String variable) { |
| 188 return "\\($variable ?!== ?\\($variable ?\\| ?0\\)|" | 194 return "\\($variable ?!== ?\\($variable ?\\| ?0\\)|" |
| 189 "\\($variable ?>>> ?0 ?!== ?$variable"; | 195 "\\($variable ?>>> ?0 ?!== ?$variable"; |
| 190 } | 196 } |
| 191 | 197 |
| 192 String getNumberTypeCheck(String variable) { | 198 String getNumberTypeCheck(String variable) { |
| 193 return """\\(typeof $variable ?!== ?"number"\\)"""; | 199 return """\\(typeof $variable ?!== ?"number"\\)"""; |
| 194 } | 200 } |
| 195 | 201 |
| 196 bool checkNumberOfMatches(Iterator it, int nb) { | 202 void checkNumberOfMatches(Iterator it, int nb) { |
| 197 bool hasNext = it.moveNext(); | 203 bool hasNext = it.moveNext(); |
| 198 for (int i = 0; i < nb; i++) { | 204 for (int i = 0; i < nb; i++) { |
| 199 Expect.isTrue(hasNext, "Found less than $nb matches"); | 205 Expect.isTrue(hasNext, "Found less than $nb matches"); |
| 200 hasNext = it.moveNext(); | 206 hasNext = it.moveNext(); |
| 201 } | 207 } |
| 202 Expect.isFalse(hasNext, "Found more than $nb matches"); | 208 Expect.isFalse(hasNext, "Found more than $nb matches"); |
| 203 } | 209 } |
| 204 | 210 |
| 205 void compileAndMatch(String code, String entry, RegExp regexp) { | 211 Future compileAndMatch(String code, String entry, RegExp regexp) { |
| 206 String generated = compile(code, entry: entry); | 212 return compile(code, entry: entry, check: (String generated) { |
| 207 Expect.isTrue(regexp.hasMatch(generated), | 213 Expect.isTrue(regexp.hasMatch(generated), |
| 208 '"$generated" does not match /$regexp/'); | 214 '"$generated" does not match /$regexp/'); |
| 215 }); |
| 209 } | 216 } |
| 210 | 217 |
| 211 void compileAndDoNotMatch(String code, String entry, RegExp regexp) { | 218 Future compileAndDoNotMatch(String code, String entry, RegExp regexp) { |
| 212 String generated = compile(code, entry: entry); | 219 return compile(code, entry: entry, check: (String generated) { |
| 213 Expect.isFalse(regexp.hasMatch(generated), | 220 Expect.isFalse(regexp.hasMatch(generated), |
| 214 '"$generated" has a match in /$regexp/'); | 221 '"$generated" has a match in /$regexp/'); |
| 222 }); |
| 215 } | 223 } |
| 216 | 224 |
| 217 int length(Link link) => link.isEmpty ? 0 : length(link.tail) + 1; | 225 int length(Link link) => link.isEmpty ? 0 : length(link.tail) + 1; |
| 218 | 226 |
| 219 // Does a compile and then a match where every 'x' is replaced by something | 227 // Does a compile and then a match where every 'x' is replaced by something |
| 220 // that matches any variable, and every space is optional. | 228 // that matches any variable, and every space is optional. |
| 221 void compileAndMatchFuzzy(String code, String entry, String regexp) { | 229 Future compileAndMatchFuzzy(String code, String entry, String regexp) { |
| 222 compileAndMatchFuzzyHelper(code, entry, regexp, true); | 230 return compileAndMatchFuzzyHelper(code, entry, regexp, true); |
| 223 } | 231 } |
| 224 | 232 |
| 225 void compileAndDoNotMatchFuzzy(String code, String entry, String regexp) { | 233 Future compileAndDoNotMatchFuzzy(String code, String entry, String regexp) { |
| 226 compileAndMatchFuzzyHelper(code, entry, regexp, false); | 234 return compileAndMatchFuzzyHelper(code, entry, regexp, false); |
| 227 } | 235 } |
| 228 | 236 |
| 229 void compileAndMatchFuzzyHelper( | 237 Future compileAndMatchFuzzyHelper( |
| 230 String code, String entry, String regexp, bool shouldMatch) { | 238 String code, String entry, String regexp, bool shouldMatch) { |
| 231 String generated = compile(code, entry: entry); | 239 return compile(code, entry: entry, check: (String generated) { |
| 232 final xRe = new RegExp('\\bx\\b'); | 240 final xRe = new RegExp('\\bx\\b'); |
| 233 regexp = regexp.replaceAll(xRe, '(?:$anyIdentifier)'); | 241 regexp = regexp.replaceAll(xRe, '(?:$anyIdentifier)'); |
| 234 final spaceRe = new RegExp('\\s+'); | 242 final spaceRe = new RegExp('\\s+'); |
| 235 regexp = regexp.replaceAll(spaceRe, '(?:\\s*)'); | 243 regexp = regexp.replaceAll(spaceRe, '(?:\\s*)'); |
| 236 if (shouldMatch) { | 244 if (shouldMatch) { |
| 237 Expect.isTrue(new RegExp(regexp).hasMatch(generated)); | 245 Expect.isTrue(new RegExp(regexp).hasMatch(generated)); |
| 238 } else { | 246 } else { |
| 239 Expect.isFalse(new RegExp(regexp).hasMatch(generated)); | 247 Expect.isFalse(new RegExp(regexp).hasMatch(generated)); |
| 240 } | 248 } |
| 249 }); |
| 241 } | 250 } |
| OLD | NEW |