From f4f3958af13fd94d0d1cb67bd75f59bd2713af4a Mon Sep 17 00:00:00 2001 From: "johnniwinther@google.com" Date: Mon, 22 Apr 2013 13:01:20 +0000 Subject: [PATCH] Change MirrorSystem.libraries to Map BUG= Review URL: https://codereview.chromium.org//13797002 git-svn-id: https://dart.googlecode.com/svn/branches/bleeding_edge/dart@21812 260f80e4-7a28-3924-810f-c04153c831b5 --- .../compiler/implementation/dart2js.dart | 23 ++++++------ .../compiler/implementation/filenames.dart | 7 ++-- .../mirrors/dart2js_mirror.dart | 26 ++++++------- .../implementation/mirrors/mirrors.dart | 12 +++++- .../implementation/source_file_provider.dart | 6 +-- tests/compiler/dart2js/analyze_api_test.dart | 3 +- tests/compiler/dart2js/bad_loop_test.dart | 3 +- ...deferred_load_graph_segmentation_test.dart | 3 +- .../dart2js/memory_source_file_helper.dart | 2 +- .../dart2js/mirrors_metadata_test.dart | 9 +++-- tests/compiler/dart2js/mirrors_test.dart | 11 ++++-- utils/apidoc/apidoc.dart | 9 +++-- utils/apidoc/html_diff.dart | 37 +++++++++---------- 13 files changed, 79 insertions(+), 72 deletions(-) diff --git a/sdk/lib/_internal/compiler/implementation/dart2js.dart b/sdk/lib/_internal/compiler/implementation/dart2js.dart index 4031bebc497..61660e55fb3 100644 --- a/sdk/lib/_internal/compiler/implementation/dart2js.dart +++ b/sdk/lib/_internal/compiler/implementation/dart2js.dart @@ -80,10 +80,9 @@ void parseCommandLine(List handlers, List argv) { void compile(List argv) { bool isWindows = (Platform.operatingSystem == 'windows'); - Uri cwd = getCurrentDirectory(); - Uri libraryRoot = cwd; - Uri out = cwd.resolve('out.js'); - Uri sourceMapOut = cwd.resolve('out.js.map'); + Uri libraryRoot = currentDirectory; + Uri out = currentDirectory.resolve('out.js'); + Uri sourceMapOut = currentDirectory.resolve('out.js.map'); Uri packageRoot = null; List options = new List(); bool explicitOut = false; @@ -102,16 +101,16 @@ void compile(List argv) { } setLibraryRoot(String argument) { - libraryRoot = cwd.resolve(extractPath(argument)); + libraryRoot = currentDirectory.resolve(extractPath(argument)); } setPackageRoot(String argument) { - packageRoot = cwd.resolve(extractPath(argument)); + packageRoot = currentDirectory.resolve(extractPath(argument)); } setOutput(String argument) { explicitOut = true; - out = cwd.resolve(nativeToUriPath(extractParameter(argument))); + out = currentDirectory.resolve(nativeToUriPath(extractParameter(argument))); sourceMapOut = Uri.parse('$out.map'); } @@ -119,8 +118,8 @@ void compile(List argv) { if (argument == '--output-type=dart') { outputLanguage = OUTPUT_LANGUAGE_DART; if (!explicitOut) { - out = cwd.resolve('out.dart'); - sourceMapOut = cwd.resolve('out.dart.map'); + out = currentDirectory.resolve('out.dart'); + sourceMapOut = currentDirectory.resolve('out.dart.map'); } } passThrough(argument); @@ -254,7 +253,7 @@ void compile(List argv) { diagnosticHandler.diagnosticHandler(uri, begin, end, message, kind); } - Uri uri = cwd.resolve(arguments[0]); + Uri uri = currentDirectory.resolve(arguments[0]); if (packageRoot == null) { packageRoot = uri.resolve('./packages/'); } @@ -273,10 +272,10 @@ void compile(List argv) { diagnosticHandler.info( 'compiled ${inputProvider.dartCharactersRead} characters Dart ' '-> $charactersWritten characters $outputLanguage ' - 'in ${relativize(cwd, out, isWindows)}'); + 'in ${relativize(currentDirectory, out, isWindows)}'); if (!explicitOut) { String input = uriPathToNative(arguments[0]); - String output = relativize(cwd, out, isWindows); + String output = relativize(currentDirectory, out, isWindows); print('Dart file $input compiled to $outputLanguage: $output'); } } diff --git a/sdk/lib/_internal/compiler/implementation/filenames.dart b/sdk/lib/_internal/compiler/implementation/filenames.dart index 198a28f3882..46bc421a1cc 100644 --- a/sdk/lib/_internal/compiler/implementation/filenames.dart +++ b/sdk/lib/_internal/compiler/implementation/filenames.dart @@ -21,9 +21,8 @@ String uriPathToNative(String path) { return new Path(path).toNativePath(); } -Uri getCurrentDirectory() { - final String dir = nativeToUriPath(new File('.').fullPathSync()); - return new Uri.fromComponents(scheme: 'file', path: appendSlash(dir)); -} +final Uri currentDirectory = new Uri.fromComponents( + scheme: 'file', + path: appendSlash(nativeToUriPath(new File('.').fullPathSync()))); String appendSlash(String path) => path.endsWith('/') ? path : '$path/'; diff --git a/sdk/lib/_internal/compiler/implementation/mirrors/dart2js_mirror.dart b/sdk/lib/_internal/compiler/implementation/mirrors/dart2js_mirror.dart index 17f40f68243..f3dce015cc5 100644 --- a/sdk/lib/_internal/compiler/implementation/mirrors/dart2js_mirror.dart +++ b/sdk/lib/_internal/compiler/implementation/mirrors/dart2js_mirror.dart @@ -209,17 +209,16 @@ Future compile(Path script, {Path packageRoot, List options: const [], api.DiagnosticHandler diagnosticHandler}) { - Uri cwd = getCurrentDirectory(); SourceFileProvider provider = new SourceFileProvider(); if (diagnosticHandler == null) { diagnosticHandler = new FormattingDiagnosticHandler(provider).diagnosticHandler; } - Uri scriptUri = cwd.resolve(script.toString()); - Uri libraryUri = cwd.resolve(appendSlash('$libraryRoot')); + Uri scriptUri = currentDirectory.resolve(script.toString()); + Uri libraryUri = currentDirectory.resolve(appendSlash('$libraryRoot')); Uri packageUri = null; if (packageRoot != null) { - packageUri = cwd.resolve(appendSlash('$packageRoot')); + packageUri = currentDirectory.resolve(appendSlash('$packageRoot')); } return api.compile(scriptUri, libraryUri, packageUri, provider.readStringFromUri, diagnosticHandler, options); @@ -282,20 +281,19 @@ Future analyze(List libraries, {Path packageRoot, List options: const [], api.DiagnosticHandler diagnosticHandler}) { - Uri cwd = getCurrentDirectory(); SourceFileProvider provider = new SourceFileProvider(); if (diagnosticHandler == null) { diagnosticHandler = new FormattingDiagnosticHandler(provider).diagnosticHandler; } - Uri libraryUri = cwd.resolve(appendSlash('$libraryRoot')); + Uri libraryUri = currentDirectory.resolve(appendSlash('$libraryRoot')); Uri packageUri = null; if (packageRoot != null) { - packageUri = cwd.resolve(appendSlash('$packageRoot')); + packageUri = currentDirectory.resolve(appendSlash('$packageRoot')); } List librariesUri = []; for (Path library in libraries) { - librariesUri.add(cwd.resolve(library.toString())); + librariesUri.add(currentDirectory.resolve(library.toString())); } return analyzeUri(librariesUri, libraryUri, packageUri, provider.readStringFromUri, diagnosticHandler, options); @@ -465,9 +463,9 @@ abstract class Dart2JsMemberMirror extends Dart2JsElementMirror // Mirror system implementation. //------------------------------------------------------------------------------ -class Dart2JsMirrorSystem implements MirrorSystem { +class Dart2JsMirrorSystem extends MirrorSystem { final Compiler compiler; - Map _libraries; + Map _libraries; Map _libraryMap; Dart2JsMirrorSystem(this.compiler) @@ -475,18 +473,18 @@ class Dart2JsMirrorSystem implements MirrorSystem { void _ensureLibraries() { if (_libraries == null) { - _libraries = {}; + _libraries = new Map(); compiler.libraries.forEach((_, LibraryElement v) { var mirror = new Dart2JsLibraryMirror(mirrors, v); - _libraries[mirror.simpleName] = mirror; + _libraries[mirror.uri] = mirror; _libraryMap[v] = mirror; }); } } - Map get libraries { + Map get libraries { _ensureLibraries(); - return new ImmutableMapWrapper(_libraries); + return new ImmutableMapWrapper(_libraries); } Dart2JsLibraryMirror _getLibrary(LibraryElement element) => diff --git a/sdk/lib/_internal/compiler/implementation/mirrors/mirrors.dart b/sdk/lib/_internal/compiler/implementation/mirrors/mirrors.dart index dd8c70fdbaa..de1cedad5dd 100644 --- a/sdk/lib/_internal/compiler/implementation/mirrors/mirrors.dart +++ b/sdk/lib/_internal/compiler/implementation/mirrors/mirrors.dart @@ -17,8 +17,16 @@ abstract class MirrorSystem { /** * Returns an unmodifiable map of all libraries in this mirror system. */ - // TODO(johnniwinther): Change to Map. - Map get libraries; + Map get libraries; + + /** + * Returns an iterable of all libraries in the mirror system whose library + * name is [libraryName]. + */ + Iterable findLibrary(String libraryName) { + return libraries.values.where( + (library) => library.simpleName == libraryName); + } /** * A mirror on the [:dynamic:] type. diff --git a/sdk/lib/_internal/compiler/implementation/source_file_provider.dart b/sdk/lib/_internal/compiler/implementation/source_file_provider.dart index b75ba4ada2f..70659e075b3 100644 --- a/sdk/lib/_internal/compiler/implementation/source_file_provider.dart +++ b/sdk/lib/_internal/compiler/implementation/source_file_provider.dart @@ -27,7 +27,7 @@ String readAll(String filename) { class SourceFileProvider { bool isWindows = (Platform.operatingSystem == 'windows'); - Uri cwd = getCurrentDirectory(); + Uri cwd = currentDirectory; Map sourceFiles = {}; int dartCharactersRead = 0; @@ -43,8 +43,8 @@ class SourceFileProvider { '(${ex.osError}).'; } dartCharactersRead += source.length; - sourceFiles[resourceUri.toString()] = - new SourceFile(relativize(cwd, resourceUri, isWindows), source); + sourceFiles[resourceUri.toString()] = new SourceFile( + relativize(cwd, resourceUri, isWindows), source); return new Future.value(source); } } diff --git a/tests/compiler/dart2js/analyze_api_test.dart b/tests/compiler/dart2js/analyze_api_test.dart index 3c9201206ba..a70de8bba27 100644 --- a/tests/compiler/dart2js/analyze_api_test.dart +++ b/tests/compiler/dart2js/analyze_api_test.dart @@ -107,8 +107,7 @@ class CollectingDiagnosticHandler extends FormattingDiagnosticHandler { } void main() { - Uri currentWorkingDirectory = getCurrentDirectory(); - var libraryRoot = currentWorkingDirectory.resolve('sdk/'); + var libraryRoot = currentDirectory.resolve('sdk/'); var uriList = new List(); LIBRARIES.forEach((String name, LibraryInfo info) { if (info.documented) { diff --git a/tests/compiler/dart2js/bad_loop_test.dart b/tests/compiler/dart2js/bad_loop_test.dart index aedbfc03f88..57050953553 100644 --- a/tests/compiler/dart2js/bad_loop_test.dart +++ b/tests/compiler/dart2js/bad_loop_test.dart @@ -9,8 +9,7 @@ import '../../../sdk/lib/_internal/compiler/compiler.dart' show Diagnostic; main() { - Uri cwd = getCurrentDirectory(); - Uri script = cwd.resolve(nativeToUriPath(new Options().script)); + Uri script = currentDirectory.resolve(nativeToUriPath(new Options().script)); Uri libraryRoot = script.resolve('../../../sdk/'); Uri packageRoot = script.resolve('./packages/'); diff --git a/tests/compiler/dart2js/deferred_load_graph_segmentation_test.dart b/tests/compiler/dart2js/deferred_load_graph_segmentation_test.dart index 7c4c551de0d..b05ead822dc 100644 --- a/tests/compiler/dart2js/deferred_load_graph_segmentation_test.dart +++ b/tests/compiler/dart2js/deferred_load_graph_segmentation_test.dart @@ -13,8 +13,7 @@ import '../../../sdk/lib/_internal/compiler/implementation/dart2jslib.dart' as dart2js; void main() { - Uri cwd = getCurrentDirectory(); - Uri script = cwd.resolve(nativeToUriPath(new Options().script)); + Uri script = currentDirectory.resolve(nativeToUriPath(new Options().script)); Uri libraryRoot = script.resolve('../../../sdk/'); Uri packageRoot = script.resolve('./packages/'); diff --git a/tests/compiler/dart2js/memory_source_file_helper.dart b/tests/compiler/dart2js/memory_source_file_helper.dart index 26a33b0b906..db1c4f0c77e 100644 --- a/tests/compiler/dart2js/memory_source_file_helper.dart +++ b/tests/compiler/dart2js/memory_source_file_helper.dart @@ -14,7 +14,7 @@ export '../../../sdk/lib/_internal/compiler/implementation/apiimpl.dart' show Compiler; export '../../../sdk/lib/_internal/compiler/implementation/filenames.dart' - show getCurrentDirectory, nativeToUriPath; + show currentDirectory, nativeToUriPath; import '../../../sdk/lib/_internal/compiler/implementation/source_file.dart' show SourceFile; diff --git a/tests/compiler/dart2js/mirrors_metadata_test.dart b/tests/compiler/dart2js/mirrors_metadata_test.dart index 5b4294768bf..d853ac14738 100644 --- a/tests/compiler/dart2js/mirrors_metadata_test.dart +++ b/tests/compiler/dart2js/mirrors_metadata_test.dart @@ -6,6 +6,7 @@ import 'package:expect/expect.dart'; import 'dart:async'; import 'dart:io'; import 'dart:uri'; +import 'package:expect/expect.dart'; import '../../../sdk/lib/_internal/compiler/implementation/filenames.dart'; import '../../../sdk/lib/_internal/compiler/implementation/mirrors/mirrors.dart'; import '../../../sdk/lib/_internal/compiler/implementation/mirrors/dart2js_mirror.dart'; @@ -13,15 +14,15 @@ import '../../../sdk/lib/_internal/compiler/implementation/source_file_provider. import 'mock_compiler.dart'; const String SOURCE = 'source'; +const Uri SOURCE_URI = const Uri.fromComponents(scheme: SOURCE, path: SOURCE); MirrorSystem createMirrorSystem(String source) { - Uri sourceUri = new Uri.fromComponents(scheme: SOURCE, path: SOURCE); MockCompiler compiler = new MockCompiler( analyzeOnly: true, analyzeAll: true, preserveComments: true); - compiler.registerSource(sourceUri, source); - compiler.librariesToAnalyzeWhenRun = [sourceUri]; + compiler.registerSource(SOURCE_URI, source); + compiler.librariesToAnalyzeWhenRun = [SOURCE_URI]; compiler.runCompiler(null); return new Dart2JsMirrorSystem(compiler); } @@ -32,7 +33,7 @@ void validateDeclarationComment(String code, bool isDocComment, List declarationNames) { MirrorSystem mirrors = createMirrorSystem(code); - LibraryMirror library = mirrors.libraries[SOURCE]; + LibraryMirror library = mirrors.libraries[SOURCE_URI]; Expect.isNotNull(library); for (String declarationName in declarationNames) { DeclarationMirror declaration = library.members[declarationName]; diff --git a/tests/compiler/dart2js/mirrors_test.dart b/tests/compiler/dart2js/mirrors_test.dart index 2c056baf3dd..6726116581f 100644 --- a/tests/compiler/dart2js/mirrors_test.dart +++ b/tests/compiler/dart2js/mirrors_test.dart @@ -8,6 +8,10 @@ import '../../../sdk/lib/_internal/compiler/implementation/mirrors/mirrors_util. import '../../../sdk/lib/_internal/compiler/implementation/mirrors/dart2js_mirror.dart'; import 'dart:io'; +import 'dart:uri'; + +const Uri DART_MIRRORS_URI = + const Uri.fromComponents(scheme: 'dart', path: 'mirrors'); int count(Iterable iterable) { var count = 0; @@ -26,7 +30,7 @@ bool containsType(TypeMirror expected, Iterable iterable) { return false; } -DeclarationMirror findMirror(List list, String name) { +DeclarationMirror findMirror(Iterable list, String name) { for (DeclarationMirror mirror in list) { if (mirror.simpleName == name) { return mirror; @@ -54,12 +58,13 @@ void test(MirrorSystem mirrors) { Expect.isNotNull(libraries, "No libraries map returned"); Expect.isFalse(libraries.isEmpty, "Empty libraries map returned"); - var helperLibrary = libraries["mirrors_helper"]; + var helperLibrary = findMirror(libraries.values, "mirrors_helper"); Expect.isNotNull(helperLibrary, "Library 'mirrors_helper' not found"); Expect.stringEquals("mirrors_helper", helperLibrary.simpleName, "Unexpected library simple name"); Expect.stringEquals("mirrors_helper", helperLibrary.qualifiedName, "Unexpected library qualified name"); + Expect.equals(helperLibrary, mirrors.findLibrary('mirrors_helper').single); var helperLibraryLocation = helperLibrary.location; Expect.isNotNull(helperLibraryLocation); @@ -181,7 +186,7 @@ void testFoo(MirrorSystem system, LibraryMirror helperLibrary, var metadataListIndex = 0; var metadata; - var dartMirrorsLibrary = system.libraries['dart.mirrors']; + var dartMirrorsLibrary = system.libraries[DART_MIRRORS_URI]; Expect.isNotNull(dartMirrorsLibrary); var commentType = dartMirrorsLibrary.classes['Comment']; Expect.isNotNull(commentType); diff --git a/utils/apidoc/apidoc.dart b/utils/apidoc/apidoc.dart index 233f9be67e8..8979ddd9e22 100644 --- a/utils/apidoc/apidoc.dart +++ b/utils/apidoc/apidoc.dart @@ -24,6 +24,7 @@ import 'html_diff.dart'; // TODO(rnystrom): Use "package:" URL (#4968). import '../../sdk/lib/_internal/compiler/implementation/mirrors/mirrors.dart'; import '../../sdk/lib/_internal/compiler/implementation/mirrors/mirrors_util.dart'; +import '../../sdk/lib/_internal/compiler/implementation/filenames.dart'; import '../../sdk/lib/_internal/dartdoc/lib/dartdoc.dart'; import '../../sdk/lib/_internal/libraries.dart'; import 'package:pathos/path.dart' as pathos; @@ -101,7 +102,7 @@ void main() { // TODO(amouravski): move HtmlDiff inside of the future chain below to re-use // the MirrorSystem already analyzed. _diff = new HtmlDiff(printWarnings:false); - Future htmlDiff = _diff.run(libPath); + Future htmlDiff = _diff.run(currentDirectory.resolve(libPath.toString())); // TODO(johnniwinther): Libraries for the compilation seem to be more like // URIs. Perhaps Path should have a toURI() method. @@ -353,7 +354,7 @@ class Apidoc extends Dartdoc { } var typeString = ''; - if (HTML_LIBRARY_NAMES.contains(displayName(type.library))) { + if (HTML_LIBRARY_URIS.contains(type.library.uri)) { // If it's an HTML type, try to map it to a base DOM type so we can find // the MDN docs. final domTypes = _diff.htmlTypesToDom[type.qualifiedName]; @@ -390,7 +391,7 @@ class Apidoc extends Dartdoc { MdnComment includeMdnMemberComment(MemberMirror member) { var library = findLibrary(member); var memberString = ''; - if (HTML_LIBRARY_NAMES.contains(displayName(library))) { + if (HTML_LIBRARY_URIS.contains(library.uri)) { // If it's an HTML type, try to map it to a DOM type name so we can find // the MDN docs. final domMembers = _diff.htmlToDom[member.qualifiedName]; @@ -443,7 +444,7 @@ class Apidoc extends Dartdoc { String _linkMember(MemberMirror member) { final typeName = member.owner.simpleName; var memberName = '$typeName.${member.simpleName}'; - if (member is MethodMirror && (member.isConstructor || member.isFactory)) { + if (member is MethodMirror && member.isConstructor) { final separator = member.constructorName == '' ? '' : '.'; memberName = 'new $typeName$separator${member.constructorName}'; } diff --git a/utils/apidoc/html_diff.dart b/utils/apidoc/html_diff.dart index 9c3d2eef603..ef6096c22f0 100644 --- a/utils/apidoc/html_diff.dart +++ b/utils/apidoc/html_diff.dart @@ -10,6 +10,7 @@ library html_diff; import 'dart:async'; import 'dart:io'; +import 'dart:uri'; import 'lib/metadata.dart'; @@ -17,22 +18,18 @@ import 'lib/metadata.dart'; import '../../sdk/lib/_internal/compiler/implementation/mirrors/dart2js_mirror.dart'; import '../../sdk/lib/_internal/compiler/implementation/mirrors/mirrors.dart'; import '../../sdk/lib/_internal/compiler/implementation/mirrors/mirrors_util.dart'; +import '../../sdk/lib/_internal/compiler/implementation/source_file_provider.dart'; import '../../sdk/lib/_internal/dartdoc/lib/dartdoc.dart'; import '../../sdk/lib/html/html_common/metadata.dart'; // TODO(amouravski): There is currently magic that looks at dart:* libraries // rather than the declared library names. This changed due to recent syntax // changes. We should only need to look at the library 'html'. -const List HTML_LIBRARY_NAMES = const [ - 'dart:html', - 'dart:indexed_db', - 'dart:svg', - 'dart:web_audio']; -const List HTML_DECLARED_NAMES = const [ - 'dart.dom.html', - 'dart.dom.indexed_db', - 'dart.dom.svg', - 'dart.dom.web_audio']; +const List HTML_LIBRARY_URIS = const [ + const Uri.fromComponents(scheme: 'dart', path: 'html'), + const Uri.fromComponents(scheme: 'dart', path: 'indexed_db'), + const Uri.fromComponents(scheme: 'dart', path: 'svg'), + const Uri.fromComponents(scheme: 'dart', path: 'web_audio')]; /** * A class for computing a many-to-many mapping between the types and @@ -92,17 +89,19 @@ class HtmlDiff { * should be initialized (via [parseOptions] and [initializeWorld]) and * [HtmlDiff.initialize] should be called. */ - Future run(Path libDir) { + Future run(Uri libraryRoot) { var result = new Completer(); - var paths = []; - for (var libraryName in HTML_LIBRARY_NAMES) { - paths.add(new Path(libraryName)); - } - analyze(paths, libDir).then((MirrorSystem mirrors) { - for (var libraryName in HTML_DECLARED_NAMES) { - var library = mirrors.libraries[libraryName]; + var provider = new SourceFileProvider(); + var handler = new FormattingDiagnosticHandler(provider); + Future analysis = analyzeUri( + HTML_LIBRARY_URIS, libraryRoot, null, + provider.readStringFromUri, + handler.diagnosticHandler); + analysis.then((MirrorSystem mirrors) { + for (var libraryUri in HTML_LIBRARY_URIS) { + var library = mirrors.libraries[libraryUri]; if (library == null) { - warn('Could not find $libraryName'); + warn('Could not find $libraryUri'); result.complete(false); } for (ClassMirror type in library.classes.values) {