| 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 20 matching lines...) Expand all Loading... |
| 31 // vm_isolate_snapshot_buffer points to a snapshot for the vm isolate if we | 31 // vm_isolate_snapshot_buffer points to a snapshot for the vm isolate if we |
| 32 // link in a snapshot otherwise it is initialized to NULL. | 32 // link in a snapshot otherwise it is initialized to NULL. |
| 33 extern const uint8_t* vm_isolate_snapshot_buffer; | 33 extern const uint8_t* vm_isolate_snapshot_buffer; |
| 34 | 34 |
| 35 // isolate_snapshot_buffer points to a snapshot for an isolate if we link in a | 35 // isolate_snapshot_buffer points to a snapshot for an isolate if we link in a |
| 36 // snapshot otherwise it is initialized to NULL. | 36 // snapshot otherwise it is initialized to NULL. |
| 37 extern const uint8_t* isolate_snapshot_buffer; | 37 extern const uint8_t* isolate_snapshot_buffer; |
| 38 | 38 |
| 39 // Global state that stores a pointer to the application script snapshot. | 39 // Global state that stores a pointer to the application script snapshot. |
| 40 static bool generate_script_snapshot = false; | 40 static bool generate_script_snapshot = false; |
| 41 static bool generate_script_snapshot_after_run = false; |
| 41 static const char* snapshot_filename = NULL; | 42 static const char* snapshot_filename = NULL; |
| 42 | 43 |
| 43 | 44 |
| 44 // Global state that indicates whether there is a debug breakpoint. | 45 // Global state that indicates whether there is a debug breakpoint. |
| 45 // This pointer points into an argv buffer and does not need to be | 46 // This pointer points into an argv buffer and does not need to be |
| 46 // free'd. | 47 // free'd. |
| 47 static const char* breakpoint_at = NULL; | 48 static const char* breakpoint_at = NULL; |
| 48 | 49 |
| 49 | 50 |
| 50 // Global state that indicates whether we should open a connection | 51 // Global state that indicates whether we should open a connection |
| (...skipping 323 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 374 "Use --debug[:<port number>[/<IPv4 address>]]\n"); | 375 "Use --debug[:<port number>[/<IPv4 address>]]\n"); |
| 375 return false; | 376 return false; |
| 376 } | 377 } |
| 377 | 378 |
| 378 breakpoint_at = "main"; | 379 breakpoint_at = "main"; |
| 379 start_debugger = true; | 380 start_debugger = true; |
| 380 return true; | 381 return true; |
| 381 } | 382 } |
| 382 | 383 |
| 383 | 384 |
| 384 static bool ProcessGenScriptSnapshotOption(const char* filename, | 385 static bool ProcessScriptSnapshotOptionHelper(const char* filename, |
| 385 CommandLineOptions* vm_options) { | 386 bool* snapshot_option) { |
| 386 if (filename != NULL && strlen(filename) != 0) { | 387 *snapshot_option = false; |
| 388 if ((filename != NULL) && (strlen(filename) != 0)) { |
| 387 // Ensure that we are already running using a full snapshot. | 389 // Ensure that we are already running using a full snapshot. |
| 388 if (isolate_snapshot_buffer == NULL) { | 390 if (isolate_snapshot_buffer == NULL) { |
| 389 Log::PrintErr("Script snapshots cannot be generated in this version of" | 391 Log::PrintErr("Script snapshots cannot be generated in this version of" |
| 390 " dart\n"); | 392 " dart\n"); |
| 391 return false; | 393 return false; |
| 392 } | 394 } |
| 393 snapshot_filename = filename; | 395 snapshot_filename = filename; |
| 394 generate_script_snapshot = true; | 396 *snapshot_option = true; |
| 397 if (generate_script_snapshot && generate_script_snapshot_after_run) { |
| 398 Log::PrintErr("--snapshot and --snapshot-after-run options" |
| 399 " cannot be specified at the same time\n"); |
| 400 return false; |
| 401 } |
| 395 return true; | 402 return true; |
| 396 } | 403 } |
| 397 return false; | 404 return false; |
| 398 } | 405 } |
| 399 | 406 |
| 400 | 407 |
| 408 static bool ProcessScriptSnapshotOption(const char* filename, |
| 409 CommandLineOptions* vm_options) { |
| 410 return ProcessScriptSnapshotOptionHelper(filename, &generate_script_snapshot); |
| 411 } |
| 412 |
| 413 |
| 414 static bool ProcessScriptSnapshotAfterRunOption( |
| 415 const char* filename, CommandLineOptions* vm_options) { |
| 416 return ProcessScriptSnapshotOptionHelper(filename, |
| 417 &generate_script_snapshot_after_run); |
| 418 } |
| 419 |
| 420 |
| 401 static bool ProcessEnableVmServiceOption(const char* option_value, | 421 static bool ProcessEnableVmServiceOption(const char* option_value, |
| 402 CommandLineOptions* vm_options) { | 422 CommandLineOptions* vm_options) { |
| 403 ASSERT(option_value != NULL); | 423 ASSERT(option_value != NULL); |
| 404 | 424 |
| 405 if (!ExtractPortAndIP(option_value, | 425 if (!ExtractPortAndIP(option_value, |
| 406 &vm_service_server_port, | 426 &vm_service_server_port, |
| 407 &vm_service_server_ip, | 427 &vm_service_server_ip, |
| 408 DEFAULT_VM_SERVICE_SERVER_PORT, | 428 DEFAULT_VM_SERVICE_SERVER_PORT, |
| 409 DEFAULT_VM_SERVICE_SERVER_IP)) { | 429 DEFAULT_VM_SERVICE_SERVER_IP)) { |
| 410 Log::PrintErr("unrecognized --enable-vm-service option syntax. " | 430 Log::PrintErr("unrecognized --enable-vm-service option syntax. " |
| (...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 501 // VM specific options to the standalone dart program. | 521 // VM specific options to the standalone dart program. |
| 502 { "--break-at=", ProcessBreakpointOption }, | 522 { "--break-at=", ProcessBreakpointOption }, |
| 503 { "--compile_all", ProcessCompileAllOption }, | 523 { "--compile_all", ProcessCompileAllOption }, |
| 504 { "--debug", ProcessDebugOption }, | 524 { "--debug", ProcessDebugOption }, |
| 505 { "--enable-vm-service", ProcessEnableVmServiceOption }, | 525 { "--enable-vm-service", ProcessEnableVmServiceOption }, |
| 506 { "--gen-precompiled-snapshot", ProcessGenPrecompiledSnapshotOption }, | 526 { "--gen-precompiled-snapshot", ProcessGenPrecompiledSnapshotOption }, |
| 507 { "--noopt", ProcessNooptOption }, | 527 { "--noopt", ProcessNooptOption }, |
| 508 { "--observe", ProcessObserveOption }, | 528 { "--observe", ProcessObserveOption }, |
| 509 { "--run-precompiled-snapshot", ProcessRunPrecompiledSnapshotOption }, | 529 { "--run-precompiled-snapshot", ProcessRunPrecompiledSnapshotOption }, |
| 510 { "--shutdown", ProcessShutdownOption }, | 530 { "--shutdown", ProcessShutdownOption }, |
| 511 { "--snapshot=", ProcessGenScriptSnapshotOption }, | 531 { "--snapshot=", ProcessScriptSnapshotOption }, |
| 532 { "--snapshot-after-run=", ProcessScriptSnapshotAfterRunOption }, |
| 512 { "--trace-debug-protocol", ProcessTraceDebugProtocolOption }, | 533 { "--trace-debug-protocol", ProcessTraceDebugProtocolOption }, |
| 513 { "--trace-loading", ProcessTraceLoadingOption }, | 534 { "--trace-loading", ProcessTraceLoadingOption }, |
| 514 { NULL, NULL } | 535 { NULL, NULL } |
| 515 }; | 536 }; |
| 516 | 537 |
| 517 | 538 |
| 518 static bool ProcessMainOptions(const char* option, | 539 static bool ProcessMainOptions(const char* option, |
| 519 CommandLineOptions* vm_options) { | 540 CommandLineOptions* vm_options) { |
| 520 int i = 0; | 541 int i = 0; |
| 521 const char* name = main_options[0].option_name; | 542 const char* name = main_options[0].option_name; |
| (...skipping 577 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1099 } | 1120 } |
| 1100 void* symbol = Extensions::ResolveSymbol(library, symname); | 1121 void* symbol = Extensions::ResolveSymbol(library, symname); |
| 1101 if (symbol == NULL) { | 1122 if (symbol == NULL) { |
| 1102 Log::PrintErr("Error: Failed to load symbol '%s'\n", symname); | 1123 Log::PrintErr("Error: Failed to load symbol '%s'\n", symname); |
| 1103 exit(kErrorExitCode); | 1124 exit(kErrorExitCode); |
| 1104 } | 1125 } |
| 1105 return symbol; | 1126 return symbol; |
| 1106 } | 1127 } |
| 1107 | 1128 |
| 1108 | 1129 |
| 1130 static void GenerateScriptSnapshot() { |
| 1131 // First create a snapshot. |
| 1132 uint8_t* buffer = NULL; |
| 1133 intptr_t size = 0; |
| 1134 Dart_Handle result = Dart_CreateScriptSnapshot(&buffer, &size); |
| 1135 if (Dart_IsError(result)) { |
| 1136 ErrorExit(kErrorExitCode, "%s\n", Dart_GetError(result)); |
| 1137 } |
| 1138 |
| 1139 // Open the snapshot file. |
| 1140 File* snapshot_file = File::Open(snapshot_filename, File::kWriteTruncate); |
| 1141 if (snapshot_file == NULL) { |
| 1142 ErrorExit(kErrorExitCode, |
| 1143 "Unable to open file %s for writing the snapshot\n", |
| 1144 snapshot_filename); |
| 1145 } |
| 1146 |
| 1147 // Write the magic number to indicate file is a script snapshot. |
| 1148 DartUtils::WriteMagicNumber(snapshot_file); |
| 1149 |
| 1150 // Now write the snapshot out to specified file. |
| 1151 bool bytes_written = snapshot_file->WriteFully(buffer, size); |
| 1152 ASSERT(bytes_written); |
| 1153 delete snapshot_file; |
| 1154 snapshot_file = NULL; |
| 1155 } |
| 1156 |
| 1157 |
| 1109 #define CHECK_RESULT(result) \ | 1158 #define CHECK_RESULT(result) \ |
| 1110 if (Dart_IsError(result)) { \ | 1159 if (Dart_IsError(result)) { \ |
| 1111 if (Dart_IsVMRestartRequest(result)) { \ | 1160 if (Dart_IsVMRestartRequest(result)) { \ |
| 1112 Dart_ExitScope(); \ | 1161 Dart_ExitScope(); \ |
| 1113 Dart_ShutdownIsolate(); \ | 1162 Dart_ShutdownIsolate(); \ |
| 1114 return true; \ | 1163 return true; \ |
| 1115 } \ | 1164 } \ |
| 1116 const int exit_code = Dart_IsCompilationError(result) ? \ | 1165 const int exit_code = Dart_IsCompilationError(result) ? \ |
| 1117 kCompilationErrorExitCode : kErrorExitCode; \ | 1166 kCompilationErrorExitCode : kErrorExitCode; \ |
| 1118 ErrorExit(exit_code, "%s\n", Dart_GetError(result)); \ | 1167 ErrorExit(exit_code, "%s\n", Dart_GetError(result)); \ |
| 1119 } | 1168 } |
| 1120 | 1169 |
| 1170 |
| 1121 bool RunMainIsolate(const char* script_name, | 1171 bool RunMainIsolate(const char* script_name, |
| 1122 CommandLineOptions* dart_options) { | 1172 CommandLineOptions* dart_options) { |
| 1123 // Call CreateIsolateAndSetup which creates an isolate and loads up | 1173 // Call CreateIsolateAndSetup which creates an isolate and loads up |
| 1124 // the specified application script. | 1174 // the specified application script. |
| 1125 char* error = NULL; | 1175 char* error = NULL; |
| 1126 int exit_code = 0; | 1176 int exit_code = 0; |
| 1127 char* isolate_name = BuildIsolateName(script_name, "main"); | 1177 char* isolate_name = BuildIsolateName(script_name, "main"); |
| 1128 Dart_Isolate isolate = CreateIsolateAndSetupHelper(script_name, | 1178 Dart_Isolate isolate = CreateIsolateAndSetupHelper(script_name, |
| 1129 "main", | 1179 "main", |
| 1130 commandline_package_root, | 1180 commandline_package_root, |
| (...skipping 26 matching lines...) Expand all Loading... |
| 1157 delete [] isolate_name; | 1207 delete [] isolate_name; |
| 1158 | 1208 |
| 1159 Dart_EnterIsolate(isolate); | 1209 Dart_EnterIsolate(isolate); |
| 1160 ASSERT(isolate == Dart_CurrentIsolate()); | 1210 ASSERT(isolate == Dart_CurrentIsolate()); |
| 1161 ASSERT(isolate != NULL); | 1211 ASSERT(isolate != NULL); |
| 1162 Dart_Handle result; | 1212 Dart_Handle result; |
| 1163 | 1213 |
| 1164 Dart_EnterScope(); | 1214 Dart_EnterScope(); |
| 1165 | 1215 |
| 1166 if (generate_script_snapshot) { | 1216 if (generate_script_snapshot) { |
| 1167 // First create a snapshot. | 1217 GenerateScriptSnapshot(); |
| 1168 Dart_Handle result; | |
| 1169 uint8_t* buffer = NULL; | |
| 1170 intptr_t size = 0; | |
| 1171 result = Dart_CreateScriptSnapshot(&buffer, &size); | |
| 1172 CHECK_RESULT(result); | |
| 1173 | |
| 1174 // Open the snapshot file. | |
| 1175 File* snapshot_file = File::Open(snapshot_filename, File::kWriteTruncate); | |
| 1176 if (snapshot_file == NULL) { | |
| 1177 ErrorExit(kErrorExitCode, | |
| 1178 "Unable to open file %s for writing the snapshot\n", | |
| 1179 snapshot_filename); | |
| 1180 } | |
| 1181 | |
| 1182 // Write the magic number to indicate file is a script snapshot. | |
| 1183 DartUtils::WriteMagicNumber(snapshot_file); | |
| 1184 | |
| 1185 // Now write the snapshot out to specified file. | |
| 1186 bool bytes_written = snapshot_file->WriteFully(buffer, size); | |
| 1187 ASSERT(bytes_written); | |
| 1188 delete snapshot_file; | |
| 1189 snapshot_file = NULL; | |
| 1190 } else { | 1218 } else { |
| 1191 // Lookup the library of the root script. | 1219 // Lookup the library of the root script. |
| 1192 Dart_Handle root_lib = Dart_RootLibrary(); | 1220 Dart_Handle root_lib = Dart_RootLibrary(); |
| 1193 // Import the root library into the builtin library so that we can easily | 1221 // Import the root library into the builtin library so that we can easily |
| 1194 // lookup the main entry point exported from the root library. | 1222 // lookup the main entry point exported from the root library. |
| 1195 Dart_Handle builtin_lib = | 1223 Dart_Handle builtin_lib = |
| 1196 Builtin::LoadAndCheckLibrary(Builtin::kBuiltinLibrary); | 1224 Builtin::LoadAndCheckLibrary(Builtin::kBuiltinLibrary); |
| 1197 ASSERT(!Dart_IsError(builtin_lib)); | 1225 ASSERT(!Dart_IsError(builtin_lib)); |
| 1198 result = Dart_LibraryImportLibrary(builtin_lib, root_lib, Dart_Null()); | 1226 result = Dart_LibraryImportLibrary(builtin_lib, root_lib, Dart_Null()); |
| 1199 | 1227 |
| (...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1292 Dart_Handle isolate_lib = | 1320 Dart_Handle isolate_lib = |
| 1293 Dart_LookupLibrary(Dart_NewStringFromCString("dart:isolate")); | 1321 Dart_LookupLibrary(Dart_NewStringFromCString("dart:isolate")); |
| 1294 result = Dart_Invoke(isolate_lib, | 1322 result = Dart_Invoke(isolate_lib, |
| 1295 Dart_NewStringFromCString("_startMainIsolate"), | 1323 Dart_NewStringFromCString("_startMainIsolate"), |
| 1296 kNumIsolateArgs, isolate_args); | 1324 kNumIsolateArgs, isolate_args); |
| 1297 CHECK_RESULT(result); | 1325 CHECK_RESULT(result); |
| 1298 | 1326 |
| 1299 // Keep handling messages until the last active receive port is closed. | 1327 // Keep handling messages until the last active receive port is closed. |
| 1300 result = Dart_RunLoop(); | 1328 result = Dart_RunLoop(); |
| 1301 CHECK_RESULT(result); | 1329 CHECK_RESULT(result); |
| 1330 |
| 1331 // Generate a script snapshot after execution if specified. |
| 1332 if (generate_script_snapshot_after_run) { |
| 1333 GenerateScriptSnapshot(); |
| 1334 } |
| 1302 } | 1335 } |
| 1303 } | 1336 } |
| 1304 | 1337 |
| 1305 Dart_ExitScope(); | 1338 Dart_ExitScope(); |
| 1306 // Shutdown the isolate. | 1339 // Shutdown the isolate. |
| 1307 Dart_ShutdownIsolate(); | 1340 Dart_ShutdownIsolate(); |
| 1308 | 1341 |
| 1309 // No restart. | 1342 // No restart. |
| 1310 return false; | 1343 return false; |
| 1311 } | 1344 } |
| (...skipping 144 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1456 exit(Process::GlobalExitCode()); | 1489 exit(Process::GlobalExitCode()); |
| 1457 } | 1490 } |
| 1458 | 1491 |
| 1459 } // namespace bin | 1492 } // namespace bin |
| 1460 } // namespace dart | 1493 } // namespace dart |
| 1461 | 1494 |
| 1462 int main(int argc, char** argv) { | 1495 int main(int argc, char** argv) { |
| 1463 dart::bin::main(argc, argv); | 1496 dart::bin::main(argc, argv); |
| 1464 UNREACHABLE(); | 1497 UNREACHABLE(); |
| 1465 } | 1498 } |
| OLD | NEW |