| 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" | |
| 19 #include "bin/socket.h" | 18 #include "bin/socket.h" |
| 20 #include "bin/utils.h" | 19 #include "bin/utils.h" |
| 21 | 20 |
| 22 namespace dart { | 21 namespace dart { |
| 23 namespace bin { | 22 namespace bin { |
| 24 | 23 |
| 25 const char* DartUtils::original_working_directory = NULL; | 24 const char* DartUtils::original_working_directory = NULL; |
| 26 const char* DartUtils::kDartScheme = "dart:"; | 25 const char* DartUtils::kDartScheme = "dart:"; |
| 27 const char* DartUtils::kDartExtensionScheme = "dart-ext:"; | 26 const char* DartUtils::kDartExtensionScheme = "dart-ext:"; |
| 28 const char* DartUtils::kAsyncLibURL = "dart:async"; | 27 const char* DartUtils::kAsyncLibURL = "dart:async"; |
| (...skipping 442 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 471 void DartUtils::WriteMagicNumber(File* file) { | 470 void DartUtils::WriteMagicNumber(File* file) { |
| 472 // Write a magic number and version information into the snapshot file. | 471 // Write a magic number and version information into the snapshot file. |
| 473 bool bytes_written = file->WriteFully(magic_number, sizeof(magic_number)); | 472 bool bytes_written = file->WriteFully(magic_number, sizeof(magic_number)); |
| 474 ASSERT(bytes_written); | 473 ASSERT(bytes_written); |
| 475 } | 474 } |
| 476 | 475 |
| 477 | 476 |
| 478 Dart_Handle DartUtils::LoadScript(const char* script_uri, | 477 Dart_Handle DartUtils::LoadScript(const char* script_uri, |
| 479 Dart_Handle builtin_lib) { | 478 Dart_Handle builtin_lib) { |
| 480 Dart_Handle uri = Dart_NewStringFromCString(script_uri); | 479 Dart_Handle uri = Dart_NewStringFromCString(script_uri); |
| 481 | |
| 482 Dart_Port load_port = Dart_ServiceWaitForLoadPort(); | |
| 483 if (load_port == ILLEGAL_PORT) { | |
| 484 return NewDartUnsupportedError("Service did not return load port."); | |
| 485 } | |
| 486 Builtin::SetLoadPort(load_port); | |
| 487 | |
| 488 return LoadDataAsync_Invoke(Dart_Null(), uri, Dart_Null(), builtin_lib); | 480 return LoadDataAsync_Invoke(Dart_Null(), uri, Dart_Null(), builtin_lib); |
| 489 } | 481 } |
| 490 | 482 |
| 491 | 483 |
| 492 // Callback function, gets called from asynchronous script and library | 484 // Callback function, gets called from asynchronous script and library |
| 493 // reading code when there is an i/o error. | 485 // reading code when there is an i/o error. |
| 494 void FUNCTION_NAME(Builtin_AsyncLoadError)(Dart_NativeArguments args) { | 486 void FUNCTION_NAME(Builtin_AsyncLoadError)(Dart_NativeArguments args) { |
| 495 // Dart_Handle source_uri = Dart_GetNativeArgument(args, 0); | 487 // Dart_Handle source_uri = Dart_GetNativeArgument(args, 0); |
| 496 Dart_Handle library_uri = Dart_GetNativeArgument(args, 1); | 488 Dart_Handle library_uri = Dart_GetNativeArgument(args, 1); |
| 497 Dart_Handle error = Dart_GetNativeArgument(args, 2); | 489 Dart_Handle error = Dart_GetNativeArgument(args, 2); |
| (...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 591 Dart_Handle res = Dart_FinalizeLoading(true); | 583 Dart_Handle res = Dart_FinalizeLoading(true); |
| 592 if (Dart_IsError(res)) { | 584 if (Dart_IsError(res)) { |
| 593 // TODO(hausner): If compilation/loading errors are supposed to | 585 // TODO(hausner): If compilation/loading errors are supposed to |
| 594 // be observable by the program, we need to mark the bad library | 586 // be observable by the program, we need to mark the bad library |
| 595 // with the error instead of propagating it. | 587 // with the error instead of propagating it. |
| 596 Dart_PropagateError(res); | 588 Dart_PropagateError(res); |
| 597 } | 589 } |
| 598 } | 590 } |
| 599 | 591 |
| 600 | 592 |
| 601 void FUNCTION_NAME(Builtin_NativeLibraryExtension)(Dart_NativeArguments args) { | |
| 602 const char* suffix = Platform::LibraryExtension(); | |
| 603 ASSERT(suffix != NULL); | |
| 604 Dart_Handle res = Dart_NewStringFromCString(suffix); | |
| 605 if (Dart_IsError(res)) { | |
| 606 Dart_PropagateError(res); | |
| 607 } | |
| 608 Dart_SetReturnValue(args, res); | |
| 609 } | |
| 610 | |
| 611 | |
| 612 void DartUtils::PrepareBuiltinLibrary(Dart_Handle builtin_lib, | |
| 613 const char* package_root) { | |
| 614 Dart_Handle url = NewString(kInternalLibURL); | |
| 615 DART_CHECK_VALID(url); | |
| 616 Dart_Handle internal_lib = Dart_LookupLibrary(url); | |
| 617 DART_CHECK_VALID(internal_lib); | |
| 618 | |
| 619 // Setup the internal library's 'internalPrint' function. | |
| 620 Dart_Handle print = Dart_Invoke( | |
| 621 builtin_lib, NewString("_getPrintClosure"), 0, NULL); | |
| 622 DART_CHECK_VALID(print); | |
| 623 Dart_Handle result = | |
| 624 Dart_SetField(internal_lib, NewString("_printClosure"), print); | |
| 625 DART_CHECK_VALID(result); | |
| 626 | |
| 627 if (IsWindowsHost()) { | |
| 628 // Set running on Windows flag. | |
| 629 result = Dart_Invoke(builtin_lib, NewString("_setWindows"), 0, NULL); | |
| 630 DART_CHECK_VALID(result); | |
| 631 } | |
| 632 | |
| 633 // Set current working directory. | |
| 634 result = SetWorkingDirectory(builtin_lib); | |
| 635 DART_CHECK_VALID(result); | |
| 636 | |
| 637 // Set up package root if specified. | |
| 638 if (package_root != NULL) { | |
| 639 result = NewString(package_root); | |
| 640 DART_CHECK_VALID(result); | |
| 641 const int kNumArgs = 1; | |
| 642 Dart_Handle dart_args[kNumArgs]; | |
| 643 dart_args[0] = result; | |
| 644 result = Dart_Invoke(builtin_lib, | |
| 645 NewString("_setPackageRoot"), | |
| 646 kNumArgs, | |
| 647 dart_args); | |
| 648 DART_CHECK_VALID(result); | |
| 649 } | |
| 650 } | |
| 651 | |
| 652 | |
| 653 void DartUtils::PrepareCoreLibrary(Dart_Handle builtin_lib) { | |
| 654 Dart_Handle url = NewString(kCoreLibURL); | |
| 655 DART_CHECK_VALID(url); | |
| 656 Dart_Handle core_lib = Dart_LookupLibrary(url); | |
| 657 DART_CHECK_VALID(core_lib); | |
| 658 | |
| 659 // Setup the 'Uri.base' getter in dart:core. | |
| 660 Dart_Handle uri_base = Dart_Invoke( | |
| 661 builtin_lib, NewString("_getUriBaseClosure"), 0, NULL); | |
| 662 DART_CHECK_VALID(uri_base); | |
| 663 Dart_Handle result = Dart_SetField(core_lib, | |
| 664 NewString("_uriBaseClosure"), | |
| 665 uri_base); | |
| 666 DART_CHECK_VALID(result); | |
| 667 } | |
| 668 | |
| 669 | |
| 670 void DartUtils::PrepareAsyncLibrary(Dart_Handle async_lib, | |
| 671 Dart_Handle io_lib) { | |
| 672 Dart_Handle url = NewString(kIsolateLibURL); | |
| 673 DART_CHECK_VALID(url); | |
| 674 Dart_Handle isolate_lib = Dart_LookupLibrary(url); | |
| 675 DART_CHECK_VALID(isolate_lib); | |
| 676 | |
| 677 // Setup the 'timer' factory in dart:async. | |
| 678 Dart_Handle timer_closure = | |
| 679 Dart_Invoke(io_lib, NewString("_getTimerFactoryClosure"), 0, NULL); | |
| 680 DART_CHECK_VALID(timer_closure); | |
| 681 Dart_Handle args[1]; | |
| 682 args[0] = timer_closure; | |
| 683 DART_CHECK_VALID(Dart_Invoke( | |
| 684 async_lib, NewString("_setTimerFactoryClosure"), 1, args)); | |
| 685 | |
| 686 // Setup the 'scheduleImmediate' closure in dart:async. | |
| 687 Dart_Handle schedule_immediate_closure = | |
| 688 Dart_Invoke(isolate_lib, NewString("_getIsolateScheduleImmediateClosure"), | |
| 689 0, NULL); | |
| 690 args[0] = schedule_immediate_closure; | |
| 691 DART_CHECK_VALID(Dart_Invoke( | |
| 692 async_lib, NewString("_setScheduleImmediateClosure"), 1, args)); | |
| 693 } | |
| 694 | |
| 695 | |
| 696 Dart_Handle DartUtils::PrepareForScriptLoading(const char* package_root, | 593 Dart_Handle DartUtils::PrepareForScriptLoading(const char* package_root, |
| 697 Dart_Handle builtin_lib) { | 594 Dart_Handle builtin_lib) { |
| 698 // First ensure all required libraries are available. | 595 // First ensure all required libraries are available. |
| 699 Dart_Handle url = NewString(kAsyncLibURL); | 596 Dart_Handle url = NewString(kAsyncLibURL); |
| 700 DART_CHECK_VALID(url); | 597 DART_CHECK_VALID(url); |
| 701 Dart_Handle async_lib = Dart_LookupLibrary(url); | 598 Dart_Handle async_lib = Dart_LookupLibrary(url); |
| 702 DART_CHECK_VALID(async_lib); | 599 DART_CHECK_VALID(async_lib); |
| 703 Dart_Handle io_lib = Builtin::LoadAndCheckLibrary(Builtin::kIOLibrary); | 600 Dart_Handle io_lib = Builtin::LoadAndCheckLibrary(Builtin::kIOLibrary); |
| 704 | 601 |
| 705 // We need to ensure that all the scripts loaded so far are finalized | 602 // We need to ensure that all the scripts loaded so far are finalized |
| 706 // as we are about to invoke some Dart code below to setup closures. | 603 // as we are about to invoke some Dart code below to setup closures. |
| 707 Dart_Handle result = Dart_FinalizeLoading(false); | 604 Dart_Handle result = Dart_FinalizeLoading(false); |
| 708 DART_CHECK_VALID(result); | 605 DART_CHECK_VALID(result); |
| 709 | 606 |
| 710 PrepareBuiltinLibrary(builtin_lib, package_root); | 607 // Setup the internal library's 'internalPrint' function. |
| 711 PrepareAsyncLibrary(async_lib, io_lib); | 608 Dart_Handle print = Dart_Invoke( |
| 712 PrepareCoreLibrary(builtin_lib); | 609 builtin_lib, NewString("_getPrintClosure"), 0, NULL); |
| 610 url = NewString(kInternalLibURL); |
| 611 DART_CHECK_VALID(url); |
| 612 Dart_Handle internal_lib = Dart_LookupLibrary(url); |
| 613 DART_CHECK_VALID(internal_lib); |
| 614 result = Dart_SetField(internal_lib, |
| 615 NewString("_printClosure"), |
| 616 print); |
| 617 DART_CHECK_VALID(result); |
| 713 | 618 |
| 619 // Setup the 'timer' factory. |
| 620 Dart_Handle timer_closure = |
| 621 Dart_Invoke(io_lib, NewString("_getTimerFactoryClosure"), 0, NULL); |
| 622 Dart_Handle args[1]; |
| 623 args[0] = timer_closure; |
| 624 DART_CHECK_VALID(Dart_Invoke( |
| 625 async_lib, NewString("_setTimerFactoryClosure"), 1, args)); |
| 626 |
| 627 // Setup the 'scheduleImmediate' closure. |
| 628 url = NewString(kIsolateLibURL); |
| 629 DART_CHECK_VALID(url); |
| 630 Dart_Handle isolate_lib = Dart_LookupLibrary(url); |
| 631 DART_CHECK_VALID(isolate_lib); |
| 632 Dart_Handle schedule_immediate_closure = |
| 633 Dart_Invoke(isolate_lib, NewString("_getIsolateScheduleImmediateClosure"), |
| 634 0, NULL); |
| 635 args[0] = schedule_immediate_closure; |
| 636 DART_CHECK_VALID(Dart_Invoke( |
| 637 async_lib, NewString("_setScheduleImmediateClosure"), 1, args)); |
| 638 |
| 639 // Setup the corelib 'Uri.base' getter. |
| 640 url = NewString(kCoreLibURL); |
| 641 DART_CHECK_VALID(url); |
| 642 Dart_Handle corelib = Dart_LookupLibrary(url); |
| 643 DART_CHECK_VALID(corelib); |
| 644 Dart_Handle uri_base = Dart_Invoke( |
| 645 builtin_lib, NewString("_getUriBaseClosure"), 0, NULL); |
| 646 DART_CHECK_VALID(uri_base); |
| 647 result = Dart_SetField(corelib, |
| 648 NewString("_uriBaseClosure"), |
| 649 uri_base); |
| 650 DART_CHECK_VALID(result); |
| 651 |
| 652 if (IsWindowsHost()) { |
| 653 // Set running on Windows flag. |
| 654 result = Dart_Invoke(builtin_lib, NewString("_setWindows"), 0, NULL); |
| 655 if (Dart_IsError(result)) { |
| 656 return result; |
| 657 } |
| 658 } |
| 659 |
| 660 // Set current working directory. |
| 661 result = SetWorkingDirectory(builtin_lib); |
| 662 if (Dart_IsError(result)) { |
| 663 return result; |
| 664 } |
| 665 |
| 666 // Set up package root if specified. |
| 667 if (package_root != NULL) { |
| 668 result = NewString(package_root); |
| 669 if (!Dart_IsError(result)) { |
| 670 const int kNumArgs = 1; |
| 671 Dart_Handle dart_args[kNumArgs]; |
| 672 dart_args[0] = result; |
| 673 return Dart_Invoke(builtin_lib, |
| 674 NewString("_setPackageRoot"), |
| 675 kNumArgs, |
| 676 dart_args); |
| 677 } |
| 678 } |
| 714 return result; | 679 return result; |
| 715 } | 680 } |
| 716 | 681 |
| 717 | 682 |
| 718 void DartUtils::SetupIOLibrary(const char* script_uri) { | |
| 719 Dart_Handle io_lib_url = NewString(kIOLibURL); | |
| 720 DART_CHECK_VALID(io_lib_url); | |
| 721 Dart_Handle io_lib = Dart_LookupLibrary(io_lib_url); | |
| 722 DART_CHECK_VALID(io_lib); | |
| 723 Dart_Handle platform_type = GetDartType(DartUtils::kIOLibURL, "_Platform"); | |
| 724 DART_CHECK_VALID(platform_type); | |
| 725 Dart_Handle script_name = NewString("_nativeScript"); | |
| 726 DART_CHECK_VALID(script_name); | |
| 727 Dart_Handle dart_script = NewString(script_uri); | |
| 728 DART_CHECK_VALID(dart_script); | |
| 729 Dart_Handle set_script_name = | |
| 730 Dart_SetField(platform_type, script_name, dart_script); | |
| 731 DART_CHECK_VALID(set_script_name); | |
| 732 } | |
| 733 | |
| 734 | |
| 735 bool DartUtils::PostNull(Dart_Port port_id) { | 683 bool DartUtils::PostNull(Dart_Port port_id) { |
| 736 // Post a message with just the null object. | 684 // Post a message with just the null object. |
| 737 return Dart_PostCObject(port_id, CObject::Null()->AsApiCObject()); | 685 return Dart_PostCObject(port_id, CObject::Null()->AsApiCObject()); |
| 738 } | 686 } |
| 739 | 687 |
| 740 | 688 |
| 741 bool DartUtils::PostInt32(Dart_Port port_id, int32_t value) { | 689 bool DartUtils::PostInt32(Dart_Port port_id, int32_t value) { |
| 742 // Post a message with the integer value. | 690 // Post a message with the integer value. |
| 743 int32_t min = 0xc0000000; // -1073741824 | 691 int32_t min = 0xc0000000; // -1073741824 |
| 744 int32_t max = 0x3fffffff; // 1073741823 | 692 int32_t max = 0x3fffffff; // 1073741823 |
| (...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 814 } | 762 } |
| 815 | 763 |
| 816 | 764 |
| 817 Dart_Handle DartUtils::NewDartArgumentError(const char* message) { | 765 Dart_Handle DartUtils::NewDartArgumentError(const char* message) { |
| 818 return NewDartExceptionWithMessage(kCoreLibURL, | 766 return NewDartExceptionWithMessage(kCoreLibURL, |
| 819 "ArgumentError", | 767 "ArgumentError", |
| 820 message); | 768 message); |
| 821 } | 769 } |
| 822 | 770 |
| 823 | 771 |
| 824 Dart_Handle DartUtils::NewDartUnsupportedError(const char* message) { | |
| 825 return NewDartExceptionWithMessage(kCoreLibURL, | |
| 826 "UnsupportedError", | |
| 827 message); | |
| 828 } | |
| 829 | |
| 830 | |
| 831 Dart_Handle DartUtils::NewDartIOException(const char* exception_name, | 772 Dart_Handle DartUtils::NewDartIOException(const char* exception_name, |
| 832 const char* message, | 773 const char* message, |
| 833 Dart_Handle os_error) { | 774 Dart_Handle os_error) { |
| 834 // Create a dart:io exception object of the given type. | 775 // Create a dart:io exception object of the given type. |
| 835 return NewDartExceptionWithOSError(kIOLibURL, | 776 return NewDartExceptionWithOSError(kIOLibURL, |
| 836 exception_name, | 777 exception_name, |
| 837 message, | 778 message, |
| 838 os_error); | 779 os_error); |
| 839 } | 780 } |
| 840 | 781 |
| (...skipping 329 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1170 new CObjectString(CObject::NewString(os_error->message())); | 1111 new CObjectString(CObject::NewString(os_error->message())); |
| 1171 CObjectArray* result = new CObjectArray(CObject::NewArray(3)); | 1112 CObjectArray* result = new CObjectArray(CObject::NewArray(3)); |
| 1172 result->SetAt(0, new CObjectInt32(CObject::NewInt32(kOSError))); | 1113 result->SetAt(0, new CObjectInt32(CObject::NewInt32(kOSError))); |
| 1173 result->SetAt(1, new CObjectInt32(CObject::NewInt32(os_error->code()))); | 1114 result->SetAt(1, new CObjectInt32(CObject::NewInt32(os_error->code()))); |
| 1174 result->SetAt(2, error_message); | 1115 result->SetAt(2, error_message); |
| 1175 return result; | 1116 return result; |
| 1176 } | 1117 } |
| 1177 | 1118 |
| 1178 } // namespace bin | 1119 } // namespace bin |
| 1179 } // namespace dart | 1120 } // namespace dart |
| OLD | NEW |