OLD | NEW |
| (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 | |
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 | |
7 // Kernel binary format. | |
8 | |
9 import 'dart:isolate'; | |
10 import 'dart:async'; | |
11 import 'dart:io'; | |
12 import 'dart:typed_data'; | |
13 | |
14 import 'package:kernel/binary/ast_to_binary.dart'; | |
15 import 'package:kernel/analyzer/loader.dart'; | |
16 import 'package:kernel/kernel.dart'; | |
17 import 'package:kernel/target/targets.dart'; | |
18 | |
19 const verbose = false; | |
20 | |
21 class DataSink implements Sink<List<int>> { | |
22 final BytesBuilder builder = new BytesBuilder(); | |
23 | |
24 void add(List<int> data) { | |
25 builder.add(data); | |
26 } | |
27 | |
28 void close() { | |
29 // Nothing to do. | |
30 } | |
31 } | |
32 | |
33 Future<Uint8List> parseScript( | |
34 Uri fileName, String packageConfig, String sdkPath) async { | |
35 if (!FileSystemEntity.isFileSync(fileName.path)) { | |
36 throw "Input file '${fileName.path}' does not exist."; | |
37 } | |
38 | |
39 if (!FileSystemEntity.isDirectorySync(sdkPath)) { | |
40 throw "Patched sdk directory not found at $sdkPath"; | |
41 } | |
42 | |
43 Target target = getTarget("vm", new TargetFlags(strongMode: false)); | |
44 DartOptions dartOptions = new DartOptions( | |
45 strongMode: false, | |
46 strongModeSdk: false, | |
47 sdk: sdkPath, | |
48 packagePath: packageConfig, | |
49 customUriMappings: const {}, | |
50 declaredVariables: const {}); | |
51 DartLoader loader = | |
52 await new DartLoaderBatch().getLoader(new Repository(), dartOptions); | |
53 var program = loader.loadProgram(fileName, target: target); | |
54 | |
55 var errors = loader.errors; | |
56 if (errors.isNotEmpty) { | |
57 throw loader.errors.first; | |
58 } | |
59 | |
60 // Link program into one file, cf. --link option in dartk. | |
61 target.transformProgram(program); | |
62 | |
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(); | |
67 } | |
68 | |
69 Future _processLoadRequest(request) async { | |
70 if (verbose) { | |
71 print("FROM DART KERNEL: load request: $request"); | |
72 print("FROM DART KERNEL: package: ${Platform.packageConfig}"); | |
73 print("FROM DART KERNEL: exec: ${Platform.resolvedExecutable}"); | |
74 } | |
75 | |
76 int tag = request[0]; | |
77 SendPort port = request[1]; | |
78 String inputFileUrl = request[2]; | |
79 Uri scriptUri = Uri.parse(inputFileUrl); | |
80 Uri packagesUri = Uri.parse(Platform.packageConfig ?? ".packages"); | |
81 Uri patchedSdk = | |
82 Uri.parse(Platform.resolvedExecutable).resolve("patched_sdk"); | |
83 | |
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 } | |
91 | |
92 port.send([tag, inputFileUrl, inputFileUrl, null, result]); | |
93 } | |
94 | |
95 main() => new RawReceivePort()..handler = _processLoadRequest; | |
OLD | NEW |