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 bool verbose = false; |
| 20 |
| 21 final RawReceivePort scriptLoadPort = new RawReceivePort(); |
| 22 |
| 23 |
| 24 bool checkIsFile(String path) { |
| 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. |
| 55 } |
| 56 } |
| 57 |
| 58 |
| 59 List writeProgramToBuffer(Program program) { |
| 60 var sink = new DataSink(); |
| 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."; |
| 74 } |
| 75 |
| 76 if (!checkSdkDirectory(sdkPath)) { |
| 77 throw "Patched sdk directory not found at $sdkPath"; |
| 78 } |
| 79 |
| 80 Target target = getTarget("vm", new TargetFlags(strongMode: false)); |
| 81 DartOptions dartOptions = new DartOptions( |
| 82 strongMode: false, |
| 83 strongModeSdk: false, |
| 84 sdk: sdkPath, |
| 85 packagePath: packageConfig, |
| 86 customUriMappings: {}, |
| 87 declaredVariables: {}); |
| 88 DartLoader loader = |
| 89 await new DartLoaderBatch().getLoader(new Repository(), dartOptions); |
| 90 var program = loader.loadProgram(fileName, target: target); |
| 91 |
| 92 var errors = loader.errors; |
| 93 if (errors.isNotEmpty) { |
| 94 throw loader.errors.first; |
| 95 } |
| 96 |
| 97 // Link program into one file, cf. --link option in dartk |
| 98 target.transformProgram(program); |
| 99 |
| 100 return writeProgramToBuffer(program); |
| 101 } |
| 102 |
| 103 |
| 104 _processLoadRequest(request) { |
| 105 if (verbose) { |
| 106 print("FROM DART KERNEL: load request: $request"); |
| 107 print("FROM DART KERNEL: package: ${Platform.packageConfig}"); |
| 108 print("FROM DART KERNEL: exec: ${Platform.resolvedExecutable}"); |
| 109 } |
| 110 int tag = request[0]; |
| 111 SendPort sp = request[1]; |
| 112 String inputFileUrl = request[2]; |
| 113 Uri scriptUri = Uri.parse(inputFileUrl); |
| 114 Uri packagesUri = Uri.parse(Platform.packageConfig ?? ".packages"); |
| 115 Uri patched_sdk = Uri.parse(Platform.resolvedExecutable).resolve("patched_sdk"
); |
| 116 |
| 117 var parsingDone = parseScript(scriptUri, packagesUri.path, patched_sdk.path); |
| 118 |
| 119 parsingDone |
| 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 } |
| 140 |
| 141 |
| 142 main() { |
| 143 scriptLoadPort.handler = _processLoadRequest; |
| 144 Timer.run(() {}); |
| 145 return scriptLoadPort; |
| 146 } |
OLD | NEW |