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 442 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
470 void DartUtils::WriteMagicNumber(File* file) { | 471 void DartUtils::WriteMagicNumber(File* file) { |
471 // Write a magic number and version information into the snapshot file. | 472 // Write a magic number and version information into the snapshot file. |
472 bool bytes_written = file->WriteFully(magic_number, sizeof(magic_number)); | 473 bool bytes_written = file->WriteFully(magic_number, sizeof(magic_number)); |
473 ASSERT(bytes_written); | 474 ASSERT(bytes_written); |
474 } | 475 } |
475 | 476 |
476 | 477 |
477 Dart_Handle DartUtils::LoadScript(const char* script_uri, | 478 Dart_Handle DartUtils::LoadScript(const char* script_uri, |
478 Dart_Handle builtin_lib) { | 479 Dart_Handle builtin_lib) { |
479 Dart_Handle uri = Dart_NewStringFromCString(script_uri); | 480 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 |
480 return LoadDataAsync_Invoke(Dart_Null(), uri, Dart_Null(), builtin_lib); | 488 return LoadDataAsync_Invoke(Dart_Null(), uri, Dart_Null(), builtin_lib); |
481 } | 489 } |
482 | 490 |
483 | 491 |
484 // Callback function, gets called from asynchronous script and library | 492 // Callback function, gets called from asynchronous script and library |
485 // reading code when there is an i/o error. | 493 // reading code when there is an i/o error. |
486 void FUNCTION_NAME(Builtin_AsyncLoadError)(Dart_NativeArguments args) { | 494 void FUNCTION_NAME(Builtin_AsyncLoadError)(Dart_NativeArguments args) { |
487 // Dart_Handle source_uri = Dart_GetNativeArgument(args, 0); | 495 // Dart_Handle source_uri = Dart_GetNativeArgument(args, 0); |
488 Dart_Handle library_uri = Dart_GetNativeArgument(args, 1); | 496 Dart_Handle library_uri = Dart_GetNativeArgument(args, 1); |
489 Dart_Handle error = Dart_GetNativeArgument(args, 2); | 497 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); | 591 Dart_Handle res = Dart_FinalizeLoading(true); |
584 if (Dart_IsError(res)) { | 592 if (Dart_IsError(res)) { |
585 // TODO(hausner): If compilation/loading errors are supposed to | 593 // TODO(hausner): If compilation/loading errors are supposed to |
586 // be observable by the program, we need to mark the bad library | 594 // be observable by the program, we need to mark the bad library |
587 // with the error instead of propagating it. | 595 // with the error instead of propagating it. |
588 Dart_PropagateError(res); | 596 Dart_PropagateError(res); |
589 } | 597 } |
590 } | 598 } |
591 | 599 |
592 | 600 |
| 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 |
593 Dart_Handle DartUtils::PrepareForScriptLoading(const char* package_root, | 696 Dart_Handle DartUtils::PrepareForScriptLoading(const char* package_root, |
594 Dart_Handle builtin_lib) { | 697 Dart_Handle builtin_lib) { |
595 // First ensure all required libraries are available. | 698 // First ensure all required libraries are available. |
596 Dart_Handle url = NewString(kAsyncLibURL); | 699 Dart_Handle url = NewString(kAsyncLibURL); |
597 DART_CHECK_VALID(url); | 700 DART_CHECK_VALID(url); |
598 Dart_Handle async_lib = Dart_LookupLibrary(url); | 701 Dart_Handle async_lib = Dart_LookupLibrary(url); |
599 DART_CHECK_VALID(async_lib); | 702 DART_CHECK_VALID(async_lib); |
600 Dart_Handle io_lib = Builtin::LoadAndCheckLibrary(Builtin::kIOLibrary); | 703 Dart_Handle io_lib = Builtin::LoadAndCheckLibrary(Builtin::kIOLibrary); |
601 | 704 |
602 // We need to ensure that all the scripts loaded so far are finalized | 705 // We need to ensure that all the scripts loaded so far are finalized |
603 // as we are about to invoke some Dart code below to setup closures. | 706 // as we are about to invoke some Dart code below to setup closures. |
604 Dart_Handle result = Dart_FinalizeLoading(false); | 707 Dart_Handle result = Dart_FinalizeLoading(false); |
605 DART_CHECK_VALID(result); | 708 DART_CHECK_VALID(result); |
606 | 709 |
607 // Setup the internal library's 'internalPrint' function. | 710 PrepareBuiltinLibrary(builtin_lib, package_root); |
608 Dart_Handle print = Dart_Invoke( | 711 PrepareAsyncLibrary(async_lib, io_lib); |
609 builtin_lib, NewString("_getPrintClosure"), 0, NULL); | 712 PrepareCoreLibrary(builtin_lib); |
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); | |
618 | 713 |
619 // Setup the 'timer' factory. | 714 return result; |
620 Dart_Handle timer_closure = | 715 } |
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 | 716 |
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 | 717 |
639 // Setup the corelib 'Uri.base' getter. | 718 void DartUtils::SetupIOLibrary(const char* script_uri) { |
640 url = NewString(kCoreLibURL); | 719 Dart_Handle io_lib_url = NewString(kIOLibURL); |
641 DART_CHECK_VALID(url); | 720 DART_CHECK_VALID(io_lib_url); |
642 Dart_Handle corelib = Dart_LookupLibrary(url); | 721 Dart_Handle io_lib = Dart_LookupLibrary(io_lib_url); |
643 DART_CHECK_VALID(corelib); | 722 DART_CHECK_VALID(io_lib); |
644 Dart_Handle uri_base = Dart_Invoke( | 723 Dart_Handle platform_type = GetDartType(DartUtils::kIOLibURL, "_Platform"); |
645 builtin_lib, NewString("_getUriBaseClosure"), 0, NULL); | 724 DART_CHECK_VALID(platform_type); |
646 DART_CHECK_VALID(uri_base); | 725 Dart_Handle script_name = NewString("_nativeScript"); |
647 result = Dart_SetField(corelib, | 726 DART_CHECK_VALID(script_name); |
648 NewString("_uriBaseClosure"), | 727 Dart_Handle dart_script = NewString(script_uri); |
649 uri_base); | 728 DART_CHECK_VALID(dart_script); |
650 DART_CHECK_VALID(result); | 729 Dart_Handle set_script_name = |
651 | 730 Dart_SetField(platform_type, script_name, dart_script); |
652 if (IsWindowsHost()) { | 731 DART_CHECK_VALID(set_script_name); |
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 } | |
679 return result; | |
680 } | 732 } |
681 | 733 |
682 | 734 |
683 bool DartUtils::PostNull(Dart_Port port_id) { | 735 bool DartUtils::PostNull(Dart_Port port_id) { |
684 // Post a message with just the null object. | 736 // Post a message with just the null object. |
685 return Dart_PostCObject(port_id, CObject::Null()->AsApiCObject()); | 737 return Dart_PostCObject(port_id, CObject::Null()->AsApiCObject()); |
686 } | 738 } |
687 | 739 |
688 | 740 |
689 bool DartUtils::PostInt32(Dart_Port port_id, int32_t value) { | 741 bool DartUtils::PostInt32(Dart_Port port_id, int32_t value) { |
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
762 } | 814 } |
763 | 815 |
764 | 816 |
765 Dart_Handle DartUtils::NewDartArgumentError(const char* message) { | 817 Dart_Handle DartUtils::NewDartArgumentError(const char* message) { |
766 return NewDartExceptionWithMessage(kCoreLibURL, | 818 return NewDartExceptionWithMessage(kCoreLibURL, |
767 "ArgumentError", | 819 "ArgumentError", |
768 message); | 820 message); |
769 } | 821 } |
770 | 822 |
771 | 823 |
| 824 Dart_Handle DartUtils::NewDartUnsupportedError(const char* message) { |
| 825 return NewDartExceptionWithMessage(kCoreLibURL, |
| 826 "UnsupportedError", |
| 827 message); |
| 828 } |
| 829 |
| 830 |
772 Dart_Handle DartUtils::NewDartIOException(const char* exception_name, | 831 Dart_Handle DartUtils::NewDartIOException(const char* exception_name, |
773 const char* message, | 832 const char* message, |
774 Dart_Handle os_error) { | 833 Dart_Handle os_error) { |
775 // Create a dart:io exception object of the given type. | 834 // Create a dart:io exception object of the given type. |
776 return NewDartExceptionWithOSError(kIOLibURL, | 835 return NewDartExceptionWithOSError(kIOLibURL, |
777 exception_name, | 836 exception_name, |
778 message, | 837 message, |
779 os_error); | 838 os_error); |
780 } | 839 } |
781 | 840 |
(...skipping 329 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1111 new CObjectString(CObject::NewString(os_error->message())); | 1170 new CObjectString(CObject::NewString(os_error->message())); |
1112 CObjectArray* result = new CObjectArray(CObject::NewArray(3)); | 1171 CObjectArray* result = new CObjectArray(CObject::NewArray(3)); |
1113 result->SetAt(0, new CObjectInt32(CObject::NewInt32(kOSError))); | 1172 result->SetAt(0, new CObjectInt32(CObject::NewInt32(kOSError))); |
1114 result->SetAt(1, new CObjectInt32(CObject::NewInt32(os_error->code()))); | 1173 result->SetAt(1, new CObjectInt32(CObject::NewInt32(os_error->code()))); |
1115 result->SetAt(2, error_message); | 1174 result->SetAt(2, error_message); |
1116 return result; | 1175 return result; |
1117 } | 1176 } |
1118 | 1177 |
1119 } // namespace bin | 1178 } // namespace bin |
1120 } // namespace dart | 1179 } // namespace dart |
OLD | NEW |