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 | 4 |
5 // This is an interface to the Dart Kernel parser and Kernel binary generator. | 5 // This is an interface to the Dart Kernel parser and Kernel binary generator. |
6 // It is used by the kernel-isolate to load Dart source code and generate | 6 // It is used by the kernel-isolate to load Dart source code and generate |
7 // Kernel binary format. | 7 // Kernel binary format. |
8 | 8 |
9 import 'dart:isolate'; | 9 import 'dart:isolate'; |
10 import 'dart:async'; | 10 import 'dart:async'; |
11 import "dart:io"; | 11 import 'dart:io'; |
12 import "dart:typed_data"; | 12 import 'dart:typed_data'; |
13 | 13 |
14 import 'package:kernel/binary/ast_to_binary.dart'; | 14 import 'package:kernel/binary/ast_to_binary.dart'; |
15 import 'package:kernel/analyzer/loader.dart'; | 15 import 'package:kernel/analyzer/loader.dart'; |
16 import 'package:kernel/kernel.dart'; | 16 import 'package:kernel/kernel.dart'; |
17 import 'package:kernel/target/targets.dart'; | 17 import 'package:kernel/target/targets.dart'; |
18 | 18 |
19 bool verbose = false; | 19 const verbose = false; |
20 | 20 |
21 final RawReceivePort scriptLoadPort = new RawReceivePort(); | 21 class DataSink implements Sink<List<int>> { |
| 22 final BytesBuilder builder = new BytesBuilder(); |
22 | 23 |
| 24 void add(List<int> data) { |
| 25 builder.add(data); |
| 26 } |
23 | 27 |
24 bool checkIsFile(String path) { | 28 void close() { |
25 var stat = new File(path).statSync(); | |
26 switch (stat.type) { | |
27 case FileSystemEntityType.DIRECTORY: | |
28 return false; | |
29 case FileSystemEntityType.NOT_FOUND: | |
30 return false; | |
31 } | |
32 return true; | |
33 } | |
34 | |
35 | |
36 void checkSdkDirectory(String path) { | |
37 var stat = new File(path).statSync(); | |
38 switch (stat.type) { | |
39 case FileSystemEntityType.DIRECTORY: | |
40 case FileSystemEntityType.LINK: | |
41 return true; | |
42 default: | |
43 return false; | |
44 } | |
45 } | |
46 | |
47 | |
48 class DataSink implements StreamSink<List<int>> { | |
49 var buffer = []; | |
50 add(List<int> data) { | |
51 buffer.addAll(data); | |
52 } | |
53 close() { | |
54 // Nothing to do. | 29 // Nothing to do. |
55 } | 30 } |
56 } | 31 } |
57 | 32 |
58 | 33 Future<Uint8List> parseScript( |
59 List writeProgramToBuffer(Program program) { | 34 Uri fileName, String packageConfig, String sdkPath) async { |
60 var sink = new DataSink(); | 35 if (!FileSystemEntity.isFileSync(fileName.path)) { |
61 try { | |
62 new BinaryPrinter(sink).writeProgramFile(program); | |
63 } finally { | |
64 sink.close(); | |
65 } | |
66 return new Uint8List.fromList(sink.buffer); | |
67 } | |
68 | |
69 | |
70 Future parseScript(Uri fileName, String packageConfig, String sdkPath) async { | |
71 | |
72 if (!checkIsFile(fileName.path)) { | |
73 throw "Input file '${fileName.path}' does not exist."; | 36 throw "Input file '${fileName.path}' does not exist."; |
74 } | 37 } |
75 | 38 |
76 if (!checkSdkDirectory(sdkPath)) { | 39 if (!FileSystemEntity.isDirectorySync(sdkPath)) { |
77 throw "Patched sdk directory not found at $sdkPath"; | 40 throw "Patched sdk directory not found at $sdkPath"; |
78 } | 41 } |
79 | 42 |
80 Target target = getTarget("vm", new TargetFlags(strongMode: false)); | 43 Target target = getTarget("vm", new TargetFlags(strongMode: false)); |
81 DartOptions dartOptions = new DartOptions( | 44 DartOptions dartOptions = new DartOptions( |
82 strongMode: false, | 45 strongMode: false, |
83 strongModeSdk: false, | 46 strongModeSdk: false, |
84 sdk: sdkPath, | 47 sdk: sdkPath, |
85 packagePath: packageConfig, | 48 packagePath: packageConfig, |
86 customUriMappings: {}, | 49 customUriMappings: const {}, |
87 declaredVariables: {}); | 50 declaredVariables: const {}); |
88 DartLoader loader = | 51 DartLoader loader = |
89 await new DartLoaderBatch().getLoader(new Repository(), dartOptions); | 52 await new DartLoaderBatch().getLoader(new Repository(), dartOptions); |
90 var program = loader.loadProgram(fileName, target: target); | 53 var program = loader.loadProgram(fileName, target: target); |
91 | 54 |
92 var errors = loader.errors; | 55 var errors = loader.errors; |
93 if (errors.isNotEmpty) { | 56 if (errors.isNotEmpty) { |
94 throw loader.errors.first; | 57 throw loader.errors.first; |
95 } | 58 } |
96 | 59 |
97 // Link program into one file, cf. --link option in dartk | 60 // Link program into one file, cf. --link option in dartk. |
98 target.transformProgram(program); | 61 target.transformProgram(program); |
99 | 62 |
100 return writeProgramToBuffer(program); | 63 // Write the program to a list of bytes and return it. |
| 64 var sink = new DataSink(); |
| 65 new BinaryPrinter(sink).writeProgramFile(program); |
| 66 return sink.builder.takeBytes(); |
101 } | 67 } |
102 | 68 |
103 | 69 Future _processLoadRequest(request) async { |
104 _processLoadRequest(request) { | |
105 if (verbose) { | 70 if (verbose) { |
106 print("FROM DART KERNEL: load request: $request"); | 71 print("FROM DART KERNEL: load request: $request"); |
107 print("FROM DART KERNEL: package: ${Platform.packageConfig}"); | 72 print("FROM DART KERNEL: package: ${Platform.packageConfig}"); |
108 print("FROM DART KERNEL: exec: ${Platform.resolvedExecutable}"); | 73 print("FROM DART KERNEL: exec: ${Platform.resolvedExecutable}"); |
109 } | 74 } |
| 75 |
110 int tag = request[0]; | 76 int tag = request[0]; |
111 SendPort sp = request[1]; | 77 SendPort port = request[1]; |
112 String inputFileUrl = request[2]; | 78 String inputFileUrl = request[2]; |
113 Uri scriptUri = Uri.parse(inputFileUrl); | 79 Uri scriptUri = Uri.parse(inputFileUrl); |
114 Uri packagesUri = Uri.parse(Platform.packageConfig ?? ".packages"); | 80 Uri packagesUri = Uri.parse(Platform.packageConfig ?? ".packages"); |
115 Uri patched_sdk = Uri.parse(Platform.resolvedExecutable).resolve("patched_sdk"
); | 81 Uri patchedSdk = |
| 82 Uri.parse(Platform.resolvedExecutable).resolve("patched_sdk"); |
116 | 83 |
117 var parsingDone = parseScript(scriptUri, packagesUri.path, patched_sdk.path); | 84 var result; |
| 85 try { |
| 86 result = await parseScript(scriptUri, packagesUri.path, patchedSdk.path); |
| 87 } catch (error) { |
| 88 tag = -tag; // Mark reply as an exception. |
| 89 result = error.toString(); |
| 90 } |
118 | 91 |
119 parsingDone | 92 port.send([tag, inputFileUrl, inputFileUrl, null, result]); |
120 .then((data) { | |
121 var msg = new List(5); | |
122 msg[0] = tag; | |
123 msg[1] = inputFileUrl; | |
124 msg[2] = inputFileUrl; | |
125 msg[3] = null; | |
126 msg[4] = data; | |
127 sp.send(msg); | |
128 return; | |
129 }) | |
130 .catchError((e) { | |
131 var msg = new List(5); | |
132 msg[0] = -tag; | |
133 msg[1] = inputFileUrl; | |
134 msg[2] = inputFileUrl; | |
135 msg[3] = null; | |
136 msg[4] = e.toString(); | |
137 sp.send(msg); | |
138 }); | |
139 } | 93 } |
140 | 94 |
141 | 95 main() => new RawReceivePort()..handler = _processLoadRequest; |
142 main() { | |
143 scriptLoadPort.handler = _processLoadRequest; | |
144 Timer.run(() {}); | |
145 return scriptLoadPort; | |
146 } | |
OLD | NEW |