| 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 "include/dart_api.h" | 7 #include "include/dart_api.h" |
| 8 #include "include/dart_native_api.h" | 8 #include "include/dart_native_api.h" |
| 9 | 9 |
| 10 #include "platform/assert.h" | 10 #include "platform/assert.h" |
| (...skipping 231 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 242 snprintf(msg, len + 1, format, __VA_ARGS__); \ | 242 snprintf(msg, len + 1, format, __VA_ARGS__); \ |
| 243 *error_msg = msg | 243 *error_msg = msg |
| 244 | 244 |
| 245 | 245 |
| 246 Dart_Handle MakeHttpRequest(Dart_Handle uri, Dart_Handle builtin_lib, | 246 Dart_Handle MakeHttpRequest(Dart_Handle uri, Dart_Handle builtin_lib, |
| 247 uint8_t** buffer, intptr_t* buffer_len) { | 247 uint8_t** buffer, intptr_t* buffer_len) { |
| 248 const intptr_t HttpResponseCodeOK = 200; | 248 const intptr_t HttpResponseCodeOK = 200; |
| 249 ASSERT(buffer != NULL); | 249 ASSERT(buffer != NULL); |
| 250 ASSERT(buffer_len != NULL); | 250 ASSERT(buffer_len != NULL); |
| 251 ASSERT(!Dart_HasLivePorts()); | 251 ASSERT(!Dart_HasLivePorts()); |
| 252 |
| 253 // MakeHttpRequest uses the event loop (Dart_RunLoop) to handle the |
| 254 // asynchronous request. This interferes with the use of Dart_RunLoop |
| 255 // for asynchronous loading. |
| 256 ASSERT(!Dart_IsVMFlagSet("load_async")); |
| 257 |
| 252 SingleArgDart_Invoke(builtin_lib, "_makeHttpRequest", uri); | 258 SingleArgDart_Invoke(builtin_lib, "_makeHttpRequest", uri); |
| 253 // Run until all ports to isolate are closed. | 259 // Run until all ports to isolate are closed. |
| 254 Dart_Handle result = Dart_RunLoop(); | 260 Dart_Handle result = Dart_RunLoop(); |
| 255 if (Dart_IsError(result)) { | 261 if (Dart_IsError(result)) { |
| 256 return result; | 262 return result; |
| 257 } | 263 } |
| 258 result = Dart_Invoke(builtin_lib, | 264 result = Dart_Invoke(builtin_lib, |
| 259 DartUtils::NewString("_getHttpRequestResponseCode"), | 265 DartUtils::NewString("_getHttpRequestResponseCode"), |
| 260 0, | 266 0, |
| 261 NULL); | 267 NULL); |
| (...skipping 260 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 522 Dart_StringToCString(Dart_ListGetAt(path_parts, 1), &extension_filename); | 528 Dart_StringToCString(Dart_ListGetAt(path_parts, 1), &extension_filename); |
| 523 const char* extension_name = NULL; | 529 const char* extension_name = NULL; |
| 524 Dart_StringToCString(Dart_ListGetAt(path_parts, 2), &extension_name); | 530 Dart_StringToCString(Dart_ListGetAt(path_parts, 2), &extension_name); |
| 525 | 531 |
| 526 return Extensions::LoadExtension(extension_directory, | 532 return Extensions::LoadExtension(extension_directory, |
| 527 extension_filename, | 533 extension_filename, |
| 528 extension_name, | 534 extension_name, |
| 529 library); | 535 library); |
| 530 } else { | 536 } else { |
| 531 // Handle 'import' or 'part' requests for all other URIs. | 537 // Handle 'import' or 'part' requests for all other URIs. |
| 538 |
| 539 if (Dart_IsVMFlagSet("load_async")) { |
| 540 // Call dart code to read the source code asynchronously. |
| 541 const int kNumArgs = 3; |
| 542 Dart_Handle dart_args[kNumArgs]; |
| 543 dart_args[0] = Dart_NewInteger(tag); |
| 544 dart_args[1] = url; |
| 545 dart_args[2] = library_url; |
| 546 return Dart_Invoke(builtin_lib, |
| 547 NewString("_loadSourceAsync"), |
| 548 kNumArgs, |
| 549 dart_args); |
| 550 } |
| 551 |
| 532 // Get the file path out of the url. | 552 // Get the file path out of the url. |
| 533 Dart_Handle file_path = DartUtils::FilePathFromUri(url, builtin_lib); | 553 Dart_Handle file_path = DartUtils::FilePathFromUri(url, builtin_lib); |
| 534 if (Dart_IsError(file_path)) { | 554 if (Dart_IsError(file_path)) { |
| 535 return file_path; | 555 return file_path; |
| 536 } | 556 } |
| 537 const char* final_path = NULL; | 557 const char* final_path = NULL; |
| 538 Dart_StringToCString(file_path, &final_path); | 558 Dart_StringToCString(file_path, &final_path); |
| 539 result = DartUtils::LoadSource(library, url, tag, final_path); | 559 result = DartUtils::LoadSource(library, url, tag, final_path); |
| 540 return result; | 560 return result; |
| 541 } | 561 } |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 583 Dart_Handle source = Dart_NewStringFromUTF8(payload, len); | 603 Dart_Handle source = Dart_NewStringFromUTF8(payload, len); |
| 584 free(buffer); | 604 free(buffer); |
| 585 if (Dart_IsError(source)) { | 605 if (Dart_IsError(source)) { |
| 586 return source; | 606 return source; |
| 587 } | 607 } |
| 588 return Dart_LoadScript(uri, source, 0, 0); | 608 return Dart_LoadScript(uri, source, 0, 0); |
| 589 } | 609 } |
| 590 } | 610 } |
| 591 | 611 |
| 592 | 612 |
| 613 Dart_Handle DartUtils::LoadScriptDataAsync(Dart_Handle script_uri, |
| 614 Dart_Handle builtin_lib) { |
| 615 const int kNumArgs = 1; |
| 616 Dart_Handle dart_args[kNumArgs]; |
| 617 dart_args[0] = script_uri; |
| 618 return Dart_Invoke(builtin_lib, |
| 619 NewString("_loadDataAsync"), |
| 620 kNumArgs, |
| 621 dart_args); |
| 622 } |
| 623 |
| 624 |
| 593 Dart_Handle DartUtils::LoadScript(const char* script_uri, | 625 Dart_Handle DartUtils::LoadScript(const char* script_uri, |
| 594 Dart_Handle builtin_lib) { | 626 Dart_Handle builtin_lib) { |
| 627 if (Dart_IsVMFlagSet("load_async")) { |
| 628 Dart_Handle uri_handle = Dart_NewStringFromCString(script_uri); |
| 629 return LoadScriptDataAsync(uri_handle, builtin_lib); |
| 630 } |
| 631 |
| 595 Dart_Handle resolved_script_uri = | 632 Dart_Handle resolved_script_uri = |
| 596 ResolveScriptUri(NewString(script_uri), builtin_lib); | 633 ResolveScriptUri(NewString(script_uri), builtin_lib); |
| 597 if (Dart_IsError(resolved_script_uri)) { | 634 if (Dart_IsError(resolved_script_uri)) { |
| 598 return resolved_script_uri; | 635 return resolved_script_uri; |
| 599 } | 636 } |
| 600 // Handle http: requests separately. | 637 // Handle http: requests separately. |
| 601 if (DartUtils::IsHttpSchemeURL(script_uri)) { | 638 if (DartUtils::IsHttpSchemeURL(script_uri)) { |
| 602 return LoadScriptHttp(resolved_script_uri, builtin_lib); | 639 return LoadScriptHttp(resolved_script_uri, builtin_lib); |
| 603 } | 640 } |
| 604 Dart_Handle script_path = DartUtils::FilePathFromUri(resolved_script_uri, | 641 Dart_Handle script_path = DartUtils::FilePathFromUri(resolved_script_uri, |
| (...skipping 22 matching lines...) Expand all Loading... |
| 627 returnValue = NewError("%s is not a valid UTF-8 script", script_uri); | 664 returnValue = NewError("%s is not a valid UTF-8 script", script_uri); |
| 628 } else { | 665 } else { |
| 629 returnValue = Dart_LoadScript(resolved_script_uri, source, 0, 0); | 666 returnValue = Dart_LoadScript(resolved_script_uri, source, 0, 0); |
| 630 } | 667 } |
| 631 } | 668 } |
| 632 free(const_cast<uint8_t *>(buffer)); | 669 free(const_cast<uint8_t *>(buffer)); |
| 633 return returnValue; | 670 return returnValue; |
| 634 } | 671 } |
| 635 | 672 |
| 636 | 673 |
| 674 // Callback function that gets called from asynchronous script loading code |
| 675 // when the data has been read. Loads the script or snapshot into the VM. |
| 676 void FUNCTION_NAME(Builtin_LoadScript)(Dart_NativeArguments args) { |
| 677 Dart_Handle resolved_script_uri = Dart_GetNativeArgument(args, 0); |
| 678 Dart_Handle data = Dart_GetNativeArgument(args, 1); |
| 679 |
| 680 intptr_t num_bytes = 0; |
| 681 Dart_Handle result = Dart_ListLength(data, &num_bytes); |
| 682 DART_CHECK_VALID(result); |
| 683 |
| 684 uint8_t* buffer = reinterpret_cast<uint8_t*>(malloc(num_bytes)); |
| 685 Dart_ListGetAsBytes(data, 0, buffer, num_bytes); |
| 686 |
| 687 bool is_snapshot = false; |
| 688 const uint8_t *payload = |
| 689 DartUtils::SniffForMagicNumber(buffer, &num_bytes, &is_snapshot); |
| 690 |
| 691 if (is_snapshot) { |
| 692 result = Dart_LoadScriptFromSnapshot(payload, num_bytes); |
| 693 } else { |
| 694 Dart_Handle source = Dart_NewStringFromUTF8(buffer, num_bytes); |
| 695 if (Dart_IsError(source)) { |
| 696 result = DartUtils::NewError("%s is not a valid UTF-8 script", |
| 697 resolved_script_uri); |
| 698 } else { |
| 699 result = Dart_LoadScript(resolved_script_uri, source, 0, 0); |
| 700 } |
| 701 } |
| 702 free(const_cast<uint8_t *>(buffer)); |
| 703 if (Dart_IsError(result)) { |
| 704 Dart_PropagateError(result); |
| 705 } |
| 706 } |
| 707 |
| 708 |
| 709 // Callback function, gets called from asynchronous script and library |
| 710 // reading code when there is an i/o error. |
| 711 void FUNCTION_NAME(Builtin_AsyncLoadError)(Dart_NativeArguments args) { |
| 712 // Dart_Handle script_uri = Dart_GetNativeArgument(args, 0); |
| 713 Dart_Handle error = Dart_GetNativeArgument(args, 1); |
| 714 Dart_Handle res = Dart_NewUnhandledExceptionError(error); |
| 715 Dart_PropagateError(res); |
| 716 } |
| 717 |
| 718 |
| 719 // Callback function that gets called from dartutils when the library |
| 720 // source has been read. Loads the library or part into the VM. |
| 721 void FUNCTION_NAME(Builtin_LoadLibrarySource)(Dart_NativeArguments args) { |
| 722 Dart_Handle tag_in = Dart_GetNativeArgument(args, 0); |
| 723 Dart_Handle resolved_script_uri = Dart_GetNativeArgument(args, 1); |
| 724 Dart_Handle library_uri = Dart_GetNativeArgument(args, 2); |
| 725 Dart_Handle sourceText = Dart_GetNativeArgument(args, 3); |
| 726 |
| 727 int64_t tag = DartUtils::GetIntegerValue(tag_in); |
| 728 |
| 729 Dart_Handle result; |
| 730 if (tag == Dart_kImportTag) { |
| 731 result = Dart_LoadLibrary(resolved_script_uri, sourceText); |
| 732 } else { |
| 733 ASSERT(tag == Dart_kSourceTag); |
| 734 Dart_Handle library = Dart_LookupLibrary(library_uri); |
| 735 DART_CHECK_VALID(library); |
| 736 result = Dart_LoadSource(library, resolved_script_uri, sourceText); |
| 737 } |
| 738 if (Dart_IsError(result)) Dart_PropagateError(result); |
| 739 } |
| 740 |
| 741 |
| 637 Dart_Handle DartUtils::LoadSource(Dart_Handle library, | 742 Dart_Handle DartUtils::LoadSource(Dart_Handle library, |
| 638 Dart_Handle url, | 743 Dart_Handle url, |
| 639 Dart_LibraryTag tag, | 744 Dart_LibraryTag tag, |
| 640 const char* url_string) { | 745 const char* url_string) { |
| 641 bool is_http_scheme_url = DartUtils::IsHttpSchemeURL(url_string); | 746 bool is_http_scheme_url = DartUtils::IsHttpSchemeURL(url_string); |
| 642 Dart_Handle source; | 747 Dart_Handle source; |
| 643 if (is_http_scheme_url) { | 748 if (is_http_scheme_url) { |
| 644 // Read the file over http. | 749 // Read the file over http. |
| 645 source = DartUtils::ReadStringFromHttp(url_string); | 750 source = DartUtils::ReadStringFromHttp(url_string); |
| 646 } else { | 751 } else { |
| (...skipping 389 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1036 new CObjectString(CObject::NewString(os_error->message())); | 1141 new CObjectString(CObject::NewString(os_error->message())); |
| 1037 CObjectArray* result = new CObjectArray(CObject::NewArray(3)); | 1142 CObjectArray* result = new CObjectArray(CObject::NewArray(3)); |
| 1038 result->SetAt(0, new CObjectInt32(CObject::NewInt32(kOSError))); | 1143 result->SetAt(0, new CObjectInt32(CObject::NewInt32(kOSError))); |
| 1039 result->SetAt(1, new CObjectInt32(CObject::NewInt32(os_error->code()))); | 1144 result->SetAt(1, new CObjectInt32(CObject::NewInt32(os_error->code()))); |
| 1040 result->SetAt(2, error_message); | 1145 result->SetAt(2, error_message); |
| 1041 return result; | 1146 return result; |
| 1042 } | 1147 } |
| 1043 | 1148 |
| 1044 } // namespace bin | 1149 } // namespace bin |
| 1045 } // namespace dart | 1150 } // namespace dart |
| OLD | NEW |