Chromium Code Reviews| 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 <stdlib.h> | 5 #include <stdlib.h> |
| 6 #include <string.h> | 6 #include <string.h> |
| 7 #include <stdio.h> | 7 #include <stdio.h> |
| 8 | 8 |
| 9 #include "include/dart_api.h" | 9 #include "include/dart_api.h" |
| 10 #include "include/dart_debugger_api.h" | 10 #include "include/dart_debugger_api.h" |
| (...skipping 671 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 682 return buffer; | 682 return buffer; |
| 683 } | 683 } |
| 684 | 684 |
| 685 | 685 |
| 686 // Exit code indicating a compilation error. | 686 // Exit code indicating a compilation error. |
| 687 static const int kCompilationErrorExitCode = 254; | 687 static const int kCompilationErrorExitCode = 254; |
| 688 | 688 |
| 689 // Exit code indicating an unhandled error that is not a compilation error. | 689 // Exit code indicating an unhandled error that is not a compilation error. |
| 690 static const int kErrorExitCode = 255; | 690 static const int kErrorExitCode = 255; |
| 691 | 691 |
| 692 static void Exit(int exit_code) { | |
|
Ivan Posva
2013/11/26 18:05:53
Seems like extra unneeded abstraction.
| |
| 693 exit(exit_code); | |
| 694 } | |
| 692 | 695 |
| 693 static int ErrorExit(int exit_code, const char* format, ...) { | 696 static void ErrorExit(int exit_code, const char* format, ...) { |
| 694 va_list arguments; | 697 va_list arguments; |
| 695 va_start(arguments, format); | 698 va_start(arguments, format); |
| 696 Log::VPrintErr(format, arguments); | 699 Log::VPrintErr(format, arguments); |
| 697 va_end(arguments); | 700 va_end(arguments); |
| 698 fflush(stderr); | 701 fflush(stderr); |
| 699 | 702 |
| 700 Dart_ExitScope(); | 703 Dart_ExitScope(); |
| 701 Dart_ShutdownIsolate(); | 704 Dart_ShutdownIsolate(); |
| 702 | 705 |
| 703 Dart_Cleanup(); | 706 Dart_Cleanup(); |
| 704 | 707 |
| 705 return exit_code; | 708 Exit(exit_code); |
| 706 } | 709 } |
| 707 | 710 |
| 708 | 711 |
| 709 static int DartErrorExit(Dart_Handle error) { | 712 static void DartErrorExit(Dart_Handle error) { |
|
Ivan Posva
2013/11/26 18:05:53
How about DartExitOnError(Dart_Handle result)? To
| |
| 710 const int exit_code = Dart_IsCompilationError(error) ? | 713 const int exit_code = Dart_IsCompilationError(error) ? |
| 711 kCompilationErrorExitCode : kErrorExitCode; | 714 kCompilationErrorExitCode : kErrorExitCode; |
| 712 return ErrorExit(exit_code, "%s\n", Dart_GetError(error)); | 715 ErrorExit(exit_code, "%s\n", Dart_GetError(error)); |
| 713 } | 716 } |
| 714 | 717 |
| 715 | 718 |
| 716 static void ShutdownIsolate(void* callback_data) { | 719 static void ShutdownIsolate(void* callback_data) { |
| 717 VmService::VmServiceShutdownCallback(callback_data); | 720 VmService::VmServiceShutdownCallback(callback_data); |
| 718 IsolateData* isolate_data = reinterpret_cast<IsolateData*>(callback_data); | 721 IsolateData* isolate_data = reinterpret_cast<IsolateData*>(callback_data); |
| 719 delete isolate_data; | 722 delete isolate_data; |
| 720 } | 723 } |
| 721 | 724 |
| 722 | 725 |
| (...skipping 24 matching lines...) Expand all Loading... | |
| 747 result = Dart_StringToCString(result, &script_source); | 750 result = Dart_StringToCString(result, &script_source); |
| 748 if (Dart_IsError(result)) { | 751 if (Dart_IsError(result)) { |
| 749 return result; | 752 return result; |
| 750 } | 753 } |
| 751 Log::Print("%s\n", script_source); | 754 Log::Print("%s\n", script_source); |
| 752 } | 755 } |
| 753 return Dart_True(); | 756 return Dart_True(); |
| 754 } | 757 } |
| 755 | 758 |
| 756 | 759 |
| 757 int main(int argc, char** argv) { | 760 void main(int argc, char** argv) { |
| 758 char* script_name; | 761 char* script_name; |
| 759 CommandLineOptions vm_options(argc); | 762 CommandLineOptions vm_options(argc); |
| 760 CommandLineOptions dart_options(argc); | 763 CommandLineOptions dart_options(argc); |
| 761 bool print_flags_seen = false; | 764 bool print_flags_seen = false; |
| 762 bool verbose_debug_seen = false; | 765 bool verbose_debug_seen = false; |
| 763 | 766 |
| 764 // Perform platform specific initialization. | 767 // Perform platform specific initialization. |
| 765 if (!Platform::Initialize()) { | 768 if (!Platform::Initialize()) { |
| 766 Log::PrintErr("Initialization failed\n"); | 769 Log::PrintErr("Initialization failed\n"); |
| 767 } | 770 } |
| 768 | 771 |
| 769 // On Windows, the argv strings are code page encoded and not | 772 // On Windows, the argv strings are code page encoded and not |
| 770 // utf8. We need to convert them to utf8. | 773 // utf8. We need to convert them to utf8. |
| 771 bool argv_converted = Utf8ConvertArgv(argc, argv); | 774 bool argv_converted = Utf8ConvertArgv(argc, argv); |
| 772 | 775 |
| 773 // Parse command line arguments. | 776 // Parse command line arguments. |
| 774 if (ParseArguments(argc, | 777 if (ParseArguments(argc, |
| 775 argv, | 778 argv, |
| 776 &vm_options, | 779 &vm_options, |
| 777 &script_name, | 780 &script_name, |
| 778 &dart_options, | 781 &dart_options, |
| 779 &print_flags_seen, | 782 &print_flags_seen, |
| 780 &verbose_debug_seen) < 0) { | 783 &verbose_debug_seen) < 0) { |
| 781 if (has_help_option) { | 784 if (has_help_option) { |
| 782 PrintUsage(); | 785 PrintUsage(); |
| 783 return 0; | 786 Exit(0); |
| 784 } else if (has_version_option) { | 787 } else if (has_version_option) { |
| 785 PrintVersion(); | 788 PrintVersion(); |
| 786 return 0; | 789 Exit(0); |
| 787 } else if (print_flags_seen) { | 790 } else if (print_flags_seen) { |
| 788 // Will set the VM flags, print them out and then we exit as no | 791 // Will set the VM flags, print them out and then we exit as no |
| 789 // script was specified on the command line. | 792 // script was specified on the command line. |
| 790 Dart_SetVMFlags(vm_options.count(), vm_options.arguments()); | 793 Dart_SetVMFlags(vm_options.count(), vm_options.arguments()); |
| 791 return 0; | 794 Exit(0); |
| 792 } else { | 795 } else { |
| 793 PrintUsage(); | 796 PrintUsage(); |
| 794 return kErrorExitCode; | 797 Exit(kErrorExitCode); |
| 795 } | 798 } |
| 796 } | 799 } |
| 797 | 800 |
| 798 if (!DartUtils::SetOriginalWorkingDirectory()) { | 801 if (!DartUtils::SetOriginalWorkingDirectory()) { |
| 799 OSError err; | 802 OSError err; |
| 800 fprintf(stderr, "Error determinig current directory: %s\n", err.message()); | 803 fprintf(stderr, "Error determinig current directory: %s\n", err.message()); |
| 801 fflush(stderr); | 804 fflush(stderr); |
| 802 return kErrorExitCode; | 805 Exit(kErrorExitCode); |
| 803 } | 806 } |
| 804 | 807 |
| 805 Dart_SetVMFlags(vm_options.count(), vm_options.arguments()); | 808 Dart_SetVMFlags(vm_options.count(), vm_options.arguments()); |
| 806 | 809 |
| 807 // Initialize the Dart VM. | 810 // Initialize the Dart VM. |
| 808 if (!Dart_Initialize(CreateIsolateAndSetup, NULL, NULL, ShutdownIsolate, | 811 if (!Dart_Initialize(CreateIsolateAndSetup, NULL, NULL, ShutdownIsolate, |
| 809 DartUtils::OpenFile, | 812 DartUtils::OpenFile, |
| 810 DartUtils::ReadFile, | 813 DartUtils::ReadFile, |
| 811 DartUtils::WriteFile, | 814 DartUtils::WriteFile, |
| 812 DartUtils::CloseFile, | 815 DartUtils::CloseFile, |
| 813 DartUtils::EntropySource)) { | 816 DartUtils::EntropySource)) { |
| 814 fprintf(stderr, "%s", "VM initialization failed\n"); | 817 fprintf(stderr, "%s", "VM initialization failed\n"); |
| 815 fflush(stderr); | 818 fflush(stderr); |
| 816 return kErrorExitCode; | 819 Exit(kErrorExitCode); |
| 817 } | 820 } |
| 818 | 821 |
| 819 // Start the debugger wire protocol handler if necessary. | 822 // Start the debugger wire protocol handler if necessary. |
| 820 if (start_debugger) { | 823 if (start_debugger) { |
| 821 ASSERT(debug_port >= 0); | 824 ASSERT(debug_port >= 0); |
| 822 bool print_msg = verbose_debug_seen || (debug_port == 0); | 825 bool print_msg = verbose_debug_seen || (debug_port == 0); |
| 823 debug_port = DebuggerConnectionHandler::StartHandler(debug_ip, debug_port); | 826 debug_port = DebuggerConnectionHandler::StartHandler(debug_ip, debug_port); |
| 824 if (print_msg) { | 827 if (print_msg) { |
| 825 Log::Print("Debugger listening on port %d\n", debug_port); | 828 Log::Print("Debugger listening on port %d\n", debug_port); |
| 826 } | 829 } |
| (...skipping 20 matching lines...) Expand all Loading... | |
| 847 IsolateData* isolate_data = new IsolateData(script_name); | 850 IsolateData* isolate_data = new IsolateData(script_name); |
| 848 Dart_Isolate isolate = CreateIsolateAndSetupHelper(script_name, | 851 Dart_Isolate isolate = CreateIsolateAndSetupHelper(script_name, |
| 849 "main", | 852 "main", |
| 850 isolate_data, | 853 isolate_data, |
| 851 &error, | 854 &error, |
| 852 &is_compile_error); | 855 &is_compile_error); |
| 853 if (isolate == NULL) { | 856 if (isolate == NULL) { |
| 854 Log::PrintErr("%s\n", error); | 857 Log::PrintErr("%s\n", error); |
| 855 free(error); | 858 free(error); |
| 856 delete [] isolate_name; | 859 delete [] isolate_name; |
| 857 return is_compile_error ? kCompilationErrorExitCode : kErrorExitCode; | 860 Exit(is_compile_error ? kCompilationErrorExitCode : kErrorExitCode); |
| 858 } | 861 } |
| 859 delete [] isolate_name; | 862 delete [] isolate_name; |
| 860 | 863 |
| 861 Dart_EnterIsolate(isolate); | 864 Dart_EnterIsolate(isolate); |
| 862 ASSERT(isolate == Dart_CurrentIsolate()); | 865 ASSERT(isolate == Dart_CurrentIsolate()); |
| 863 ASSERT(isolate != NULL); | 866 ASSERT(isolate != NULL); |
| 864 Dart_Handle result; | 867 Dart_Handle result; |
| 865 | 868 |
| 866 Dart_EnterScope(); | 869 Dart_EnterScope(); |
| 867 | 870 |
| 868 if (generate_script_snapshot) { | 871 if (generate_script_snapshot) { |
| 869 // First create a snapshot. | 872 // First create a snapshot. |
| 870 Dart_Handle result; | 873 Dart_Handle result; |
| 871 uint8_t* buffer = NULL; | 874 uint8_t* buffer = NULL; |
| 872 intptr_t size = 0; | 875 intptr_t size = 0; |
| 873 result = Dart_CreateScriptSnapshot(&buffer, &size); | 876 result = Dart_CreateScriptSnapshot(&buffer, &size); |
| 874 if (Dart_IsError(result)) { | 877 if (Dart_IsError(result)) { |
| 875 Log::PrintErr("%s\n", Dart_GetError(result)); | 878 Log::PrintErr("%s\n", Dart_GetError(result)); |
| 876 return DartErrorExit(result); | 879 DartErrorExit(result); |
| 877 } | 880 } |
| 878 | 881 |
| 879 // Write the magic number to indicate file is a script snapshot. | 882 // Write the magic number to indicate file is a script snapshot. |
| 880 DartUtils::WriteMagicNumber(snapshot_file); | 883 DartUtils::WriteMagicNumber(snapshot_file); |
| 881 | 884 |
| 882 // Now write the snapshot out to specified file. | 885 // Now write the snapshot out to specified file. |
| 883 bool bytes_written = snapshot_file->WriteFully(buffer, size); | 886 bool bytes_written = snapshot_file->WriteFully(buffer, size); |
| 884 ASSERT(bytes_written); | 887 ASSERT(bytes_written); |
| 885 delete snapshot_file; | 888 delete snapshot_file; |
| 886 } else { | 889 } else { |
| 887 // Lookup the library of the root script. | 890 // Lookup the library of the root script. |
| 888 Dart_Handle root_lib = Dart_RootLibrary(); | 891 Dart_Handle root_lib = Dart_RootLibrary(); |
| 889 // Import the root library into the builtin library so that we can easily | 892 // Import the root library into the builtin library so that we can easily |
| 890 // lookup the main entry point exported from the root library. | 893 // lookup the main entry point exported from the root library. |
| 891 Dart_Handle builtin_lib = | 894 Dart_Handle builtin_lib = |
| 892 Builtin::LoadAndCheckLibrary(Builtin::kBuiltinLibrary); | 895 Builtin::LoadAndCheckLibrary(Builtin::kBuiltinLibrary); |
| 893 result = Dart_LibraryImportLibrary(builtin_lib, root_lib, Dart_Null()); | 896 result = Dart_LibraryImportLibrary(builtin_lib, root_lib, Dart_Null()); |
| 894 | 897 |
| 895 if (has_compile_all) { | 898 if (has_compile_all) { |
| 896 result = Dart_CompileAll(); | 899 result = Dart_CompileAll(); |
| 897 if (Dart_IsError(result)) { | 900 if (Dart_IsError(result)) { |
| 898 return DartErrorExit(result); | 901 DartErrorExit(result); |
| 899 } | 902 } |
| 900 } | 903 } |
| 901 | 904 |
| 902 if (Dart_IsNull(root_lib)) { | 905 if (Dart_IsNull(root_lib)) { |
| 903 return ErrorExit(kErrorExitCode, | 906 ErrorExit(kErrorExitCode, |
| 904 "Unable to find root library for '%s'\n", | 907 "Unable to find root library for '%s'\n", |
| 905 script_name); | 908 script_name); |
| 906 } | 909 } |
| 907 if (has_print_script) { | 910 if (has_print_script) { |
| 908 result = GenerateScriptSource(); | 911 result = GenerateScriptSource(); |
| 909 if (Dart_IsError(result)) { | 912 if (Dart_IsError(result)) { |
| 910 return DartErrorExit(result); | 913 DartErrorExit(result); |
| 911 } | 914 } |
| 912 } else { | 915 } else { |
| 913 // The helper function _getMainClosure creates a closure for the main | 916 // The helper function _getMainClosure creates a closure for the main |
| 914 // entry point which is either explicitly or implictly exported from the | 917 // entry point which is either explicitly or implictly exported from the |
| 915 // root library. | 918 // root library. |
| 916 Dart_Handle main_closure = Dart_Invoke( | 919 Dart_Handle main_closure = Dart_Invoke( |
| 917 builtin_lib, Dart_NewStringFromCString("_getMainClosure"), 0, NULL); | 920 builtin_lib, Dart_NewStringFromCString("_getMainClosure"), 0, NULL); |
| 918 if (Dart_IsError(main_closure)) { | 921 if (Dart_IsError(main_closure)) { |
| 919 return DartErrorExit(result); | 922 DartErrorExit(result); |
|
Ivan Posva
2013/11/26 18:05:53
?
| |
| 920 } | 923 } |
| 921 | 924 |
| 922 // Set debug breakpoint if specified on the command line before calling | 925 // Set debug breakpoint if specified on the command line before calling |
| 923 // the main function. | 926 // the main function. |
| 924 if (breakpoint_at != NULL) { | 927 if (breakpoint_at != NULL) { |
| 925 result = SetBreakpoint(breakpoint_at, root_lib); | 928 result = SetBreakpoint(breakpoint_at, root_lib); |
| 926 if (Dart_IsError(result)) { | 929 if (Dart_IsError(result)) { |
| 927 return ErrorExit(kErrorExitCode, | 930 ErrorExit(kErrorExitCode, |
| 928 "Error setting breakpoint at '%s': %s\n", | 931 "Error setting breakpoint at '%s': %s\n", |
| 929 breakpoint_at, | 932 breakpoint_at, |
| 930 Dart_GetError(result)); | 933 Dart_GetError(result)); |
| 931 } | 934 } |
| 932 } | 935 } |
| 933 | 936 |
| 934 // Call _startIsolate in the isolate library to enable dispatching the | 937 // Call _startIsolate in the isolate library to enable dispatching the |
| 935 // initial startup message. | 938 // initial startup message. |
| 936 Dart_Handle isolate_args[2]; | 939 Dart_Handle isolate_args[2]; |
| 937 isolate_args[0] = main_closure; | 940 isolate_args[0] = main_closure; |
| 938 isolate_args[1] = Dart_True(); | 941 isolate_args[1] = Dart_True(); |
| 939 | 942 |
| 940 Dart_Handle isolate_lib = Dart_LookupLibrary( | 943 Dart_Handle isolate_lib = Dart_LookupLibrary( |
| 941 Dart_NewStringFromCString("dart:isolate")); | 944 Dart_NewStringFromCString("dart:isolate")); |
| 942 result = Dart_Invoke(isolate_lib, | 945 result = Dart_Invoke(isolate_lib, |
| 943 Dart_NewStringFromCString("_startIsolate"), | 946 Dart_NewStringFromCString("_startIsolate"), |
| 944 2, isolate_args); | 947 2, isolate_args); |
| 945 | 948 |
| 946 // Setup the arguments in the initial startup message and leave the | 949 // Setup the arguments in the initial startup message and leave the |
| 947 // replyTo and message fields empty. | 950 // replyTo and message fields empty. |
| 948 Dart_Handle initial_startup_msg = Dart_NewList(3); | 951 Dart_Handle initial_startup_msg = Dart_NewList(3); |
| 949 result = Dart_ListSetAt(initial_startup_msg, 1, | 952 result = Dart_ListSetAt(initial_startup_msg, 1, |
| 950 CreateRuntimeOptions(&dart_options)); | 953 CreateRuntimeOptions(&dart_options)); |
| 951 if (Dart_IsError(result)) { | 954 if (Dart_IsError(result)) { |
| 952 return DartErrorExit(result); | 955 DartErrorExit(result); |
| 953 } | 956 } |
| 954 Dart_Port main_port = Dart_GetMainPortId(); | 957 Dart_Port main_port = Dart_GetMainPortId(); |
| 955 bool posted = Dart_Post(main_port, initial_startup_msg); | 958 bool posted = Dart_Post(main_port, initial_startup_msg); |
| 956 if (!posted) { | 959 if (!posted) { |
| 957 return ErrorExit(kErrorExitCode, | 960 ErrorExit(kErrorExitCode, |
| 958 "Failed posting startup message to main " | 961 "Failed posting startup message to main " |
| 959 "isolate control port."); | 962 "isolate control port."); |
| 960 } | 963 } |
| 961 | 964 |
| 962 // Keep handling messages until the last active receive port is closed. | 965 // Keep handling messages until the last active receive port is closed. |
| 963 result = Dart_RunLoop(); | 966 result = Dart_RunLoop(); |
| 964 if (Dart_IsError(result)) { | 967 if (Dart_IsError(result)) { |
| 965 return DartErrorExit(result); | 968 DartErrorExit(result); |
| 966 } | 969 } |
| 967 } | 970 } |
| 968 } | 971 } |
| 969 | 972 |
| 970 Dart_ExitScope(); | 973 Dart_ExitScope(); |
| 971 // Shutdown the isolate. | 974 // Shutdown the isolate. |
| 972 Dart_ShutdownIsolate(); | 975 Dart_ShutdownIsolate(); |
| 973 // Terminate process exit-code handler. | 976 // Terminate process exit-code handler. |
| 974 Process::TerminateExitCodeHandler(); | 977 Process::TerminateExitCodeHandler(); |
| 975 | 978 |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 986 p != NULL; | 989 p != NULL; |
| 987 p = environment->Next(p)) { | 990 p = environment->Next(p)) { |
| 988 free(p->key); | 991 free(p->key); |
| 989 free(p->value); | 992 free(p->value); |
| 990 } | 993 } |
| 991 free(environment); | 994 free(environment); |
| 992 } | 995 } |
| 993 | 996 |
| 994 Platform::Cleanup(); | 997 Platform::Cleanup(); |
| 995 | 998 |
| 996 return Process::GlobalExitCode(); | 999 Exit(Process::GlobalExitCode()); |
| 997 } | 1000 } |
| 998 | 1001 |
| 999 } // namespace bin | 1002 } // namespace bin |
| 1000 } // namespace dart | 1003 } // namespace dart |
| 1001 | 1004 |
| 1002 int main(int argc, char** argv) { | 1005 int main(int argc, char** argv) { |
| 1003 return dart::bin::main(argc, argv); | 1006 dart::bin::main(argc, argv); |
| 1007 UNREACHABLE(); | |
| 1004 } | 1008 } |
| OLD | NEW |