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

Side by Side Diff: web/web_command.dart

Issue 2183603003: Working compiler in browser. (Closed) Base URL: https://github.com/dart-lang/dev_compiler.git@master
Patch Set: Git merged master 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
(Empty)
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
3 // BSD-style license that can be found in the LICENSE file.
4 import 'dart:html' show HttpRequest;
5 import 'dart:convert' show BASE64;
6
7 import 'package:analyzer/file_system/file_system.dart'
8 show ResourceProvider, ResourceUriResolver;
9 import 'package:analyzer/file_system/memory_file_system.dart'
10 show MemoryResourceProvider;
11 import 'package:analyzer/src/context/cache.dart'
12 show AnalysisCache, CachePartition;
13 import 'package:analyzer/src/context/context.dart' show AnalysisContextImpl;
14 import 'package:analyzer/src/generated/engine.dart'
15 show AnalysisContext, AnalysisEngine, TimestampedData;
16 import 'package:analyzer/src/generated/sdk.dart'
17 show DartSdk, SdkLibrary, SdkLibraryImpl;
18 import 'package:analyzer/src/generated/source.dart'
19 show DartUriResolver, Source, SourceFactory, UriKind;
20 import 'package:analyzer/src/summary/idl.dart' show PackageBundle;
21 import 'package:analyzer/src/summary/summary_sdk.dart' show SummaryBasedDartSdk;
22
23 import 'package:args/command_runner.dart';
24
25 import 'package:dev_compiler/src/analyzer/context.dart' show AnalyzerOptions;
26 import 'package:dev_compiler/src/compiler/compiler.dart'
27 show BuildUnit, CompilerOptions, JSModuleFile, ModuleCompiler;
28
29 typedef void MessageHandler(Object message);
30 typedef String CompileFn(String dart);
31 typedef void OnLoadFn(CompileFn compile);
32
33 /// The command for invoking the modular compiler.
34 class WebCompileCommand extends Command {
35 get name => 'compile';
36 get description => 'Compile a set of Dart files into a JavaScript module.';
37 final MessageHandler messageHandler;
38 final OnLoadFn onload;
39
40 WebCompileCommand(this.onload, {MessageHandler messageHandler})
41 : this.messageHandler = messageHandler ?? print {
42 CompilerOptions.addArguments(argParser);
43 AnalyzerOptions.addArguments(argParser);
44 }
45
46 @override
47 void run() {
48 var request = new HttpRequest();
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 }
63
64 CompileFn setUpCompile(List<int> sdkBytes) {
65 var resourceProvider = new MemoryResourceProvider();
66 var packageBundle = new PackageBundle.fromBuffer(sdkBytes);
67 var webDartSdk = new SummaryBasedDartSdk.fromBundle(
68 true, packageBundle, resourceProvider);
69
70 var sdkResolver = new DartUriResolver(webDartSdk);
71 var fileResolvers = [new ResourceUriResolver(resourceProvider)];
72
73 var compiler = new ModuleCompiler(
74 new AnalyzerOptions(dartSdkPath: '/dart-sdk'),
75 sdkResolver: sdkResolver,
76 fileResolvers: fileResolvers,
77 resourceProvider: resourceProvider);
78
79 var compilerOptions = new CompilerOptions.fromArguments(argResults);
80
81 var number = 0;
82
83 return (String dart) {
84 // Create a new virtual File that contains the given Dart source.
85 number++;
86 resourceProvider.newFile("/expression$number.dart", dart);
87
88 var unit =
89 new BuildUnit("", "", ["file:///expression$number.dart"], null);
90
91 JSModuleFile module = compiler.compile(unit, compilerOptions);
92 module.errors.forEach(messageHandler);
93
94 if (!module.isValid) throw new CompileErrorException();
95 return module.code;
96 };
97 }
98 }
99
100 /// Thrown when the input source code has errors.
101 class CompileErrorException implements Exception {
102 toString() => '\nPlease fix all errors before compiling (warnings are okay).';
103 }
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