OLD | NEW |
---|---|
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 | |
4 import 'dart:html' show HttpRequest; | 7 import 'dart:html' show HttpRequest; |
5 import 'dart:convert' show BASE64; | 8 import 'dart:convert' show BASE64; |
9 import 'dart:async'; | |
Jacob
2016/08/03 23:58:32
sort imports in alphabetical order.
priscillalee
2016/08/04 16:01:56
Done.
| |
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; | |
21 import 'package:analyzer/src/summary/summary_sdk.dart' show SummaryBasedDartSdk; | 30 import 'package:analyzer/src/summary/summary_sdk.dart' show SummaryBasedDartSdk; |
22 | 31 |
23 import 'package:args/command_runner.dart'; | 32 import 'package:args/command_runner.dart'; |
24 | 33 |
25 import 'package:dev_compiler/src/analyzer/context.dart' show AnalyzerOptions; | 34 import 'package:dev_compiler/src/analyzer/context.dart' show AnalyzerOptions; |
26 import 'package:dev_compiler/src/compiler/compiler.dart' | 35 import 'package:dev_compiler/src/compiler/compiler.dart' |
27 show BuildUnit, CompilerOptions, JSModuleFile, ModuleCompiler; | 36 show BuildUnit, CompilerOptions, JSModuleFile, ModuleCompiler; |
28 | 37 |
38 import 'package:js/js.dart'; | |
39 | |
29 typedef void MessageHandler(Object message); | 40 typedef void MessageHandler(Object message); |
30 typedef String CompileFn(String dart); | |
31 typedef void OnLoadFn(CompileFn compile); | |
32 | 41 |
33 /// The command for invoking the modular compiler. | 42 /// The command for invoking the modular compiler. |
34 class WebCompileCommand extends Command { | 43 class WebCompileCommand extends Command { |
35 get name => 'compile'; | 44 get name => 'compile'; |
36 get description => 'Compile a set of Dart files into a JavaScript module.'; | 45 get description => 'Compile a set of Dart files into a JavaScript module.'; |
37 final MessageHandler messageHandler; | 46 final MessageHandler messageHandler; |
38 final OnLoadFn onload; | |
39 | 47 |
40 WebCompileCommand(this.onload, {MessageHandler messageHandler}) | 48 WebCompileCommand({MessageHandler messageHandler}) |
41 : this.messageHandler = messageHandler ?? print { | 49 : this.messageHandler = messageHandler ?? print { |
42 CompilerOptions.addArguments(argParser); | 50 CompilerOptions.addArguments(argParser); |
43 AnalyzerOptions.addArguments(argParser); | 51 AnalyzerOptions.addArguments(argParser); |
44 } | 52 } |
45 | 53 |
46 @override | 54 @override |
47 void run() { | 55 Function run() { |
48 var request = new HttpRequest(); | 56 return requestSummaries; |
Jacob
2016/08/03 23:58:31
Are we really getting anything from implementing C
priscillalee
2016/08/04 16:01:56
I agree. It does seem strange. I don't understand
| |
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 } | 57 } |
63 | 58 |
64 CompileFn setUpCompile(List<int> sdkBytes) { | 59 void requestSummaries( |
60 String sdkUrl, List<String> summaryUrls, Function callback) { | |
61 HttpRequest.request(sdkUrl).then((sdkRequest) { | |
62 var sdkResponse = sdkRequest.responseText; | |
63 var sdkBytes = BASE64.decode(sdkResponse); | |
64 | |
65 // Map summary URLs to HttpRequests. | |
66 var summaryRequests = summaryUrls | |
67 .map((summary) => new Future(() => HttpRequest.request(summary))); | |
68 | |
69 Future.wait(summaryRequests).then((summaryResponses) { | |
70 // Map summary responses to summary bytes. | |
71 var summaryBytes = summaryResponses | |
72 .map((response) => BASE64.decode(response.responseText)); | |
73 | |
74 var compileFn = setUpCompile(sdkBytes, summaryBytes); | |
75 callback(compileFn); | |
76 }); | |
Jacob
2016/08/03 23:58:31
You should handle if a summary fails to load.
priscillalee
2016/08/04 16:01:56
I added status checks, but should I also throw exc
Jacob
2016/08/04 16:27:48
Ideally you would report the error back to the cal
priscillalee
2016/08/04 17:56:33
Done.
| |
77 }); | |
78 } | |
79 | |
80 Function setUpCompile(List<int> sdkBytes, List<List<int>> summaryBytes) { | |
65 var resourceProvider = new MemoryResourceProvider(); | 81 var resourceProvider = new MemoryResourceProvider(); |
82 var resourceUriResolver = new ResourceUriResolver(resourceProvider); | |
83 | |
66 var packageBundle = new PackageBundle.fromBuffer(sdkBytes); | 84 var packageBundle = new PackageBundle.fromBuffer(sdkBytes); |
67 var webDartSdk = new SummaryBasedDartSdk.fromBundle( | 85 var webDartSdk = new SummaryBasedDartSdk.fromBundle( |
68 true, packageBundle, resourceProvider); | 86 true, packageBundle, resourceProvider); |
87 var sdkResolver = new DartUriResolver(webDartSdk); | |
69 | 88 |
70 var sdkResolver = new DartUriResolver(webDartSdk); | 89 var summaryDataStore = new SummaryDataStore([]); |
71 var fileResolvers = [new ResourceUriResolver(resourceProvider)]; | 90 for (var bytes in summaryBytes) { |
91 var summaryBundle = new PackageBundle.fromBuffer(bytes); | |
92 summaryDataStore.addBundle('', summaryBundle); | |
93 } | |
94 var summaryResolver = new InSummaryPackageUriResolver(summaryDataStore); | |
95 | |
96 var fileResolvers = [summaryResolver, resourceUriResolver]; | |
72 | 97 |
73 var compiler = new ModuleCompiler( | 98 var compiler = new ModuleCompiler( |
74 new AnalyzerOptions(dartSdkPath: '/dart-sdk'), | 99 new AnalyzerOptions(dartSdkPath: '/dart-sdk'), |
75 sdkResolver: sdkResolver, | 100 sdkResolver: sdkResolver, |
76 fileResolvers: fileResolvers, | 101 fileResolvers: fileResolvers, |
77 resourceProvider: resourceProvider); | 102 resourceProvider: resourceProvider); |
78 | 103 |
104 compiler.context.resultProvider = | |
105 new InputPackagesResultProvider(compiler.context, summaryDataStore); | |
106 | |
79 var compilerOptions = new CompilerOptions.fromArguments(argResults); | 107 var compilerOptions = new CompilerOptions.fromArguments(argResults); |
80 | 108 |
81 var number = 0; | 109 var number = 0; |
82 | 110 |
83 return (String dart) { | 111 var compileFn = (String dart) { |
84 // Create a new virtual File that contains the given Dart source. | 112 // Create a new virtual File that contains the given Dart source. |
85 number++; | 113 number++; |
86 resourceProvider.newFile("/expression$number.dart", dart); | 114 resourceProvider.newFile("/expression$number.dart", dart); |
87 | 115 |
88 var unit = | 116 var unit = new BuildUnit( |
89 new BuildUnit("", "", ["file:///expression$number.dart"], null); | 117 "", "", ["file:///expression$number.dart"], (source) => ""); |
Jacob
2016/08/03 23:58:32
what is the fn (source) => "" doing?
shouldn't it
Jacob
2016/08/03 23:58:32
nit: put {} around number for clarity.
e.g.
${num
priscillalee
2016/08/04 16:01:56
It's the libraryToModule function of a BuildUnit.
priscillalee
2016/08/04 16:01:56
Done.
Jacob
2016/08/04 16:27:48
do that everywhere in the file where $number occur
priscillalee
2016/08/04 17:56:33
Done.
| |
90 | 118 |
91 JSModuleFile module = compiler.compile(unit, compilerOptions); | 119 JSModuleFile module = compiler.compile(unit, compilerOptions); |
92 module.errors.forEach(messageHandler); | 120 module.errors.forEach(messageHandler); |
93 | 121 |
94 if (!module.isValid) throw new CompileErrorException(); | 122 if (!module.isValid) throw new CompileErrorException(); |
95 return module.code; | 123 return module.code; |
96 }; | 124 }; |
125 | |
126 return allowInterop(compileFn); | |
97 } | 127 } |
98 } | 128 } |
99 | 129 |
100 /// Thrown when the input source code has errors. | 130 /// Thrown when the input source code has errors. |
101 class CompileErrorException implements Exception { | 131 class CompileErrorException implements Exception { |
102 toString() => '\nPlease fix all errors before compiling (warnings are okay).'; | 132 toString() => '\nPlease fix all errors before compiling (warnings are okay).'; |
103 } | 133 } |
OLD | NEW |