| 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" |
| 8 |
| 9 #include "vm/kernel.h" |
| 7 | 10 |
| 8 namespace dart { | 11 namespace dart { |
| 9 namespace bin { | 12 namespace bin { |
| 10 | 13 |
| 11 DFE::DFE() : frontend_filename_(NULL), platform_binary_filename_(NULL) {} | 14 DFE::DFE() |
| 15 : frontend_filename_(NULL), |
| 16 platform_binary_filename_(NULL), |
| 17 kernel_platform_(NULL) {} |
| 12 | 18 |
| 13 | 19 |
| 14 DFE::~DFE() { | 20 DFE::~DFE() { |
| 15 frontend_filename_ = NULL; | 21 frontend_filename_ = NULL; |
| 16 platform_binary_filename_ = NULL; | 22 platform_binary_filename_ = NULL; |
| 23 if (kernel_platform_ != NULL) { |
| 24 delete reinterpret_cast<kernel::Program*>(kernel_platform_); |
| 25 } |
| 26 kernel_platform_ = NULL; |
| 17 } | 27 } |
| 18 | 28 |
| 19 Dart_Handle DFE::ReloadScript(Dart_Isolate isolate, Dart_Handle url) { | 29 Dart_Handle DFE::ReloadScript(Dart_Isolate isolate, const char* url_string) { |
| 20 ASSERT(!Dart_IsServiceIsolate(isolate) && !Dart_IsKernelIsolate(isolate)); | 30 ASSERT(!Dart_IsServiceIsolate(isolate) && !Dart_IsKernelIsolate(isolate)); |
| 21 const char* url_string = NULL; | |
| 22 Dart_Handle result = Dart_StringToCString(url, &url_string); | |
| 23 if (Dart_IsError(result)) { | |
| 24 return result; | |
| 25 } | |
| 26 // First check if the URL points to a Kernel IR file in which case we | 31 // First check if the URL points to a Kernel IR file in which case we |
| 27 // skip the compilation step and directly reload the file. | 32 // skip the compilation step and directly reload the file. |
| 28 const uint8_t* kernel_ir = NULL; | 33 const uint8_t* kernel_ir = NULL; |
| 29 intptr_t kernel_ir_size = -1; | 34 intptr_t kernel_ir_size = -1; |
| 30 if (!TryReadKernelFile(url_string, &kernel_ir, &kernel_ir_size)) { | 35 if (!TryReadKernelFile(url_string, &kernel_ir, &kernel_ir_size)) { |
| 31 // We have a source file, compile it into a kernel ir first. | 36 // We have a source file, compile it into a kernel ir first. |
| 32 // TODO(asiva): We will have to change this API to pass in a list of files | 37 // TODO(asiva): We will have to change this API to pass in a list of files |
| 33 // that have changed. For now just pass in the main url_string and have it | 38 // that have changed. For now just pass in the main url_string and have it |
| 34 // recompile the script. | 39 // recompile the script. |
| 35 Dart_KernelCompilationResult kresult = Dart_CompileToKernel(url_string); | 40 Dart_KernelCompilationResult kresult = Dart_CompileToKernel(url_string); |
| 36 if (kresult.status != Dart_KernelCompilationStatus_Ok) { | 41 if (kresult.status != Dart_KernelCompilationStatus_Ok) { |
| 37 return Dart_NewApiError(kresult.error); | 42 return Dart_NewApiError(kresult.error); |
| 38 } | 43 } |
| 39 kernel_ir = kresult.kernel; | 44 kernel_ir = kresult.kernel; |
| 40 kernel_ir_size = kresult.kernel_size; | 45 kernel_ir_size = kresult.kernel_size; |
| 41 } | 46 } |
| 42 void* kernel_program = Dart_ReadKernelBinary(kernel_ir, kernel_ir_size); | 47 void* kernel_program = Dart_ReadKernelBinary(kernel_ir, kernel_ir_size); |
| 43 ASSERT(kernel_program != NULL); | 48 ASSERT(kernel_program != NULL); |
| 44 result = Dart_LoadKernel(kernel_program); | 49 Dart_Handle result = Dart_LoadKernel(kernel_program); |
| 45 if (Dart_IsError(result)) { | 50 if (Dart_IsError(result)) { |
| 46 return result; | 51 return result; |
| 47 } | 52 } |
| 48 // Finalize loading. This will complete any futures for completed deferred | 53 // Finalize loading. This will complete any futures for completed deferred |
| 49 // loads. | 54 // loads. |
| 50 result = Dart_FinalizeLoading(true); | 55 result = Dart_FinalizeLoading(true); |
| 51 if (Dart_IsError(result)) { | 56 if (Dart_IsError(result)) { |
| 52 return result; | 57 return result; |
| 53 } | 58 } |
| 54 return Dart_Null(); | 59 return Dart_Null(); |
| 55 } | 60 } |
| 56 | 61 |
| 57 | 62 |
| 63 void* DFE::CompileAndReadScript(const char* script_uri, |
| 64 char** error, |
| 65 int* exit_code) { |
| 66 Dart_KernelCompilationResult result = Dart_CompileToKernel(script_uri); |
| 67 switch (result.status) { |
| 68 case Dart_KernelCompilationStatus_Ok: |
| 69 return Dart_ReadKernelBinary(result.kernel, result.kernel_size); |
| 70 case Dart_KernelCompilationStatus_Error: |
| 71 *error = result.error; // Copy error message. |
| 72 *exit_code = kCompilationErrorExitCode; |
| 73 break; |
| 74 case Dart_KernelCompilationStatus_Crash: |
| 75 *error = result.error; // Copy error message. |
| 76 *exit_code = kDartFrontendErrorExitCode; |
| 77 break; |
| 78 case Dart_KernelCompilationStatus_Unknown: |
| 79 *error = result.error; // Copy error message. |
| 80 *exit_code = kErrorExitCode; |
| 81 break; |
| 82 } |
| 83 return NULL; |
| 84 } |
| 85 |
| 86 |
| 87 void* DFE::ReadPlatform() { |
| 88 const uint8_t* buffer = NULL; |
| 89 intptr_t buffer_length = -1; |
| 90 bool result = |
| 91 TryReadKernelFile(platform_binary_filename_, &buffer, &buffer_length); |
| 92 if (result) { |
| 93 kernel_platform_ = Dart_ReadKernelBinary(buffer, buffer_length); |
| 94 return kernel_platform_; |
| 95 } |
| 96 return NULL; |
| 97 } |
| 98 |
| 99 |
| 100 void* DFE::ReadScript(const char* script_uri) { |
| 101 const uint8_t* buffer = NULL; |
| 102 intptr_t buffer_length = -1; |
| 103 bool result = TryReadKernelFile(script_uri, &buffer, &buffer_length); |
| 104 if (result) { |
| 105 return Dart_ReadKernelBinary(buffer, buffer_length); |
| 106 } |
| 107 return NULL; |
| 108 } |
| 109 |
| 110 |
| 58 bool DFE::TryReadKernelFile(const char* script_uri, | 111 bool DFE::TryReadKernelFile(const char* script_uri, |
| 59 const uint8_t** kernel_ir, | 112 const uint8_t** kernel_ir, |
| 60 intptr_t* kernel_ir_size) { | 113 intptr_t* kernel_ir_size) { |
| 61 *kernel_ir = NULL; | 114 *kernel_ir = NULL; |
| 62 *kernel_ir_size = -1; | 115 *kernel_ir_size = -1; |
| 63 void* script_file = DartUtils::OpenFile(script_uri, false); | 116 void* script_file = DartUtils::OpenFile(script_uri, false); |
| 64 if (script_file != NULL) { | 117 if (script_file != NULL) { |
| 65 const uint8_t* buffer = NULL; | 118 const uint8_t* buffer = NULL; |
| 66 DartUtils::ReadFile(&buffer, kernel_ir_size, script_file); | 119 DartUtils::ReadFile(&buffer, kernel_ir_size, script_file); |
| 67 DartUtils::CloseFile(script_file); | 120 DartUtils::CloseFile(script_file); |
| (...skipping 16 matching lines...) Expand all Loading... |
| 84 return true; | 137 return true; |
| 85 } | 138 } |
| 86 } | 139 } |
| 87 } | 140 } |
| 88 return false; | 141 return false; |
| 89 } | 142 } |
| 90 | 143 |
| 91 | 144 |
| 92 } // namespace bin | 145 } // namespace bin |
| 93 } // namespace dart | 146 } // namespace dart |
| OLD | NEW |