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 107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
118 static const int DEFAULT_VM_SERVICE_SERVER_PORT = 8181; | 118 static const int DEFAULT_VM_SERVICE_SERVER_PORT = 8181; |
119 // VM Service options. | 119 // VM Service options. |
120 static const char* vm_service_server_ip = DEFAULT_VM_SERVICE_SERVER_IP; | 120 static const char* vm_service_server_ip = DEFAULT_VM_SERVICE_SERVER_IP; |
121 // The 0 port is a magic value which results in the first available port | 121 // The 0 port is a magic value which results in the first available port |
122 // being allocated. | 122 // being allocated. |
123 static int vm_service_server_port = -1; | 123 static int vm_service_server_port = -1; |
124 // True when we are running in development mode and cross origin security | 124 // True when we are running in development mode and cross origin security |
125 // checks are disabled. | 125 // checks are disabled. |
126 static bool vm_service_dev_mode = false; | 126 static bool vm_service_dev_mode = false; |
127 | 127 |
| 128 // Exit code indicating an internal Dart Frontend error. |
| 129 static const int kDartFrontendErrorExitCode = 252; |
128 // Exit code indicating an API error. | 130 // Exit code indicating an API error. |
129 static const int kApiErrorExitCode = 253; | 131 static const int kApiErrorExitCode = 253; |
130 // Exit code indicating a compilation error. | 132 // Exit code indicating a compilation error. |
131 static const int kCompilationErrorExitCode = 254; | 133 static const int kCompilationErrorExitCode = 254; |
132 // Exit code indicating an unhandled error that is not a compilation error. | 134 // Exit code indicating an unhandled error that is not a compilation error. |
133 static const int kErrorExitCode = 255; | 135 static const int kErrorExitCode = 255; |
134 // Exit code indicating a vm restart request. Never returned to the user. | 136 // Exit code indicating a vm restart request. Never returned to the user. |
135 static const int kRestartRequestExitCode = 1000; | 137 static const int kRestartRequestExitCode = 1000; |
136 | 138 |
137 static void ErrorExit(int exit_code, const char* format, ...) { | 139 static void ErrorExit(int exit_code, const char* format, ...) { |
(...skipping 664 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
802 } \ | 804 } \ |
803 Dart_ExitScope(); \ | 805 Dart_ExitScope(); \ |
804 Dart_ShutdownIsolate(); \ | 806 Dart_ShutdownIsolate(); \ |
805 return NULL; \ | 807 return NULL; \ |
806 } | 808 } |
807 | 809 |
808 | 810 |
809 static void SnapshotOnExitHook(int64_t exit_code); | 811 static void SnapshotOnExitHook(int64_t exit_code); |
810 | 812 |
811 | 813 |
812 // Returns true on success, false on failure. | 814 // Returns newly created Isolate on success, NULL on failure. |
813 static Dart_Isolate CreateIsolateAndSetupHelper(bool is_main_isolate, | 815 static Dart_Isolate CreateIsolateAndSetupHelper(bool is_main_isolate, |
814 const char* script_uri, | 816 const char* script_uri, |
815 const char* main, | 817 const char* main, |
816 const char* package_root, | 818 const char* package_root, |
817 const char* packages_config, | 819 const char* packages_config, |
818 Dart_IsolateFlags* flags, | 820 Dart_IsolateFlags* flags, |
819 char** error, | 821 char** error, |
820 int* exit_code) { | 822 int* exit_code) { |
821 ASSERT(script_uri != NULL); | 823 ASSERT(script_uri != NULL); |
822 if (strcmp(script_uri, DART_KERNEL_ISOLATE_NAME) == 0) { | 824 const bool is_service_isolate = |
| 825 strcmp(script_uri, DART_VM_SERVICE_ISOLATE_NAME) == 0; |
| 826 const bool is_kernel_isolate = |
| 827 strcmp(script_uri, DART_KERNEL_ISOLATE_NAME) == 0; |
| 828 if (is_kernel_isolate) { |
823 if (!use_dart_frontend) { | 829 if (!use_dart_frontend) { |
824 *error = strdup("Kernel isolate not supported."); | 830 *error = strdup("Kernel isolate not supported."); |
825 return NULL; | 831 return NULL; |
826 } else { | 832 } else { |
827 if (packages_config == NULL) { | 833 if (packages_config == NULL) { |
828 packages_config = commandline_packages_file; | 834 packages_config = commandline_packages_file; |
829 } | 835 } |
830 } | 836 } |
831 } | 837 } |
832 | 838 |
(...skipping 12 matching lines...) Expand all Loading... |
845 core_isolate_snapshot_instructions; | 851 core_isolate_snapshot_instructions; |
846 if ((app_isolate_snapshot_data != NULL) && | 852 if ((app_isolate_snapshot_data != NULL) && |
847 (is_main_isolate || ((app_script_uri != NULL) && | 853 (is_main_isolate || ((app_script_uri != NULL) && |
848 (strcmp(script_uri, app_script_uri) == 0)))) { | 854 (strcmp(script_uri, app_script_uri) == 0)))) { |
849 isolate_run_app_snapshot = true; | 855 isolate_run_app_snapshot = true; |
850 isolate_snapshot_data = app_isolate_snapshot_data; | 856 isolate_snapshot_data = app_isolate_snapshot_data; |
851 isolate_snapshot_instructions = app_isolate_snapshot_instructions; | 857 isolate_snapshot_instructions = app_isolate_snapshot_instructions; |
852 } | 858 } |
853 #endif | 859 #endif |
854 | 860 |
| 861 const uint8_t* kernel_file = NULL; |
| 862 intptr_t kernel_length = -1; |
| 863 bool is_kernel = false; |
| 864 |
| 865 if (use_dart_frontend && !is_kernel_isolate && !is_service_isolate) { |
| 866 Dart_KernelCompilationResult result = Dart_CompileToKernel(script_uri); |
| 867 *error = result.error; // Copy error message (if any). |
| 868 switch (result.status) { |
| 869 case Dart_KernelCompilationStatus_Ok: |
| 870 is_kernel = true; |
| 871 kernel_file = result.kernel; |
| 872 kernel_length = result.kernel_size; |
| 873 break; |
| 874 case Dart_KernelCompilationStatus_Error: |
| 875 *exit_code = kCompilationErrorExitCode; |
| 876 return NULL; |
| 877 case Dart_KernelCompilationStatus_Crash: |
| 878 *exit_code = kDartFrontendErrorExitCode; |
| 879 return NULL; |
| 880 case Dart_KernelCompilationStatus_Unknown: |
| 881 *exit_code = kErrorExitCode; |
| 882 return NULL; |
| 883 } |
| 884 } |
| 885 |
855 // If the script is a Kernel binary, then we will try to bootstrap from the | 886 // If the script is a Kernel binary, then we will try to bootstrap from the |
856 // script. | 887 // script. |
857 const uint8_t* kernel_file = NULL; | 888 if (!is_kernel && !isolate_run_app_snapshot) { |
858 intptr_t kernel_length = -1; | 889 is_kernel = TryReadKernel(script_uri, &kernel_file, &kernel_length); |
859 const bool is_kernel = | 890 } |
860 !isolate_run_app_snapshot && | |
861 TryReadKernel(script_uri, &kernel_file, &kernel_length); | |
862 | 891 |
863 void* kernel_program = NULL; | 892 void* kernel_program = NULL; |
864 if (is_kernel) { | 893 if (is_kernel) { |
865 kernel_program = Dart_ReadKernelBinary(kernel_file, kernel_length); | 894 kernel_program = Dart_ReadKernelBinary(kernel_file, kernel_length); |
866 free(const_cast<uint8_t*>(kernel_file)); | 895 free(const_cast<uint8_t*>(kernel_file)); |
867 } | 896 } |
868 | 897 |
869 IsolateData* isolate_data = | 898 IsolateData* isolate_data = |
870 new IsolateData(script_uri, package_root, packages_config); | 899 new IsolateData(script_uri, package_root, packages_config); |
871 Dart_Isolate isolate = | 900 Dart_Isolate isolate = |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
933 script_uri = frontend_filename; | 962 script_uri = frontend_filename; |
934 } | 963 } |
935 | 964 |
936 // Setup package root if specified. | 965 // Setup package root if specified. |
937 result = DartUtils::SetupPackageRoot(package_root, packages_config); | 966 result = DartUtils::SetupPackageRoot(package_root, packages_config); |
938 CHECK_RESULT(result); | 967 CHECK_RESULT(result); |
939 | 968 |
940 result = Dart_SetEnvironmentCallback(EnvironmentCallback); | 969 result = Dart_SetEnvironmentCallback(EnvironmentCallback); |
941 CHECK_RESULT(result); | 970 CHECK_RESULT(result); |
942 | 971 |
943 if (!Dart_IsKernelIsolate(isolate) && use_dart_frontend) { | |
944 // This must be the main script to be loaded. Wait for Kernel isolate | |
945 // to finish initialization. | |
946 Dart_Port port = Dart_ServiceWaitForKernelPort(); | |
947 if (port == ILLEGAL_PORT) { | |
948 *error = strdup("Error while initializing Kernel isolate"); | |
949 return NULL; | |
950 } | |
951 } | |
952 | |
953 if (isolate_run_app_snapshot) { | 972 if (isolate_run_app_snapshot) { |
954 result = DartUtils::SetupIOLibrary(script_uri); | 973 result = DartUtils::SetupIOLibrary(script_uri); |
955 CHECK_RESULT(result); | 974 CHECK_RESULT(result); |
956 Loader::InitForSnapshot(script_uri); | 975 Loader::InitForSnapshot(script_uri); |
957 #if !defined(DART_PRECOMPILED_RUNTIME) | 976 #if !defined(DART_PRECOMPILED_RUNTIME) |
958 if (is_main_isolate) { | 977 if (is_main_isolate) { |
959 // Find the canonical uri of the app snapshot. We'll use this to decide if | 978 // Find the canonical uri of the app snapshot. We'll use this to decide if |
960 // other isolates should use the app snapshot or the core snapshot. | 979 // other isolates should use the app snapshot or the core snapshot. |
961 const char* resolved_script_uri = NULL; | 980 const char* resolved_script_uri = NULL; |
962 result = Dart_StringToCString( | 981 result = Dart_StringToCString( |
963 DartUtils::ResolveScript(Dart_NewStringFromCString(script_uri)), | 982 DartUtils::ResolveScript(Dart_NewStringFromCString(script_uri)), |
964 &resolved_script_uri); | 983 &resolved_script_uri); |
965 CHECK_RESULT(result); | 984 CHECK_RESULT(result); |
966 ASSERT(app_script_uri == NULL); | 985 ASSERT(app_script_uri == NULL); |
967 app_script_uri = strdup(resolved_script_uri); | 986 app_script_uri = strdup(resolved_script_uri); |
968 } | 987 } |
969 #endif // !defined(DART_PRECOMPILED_RUNTIME) | 988 #endif // !defined(DART_PRECOMPILED_RUNTIME) |
970 } else { | 989 } else { |
971 // Load the specified application script into the newly created isolate. | 990 // Load the specified application script into the newly created isolate. |
972 Dart_Handle uri = | 991 Dart_Handle uri = |
973 DartUtils::ResolveScript(Dart_NewStringFromCString(script_uri)); | 992 DartUtils::ResolveScript(Dart_NewStringFromCString(script_uri)); |
974 CHECK_RESULT(uri); | 993 CHECK_RESULT(uri); |
975 if (!is_kernel) { | 994 if (!is_kernel) { |
976 result = Loader::LibraryTagHandler(Dart_kScriptTag, Dart_Null(), uri); | 995 result = Loader::LibraryTagHandler(Dart_kScriptTag, Dart_Null(), uri); |
977 CHECK_RESULT(result); | 996 CHECK_RESULT(result); |
| 997 } else { |
| 998 // Various core-library parts will send requests to the Loader to resolve |
| 999 // relative URIs and perform other related tasks. We need Loader to be |
| 1000 // initialized for this to work because loading from Kernel binary |
| 1001 // bypasses normal source code loading paths that initialize it. |
| 1002 Loader::InitForSnapshot(script_uri); |
978 } | 1003 } |
979 | 1004 |
980 Dart_TimelineEvent("LoadScript", Dart_TimelineGetMicros(), | 1005 Dart_TimelineEvent("LoadScript", Dart_TimelineGetMicros(), |
981 Dart_GetMainPortId(), Dart_Timeline_Event_Async_End, 0, | 1006 Dart_GetMainPortId(), Dart_Timeline_Event_Async_End, 0, |
982 NULL, NULL); | 1007 NULL, NULL); |
983 | 1008 |
984 result = DartUtils::SetupIOLibrary(script_uri); | 1009 result = DartUtils::SetupIOLibrary(script_uri); |
985 CHECK_RESULT(result); | 1010 CHECK_RESULT(result); |
986 } | 1011 } |
987 | 1012 |
(...skipping 1074 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2062 Platform::Exit(Process::GlobalExitCode()); | 2087 Platform::Exit(Process::GlobalExitCode()); |
2063 } | 2088 } |
2064 | 2089 |
2065 } // namespace bin | 2090 } // namespace bin |
2066 } // namespace dart | 2091 } // namespace dart |
2067 | 2092 |
2068 int main(int argc, char** argv) { | 2093 int main(int argc, char** argv) { |
2069 dart::bin::main(argc, argv); | 2094 dart::bin::main(argc, argv); |
2070 UNREACHABLE(); | 2095 UNREACHABLE(); |
2071 } | 2096 } |
OLD | NEW |