Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | |
| 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. | |
| 4 | |
| 5 // Test of the graph segmentation algorithm used by deferred loading | |
| 6 // to determine which elements can be deferred and which libraries | |
| 7 // much be included in the initial download (loaded eagerly). | |
| 8 | |
| 9 import 'dart:async' show Future; | |
| 10 import 'dart:uri' show Uri; | |
| 11 | |
| 12 import '../../../sdk/lib/_internal/compiler/implementation/apiimpl.dart' | |
| 13 show Compiler; | |
| 14 | |
| 15 import '../../../sdk/lib/_internal/compiler/implementation/dart2jslib.dart' | |
| 16 as dart2js; | |
| 17 | |
| 18 import '../../../sdk/lib/_internal/compiler/implementation/filenames.dart' | |
| 19 show getCurrentDirectory; | |
| 20 | |
| 21 import '../../../sdk/lib/_internal/compiler/implementation/source_file.dart' | |
| 22 show SourceFile; | |
| 23 | |
| 24 import '../../../sdk/lib/_internal/compiler/implementation/source_file_provider. dart' | |
| 25 show FormattingDiagnosticHandler, | |
| 26 SourceFileProvider; | |
| 27 | |
| 28 class MemorySourceFileProvider extends SourceFileProvider { | |
| 29 Future<String> readStringFromUri(Uri resourceUri) { | |
| 30 if (resourceUri.scheme != 'memory') { | |
| 31 return super.readStringFromUri(resourceUri); | |
| 32 } | |
| 33 String source = MEMORY_SOURCE_FILES[resourceUri.path]; | |
| 34 // TODO(ahe): Return new Future.immediateError(...) ? | |
| 35 if (source == null) throw 'No such file $resourceUri'; | |
| 36 String resourceName = '$resourceUri'; | |
| 37 this.sourceFiles[resourceName] = new SourceFile(resourceName, source); | |
| 38 return new Future.immediate(source); | |
| 39 } | |
| 40 } | |
| 41 | |
| 42 void main() { | |
| 43 Uri cwd = getCurrentDirectory(); | |
| 44 Uri script = cwd.resolve(new Options().script); | |
| 45 Uri libraryRoot = script.resolve('../../../sdk/'); | |
| 46 Uri packageRoot = script.resolve('./packages/'); | |
| 47 | |
| 48 var provider = new MemorySourceFileProvider(); | |
| 49 var handler = new FormattingDiagnosticHandler(provider); | |
| 50 | |
| 51 Compiler compiler = new Compiler(provider.readStringFromUri, | |
| 52 (name, extension) => null, | |
| 53 handler.diagnosticHandler, | |
| 54 libraryRoot, | |
| 55 packageRoot, | |
| 56 ['--analyze-only']); | |
| 57 compiler.run(new Uri('memory:main.dart')); | |
| 58 var main = compiler.mainApp.find(dart2js.Compiler.MAIN); | |
| 59 Expect.isNotNull(main, 'Could not find "main"'); | |
| 60 compiler.deferredLoadTask.onResolutionComplete(main); | |
| 61 | |
| 62 var deferredClasses = | |
| 63 compiler.deferredLoadTask.deferredElements.where((e) => e.isClass()) | |
|
kasperl
2013/03/06 10:26:51
4 space indent.
ahe
2013/03/06 16:01:41
Done.
| |
| 64 .toSet(); | |
| 65 | |
| 66 var expando = | |
| 67 deferredClasses.where((e) => e.name.slowToString() == 'Expando').single; | |
| 68 | |
| 69 var myClass = | |
| 70 deferredClasses.where((e) => e.name.slowToString() == 'MyClass').single; | |
| 71 | |
| 72 var deferredLibrary = compiler.libraries['memory:deferred.dart']; | |
| 73 | |
| 74 Expect.equals(deferredLibrary, myClass.getLibrary()); | |
| 75 Expect.equals(compiler.coreLibrary, expando.declaration.getLibrary()); | |
| 76 } | |
| 77 | |
| 78 const Map MEMORY_SOURCE_FILES = const { | |
| 79 'main.dart': """ | |
| 80 import 'dart:async'; | |
| 81 | |
| 82 @lazy import 'deferred.dart'; | |
| 83 | |
| 84 const lazy = const DeferredLibrary('deferred'); | |
| 85 | |
| 86 main() { | |
| 87 lazy.load().then((_) { | |
| 88 Expect.equals(42, new MyClass().foo(87)); | |
| 89 }); | |
| 90 } | |
| 91 | |
| 92 """, | |
| 93 'deferred.dart': """ | |
| 94 library deferred; | |
| 95 | |
| 96 class MyClass { | |
| 97 const MyClass(); | |
| 98 | |
| 99 foo(x) { | |
| 100 new Expando(); | |
| 101 return (x - 3) ~/ 2; | |
| 102 } | |
| 103 } | |
| 104 """, | |
| 105 }; | |
| OLD | NEW |