Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(21)

Side by Side Diff: runtime/bin/main.cc

Issue 2041293003: Allow embedder service request handlers to return JSON-RPC errors. (Closed) Base URL: git@github.com:dart-lang/sdk.git@master
Patch Set: Created 4 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | runtime/include/dart_tools_api.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 917 matching lines...) Expand 10 before | Expand all | Expand 10 after
928 snprintf(buffer, len, kFormat, script_name, func_name); 928 snprintf(buffer, len, kFormat, script_name, func_name);
929 return buffer; 929 return buffer;
930 } 930 }
931 931
932 static void ShutdownIsolate(void* callback_data) { 932 static void ShutdownIsolate(void* callback_data) {
933 IsolateData* isolate_data = reinterpret_cast<IsolateData*>(callback_data); 933 IsolateData* isolate_data = reinterpret_cast<IsolateData*>(callback_data);
934 delete isolate_data; 934 delete isolate_data;
935 } 935 }
936 936
937 937
938 static const char* ServiceRequestError(Dart_Handle error) { 938 static const char* InternalJsonRpcError(Dart_Handle error) {
939 TextBuffer buffer(128); 939 TextBuffer buffer(128);
940 buffer.Printf("{\"type\":\"Error\",\"text\":\"Internal error %s\"}", 940 buffer.Printf("{\"code\":-32603,"
941 "\"message\":\"Internal error\","
942 "\"details\": \"%s\"}",
941 Dart_GetError(error)); 943 Dart_GetError(error));
942 return buffer.Steal(); 944 return buffer.Steal();
943 } 945 }
944 946
945 947
946 class DartScope { 948 class DartScope {
947 public: 949 public:
948 DartScope() { Dart_EnterScope(); } 950 DartScope() { Dart_EnterScope(); }
949 ~DartScope() { Dart_ExitScope(); } 951 ~DartScope() { Dart_ExitScope(); }
950 }; 952 };
951 953
952 954
953 static const char* ServiceGetIOHandler( 955 static bool ServiceGetIOHandler(
954 const char* method, 956 const char* method,
955 const char** param_keys, 957 const char** param_keys,
956 const char** param_values, 958 const char** param_values,
957 intptr_t num_params, 959 intptr_t num_params,
958 void* user_data) { 960 void* user_data,
961 const char** response) {
959 DartScope scope; 962 DartScope scope;
960 // TODO(ajohnsen): Store the library/function in isolate data or user_data. 963 // TODO(ajohnsen): Store the library/function in isolate data or user_data.
961 Dart_Handle dart_io_str = Dart_NewStringFromCString("dart:io"); 964 Dart_Handle dart_io_str = Dart_NewStringFromCString("dart:io");
962 if (Dart_IsError(dart_io_str)) { 965 if (Dart_IsError(dart_io_str)) {
963 return ServiceRequestError(dart_io_str); 966 *response = InternalJsonRpcError(dart_io_str);
967 return false;
964 } 968 }
965 969
966 Dart_Handle io_lib = Dart_LookupLibrary(dart_io_str); 970 Dart_Handle io_lib = Dart_LookupLibrary(dart_io_str);
967 if (Dart_IsError(io_lib)) { 971 if (Dart_IsError(io_lib)) {
968 return ServiceRequestError(io_lib); 972 *response = InternalJsonRpcError(io_lib);
973 return false;
969 } 974 }
970 975
971 Dart_Handle handler_function_name = 976 Dart_Handle handler_function_name =
972 Dart_NewStringFromCString("_serviceObjectHandler"); 977 Dart_NewStringFromCString("_serviceObjectHandler");
973 if (Dart_IsError(handler_function_name)) { 978 if (Dart_IsError(handler_function_name)) {
974 return ServiceRequestError(handler_function_name); 979 *response = InternalJsonRpcError(handler_function_name);
980 return false;
975 } 981 }
976 982
977 // TODO(johnmccutchan): paths is no longer used. Update the io 983 // TODO(johnmccutchan): paths is no longer used. Update the io
978 // _serviceObjectHandler function to use json rpc. 984 // _serviceObjectHandler function to use json rpc.
979 Dart_Handle paths = Dart_NewList(0); 985 Dart_Handle paths = Dart_NewList(0);
980 Dart_Handle keys = Dart_NewList(num_params); 986 Dart_Handle keys = Dart_NewList(num_params);
981 Dart_Handle values = Dart_NewList(num_params); 987 Dart_Handle values = Dart_NewList(num_params);
982 for (int i = 0; i < num_params; i++) { 988 for (int i = 0; i < num_params; i++) {
983 Dart_ListSetAt(keys, i, Dart_NewStringFromCString(param_keys[i])); 989 Dart_ListSetAt(keys, i, Dart_NewStringFromCString(param_keys[i]));
984 Dart_ListSetAt(values, i, Dart_NewStringFromCString(param_values[i])); 990 Dart_ListSetAt(values, i, Dart_NewStringFromCString(param_values[i]));
985 } 991 }
986 Dart_Handle args[] = {paths, keys, values}; 992 Dart_Handle args[] = {paths, keys, values};
987 Dart_Handle result = Dart_Invoke(io_lib, handler_function_name, 3, args); 993 Dart_Handle result = Dart_Invoke(io_lib, handler_function_name, 3, args);
988 if (Dart_IsError(result)) { 994 if (Dart_IsError(result)) {
989 return ServiceRequestError(result); 995 *response = InternalJsonRpcError(result);
996 return false;
990 } 997 }
991 998
992 const char *json; 999 const char *json;
993 result = Dart_StringToCString(result, &json); 1000 result = Dart_StringToCString(result, &json);
994 if (Dart_IsError(result)) { 1001 if (Dart_IsError(result)) {
995 return ServiceRequestError(result); 1002 *response = InternalJsonRpcError(result);
1003 return false;
996 } 1004 }
997 return strdup(json); 1005 *response = strdup(json);
1006 return true;
998 } 1007 }
999 1008
1000 1009
1001 static const char* kStdoutStreamId = "Stdout"; 1010 static const char* kStdoutStreamId = "Stdout";
1002 static const char* kStderrStreamId = "Stderr"; 1011 static const char* kStderrStreamId = "Stderr";
1003 1012
1004 1013
1005 static bool ServiceStreamListenCallback(const char* stream_id) { 1014 static bool ServiceStreamListenCallback(const char* stream_id) {
1006 if (strcmp(stream_id, kStdoutStreamId) == 0) { 1015 if (strcmp(stream_id, kStdoutStreamId) == 0) {
1007 SetCaptureStdout(true); 1016 SetCaptureStdout(true);
(...skipping 722 matching lines...) Expand 10 before | Expand all | Expand 10 after
1730 Platform::Exit(Process::GlobalExitCode()); 1739 Platform::Exit(Process::GlobalExitCode());
1731 } 1740 }
1732 1741
1733 } // namespace bin 1742 } // namespace bin
1734 } // namespace dart 1743 } // namespace dart
1735 1744
1736 int main(int argc, char** argv) { 1745 int main(int argc, char** argv) {
1737 dart::bin::main(argc, argv); 1746 dart::bin::main(argc, argv);
1738 UNREACHABLE(); 1747 UNREACHABLE();
1739 } 1748 }
OLDNEW
« no previous file with comments | « no previous file | runtime/include/dart_tools_api.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698