OLD | NEW |
(Empty) | |
| 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 |
| 3 // BSD-style license that can be found in the LICENSE file. |
| 4 |
| 5 #include "bin/dfe.h" |
| 6 #include "bin/dartutils.h" |
| 7 |
| 8 namespace dart { |
| 9 namespace bin { |
| 10 |
| 11 DFE::DFE() : frontend_filename_(NULL), platform_binary_filename_(NULL) {} |
| 12 |
| 13 |
| 14 DFE::~DFE() { |
| 15 frontend_filename_ = NULL; |
| 16 platform_binary_filename_ = NULL; |
| 17 } |
| 18 |
| 19 Dart_Handle DFE::ReloadScript(Dart_Isolate isolate, Dart_Handle url) { |
| 20 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 |
| 27 // skip the compilation step and directly reload the file. |
| 28 const uint8_t* kernel_ir = NULL; |
| 29 intptr_t kernel_ir_size = -1; |
| 30 if (!TryReadKernelFile(url_string, &kernel_ir, &kernel_ir_size)) { |
| 31 // 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 |
| 33 // that have changed. For now just pass in the main url_string and have it |
| 34 // recompile the script. |
| 35 Dart_KernelCompilationResult kresult = Dart_CompileToKernel(url_string); |
| 36 if (kresult.status != Dart_KernelCompilationStatus_Ok) { |
| 37 return Dart_NewApiError(kresult.error); |
| 38 } |
| 39 kernel_ir = kresult.kernel; |
| 40 kernel_ir_size = kresult.kernel_size; |
| 41 } |
| 42 void* kernel_program = Dart_ReadKernelBinary(kernel_ir, kernel_ir_size); |
| 43 ASSERT(kernel_program != NULL); |
| 44 result = Dart_LoadKernel(kernel_program); |
| 45 if (Dart_IsError(result)) { |
| 46 return result; |
| 47 } |
| 48 // Finalize loading. This will complete any futures for completed deferred |
| 49 // loads. |
| 50 result = Dart_FinalizeLoading(true); |
| 51 if (Dart_IsError(result)) { |
| 52 return result; |
| 53 } |
| 54 return Dart_Null(); |
| 55 } |
| 56 |
| 57 |
| 58 bool DFE::TryReadKernelFile(const char* script_uri, |
| 59 const uint8_t** kernel_ir, |
| 60 intptr_t* kernel_ir_size) { |
| 61 *kernel_ir = NULL; |
| 62 *kernel_ir_size = -1; |
| 63 void* script_file = DartUtils::OpenFile(script_uri, false); |
| 64 if (script_file != NULL) { |
| 65 const uint8_t* buffer = NULL; |
| 66 DartUtils::ReadFile(&buffer, kernel_ir_size, script_file); |
| 67 DartUtils::CloseFile(script_file); |
| 68 if (*kernel_ir_size > 0 && buffer != NULL) { |
| 69 // We need a temporary variable because SniffForMagicNumber modifies the |
| 70 // buffer pointer to skip snapshot magic number. |
| 71 const uint8_t* temp = buffer; |
| 72 if (DartUtils::SniffForMagicNumber(&temp, kernel_ir_size) != |
| 73 DartUtils::kKernelMagicNumber) { |
| 74 free(const_cast<uint8_t*>(buffer)); |
| 75 *kernel_ir = NULL; |
| 76 *kernel_ir_size = -1; |
| 77 return false; |
| 78 } else { |
| 79 // Do not free buffer if this is a kernel file - kernel_file will be |
| 80 // backed by the same memory as the buffer and caller will own it. |
| 81 // Caller is responsible for freeing the buffer when this function |
| 82 // returns true. |
| 83 *kernel_ir = buffer; |
| 84 return true; |
| 85 } |
| 86 } |
| 87 } |
| 88 return false; |
| 89 } |
| 90 |
| 91 |
| 92 } // namespace bin |
| 93 } // namespace dart |
OLD | NEW |