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 879 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
890 } | 890 } |
891 | 891 |
892 // Write the magic number to indicate file is a script snapshot. | 892 // Write the magic number to indicate file is a script snapshot. |
893 DartUtils::WriteMagicNumber(snapshot_file); | 893 DartUtils::WriteMagicNumber(snapshot_file); |
894 | 894 |
895 // Now write the snapshot out to specified file. | 895 // Now write the snapshot out to specified file. |
896 bool bytes_written = snapshot_file->WriteFully(buffer, size); | 896 bool bytes_written = snapshot_file->WriteFully(buffer, size); |
897 ASSERT(bytes_written); | 897 ASSERT(bytes_written); |
898 delete snapshot_file; | 898 delete snapshot_file; |
899 } else { | 899 } else { |
| 900 // Lookup the library of the root script. |
| 901 Dart_Handle root_lib = Dart_RootLibrary(); |
| 902 // Import the root library into the builtin library so that we can easily |
| 903 // lookup the main entry point exported from the root library. |
| 904 Dart_Handle builtin_lib = |
| 905 Builtin::LoadAndCheckLibrary(Builtin::kBuiltinLibrary); |
| 906 result = Dart_LibraryImportLibrary(builtin_lib, root_lib, Dart_Null()); |
| 907 |
900 if (has_compile_all) { | 908 if (has_compile_all) { |
901 result = Dart_CompileAll(); | 909 result = Dart_CompileAll(); |
902 if (Dart_IsError(result)) { | 910 if (Dart_IsError(result)) { |
903 return DartErrorExit(result); | 911 return DartErrorExit(result); |
904 } | 912 } |
905 } | 913 } |
906 | 914 |
907 if (has_check_function_fingerprints) { | 915 if (has_check_function_fingerprints) { |
908 result = Dart_CheckFunctionFingerprints(); | 916 result = Dart_CheckFunctionFingerprints(); |
909 if (Dart_IsError(result)) { | 917 if (Dart_IsError(result)) { |
910 return DartErrorExit(result); | 918 return DartErrorExit(result); |
911 } | 919 } |
912 } | 920 } |
913 | 921 |
914 // Lookup the library of the root script. | 922 if (Dart_IsNull(root_lib)) { |
915 Dart_Handle library = Dart_RootLibrary(); | |
916 if (Dart_IsNull(library)) { | |
917 return ErrorExit(kErrorExitCode, | 923 return ErrorExit(kErrorExitCode, |
918 "Unable to find root library for '%s'\n", | 924 "Unable to find root library for '%s'\n", |
919 script_name); | 925 script_name); |
920 } | 926 } |
921 // Set debug breakpoint if specified on the command line. | |
922 if (breakpoint_at != NULL) { | |
923 result = SetBreakpoint(breakpoint_at, library); | |
924 if (Dart_IsError(result)) { | |
925 return ErrorExit(kErrorExitCode, | |
926 "Error setting breakpoint at '%s': %s\n", | |
927 breakpoint_at, | |
928 Dart_GetError(result)); | |
929 } | |
930 } | |
931 if (has_print_script) { | 927 if (has_print_script) { |
932 result = GenerateScriptSource(); | 928 result = GenerateScriptSource(); |
933 if (Dart_IsError(result)) { | 929 if (Dart_IsError(result)) { |
934 return DartErrorExit(result); | 930 return DartErrorExit(result); |
935 } | 931 } |
936 } else { | 932 } else { |
937 // Lookup and invoke the top level main function. | 933 // The helper function _getMainClosure creates a closure for the main |
938 // The top-level function may accept up to two arguments: | 934 // entry point which is either explicitly or implictly exported from the |
939 // main(List<String> args, var message). | 935 // root library. |
940 // However most commonly it either accepts one (the args list) or | 936 Dart_Handle main_closure = Dart_Invoke( |
941 // none. | 937 builtin_lib, Dart_NewStringFromCString("_getMainClosure"), 0, NULL); |
942 // If the message is optional, main(args, [message]), it is invoked with | 938 if (Dart_IsError(main_closure)) { |
943 // one argument only. | 939 return DartErrorExit(result); |
944 Dart_Handle main_args[2]; | 940 } |
945 main_args[0] = CreateRuntimeOptions(&dart_options); | 941 |
946 main_args[1] = Dart_Null(); | 942 // Set debug breakpoint if specified on the command line before calling |
947 // First try with 1 argument. | 943 // the main function. |
948 result = Dart_Invoke(library, DartUtils::NewString("main"), 1, main_args); | 944 if (breakpoint_at != NULL) { |
949 // TODO(iposva): Return a special error type for mismatched argument | 945 result = SetBreakpoint(breakpoint_at, root_lib); |
950 // counts from Dart_Invoke to avoid the string comparison. | 946 if (Dart_IsError(result)) { |
951 const char* expected_error = "Dart_Invoke: wrong argument count for " | 947 return ErrorExit(kErrorExitCode, |
952 "function 'main': "; | 948 "Error setting breakpoint at '%s': %s\n", |
953 intptr_t length = strlen(expected_error); | 949 breakpoint_at, |
954 if (Dart_IsError(result) && | 950 Dart_GetError(result)); |
955 strncmp(expected_error, Dart_GetError(result), length) == 0) { | |
956 // Try with two arguments. | |
957 result = | |
958 Dart_Invoke(library, DartUtils::NewString("main"), 2, main_args); | |
959 if (Dart_IsError(result) && | |
960 strncmp(expected_error, Dart_GetError(result), length) == 0) { | |
961 // Finally try with 0 arguments. | |
962 result = Dart_Invoke(library, DartUtils::NewString("main"), 0, NULL); | |
963 } | 951 } |
964 } | 952 } |
| 953 |
| 954 // Call _startIsolate in the isolate library to enable dispatching the |
| 955 // initial startup message. |
| 956 Dart_Handle isolate_args[2]; |
| 957 isolate_args[0] = main_closure; |
| 958 isolate_args[1] = Dart_True(); |
| 959 |
| 960 Dart_Handle isolate_lib = Dart_LookupLibrary( |
| 961 Dart_NewStringFromCString("dart:isolate")); |
| 962 result = Dart_Invoke(isolate_lib, |
| 963 Dart_NewStringFromCString("_startIsolate"), |
| 964 2, isolate_args); |
| 965 |
| 966 // Setup the arguments in the initial startup message and leave the |
| 967 // replyTo and message fields empty. |
| 968 Dart_Handle initial_startup_msg = Dart_NewList(3); |
| 969 result = Dart_ListSetAt(initial_startup_msg, 1, |
| 970 CreateRuntimeOptions(&dart_options)); |
965 if (Dart_IsError(result)) { | 971 if (Dart_IsError(result)) { |
966 return DartErrorExit(result); | 972 return DartErrorExit(result); |
967 } | 973 } |
| 974 Dart_Port main_port = Dart_GetMainPortId(); |
| 975 bool posted = Dart_Post(main_port, initial_startup_msg); |
| 976 if (!posted) { |
| 977 return ErrorExit(kErrorExitCode, |
| 978 "Failed posting startup message to main " |
| 979 "isolate control port."); |
| 980 } |
968 | 981 |
969 // Keep handling messages until the last active receive port is closed. | 982 // Keep handling messages until the last active receive port is closed. |
970 result = Dart_RunLoop(); | 983 result = Dart_RunLoop(); |
971 if (Dart_IsError(result)) { | 984 if (Dart_IsError(result)) { |
972 return DartErrorExit(result); | 985 return DartErrorExit(result); |
973 } | 986 } |
974 } | 987 } |
975 } | 988 } |
976 | 989 |
977 Dart_ExitScope(); | 990 Dart_ExitScope(); |
(...skipping 22 matching lines...) Expand all Loading... |
1000 | 1013 |
1001 return Process::GlobalExitCode(); | 1014 return Process::GlobalExitCode(); |
1002 } | 1015 } |
1003 | 1016 |
1004 } // namespace bin | 1017 } // namespace bin |
1005 } // namespace dart | 1018 } // namespace dart |
1006 | 1019 |
1007 int main(int argc, char** argv) { | 1020 int main(int argc, char** argv) { |
1008 return dart::bin::main(argc, argv); | 1021 return dart::bin::main(argc, argv); |
1009 } | 1022 } |
OLD | NEW |