Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(778)

Side by Side Diff: runtime/bin/main.cc

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

Powered by Google App Engine
This is Rietveld 408576698