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_debugger_api.h" | 10 #include "include/dart_debugger_api.h" |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
68 | 68 |
69 static const char* DEFAULT_VM_SERVICE_SERVER_IP = "127.0.0.1"; | 69 static const char* DEFAULT_VM_SERVICE_SERVER_IP = "127.0.0.1"; |
70 static const int DEFAULT_VM_SERVICE_SERVER_PORT = 8181; | 70 static const int DEFAULT_VM_SERVICE_SERVER_PORT = 8181; |
71 // VM Service options. | 71 // VM Service options. |
72 static bool start_vm_service = false; | 72 static bool start_vm_service = false; |
73 static const char* vm_service_server_ip = DEFAULT_VM_SERVICE_SERVER_IP; | 73 static const char* vm_service_server_ip = DEFAULT_VM_SERVICE_SERVER_IP; |
74 // The 0 port is a magic value which results in the first available port | 74 // The 0 port is a magic value which results in the first available port |
75 // being allocated. | 75 // being allocated. |
76 static int vm_service_server_port = -1; | 76 static int vm_service_server_port = -1; |
77 | 77 |
| 78 |
| 79 // Exit code indicating an API error. |
| 80 static const int kApiErrorExitCode = 253; |
| 81 // Exit code indicating a compilation error. |
| 82 static const int kCompilationErrorExitCode = 254; |
| 83 // Exit code indicating an unhandled error that is not a compilation error. |
| 84 static const int kErrorExitCode = 255; |
| 85 |
| 86 static void ErrorExit(int exit_code, const char* format, ...) { |
| 87 va_list arguments; |
| 88 va_start(arguments, format); |
| 89 Log::VPrintErr(format, arguments); |
| 90 va_end(arguments); |
| 91 fflush(stderr); |
| 92 |
| 93 Dart_ExitScope(); |
| 94 Dart_ShutdownIsolate(); |
| 95 |
| 96 Dart_Cleanup(); |
| 97 |
| 98 exit(exit_code); |
| 99 } |
| 100 |
| 101 |
78 // The environment provided through the command line using -D options. | 102 // The environment provided through the command line using -D options. |
79 static dart::HashMap* environment = NULL; | 103 static dart::HashMap* environment = NULL; |
80 | 104 |
81 static bool IsValidFlag(const char* name, | 105 static bool IsValidFlag(const char* name, |
82 const char* prefix, | 106 const char* prefix, |
83 intptr_t prefix_length) { | 107 intptr_t prefix_length) { |
84 intptr_t name_length = strlen(name); | 108 intptr_t name_length = strlen(name); |
85 return ((name_length > prefix_length) && | 109 return ((name_length > prefix_length) && |
86 (strncmp(name, prefix, prefix_length) == 0)); | 110 (strncmp(name, prefix, prefix_length) == 0)); |
87 } | 111 } |
(...skipping 445 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
533 } | 557 } |
534 free(name_chars); | 558 free(name_chars); |
535 } | 559 } |
536 return result; | 560 return result; |
537 } | 561 } |
538 | 562 |
539 | 563 |
540 #define CHECK_RESULT(result) \ | 564 #define CHECK_RESULT(result) \ |
541 if (Dart_IsError(result)) { \ | 565 if (Dart_IsError(result)) { \ |
542 *error = strdup(Dart_GetError(result)); \ | 566 *error = strdup(Dart_GetError(result)); \ |
543 *is_compile_error = Dart_IsCompilationError(result); \ | 567 *exit_code = Dart_IsCompilationError(result) ? kCompilationErrorExitCode : \ |
| 568 (Dart_IsApiError(result) ? kApiErrorExitCode : kErrorExitCode); \ |
544 Dart_ExitScope(); \ | 569 Dart_ExitScope(); \ |
545 Dart_ShutdownIsolate(); \ | 570 Dart_ShutdownIsolate(); \ |
546 return NULL; \ | 571 return NULL; \ |
547 } \ | 572 } \ |
548 | 573 |
549 | 574 |
550 // Returns true on success, false on failure. | 575 // Returns true on success, false on failure. |
551 static Dart_Isolate CreateIsolateAndSetupHelper(const char* script_uri, | 576 static Dart_Isolate CreateIsolateAndSetupHelper(const char* script_uri, |
552 const char* main, | 577 const char* main, |
553 const char* package_root, | 578 const char* package_root, |
554 char** error, | 579 char** error, |
555 bool* is_compile_error) { | 580 int* exit_code) { |
556 ASSERT(script_uri != NULL); | 581 ASSERT(script_uri != NULL); |
557 IsolateData* isolate_data = new IsolateData(script_uri, package_root); | 582 IsolateData* isolate_data = new IsolateData(script_uri, package_root); |
558 Dart_Isolate isolate = NULL; | 583 Dart_Isolate isolate = NULL; |
559 | 584 |
560 isolate = Dart_CreateIsolate(script_uri, | 585 isolate = Dart_CreateIsolate(script_uri, |
561 main, | 586 main, |
562 snapshot_buffer, | 587 snapshot_buffer, |
563 isolate_data, | 588 isolate_data, |
564 error); | 589 error); |
565 | 590 |
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
633 return isolate; | 658 return isolate; |
634 } | 659 } |
635 | 660 |
636 #undef CHECK_RESULT | 661 #undef CHECK_RESULT |
637 | 662 |
638 static Dart_Isolate CreateIsolateAndSetup(const char* script_uri, | 663 static Dart_Isolate CreateIsolateAndSetup(const char* script_uri, |
639 const char* main, | 664 const char* main, |
640 const char* package_root, | 665 const char* package_root, |
641 void* data, char** error) { | 666 void* data, char** error) { |
642 IsolateData* parent_isolate_data = reinterpret_cast<IsolateData*>(data); | 667 IsolateData* parent_isolate_data = reinterpret_cast<IsolateData*>(data); |
643 bool is_compile_error = false; | 668 int exit_code = 0; |
644 if (script_uri == NULL) { | 669 if (script_uri == NULL) { |
645 if (data == NULL) { | 670 if (data == NULL) { |
646 *error = strdup("Invalid 'callback_data' - Unable to spawn new isolate"); | 671 *error = strdup("Invalid 'callback_data' - Unable to spawn new isolate"); |
647 return NULL; | 672 return NULL; |
648 } | 673 } |
649 script_uri = parent_isolate_data->script_url; | 674 script_uri = parent_isolate_data->script_url; |
650 if (script_uri == NULL) { | 675 if (script_uri == NULL) { |
651 *error = strdup("Invalid 'callback_data' - Unable to spawn new isolate"); | 676 *error = strdup("Invalid 'callback_data' - Unable to spawn new isolate"); |
652 return NULL; | 677 return NULL; |
653 } | 678 } |
654 } | 679 } |
655 if (package_root == NULL) { | 680 if (package_root == NULL) { |
656 if (parent_isolate_data != NULL) { | 681 if (parent_isolate_data != NULL) { |
657 package_root = parent_isolate_data->package_root; | 682 package_root = parent_isolate_data->package_root; |
658 } else { | 683 } else { |
659 package_root = "."; | 684 package_root = "."; |
660 } | 685 } |
661 } | 686 } |
662 return CreateIsolateAndSetupHelper(script_uri, | 687 return CreateIsolateAndSetupHelper(script_uri, |
663 main, | 688 main, |
664 package_root, | 689 package_root, |
665 error, | 690 error, |
666 &is_compile_error); | 691 &exit_code); |
667 } | 692 } |
668 | 693 |
669 | 694 |
670 static void PrintVersion() { | 695 static void PrintVersion() { |
671 Log::PrintErr("Dart VM version: %s\n", Dart_VersionString()); | 696 Log::PrintErr("Dart VM version: %s\n", Dart_VersionString()); |
672 } | 697 } |
673 | 698 |
674 | 699 |
675 static void PrintUsage() { | 700 static void PrintUsage() { |
676 Log::PrintErr( | 701 Log::PrintErr( |
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
758 } | 783 } |
759 | 784 |
760 const char* kFormat = "%s/%s"; | 785 const char* kFormat = "%s/%s"; |
761 intptr_t len = strlen(script_name) + strlen(func_name) + 2; | 786 intptr_t len = strlen(script_name) + strlen(func_name) + 2; |
762 char* buffer = new char[len]; | 787 char* buffer = new char[len]; |
763 ASSERT(buffer != NULL); | 788 ASSERT(buffer != NULL); |
764 snprintf(buffer, len, kFormat, script_name, func_name); | 789 snprintf(buffer, len, kFormat, script_name, func_name); |
765 return buffer; | 790 return buffer; |
766 } | 791 } |
767 | 792 |
768 | |
769 // Exit code indicating a compilation error. | |
770 static const int kCompilationErrorExitCode = 254; | |
771 | |
772 // Exit code indicating an unhandled error that is not a compilation error. | |
773 static const int kErrorExitCode = 255; | |
774 | |
775 static void ErrorExit(int exit_code, const char* format, ...) { | |
776 va_list arguments; | |
777 va_start(arguments, format); | |
778 Log::VPrintErr(format, arguments); | |
779 va_end(arguments); | |
780 fflush(stderr); | |
781 | |
782 Dart_ExitScope(); | |
783 Dart_ShutdownIsolate(); | |
784 | |
785 Dart_Cleanup(); | |
786 | |
787 exit(exit_code); | |
788 } | |
789 | |
790 | |
791 static void DartExitOnError(Dart_Handle error) { | 793 static void DartExitOnError(Dart_Handle error) { |
792 if (!Dart_IsError(error)) { | 794 if (!Dart_IsError(error)) { |
793 return; | 795 return; |
794 } | 796 } |
795 const int exit_code = Dart_IsCompilationError(error) ? | 797 const int exit_code = Dart_IsCompilationError(error) ? |
796 kCompilationErrorExitCode : kErrorExitCode; | 798 kCompilationErrorExitCode : kErrorExitCode; |
797 ErrorExit(exit_code, "%s\n", Dart_GetError(error)); | 799 ErrorExit(exit_code, "%s\n", Dart_GetError(error)); |
798 } | 800 } |
799 | 801 |
800 | 802 |
(...skipping 174 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
975 } else { | 977 } else { |
976 DebuggerConnectionHandler::InitForVmService(); | 978 DebuggerConnectionHandler::InitForVmService(); |
977 } | 979 } |
978 | 980 |
979 Dart_RegisterIsolateServiceRequestCallback( | 981 Dart_RegisterIsolateServiceRequestCallback( |
980 "io", &ServiceRequestHandler, NULL); | 982 "io", &ServiceRequestHandler, NULL); |
981 | 983 |
982 // Call CreateIsolateAndSetup which creates an isolate and loads up | 984 // Call CreateIsolateAndSetup which creates an isolate and loads up |
983 // the specified application script. | 985 // the specified application script. |
984 char* error = NULL; | 986 char* error = NULL; |
985 bool is_compile_error = false; | 987 int exit_code = 0; |
986 char* isolate_name = BuildIsolateName(script_name, "main"); | 988 char* isolate_name = BuildIsolateName(script_name, "main"); |
987 Dart_Isolate isolate = CreateIsolateAndSetupHelper(script_name, | 989 Dart_Isolate isolate = CreateIsolateAndSetupHelper(script_name, |
988 "main", | 990 "main", |
989 commandline_package_root, | 991 commandline_package_root, |
990 &error, | 992 &error, |
991 &is_compile_error); | 993 &exit_code); |
992 if (isolate == NULL) { | 994 if (isolate == NULL) { |
993 Log::PrintErr("%s\n", error); | 995 Log::PrintErr("%s\n", error); |
994 free(error); | 996 free(error); |
995 delete [] isolate_name; | 997 delete [] isolate_name; |
996 exit(is_compile_error ? kCompilationErrorExitCode : kErrorExitCode); | 998 exit((exit_code != 0) ? exit_code : kErrorExitCode); |
997 } | 999 } |
998 delete [] isolate_name; | 1000 delete [] isolate_name; |
999 | 1001 |
1000 Dart_EnterIsolate(isolate); | 1002 Dart_EnterIsolate(isolate); |
1001 ASSERT(isolate == Dart_CurrentIsolate()); | 1003 ASSERT(isolate == Dart_CurrentIsolate()); |
1002 ASSERT(isolate != NULL); | 1004 ASSERT(isolate != NULL); |
1003 Dart_Handle result; | 1005 Dart_Handle result; |
1004 | 1006 |
1005 Dart_EnterScope(); | 1007 Dart_EnterScope(); |
1006 | 1008 |
(...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1117 exit(Process::GlobalExitCode()); | 1119 exit(Process::GlobalExitCode()); |
1118 } | 1120 } |
1119 | 1121 |
1120 } // namespace bin | 1122 } // namespace bin |
1121 } // namespace dart | 1123 } // namespace dart |
1122 | 1124 |
1123 int main(int argc, char** argv) { | 1125 int main(int argc, char** argv) { |
1124 dart::bin::main(argc, argv); | 1126 dart::bin::main(argc, argv); |
1125 UNREACHABLE(); | 1127 UNREACHABLE(); |
1126 } | 1128 } |
OLD | NEW |