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 1138 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1149 snprintf(buffer, len, kFormat, script_name, func_name); | 1149 snprintf(buffer, len, kFormat, script_name, func_name); |
1150 return buffer; | 1150 return buffer; |
1151 } | 1151 } |
1152 | 1152 |
1153 static void ShutdownIsolate(void* callback_data) { | 1153 static void ShutdownIsolate(void* callback_data) { |
1154 IsolateData* isolate_data = reinterpret_cast<IsolateData*>(callback_data); | 1154 IsolateData* isolate_data = reinterpret_cast<IsolateData*>(callback_data); |
1155 delete isolate_data; | 1155 delete isolate_data; |
1156 } | 1156 } |
1157 | 1157 |
1158 | 1158 |
1159 static const char* InternalJsonRpcError(Dart_Handle error) { | |
1160 TextBuffer buffer(128); | |
1161 buffer.Printf( | |
1162 "{\"code\":-32603," | |
1163 "\"message\":\"Internal error\"," | |
1164 "\"details\": \"%s\"}", | |
1165 Dart_GetError(error)); | |
1166 return buffer.Steal(); | |
1167 } | |
1168 | |
1169 | |
1170 class DartScope { | |
1171 public: | |
1172 DartScope() { Dart_EnterScope(); } | |
1173 ~DartScope() { Dart_ExitScope(); } | |
1174 }; | |
1175 | |
1176 | |
1177 static bool ServiceGetIOHandler(const char* method, | |
1178 const char** param_keys, | |
1179 const char** param_values, | |
1180 intptr_t num_params, | |
1181 void* user_data, | |
1182 const char** response) { | |
1183 DartScope scope; | |
1184 // TODO(ajohnsen): Store the library/function in isolate data or user_data. | |
1185 Dart_Handle dart_io_str = Dart_NewStringFromCString("dart:io"); | |
1186 if (Dart_IsError(dart_io_str)) { | |
1187 *response = InternalJsonRpcError(dart_io_str); | |
1188 return false; | |
1189 } | |
1190 | |
1191 Dart_Handle io_lib = Dart_LookupLibrary(dart_io_str); | |
1192 if (Dart_IsError(io_lib)) { | |
1193 *response = InternalJsonRpcError(io_lib); | |
1194 return false; | |
1195 } | |
1196 | |
1197 Dart_Handle handler_function_name = | |
1198 Dart_NewStringFromCString("_serviceObjectHandler"); | |
1199 if (Dart_IsError(handler_function_name)) { | |
1200 *response = InternalJsonRpcError(handler_function_name); | |
1201 return false; | |
1202 } | |
1203 | |
1204 // TODO(johnmccutchan): paths is no longer used. Update the io | |
1205 // _serviceObjectHandler function to use json rpc. | |
1206 Dart_Handle paths = Dart_NewList(0); | |
1207 Dart_Handle keys = Dart_NewList(num_params); | |
1208 Dart_Handle values = Dart_NewList(num_params); | |
1209 for (int i = 0; i < num_params; i++) { | |
1210 Dart_ListSetAt(keys, i, Dart_NewStringFromCString(param_keys[i])); | |
1211 Dart_ListSetAt(values, i, Dart_NewStringFromCString(param_values[i])); | |
1212 } | |
1213 Dart_Handle args[] = {paths, keys, values}; | |
1214 Dart_Handle result = Dart_Invoke(io_lib, handler_function_name, 3, args); | |
1215 if (Dart_IsError(result)) { | |
1216 *response = InternalJsonRpcError(result); | |
1217 return false; | |
1218 } | |
1219 | |
1220 const char* json; | |
1221 result = Dart_StringToCString(result, &json); | |
1222 if (Dart_IsError(result)) { | |
1223 *response = InternalJsonRpcError(result); | |
1224 return false; | |
1225 } | |
1226 *response = strdup(json); | |
1227 return true; | |
1228 } | |
1229 | |
1230 | |
1231 static const char* kStdoutStreamId = "Stdout"; | 1159 static const char* kStdoutStreamId = "Stdout"; |
1232 static const char* kStderrStreamId = "Stderr"; | 1160 static const char* kStderrStreamId = "Stderr"; |
1233 | 1161 |
1234 | 1162 |
1235 static bool ServiceStreamListenCallback(const char* stream_id) { | 1163 static bool ServiceStreamListenCallback(const char* stream_id) { |
1236 if (strcmp(stream_id, kStdoutStreamId) == 0) { | 1164 if (strcmp(stream_id, kStdoutStreamId) == 0) { |
1237 SetCaptureStdout(true); | 1165 SetCaptureStdout(true); |
1238 return true; | 1166 return true; |
1239 } else if (strcmp(stream_id, kStderrStreamId) == 0) { | 1167 } else if (strcmp(stream_id, kStderrStreamId) == 0) { |
1240 SetCaptureStderr(true); | 1168 SetCaptureStderr(true); |
(...skipping 453 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1694 init_params.get_service_assets = GetVMServiceAssetsArchiveCallback; | 1622 init_params.get_service_assets = GetVMServiceAssetsArchiveCallback; |
1695 | 1623 |
1696 char* error = Dart_Initialize(&init_params); | 1624 char* error = Dart_Initialize(&init_params); |
1697 if (error != NULL) { | 1625 if (error != NULL) { |
1698 EventHandler::Stop(); | 1626 EventHandler::Stop(); |
1699 Log::PrintErr("VM initialization failed: %s\n", error); | 1627 Log::PrintErr("VM initialization failed: %s\n", error); |
1700 free(error); | 1628 free(error); |
1701 Platform::Exit(kErrorExitCode); | 1629 Platform::Exit(kErrorExitCode); |
1702 } | 1630 } |
1703 | 1631 |
1704 Dart_RegisterIsolateServiceRequestCallback("getIO", &ServiceGetIOHandler, | |
1705 NULL); | |
1706 Dart_SetServiceStreamCallbacks(&ServiceStreamListenCallback, | 1632 Dart_SetServiceStreamCallbacks(&ServiceStreamListenCallback, |
1707 &ServiceStreamCancelCallback); | 1633 &ServiceStreamCancelCallback); |
1708 Dart_SetFileModifiedCallback(&FileModifiedCallback); | 1634 Dart_SetFileModifiedCallback(&FileModifiedCallback); |
1709 | 1635 |
1710 // Run the main isolate until we aren't told to restart. | 1636 // Run the main isolate until we aren't told to restart. |
1711 while (RunMainIsolate(script_name, &dart_options)) { | 1637 while (RunMainIsolate(script_name, &dart_options)) { |
1712 Log::PrintErr("Restarting VM\n"); | 1638 Log::PrintErr("Restarting VM\n"); |
1713 } | 1639 } |
1714 | 1640 |
1715 // Terminate process exit-code handler. | 1641 // Terminate process exit-code handler. |
(...skipping 29 matching lines...) Expand all Loading... |
1745 Platform::Exit(Process::GlobalExitCode()); | 1671 Platform::Exit(Process::GlobalExitCode()); |
1746 } | 1672 } |
1747 | 1673 |
1748 } // namespace bin | 1674 } // namespace bin |
1749 } // namespace dart | 1675 } // namespace dart |
1750 | 1676 |
1751 int main(int argc, char** argv) { | 1677 int main(int argc, char** argv) { |
1752 dart::bin::main(argc, argv); | 1678 dart::bin::main(argc, argv); |
1753 UNREACHABLE(); | 1679 UNREACHABLE(); |
1754 } | 1680 } |
OLD | NEW |