| OLD | NEW |
| 1 #!/usr/bin/env dart | 1 #!/usr/bin/env dart |
| 2 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file | 2 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file |
| 3 // for details. All rights reserved. Use of this source code is governed by a | 3 // for details. All rights reserved. Use of this source code is governed by a |
| 4 // BSD-style license that can be found in the LICENSE file. | 4 // BSD-style license that can be found in the LICENSE file. |
| 5 | 5 |
| 6 /// Command line tool to merge the SDK libraries and our patch files. | 6 /// Command line tool to merge the SDK libraries and our patch files. |
| 7 /// This is currently designed as an offline tool, but we could automate it. | 7 /// This is currently designed as an offline tool, but we could automate it. |
| 8 | 8 |
| 9 import 'dart:io'; | 9 import 'dart:io'; |
| 10 import 'dart:math' as math; | 10 import 'dart:math' as math; |
| (...skipping 239 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 250 if (!patchFinder.applied.contains(patched)) { | 250 if (!patchFinder.applied.contains(patched)) { |
| 251 print('*** ${patched}'); | 251 print('*** ${patched}'); |
| 252 } | 252 } |
| 253 } | 253 } |
| 254 throw "Failed to apply all @patch-es"; | 254 throw "Failed to apply all @patch-es"; |
| 255 } | 255 } |
| 256 | 256 |
| 257 return new List<String>.from(results.map((e) => e.toString())); | 257 return new List<String>.from(results.map((e) => e.toString())); |
| 258 } | 258 } |
| 259 | 259 |
| 260 final injectedCidFields = [ |
| 261 'Array', 'ExternalOneByteString', 'GrowableObjectArray', |
| 262 'ImmutableArray', 'OneByteString', 'TwoByteString', 'Bigint' |
| 263 ].map((name) => "static final int cid${name} = 0;").join('\n'); |
| 264 |
| 260 /// Merge `@patch` declarations into `external` declarations. | 265 /// Merge `@patch` declarations into `external` declarations. |
| 261 class PatchApplier extends GeneralizingAstVisitor { | 266 class PatchApplier extends GeneralizingAstVisitor { |
| 262 final StringEditBuffer edits; | 267 final StringEditBuffer edits; |
| 263 final PatchFinder patch; | 268 final PatchFinder patch; |
| 264 | 269 |
| 265 bool _isLibrary = true; // until proven otherwise. | 270 bool _isLibrary = true; // until proven otherwise. |
| 266 | 271 |
| 267 PatchApplier(this.edits, this.patch); | 272 PatchApplier(this.edits, this.patch); |
| 268 | 273 |
| 269 @override | 274 @override |
| 270 visitCompilationUnit(CompilationUnit node) { | 275 visitCompilationUnit(CompilationUnit node) { |
| 271 super.visitCompilationUnit(node); | 276 super.visitCompilationUnit(node); |
| 272 if (_isLibrary) _mergeUnpatched(node); | 277 if (_isLibrary) _mergeUnpatched(node); |
| 273 } | 278 } |
| 274 | 279 |
| 275 void _merge(AstNode node, int pos) { | 280 void _merge(AstNode node, int pos) { |
| 276 var code = patch.contents.substring(node.offset, node.end); | 281 var code = patch.contents.substring(node.offset, node.end); |
| 282 |
| 283 // We inject a number of static fields into dart:internal.ClassID class. |
| 284 // These fields represent various VM class ids and are only used to |
| 285 // make core libraries compile. Kernel reader will actually ignore this |
| 286 // fields and instead inject concrete constants into this class. |
| 287 if (node is ClassDeclaration && node.name.name == 'ClassID') { |
| 288 code = code.replaceFirst( |
| 289 new RegExp(r'}$'), injectedCidFields + '}'); |
| 290 } |
| 277 edits.insert(pos, '\n' + code); | 291 edits.insert(pos, '\n' + code); |
| 278 } | 292 } |
| 279 | 293 |
| 280 /// Merges directives and declarations that are not `@patch` into the library. | 294 /// Merges directives and declarations that are not `@patch` into the library. |
| 281 void _mergeUnpatched(CompilationUnit unit) { | 295 void _mergeUnpatched(CompilationUnit unit) { |
| 282 // Merge imports from the patch | 296 // Merge imports from the patch |
| 283 // TODO(jmesserly): remove duplicate imports | 297 // TODO(jmesserly): remove duplicate imports |
| 284 | 298 |
| 285 // To patch a library, we must have a library directive | 299 // To patch a library, we must have a library directive |
| 286 var libDir = unit.directives.first as LibraryDirective; | 300 var libDir = unit.directives.first as LibraryDirective; |
| (...skipping 233 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 520 if (diff != 0) return diff; | 534 if (diff != 0) return diff; |
| 521 return end - other.end; | 535 return end - other.end; |
| 522 } | 536 } |
| 523 } | 537 } |
| 524 | 538 |
| 525 List<SdkLibrary> _getSdkLibraries(String contents) { | 539 List<SdkLibrary> _getSdkLibraries(String contents) { |
| 526 var libraryBuilder = new SdkLibrariesReader_LibraryBuilder(true); | 540 var libraryBuilder = new SdkLibrariesReader_LibraryBuilder(true); |
| 527 parseCompilationUnit(contents).accept(libraryBuilder); | 541 parseCompilationUnit(contents).accept(libraryBuilder); |
| 528 return libraryBuilder.librariesMap.sdkLibraries; | 542 return libraryBuilder.librariesMap.sdkLibraries; |
| 529 } | 543 } |
| OLD | NEW |