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" |
11 #include "bin/file.h" | 11 #include "bin/file.h" |
12 #include "bin/lockers.h" | 12 #include "bin/lockers.h" |
13 #include "bin/utils.h" | 13 #include "bin/utils.h" |
| 14 #include "include/dart_tools_api.h" |
14 | 15 |
15 namespace dart { | 16 namespace dart { |
16 namespace bin { | 17 namespace bin { |
17 | 18 |
18 // Development flag. | 19 // Development flag. |
19 static bool trace_loader = false; | 20 static bool trace_loader = false; |
20 // Keep in sync with loader.dart. | 21 // Keep in sync with loader.dart. |
21 static const intptr_t _Dart_kImportExtension = 9; | 22 static const intptr_t _Dart_kImportExtension = 9; |
22 | 23 |
23 Loader::Loader(IsolateData* isolate_data) | 24 Loader::Loader(IsolateData* isolate_data) |
(...skipping 330 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
354 intptr_t tag = result->tag; | 355 intptr_t tag = result->tag; |
355 | 356 |
356 // No touching. | 357 // No touching. |
357 result = NULL; | 358 result = NULL; |
358 | 359 |
359 // We must drop the lock here because the tag handler may be recursively | 360 // We must drop the lock here because the tag handler may be recursively |
360 // invoked and it will attempt to acquire the lock to queue more work. | 361 // invoked and it will attempt to acquire the lock to queue more work. |
361 loader->monitor_->Exit(); | 362 loader->monitor_->Exit(); |
362 | 363 |
363 Dart_Handle dart_result = Dart_Null(); | 364 Dart_Handle dart_result = Dart_Null(); |
| 365 bool reload_extensions = false; |
364 | 366 |
365 switch (tag) { | 367 switch (tag) { |
366 case Dart_kImportTag: | 368 case Dart_kImportTag: |
367 dart_result = Dart_LoadLibrary(uri, resolved_uri, source, 0, 0); | 369 dart_result = Dart_LoadLibrary(uri, resolved_uri, source, 0, 0); |
368 break; | 370 break; |
369 case Dart_kSourceTag: { | 371 case Dart_kSourceTag: { |
370 ASSERT(library_uri != Dart_Null()); | 372 ASSERT(library_uri != Dart_Null()); |
371 Dart_Handle library = Dart_LookupLibrary(library_uri); | 373 Dart_Handle library = Dart_LookupLibrary(library_uri); |
372 ASSERT(!Dart_IsError(library)); | 374 ASSERT(!Dart_IsError(library)); |
373 dart_result = Dart_LoadSource(library, uri, resolved_uri, source, 0, 0); | 375 dart_result = Dart_LoadSource(library, uri, resolved_uri, source, 0, 0); |
374 } | 376 } |
375 break; | 377 break; |
376 case Dart_kScriptTag: | 378 case Dart_kScriptTag: |
377 if (payload_type == DartUtils::kSnapshotMagicNumber) { | 379 if (payload_type == DartUtils::kSnapshotMagicNumber) { |
378 dart_result = Dart_LoadScriptFromSnapshot(payload, payload_length); | 380 dart_result = Dart_LoadScriptFromSnapshot(payload, payload_length); |
| 381 reload_extensions = true; |
379 } else if (payload_type == DartUtils::kKernelMagicNumber) { | 382 } else if (payload_type == DartUtils::kKernelMagicNumber) { |
380 dart_result = Dart_LoadKernel(payload, payload_length); | 383 dart_result = Dart_LoadKernel(payload, payload_length); |
381 } else { | 384 } else { |
382 dart_result = Dart_LoadScript(uri, resolved_uri, source, 0, 0); | 385 dart_result = Dart_LoadScript(uri, resolved_uri, source, 0, 0); |
383 } | 386 } |
384 break; | 387 break; |
385 default: | 388 default: |
386 UNREACHABLE(); | 389 UNREACHABLE(); |
387 } | 390 } |
388 | 391 |
389 // 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), |
390 loader->monitor_->Enter(); | 393 loader->monitor_->Enter(); |
391 if (Dart_IsError(dart_result)) { | 394 if (Dart_IsError(dart_result)) { |
392 // Remember the error if we encountered one. | 395 // Remember the error if we encountered one. |
393 loader->error_ = dart_result; | 396 loader->error_ = dart_result; |
394 return false; | 397 return false; |
395 } | 398 } |
396 | 399 |
| 400 if (reload_extensions) { |
| 401 dart_result = ReloadNativeExtensions(); |
| 402 if (Dart_IsError(dart_result)) { |
| 403 // Remember the error if we encountered one. |
| 404 loader->error_ = dart_result; |
| 405 return false; |
| 406 } |
| 407 } |
| 408 |
397 return true; | 409 return true; |
398 } | 410 } |
399 | 411 |
400 | 412 |
401 bool Loader::ProcessUrlLoadResultLocked(Loader* loader, | 413 bool Loader::ProcessUrlLoadResultLocked(Loader* loader, |
402 Loader::IOResult* result) { | 414 Loader::IOResult* result) { |
403 // A negative result tag indicates a loading error occurred in the service | 415 // A negative result tag indicates a loading error occurred in the service |
404 // isolate. The payload is a C string of the error message. | 416 // isolate. The payload is a C string of the error message. |
405 if (result->tag < 0) { | 417 if (result->tag < 0) { |
406 Dart_Handle error = Dart_NewStringFromUTF8(result->payload, | 418 Dart_Handle error = Dart_NewStringFromUTF8(result->payload, |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
441 // Send the init message. | 453 // Send the init message. |
442 loader->Init(isolate_data->package_root, | 454 loader->Init(isolate_data->package_root, |
443 isolate_data->packages_file, | 455 isolate_data->packages_file, |
444 DartUtils::original_working_directory, | 456 DartUtils::original_working_directory, |
445 snapshot_uri); | 457 snapshot_uri); |
446 // Destroy the loader. The destructor does a bunch of leg work. | 458 // Destroy the loader. The destructor does a bunch of leg work. |
447 delete loader; | 459 delete loader; |
448 } | 460 } |
449 | 461 |
450 | 462 |
| 463 #define RETURN_ERROR(result) \ |
| 464 if (Dart_IsError(result)) return result; |
| 465 |
| 466 Dart_Handle Loader::ReloadNativeExtensions() { |
| 467 Dart_Handle scheme = |
| 468 Dart_NewStringFromCString(DartUtils::kDartExtensionScheme); |
| 469 Dart_Handle extension_imports = Dart_GetImportsOfScheme(scheme); |
| 470 RETURN_ERROR(extension_imports); |
| 471 |
| 472 intptr_t length = -1; |
| 473 Dart_Handle result = Dart_ListLength(extension_imports, &length); |
| 474 RETURN_ERROR(result); |
| 475 Dart_Handle* import_handles = reinterpret_cast<Dart_Handle*>( |
| 476 Dart_ScopeAllocate(sizeof(Dart_Handle) * length)); |
| 477 result = Dart_ListGetRange(extension_imports, 0, length, import_handles); |
| 478 RETURN_ERROR(result); |
| 479 for (intptr_t i = 0; i < length; i += 2) { |
| 480 Dart_Handle importer = import_handles[i]; |
| 481 Dart_Handle importee = import_handles[i + 1]; |
| 482 |
| 483 const char* extension_uri = NULL; |
| 484 result = Dart_StringToCString(Dart_LibraryUrl(importee), &extension_uri); |
| 485 RETURN_ERROR(result); |
| 486 const char* extension_path = DartUtils::RemoveScheme(extension_uri); |
| 487 |
| 488 const char* lib_uri = NULL; |
| 489 result = Dart_StringToCString(Dart_LibraryUrl(importer), &lib_uri); |
| 490 RETURN_ERROR(result); |
| 491 |
| 492 char* lib_path = NULL; |
| 493 if (strncmp(lib_uri, "file://", 7) == 0) { |
| 494 lib_path = DartUtils::DirName(DartUtils::RemoveScheme(lib_uri)); |
| 495 } else { |
| 496 lib_path = strdup(lib_uri); |
| 497 } |
| 498 |
| 499 result = Extensions::LoadExtension(lib_path, extension_path, importer); |
| 500 free(lib_path); |
| 501 RETURN_ERROR(result); |
| 502 } |
| 503 |
| 504 return Dart_True(); |
| 505 } |
| 506 |
| 507 |
451 Dart_Handle Loader::LoadUrlContents(Dart_Handle url, | 508 Dart_Handle Loader::LoadUrlContents(Dart_Handle url, |
452 uint8_t** payload, | 509 uint8_t** payload, |
453 intptr_t* payload_length) { | 510 intptr_t* payload_length) { |
454 IsolateData* isolate_data = | 511 IsolateData* isolate_data = |
455 reinterpret_cast<IsolateData*>(Dart_CurrentIsolateData()); | 512 reinterpret_cast<IsolateData*>(Dart_CurrentIsolateData()); |
456 ASSERT(isolate_data != NULL); | 513 ASSERT(isolate_data != NULL); |
457 ASSERT(!isolate_data->HasLoader()); | 514 ASSERT(!isolate_data->HasLoader()); |
458 Loader* loader = NULL; | 515 Loader* loader = NULL; |
459 | 516 |
460 // Setup the loader. The constructor does a bunch of leg work. | 517 // Setup the loader. The constructor does a bunch of leg work. |
(...skipping 294 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
755 MutexLocker ml(loader_infos_lock_); | 812 MutexLocker ml(loader_infos_lock_); |
756 Loader* loader = LoaderForLocked(dest_port_id); | 813 Loader* loader = LoaderForLocked(dest_port_id); |
757 if (loader == NULL) { | 814 if (loader == NULL) { |
758 return; | 815 return; |
759 } | 816 } |
760 loader->QueueMessage(message); | 817 loader->QueueMessage(message); |
761 } | 818 } |
762 | 819 |
763 } // namespace bin | 820 } // namespace bin |
764 } // namespace dart | 821 } // namespace dart |
OLD | NEW |