| OLD | NEW |
| 1 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file | 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 | 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 | 5 |
| 6 #include "bin/loader.h" | 6 #include "bin/loader.h" |
| 7 | 7 |
| 8 #include "bin/builtin.h" | 8 #include "bin/builtin.h" |
| 9 #include "bin/dartutils.h" | 9 #include "bin/dartutils.h" |
| 10 #include "bin/extensions.h" | 10 #include "bin/extensions.h" |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 59 free(results_); | 59 free(results_); |
| 60 results_ = NULL; | 60 results_ = NULL; |
| 61 payload_ = NULL; | 61 payload_ = NULL; |
| 62 payload_length_ = 0; | 62 payload_length_ = 0; |
| 63 } | 63 } |
| 64 | 64 |
| 65 | 65 |
| 66 // Copy the contents of |message| into an |IOResult|. | 66 // Copy the contents of |message| into an |IOResult|. |
| 67 void Loader::IOResult::Setup(Dart_CObject* message) { | 67 void Loader::IOResult::Setup(Dart_CObject* message) { |
| 68 ASSERT(message->type == Dart_CObject_kArray); | 68 ASSERT(message->type == Dart_CObject_kArray); |
| 69 ASSERT(message->value.as_array.length == 4); | 69 ASSERT(message->value.as_array.length == 5); |
| 70 Dart_CObject* tag_message = message->value.as_array.values[0]; | 70 Dart_CObject* tag_message = message->value.as_array.values[0]; |
| 71 ASSERT(tag_message != NULL); | 71 ASSERT(tag_message != NULL); |
| 72 Dart_CObject* uri_message = message->value.as_array.values[1]; | 72 Dart_CObject* uri_message = message->value.as_array.values[1]; |
| 73 ASSERT(uri_message != NULL); | 73 ASSERT(uri_message != NULL); |
| 74 Dart_CObject* library_uri_message = message->value.as_array.values[2]; | 74 Dart_CObject* resolved_uri_message = message->value.as_array.values[2]; |
| 75 ASSERT(resolved_uri_message != NULL); |
| 76 Dart_CObject* library_uri_message = message->value.as_array.values[3]; |
| 75 ASSERT(library_uri_message != NULL); | 77 ASSERT(library_uri_message != NULL); |
| 76 Dart_CObject* payload_message = message->value.as_array.values[3]; | 78 Dart_CObject* payload_message = message->value.as_array.values[4]; |
| 77 ASSERT(payload_message != NULL); | 79 ASSERT(payload_message != NULL); |
| 78 | 80 |
| 79 // Grab the tag. | 81 // Grab the tag. |
| 80 ASSERT(tag_message->type == Dart_CObject_kInt32); | 82 ASSERT(tag_message->type == Dart_CObject_kInt32); |
| 81 tag = tag_message->value.as_int32; | 83 tag = tag_message->value.as_int32; |
| 82 | 84 |
| 83 // Grab the uri id. | 85 // Grab the uri id. |
| 84 ASSERT(uri_message->type == Dart_CObject_kString); | 86 ASSERT(uri_message->type == Dart_CObject_kString); |
| 85 uri = strdup(uri_message->value.as_string); | 87 uri = strdup(uri_message->value.as_string); |
| 86 | 88 |
| 89 // Grab the resolved uri. |
| 90 ASSERT(resolved_uri_message->type == Dart_CObject_kString); |
| 91 resolved_uri = strdup(resolved_uri_message->value.as_string); |
| 92 |
| 87 // Grab the library uri if one is present. | 93 // Grab the library uri if one is present. |
| 88 if (library_uri_message->type != Dart_CObject_kNull) { | 94 if (library_uri_message->type != Dart_CObject_kNull) { |
| 89 ASSERT(library_uri_message->type == Dart_CObject_kString); | 95 ASSERT(library_uri_message->type == Dart_CObject_kString); |
| 90 library_uri = strdup(library_uri_message->value.as_string); | 96 library_uri = strdup(library_uri_message->value.as_string); |
| 91 } else { | 97 } else { |
| 92 library_uri = NULL; | 98 library_uri = NULL; |
| 93 } | 99 } |
| 94 | 100 |
| 95 // Grab the payload. | 101 // Grab the payload. |
| 96 if (payload_message->type == Dart_CObject_kString) { | 102 if (payload_message->type == Dart_CObject_kString) { |
| 97 // Payload is an error message. | 103 // Payload is an error message. |
| 98 payload_length = strlen(payload_message->value.as_string); | 104 payload_length = strlen(payload_message->value.as_string); |
| 99 payload = | 105 payload = |
| 100 reinterpret_cast<uint8_t*>(strdup(payload_message->value.as_string)); | 106 reinterpret_cast<uint8_t*>(strdup(payload_message->value.as_string)); |
| 101 } else { | 107 } else { |
| 102 // Payload is the contents of a file. | 108 // Payload is the contents of a file. |
| 103 ASSERT(payload_message->type == Dart_CObject_kTypedData); | 109 ASSERT(payload_message->type == Dart_CObject_kTypedData); |
| 104 ASSERT(payload_message->value.as_typed_data.type == Dart_TypedData_kUint8); | 110 ASSERT(payload_message->value.as_typed_data.type == Dart_TypedData_kUint8); |
| 105 payload_length = payload_message->value.as_typed_data.length; | 111 payload_length = payload_message->value.as_typed_data.length; |
| 106 payload = reinterpret_cast<uint8_t*>(malloc(payload_length)); | 112 payload = reinterpret_cast<uint8_t*>(malloc(payload_length)); |
| 107 memmove(payload, | 113 memmove(payload, |
| 108 payload_message->value.as_typed_data.values, | 114 payload_message->value.as_typed_data.values, |
| 109 payload_length); | 115 payload_length); |
| 110 } | 116 } |
| 111 } | 117 } |
| 112 | 118 |
| 113 | 119 |
| 114 void Loader::IOResult::Cleanup() { | 120 void Loader::IOResult::Cleanup() { |
| 115 free(uri); | 121 free(uri); |
| 122 free(resolved_uri); |
| 116 free(library_uri); | 123 free(library_uri); |
| 117 free(payload); | 124 free(payload); |
| 118 } | 125 } |
| 119 | 126 |
| 120 | 127 |
| 121 // Send the Loader Initialization message to the service isolate. This | 128 // Send the Loader Initialization message to the service isolate. This |
| 122 // message is sent the first time a loader is constructed for an isolate and | 129 // message is sent the first time a loader is constructed for an isolate and |
| 123 // seeds the service isolate with some initial state about this isolate. | 130 // seeds the service isolate with some initial state about this isolate. |
| 124 void Loader::Init(const char* package_root, | 131 void Loader::Init(const char* package_root, |
| 125 const char* packages_file, | 132 const char* packages_file, |
| (...skipping 135 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 261 return false; | 268 return false; |
| 262 #endif // defined(TARGET_OS_WINDOWS) | 269 #endif // defined(TARGET_OS_WINDOWS) |
| 263 } | 270 } |
| 264 | 271 |
| 265 | 272 |
| 266 bool Loader::ProcessResultLocked(Loader* loader, Loader::IOResult* result) { | 273 bool Loader::ProcessResultLocked(Loader* loader, Loader::IOResult* result) { |
| 267 // We have to copy everything we care about out of |result| because after | 274 // We have to copy everything we care about out of |result| because after |
| 268 // dropping the lock below |result| may no longer valid. | 275 // dropping the lock below |result| may no longer valid. |
| 269 Dart_Handle uri = | 276 Dart_Handle uri = |
| 270 Dart_NewStringFromCString(reinterpret_cast<char*>(result->uri)); | 277 Dart_NewStringFromCString(reinterpret_cast<char*>(result->uri)); |
| 278 Dart_Handle resolved_uri = |
| 279 Dart_NewStringFromCString(reinterpret_cast<char*>(result->resolved_uri)); |
| 271 Dart_Handle library_uri = Dart_Null(); | 280 Dart_Handle library_uri = Dart_Null(); |
| 272 if (result->library_uri != NULL) { | 281 if (result->library_uri != NULL) { |
| 273 library_uri = | 282 library_uri = |
| 274 Dart_NewStringFromCString(reinterpret_cast<char*>(result->library_uri)); | 283 Dart_NewStringFromCString(reinterpret_cast<char*>(result->library_uri)); |
| 275 } | 284 } |
| 276 | 285 |
| 277 // A negative result tag indicates a loading error occurred in the service | 286 // A negative result tag indicates a loading error occurred in the service |
| 278 // isolate. The payload is a C string of the error message. | 287 // isolate. The payload is a C string of the error message. |
| 279 if (result->tag < 0) { | 288 if (result->tag < 0) { |
| 280 Dart_Handle library = Dart_LookupLibrary(uri); | 289 Dart_Handle library = Dart_LookupLibrary(uri); |
| (...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 353 result = NULL; | 362 result = NULL; |
| 354 | 363 |
| 355 // We must drop the lock here because the tag handler may be recursively | 364 // We must drop the lock here because the tag handler may be recursively |
| 356 // invoked and it will attempt to acquire the lock to queue more work. | 365 // invoked and it will attempt to acquire the lock to queue more work. |
| 357 loader->monitor_->Exit(); | 366 loader->monitor_->Exit(); |
| 358 | 367 |
| 359 Dart_Handle dart_result = Dart_Null(); | 368 Dart_Handle dart_result = Dart_Null(); |
| 360 | 369 |
| 361 switch (tag) { | 370 switch (tag) { |
| 362 case Dart_kImportTag: | 371 case Dart_kImportTag: |
| 363 dart_result = Dart_LoadLibrary(uri, source, 0, 0); | 372 dart_result = Dart_LoadLibrary(uri, resolved_uri, source, 0, 0); |
| 364 break; | 373 break; |
| 365 case Dart_kSourceTag: { | 374 case Dart_kSourceTag: { |
| 366 ASSERT(library_uri != Dart_Null()); | 375 ASSERT(library_uri != Dart_Null()); |
| 367 Dart_Handle library = Dart_LookupLibrary(library_uri); | 376 Dart_Handle library = Dart_LookupLibrary(library_uri); |
| 368 ASSERT(!Dart_IsError(library)); | 377 ASSERT(!Dart_IsError(library)); |
| 369 dart_result = Dart_LoadSource(library, uri, source, 0, 0); | 378 dart_result = Dart_LoadSource(library, uri, resolved_uri, source, 0, 0); |
| 370 } | 379 } |
| 371 break; | 380 break; |
| 372 case Dart_kScriptTag: | 381 case Dart_kScriptTag: |
| 373 if (is_snapshot) { | 382 if (is_snapshot) { |
| 374 dart_result = Dart_LoadScriptFromSnapshot(payload, payload_length); | 383 dart_result = Dart_LoadScriptFromSnapshot(payload, payload_length); |
| 375 } else { | 384 } else { |
| 376 dart_result = Dart_LoadScript(uri, source, 0, 0); | 385 dart_result = Dart_LoadScript(uri, resolved_uri, source, 0, 0); |
| 377 } | 386 } |
| 378 break; | 387 break; |
| 379 default: | 388 default: |
| 380 UNREACHABLE(); | 389 UNREACHABLE(); |
| 381 } | 390 } |
| 382 | 391 |
| 383 // Re-acquire the lock before exiting the function (it was held before entry), | 392 // Re-acquire the lock before exiting the function (it was held before entry), |
| 384 loader->monitor_->Enter(); | 393 loader->monitor_->Enter(); |
| 385 if (Dart_IsError(dart_result)) { | 394 if (Dart_IsError(dart_result)) { |
| 386 // Remember the error if we encountered one. | 395 // Remember the error if we encountered one. |
| (...skipping 259 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 646 " on the stand-alone VM. Trying to load" | 655 " on the stand-alone VM. Trying to load" |
| 647 " '%s'.\n", library_url_string, url_string); | 656 " '%s'.\n", library_url_string, url_string); |
| 648 } | 657 } |
| 649 // Prepend the library URI to form a unique script URI for the part. | 658 // Prepend the library URI to form a unique script URI for the part. |
| 650 intptr_t len = snprintf(NULL, 0, "%s/%s", library_url_string, url_string); | 659 intptr_t len = snprintf(NULL, 0, "%s/%s", library_url_string, url_string); |
| 651 char* part_uri = reinterpret_cast<char*>(malloc(len + 1)); | 660 char* part_uri = reinterpret_cast<char*>(malloc(len + 1)); |
| 652 snprintf(part_uri, len + 1, "%s/%s", library_url_string, url_string); | 661 snprintf(part_uri, len + 1, "%s/%s", library_url_string, url_string); |
| 653 Dart_Handle part_uri_obj = DartUtils::NewString(part_uri); | 662 Dart_Handle part_uri_obj = DartUtils::NewString(part_uri); |
| 654 free(part_uri); | 663 free(part_uri); |
| 655 return Dart_LoadSource(library, | 664 return Dart_LoadSource(library, |
| 656 part_uri_obj, | 665 part_uri_obj, Dart_Null(), |
| 657 Builtin::PartSource(id, url_string), 0, 0); | 666 Builtin::PartSource(id, url_string), 0, 0); |
| 658 } | 667 } |
| 659 // All cases should have been handled above. | 668 // All cases should have been handled above. |
| 660 UNREACHABLE(); | 669 UNREACHABLE(); |
| 661 } | 670 } |
| 662 | 671 |
| 663 | 672 |
| 664 void Loader::InitOnce() { | 673 void Loader::InitOnce() { |
| 665 loader_infos_lock_ = new Mutex(); | 674 loader_infos_lock_ = new Mutex(); |
| 666 } | 675 } |
| (...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 748 MutexLocker ml(loader_infos_lock_); | 757 MutexLocker ml(loader_infos_lock_); |
| 749 Loader* loader = LoaderForLocked(dest_port_id); | 758 Loader* loader = LoaderForLocked(dest_port_id); |
| 750 if (loader == NULL) { | 759 if (loader == NULL) { |
| 751 return; | 760 return; |
| 752 } | 761 } |
| 753 loader->QueueMessage(message); | 762 loader->QueueMessage(message); |
| 754 } | 763 } |
| 755 | 764 |
| 756 } // namespace bin | 765 } // namespace bin |
| 757 } // namespace dart | 766 } // namespace dart |
| OLD | NEW |