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