OLD | NEW |
1 // Copyright (c) 2017, the Dart project authors. Please see the AUTHORS file | 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 | 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 #include "bin/dfe.h" | 5 #include "bin/dfe.h" |
6 #include "bin/dartutils.h" | 6 #include "bin/dartutils.h" |
7 #include "bin/error_exit.h" | 7 #include "bin/error_exit.h" |
8 #include "bin/file.h" | 8 #include "bin/file.h" |
9 | 9 |
10 #include "vm/kernel.h" | 10 #include "vm/kernel.h" |
11 | 11 |
12 namespace dart { | 12 namespace dart { |
13 namespace bin { | 13 namespace bin { |
14 | 14 |
15 const char kPlatformBinaryName[] = "platform.dill"; | 15 const char kPlatformBinaryName[] = "platform.dill"; |
16 const char kVMServiceIOBinaryName[] = "vmservice_io.dill"; | 16 const char kVMServiceIOBinaryName[] = "vmservice_io.dill"; |
17 | 17 |
18 | |
19 DFE::DFE() | 18 DFE::DFE() |
20 : frontend_filename_(NULL), | 19 : frontend_filename_(NULL), |
21 platform_binary_filename_(NULL), | 20 platform_binary_filename_(NULL), |
22 vmservice_io_binary_filename_(NULL), | 21 vmservice_io_binary_filename_(NULL), |
23 kernel_platform_(NULL), | 22 kernel_platform_(NULL), |
24 kernel_vmservice_io_(NULL) {} | 23 kernel_vmservice_io_(NULL) {} |
25 | 24 |
26 | |
27 DFE::~DFE() { | 25 DFE::~DFE() { |
28 frontend_filename_ = NULL; | 26 frontend_filename_ = NULL; |
29 | 27 |
30 if (platform_binary_filename_ != NULL) { | 28 if (platform_binary_filename_ != NULL) { |
31 delete platform_binary_filename_; | 29 delete platform_binary_filename_; |
32 platform_binary_filename_ = NULL; | 30 platform_binary_filename_ = NULL; |
33 } | 31 } |
34 | 32 |
35 if (kernel_platform_ != NULL) { | 33 if (kernel_platform_ != NULL) { |
36 delete reinterpret_cast<kernel::Program*>(kernel_platform_); | 34 delete reinterpret_cast<kernel::Program*>(kernel_platform_); |
37 kernel_platform_ = NULL; | 35 kernel_platform_ = NULL; |
38 } | 36 } |
39 | 37 |
40 if (kernel_vmservice_io_ != NULL) { | 38 if (kernel_vmservice_io_ != NULL) { |
41 delete reinterpret_cast<kernel::Program*>(kernel_vmservice_io_); | 39 delete reinterpret_cast<kernel::Program*>(kernel_vmservice_io_); |
42 kernel_vmservice_io_ = NULL; | 40 kernel_vmservice_io_ = NULL; |
43 } | 41 } |
44 } | 42 } |
45 | 43 |
46 | |
47 void DFE::SetKernelBinaries(const char* name) { | 44 void DFE::SetKernelBinaries(const char* name) { |
48 intptr_t len = snprintf(NULL, 0, "%s%s%s", name, File::PathSeparator(), | 45 intptr_t len = snprintf(NULL, 0, "%s%s%s", name, File::PathSeparator(), |
49 kPlatformBinaryName) + | 46 kPlatformBinaryName) + |
50 1; | 47 1; |
51 platform_binary_filename_ = new char[len]; | 48 platform_binary_filename_ = new char[len]; |
52 snprintf(platform_binary_filename_, len, "%s%s%s", name, | 49 snprintf(platform_binary_filename_, len, "%s%s%s", name, |
53 File::PathSeparator(), kPlatformBinaryName); | 50 File::PathSeparator(), kPlatformBinaryName); |
54 | 51 |
55 len = snprintf(NULL, 0, "%s%s%s", name, File::PathSeparator(), | 52 len = snprintf(NULL, 0, "%s%s%s", name, File::PathSeparator(), |
56 kVMServiceIOBinaryName) + | 53 kVMServiceIOBinaryName) + |
57 1; | 54 1; |
58 vmservice_io_binary_filename_ = new char[len]; | 55 vmservice_io_binary_filename_ = new char[len]; |
59 snprintf(vmservice_io_binary_filename_, len, "%s%s%s", name, | 56 snprintf(vmservice_io_binary_filename_, len, "%s%s%s", name, |
60 File::PathSeparator(), kVMServiceIOBinaryName); | 57 File::PathSeparator(), kVMServiceIOBinaryName); |
61 } | 58 } |
62 | 59 |
63 | |
64 Dart_Handle DFE::ReloadScript(Dart_Isolate isolate, const char* url_string) { | 60 Dart_Handle DFE::ReloadScript(Dart_Isolate isolate, const char* url_string) { |
65 ASSERT(!Dart_IsServiceIsolate(isolate) && !Dart_IsKernelIsolate(isolate)); | 61 ASSERT(!Dart_IsServiceIsolate(isolate) && !Dart_IsKernelIsolate(isolate)); |
66 // First check if the URL points to a Kernel IR file in which case we | 62 // First check if the URL points to a Kernel IR file in which case we |
67 // skip the compilation step and directly reload the file. | 63 // skip the compilation step and directly reload the file. |
68 const uint8_t* kernel_ir = NULL; | 64 const uint8_t* kernel_ir = NULL; |
69 intptr_t kernel_ir_size = -1; | 65 intptr_t kernel_ir_size = -1; |
70 if (!TryReadKernelFile(url_string, &kernel_ir, &kernel_ir_size)) { | 66 if (!TryReadKernelFile(url_string, &kernel_ir, &kernel_ir_size)) { |
71 // We have a source file, compile it into a kernel ir first. | 67 // We have a source file, compile it into a kernel ir first. |
72 // TODO(asiva): We will have to change this API to pass in a list of files | 68 // TODO(asiva): We will have to change this API to pass in a list of files |
73 // that have changed. For now just pass in the main url_string and have it | 69 // that have changed. For now just pass in the main url_string and have it |
(...skipping 13 matching lines...) Expand all Loading... |
87 } | 83 } |
88 // Finalize loading. This will complete any futures for completed deferred | 84 // Finalize loading. This will complete any futures for completed deferred |
89 // loads. | 85 // loads. |
90 result = Dart_FinalizeLoading(true); | 86 result = Dart_FinalizeLoading(true); |
91 if (Dart_IsError(result)) { | 87 if (Dart_IsError(result)) { |
92 return result; | 88 return result; |
93 } | 89 } |
94 return Dart_Null(); | 90 return Dart_Null(); |
95 } | 91 } |
96 | 92 |
97 | |
98 void* DFE::CompileAndReadScript(const char* script_uri, | 93 void* DFE::CompileAndReadScript(const char* script_uri, |
99 char** error, | 94 char** error, |
100 int* exit_code) { | 95 int* exit_code) { |
101 Dart_KernelCompilationResult result = Dart_CompileToKernel(script_uri); | 96 Dart_KernelCompilationResult result = Dart_CompileToKernel(script_uri); |
102 switch (result.status) { | 97 switch (result.status) { |
103 case Dart_KernelCompilationStatus_Ok: | 98 case Dart_KernelCompilationStatus_Ok: |
104 return Dart_ReadKernelBinary(result.kernel, result.kernel_size); | 99 return Dart_ReadKernelBinary(result.kernel, result.kernel_size); |
105 case Dart_KernelCompilationStatus_Error: | 100 case Dart_KernelCompilationStatus_Error: |
106 *error = result.error; // Copy error message. | 101 *error = result.error; // Copy error message. |
107 *exit_code = kCompilationErrorExitCode; | 102 *exit_code = kCompilationErrorExitCode; |
108 break; | 103 break; |
109 case Dart_KernelCompilationStatus_Crash: | 104 case Dart_KernelCompilationStatus_Crash: |
110 *error = result.error; // Copy error message. | 105 *error = result.error; // Copy error message. |
111 *exit_code = kDartFrontendErrorExitCode; | 106 *exit_code = kDartFrontendErrorExitCode; |
112 break; | 107 break; |
113 case Dart_KernelCompilationStatus_Unknown: | 108 case Dart_KernelCompilationStatus_Unknown: |
114 *error = result.error; // Copy error message. | 109 *error = result.error; // Copy error message. |
115 *exit_code = kErrorExitCode; | 110 *exit_code = kErrorExitCode; |
116 break; | 111 break; |
117 } | 112 } |
118 return NULL; | 113 return NULL; |
119 } | 114 } |
120 | 115 |
121 | |
122 void* DFE::ReadPlatform() { | 116 void* DFE::ReadPlatform() { |
123 return kernel_platform_ = ReadScript(platform_binary_filename_); | 117 return kernel_platform_ = ReadScript(platform_binary_filename_); |
124 } | 118 } |
125 | 119 |
126 | |
127 void* DFE::ReadVMServiceIO() { | 120 void* DFE::ReadVMServiceIO() { |
128 return kernel_vmservice_io_ = ReadScript(vmservice_io_binary_filename_); | 121 return kernel_vmservice_io_ = ReadScript(vmservice_io_binary_filename_); |
129 } | 122 } |
130 | 123 |
131 | |
132 void* DFE::ReadScript(const char* script_uri) { | 124 void* DFE::ReadScript(const char* script_uri) { |
133 const uint8_t* buffer = NULL; | 125 const uint8_t* buffer = NULL; |
134 intptr_t buffer_length = -1; | 126 intptr_t buffer_length = -1; |
135 bool result = TryReadKernelFile(script_uri, &buffer, &buffer_length); | 127 bool result = TryReadKernelFile(script_uri, &buffer, &buffer_length); |
136 if (result) { | 128 if (result) { |
137 return Dart_ReadKernelBinary(buffer, buffer_length); | 129 return Dart_ReadKernelBinary(buffer, buffer_length); |
138 } | 130 } |
139 return NULL; | 131 return NULL; |
140 } | 132 } |
141 | 133 |
142 | |
143 bool DFE::TryReadKernelFile(const char* script_uri, | 134 bool DFE::TryReadKernelFile(const char* script_uri, |
144 const uint8_t** kernel_ir, | 135 const uint8_t** kernel_ir, |
145 intptr_t* kernel_ir_size) { | 136 intptr_t* kernel_ir_size) { |
146 *kernel_ir = NULL; | 137 *kernel_ir = NULL; |
147 *kernel_ir_size = -1; | 138 *kernel_ir_size = -1; |
148 void* script_file = DartUtils::OpenFile(script_uri, false); | 139 void* script_file = DartUtils::OpenFile(script_uri, false); |
149 if (script_file != NULL) { | 140 if (script_file != NULL) { |
150 const uint8_t* buffer = NULL; | 141 const uint8_t* buffer = NULL; |
151 DartUtils::ReadFile(&buffer, kernel_ir_size, script_file); | 142 DartUtils::ReadFile(&buffer, kernel_ir_size, script_file); |
152 DartUtils::CloseFile(script_file); | 143 DartUtils::CloseFile(script_file); |
(...skipping 13 matching lines...) Expand all Loading... |
166 // Caller is responsible for freeing the buffer when this function | 157 // Caller is responsible for freeing the buffer when this function |
167 // returns true. | 158 // returns true. |
168 *kernel_ir = buffer; | 159 *kernel_ir = buffer; |
169 return true; | 160 return true; |
170 } | 161 } |
171 } | 162 } |
172 } | 163 } |
173 return false; | 164 return false; |
174 } | 165 } |
175 | 166 |
176 | |
177 } // namespace bin | 167 } // namespace bin |
178 } // namespace dart | 168 } // namespace dart |
OLD | NEW |