OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2017, 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 /// API for compiling Dart source code to .dill (Kernel IR) files. | |
6 library front_end.vm; | |
7 // TODO(ahe): Move this to lib/ once the API has matured. | |
8 | |
9 import 'dart:async' show Future; | |
10 | |
11 import 'dart:io' show File, Platform; | |
12 | |
13 import 'dart:typed_data' show Uint8List; | |
14 | |
15 import 'fasta/outline.dart' as fasta; | |
16 | |
17 /// Compilation status codes. | |
18 /// | |
19 /// Note: The [index] property of these constants must match | |
20 /// `Dart_KernelCompilationStatus` in | |
21 /// [dart_api.h](../../../../runtime/include/dart_api.h). | |
22 enum Status { | |
23 /// Compilation was successful. | |
24 ok, | |
25 | |
26 /// Compilation failed with a compile time error. | |
27 error, | |
28 | |
29 /// Compiler crashed. | |
30 crash, | |
31 } | |
32 | |
33 abstract class CompilationResult { | |
34 CompilationResult._(); | |
35 | |
36 factory CompilationResult.ok(Uint8List bytes) = _CompilationOk; | |
Vyacheslav Egorov (Google)
2017/03/10 09:58:15
This is cool. I learned something today!
ahe
2017/03/10 12:05:49
Acknowledged.
| |
37 | |
38 factory CompilationResult.error(List<String> errors) = _CompilationError; | |
39 | |
40 factory CompilationResult.crash(Object exception, StackTrace stack) = | |
41 _CompilationCrash; | |
42 | |
43 Status get status; | |
44 | |
45 get payload; | |
46 | |
47 List toResponse() => [status.index, payload]; | |
48 } | |
49 | |
50 Future<CompilationResult> parseScript(Uri script, {bool verbose: false}) async { | |
51 final Uri packagesUri = (Platform.packageConfig != null) | |
52 ? Uri.parse(Platform.packageConfig) | |
53 : await _findPackagesFile(script); | |
54 if (packagesUri == null) { | |
55 throw "Could not find .packages"; | |
56 } | |
57 | |
58 final Uri patchedSdk = Uri.base | |
59 .resolveUri(new Uri.file(Platform.resolvedExecutable)) | |
60 .resolveUri(new Uri.directory("patched_sdk")); | |
61 | |
62 if (verbose) { | |
63 print("""DFE: Requesting compilation { | |
64 scriptUri: ${script} | |
65 packagesUri: ${packagesUri} | |
66 patchedSdk: ${patchedSdk} | |
67 }"""); | |
68 } | |
69 | |
70 try { | |
71 return await fasta.parseScript(script, packagesUri, patchedSdk, verbose); | |
72 } catch (err, stack) { | |
73 return new CompilationResult.crash(err, stack); | |
74 } | |
75 } | |
76 | |
77 class _CompilationOk extends CompilationResult { | |
78 final Uint8List bytes; | |
79 | |
80 _CompilationOk(this.bytes) : super._(); | |
81 | |
82 @override | |
83 Status get status => Status.ok; | |
84 | |
85 @override | |
86 get payload => bytes; | |
87 | |
88 String toString() => "_CompilationOk(${bytes.length} bytes)"; | |
89 } | |
90 | |
91 abstract class _CompilationFail extends CompilationResult { | |
92 _CompilationFail() : super._(); | |
93 | |
94 String get errorString; | |
95 | |
96 @override | |
97 get payload => errorString; | |
98 } | |
99 | |
100 class _CompilationError extends _CompilationFail { | |
101 final List<String> errors; | |
102 | |
103 _CompilationError(this.errors); | |
104 | |
105 @override | |
106 Status get status => Status.error; | |
107 | |
108 @override | |
109 String get errorString => errors.take(10).join('\n'); | |
110 | |
111 String toString() => "_CompilationError(${errorString})"; | |
112 } | |
113 | |
114 class _CompilationCrash extends _CompilationFail { | |
115 final Object exception; | |
116 final StackTrace stack; | |
117 | |
118 _CompilationCrash(this.exception, this.stack); | |
119 | |
120 @override | |
121 Status get status => Status.crash; | |
122 | |
123 @override | |
124 String get errorString => "${exception}\n${stack}"; | |
125 | |
126 String toString() => "_CompilationCrash(${errorString})"; | |
127 } | |
128 | |
129 /// This duplicates functionality from the Loader which we can't easily | |
130 /// access from here. | |
131 Future<Uri> _findPackagesFile(Uri base) async { | |
132 var dir = new File.fromUri(base).parent; | |
133 while (true) { | |
134 final packagesFile = dir.uri.resolve(".packages"); | |
135 if (await new File.fromUri(packagesFile).exists()) { | |
136 return packagesFile; | |
137 } | |
138 if (dir.parent.path == dir.path) { | |
139 break; | |
140 } | |
141 dir = dir.parent; | |
142 } | |
143 return null; | |
144 } | |
OLD | NEW |