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" |
11 #include "platform/globals.h" | 11 #include "platform/globals.h" |
12 | 12 |
13 #include "bin/crypto.h" | 13 #include "bin/crypto.h" |
14 #include "bin/directory.h" | 14 #include "bin/directory.h" |
15 #include "bin/extensions.h" | 15 #include "bin/extensions.h" |
16 #include "bin/file.h" | 16 #include "bin/file.h" |
17 #include "bin/io_buffer.h" | 17 #include "bin/io_buffer.h" |
| 18 #include "bin/platform.h" |
18 #include "bin/socket.h" | 19 #include "bin/socket.h" |
19 #include "bin/utils.h" | 20 #include "bin/utils.h" |
20 | 21 |
21 namespace dart { | 22 namespace dart { |
22 namespace bin { | 23 namespace bin { |
23 | 24 |
24 const char* DartUtils::original_working_directory = NULL; | 25 const char* DartUtils::original_working_directory = NULL; |
25 const char* DartUtils::kDartScheme = "dart:"; | 26 const char* DartUtils::kDartScheme = "dart:"; |
26 const char* DartUtils::kDartExtensionScheme = "dart-ext:"; | 27 const char* DartUtils::kDartExtensionScheme = "dart-ext:"; |
27 const char* DartUtils::kAsyncLibURL = "dart:async"; | 28 const char* DartUtils::kAsyncLibURL = "dart:async"; |
(...skipping 11 matching lines...) Expand all Loading... |
39 | 40 |
40 static bool IsWindowsHost() { | 41 static bool IsWindowsHost() { |
41 #if defined(TARGET_OS_WINDOWS) | 42 #if defined(TARGET_OS_WINDOWS) |
42 return true; | 43 return true; |
43 #else // defined(TARGET_OS_WINDOWS) | 44 #else // defined(TARGET_OS_WINDOWS) |
44 return false; | 45 return false; |
45 #endif // defined(TARGET_OS_WINDOWS) | 46 #endif // defined(TARGET_OS_WINDOWS) |
46 } | 47 } |
47 | 48 |
48 | 49 |
| 50 // Experimental flag that offloads all script loading I/O onto |
| 51 // the service isolate. Disabled for now. |
| 52 // #define LOAD_VIA_SERVICE_ISOLATE |
| 53 |
49 const char* DartUtils::MapLibraryUrl(CommandLineOptions* url_mapping, | 54 const char* DartUtils::MapLibraryUrl(CommandLineOptions* url_mapping, |
50 const char* url_string) { | 55 const char* url_string) { |
51 ASSERT(url_mapping != NULL); | 56 ASSERT(url_mapping != NULL); |
52 // We need to check if the passed in url is found in the url_mapping array, | 57 // We need to check if the passed in url is found in the url_mapping array, |
53 // in that case use the mapped entry. | 58 // in that case use the mapped entry. |
54 intptr_t len = strlen(url_string); | 59 intptr_t len = strlen(url_string); |
55 for (intptr_t idx = 0; idx < url_mapping->count(); idx++) { | 60 for (intptr_t idx = 0; idx < url_mapping->count(); idx++) { |
56 const char* url_name = url_mapping->GetArgument(idx); | 61 const char* url_name = url_mapping->GetArgument(idx); |
57 if (!strncmp(url_string, url_name, len) && (url_name[len] == ',')) { | 62 if (!strncmp(url_string, url_name, len) && (url_name[len] == ',')) { |
58 const char* url_mapped_name = url_name + len + 1; | 63 const char* url_mapped_name = url_name + len + 1; |
(...skipping 411 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
470 void DartUtils::WriteMagicNumber(File* file) { | 475 void DartUtils::WriteMagicNumber(File* file) { |
471 // Write a magic number and version information into the snapshot file. | 476 // Write a magic number and version information into the snapshot file. |
472 bool bytes_written = file->WriteFully(magic_number, sizeof(magic_number)); | 477 bool bytes_written = file->WriteFully(magic_number, sizeof(magic_number)); |
473 ASSERT(bytes_written); | 478 ASSERT(bytes_written); |
474 } | 479 } |
475 | 480 |
476 | 481 |
477 Dart_Handle DartUtils::LoadScript(const char* script_uri, | 482 Dart_Handle DartUtils::LoadScript(const char* script_uri, |
478 Dart_Handle builtin_lib) { | 483 Dart_Handle builtin_lib) { |
479 Dart_Handle uri = Dart_NewStringFromCString(script_uri); | 484 Dart_Handle uri = Dart_NewStringFromCString(script_uri); |
| 485 |
| 486 #if defined(LOAD_VIA_SERVICE_ISOLATE) |
| 487 Dart_Port load_port = Dart_ServiceWaitForLoadPort(); |
| 488 if (load_port == ILLEGAL_PORT) { |
| 489 return NewDartUnsupportedError("Service did not return load port."); |
| 490 } |
| 491 Builtin::SetLoadPort(load_port); |
| 492 #endif |
| 493 |
480 return LoadDataAsync_Invoke(Dart_Null(), uri, Dart_Null(), builtin_lib); | 494 return LoadDataAsync_Invoke(Dart_Null(), uri, Dart_Null(), builtin_lib); |
481 } | 495 } |
482 | 496 |
483 | 497 |
484 // Callback function, gets called from asynchronous script and library | 498 // Callback function, gets called from asynchronous script and library |
485 // reading code when there is an i/o error. | 499 // reading code when there is an i/o error. |
486 void FUNCTION_NAME(Builtin_AsyncLoadError)(Dart_NativeArguments args) { | 500 void FUNCTION_NAME(Builtin_AsyncLoadError)(Dart_NativeArguments args) { |
487 // Dart_Handle source_uri = Dart_GetNativeArgument(args, 0); | 501 // Dart_Handle source_uri = Dart_GetNativeArgument(args, 0); |
488 Dart_Handle library_uri = Dart_GetNativeArgument(args, 1); | 502 Dart_Handle library_uri = Dart_GetNativeArgument(args, 1); |
489 Dart_Handle error = Dart_GetNativeArgument(args, 2); | 503 Dart_Handle error = Dart_GetNativeArgument(args, 2); |
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
583 Dart_Handle res = Dart_FinalizeLoading(true); | 597 Dart_Handle res = Dart_FinalizeLoading(true); |
584 if (Dart_IsError(res)) { | 598 if (Dart_IsError(res)) { |
585 // TODO(hausner): If compilation/loading errors are supposed to | 599 // TODO(hausner): If compilation/loading errors are supposed to |
586 // be observable by the program, we need to mark the bad library | 600 // be observable by the program, we need to mark the bad library |
587 // with the error instead of propagating it. | 601 // with the error instead of propagating it. |
588 Dart_PropagateError(res); | 602 Dart_PropagateError(res); |
589 } | 603 } |
590 } | 604 } |
591 | 605 |
592 | 606 |
| 607 void FUNCTION_NAME(Builtin_NativeLibraryExtension)(Dart_NativeArguments args) { |
| 608 const char* suffix = Platform::LibraryExtension(); |
| 609 ASSERT(suffix != NULL); |
| 610 Dart_Handle res = Dart_NewStringFromCString(suffix); |
| 611 if (Dart_IsError(res)) { |
| 612 Dart_PropagateError(res); |
| 613 } |
| 614 Dart_SetReturnValue(args, res); |
| 615 } |
| 616 |
| 617 |
| 618 void DartUtils::PrepareBuiltinLibrary(Dart_Handle builtin_lib, |
| 619 Dart_Handle internal_lib, |
| 620 bool is_service_isolate, |
| 621 const char* package_root) { |
| 622 // Setup the internal library's 'internalPrint' function. |
| 623 Dart_Handle print = Dart_Invoke( |
| 624 builtin_lib, NewString("_getPrintClosure"), 0, NULL); |
| 625 DART_CHECK_VALID(print); |
| 626 Dart_Handle result = |
| 627 Dart_SetField(internal_lib, NewString("_printClosure"), print); |
| 628 DART_CHECK_VALID(result); |
| 629 |
| 630 if (!is_service_isolate) { |
| 631 result = |
| 632 Dart_SetField(builtin_lib, NewString("_isWindows"), |
| 633 IsWindowsHost() ? Dart_True() : Dart_False()); |
| 634 DART_CHECK_VALID(result); |
| 635 } |
| 636 |
| 637 #if defined(LOAD_VIA_SERVICE_ISOLATE) |
| 638 result = Dart_SetField(builtin_lib, NewString("_load_via_service_isolate"), |
| 639 Dart_True()); |
| 640 DART_CHECK_VALID(result); |
| 641 #endif |
| 642 |
| 643 if (!is_service_isolate) { |
| 644 // Set current working directory. |
| 645 result = SetWorkingDirectory(builtin_lib); |
| 646 DART_CHECK_VALID(result); |
| 647 } |
| 648 |
| 649 // Set up package root if specified. |
| 650 if (package_root != NULL) { |
| 651 result = NewString(package_root); |
| 652 DART_CHECK_VALID(result); |
| 653 const int kNumArgs = 1; |
| 654 Dart_Handle dart_args[kNumArgs]; |
| 655 dart_args[0] = result; |
| 656 result = Dart_Invoke(builtin_lib, |
| 657 NewString("_setPackageRoot"), |
| 658 kNumArgs, |
| 659 dart_args); |
| 660 DART_CHECK_VALID(result); |
| 661 } |
| 662 } |
| 663 |
| 664 |
| 665 void DartUtils::PrepareCoreLibrary(Dart_Handle core_lib, |
| 666 Dart_Handle builtin_lib, |
| 667 bool is_service_isolate) { |
| 668 if (!is_service_isolate) { |
| 669 // Setup the 'Uri.base' getter in dart:core. |
| 670 Dart_Handle uri_base = Dart_Invoke( |
| 671 builtin_lib, NewString("_getUriBaseClosure"), 0, NULL); |
| 672 DART_CHECK_VALID(uri_base); |
| 673 Dart_Handle result = Dart_SetField(core_lib, |
| 674 NewString("_uriBaseClosure"), |
| 675 uri_base); |
| 676 DART_CHECK_VALID(result); |
| 677 } |
| 678 } |
| 679 |
| 680 |
| 681 void DartUtils::PrepareAsyncLibrary(Dart_Handle async_lib, |
| 682 Dart_Handle isolate_lib) { |
| 683 Dart_Handle schedule_immediate_closure = |
| 684 Dart_Invoke(isolate_lib, NewString("_getIsolateScheduleImmediateClosure"), |
| 685 0, NULL); |
| 686 Dart_Handle args[1]; |
| 687 args[0] = schedule_immediate_closure; |
| 688 DART_CHECK_VALID(Dart_Invoke( |
| 689 async_lib, NewString("_setScheduleImmediateClosure"), 1, args)); |
| 690 } |
| 691 |
| 692 |
| 693 void DartUtils::PrepareIOLibrary(Dart_Handle io_lib) { |
| 694 DART_CHECK_VALID(Dart_Invoke(io_lib, NewString("_setupHooks"), 0, NULL)); |
| 695 } |
| 696 |
| 697 |
| 698 void DartUtils::PrepareIsolateLibrary(Dart_Handle isolate_lib) { |
| 699 DART_CHECK_VALID(Dart_Invoke(isolate_lib, NewString("_setupHooks"), 0, NULL)); |
| 700 } |
| 701 |
| 702 |
593 Dart_Handle DartUtils::PrepareForScriptLoading(const char* package_root, | 703 Dart_Handle DartUtils::PrepareForScriptLoading(const char* package_root, |
| 704 bool is_service_isolate, |
594 Dart_Handle builtin_lib) { | 705 Dart_Handle builtin_lib) { |
595 // First ensure all required libraries are available. | 706 // First ensure all required libraries are available. |
596 Dart_Handle url = NewString(kAsyncLibURL); | 707 Dart_Handle url = NewString(kCoreLibURL); |
| 708 DART_CHECK_VALID(url); |
| 709 Dart_Handle core_lib = Dart_LookupLibrary(url); |
| 710 DART_CHECK_VALID(core_lib); |
| 711 url = NewString(kAsyncLibURL); |
597 DART_CHECK_VALID(url); | 712 DART_CHECK_VALID(url); |
598 Dart_Handle async_lib = Dart_LookupLibrary(url); | 713 Dart_Handle async_lib = Dart_LookupLibrary(url); |
599 DART_CHECK_VALID(async_lib); | 714 DART_CHECK_VALID(async_lib); |
600 url = NewString(kIsolateLibURL); | 715 url = NewString(kIsolateLibURL); |
601 DART_CHECK_VALID(url); | 716 DART_CHECK_VALID(url); |
602 Dart_Handle isolate_lib = Dart_LookupLibrary(url); | 717 Dart_Handle isolate_lib = Dart_LookupLibrary(url); |
603 DART_CHECK_VALID(isolate_lib); | 718 DART_CHECK_VALID(isolate_lib); |
604 url = NewString(kInternalLibURL); | 719 url = NewString(kInternalLibURL); |
605 DART_CHECK_VALID(url); | 720 DART_CHECK_VALID(url); |
606 Dart_Handle internal_lib = Dart_LookupLibrary(url); | 721 Dart_Handle internal_lib = Dart_LookupLibrary(url); |
607 DART_CHECK_VALID(internal_lib); | 722 DART_CHECK_VALID(internal_lib); |
608 | |
609 Dart_Handle io_lib = Builtin::LoadAndCheckLibrary(Builtin::kIOLibrary); | 723 Dart_Handle io_lib = Builtin::LoadAndCheckLibrary(Builtin::kIOLibrary); |
610 DART_CHECK_VALID(io_lib); | 724 DART_CHECK_VALID(io_lib); |
611 | 725 |
612 // We need to ensure that all the scripts loaded so far are finalized | 726 // We need to ensure that all the scripts loaded so far are finalized |
613 // as we are about to invoke some Dart code below to setup closures. | 727 // as we are about to invoke some Dart code below to setup closures. |
614 Dart_Handle result = Dart_FinalizeLoading(false); | 728 Dart_Handle result = Dart_FinalizeLoading(false); |
615 DART_CHECK_VALID(result); | 729 DART_CHECK_VALID(result); |
616 | 730 |
617 // Setup the internal library's 'internalPrint' function. | 731 PrepareBuiltinLibrary(builtin_lib, |
618 Dart_Handle print = Dart_Invoke( | 732 internal_lib, |
619 builtin_lib, NewString("_getPrintClosure"), 0, NULL); | 733 is_service_isolate, |
620 result = Dart_SetField(internal_lib, | 734 package_root); |
621 NewString("_printClosure"), | 735 PrepareAsyncLibrary(async_lib, isolate_lib); |
622 print); | 736 PrepareCoreLibrary(core_lib, builtin_lib, is_service_isolate); |
623 DART_CHECK_VALID(result); | 737 PrepareIsolateLibrary(isolate_lib); |
624 | 738 PrepareIOLibrary(io_lib); |
625 DART_CHECK_VALID(Dart_Invoke(isolate_lib, NewString("_setupHooks"), 0, NULL)); | |
626 DART_CHECK_VALID(Dart_Invoke(io_lib, NewString("_setupHooks"), 0, NULL)); | |
627 | |
628 | |
629 // Setup the 'scheduleImmediate' closure. | |
630 Dart_Handle schedule_immediate_closure = | |
631 Dart_Invoke(isolate_lib, NewString("_getIsolateScheduleImmediateClosure"), | |
632 0, NULL); | |
633 Dart_Handle args[1]; | |
634 args[0] = schedule_immediate_closure; | |
635 DART_CHECK_VALID(Dart_Invoke( | |
636 async_lib, NewString("_setScheduleImmediateClosure"), 1, args)); | |
637 | |
638 // Setup the corelib 'Uri.base' getter. | |
639 url = NewString(kCoreLibURL); | |
640 DART_CHECK_VALID(url); | |
641 Dart_Handle corelib = Dart_LookupLibrary(url); | |
642 DART_CHECK_VALID(corelib); | |
643 Dart_Handle uri_base = Dart_Invoke( | |
644 builtin_lib, NewString("_getUriBaseClosure"), 0, NULL); | |
645 DART_CHECK_VALID(uri_base); | |
646 result = Dart_SetField(corelib, | |
647 NewString("_uriBaseClosure"), | |
648 uri_base); | |
649 DART_CHECK_VALID(result); | |
650 | |
651 if (IsWindowsHost()) { | |
652 // Set running on Windows flag. | |
653 result = Dart_Invoke(builtin_lib, NewString("_setWindows"), 0, NULL); | |
654 if (Dart_IsError(result)) { | |
655 return result; | |
656 } | |
657 } | |
658 | |
659 // Set current working directory. | |
660 result = SetWorkingDirectory(builtin_lib); | |
661 if (Dart_IsError(result)) { | |
662 return result; | |
663 } | |
664 | |
665 // Set up package root if specified. | |
666 if (package_root != NULL) { | |
667 result = NewString(package_root); | |
668 if (!Dart_IsError(result)) { | |
669 const int kNumArgs = 1; | |
670 Dart_Handle dart_args[kNumArgs]; | |
671 dart_args[0] = result; | |
672 return Dart_Invoke(builtin_lib, | |
673 NewString("_setPackageRoot"), | |
674 kNumArgs, | |
675 dart_args); | |
676 } | |
677 } | |
678 return result; | 739 return result; |
679 } | 740 } |
680 | 741 |
681 | 742 |
| 743 void DartUtils::SetupIOLibrary(const char* script_uri) { |
| 744 Dart_Handle io_lib_url = NewString(kIOLibURL); |
| 745 DART_CHECK_VALID(io_lib_url); |
| 746 Dart_Handle io_lib = Dart_LookupLibrary(io_lib_url); |
| 747 DART_CHECK_VALID(io_lib); |
| 748 Dart_Handle platform_type = GetDartType(DartUtils::kIOLibURL, "_Platform"); |
| 749 DART_CHECK_VALID(platform_type); |
| 750 Dart_Handle script_name = NewString("_nativeScript"); |
| 751 DART_CHECK_VALID(script_name); |
| 752 Dart_Handle dart_script = NewString(script_uri); |
| 753 DART_CHECK_VALID(dart_script); |
| 754 Dart_Handle set_script_name = |
| 755 Dart_SetField(platform_type, script_name, dart_script); |
| 756 DART_CHECK_VALID(set_script_name); |
| 757 } |
| 758 |
| 759 |
682 bool DartUtils::PostNull(Dart_Port port_id) { | 760 bool DartUtils::PostNull(Dart_Port port_id) { |
683 // Post a message with just the null object. | 761 // Post a message with just the null object. |
684 return Dart_PostCObject(port_id, CObject::Null()->AsApiCObject()); | 762 return Dart_PostCObject(port_id, CObject::Null()->AsApiCObject()); |
685 } | 763 } |
686 | 764 |
687 | 765 |
688 bool DartUtils::PostInt32(Dart_Port port_id, int32_t value) { | 766 bool DartUtils::PostInt32(Dart_Port port_id, int32_t value) { |
689 // Post a message with the integer value. | 767 // Post a message with the integer value. |
690 int32_t min = 0xc0000000; // -1073741824 | 768 int32_t min = 0xc0000000; // -1073741824 |
691 int32_t max = 0x3fffffff; // 1073741823 | 769 int32_t max = 0x3fffffff; // 1073741823 |
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
761 } | 839 } |
762 | 840 |
763 | 841 |
764 Dart_Handle DartUtils::NewDartArgumentError(const char* message) { | 842 Dart_Handle DartUtils::NewDartArgumentError(const char* message) { |
765 return NewDartExceptionWithMessage(kCoreLibURL, | 843 return NewDartExceptionWithMessage(kCoreLibURL, |
766 "ArgumentError", | 844 "ArgumentError", |
767 message); | 845 message); |
768 } | 846 } |
769 | 847 |
770 | 848 |
| 849 Dart_Handle DartUtils::NewDartUnsupportedError(const char* message) { |
| 850 return NewDartExceptionWithMessage(kCoreLibURL, |
| 851 "UnsupportedError", |
| 852 message); |
| 853 } |
| 854 |
| 855 |
771 Dart_Handle DartUtils::NewDartIOException(const char* exception_name, | 856 Dart_Handle DartUtils::NewDartIOException(const char* exception_name, |
772 const char* message, | 857 const char* message, |
773 Dart_Handle os_error) { | 858 Dart_Handle os_error) { |
774 // Create a dart:io exception object of the given type. | 859 // Create a dart:io exception object of the given type. |
775 return NewDartExceptionWithOSError(kIOLibURL, | 860 return NewDartExceptionWithOSError(kIOLibURL, |
776 exception_name, | 861 exception_name, |
777 message, | 862 message, |
778 os_error); | 863 os_error); |
779 } | 864 } |
780 | 865 |
(...skipping 329 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1110 new CObjectString(CObject::NewString(os_error->message())); | 1195 new CObjectString(CObject::NewString(os_error->message())); |
1111 CObjectArray* result = new CObjectArray(CObject::NewArray(3)); | 1196 CObjectArray* result = new CObjectArray(CObject::NewArray(3)); |
1112 result->SetAt(0, new CObjectInt32(CObject::NewInt32(kOSError))); | 1197 result->SetAt(0, new CObjectInt32(CObject::NewInt32(kOSError))); |
1113 result->SetAt(1, new CObjectInt32(CObject::NewInt32(os_error->code()))); | 1198 result->SetAt(1, new CObjectInt32(CObject::NewInt32(os_error->code()))); |
1114 result->SetAt(2, error_message); | 1199 result->SetAt(2, error_message); |
1115 return result; | 1200 return result; |
1116 } | 1201 } |
1117 | 1202 |
1118 } // namespace bin | 1203 } // namespace bin |
1119 } // namespace dart | 1204 } // namespace dart |
OLD | NEW |