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 827 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
838 result = Dart_StringToCString(result, &script_source); | 838 result = Dart_StringToCString(result, &script_source); |
839 if (Dart_IsError(result)) { | 839 if (Dart_IsError(result)) { |
840 return result; | 840 return result; |
841 } | 841 } |
842 Log::Print("%s\n", script_source); | 842 Log::Print("%s\n", script_source); |
843 } | 843 } |
844 return Dart_True(); | 844 return Dart_True(); |
845 } | 845 } |
846 | 846 |
847 | 847 |
848 static const char* ServiceRequestError(const char* message) { | |
849 TextBuffer buffer(128); | |
850 buffer.Printf("{\"type\":\"Error\",\"text\":\"%s\"}", message); | |
851 return buffer.Steal(); | |
852 } | |
853 | |
854 | |
855 static const char* ServiceRequestError(Dart_Handle error) { | 848 static const char* ServiceRequestError(Dart_Handle error) { |
856 TextBuffer buffer(128); | 849 TextBuffer buffer(128); |
857 buffer.Printf("{\"type\":\"Error\",\"text\":\"Internal error %s\"}", | 850 buffer.Printf("{\"type\":\"Error\",\"text\":\"Internal error %s\"}", |
858 Dart_GetError(error)); | 851 Dart_GetError(error)); |
859 return buffer.Steal(); | 852 return buffer.Steal(); |
860 } | 853 } |
861 | 854 |
862 | 855 |
863 class DartScope { | 856 class DartScope { |
864 public: | 857 public: |
865 DartScope() { Dart_EnterScope(); } | 858 DartScope() { Dart_EnterScope(); } |
866 ~DartScope() { Dart_ExitScope(); } | 859 ~DartScope() { Dart_ExitScope(); } |
867 }; | 860 }; |
868 | 861 |
869 | 862 |
870 static const char* ServiceRequestHandler( | 863 static const char* ServiceRequestHandler( |
871 const char* name, | 864 const char* name, |
872 const char** arguments, | 865 const char** arguments, |
873 intptr_t num_arguments, | 866 intptr_t num_arguments, |
874 const char** option_keys, | 867 const char** option_keys, |
875 const char** option_values, | 868 const char** option_values, |
876 intptr_t num_options, | 869 intptr_t num_options, |
877 void* user_data) { | 870 void* user_data) { |
878 DartScope scope; | 871 DartScope scope; |
879 const char* kSockets = "sockets"; | 872 ASSERT(num_arguments > 0); |
880 if (num_arguments == 2 && | 873 ASSERT(strncmp(arguments[0], "io", 2) == 0); |
881 strncmp(arguments[1], kSockets, strlen(kSockets)) == 0) { | 874 // TODO(ajohnsen): Store the library/function in isolate data or user_data. |
882 Dart_Handle dart_io_str = Dart_NewStringFromCString("dart:io"); | 875 Dart_Handle dart_io_str = Dart_NewStringFromCString("dart:io"); |
883 if (Dart_IsError(dart_io_str)) return ServiceRequestError(dart_io_str); | 876 if (Dart_IsError(dart_io_str)) return ServiceRequestError(dart_io_str); |
884 Dart_Handle io_lib = Dart_LookupLibrary(dart_io_str); | 877 Dart_Handle io_lib = Dart_LookupLibrary(dart_io_str); |
885 if (Dart_IsError(io_lib)) return ServiceRequestError(io_lib); | 878 if (Dart_IsError(io_lib)) return ServiceRequestError(io_lib); |
886 Dart_Handle handler_function_name = | 879 Dart_Handle handler_function_name = |
887 Dart_NewStringFromCString("_socketsStats"); | 880 Dart_NewStringFromCString("_serviceObjectHandler"); |
888 if (Dart_IsError(handler_function_name)) { | 881 if (Dart_IsError(handler_function_name)) { |
889 return ServiceRequestError(handler_function_name); | 882 return ServiceRequestError(handler_function_name); |
890 } | |
891 Dart_Handle result = Dart_Invoke(io_lib, handler_function_name, 0, NULL); | |
892 if (Dart_IsError(result)) return ServiceRequestError(result); | |
893 const char *json; | |
894 result = Dart_StringToCString(result, &json); | |
895 if (Dart_IsError(result)) return ServiceRequestError(result); | |
896 return strdup(json); | |
897 } else { | |
898 return ServiceRequestError("Unrecognized path"); | |
899 } | 883 } |
| 884 Dart_Handle paths = Dart_NewList(num_arguments - 1); |
| 885 for (int i = 0; i < num_arguments - 1; i++) { |
| 886 Dart_ListSetAt(paths, i, Dart_NewStringFromCString(arguments[i + 1])); |
| 887 } |
| 888 Dart_Handle keys = Dart_NewList(num_options); |
| 889 Dart_Handle values = Dart_NewList(num_options); |
| 890 for (int i = 0; i < num_options; i++) { |
| 891 Dart_ListSetAt(keys, i, Dart_NewStringFromCString(option_keys[i])); |
| 892 Dart_ListSetAt(values, i, Dart_NewStringFromCString(option_values[i])); |
| 893 } |
| 894 Dart_Handle args[] = {paths, keys, values}; |
| 895 Dart_Handle result = Dart_Invoke(io_lib, handler_function_name, 3, args); |
| 896 if (Dart_IsError(result)) return ServiceRequestError(result); |
| 897 const char *json; |
| 898 result = Dart_StringToCString(result, &json); |
| 899 if (Dart_IsError(result)) return ServiceRequestError(result); |
| 900 return strdup(json); |
900 } | 901 } |
901 | 902 |
902 | 903 |
903 void main(int argc, char** argv) { | 904 void main(int argc, char** argv) { |
904 char* script_name; | 905 char* script_name; |
905 CommandLineOptions vm_options(argc); | 906 CommandLineOptions vm_options(argc); |
906 CommandLineOptions dart_options(argc); | 907 CommandLineOptions dart_options(argc); |
907 bool print_flags_seen = false; | 908 bool print_flags_seen = false; |
908 bool verbose_debug_seen = false; | 909 bool verbose_debug_seen = false; |
909 | 910 |
(...skipping 225 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1135 exit(Process::GlobalExitCode()); | 1136 exit(Process::GlobalExitCode()); |
1136 } | 1137 } |
1137 | 1138 |
1138 } // namespace bin | 1139 } // namespace bin |
1139 } // namespace dart | 1140 } // namespace dart |
1140 | 1141 |
1141 int main(int argc, char** argv) { | 1142 int main(int argc, char** argv) { |
1142 dart::bin::main(argc, argv); | 1143 dart::bin::main(argc, argv); |
1143 UNREACHABLE(); | 1144 UNREACHABLE(); |
1144 } | 1145 } |
OLD | NEW |