Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(409)

Side by Side Diff: web/web_command.dart

Issue 2208243002: Restructured the set up for the compiling function. Also added room for list of additional summarie… (Closed) Base URL: https://github.com/dart-lang/dev_compiler.git@master
Patch Set: Added module names and bundle names to allow for import statements in the console. Created 4 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « web/main.dart ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2016, 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 @JS()
5 library dev_compiler.web.web_command;
6
7 import 'dart:async';
4 import 'dart:html' show HttpRequest; 8 import 'dart:html' show HttpRequest;
5 import 'dart:convert' show BASE64; 9 import 'dart:convert' show BASE64;
6 10
7 import 'package:analyzer/file_system/file_system.dart' 11 import 'package:analyzer/file_system/file_system.dart'
8 show ResourceProvider, ResourceUriResolver; 12 show ResourceProvider, ResourceUriResolver;
9 import 'package:analyzer/file_system/memory_file_system.dart' 13 import 'package:analyzer/file_system/memory_file_system.dart'
10 show MemoryResourceProvider; 14 show MemoryResourceProvider;
11 import 'package:analyzer/src/context/cache.dart' 15 import 'package:analyzer/src/context/cache.dart'
12 show AnalysisCache, CachePartition; 16 show AnalysisCache, CachePartition;
13 import 'package:analyzer/src/context/context.dart' show AnalysisContextImpl; 17 import 'package:analyzer/src/context/context.dart' show AnalysisContextImpl;
14 import 'package:analyzer/src/generated/engine.dart' 18 import 'package:analyzer/src/generated/engine.dart'
15 show AnalysisContext, AnalysisEngine, TimestampedData; 19 show AnalysisContext, AnalysisEngine, TimestampedData;
16 import 'package:analyzer/src/generated/sdk.dart' 20 import 'package:analyzer/src/generated/sdk.dart'
17 show DartSdk, SdkLibrary, SdkLibraryImpl; 21 show DartSdk, SdkLibrary, SdkLibraryImpl;
18 import 'package:analyzer/src/generated/source.dart' 22 import 'package:analyzer/src/generated/source.dart'
19 show DartUriResolver, Source, SourceFactory, UriKind; 23 show DartUriResolver, Source, SourceFactory, UriKind;
20 import 'package:analyzer/src/summary/idl.dart' show PackageBundle; 24 import 'package:analyzer/src/summary/idl.dart' show PackageBundle;
25 import 'package:analyzer/src/summary/package_bundle_reader.dart'
26 show
27 SummaryDataStore,
28 InSummaryPackageUriResolver,
29 InputPackagesResultProvider,
30 InSummarySource;
21 import 'package:analyzer/src/summary/summary_sdk.dart' show SummaryBasedDartSdk; 31 import 'package:analyzer/src/summary/summary_sdk.dart' show SummaryBasedDartSdk;
22 32
23 import 'package:args/command_runner.dart'; 33 import 'package:args/command_runner.dart';
24 34
25 import 'package:dev_compiler/src/analyzer/context.dart' show AnalyzerOptions; 35 import 'package:dev_compiler/src/analyzer/context.dart' show AnalyzerOptions;
26 import 'package:dev_compiler/src/compiler/compiler.dart' 36 import 'package:dev_compiler/src/compiler/compiler.dart'
27 show BuildUnit, CompilerOptions, JSModuleFile, ModuleCompiler; 37 show BuildUnit, CompilerOptions, JSModuleFile, ModuleCompiler;
28 38
39 import 'package:js/js.dart';
40
29 typedef void MessageHandler(Object message); 41 typedef void MessageHandler(Object message);
30 typedef String CompileFn(String dart);
31 typedef void OnLoadFn(CompileFn compile);
32 42
33 /// The command for invoking the modular compiler. 43 /// The command for invoking the modular compiler.
34 class WebCompileCommand extends Command { 44 class WebCompileCommand extends Command {
35 get name => 'compile'; 45 get name => 'compile';
36 get description => 'Compile a set of Dart files into a JavaScript module.'; 46 get description => 'Compile a set of Dart files into a JavaScript module.';
37 final MessageHandler messageHandler; 47 final MessageHandler messageHandler;
38 final OnLoadFn onload;
39 48
40 WebCompileCommand(this.onload, {MessageHandler messageHandler}) 49 WebCompileCommand({MessageHandler messageHandler})
41 : this.messageHandler = messageHandler ?? print { 50 : this.messageHandler = messageHandler ?? print {
42 CompilerOptions.addArguments(argParser); 51 CompilerOptions.addArguments(argParser);
43 AnalyzerOptions.addArguments(argParser); 52 AnalyzerOptions.addArguments(argParser);
44 } 53 }
45 54
46 @override 55 @override
47 void run() { 56 Function run() {
48 var request = new HttpRequest(); 57 return requestSummaries;
49
50 request.onReadyStateChange.listen((_) {
51 if (request.readyState == HttpRequest.DONE &&
52 (request.status == 200 || request.status == 0)) {
53 var response = request.responseText;
54 var sdkBytes = BASE64.decode(response);
55 var result = setUpCompile(sdkBytes);
56 onload(result);
57 }
58 });
59
60 request.open('get', 'dart_sdk.sum');
61 request.send();
62 } 58 }
63 59
64 CompileFn setUpCompile(List<int> sdkBytes) { 60 void requestSummaries(String sdkUrl, List<String> summaryUrls,
61 Function onCompileReady, Function onError) {
62 HttpRequest.request(sdkUrl).then((sdkRequest) {
63 var sdkResponse = sdkRequest.responseText;
64 var sdkBytes = BASE64.decode(sdkResponse);
65
66 // Map summary URLs to HttpRequests.
67 var summaryRequests = summaryUrls
68 .map((summary) => new Future(() => HttpRequest.request(summary)));
69
70 Future.wait(summaryRequests).then((summaryResponses) {
71 // Map summary responses to summary bytes.
72 var summaryBytes = [];
73 for (var response in summaryResponses) {
74 summaryBytes.add(BASE64.decode(response.responseText));
75 }
76
77 var compileFn = setUpCompile(sdkBytes, summaryBytes, summaryUrls);
78 onCompileReady(compileFn);
79 }).catchError((error) => onError('Summaries failed to load: $error'));
80 }).catchError(
81 (error) => onError('Dart sdk summaries failed to load: $error'));
82 }
83
84 Function setUpCompile(List<int> sdkBytes, List<List<int>> summaryBytes,
85 List<String> summaryUrls) {
65 var resourceProvider = new MemoryResourceProvider(); 86 var resourceProvider = new MemoryResourceProvider();
87 var resourceUriResolver = new ResourceUriResolver(resourceProvider);
88
66 var packageBundle = new PackageBundle.fromBuffer(sdkBytes); 89 var packageBundle = new PackageBundle.fromBuffer(sdkBytes);
67 var webDartSdk = new SummaryBasedDartSdk.fromBundle( 90 var webDartSdk = new SummaryBasedDartSdk.fromBundle(
68 true, packageBundle, resourceProvider); 91 true, packageBundle, resourceProvider);
92 var sdkResolver = new DartUriResolver(webDartSdk);
69 93
70 var sdkResolver = new DartUriResolver(webDartSdk); 94 var summaryDataStore = new SummaryDataStore([]);
71 var fileResolvers = [new ResourceUriResolver(resourceProvider)]; 95 for (var i = 0; i < summaryBytes.length; i++) {
96 var bytes = summaryBytes[i];
97 var url = summaryUrls[i];
98 var summaryBundle = new PackageBundle.fromBuffer(bytes);
99 summaryDataStore.addBundle(url, summaryBundle);
100 }
101 var summaryResolver = new InSummaryPackageUriResolver(summaryDataStore);
102
103 var fileResolvers = [summaryResolver, resourceUriResolver];
72 104
73 var compiler = new ModuleCompiler( 105 var compiler = new ModuleCompiler(
74 new AnalyzerOptions(dartSdkPath: '/dart-sdk'), 106 new AnalyzerOptions(dartSdkPath: '/dart-sdk'),
75 sdkResolver: sdkResolver, 107 sdkResolver: sdkResolver,
76 fileResolvers: fileResolvers, 108 fileResolvers: fileResolvers,
77 resourceProvider: resourceProvider); 109 resourceProvider: resourceProvider);
78 110
111 compiler.context.resultProvider =
112 new InputPackagesResultProvider(compiler.context, summaryDataStore);
113
79 var compilerOptions = new CompilerOptions.fromArguments(argResults); 114 var compilerOptions = new CompilerOptions.fromArguments(argResults);
80 115
81 var number = 0; 116 var compileFn = (String dart, int number) {
117 // Create a new virtual File that contains the given Dart source.
118 resourceProvider.newFile("/expression${number}.dart", dart);
82 119
83 return (String dart) { 120 var unit = new BuildUnit("expression${number}", "",
84 // Create a new virtual File that contains the given Dart source. 121 ["file:///expression${number}.dart"], _moduleForLibrary);
85 number++;
86 resourceProvider.newFile("/expression$number.dart", dart);
87
88 var unit =
89 new BuildUnit("", "", ["file:///expression$number.dart"], null);
90 122
91 JSModuleFile module = compiler.compile(unit, compilerOptions); 123 JSModuleFile module = compiler.compile(unit, compilerOptions);
92 module.errors.forEach(messageHandler); 124 module.errors.forEach(messageHandler);
93 125
94 if (!module.isValid) throw new CompileErrorException(); 126 if (!module.isValid) throw new CompileErrorException();
95 return module.code; 127 return module.code;
96 }; 128 };
129
130 return allowInterop(compileFn);
97 } 131 }
98 } 132 }
99 133
134 // Given path, determine corresponding dart library.
135 String _moduleForLibrary(source) {
136 if (source is InSummarySource) {
137 return source.summaryPath.substring(1).replaceAll('.api.ds', '');
138 }
139 return source.toString().substring(1).replaceAll('.dart', '');
140 }
141
100 /// Thrown when the input source code has errors. 142 /// Thrown when the input source code has errors.
101 class CompileErrorException implements Exception { 143 class CompileErrorException implements Exception {
102 toString() => '\nPlease fix all errors before compiling (warnings are okay).'; 144 toString() => '\nPlease fix all errors before compiling (warnings are okay).';
103 } 145 }
OLDNEW
« no previous file with comments | « web/main.dart ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698