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 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
55 bool analyzeAll: false}) { | 55 bool analyzeAll: false}) { |
56 MockCompiler compiler = | 56 MockCompiler compiler = |
57 new MockCompiler(enableTypeAssertions: enableTypeAssertions, | 57 new MockCompiler(enableTypeAssertions: enableTypeAssertions, |
58 coreSource: coreSource, | 58 coreSource: coreSource, |
59 // Type inference does not run when manually | 59 // Type inference does not run when manually |
60 // compiling a method. | 60 // compiling a method. |
61 disableTypeInference: true, | 61 disableTypeInference: true, |
62 interceptorsSource: interceptorsSource, | 62 interceptorsSource: interceptorsSource, |
63 enableMinification: minify); | 63 enableMinification: minify); |
64 compiler.parseScript(code); | 64 compiler.parseScript(code); |
65 lego.Element element = compiler.mainApp.find(buildSourceString(entry)); | 65 lego.Element element = compiler.mainApp.find(entry); |
66 if (element == null) return null; | 66 if (element == null) return null; |
67 compiler.phase = Compiler.PHASE_RESOLVING; | 67 compiler.phase = Compiler.PHASE_RESOLVING; |
68 compiler.backend.enqueueHelpers(compiler.enqueuer.resolution, | 68 compiler.backend.enqueueHelpers(compiler.enqueuer.resolution, |
69 compiler.globalDependencies); | 69 compiler.globalDependencies); |
70 compiler.processQueue(compiler.enqueuer.resolution, element); | 70 compiler.processQueue(compiler.enqueuer.resolution, element); |
71 compiler.world.populate(); | 71 compiler.world.populate(); |
72 var context = new js.JavaScriptItemCompilationContext(); | 72 var context = new js.JavaScriptItemCompilationContext(); |
73 leg.ResolutionWorkItem resolutionWork = | 73 leg.ResolutionWorkItem resolutionWork = |
74 new leg.ResolutionWorkItem(element, context); | 74 new leg.ResolutionWorkItem(element, context); |
75 resolutionWork.run(compiler, compiler.enqueuer.resolution); | 75 resolutionWork.run(compiler, compiler.enqueuer.resolution); |
76 leg.CodegenWorkItem work = | 76 leg.CodegenWorkItem work = |
77 new leg.CodegenWorkItem(element, context); | 77 new leg.CodegenWorkItem(element, context); |
78 compiler.phase = Compiler.PHASE_COMPILING; | 78 compiler.phase = Compiler.PHASE_COMPILING; |
79 work.run(compiler, compiler.enqueuer.codegen); | 79 work.run(compiler, compiler.enqueuer.codegen); |
80 js.JavaScriptBackend backend = compiler.backend; | 80 js.JavaScriptBackend backend = compiler.backend; |
81 return backend.assembleCode(element); | 81 return backend.assembleCode(element); |
82 } | 82 } |
83 | 83 |
84 MockCompiler compilerFor(String code, Uri uri, | 84 MockCompiler compilerFor(String code, Uri uri, |
85 {bool analyzeAll: false, | 85 {bool analyzeAll: false, |
86 bool analyzeOnly: false, | 86 bool analyzeOnly: false, |
87 String coreSource: DEFAULT_CORELIB}) { | 87 String coreSource: DEFAULT_CORELIB}) { |
88 MockCompiler compiler = new MockCompiler( | 88 MockCompiler compiler = new MockCompiler( |
89 analyzeAll: analyzeAll, | 89 analyzeAll: analyzeAll, |
90 analyzeOnly: analyzeOnly, | 90 analyzeOnly: analyzeOnly, |
91 coreSource: coreSource); | 91 coreSource: coreSource); |
92 compiler.sourceFiles[uri.toString()] = new SourceFile(uri.toString(), code); | 92 compiler.sourceFiles[uri.toString()] = |
| 93 new StringSourceFile(uri.toString(), code); |
93 return compiler; | 94 return compiler; |
94 } | 95 } |
95 | 96 |
96 Future<String> compileAll(String code, {String coreSource: DEFAULT_CORELIB}) { | 97 Future<String> compileAll(String code, {String coreSource: DEFAULT_CORELIB}) { |
97 Uri uri = new Uri(scheme: 'source'); | 98 Uri uri = new Uri(scheme: 'source'); |
98 MockCompiler compiler = compilerFor(code, uri, coreSource: coreSource); | 99 MockCompiler compiler = compilerFor(code, uri, coreSource: coreSource); |
99 return compiler.runCompiler(uri).then((_) { | 100 return compiler.runCompiler(uri).then((_) { |
100 Expect.isFalse(compiler.compilationFailed, | 101 Expect.isFalse(compiler.compilationFailed, |
101 'Unexpected compilation error'); | 102 'Unexpected compilation error'); |
102 return compiler.assembledCode; | 103 return compiler.assembledCode; |
(...skipping 22 matching lines...) Expand all Loading... |
125 if (path == 'main.dart') return; | 126 if (path == 'main.dart') return; |
126 compiler.registerSource(base.resolve(path), code); | 127 compiler.registerSource(base.resolve(path), code); |
127 }); | 128 }); |
128 | 129 |
129 return compiler.runCompiler(mainUri).then((_) { | 130 return compiler.runCompiler(mainUri).then((_) { |
130 return check(compiler); | 131 return check(compiler); |
131 }); | 132 }); |
132 } | 133 } |
133 | 134 |
134 lego.Element findElement(compiler, String name) { | 135 lego.Element findElement(compiler, String name) { |
135 var element = compiler.mainApp.find(buildSourceString(name)); | 136 var element = compiler.mainApp.find(name); |
136 Expect.isNotNull(element, 'Could not locate $name.'); | 137 Expect.isNotNull(element, 'Could not locate $name.'); |
137 return element; | 138 return element; |
138 } | 139 } |
139 | 140 |
140 types.TypeMask findTypeMask(compiler, String name, | 141 types.TypeMask findTypeMask(compiler, String name, |
141 [String how = 'nonNullExact']) { | 142 [String how = 'nonNullExact']) { |
142 var sourceName = buildSourceString(name); | 143 var sourceName = name; |
143 var element = compiler.mainApp.find(sourceName); | 144 var element = compiler.mainApp.find(sourceName); |
144 if (element == null) { | 145 if (element == null) { |
145 element = compiler.interceptorsLibrary.find(sourceName); | 146 element = compiler.interceptorsLibrary.find(sourceName); |
146 } | 147 } |
147 if (element == null) { | 148 if (element == null) { |
148 element = compiler.coreLibrary.find(sourceName); | 149 element = compiler.coreLibrary.find(sourceName); |
149 } | 150 } |
150 Expect.isNotNull(element, 'Could not locate $name'); | 151 Expect.isNotNull(element, 'Could not locate $name'); |
151 switch (how) { | 152 switch (how) { |
152 case 'exact': return new types.TypeMask.exact(element); | 153 case 'exact': return new types.TypeMask.exact(element); |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
213 final xRe = new RegExp('\\bx\\b'); | 214 final xRe = new RegExp('\\bx\\b'); |
214 regexp = regexp.replaceAll(xRe, '(?:$anyIdentifier)'); | 215 regexp = regexp.replaceAll(xRe, '(?:$anyIdentifier)'); |
215 final spaceRe = new RegExp('\\s+'); | 216 final spaceRe = new RegExp('\\s+'); |
216 regexp = regexp.replaceAll(spaceRe, '(?:\\s*)'); | 217 regexp = regexp.replaceAll(spaceRe, '(?:\\s*)'); |
217 if (shouldMatch) { | 218 if (shouldMatch) { |
218 Expect.isTrue(new RegExp(regexp).hasMatch(generated)); | 219 Expect.isTrue(new RegExp(regexp).hasMatch(generated)); |
219 } else { | 220 } else { |
220 Expect.isFalse(new RegExp(regexp).hasMatch(generated)); | 221 Expect.isFalse(new RegExp(regexp).hasMatch(generated)); |
221 } | 222 } |
222 } | 223 } |
OLD | NEW |