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

Side by Side Diff: pkg/dev_compiler/web/web_command.dart

Issue 2811343002: Dev compiler debugger related tweaks. (Closed)
Patch Set: Dev compiler debugger related tweaks. Created 3 years, 8 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
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() 4 @JS()
5 library dev_compiler.web.web_command; 5 library dev_compiler.web.web_command;
6 6
7 import 'dart:async'; 7 import 'dart:async';
8 import 'dart:convert'; 8 import 'dart:convert';
9 import 'dart:html' show HttpRequest; 9 import 'dart:html' show HttpRequest;
10 import 'dart:typed_data';
10 11
11 import 'package:analyzer/dart/element/element.dart' 12 import 'package:analyzer/dart/element/element.dart'
12 show 13 show
13 LibraryElement, 14 LibraryElement,
14 ImportElement, 15 ImportElement,
15 ShowElementCombinator, 16 ShowElementCombinator,
16 HideElementCombinator; 17 HideElementCombinator;
17 import 'package:analyzer/file_system/file_system.dart' show ResourceUriResolver; 18 import 'package:analyzer/file_system/file_system.dart' show ResourceUriResolver;
18 import 'package:analyzer/file_system/memory_file_system.dart' 19 import 'package:analyzer/file_system/memory_file_system.dart'
19 show MemoryResourceProvider; 20 show MemoryResourceProvider;
(...skipping 10 matching lines...) Expand all
30 show BuildUnit, CompilerOptions, JSModuleFile, ModuleCompiler; 31 show BuildUnit, CompilerOptions, JSModuleFile, ModuleCompiler;
31 32
32 import 'package:dev_compiler/src/compiler/module_builder.dart'; 33 import 'package:dev_compiler/src/compiler/module_builder.dart';
33 import 'package:js/js.dart'; 34 import 'package:js/js.dart';
34 import 'package:path/path.dart' as path; 35 import 'package:path/path.dart' as path;
35 36
36 typedef void MessageHandler(Object message); 37 typedef void MessageHandler(Object message);
37 38
38 @JS() 39 @JS()
39 @anonymous 40 @anonymous
41 class JSIterator<V> {}
42
43 @JS('Map')
44 class JSMap<K, V> {
45 external V get(K v);
46 external set(K k, V v);
47 external JSIterator<K> keys();
48 external JSIterator<V> values();
49 }
50
51 @JS('Array.from')
52 external List<V> iteratorToList<V>(JSIterator<V> iterator);
53
54 @JS()
55 @anonymous
40 class CompileResult { 56 class CompileResult {
41 external factory CompileResult( 57 external factory CompileResult(
42 {String code, List<String> errors, bool isValid}); 58 {String code, List<String> errors, bool isValid});
43 } 59 }
44 60
45 typedef CompileModule(String imports, String body, String libraryName, 61 typedef CompileModule(String imports, String body, String libraryName,
46 String existingLibrary, String fileName); 62 String existingLibrary, String fileName);
47 63
64 /// Version of HttpRequest.request that retries on 502 errors.
65 Future<HttpRequest> httpRequestRetry502Error(String url,
66 {String responseType, String mimeType}) {
67 var completer = new Completer<HttpRequest>();
68
69 var xhr = new HttpRequest();
70 xhr.open('GET', url, async: true);
71
72 if (responseType != null) {
73 xhr.responseType = responseType;
74 }
75
76 if (mimeType != null) {
77 xhr.overrideMimeType(mimeType);
78 }
79
80 xhr.onLoad.listen((e) {
81 var accepted = xhr.status >= 200 && xhr.status < 300;
82 var fileUri = xhr.status == 0; // file:// URIs have status of 0.
83 var notModified = xhr.status == 304;
84 // Redirect status is specified up to 307, but others have been used in
85 // practice. Notably Google Drive uses 308 Resume Incomplete for
86 // resumable uploads, and it's also been used as a redirect. The
87 // redirect case will be handled by the browser before it gets to us,
88 // so if we see it we should pass it through to the user.
89 var unknownRedirect = xhr.status > 307 && xhr.status < 400;
90
91 if (accepted || fileUri || notModified || unknownRedirect) {
92 completer.complete(xhr);
93 } else {
94 if (xhr.status == 502) {
95 print("Retrying due to 502 error for $url");
96 return HttpRequest.request(url,
97 responseType: responseType, mimeType: mimeType);
98 }
99 completer.completeError(e);
100 }
101 });
102
103 xhr.onError.listen(completer.completeError);
104
105 xhr.send();
106 return completer.future;
107 }
108
48 /// The command for invoking the modular compiler. 109 /// The command for invoking the modular compiler.
49 class WebCompileCommand extends Command { 110 class WebCompileCommand extends Command {
50 get name => 'compile'; 111 get name => 'compile';
51 112
52 get description => 'Compile a set of Dart files into a JavaScript module.'; 113 get description => 'Compile a set of Dart files into a JavaScript module.';
53 final MessageHandler messageHandler; 114 final MessageHandler messageHandler;
54 115
55 WebCompileCommand({MessageHandler messageHandler}) 116 WebCompileCommand({MessageHandler messageHandler})
56 : this.messageHandler = messageHandler ?? print { 117 : this.messageHandler = messageHandler ?? print {
57 CompilerOptions.addArguments(argParser); 118 CompilerOptions.addArguments(argParser);
58 AnalyzerOptions.addArguments(argParser); 119 AnalyzerOptions.addArguments(argParser);
59 } 120 }
60 121
61 @override 122 @override
62 Function run() { 123 Function run() {
63 return requestSummaries; 124 return requestSummaries;
64 } 125 }
65 126
66 void requestSummaries(String summaryRoot, String sdkUrl, 127 Future<Null> requestSummaries(String sdkUrl, JSMap<String, String> summaryMap,
67 List<String> summaryUrls, Function onCompileReady, Function onError) { 128 Function onCompileReady, Function onError) async {
68 HttpRequest 129 var sdkRequest;
69 .request(sdkUrl, 130 try {
70 responseType: "arraybuffer", mimeType: "application/octet-stream") 131 sdkRequest = await httpRequestRetry502Error(sdkUrl,
71 .then((sdkRequest) { 132 responseType: "arraybuffer", mimeType: "application/octet-stream");
72 var sdkBytes = sdkRequest.response.asUint8List(); 133 } catch (error) {
134 onError('Dart sdk summaries failed to load: $error. url: $sdkUrl');
135 return null;
136 }
73 137
74 // Map summary URLs to HttpRequests. 138 var sdkBytes = (sdkRequest.response as ByteBuffer).asUint8List();
75 var summaryRequests = summaryUrls.map((summary) => new Future(() =>
76 HttpRequest.request(summaryRoot + summary,
77 responseType: "arraybuffer",
78 mimeType: "application/octet-stream")));
79 139
80 Future.wait(summaryRequests).then((summaryResponses) { 140 // Map summary URLs to HttpRequests.
81 // Map summary responses to summary bytes. 141 var summaryRequests = iteratorToList(summaryMap.values())
82 var summaryBytes = <List<int>>[]; 142 .map((String summaryUrl) => httpRequestRetry502Error(summaryUrl,
83 for (var response in summaryResponses) { 143 responseType: "arraybuffer", mimeType: "application/octet-stream"))
84 summaryBytes.add(response.response.asUint8List()); 144 .toList();
85 } 145 var summaryResponses;
86 146 try {
87 onCompileReady(setUpCompile(sdkBytes, summaryBytes, summaryUrls)); 147 summaryResponses = await Future.wait(summaryRequests);
88 }).catchError((error) => onError('Summaries failed to load: $error')); 148 } catch (error) {
89 }).catchError((error) => 149 print('Summaries failed to load: $error');
90 onError('Dart sdk summaries failed to load: $error. url: $sdkUrl')); 150 onError('Summaries failed to load: $error');
151 return null;
152 }
153 // Map summary responses to summary bytes.
154 List<List<int>> summaryBytes = summaryResponses
155 .map((response) => (response.response as ByteBuffer).asUint8List())
156 .toList();
157 print("Summaries loaded");
158 onCompileReady(setUpCompile(
159 sdkBytes, summaryBytes, iteratorToList(summaryMap.keys())));
91 } 160 }
92 161
93 List<Function> setUpCompile(List<int> sdkBytes, List<List<int>> summaryBytes, 162 List<Function> setUpCompile(List<int> sdkBytes, List<List<int>> summaryBytes,
94 List<String> summaryUrls) { 163 List<String> moduleIds) {
95 var dartSdkSummaryPath = '/dart-sdk/lib/_internal/web_sdk.sum'; 164 var dartSdkSummaryPath = '/dart-sdk/lib/_internal/web_sdk.sum';
96 165
97 var resourceProvider = new MemoryResourceProvider() 166 var resourceProvider = new MemoryResourceProvider()
98 ..newFileWithBytes(dartSdkSummaryPath, sdkBytes); 167 ..newFileWithBytes(dartSdkSummaryPath, sdkBytes);
99 168
100 var resourceUriResolver = new ResourceUriResolver(resourceProvider); 169 var resourceUriResolver = new ResourceUriResolver(resourceProvider);
101 170
102 var options = new AnalyzerOptions.basic( 171 var options = new AnalyzerOptions.basic(
103 dartSdkPath: '/dart-sdk', dartSdkSummaryPath: dartSdkSummaryPath); 172 dartSdkPath: '/dart-sdk', dartSdkSummaryPath: dartSdkSummaryPath);
104 173
105 var summaryDataStore = new SummaryDataStore(options.summaryPaths, 174 var summaryDataStore = new SummaryDataStore(options.summaryPaths,
106 resourceProvider: resourceProvider, recordDependencyInfo: true); 175 resourceProvider: resourceProvider, recordDependencyInfo: true);
107 for (var i = 0; i < summaryBytes.length; i++) { 176 for (var i = 0; i < summaryBytes.length; i++) {
108 var bytes = summaryBytes[i]; 177 var bytes = summaryBytes[i];
109 var url = '/' + summaryUrls[i]; 178 // Packages with no dart source files will have empty summaries which is f ine.
110 var summaryBundle = new PackageBundle.fromBuffer(bytes); 179 if (bytes.length == 0) continue;
111 summaryDataStore.addBundle(url, summaryBundle); 180 var url = '/${moduleIds[i]}.api.ds';
181 try {
182 var summaryBundle = new PackageBundle.fromBuffer(bytes);
183 summaryDataStore.addBundle(url, summaryBundle);
184 } catch (e) {
185 print(
186 "XXX failed to load summary for $i, ${moduleIds[i]}, len = ${summary Bytes[i].length}");
187 continue;
188 }
112 } 189 }
113 var summaryResolver = 190 var summaryResolver =
114 new InSummaryUriResolver(resourceProvider, summaryDataStore); 191 new InSummaryUriResolver(resourceProvider, summaryDataStore);
115 192
116 var fileResolvers = [summaryResolver, resourceUriResolver]; 193 var fileResolvers = [summaryResolver, resourceUriResolver];
117 194
118 var compiler = new ModuleCompiler(options, 195 var compiler = new ModuleCompiler(options,
119 analysisRoot: '/web-compile-root', 196 analysisRoot: '/web-compile-root',
120 fileResolvers: fileResolvers, 197 fileResolvers: fileResolvers,
121 resourceProvider: resourceProvider, 198 resourceProvider: resourceProvider,
(...skipping 154 matching lines...) Expand 10 before | Expand all | Expand 10 after
276 if (source is InSummarySource) { 353 if (source is InSummarySource) {
277 return source.summaryPath.substring(1).replaceAll('.api.ds', ''); 354 return source.summaryPath.substring(1).replaceAll('.api.ds', '');
278 } 355 }
279 return source.toString().substring(1).replaceAll('.dart', ''); 356 return source.toString().substring(1).replaceAll('.dart', '');
280 } 357 }
281 358
282 /// Thrown when the input source code has errors. 359 /// Thrown when the input source code has errors.
283 class CompileErrorException implements Exception { 360 class CompileErrorException implements Exception {
284 toString() => '\nPlease fix all errors before compiling (warnings are okay).'; 361 toString() => '\nPlease fix all errors before compiling (warnings are okay).';
285 } 362 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698