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

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

Issue 1371193005: VM restart + shutdown fixes (Closed) Base URL: git@github.com:dart-lang/sdk.git@master
Patch Set: more code review Created 5 years, 2 months 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
« no previous file with comments | « no previous file | runtime/include/dart_api.h » ('j') | 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_tools_api.h" 10 #include "include/dart_tools_api.h"
(...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after
107 // being allocated. 107 // being allocated.
108 static int vm_service_server_port = -1; 108 static int vm_service_server_port = -1;
109 109
110 110
111 // Exit code indicating an API error. 111 // Exit code indicating an API error.
112 static const int kApiErrorExitCode = 253; 112 static const int kApiErrorExitCode = 253;
113 // Exit code indicating a compilation error. 113 // Exit code indicating a compilation error.
114 static const int kCompilationErrorExitCode = 254; 114 static const int kCompilationErrorExitCode = 254;
115 // Exit code indicating an unhandled error that is not a compilation error. 115 // Exit code indicating an unhandled error that is not a compilation error.
116 static const int kErrorExitCode = 255; 116 static const int kErrorExitCode = 255;
117 // Exit code indicating a vm restart request. Never returned to the user.
118 static const int kRestartRequestExitCode = 1000;
117 119
118 extern bool do_vm_shutdown; // Defined in bin/process.cc 120 extern bool do_vm_shutdown; // Defined in bin/process.cc
119 static void ErrorExit(int exit_code, const char* format, ...) { 121 static void ErrorExit(int exit_code, const char* format, ...) {
120 va_list arguments; 122 va_list arguments;
121 va_start(arguments, format); 123 va_start(arguments, format);
122 Log::VPrintErr(format, arguments); 124 Log::VPrintErr(format, arguments);
123 va_end(arguments); 125 va_end(arguments);
124 fflush(stderr); 126 fflush(stderr);
125 127
126 Dart_ExitScope(); 128 Dart_ExitScope();
(...skipping 551 matching lines...) Expand 10 before | Expand all | Expand 10 after
678 } 680 }
679 free(name_chars); 681 free(name_chars);
680 } 682 }
681 return result; 683 return result;
682 } 684 }
683 685
684 686
685 #define CHECK_RESULT(result) \ 687 #define CHECK_RESULT(result) \
686 if (Dart_IsError(result)) { \ 688 if (Dart_IsError(result)) { \
687 *error = strdup(Dart_GetError(result)); \ 689 *error = strdup(Dart_GetError(result)); \
688 *exit_code = Dart_IsCompilationError(result) ? kCompilationErrorExitCode : \ 690 if (Dart_IsCompilationError(result)) { \
689 (Dart_IsApiError(result) ? kApiErrorExitCode : kErrorExitCode); \ 691 *exit_code = kCompilationErrorExitCode; \
692 } else if (Dart_IsApiError(result)) { \
693 *exit_code = kApiErrorExitCode; \
694 } else if (Dart_IsVMRestartRequest(result)) { \
695 *exit_code = kRestartRequestExitCode; \
696 } else { \
697 *exit_code = kErrorExitCode; \
698 } \
690 Dart_ExitScope(); \ 699 Dart_ExitScope(); \
691 Dart_ShutdownIsolate(); \ 700 Dart_ShutdownIsolate(); \
692 return NULL; \ 701 return NULL; \
693 } \ 702 } \
694 703
695 704
696 // Returns true on success, false on failure. 705 // Returns true on success, false on failure.
697 static Dart_Isolate CreateIsolateAndSetupHelper(const char* script_uri, 706 static Dart_Isolate CreateIsolateAndSetupHelper(const char* script_uri,
698 const char* main, 707 const char* main,
699 const char* package_root, 708 const char* package_root,
(...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after
795 Dart_EnterIsolate(isolate); 804 Dart_EnterIsolate(isolate);
796 Dart_ShutdownIsolate(); 805 Dart_ShutdownIsolate();
797 return NULL; 806 return NULL;
798 } 807 }
799 808
800 return isolate; 809 return isolate;
801 } 810 }
802 811
803 #undef CHECK_RESULT 812 #undef CHECK_RESULT
804 813
814
805 static Dart_Isolate CreateIsolateAndSetup(const char* script_uri, 815 static Dart_Isolate CreateIsolateAndSetup(const char* script_uri,
806 const char* main, 816 const char* main,
807 const char* package_root, 817 const char* package_root,
808 Dart_IsolateFlags* flags, 818 Dart_IsolateFlags* flags,
809 void* data, char** error) { 819 void* data, char** error) {
810 // The VM should never call the isolate helper with a NULL flags. 820 // The VM should never call the isolate helper with a NULL flags.
811 ASSERT(flags != NULL); 821 ASSERT(flags != NULL);
812 ASSERT(flags->version == DART_FLAGS_CURRENT_VERSION); 822 ASSERT(flags->version == DART_FLAGS_CURRENT_VERSION);
813 IsolateData* parent_isolate_data = reinterpret_cast<IsolateData*>(data); 823 IsolateData* parent_isolate_data = reinterpret_cast<IsolateData*>(data);
814 int exit_code = 0; 824 int exit_code = 0;
(...skipping 128 matching lines...) Expand 10 before | Expand all | Expand 10 after
943 } 953 }
944 954
945 const char* kFormat = "%s/%s"; 955 const char* kFormat = "%s/%s";
946 intptr_t len = strlen(script_name) + strlen(func_name) + 2; 956 intptr_t len = strlen(script_name) + strlen(func_name) + 2;
947 char* buffer = new char[len]; 957 char* buffer = new char[len];
948 ASSERT(buffer != NULL); 958 ASSERT(buffer != NULL);
949 snprintf(buffer, len, kFormat, script_name, func_name); 959 snprintf(buffer, len, kFormat, script_name, func_name);
950 return buffer; 960 return buffer;
951 } 961 }
952 962
953 static void DartExitOnError(Dart_Handle error) {
954 if (!Dart_IsError(error)) {
955 return;
956 }
957 const int exit_code = Dart_IsCompilationError(error) ?
958 kCompilationErrorExitCode : kErrorExitCode;
959 ErrorExit(exit_code, "%s\n", Dart_GetError(error));
960 }
961
962
963 static void ShutdownIsolate(void* callback_data) { 963 static void ShutdownIsolate(void* callback_data) {
964 IsolateData* isolate_data = reinterpret_cast<IsolateData*>(callback_data); 964 IsolateData* isolate_data = reinterpret_cast<IsolateData*>(callback_data);
965 delete isolate_data; 965 delete isolate_data;
966 } 966 }
967 967
968 968
969 static const char* ServiceRequestError(Dart_Handle error) { 969 static const char* ServiceRequestError(Dart_Handle error) {
970 TextBuffer buffer(128); 970 TextBuffer buffer(128);
971 buffer.Printf("{\"type\":\"Error\",\"text\":\"Internal error %s\"}", 971 buffer.Printf("{\"type\":\"Error\",\"text\":\"Internal error %s\"}",
972 Dart_GetError(error)); 972 Dart_GetError(error));
(...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after
1087 } 1087 }
1088 void* symbol = Extensions::ResolveSymbol(library, symname); 1088 void* symbol = Extensions::ResolveSymbol(library, symname);
1089 if (symbol == NULL) { 1089 if (symbol == NULL) {
1090 Log::PrintErr("Error: Failed to load symbol '%s'\n", symname); 1090 Log::PrintErr("Error: Failed to load symbol '%s'\n", symname);
1091 exit(kErrorExitCode); 1091 exit(kErrorExitCode);
1092 } 1092 }
1093 return symbol; 1093 return symbol;
1094 } 1094 }
1095 1095
1096 1096
1097 void main(int argc, char** argv) { 1097 #define CHECK_RESULT(result) \
1098 char* script_name; 1098 if (Dart_IsError(result)) { \
1099 const int EXTRA_VM_ARGUMENTS = 2; 1099 if (Dart_IsVMRestartRequest(result)) { \
1100 CommandLineOptions vm_options(argc + EXTRA_VM_ARGUMENTS); 1100 Dart_ExitScope(); \
1101 CommandLineOptions dart_options(argc); 1101 Dart_ShutdownIsolate(); \
1102 bool print_flags_seen = false; 1102 return true; \
1103 bool verbose_debug_seen = false; 1103 } \
1104 1104 const int exit_code = Dart_IsCompilationError(result) ? \
1105 vm_options.AddArgument("--no_write_protect_code"); 1105 kCompilationErrorExitCode : kErrorExitCode; \
1106 // Perform platform specific initialization. 1106 ErrorExit(exit_code, "%s\n", Dart_GetError(result)); \
1107 if (!Platform::Initialize()) {
1108 Log::PrintErr("Initialization failed\n");
1109 } 1107 }
1110 1108
1111 // On Windows, the argv strings are code page encoded and not 1109 bool RunMainIsolate(const char* script_name,
1112 // utf8. We need to convert them to utf8. 1110 CommandLineOptions* dart_options) {
1113 bool argv_converted = ShellUtils::GetUtf8Argv(argc, argv);
1114
1115 // Parse command line arguments.
1116 if (ParseArguments(argc,
1117 argv,
1118 &vm_options,
1119 &script_name,
1120 &dart_options,
1121 &print_flags_seen,
1122 &verbose_debug_seen) < 0) {
1123 if (has_help_option) {
1124 PrintUsage();
1125 exit(0);
1126 } else if (has_version_option) {
1127 PrintVersion();
1128 exit(0);
1129 } else if (print_flags_seen) {
1130 // Will set the VM flags, print them out and then we exit as no
1131 // script was specified on the command line.
1132 Dart_SetVMFlags(vm_options.count(), vm_options.arguments());
1133 exit(0);
1134 } else {
1135 PrintUsage();
1136 exit(kErrorExitCode);
1137 }
1138 }
1139
1140 Thread::InitOnce();
1141
1142 if (!DartUtils::SetOriginalWorkingDirectory()) {
1143 OSError err;
1144 fprintf(stderr, "Error determining current directory: %s\n", err.message());
1145 fflush(stderr);
1146 exit(kErrorExitCode);
1147 }
1148
1149 if (generate_script_snapshot) {
1150 vm_options.AddArgument("--load_deferred_eagerly");
1151 }
1152
1153 Dart_SetVMFlags(vm_options.count(), vm_options.arguments());
1154
1155 // Start event handler.
1156 EventHandler::Start();
1157
1158 // Start the debugger wire protocol handler if necessary.
1159 if (start_debugger) {
1160 ASSERT(debug_port >= 0);
1161 bool print_msg = verbose_debug_seen || (debug_port == 0);
1162 debug_port = DebuggerConnectionHandler::StartHandler(debug_ip, debug_port);
1163 if (print_msg) {
1164 Log::Print("Debugger listening on port %d\n", debug_port);
1165 }
1166 } else {
1167 DebuggerConnectionHandler::InitForVmService();
1168 }
1169
1170 const uint8_t* instructions_snapshot = NULL;
1171 if (has_run_precompiled_snapshot) {
1172 instructions_snapshot = reinterpret_cast<const uint8_t*>(
1173 LoadLibrarySymbol(kPrecompiledLibraryName, kPrecompiledSymbolName));
1174 ReadSnapshotFile(kPrecompiledVmIsolateName, &vm_isolate_snapshot_buffer);
1175 ReadSnapshotFile(kPrecompiledIsolateName, &isolate_snapshot_buffer);
1176 }
1177
1178 // Initialize the Dart VM.
1179 char* error = Dart_Initialize(
1180 vm_isolate_snapshot_buffer, instructions_snapshot,
1181 CreateIsolateAndSetup, NULL, NULL, ShutdownIsolate,
1182 DartUtils::OpenFile,
1183 DartUtils::ReadFile,
1184 DartUtils::WriteFile,
1185 DartUtils::CloseFile,
1186 DartUtils::EntropySource);
1187 if (error != NULL) {
1188 if (do_vm_shutdown) {
1189 DebuggerConnectionHandler::StopHandler();
1190 EventHandler::Stop();
1191 }
1192 fprintf(stderr, "VM initialization failed: %s\n", error);
1193 fflush(stderr);
1194 free(error);
1195 exit(kErrorExitCode);
1196 }
1197
1198 Dart_RegisterIsolateServiceRequestCallback(
1199 "getIO", &ServiceGetIOHandler, NULL);
1200 Dart_SetServiceStreamCallbacks(&ServiceStreamListenCallback,
1201 &ServiceStreamCancelCallback);
1202
1203 // Call CreateIsolateAndSetup which creates an isolate and loads up 1111 // Call CreateIsolateAndSetup which creates an isolate and loads up
1204 // the specified application script. 1112 // the specified application script.
1113 char* error = NULL;
1205 int exit_code = 0; 1114 int exit_code = 0;
1206 char* isolate_name = BuildIsolateName(script_name, "main"); 1115 char* isolate_name = BuildIsolateName(script_name, "main");
1207 Dart_Isolate isolate = CreateIsolateAndSetupHelper(script_name, 1116 Dart_Isolate isolate = CreateIsolateAndSetupHelper(script_name,
1208 "main", 1117 "main",
1209 commandline_package_root, 1118 commandline_package_root,
1210 commandline_packages_file, 1119 commandline_packages_file,
1211 NULL, 1120 NULL,
1212 &error, 1121 &error,
1213 &exit_code); 1122 &exit_code);
1214 if (isolate == NULL) { 1123 if (isolate == NULL) {
1124 delete [] isolate_name;
1125 if (exit_code == kRestartRequestExitCode) {
1126 free(error);
1127 return true;
1128 }
1215 Log::PrintErr("%s\n", error); 1129 Log::PrintErr("%s\n", error);
1216 free(error); 1130 free(error);
1217 error = NULL; 1131 error = NULL;
1218 delete [] isolate_name;
1219 Process::TerminateExitCodeHandler(); 1132 Process::TerminateExitCodeHandler();
1220 error = Dart_Cleanup(); 1133 error = Dart_Cleanup();
1221 if (error != NULL) { 1134 if (error != NULL) {
1222 Log::PrintErr("VM cleanup failed: %s\n", error); 1135 Log::PrintErr("VM cleanup failed: %s\n", error);
1223 free(error); 1136 free(error);
1224 } 1137 }
1225 if (do_vm_shutdown) { 1138 if (do_vm_shutdown) {
1226 DebuggerConnectionHandler::StopHandler(); 1139 DebuggerConnectionHandler::StopHandler();
1227 EventHandler::Stop(); 1140 EventHandler::Stop();
1228 } 1141 }
1229 exit((exit_code != 0) ? exit_code : kErrorExitCode); 1142 exit((exit_code != 0) ? exit_code : kErrorExitCode);
1230 } 1143 }
1231 delete [] isolate_name; 1144 delete [] isolate_name;
1232 1145
1233 Dart_EnterIsolate(isolate); 1146 Dart_EnterIsolate(isolate);
1234 ASSERT(isolate == Dart_CurrentIsolate()); 1147 ASSERT(isolate == Dart_CurrentIsolate());
1235 ASSERT(isolate != NULL); 1148 ASSERT(isolate != NULL);
1236 Dart_Handle result; 1149 Dart_Handle result;
1237 1150
1238 Dart_EnterScope(); 1151 Dart_EnterScope();
1239 1152
1240 if (generate_script_snapshot) { 1153 if (generate_script_snapshot) {
1241 // First create a snapshot. 1154 // First create a snapshot.
1242 Dart_Handle result; 1155 Dart_Handle result;
1243 uint8_t* buffer = NULL; 1156 uint8_t* buffer = NULL;
1244 intptr_t size = 0; 1157 intptr_t size = 0;
1245 result = Dart_CreateScriptSnapshot(&buffer, &size); 1158 result = Dart_CreateScriptSnapshot(&buffer, &size);
1246 DartExitOnError(result); 1159 CHECK_RESULT(result);
1247 1160
1248 // Open the snapshot file. 1161 // Open the snapshot file.
1249 File* snapshot_file = File::Open(snapshot_filename, File::kWriteTruncate); 1162 File* snapshot_file = File::Open(snapshot_filename, File::kWriteTruncate);
1250 if (snapshot_file == NULL) { 1163 if (snapshot_file == NULL) {
1251 ErrorExit(kErrorExitCode, 1164 ErrorExit(kErrorExitCode,
1252 "Unable to open file %s for writing the snapshot\n", 1165 "Unable to open file %s for writing the snapshot\n",
1253 snapshot_filename); 1166 snapshot_filename);
1254 } 1167 }
1255 1168
1256 // Write the magic number to indicate file is a script snapshot. 1169 // Write the magic number to indicate file is a script snapshot.
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
1292 { "dart:io", "_ProcessStartStatus", "set:_errorMessage" }, 1205 { "dart:io", "_ProcessStartStatus", "set:_errorMessage" },
1293 { "dart:io", "_SecureFilterImpl", "get:ENCRYPTED_SIZE" }, 1206 { "dart:io", "_SecureFilterImpl", "get:ENCRYPTED_SIZE" },
1294 { "dart:io", "_SecureFilterImpl", "get:SIZE" }, 1207 { "dart:io", "_SecureFilterImpl", "get:SIZE" },
1295 { "dart:vmservice_io", "::", "_addResource" }, 1208 { "dart:vmservice_io", "::", "_addResource" },
1296 { "dart:vmservice_io", "::", "main" }, 1209 { "dart:vmservice_io", "::", "main" },
1297 { NULL, NULL, NULL } // Must be terminated with NULL entries. 1210 { NULL, NULL, NULL } // Must be terminated with NULL entries.
1298 }; 1211 };
1299 1212
1300 const bool reset_fields = has_gen_precompiled_snapshot; 1213 const bool reset_fields = has_gen_precompiled_snapshot;
1301 result = Dart_Precompile(standalone_entry_points, reset_fields); 1214 result = Dart_Precompile(standalone_entry_points, reset_fields);
1302 DartExitOnError(result); 1215 CHECK_RESULT(result);
1303 } 1216 }
1304 1217
1305 if (has_gen_precompiled_snapshot) { 1218 if (has_gen_precompiled_snapshot) {
1306 uint8_t* vm_isolate_buffer = NULL; 1219 uint8_t* vm_isolate_buffer = NULL;
1307 intptr_t vm_isolate_size = 0; 1220 intptr_t vm_isolate_size = 0;
1308 uint8_t* isolate_buffer = NULL; 1221 uint8_t* isolate_buffer = NULL;
1309 intptr_t isolate_size = 0; 1222 intptr_t isolate_size = 0;
1310 uint8_t* instructions_buffer = NULL; 1223 uint8_t* instructions_buffer = NULL;
1311 intptr_t instructions_size = 0; 1224 intptr_t instructions_size = 0;
1312 result = Dart_CreatePrecompiledSnapshot(&vm_isolate_buffer, 1225 result = Dart_CreatePrecompiledSnapshot(&vm_isolate_buffer,
1313 &vm_isolate_size, 1226 &vm_isolate_size,
1314 &isolate_buffer, 1227 &isolate_buffer,
1315 &isolate_size, 1228 &isolate_size,
1316 &instructions_buffer, 1229 &instructions_buffer,
1317 &instructions_size); 1230 &instructions_size);
1318 DartExitOnError(result); 1231 CHECK_RESULT(result);
1319 WriteSnapshotFile(kPrecompiledVmIsolateName, 1232 WriteSnapshotFile(kPrecompiledVmIsolateName,
1320 vm_isolate_buffer, 1233 vm_isolate_buffer,
1321 vm_isolate_size); 1234 vm_isolate_size);
1322 WriteSnapshotFile(kPrecompiledIsolateName, 1235 WriteSnapshotFile(kPrecompiledIsolateName,
1323 isolate_buffer, 1236 isolate_buffer,
1324 isolate_size); 1237 isolate_size);
1325 WriteSnapshotFile(kPrecompiledInstructionsName, 1238 WriteSnapshotFile(kPrecompiledInstructionsName,
1326 instructions_buffer, 1239 instructions_buffer,
1327 instructions_size); 1240 instructions_size);
1328 } else { 1241 } else {
1329 if (has_compile_all) { 1242 if (has_compile_all) {
1330 result = Dart_CompileAll(); 1243 result = Dart_CompileAll();
1331 DartExitOnError(result); 1244 CHECK_RESULT(result);
1332 } 1245 }
1333 1246
1334 if (Dart_IsNull(root_lib)) { 1247 if (Dart_IsNull(root_lib)) {
1335 ErrorExit(kErrorExitCode, 1248 ErrorExit(kErrorExitCode,
1336 "Unable to find root library for '%s'\n", 1249 "Unable to find root library for '%s'\n",
1337 script_name); 1250 script_name);
1338 } 1251 }
1339 1252
1340 // The helper function _getMainClosure creates a closure for the main 1253 // The helper function _getMainClosure creates a closure for the main
1341 // entry point which is either explicitly or implictly exported from the 1254 // entry point which is either explicitly or implictly exported from the
1342 // root library. 1255 // root library.
1343 Dart_Handle main_closure = Dart_Invoke(builtin_lib, 1256 Dart_Handle main_closure = Dart_Invoke(builtin_lib,
1344 Dart_NewStringFromCString("_getMainClosure"), 0, NULL); 1257 Dart_NewStringFromCString("_getMainClosure"), 0, NULL);
1345 DartExitOnError(main_closure); 1258 CHECK_RESULT(main_closure);
1346 1259
1347 // Set debug breakpoint if specified on the command line before calling 1260 // Set debug breakpoint if specified on the command line before calling
1348 // the main function. 1261 // the main function.
1349 if (breakpoint_at != NULL) { 1262 if (breakpoint_at != NULL) {
1350 result = SetBreakpoint(breakpoint_at, root_lib); 1263 result = SetBreakpoint(breakpoint_at, root_lib);
1351 if (Dart_IsError(result)) { 1264 if (Dart_IsError(result)) {
1352 ErrorExit(kErrorExitCode, 1265 ErrorExit(kErrorExitCode,
1353 "Error setting breakpoint at '%s': %s\n", 1266 "Error setting breakpoint at '%s': %s\n",
1354 breakpoint_at, 1267 breakpoint_at,
1355 Dart_GetError(result)); 1268 Dart_GetError(result));
1356 } 1269 }
1357 } 1270 }
1358 1271
1359 // Call _startIsolate in the isolate library to enable dispatching the 1272 // Call _startIsolate in the isolate library to enable dispatching the
1360 // initial startup message. 1273 // initial startup message.
1361 const intptr_t kNumIsolateArgs = 2; 1274 const intptr_t kNumIsolateArgs = 2;
1362 Dart_Handle isolate_args[kNumIsolateArgs]; 1275 Dart_Handle isolate_args[kNumIsolateArgs];
1363 isolate_args[0] = main_closure; // entryPoint 1276 isolate_args[0] = main_closure; // entryPoint
1364 isolate_args[1] = CreateRuntimeOptions(&dart_options); // args 1277 isolate_args[1] = CreateRuntimeOptions(dart_options); // args
1365 1278
1366 Dart_Handle isolate_lib = 1279 Dart_Handle isolate_lib =
1367 Dart_LookupLibrary(Dart_NewStringFromCString("dart:isolate")); 1280 Dart_LookupLibrary(Dart_NewStringFromCString("dart:isolate"));
1368 result = Dart_Invoke(isolate_lib, 1281 result = Dart_Invoke(isolate_lib,
1369 Dart_NewStringFromCString("_startMainIsolate"), 1282 Dart_NewStringFromCString("_startMainIsolate"),
1370 kNumIsolateArgs, isolate_args); 1283 kNumIsolateArgs, isolate_args);
1371 DartExitOnError(result); 1284 CHECK_RESULT(result);
1372 1285
1373 // Keep handling messages until the last active receive port is closed. 1286 // Keep handling messages until the last active receive port is closed.
1374 result = Dart_RunLoop(); 1287 result = Dart_RunLoop();
1375 DartExitOnError(result); 1288 CHECK_RESULT(result);
1376 } 1289 }
1377 } 1290 }
1378 1291
1379 Dart_ExitScope(); 1292 Dart_ExitScope();
1380 // Shutdown the isolate. 1293 // Shutdown the isolate.
1381 Dart_ShutdownIsolate(); 1294 Dart_ShutdownIsolate();
1295
1296 // No restart.
1297 return false;
1298 }
1299
1300 #undef CHECK_RESULT
1301
1302
1303 void main(int argc, char** argv) {
1304 char* script_name;
1305 const int EXTRA_VM_ARGUMENTS = 2;
1306 CommandLineOptions vm_options(argc + EXTRA_VM_ARGUMENTS);
1307 CommandLineOptions dart_options(argc);
1308 bool print_flags_seen = false;
1309 bool verbose_debug_seen = false;
1310
1311 vm_options.AddArgument("--no_write_protect_code");
1312 // Perform platform specific initialization.
1313 if (!Platform::Initialize()) {
1314 Log::PrintErr("Initialization failed\n");
1315 }
1316
1317 // On Windows, the argv strings are code page encoded and not
1318 // utf8. We need to convert them to utf8.
1319 bool argv_converted = ShellUtils::GetUtf8Argv(argc, argv);
1320
1321 // Parse command line arguments.
1322 if (ParseArguments(argc,
1323 argv,
1324 &vm_options,
1325 &script_name,
1326 &dart_options,
1327 &print_flags_seen,
1328 &verbose_debug_seen) < 0) {
1329 if (has_help_option) {
1330 PrintUsage();
1331 exit(0);
1332 } else if (has_version_option) {
1333 PrintVersion();
1334 exit(0);
1335 } else if (print_flags_seen) {
1336 // Will set the VM flags, print them out and then we exit as no
1337 // script was specified on the command line.
1338 Dart_SetVMFlags(vm_options.count(), vm_options.arguments());
1339 exit(0);
1340 } else {
1341 PrintUsage();
1342 exit(kErrorExitCode);
1343 }
1344 }
1345
1346 Thread::InitOnce();
1347
1348 if (!DartUtils::SetOriginalWorkingDirectory()) {
1349 OSError err;
1350 fprintf(stderr, "Error determining current directory: %s\n", err.message());
1351 fflush(stderr);
1352 exit(kErrorExitCode);
1353 }
1354
1355 if (generate_script_snapshot) {
1356 vm_options.AddArgument("--load_deferred_eagerly");
1357 }
1358
1359 Dart_SetVMFlags(vm_options.count(), vm_options.arguments());
1360
1361 // Start event handler.
1362 EventHandler::Start();
1363
1364 // Start the debugger wire protocol handler if necessary.
1365 if (start_debugger) {
1366 ASSERT(debug_port >= 0);
1367 bool print_msg = verbose_debug_seen || (debug_port == 0);
1368 debug_port = DebuggerConnectionHandler::StartHandler(debug_ip, debug_port);
1369 if (print_msg) {
1370 Log::Print("Debugger listening on port %d\n", debug_port);
1371 }
1372 } else {
1373 DebuggerConnectionHandler::InitForVmService();
1374 }
1375
1376 const uint8_t* instructions_snapshot = NULL;
1377 if (has_run_precompiled_snapshot) {
1378 instructions_snapshot = reinterpret_cast<const uint8_t*>(
1379 LoadLibrarySymbol(kPrecompiledLibraryName, kPrecompiledSymbolName));
1380 ReadSnapshotFile(kPrecompiledVmIsolateName, &vm_isolate_snapshot_buffer);
1381 ReadSnapshotFile(kPrecompiledIsolateName, &isolate_snapshot_buffer);
1382 }
1383
1384 // Initialize the Dart VM.
1385 char* error = Dart_Initialize(
1386 vm_isolate_snapshot_buffer, instructions_snapshot,
1387 CreateIsolateAndSetup, NULL, NULL, ShutdownIsolate,
1388 DartUtils::OpenFile,
1389 DartUtils::ReadFile,
1390 DartUtils::WriteFile,
1391 DartUtils::CloseFile,
1392 DartUtils::EntropySource);
1393 if (error != NULL) {
1394 if (do_vm_shutdown) {
1395 DebuggerConnectionHandler::StopHandler();
1396 EventHandler::Stop();
1397 }
1398 fprintf(stderr, "VM initialization failed: %s\n", error);
1399 fflush(stderr);
1400 free(error);
1401 exit(kErrorExitCode);
1402 }
1403
1404 Dart_RegisterIsolateServiceRequestCallback(
1405 "getIO", &ServiceGetIOHandler, NULL);
1406 Dart_SetServiceStreamCallbacks(&ServiceStreamListenCallback,
1407 &ServiceStreamCancelCallback);
1408
1409 // Run the main isolate until we aren't told to restart.
1410 while (RunMainIsolate(script_name, &dart_options)) {
1411 Log::PrintErr("Restarting VM\n");
1412 }
1413
1382 // Terminate process exit-code handler. 1414 // Terminate process exit-code handler.
1383 Process::TerminateExitCodeHandler(); 1415 Process::TerminateExitCodeHandler();
1384 1416
1385 error = Dart_Cleanup(); 1417 error = Dart_Cleanup();
1386 if (error != NULL) { 1418 if (error != NULL) {
1387 Log::PrintErr("VM cleanup failed: %s\n", error); 1419 Log::PrintErr("VM cleanup failed: %s\n", error);
1388 free(error); 1420 free(error);
1389 } 1421 }
1390 if (do_vm_shutdown) { 1422 if (do_vm_shutdown) {
1391 DebuggerConnectionHandler::StopHandler(); 1423 DebuggerConnectionHandler::StopHandler();
(...skipping 19 matching lines...) Expand all
1411 exit(Process::GlobalExitCode()); 1443 exit(Process::GlobalExitCode());
1412 } 1444 }
1413 1445
1414 } // namespace bin 1446 } // namespace bin
1415 } // namespace dart 1447 } // namespace dart
1416 1448
1417 int main(int argc, char** argv) { 1449 int main(int argc, char** argv) {
1418 dart::bin::main(argc, argv); 1450 dart::bin::main(argc, argv);
1419 UNREACHABLE(); 1451 UNREACHABLE();
1420 } 1452 }
OLDNEW
« no previous file with comments | « no previous file | runtime/include/dart_api.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698