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_tools_api.h" | 10 #include "include/dart_tools_api.h" |
(...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
101 static void ErrorExit(int exit_code, const char* format, ...) { | 101 static void ErrorExit(int exit_code, const char* format, ...) { |
102 va_list arguments; | 102 va_list arguments; |
103 va_start(arguments, format); | 103 va_start(arguments, format); |
104 Log::VPrintErr(format, arguments); | 104 Log::VPrintErr(format, arguments); |
105 va_end(arguments); | 105 va_end(arguments); |
106 fflush(stderr); | 106 fflush(stderr); |
107 | 107 |
108 Dart_ExitScope(); | 108 Dart_ExitScope(); |
109 Dart_ShutdownIsolate(); | 109 Dart_ShutdownIsolate(); |
110 | 110 |
111 Dart_Cleanup(); | 111 // Terminate process exit-code handler. |
| 112 Process::TerminateExitCodeHandler(); |
| 113 |
| 114 char* error = Dart_Cleanup(); |
| 115 if (error != NULL) { |
| 116 Log::PrintErr("VM cleanup failed: %s\n", error); |
| 117 free(error); |
| 118 } |
112 | 119 |
113 exit(exit_code); | 120 exit(exit_code); |
114 } | 121 } |
115 | 122 |
116 | 123 |
117 // The environment provided through the command line using -D options. | 124 // The environment provided through the command line using -D options. |
118 static dart::HashMap* environment = NULL; | 125 static dart::HashMap* environment = NULL; |
119 | 126 |
120 static bool IsValidFlag(const char* name, | 127 static bool IsValidFlag(const char* name, |
121 const char* prefix, | 128 const char* prefix, |
(...skipping 495 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
617 Dart_Isolate isolate = NULL; | 624 Dart_Isolate isolate = NULL; |
618 | 625 |
619 isolate = Dart_CreateIsolate(script_uri, | 626 isolate = Dart_CreateIsolate(script_uri, |
620 main, | 627 main, |
621 isolate_snapshot_buffer, | 628 isolate_snapshot_buffer, |
622 flags, | 629 flags, |
623 isolate_data, | 630 isolate_data, |
624 error); | 631 error); |
625 | 632 |
626 if (isolate == NULL) { | 633 if (isolate == NULL) { |
| 634 delete isolate_data; |
627 return NULL; | 635 return NULL; |
628 } | 636 } |
629 | 637 |
630 Dart_EnterScope(); | 638 Dart_EnterScope(); |
631 | 639 |
632 if (isolate_snapshot_buffer != NULL) { | 640 if (isolate_snapshot_buffer != NULL) { |
633 // Setup the native resolver as the snapshot does not carry it. | 641 // Setup the native resolver as the snapshot does not carry it. |
634 Builtin::SetNativeResolver(Builtin::kBuiltinLibrary); | 642 Builtin::SetNativeResolver(Builtin::kBuiltinLibrary); |
635 Builtin::SetNativeResolver(Builtin::kIOLibrary); | 643 Builtin::SetNativeResolver(Builtin::kIOLibrary); |
636 } | 644 } |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
681 // Load the script. | 689 // Load the script. |
682 result = DartUtils::LoadScript(script_uri, builtin_lib); | 690 result = DartUtils::LoadScript(script_uri, builtin_lib); |
683 CHECK_RESULT(result); | 691 CHECK_RESULT(result); |
684 | 692 |
685 // Run event-loop and wait for script loading to complete. | 693 // Run event-loop and wait for script loading to complete. |
686 result = Dart_RunLoop(); | 694 result = Dart_RunLoop(); |
687 CHECK_RESULT(result); | 695 CHECK_RESULT(result); |
688 | 696 |
689 Platform::SetPackageRoot(package_root); | 697 Platform::SetPackageRoot(package_root); |
690 | 698 |
691 DartUtils::SetupIOLibrary(script_uri); | 699 result = DartUtils::SetupIOLibrary(script_uri); |
| 700 CHECK_RESULT(result); |
692 | 701 |
693 // Make the isolate runnable so that it is ready to handle messages. | 702 // Make the isolate runnable so that it is ready to handle messages. |
694 Dart_ExitScope(); | 703 Dart_ExitScope(); |
695 Dart_ExitIsolate(); | 704 Dart_ExitIsolate(); |
696 bool retval = Dart_IsolateMakeRunnable(isolate); | 705 bool retval = Dart_IsolateMakeRunnable(isolate); |
697 if (!retval) { | 706 if (!retval) { |
698 *error = strdup("Invalid isolate state - Unable to make it runnable"); | 707 *error = strdup("Invalid isolate state - Unable to make it runnable"); |
699 Dart_EnterIsolate(isolate); | 708 Dart_EnterIsolate(isolate); |
700 Dart_ShutdownIsolate(); | 709 Dart_ShutdownIsolate(); |
701 return NULL; | 710 return NULL; |
(...skipping 320 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1022 bool print_msg = verbose_debug_seen || (debug_port == 0); | 1031 bool print_msg = verbose_debug_seen || (debug_port == 0); |
1023 debug_port = DebuggerConnectionHandler::StartHandler(debug_ip, debug_port); | 1032 debug_port = DebuggerConnectionHandler::StartHandler(debug_ip, debug_port); |
1024 if (print_msg) { | 1033 if (print_msg) { |
1025 Log::Print("Debugger listening on port %d\n", debug_port); | 1034 Log::Print("Debugger listening on port %d\n", debug_port); |
1026 } | 1035 } |
1027 } else { | 1036 } else { |
1028 DebuggerConnectionHandler::InitForVmService(); | 1037 DebuggerConnectionHandler::InitForVmService(); |
1029 } | 1038 } |
1030 | 1039 |
1031 // Initialize the Dart VM. | 1040 // Initialize the Dart VM. |
1032 if (!Dart_Initialize(vm_isolate_snapshot_buffer, | 1041 char* error = Dart_Initialize(vm_isolate_snapshot_buffer, |
1033 CreateIsolateAndSetup, NULL, NULL, ShutdownIsolate, | 1042 CreateIsolateAndSetup, NULL, NULL, ShutdownIsolate, |
1034 DartUtils::OpenFile, | 1043 DartUtils::OpenFile, |
1035 DartUtils::ReadFile, | 1044 DartUtils::ReadFile, |
1036 DartUtils::WriteFile, | 1045 DartUtils::WriteFile, |
1037 DartUtils::CloseFile, | 1046 DartUtils::CloseFile, |
1038 DartUtils::EntropySource)) { | 1047 DartUtils::EntropySource); |
1039 fprintf(stderr, "%s", "VM initialization failed\n"); | 1048 if (error != NULL) { |
| 1049 fprintf(stderr, "VM initialization failed: %s\n", error); |
1040 fflush(stderr); | 1050 fflush(stderr); |
| 1051 free(error); |
1041 exit(kErrorExitCode); | 1052 exit(kErrorExitCode); |
1042 } | 1053 } |
1043 | 1054 |
1044 Dart_RegisterIsolateServiceRequestCallback( | 1055 Dart_RegisterIsolateServiceRequestCallback( |
1045 "getIO", &ServiceGetIOHandler, NULL); | 1056 "getIO", &ServiceGetIOHandler, NULL); |
1046 Dart_SetServiceStreamCallbacks(&ServiceStreamListenCallback, | 1057 Dart_SetServiceStreamCallbacks(&ServiceStreamListenCallback, |
1047 &ServiceStreamCancelCallback); | 1058 &ServiceStreamCancelCallback); |
1048 | 1059 |
1049 // Call CreateIsolateAndSetup which creates an isolate and loads up | 1060 // Call CreateIsolateAndSetup which creates an isolate and loads up |
1050 // the specified application script. | 1061 // the specified application script. |
1051 char* error = NULL; | |
1052 int exit_code = 0; | 1062 int exit_code = 0; |
1053 char* isolate_name = BuildIsolateName(script_name, "main"); | 1063 char* isolate_name = BuildIsolateName(script_name, "main"); |
1054 Dart_Isolate isolate = CreateIsolateAndSetupHelper(script_name, | 1064 Dart_Isolate isolate = CreateIsolateAndSetupHelper(script_name, |
1055 "main", | 1065 "main", |
1056 commandline_package_root, | 1066 commandline_package_root, |
1057 commandline_packages_file, | 1067 commandline_packages_file, |
1058 NULL, | 1068 NULL, |
1059 &error, | 1069 &error, |
1060 &exit_code); | 1070 &exit_code); |
1061 if (isolate == NULL) { | 1071 if (isolate == NULL) { |
1062 Log::PrintErr("%s\n", error); | 1072 Log::PrintErr("%s\n", error); |
1063 free(error); | 1073 free(error); |
| 1074 error = NULL; |
1064 delete [] isolate_name; | 1075 delete [] isolate_name; |
| 1076 Process::TerminateExitCodeHandler(); |
| 1077 error = Dart_Cleanup(); |
| 1078 if (error != NULL) { |
| 1079 Log::PrintErr("VM cleanup failed: %s\n", error); |
| 1080 free(error); |
| 1081 } |
1065 exit((exit_code != 0) ? exit_code : kErrorExitCode); | 1082 exit((exit_code != 0) ? exit_code : kErrorExitCode); |
1066 } | 1083 } |
1067 delete [] isolate_name; | 1084 delete [] isolate_name; |
1068 | 1085 |
1069 Dart_EnterIsolate(isolate); | 1086 Dart_EnterIsolate(isolate); |
1070 ASSERT(isolate == Dart_CurrentIsolate()); | 1087 ASSERT(isolate == Dart_CurrentIsolate()); |
1071 ASSERT(isolate != NULL); | 1088 ASSERT(isolate != NULL); |
1072 Dart_Handle result; | 1089 Dart_Handle result; |
1073 | 1090 |
1074 Dart_EnterScope(); | 1091 Dart_EnterScope(); |
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1158 result = Dart_RunLoop(); | 1175 result = Dart_RunLoop(); |
1159 DartExitOnError(result); | 1176 DartExitOnError(result); |
1160 } | 1177 } |
1161 | 1178 |
1162 Dart_ExitScope(); | 1179 Dart_ExitScope(); |
1163 // Shutdown the isolate. | 1180 // Shutdown the isolate. |
1164 Dart_ShutdownIsolate(); | 1181 Dart_ShutdownIsolate(); |
1165 // Terminate process exit-code handler. | 1182 // Terminate process exit-code handler. |
1166 Process::TerminateExitCodeHandler(); | 1183 Process::TerminateExitCodeHandler(); |
1167 | 1184 |
1168 Dart_Cleanup(); | 1185 error = Dart_Cleanup(); |
| 1186 if (error != NULL) { |
| 1187 Log::PrintErr("VM cleanup failed: %s\n", error); |
| 1188 free(error); |
| 1189 } |
1169 | 1190 |
1170 // Free copied argument strings if converted. | 1191 // Free copied argument strings if converted. |
1171 if (argv_converted) { | 1192 if (argv_converted) { |
1172 for (int i = 0; i < argc; i++) free(argv[i]); | 1193 for (int i = 0; i < argc; i++) free(argv[i]); |
1173 } | 1194 } |
1174 | 1195 |
1175 // Free environment if any. | 1196 // Free environment if any. |
1176 if (environment != NULL) { | 1197 if (environment != NULL) { |
1177 for (HashMap::Entry* p = environment->Start(); | 1198 for (HashMap::Entry* p = environment->Start(); |
1178 p != NULL; | 1199 p != NULL; |
1179 p = environment->Next(p)) { | 1200 p = environment->Next(p)) { |
1180 free(p->key); | 1201 free(p->key); |
1181 free(p->value); | 1202 free(p->value); |
1182 } | 1203 } |
1183 delete environment; | 1204 delete environment; |
1184 } | 1205 } |
1185 | 1206 |
1186 exit(Process::GlobalExitCode()); | 1207 exit(Process::GlobalExitCode()); |
1187 } | 1208 } |
1188 | 1209 |
1189 } // namespace bin | 1210 } // namespace bin |
1190 } // namespace dart | 1211 } // namespace dart |
1191 | 1212 |
1192 int main(int argc, char** argv) { | 1213 int main(int argc, char** argv) { |
1193 dart::bin::main(argc, argv); | 1214 dart::bin::main(argc, argv); |
1194 UNREACHABLE(); | 1215 UNREACHABLE(); |
1195 } | 1216 } |
OLD | NEW |