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:io'; | 7 import 'dart:io'; |
8 import 'dart:uri'; | 8 import 'dart:uri'; |
9 | 9 |
10 import '../../compiler.dart' as diagnostics; | 10 import '../../compiler.dart' as diagnostics; |
11 import '../elements/elements.dart'; | 11 import '../elements/elements.dart'; |
12 import '../resolution/resolution.dart' show ResolverTask, ResolverVisitor; | 12 import '../resolution/resolution.dart' show ResolverTask, ResolverVisitor; |
13 import '../apiimpl.dart' as api; | 13 import '../apiimpl.dart' as api; |
14 import '../scanner/scannerlib.dart'; | 14 import '../scanner/scannerlib.dart' hide SourceString; |
15 import '../ssa/ssa.dart'; | 15 import '../ssa/ssa.dart'; |
16 import '../dart2jslib.dart'; | 16 import '../dart2jslib.dart'; |
17 import '../filenames.dart'; | 17 import '../filenames.dart'; |
18 import '../source_file.dart'; | 18 import '../source_file.dart'; |
19 import '../tree/tree.dart'; | 19 import '../tree/tree.dart'; |
20 import '../util/util.dart'; | 20 import '../util/util.dart'; |
21 import '../util/uri_extras.dart'; | 21 import '../util/uri_extras.dart'; |
22 import '../dart2js.dart'; | 22 import '../dart2js.dart'; |
23 import '../util/characters.dart'; | 23 import '../util/characters.dart'; |
24 | 24 |
(...skipping 359 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
384 [Path packageRoot, List<String> opts = const <String>[]]) | 384 [Path packageRoot, List<String> opts = const <String>[]]) |
385 : cwd = getCurrentDirectory(), sourceFiles = <String, SourceFile>{} { | 385 : cwd = getCurrentDirectory(), sourceFiles = <String, SourceFile>{} { |
386 var libraryUri = cwd.resolve(libraryRoot.toString()); | 386 var libraryUri = cwd.resolve(libraryRoot.toString()); |
387 var packageUri; | 387 var packageUri; |
388 if (packageRoot != null) { | 388 if (packageRoot != null) { |
389 packageUri = cwd.resolve(packageRoot.toString()); | 389 packageUri = cwd.resolve(packageRoot.toString()); |
390 } else { | 390 } else { |
391 packageUri = libraryUri; | 391 packageUri = libraryUri; |
392 } | 392 } |
393 _compiler = new api.Compiler(provider, handler, | 393 _compiler = new api.Compiler(provider, handler, |
394 libraryUri, packageUri, <String>[]); | 394 libraryUri, packageUri, opts); |
395 var scriptUri = cwd.resolve(script.toString()); | 395 var scriptUri = cwd.resolve(script.toString()); |
396 // TODO(johnniwinther): Detect file not found | 396 // TODO(johnniwinther): Detect file not found |
397 _compiler.run(scriptUri); | 397 _compiler.run(scriptUri); |
398 } | 398 } |
399 | 399 |
400 Dart2JsCompilation.library(List<Path> libraries, Path libraryRoot, | 400 Dart2JsCompilation.library(List<Path> libraries, Path libraryRoot, |
401 [Path packageRoot, List<String> opts = const <String>[]]) | 401 [Path packageRoot, List<String> opts = const <String>[]]) |
402 : cwd = getCurrentDirectory(), sourceFiles = <String, SourceFile>{} { | 402 : cwd = getCurrentDirectory(), sourceFiles = <String, SourceFile>{} { |
403 var libraryUri = cwd.resolve(libraryRoot.toString()); | 403 var libraryUri = cwd.resolve(libraryRoot.toString()); |
404 var packageUri; | 404 var packageUri; |
405 if (packageRoot != null) { | 405 if (packageRoot != null) { |
406 packageUri = cwd.resolve(packageRoot.toString()); | 406 packageUri = cwd.resolve(packageRoot.toString()); |
407 } else { | 407 } else { |
408 packageUri = libraryUri; | 408 packageUri = libraryUri; |
409 } | 409 } |
410 _compiler = new LibraryCompiler(provider, handler, | 410 _compiler = new LibraryCompiler(provider, handler, |
411 libraryUri, packageUri, <String>[]); | 411 libraryUri, packageUri, opts); |
412 var librariesUri = <Uri>[]; | 412 var librariesUri = <Uri>[]; |
413 for (Path library in libraries) { | 413 for (Path library in libraries) { |
414 librariesUri.add(cwd.resolve(library.toString())); | 414 librariesUri.add(cwd.resolve(library.toString())); |
415 // TODO(johnniwinther): Detect file not found | 415 // TODO(johnniwinther): Detect file not found |
416 } | 416 } |
417 LibraryCompiler libraryCompiler = _compiler; | 417 LibraryCompiler libraryCompiler = _compiler; |
418 libraryCompiler.runList(librariesUri); | 418 libraryCompiler.runList(librariesUri); |
419 } | 419 } |
420 | 420 |
421 MirrorSystem get mirrors => new Dart2JsMirrorSystem(_compiler); | 421 MirrorSystem get mirrors => new Dart2JsMirrorSystem(_compiler); |
(...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
527 } | 527 } |
528 return node.getBeginToken(); | 528 return node.getBeginToken(); |
529 } | 529 } |
530 | 530 |
531 Script getScript() => _element.getCompilationUnit().script; | 531 Script getScript() => _element.getCompilationUnit().script; |
532 | 532 |
533 String toString() => _element.toString(); | 533 String toString() => _element.toString(); |
534 | 534 |
535 int get hashCode => qualifiedName.hashCode; | 535 int get hashCode => qualifiedName.hashCode; |
536 | 536 |
537 void _appendCommentTokens(Token commentToken) { | |
538 while (commentToken != null && commentToken.kind == COMMENT_TOKEN) { | |
539 _metadata.add(new Dart2JsCommentInstanceMirror( | |
540 mirrors, commentToken.slowToString())); | |
541 commentToken = commentToken.next; | |
542 } | |
543 } | |
544 | |
537 List<InstanceMirror> get metadata { | 545 List<InstanceMirror> get metadata { |
538 if (_metadata == null) { | 546 if (_metadata == null) { |
539 _metadata = <InstanceMirror>[]; | 547 _metadata = <InstanceMirror>[]; |
540 for (MetadataAnnotation metadata in _element.metadata) { | 548 for (MetadataAnnotation metadata in _element.metadata) { |
549 _appendCommentTokens(mirrors.compiler.commentMap[metadata.beginToken]); | |
ahe
2013/01/03 13:04:09
I think this has horrible performance.
| |
541 metadata.ensureResolved(mirrors.compiler); | 550 metadata.ensureResolved(mirrors.compiler); |
542 _metadata.add( | 551 _metadata.add( |
543 _convertConstantToInstanceMirror(mirrors, metadata.value)); | 552 _convertConstantToInstanceMirror(mirrors, metadata.value)); |
544 } | 553 } |
554 _appendCommentTokens(mirrors.compiler.commentMap[getBeginToken()]); | |
ahe
2013/01/03 13:04:09
Ditto.
| |
545 } | 555 } |
546 // TODO(johnniwinther): Return an unmodifiable list instead. | 556 // TODO(johnniwinther): Return an unmodifiable list instead. |
547 return new List<InstanceMirror>.from(_metadata); | 557 return new List<InstanceMirror>.from(_metadata); |
548 } | 558 } |
549 } | 559 } |
550 | 560 |
551 //------------------------------------------------------------------------------ | 561 //------------------------------------------------------------------------------ |
552 // Mirror system implementation. | 562 // Mirror system implementation. |
553 //------------------------------------------------------------------------------ | 563 //------------------------------------------------------------------------------ |
554 | 564 |
(...skipping 1214 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1769 | 1779 |
1770 Future<InstanceMirror> getField(String fieldName) { | 1780 Future<InstanceMirror> getField(String fieldName) { |
1771 _ensureFieldMap(); | 1781 _ensureFieldMap(); |
1772 Constant fieldConstant = _fieldMap[fieldName]; | 1782 Constant fieldConstant = _fieldMap[fieldName]; |
1773 if (fieldConstant != null) { | 1783 if (fieldConstant != null) { |
1774 return new Future<InstanceMirror>.immediate( | 1784 return new Future<InstanceMirror>.immediate( |
1775 _convertConstantToInstanceMirror(mirrors, fieldConstant)); | 1785 _convertConstantToInstanceMirror(mirrors, fieldConstant)); |
1776 } | 1786 } |
1777 return super.getField(fieldName); | 1787 return super.getField(fieldName); |
1778 } | 1788 } |
1789 } | |
1790 | |
1791 | |
1792 /** | |
1793 * Pulls the raw text out of a doc comment (i.e. removes the comment | |
1794 * characters). | |
1795 */ | |
1796 String _stripComment(String comment) { | |
Andrei Mouravski
2013/01/02 16:56:06
Why not make this public or even put it into a uti
| |
1797 if (comment.startsWith('///')) { | |
1798 var line = comment.substring(3); | |
1799 // Allow a leading space. | |
ahe
2013/01/03 13:04:09
allow -> strip.
| |
1800 if (line.startsWith(' ')) line = line.substring(1); | |
1801 return line; | |
1802 } else { | |
ahe
2013/01/03 13:04:09
I suggest you handle this case differently, per my
| |
1803 StringBuffer buf = new StringBuffer(); | |
1804 for (var line in comment.split('\n')) { | |
1805 line = line.trim(); | |
ahe
2013/01/03 13:04:09
This deletes trailing whitespace, unlike how you h
| |
1806 if (line.startsWith('/**')) line = line.substring(3); | |
ahe
2013/01/03 13:04:09
You don't need to trim the line to test for this.
| |
1807 if (line.endsWith('*/')) line = line.substring(0, line.length - 2); | |
ahe
2013/01/03 13:04:09
You don't need to trim the line to test for this.
| |
1808 line = line.trim(); | |
ahe
2013/01/03 13:04:09
I don't understand why you're trimming again.
| |
1809 if (line.startsWith('* ')) { | |
ahe
2013/01/03 13:04:09
This has weird behavior for comments like this:
/
| |
1810 line = line.substring(2); | |
1811 } else if (line.startsWith('*')) { | |
1812 line = line.substring(1); | |
1813 } | |
1814 | |
1815 buf.add(line); | |
1816 buf.add('\n'); | |
1817 } | |
1818 return buf.toString(); | |
1819 } | |
1820 } | |
1821 | |
1822 class Dart2JsCommentInstanceMirror implements CommentInstanceMirror { | |
1823 final Dart2JsMirrorSystem mirrors; | |
1824 final String text; | |
1825 String _trimmedText; | |
1826 | |
1827 Dart2JsCommentInstanceMirror(this.mirrors, this.text); | |
1828 | |
1829 ClassMirror get type { | |
1830 var uri = new Uri.fromComponents(scheme: 'dart', path: 'mirrors'); | |
1831 LibraryElement libraryElement = | |
1832 mirrors.compiler.libraryLoader.loadLibrary(uri, null, uri); | |
ahe
2013/01/03 13:04:09
This seems overly lazy. Can you initialize the obj
Johnni Winther
2013/01/04 16:40:53
The class element is now loaded in [Compiler.scanB
| |
1833 var element = libraryElement.find(const SourceString('Comment')); | |
1834 return new Dart2JsClassMirror(mirrors, element); | |
1835 } | |
1836 | |
1837 bool get isDocComment => text.startsWith('/**') || text.startsWith('///'); | |
1838 | |
1839 String get trimmedText { | |
1840 if (_trimmedText == null) { | |
1841 _trimmedText = _stripComment(text); | |
1842 } | |
1843 return _trimmedText; | |
1844 } | |
1845 | |
1846 bool get hasReflectee => false; | |
1847 | |
1848 get reflectee { | |
1849 // TODO(johnniwinther): Which exception/error should be thrown here? | |
1850 throw new UnsupportedError('InstanceMirror does not have a reflectee'); | |
1851 } | |
1852 | |
1853 Future<InstanceMirror> getField(String fieldName) { | |
1854 if (fieldName == 'isDocComment') { | |
1855 return new Future.immediate( | |
1856 new Dart2JsBoolConstantMirror(mirrors, | |
1857 isDocComment ? new TrueConstant() : new FalseConstant())); | |
1858 } else if (fieldName == 'text') { | |
1859 return new Future.immediate( | |
1860 new Dart2JsStringConstantMirror(mirrors, | |
1861 new StringConstant(new DartString.literal(text), null))); | |
1862 } else if (fieldName == 'trimmedText') { | |
1863 return new Future.immediate( | |
1864 new Dart2JsStringConstantMirror(mirrors, | |
1865 new StringConstant(new DartString.literal(trimmedText), null))); | |
ahe
2013/01/03 13:04:09
You might benefit from a new constructor on Dart2J
Johnni Winther
2013/01/04 16:40:53
Done.
| |
1866 } | |
1867 // TODO(johnniwinther): Which exception/error should be thrown here? | |
1868 throw new UnsupportedError('InstanceMirror does not have a reflectee'); | |
1869 } | |
1779 } | 1870 } |
OLD | NEW |