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