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 mirrors_dart2js; | 5 library mirrors_dart2js; |
6 | 6 |
7 import 'dart:async'; | 7 import 'dart:async'; |
8 import 'dart:collection' show LinkedHashMap; | 8 import 'dart:collection' show LinkedHashMap; |
9 import 'dart:io'; | 9 import 'dart:io'; |
10 import 'dart:uri'; | 10 import 'dart:uri'; |
11 | 11 |
12 import '../../compiler.dart' as diagnostics; | 12 import '../../compiler.dart' as api; |
13 import '../elements/elements.dart'; | 13 import '../elements/elements.dart'; |
14 import '../resolution/resolution.dart' show ResolverTask, ResolverVisitor; | 14 import '../resolution/resolution.dart' show ResolverTask, ResolverVisitor; |
15 import '../apiimpl.dart' show Compiler; | 15 import '../apiimpl.dart' show Compiler; |
16 import '../scanner/scannerlib.dart' hide SourceString; | 16 import '../scanner/scannerlib.dart' hide SourceString; |
17 import '../ssa/ssa.dart'; | 17 import '../ssa/ssa.dart'; |
18 import '../dart2jslib.dart' hide Compiler; | 18 import '../dart2jslib.dart' hide Compiler; |
19 import '../dart_types.dart'; | 19 import '../dart_types.dart'; |
20 import '../filenames.dart'; | 20 import '../filenames.dart'; |
21 import '../source_file.dart'; | 21 import '../source_file.dart'; |
22 import '../tree/tree.dart'; | 22 import '../tree/tree.dart'; |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
77 } else if (type is FunctionType) { | 77 } else if (type is FunctionType) { |
78 return new Dart2JsFunctionTypeMirror(system, type, functionSignature); | 78 return new Dart2JsFunctionTypeMirror(system, type, functionSignature); |
79 } else if (type is VoidType) { | 79 } else if (type is VoidType) { |
80 return new Dart2JsVoidMirror(system, type); | 80 return new Dart2JsVoidMirror(system, type); |
81 } else if (type is TypedefType) { | 81 } else if (type is TypedefType) { |
82 return new Dart2JsTypedefMirror(system, type); | 82 return new Dart2JsTypedefMirror(system, type); |
83 } else if (type is MalformedType) { | 83 } else if (type is MalformedType) { |
84 // TODO(johnniwinther): We need a mirror on malformed types. | 84 // TODO(johnniwinther): We need a mirror on malformed types. |
85 return system.dynamicType; | 85 return system.dynamicType; |
86 } | 86 } |
87 _diagnosticListener.internalError( | |
88 "Unexpected type $type of kind ${type.kind}"); | |
89 system.compiler.internalError("Unexpected type $type of kind ${type.kind}"); | 87 system.compiler.internalError("Unexpected type $type of kind ${type.kind}"); |
90 } | 88 } |
91 | 89 |
92 Collection<Dart2JsMemberMirror> _convertElementMemberToMemberMirrors( | 90 Collection<Dart2JsMemberMirror> _convertElementMemberToMemberMirrors( |
93 Dart2JsContainerMirror library, Element element) { | 91 Dart2JsContainerMirror library, Element element) { |
94 if (element.isSynthesized) { | 92 if (element.isSynthesized) { |
95 return const <Dart2JsMemberMirror>[]; | 93 return const <Dart2JsMemberMirror>[]; |
96 } else if (element is VariableElement) { | 94 } else if (element is VariableElement) { |
97 return <Dart2JsMemberMirror>[new Dart2JsFieldMirror(library, element)]; | 95 return <Dart2JsMemberMirror>[new Dart2JsFieldMirror(library, element)]; |
98 } else if (element is FunctionElement) { | 96 } else if (element is FunctionElement) { |
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
186 'xor': '^', | 184 'xor': '^', |
187 'or': '|', | 185 'or': '|', |
188 }; | 186 }; |
189 String newName = mapping[name]; | 187 String newName = mapping[name]; |
190 if (newName == null) { | 188 if (newName == null) { |
191 throw new Exception('Unhandled operator name: $name'); | 189 throw new Exception('Unhandled operator name: $name'); |
192 } | 190 } |
193 return newName; | 191 return newName; |
194 } | 192 } |
195 | 193 |
196 DiagnosticListener get _diagnosticListener { | |
197 return const Dart2JsDiagnosticListener(); | |
198 } | |
199 | |
200 class Dart2JsDiagnosticListener implements DiagnosticListener { | |
201 const Dart2JsDiagnosticListener(); | |
202 | |
203 void cancel(String reason, {node, token, instruction, element}) { | |
204 print(reason); | |
205 } | |
206 | |
207 void log(message) { | |
208 print(message); | |
209 } | |
210 | |
211 void internalError(String message, | |
212 {Node node, Token token, HInstruction instruction, | |
213 Element element}) { | |
214 cancel('Internal error: $message', node: node, token: token, | |
215 instruction: instruction, element: element); | |
216 } | |
217 | |
218 void internalErrorOnElement(Element element, String message) { | |
219 internalError(message, element: element); | |
220 } | |
221 | |
222 SourceSpan spanFromSpannable(Node node, [Uri uri]) { | |
223 // TODO(johnniwinther): implement this. | |
224 throw 'unimplemented'; | |
225 } | |
226 | |
227 void reportMessage(SourceSpan span, Diagnostic message, | |
228 diagnostics.Diagnostic kind) { | |
229 // TODO(johnniwinther): implement this. | |
230 throw 'unimplemented'; | |
231 } | |
232 | |
233 bool onDeprecatedFeature(Spannable span, String feature) { | |
234 // TODO(johnniwinther): implement this? | |
235 throw 'unimplemented'; | |
236 } | |
237 } | |
238 | |
239 //------------------------------------------------------------------------------ | 194 //------------------------------------------------------------------------------ |
240 // Compilation implementation | 195 // Compilation implementation |
241 //------------------------------------------------------------------------------ | 196 //------------------------------------------------------------------------------ |
242 | 197 |
243 // TODO(johnniwinther): Support client configurable handlers/providers. | 198 // TODO(johnniwinther): Support client configurable providers. |
244 class Dart2JsCompilation implements Compilation { | |
245 Compiler _compiler; | |
246 final Uri cwd; | |
247 final SourceFileProvider provider; | |
248 | 199 |
249 Dart2JsCompilation(Path script, Path libraryRoot, | 200 /** |
250 [Path packageRoot, List<String> opts = const <String>[]]) | 201 * Returns a future that completes to a non-null String when [script] |
251 : cwd = getCurrentDirectory(), | 202 * has been successfully compiled. |
ahe
2013/03/05 14:48:31
Add TODO and explain this deprecated but here to s
Johnni Winther
2013/03/06 08:13:11
Done.
| |
252 provider = new SourceFileProvider() { | 203 */ |
253 var handler = new FormattingDiagnosticHandler(provider); | 204 Future<String> compile(Path script, |
254 var libraryUri = cwd.resolve('${libraryRoot}/'); | 205 Path libraryRoot, |
255 var packageUri; | 206 {Path packageRoot, |
256 if (packageRoot != null) { | 207 List<String> options: const <String>[], |
257 packageUri = cwd.resolve('${packageRoot}/'); | 208 api.DiagnosticHandler diagnosticHandler}) { |
258 } else { | 209 Uri cwd = getCurrentDirectory(); |
259 packageUri = libraryUri; | 210 SourceFileProvider provider = new SourceFileProvider(); |
211 if (diagnosticHandler == null) { | |
212 diagnosticHandler = | |
213 new FormattingDiagnosticHandler(provider).diagnosticHandler; | |
214 } | |
215 Uri scriptUri = cwd.resolve(script.toString()); | |
216 Uri libraryUri = cwd.resolve('${libraryRoot}/'); | |
217 Uri packageUri = null; | |
218 if (packageRoot != null) { | |
219 packageUri = cwd.resolve('${packageRoot}/'); | |
220 } | |
221 return api.compile(scriptUri, libraryUri, packageUri, | |
222 provider.readStringFromUri, diagnosticHandler, options); | |
223 } | |
224 | |
225 /** | |
226 * Analyzes set of libraries and provides a mirror system which can be used for | |
227 * static inspection of the source code. | |
228 */ | |
229 Future<MirrorSystem> analyze(List<Path> libraries, | |
230 Path libraryRoot, | |
231 {Path packageRoot, | |
232 List<String> options: const <String>[], | |
233 api.DiagnosticHandler diagnosticHandler}) { | |
234 Uri cwd = getCurrentDirectory(); | |
235 SourceFileProvider provider = new SourceFileProvider(); | |
236 if (diagnosticHandler == null) { | |
237 diagnosticHandler = | |
238 new FormattingDiagnosticHandler(provider).diagnosticHandler; | |
239 } | |
240 Uri libraryUri = cwd.resolve('${libraryRoot}/'); | |
241 Uri packageUri = null; | |
242 if (packageRoot != null) { | |
243 packageUri = cwd.resolve('${packageRoot}/'); | |
244 } | |
245 options = new List<String>.from(options, growable: true); | |
ahe
2013/03/05 14:48:31
SIGH!!!
Johnni Winther
2013/03/06 08:13:11
Done.
| |
246 options.add('--analyze-only'); | |
247 options.add('--analyze-signatures-only'); | |
248 options.add('--analyze-all'); | |
249 | |
250 bool compilationFailed = false; | |
251 void internalDiagnosticHandler(Uri uri, int begin, int end, | |
252 String message, api.Diagnostic kind) { | |
253 if (kind == api.Diagnostic.ERROR || | |
254 kind == api.Diagnostic.CRASH) { | |
255 compilationFailed = true; | |
260 } | 256 } |
261 _compiler = new Compiler(provider.readStringFromUri, | 257 diagnosticHandler(uri, begin, end, message, kind); |
262 null, | |
263 handler.diagnosticHandler, | |
264 libraryUri, packageUri, opts); | |
265 var scriptUri = cwd.resolve(script.toString()); | |
266 // TODO(johnniwinther): Detect file not found | |
267 _compiler.run(scriptUri); | |
268 } | 258 } |
269 | 259 |
270 Dart2JsCompilation.library(List<Path> libraries, Path libraryRoot, | 260 // TODO(johnniwinther): Extract the following as an [analyze] method in |
271 [Path packageRoot, List<String> opts = const <String>[]]) | 261 // [:compiler/compiler.dart:]. |
272 : cwd = getCurrentDirectory(), | 262 Compiler compiler = new Compiler(provider.readStringFromUri, |
273 provider = new SourceFileProvider() { | 263 null, |
274 var libraryUri = cwd.resolve('${libraryRoot}/'); | 264 internalDiagnosticHandler, |
275 var packageUri; | 265 libraryUri, packageUri, options); |
276 if (packageRoot != null) { | 266 Completer<MirrorSystem> result = new Completer<MirrorSystem>(); |
ahe
2013/03/05 14:48:31
I think you can use "new Future.immediate" and "ne
Johnni Winther
2013/03/06 08:13:11
Done.
| |
277 packageUri = cwd.resolve('${packageRoot}/'); | 267 List<Uri> librariesUri = <Uri>[]; |
278 } else { | 268 for (Path library in libraries) { |
279 packageUri = libraryUri; | 269 librariesUri.add(cwd.resolve(library.toString())); |
280 } | |
281 opts = new List<String>.from(opts); | |
282 opts.add('--analyze-only'); | |
283 opts.add('--analyze-signatures-only'); | |
284 opts.add('--analyze-all'); | |
285 _compiler = new Compiler(provider.readStringFromUri, | |
286 null, | |
287 silentDiagnosticHandler, | |
288 libraryUri, packageUri, opts); | |
289 var librariesUri = <Uri>[]; | |
290 for (Path library in libraries) { | |
291 librariesUri.add(cwd.resolve(library.toString())); | |
292 // TODO(johnniwinther): Detect file not found | |
293 } | |
294 _compiler.librariesToAnalyzeWhenRun = librariesUri; | |
295 _compiler.run(null); | |
296 } | 270 } |
297 | 271 compiler.librariesToAnalyzeWhenRun = librariesUri; |
298 MirrorSystem get mirrors => new Dart2JsMirrorSystem(_compiler); | 272 bool success = compiler.run(null); |
299 | 273 if (success && !compilationFailed) { |
300 Future<String> compileToJavaScript() => | 274 result.complete(new Dart2JsMirrorSystem(compiler)); |
301 new Future<String>.immediate(_compiler.assembledCode); | 275 } else { |
276 result.completeError('Failed to create mirror system.'); | |
277 } | |
278 return result.future; | |
302 } | 279 } |
303 | 280 |
304 | |
305 //------------------------------------------------------------------------------ | 281 //------------------------------------------------------------------------------ |
306 // Dart2Js specific extensions of mirror interfaces | 282 // Dart2Js specific extensions of mirror interfaces |
307 //------------------------------------------------------------------------------ | 283 //------------------------------------------------------------------------------ |
308 | 284 |
309 abstract class Dart2JsMirror implements Mirror { | 285 abstract class Dart2JsMirror implements Mirror { |
310 Dart2JsMirrorSystem get mirrors; | 286 Dart2JsMirrorSystem get mirrors; |
311 } | 287 } |
312 | 288 |
313 abstract class Dart2JsDeclarationMirror extends Dart2JsMirror | 289 abstract class Dart2JsDeclarationMirror extends Dart2JsMirror |
314 implements DeclarationMirror { | 290 implements DeclarationMirror { |
(...skipping 1419 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1734 return new Future.immediate( | 1710 return new Future.immediate( |
1735 new Dart2JsStringConstantMirror.fromString(mirrors, text)); | 1711 new Dart2JsStringConstantMirror.fromString(mirrors, text)); |
1736 } else if (fieldName == 'trimmedText') { | 1712 } else if (fieldName == 'trimmedText') { |
1737 return new Future.immediate( | 1713 return new Future.immediate( |
1738 new Dart2JsStringConstantMirror.fromString(mirrors, trimmedText)); | 1714 new Dart2JsStringConstantMirror.fromString(mirrors, trimmedText)); |
1739 } | 1715 } |
1740 // TODO(johnniwinther): Which exception/error should be thrown here? | 1716 // TODO(johnniwinther): Which exception/error should be thrown here? |
1741 throw new UnsupportedError('InstanceMirror does not have a reflectee'); | 1717 throw new UnsupportedError('InstanceMirror does not have a reflectee'); |
1742 } | 1718 } |
1743 } | 1719 } |
OLD | NEW |