| OLD | NEW |
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, 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/dartutils.h" | 5 #include "bin/dartutils.h" |
| 6 | 6 |
| 7 #include "bin/crypto.h" | 7 #include "bin/crypto.h" |
| 8 #include "bin/directory.h" | 8 #include "bin/directory.h" |
| 9 #include "bin/extensions.h" | 9 #include "bin/extensions.h" |
| 10 #include "bin/file.h" | 10 #include "bin/file.h" |
| (...skipping 527 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 538 Dart_TimelineEvent("LoadScript", | 538 Dart_TimelineEvent("LoadScript", |
| 539 Dart_TimelineGetMicros(), | 539 Dart_TimelineGetMicros(), |
| 540 Dart_GetMainPortId(), | 540 Dart_GetMainPortId(), |
| 541 Dart_Timeline_Event_Async_Begin, | 541 Dart_Timeline_Event_Async_Begin, |
| 542 0, NULL, NULL); | 542 0, NULL, NULL); |
| 543 Dart_Handle uri = Dart_NewStringFromCString(script_uri); | 543 Dart_Handle uri = Dart_NewStringFromCString(script_uri); |
| 544 return LoadDataAsync_Invoke(Dart_Null(), uri, Dart_Null()); | 544 return LoadDataAsync_Invoke(Dart_Null(), uri, Dart_Null()); |
| 545 } | 545 } |
| 546 | 546 |
| 547 | 547 |
| 548 // Callback function, gets called from asynchronous script and library | |
| 549 // reading code when there is an i/o error. | |
| 550 void FUNCTION_NAME(Builtin_AsyncLoadError)(Dart_NativeArguments args) { | |
| 551 // Dart_Handle source_uri = Dart_GetNativeArgument(args, 0); | |
| 552 Dart_Handle library_uri = Dart_GetNativeArgument(args, 1); | |
| 553 Dart_Handle error = Dart_GetNativeArgument(args, 2); | |
| 554 | |
| 555 Dart_Handle library = Dart_LookupLibrary(library_uri); | |
| 556 // If a library with the given uri exists, give it a chance to handle | |
| 557 // the error. If the load requests stems from a deferred library load, | |
| 558 // an IO error is not fatal. | |
| 559 if (!Dart_IsError(library)) { | |
| 560 ASSERT(Dart_IsLibrary(library)); | |
| 561 Dart_Handle res = Dart_LibraryHandleError(library, error); | |
| 562 if (Dart_IsNull(res)) { | |
| 563 return; | |
| 564 } | |
| 565 } | |
| 566 // The error was not handled above. Propagate an unhandled exception. | |
| 567 error = Dart_NewUnhandledExceptionError(error); | |
| 568 Dart_PropagateError(error); | |
| 569 } | |
| 570 | |
| 571 | |
| 572 // Callback function that gets called from dartutils when the library | |
| 573 // source has been read. Loads the library or part into the VM. | |
| 574 void FUNCTION_NAME(Builtin_LoadSource)(Dart_NativeArguments args) { | |
| 575 Dart_Handle tag_in = Dart_GetNativeArgument(args, 0); | |
| 576 Dart_Handle resolved_script_uri = Dart_GetNativeArgument(args, 1); | |
| 577 Dart_Handle library_uri = Dart_GetNativeArgument(args, 2); | |
| 578 Dart_Handle source_data = Dart_GetNativeArgument(args, 3); | |
| 579 | |
| 580 Dart_TypedData_Type type = Dart_GetTypeOfExternalTypedData(source_data); | |
| 581 bool external = type == Dart_TypedData_kUint8; | |
| 582 uint8_t* data = NULL; | |
| 583 intptr_t num_bytes; | |
| 584 Dart_Handle result = Dart_TypedDataAcquireData( | |
| 585 source_data, &type, reinterpret_cast<void**>(&data), &num_bytes); | |
| 586 if (Dart_IsError(result)) Dart_PropagateError(result); | |
| 587 | |
| 588 uint8_t* buffer_copy = NULL; | |
| 589 if (!external) { | |
| 590 // If the buffer is not external, take a copy. | |
| 591 buffer_copy = reinterpret_cast<uint8_t*>(malloc(num_bytes)); | |
| 592 memmove(buffer_copy, data, num_bytes); | |
| 593 data = buffer_copy; | |
| 594 } | |
| 595 | |
| 596 Dart_TypedDataReleaseData(source_data); | |
| 597 | |
| 598 if (Dart_IsNull(tag_in) && Dart_IsNull(library_uri)) { | |
| 599 // Entry file. Check for payload and load accordingly. | |
| 600 const uint8_t* payload = data; | |
| 601 const DartUtils::MagicNumber payload_type = | |
| 602 DartUtils::SniffForMagicNumber(&payload, &num_bytes); | |
| 603 | |
| 604 if (payload_type == DartUtils::kSnapshotMagicNumber) { | |
| 605 result = Dart_LoadScriptFromSnapshot(payload, num_bytes); | |
| 606 } else if (payload_type == DartUtils::kKernelMagicNumber) { | |
| 607 UNREACHABLE(); | |
| 608 } else { | |
| 609 Dart_Handle source = Dart_NewStringFromUTF8(data, num_bytes); | |
| 610 if (Dart_IsError(source)) { | |
| 611 result = DartUtils::NewError("%s is not a valid UTF-8 script", | |
| 612 resolved_script_uri); | |
| 613 } else { | |
| 614 result = Dart_LoadScript(resolved_script_uri, Dart_Null(), | |
| 615 source, 0, 0); | |
| 616 } | |
| 617 } | |
| 618 } else { | |
| 619 int64_t tag = DartUtils::GetIntegerValue(tag_in); | |
| 620 | |
| 621 Dart_Handle source = Dart_NewStringFromUTF8(data, num_bytes); | |
| 622 if (Dart_IsError(source)) { | |
| 623 result = DartUtils::NewError("%s is not a valid UTF-8 script", | |
| 624 resolved_script_uri); | |
| 625 } else { | |
| 626 if (tag == Dart_kImportTag) { | |
| 627 result = Dart_LoadLibrary(resolved_script_uri, Dart_Null(), | |
| 628 source, 0, 0); | |
| 629 } else { | |
| 630 ASSERT(tag == Dart_kSourceTag); | |
| 631 Dart_Handle library = Dart_LookupLibrary(library_uri); | |
| 632 if (Dart_IsError(library)) { | |
| 633 Dart_PropagateError(library); | |
| 634 } | |
| 635 result = Dart_LoadSource(library, resolved_script_uri, Dart_Null(), | |
| 636 source, 0, 0); | |
| 637 } | |
| 638 } | |
| 639 } | |
| 640 | |
| 641 if (buffer_copy != NULL) { | |
| 642 free(buffer_copy); | |
| 643 } | |
| 644 | |
| 645 if (Dart_IsError(result)) { | |
| 646 Dart_PropagateError(result); | |
| 647 } | |
| 648 } | |
| 649 | |
| 650 | |
| 651 // Callback function that gets called from dartutils when there are | |
| 652 // no more outstanding load requests. | |
| 653 void FUNCTION_NAME(Builtin_DoneLoading)(Dart_NativeArguments args) { | |
| 654 Dart_Handle res = Dart_FinalizeLoading(true); | |
| 655 if (Dart_IsError(res)) { | |
| 656 // TODO(hausner): If compilation/loading errors are supposed to | |
| 657 // be observable by the program, we need to mark the bad library | |
| 658 // with the error instead of propagating it. | |
| 659 Dart_PropagateError(res); | |
| 660 } | |
| 661 } | |
| 662 | |
| 663 | |
| 664 void FUNCTION_NAME(Builtin_GetCurrentDirectory)(Dart_NativeArguments args) { | 548 void FUNCTION_NAME(Builtin_GetCurrentDirectory)(Dart_NativeArguments args) { |
| 665 const char* current = Directory::Current(); | 549 const char* current = Directory::Current(); |
| 666 if (current != NULL) { | 550 if (current != NULL) { |
| 667 Dart_SetReturnValue(args, DartUtils::NewString(current)); | 551 Dart_SetReturnValue(args, DartUtils::NewString(current)); |
| 668 } else { | 552 } else { |
| 669 Dart_Handle err = DartUtils::NewError("Failed to get current directory."); | 553 Dart_Handle err = DartUtils::NewError("Failed to get current directory."); |
| 670 Dart_PropagateError(err); | 554 Dart_PropagateError(err); |
| 671 } | 555 } |
| 672 } | 556 } |
| 673 | 557 |
| (...skipping 631 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1305 new CObjectString(CObject::NewString(os_error->message())); | 1189 new CObjectString(CObject::NewString(os_error->message())); |
| 1306 CObjectArray* result = new CObjectArray(CObject::NewArray(3)); | 1190 CObjectArray* result = new CObjectArray(CObject::NewArray(3)); |
| 1307 result->SetAt(0, new CObjectInt32(CObject::NewInt32(kOSError))); | 1191 result->SetAt(0, new CObjectInt32(CObject::NewInt32(kOSError))); |
| 1308 result->SetAt(1, new CObjectInt32(CObject::NewInt32(os_error->code()))); | 1192 result->SetAt(1, new CObjectInt32(CObject::NewInt32(os_error->code()))); |
| 1309 result->SetAt(2, error_message); | 1193 result->SetAt(2, error_message); |
| 1310 return result; | 1194 return result; |
| 1311 } | 1195 } |
| 1312 | 1196 |
| 1313 } // namespace bin | 1197 } // namespace bin |
| 1314 } // namespace dart | 1198 } // namespace dart |
| OLD | NEW |