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 114 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
125 | 125 |
126 // Exit code indicating an API error. | 126 // Exit code indicating an API error. |
127 static const int kApiErrorExitCode = 253; | 127 static const int kApiErrorExitCode = 253; |
128 // Exit code indicating a compilation error. | 128 // Exit code indicating a compilation error. |
129 static const int kCompilationErrorExitCode = 254; | 129 static const int kCompilationErrorExitCode = 254; |
130 // Exit code indicating an unhandled error that is not a compilation error. | 130 // Exit code indicating an unhandled error that is not a compilation error. |
131 static const int kErrorExitCode = 255; | 131 static const int kErrorExitCode = 255; |
132 // Exit code indicating a vm restart request. Never returned to the user. | 132 // Exit code indicating a vm restart request. Never returned to the user. |
133 static const int kRestartRequestExitCode = 1000; | 133 static const int kRestartRequestExitCode = 1000; |
134 | 134 |
135 // Global flag that is used to indicate that the VM should do a clean | |
136 // shutdown. | |
137 static bool do_vm_shutdown = true; | |
138 | |
139 static void ErrorExit(int exit_code, const char* format, ...) { | 135 static void ErrorExit(int exit_code, const char* format, ...) { |
140 va_list arguments; | 136 va_list arguments; |
141 va_start(arguments, format); | 137 va_start(arguments, format); |
142 Log::VPrintErr(format, arguments); | 138 Log::VPrintErr(format, arguments); |
143 va_end(arguments); | 139 va_end(arguments); |
144 fflush(stderr); | 140 fflush(stderr); |
145 | 141 |
146 Dart_ExitScope(); | 142 Dart_ExitScope(); |
147 Dart_ShutdownIsolate(); | 143 Dart_ShutdownIsolate(); |
148 | 144 |
149 // Terminate process exit-code handler. | 145 // Terminate process exit-code handler. |
150 Process::TerminateExitCodeHandler(); | 146 Process::TerminateExitCodeHandler(); |
151 | 147 |
152 char* error = Dart_Cleanup(); | 148 char* error = Dart_Cleanup(); |
153 if (error != NULL) { | 149 if (error != NULL) { |
154 Log::PrintErr("VM cleanup failed: %s\n", error); | 150 Log::PrintErr("VM cleanup failed: %s\n", error); |
155 free(error); | 151 free(error); |
156 } | 152 } |
157 | 153 |
158 if (do_vm_shutdown) { | 154 EventHandler::Stop(); |
159 EventHandler::Stop(); | |
160 } | |
161 Platform::Exit(exit_code); | 155 Platform::Exit(exit_code); |
162 } | 156 } |
163 | 157 |
164 | 158 |
165 // The environment provided through the command line using -D options. | 159 // The environment provided through the command line using -D options. |
166 static dart::HashMap* environment = NULL; | 160 static dart::HashMap* environment = NULL; |
167 | 161 |
168 static bool IsValidFlag(const char* name, | 162 static bool IsValidFlag(const char* name, |
169 const char* prefix, | 163 const char* prefix, |
170 intptr_t prefix_length) { | 164 intptr_t prefix_length) { |
(...skipping 280 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
451 vm_options->AddArgument("--reload_every_optimized=false"); | 445 vm_options->AddArgument("--reload_every_optimized=false"); |
452 // Reload less frequently as time goes on. | 446 // Reload less frequently as time goes on. |
453 vm_options->AddArgument("--reload_every_back_off"); | 447 vm_options->AddArgument("--reload_every_back_off"); |
454 // Ensure that every isolate has reloaded once before exiting. | 448 // Ensure that every isolate has reloaded once before exiting. |
455 vm_options->AddArgument("--check_reloaded"); | 449 vm_options->AddArgument("--check_reloaded"); |
456 | 450 |
457 return true; | 451 return true; |
458 } | 452 } |
459 | 453 |
460 | 454 |
461 static bool ProcessShutdownOption(const char* arg, | |
462 CommandLineOptions* vm_options) { | |
463 ASSERT(arg != NULL); | |
464 if (*arg == '\0') { | |
465 do_vm_shutdown = true; | |
466 vm_options->AddArgument("--shutdown"); | |
467 return true; | |
468 } | |
469 | |
470 if ((*arg != '=') && (*arg != ':')) { | |
471 return false; | |
472 } | |
473 | |
474 if (strcmp(arg + 1, "true") == 0) { | |
475 do_vm_shutdown = true; | |
476 vm_options->AddArgument("--shutdown"); | |
477 return true; | |
478 } else if (strcmp(arg + 1, "false") == 0) { | |
479 do_vm_shutdown = false; | |
480 vm_options->AddArgument("--no-shutdown"); | |
481 return true; | |
482 } | |
483 | |
484 return false; | |
485 } | |
486 | |
487 | |
488 static struct { | 455 static struct { |
489 const char* option_name; | 456 const char* option_name; |
490 bool (*process)(const char* option, CommandLineOptions* vm_options); | 457 bool (*process)(const char* option, CommandLineOptions* vm_options); |
491 } main_options[] = { | 458 } main_options[] = { |
492 // Standard options shared with dart2js. | 459 // Standard options shared with dart2js. |
493 { "-D", ProcessEnvironmentOption }, | 460 { "-D", ProcessEnvironmentOption }, |
494 { "-h", ProcessHelpOption }, | 461 { "-h", ProcessHelpOption }, |
495 { "--help", ProcessHelpOption }, | 462 { "--help", ProcessHelpOption }, |
496 { "--packages=", ProcessPackagesOption }, | 463 { "--packages=", ProcessPackagesOption }, |
497 { "--package-root=", ProcessPackageRootOption }, | 464 { "--package-root=", ProcessPackageRootOption }, |
498 { "-v", ProcessVerboseOption }, | 465 { "-v", ProcessVerboseOption }, |
499 { "--verbose", ProcessVerboseOption }, | 466 { "--verbose", ProcessVerboseOption }, |
500 { "--version", ProcessVersionOption }, | 467 { "--version", ProcessVersionOption }, |
501 | 468 |
502 // VM specific options to the standalone dart program. | 469 // VM specific options to the standalone dart program. |
503 { "--compile_all", ProcessCompileAllOption }, | 470 { "--compile_all", ProcessCompileAllOption }, |
504 { "--enable-vm-service", ProcessEnableVmServiceOption }, | 471 { "--enable-vm-service", ProcessEnableVmServiceOption }, |
505 { "--disable-service-origin-check", ProcessDisableServiceOriginCheckOption }, | 472 { "--disable-service-origin-check", ProcessDisableServiceOriginCheckOption }, |
506 { "--observe", ProcessObserveOption }, | 473 { "--observe", ProcessObserveOption }, |
507 { "--shutdown", ProcessShutdownOption }, | |
508 { "--snapshot=", ProcessSnapshotFilenameOption }, | 474 { "--snapshot=", ProcessSnapshotFilenameOption }, |
509 { "--snapshot-kind=", ProcessSnapshotKindOption }, | 475 { "--snapshot-kind=", ProcessSnapshotKindOption }, |
510 { "--run-app-snapshot=", ProcessRunAppSnapshotOption }, | 476 { "--run-app-snapshot=", ProcessRunAppSnapshotOption }, |
511 { "--use-blobs", ProcessUseBlobsOption }, | 477 { "--use-blobs", ProcessUseBlobsOption }, |
512 { "--trace-loading", ProcessTraceLoadingOption }, | 478 { "--trace-loading", ProcessTraceLoadingOption }, |
513 { "--hot-reload-test-mode", ProcessHotReloadTestModeOption }, | 479 { "--hot-reload-test-mode", ProcessHotReloadTestModeOption }, |
514 { NULL, NULL } | 480 { NULL, NULL } |
515 }; | 481 }; |
516 | 482 |
517 | 483 |
(...skipping 866 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1384 } | 1350 } |
1385 Log::PrintErr("%s\n", error); | 1351 Log::PrintErr("%s\n", error); |
1386 free(error); | 1352 free(error); |
1387 error = NULL; | 1353 error = NULL; |
1388 Process::TerminateExitCodeHandler(); | 1354 Process::TerminateExitCodeHandler(); |
1389 error = Dart_Cleanup(); | 1355 error = Dart_Cleanup(); |
1390 if (error != NULL) { | 1356 if (error != NULL) { |
1391 Log::PrintErr("VM cleanup failed: %s\n", error); | 1357 Log::PrintErr("VM cleanup failed: %s\n", error); |
1392 free(error); | 1358 free(error); |
1393 } | 1359 } |
1394 if (do_vm_shutdown) { | 1360 EventHandler::Stop(); |
1395 EventHandler::Stop(); | |
1396 } | |
1397 Platform::Exit((exit_code != 0) ? exit_code : kErrorExitCode); | 1361 Platform::Exit((exit_code != 0) ? exit_code : kErrorExitCode); |
1398 } | 1362 } |
1399 delete [] isolate_name; | 1363 delete [] isolate_name; |
1400 | 1364 |
1401 Dart_EnterIsolate(isolate); | 1365 Dart_EnterIsolate(isolate); |
1402 ASSERT(isolate == Dart_CurrentIsolate()); | 1366 ASSERT(isolate == Dart_CurrentIsolate()); |
1403 ASSERT(isolate != NULL); | 1367 ASSERT(isolate != NULL); |
1404 Dart_Handle result; | 1368 Dart_Handle result; |
1405 | 1369 |
1406 Dart_EnterScope(); | 1370 Dart_EnterScope(); |
(...skipping 253 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1660 Dart_SetVMFlags(vm_options.count(), vm_options.arguments()); | 1624 Dart_SetVMFlags(vm_options.count(), vm_options.arguments()); |
1661 Platform::Exit(0); | 1625 Platform::Exit(0); |
1662 } else { | 1626 } else { |
1663 PrintUsage(); | 1627 PrintUsage(); |
1664 Platform::Exit(kErrorExitCode); | 1628 Platform::Exit(kErrorExitCode); |
1665 } | 1629 } |
1666 } | 1630 } |
1667 | 1631 |
1668 Thread::InitOnce(); | 1632 Thread::InitOnce(); |
1669 | 1633 |
| 1634 Loader::InitOnce(); |
| 1635 |
1670 if (!DartUtils::SetOriginalWorkingDirectory()) { | 1636 if (!DartUtils::SetOriginalWorkingDirectory()) { |
1671 OSError err; | 1637 OSError err; |
1672 fprintf(stderr, "Error determining current directory: %s\n", err.message()); | 1638 fprintf(stderr, "Error determining current directory: %s\n", err.message()); |
1673 fflush(stderr); | 1639 fflush(stderr); |
1674 Platform::Exit(kErrorExitCode); | 1640 Platform::Exit(kErrorExitCode); |
1675 } | 1641 } |
1676 | 1642 |
1677 #if !defined(PRODUCT) && !defined(DART_PRECOMPILED_RUNTIME) | 1643 #if !defined(PRODUCT) && !defined(DART_PRECOMPILED_RUNTIME) |
1678 // Constant true if PRODUCT or DART_PRECOMPILED_RUNTIME. | 1644 // Constant true if PRODUCT or DART_PRECOMPILED_RUNTIME. |
1679 if ((gen_snapshot_kind != kNone) || run_app_snapshot) { | 1645 if ((gen_snapshot_kind != kNone) || run_app_snapshot) { |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1724 vm_isolate_snapshot_buffer, instructions_snapshot, data_snapshot, | 1690 vm_isolate_snapshot_buffer, instructions_snapshot, data_snapshot, |
1725 CreateIsolateAndSetup, NULL, NULL, ShutdownIsolate, | 1691 CreateIsolateAndSetup, NULL, NULL, ShutdownIsolate, |
1726 NULL, | 1692 NULL, |
1727 DartUtils::OpenFile, | 1693 DartUtils::OpenFile, |
1728 DartUtils::ReadFile, | 1694 DartUtils::ReadFile, |
1729 DartUtils::WriteFile, | 1695 DartUtils::WriteFile, |
1730 DartUtils::CloseFile, | 1696 DartUtils::CloseFile, |
1731 DartUtils::EntropySource, | 1697 DartUtils::EntropySource, |
1732 GetVMServiceAssetsArchiveCallback); | 1698 GetVMServiceAssetsArchiveCallback); |
1733 if (error != NULL) { | 1699 if (error != NULL) { |
1734 if (do_vm_shutdown) { | 1700 EventHandler::Stop(); |
1735 EventHandler::Stop(); | |
1736 } | |
1737 fprintf(stderr, "VM initialization failed: %s\n", error); | 1701 fprintf(stderr, "VM initialization failed: %s\n", error); |
1738 fflush(stderr); | 1702 fflush(stderr); |
1739 free(error); | 1703 free(error); |
1740 Platform::Exit(kErrorExitCode); | 1704 Platform::Exit(kErrorExitCode); |
1741 } | 1705 } |
1742 | 1706 |
1743 Dart_RegisterIsolateServiceRequestCallback( | 1707 Dart_RegisterIsolateServiceRequestCallback( |
1744 "getIO", &ServiceGetIOHandler, NULL); | 1708 "getIO", &ServiceGetIOHandler, NULL); |
1745 Dart_SetServiceStreamCallbacks(&ServiceStreamListenCallback, | 1709 Dart_SetServiceStreamCallbacks(&ServiceStreamListenCallback, |
1746 &ServiceStreamCancelCallback); | 1710 &ServiceStreamCancelCallback); |
1747 | 1711 |
1748 // Run the main isolate until we aren't told to restart. | 1712 // Run the main isolate until we aren't told to restart. |
1749 while (RunMainIsolate(script_name, &dart_options)) { | 1713 while (RunMainIsolate(script_name, &dart_options)) { |
1750 Log::PrintErr("Restarting VM\n"); | 1714 Log::PrintErr("Restarting VM\n"); |
1751 } | 1715 } |
1752 | 1716 |
1753 // Terminate process exit-code handler. | 1717 // Terminate process exit-code handler. |
1754 Process::TerminateExitCodeHandler(); | 1718 Process::TerminateExitCodeHandler(); |
1755 | 1719 |
1756 error = Dart_Cleanup(); | 1720 error = Dart_Cleanup(); |
1757 if (error != NULL) { | 1721 if (error != NULL) { |
1758 Log::PrintErr("VM cleanup failed: %s\n", error); | 1722 Log::PrintErr("VM cleanup failed: %s\n", error); |
1759 free(error); | 1723 free(error); |
1760 } | 1724 } |
1761 if (do_vm_shutdown) { | 1725 EventHandler::Stop(); |
1762 EventHandler::Stop(); | |
1763 } | |
1764 | 1726 |
1765 // Free copied argument strings if converted. | 1727 // Free copied argument strings if converted. |
1766 if (argv_converted) { | 1728 if (argv_converted) { |
1767 for (int i = 0; i < argc; i++) { | 1729 for (int i = 0; i < argc; i++) { |
1768 free(argv[i]); | 1730 free(argv[i]); |
1769 } | 1731 } |
1770 } | 1732 } |
1771 | 1733 |
1772 // Free environment if any. | 1734 // Free environment if any. |
1773 if (environment != NULL) { | 1735 if (environment != NULL) { |
1774 for (HashMap::Entry* p = environment->Start(); | 1736 for (HashMap::Entry* p = environment->Start(); |
1775 p != NULL; | 1737 p != NULL; |
1776 p = environment->Next(p)) { | 1738 p = environment->Next(p)) { |
1777 free(p->key); | 1739 free(p->key); |
1778 free(p->value); | 1740 free(p->value); |
1779 } | 1741 } |
1780 delete environment; | 1742 delete environment; |
1781 } | 1743 } |
1782 | 1744 |
1783 Platform::Exit(Process::GlobalExitCode()); | 1745 Platform::Exit(Process::GlobalExitCode()); |
1784 } | 1746 } |
1785 | 1747 |
1786 } // namespace bin | 1748 } // namespace bin |
1787 } // namespace dart | 1749 } // namespace dart |
1788 | 1750 |
1789 int main(int argc, char** argv) { | 1751 int main(int argc, char** argv) { |
1790 dart::bin::main(argc, argv); | 1752 dart::bin::main(argc, argv); |
1791 UNREACHABLE(); | 1753 UNREACHABLE(); |
1792 } | 1754 } |
OLD | NEW |