Chromium Code Reviews| 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 /** | 5 /** |
| 6 * This library contains the infrastructure to parse and integrate patch files. | 6 * This library contains the infrastructure to parse and integrate patch files. |
| 7 * | 7 * |
| 8 * Three types of elements can be patched: [LibraryElement], [ClassElement], | 8 * Three types of elements can be patched: [LibraryElement], [ClassElement], |
| 9 * [FunctionElement]. Patches are introduced in patch libraries which are loaded | 9 * [FunctionElement]. Patches are introduced in patch libraries which are loaded |
| 10 * together with the corresponding origin library. Which libraries that are | 10 * together with the corresponding origin library. Which libraries that are |
| (...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 107 * TODO(johnniwinther): Simplify this invariant to use only declarations in | 107 * TODO(johnniwinther): Simplify this invariant to use only declarations in |
| 108 * [tree.TreeElements]. | 108 * [tree.TreeElements]. |
| 109 * - Builders shift between declaration and implementation depending on usages. | 109 * - Builders shift between declaration and implementation depending on usages. |
| 110 * - Compile-time constants use constructor implementation exclusively. | 110 * - Compile-time constants use constructor implementation exclusively. |
| 111 * - Work on function parameters is performed on the declaration of the function | 111 * - Work on function parameters is performed on the declaration of the function |
| 112 * element. | 112 * element. |
| 113 */ | 113 */ |
| 114 | 114 |
| 115 library patchparser; | 115 library patchparser; |
| 116 | 116 |
| 117 import 'dart:async'; | |
| 118 | |
| 117 import "tree/tree.dart" as tree; | 119 import "tree/tree.dart" as tree; |
| 118 import "dart2jslib.dart" as leg; // CompilerTask, Compiler. | 120 import "dart2jslib.dart" as leg; // CompilerTask, Compiler. |
| 119 import "apiimpl.dart"; | 121 import "apiimpl.dart"; |
| 120 import "../compiler.dart" as api; | 122 import "../compiler.dart" as api; |
| 121 import "scanner/scannerlib.dart"; // Scanner, Parsers, Listeners | 123 import "scanner/scannerlib.dart"; // Scanner, Parsers, Listeners |
| 122 import "elements/elements.dart"; | 124 import "elements/elements.dart"; |
| 123 import "elements/modelx.dart" show LibraryElementX, MetadataAnnotationX; | 125 import "elements/modelx.dart" show LibraryElementX, MetadataAnnotationX; |
| 124 import 'util/util.dart'; | 126 import 'util/util.dart'; |
| 125 | 127 |
| 126 class PatchParserTask extends leg.CompilerTask { | 128 class PatchParserTask extends leg.CompilerTask { |
| 127 PatchParserTask(leg.Compiler compiler): super(compiler); | 129 PatchParserTask(leg.Compiler compiler): super(compiler); |
| 128 final String name = "Patching Parser"; | 130 final String name = "Patching Parser"; |
| 129 | 131 |
| 130 /** | 132 /** |
| 131 * Scans a library patch file, applies the method patches and | 133 * Scans a library patch file, applies the method patches and |
| 132 * injections to the library, and returns a list of class | 134 * injections to the library, and returns a list of class |
| 133 * patches. | 135 * patches. |
| 134 */ | 136 */ |
| 135 void patchLibrary(leg.LibraryDependencyHandler handler, | 137 Future patchLibrary(leg.LibraryDependencyHandler handler, |
| 136 Uri patchUri, LibraryElement originLibrary) { | 138 Uri patchUri, LibraryElement originLibrary) { |
| 137 | 139 return compiler.readScript(patchUri, null).then((script) { |
| 138 leg.Script script = compiler.readScript(patchUri, null); | 140 var patchLibrary = new LibraryElementX(script, null, originLibrary); |
| 139 var patchLibrary = new LibraryElementX(script, null, originLibrary); | 141 compiler.withCurrentElement(patchLibrary, () { |
| 140 compiler.withCurrentElement(patchLibrary, () { | 142 handler.registerNewLibrary(patchLibrary); |
| 141 handler.registerNewLibrary(patchLibrary); | 143 LinkBuilder<tree.LibraryTag> imports = new LinkBuilder<tree.LibraryTag>( ); |
|
Bob Nystrom
2013/06/26 01:03:24
I'll fix this.
| |
| 142 LinkBuilder<tree.LibraryTag> imports = new LinkBuilder<tree.LibraryTag>(); | 144 compiler.withCurrentElement(patchLibrary.entryCompilationUnit, () { |
| 143 compiler.withCurrentElement(patchLibrary.entryCompilationUnit, () { | 145 // This patches the elements of the patch library into [library]. |
| 144 // This patches the elements of the patch library into [library]. | 146 // Injected elements are added directly under the compilation unit. |
| 145 // Injected elements are added directly under the compilation unit. | 147 // Patch elements are stored on the patched functions or classes. |
| 146 // Patch elements are stored on the patched functions or classes. | 148 scanLibraryElements(patchLibrary.entryCompilationUnit, imports); |
| 147 scanLibraryElements(patchLibrary.entryCompilationUnit, imports); | 149 }); |
| 150 // After scanning declarations, we handle the import tags in the patch. | |
| 151 // TODO(lrn): These imports end up in the original library and are in | |
| 152 // scope for the original methods too. This should be fixed. | |
| 153 compiler.importHelperLibrary(originLibrary); | |
| 154 return Future.forEach(imports.toLink(), (tag) { | |
| 155 return compiler.withCurrentElement(patchLibrary, () { | |
| 156 return compiler.libraryLoader.registerLibraryFromTag( | |
| 157 handler, patchLibrary, tag); | |
| 158 }); | |
| 159 }); | |
| 148 }); | 160 }); |
| 149 // After scanning declarations, we handle the import tags in the patch. | |
| 150 // TODO(lrn): These imports end up in the original library and are in | |
| 151 // scope for the original methods too. This should be fixed. | |
| 152 compiler.importHelperLibrary(originLibrary); | |
| 153 for (tree.LibraryTag tag in imports.toLink()) { | |
| 154 compiler.libraryLoader.registerLibraryFromTag( | |
| 155 handler, patchLibrary, tag); | |
| 156 } | |
| 157 }); | 161 }); |
| 158 } | 162 } |
| 159 | 163 |
| 160 void scanLibraryElements( | 164 void scanLibraryElements( |
| 161 CompilationUnitElement compilationUnit, | 165 CompilationUnitElement compilationUnit, |
| 162 LinkBuilder<tree.LibraryTag> imports) { | 166 LinkBuilder<tree.LibraryTag> imports) { |
| 163 measure(() { | 167 measure(() { |
| 164 // TODO(lrn): Possibly recursively handle #source directives in patch. | 168 // TODO(lrn): Possibly recursively handle #source directives in patch. |
| 165 leg.Script script = compilationUnit.script; | 169 leg.Script script = compilationUnit.script; |
| 166 Token tokens = new StringScanner(script.text).tokenize(); | 170 Token tokens = new StringScanner(script.text).tokenize(); |
| (...skipping 419 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 586 | 590 |
| 587 // TODO(johnniwinther): Add unittest when patch is (real) metadata. | 591 // TODO(johnniwinther): Add unittest when patch is (real) metadata. |
| 588 bool isPatchElement(Element element) { | 592 bool isPatchElement(Element element) { |
| 589 // TODO(lrn): More checks needed if we introduce metadata for real. | 593 // TODO(lrn): More checks needed if we introduce metadata for real. |
| 590 // In that case, it must have the identifier "native" as metadata. | 594 // In that case, it must have the identifier "native" as metadata. |
| 591 for (Link link = element.metadata; !link.isEmpty; link = link.tail) { | 595 for (Link link = element.metadata; !link.isEmpty; link = link.tail) { |
| 592 if (link.head is PatchMetadataAnnotation) return true; | 596 if (link.head is PatchMetadataAnnotation) return true; |
| 593 } | 597 } |
| 594 return false; | 598 return false; |
| 595 } | 599 } |
| OLD | NEW |