| OLD | NEW |
| 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, 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 "bin/vmservice_impl.h" | 5 #include "bin/vmservice_impl.h" |
| 6 | 6 |
| 7 #include "include/dart_api.h" | 7 #include "include/dart_api.h" |
| 8 | 8 |
| 9 #include "bin/builtin.h" | 9 #include "bin/builtin.h" |
| 10 #include "bin/dartutils.h" | 10 #include "bin/dartutils.h" |
| (...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 122 (num_arguments == entry.num_arguments)) { | 122 (num_arguments == entry.num_arguments)) { |
| 123 return entry.function; | 123 return entry.function; |
| 124 } | 124 } |
| 125 } | 125 } |
| 126 return NULL; | 126 return NULL; |
| 127 } | 127 } |
| 128 | 128 |
| 129 | 129 |
| 130 const char* VmService::error_msg_ = NULL; | 130 const char* VmService::error_msg_ = NULL; |
| 131 | 131 |
| 132 bool VmService::Start(const char *server_ip, intptr_t server_port) { | |
| 133 bool r = _Start(server_ip, server_port); | |
| 134 if (!r) { | |
| 135 return r; | |
| 136 } | |
| 137 // Start processing messages in a new thread. | |
| 138 Thread::Start(ThreadMain, static_cast<uword>(NULL)); | |
| 139 return true; | |
| 140 } | |
| 141 | 132 |
| 133 bool VmService::Setup(const char* server_ip, intptr_t server_port) { |
| 134 Dart_Isolate isolate = Dart_CurrentIsolate(); |
| 135 ASSERT(isolate != NULL); |
| 142 | 136 |
| 143 bool VmService::_Start(const char *server_ip, intptr_t server_port) { | 137 Dart_Handle result; |
| 144 ASSERT(Dart_CurrentIsolate() == NULL); | 138 |
| 145 Dart_Isolate isolate = Dart_GetServiceIsolate(NULL); | 139 // Load main script. |
| 146 if (isolate == NULL) { | |
| 147 error_msg_ = "Dart_GetServiceIsolate failed."; | |
| 148 return false; | |
| 149 } | |
| 150 Dart_EnterIsolate(isolate); | |
| 151 Dart_EnterScope(); | |
| 152 // Install our own library tag handler. | |
| 153 Dart_SetLibraryTagHandler(LibraryTagHandler); | 140 Dart_SetLibraryTagHandler(LibraryTagHandler); |
| 154 Dart_Handle result; | 141 Dart_Handle library = LoadScript(kVMServiceIOLibraryScriptResourceName); |
| 155 Dart_Handle library; | |
| 156 library = LoadScript(kVMServiceIOLibraryScriptResourceName); | |
| 157 // Expect a library. | |
| 158 ASSERT(library != Dart_Null()); | 142 ASSERT(library != Dart_Null()); |
| 159 SHUTDOWN_ON_ERROR(library); | 143 SHUTDOWN_ON_ERROR(library); |
| 144 result = Dart_SetNativeResolver(library, VmServiceIONativeResolver, NULL); |
| 145 SHUTDOWN_ON_ERROR(result); |
| 160 result = Dart_FinalizeLoading(false); | 146 result = Dart_FinalizeLoading(false); |
| 161 ASSERT(!Dart_IsError(result)); | 147 SHUTDOWN_ON_ERROR(result); |
| 148 |
| 149 // Make runnable. |
| 162 Dart_ExitScope(); | 150 Dart_ExitScope(); |
| 163 Dart_ExitIsolate(); | 151 Dart_ExitIsolate(); |
| 164 bool retval = Dart_IsolateMakeRunnable(isolate); | 152 bool retval = Dart_IsolateMakeRunnable(isolate); |
| 165 if (!retval) { | 153 if (!retval) { |
| 166 Dart_EnterIsolate(isolate); | 154 Dart_EnterIsolate(isolate); |
| 167 Dart_ShutdownIsolate(); | 155 Dart_ShutdownIsolate(); |
| 168 error_msg_ = "Invalid isolate state - Unable to make it runnable."; | 156 error_msg_ = "Invalid isolate state - Unable to make it runnable."; |
| 169 return false; | 157 return false; |
| 170 } | 158 } |
| 171 | |
| 172 Dart_EnterIsolate(isolate); | 159 Dart_EnterIsolate(isolate); |
| 173 Dart_EnterScope(); | 160 Dart_EnterScope(); |
| 161 |
| 174 library = Dart_RootLibrary(); | 162 library = Dart_RootLibrary(); |
| 175 result = Dart_SetNativeResolver(library, VmServiceIONativeResolver, NULL); | 163 SHUTDOWN_ON_ERROR(library); |
| 176 ASSERT(!Dart_IsError(result)); | 164 |
| 177 // Set requested TCP port. | 165 // Set HTTP server state. |
| 178 DartUtils::SetStringField(library, "_ip", server_ip); | 166 DartUtils::SetStringField(library, "_ip", server_ip); |
| 179 // If we have a port specified, start the server immediately. | 167 // If we have a port specified, start the server immediately. |
| 180 bool auto_start = server_port >= 0; | 168 bool auto_start = server_port >= 0; |
| 181 if (server_port < 0) { | 169 if (server_port < 0) { |
| 182 // Adjust server_port to port 0 which will result in the first available | 170 // Adjust server_port to port 0 which will result in the first available |
| 183 // port when the HTTP server is started. | 171 // port when the HTTP server is started. |
| 184 server_port = 0; | 172 server_port = 0; |
| 185 } | 173 } |
| 186 // Set initial state. | |
| 187 DartUtils::SetIntegerField(library, "_port", server_port); | 174 DartUtils::SetIntegerField(library, "_port", server_port); |
| 188 Dart_SetField(library, | 175 result = Dart_SetField(library, |
| 189 DartUtils::NewString("_autoStart"), | 176 DartUtils::NewString("_autoStart"), |
| 190 Dart_NewBoolean(auto_start)); | 177 Dart_NewBoolean(auto_start)); |
| 191 // We cannot register for signals on windows. | 178 SHUTDOWN_ON_ERROR(result); |
| 179 |
| 180 // Are we running on Windows? |
| 192 #if defined(TARGET_OS_WINDOWS) | 181 #if defined(TARGET_OS_WINDOWS) |
| 193 Dart_Handle is_windows = Dart_True(); | 182 Dart_Handle is_windows = Dart_True(); |
| 194 #else | 183 #else |
| 195 Dart_Handle is_windows = Dart_False(); | 184 Dart_Handle is_windows = Dart_False(); |
| 196 #endif | 185 #endif |
| 197 Dart_SetField(library, DartUtils::NewString("_isWindows"), is_windows); | 186 result = |
| 198 | 187 Dart_SetField(library, DartUtils::NewString("_isWindows"), is_windows); |
| 188 SHUTDOWN_ON_ERROR(result); |
| 199 | 189 |
| 200 // Get _getWatchSignalInternal from dart:io. | 190 // Get _getWatchSignalInternal from dart:io. |
| 201 Dart_Handle dart_io_str = Dart_NewStringFromCString(DartUtils::kIOLibURL); | 191 Dart_Handle dart_io_str = Dart_NewStringFromCString(DartUtils::kIOLibURL); |
| 192 SHUTDOWN_ON_ERROR(dart_io_str); |
| 202 Dart_Handle io_lib = Dart_LookupLibrary(dart_io_str); | 193 Dart_Handle io_lib = Dart_LookupLibrary(dart_io_str); |
| 194 SHUTDOWN_ON_ERROR(io_lib); |
| 203 Dart_Handle function_name = | 195 Dart_Handle function_name = |
| 204 Dart_NewStringFromCString("_getWatchSignalInternal"); | 196 Dart_NewStringFromCString("_getWatchSignalInternal"); |
| 197 SHUTDOWN_ON_ERROR(function_name); |
| 205 Dart_Handle signal_watch = Dart_Invoke(io_lib, function_name, 0, NULL); | 198 Dart_Handle signal_watch = Dart_Invoke(io_lib, function_name, 0, NULL); |
| 206 // Invoke main. | 199 SHUTDOWN_ON_ERROR(signal_watch); |
| 207 result = Dart_Invoke(library, DartUtils::NewString("main"), 1, &signal_watch); | 200 Dart_Handle field_name = Dart_NewStringFromCString("_signalWatch"); |
| 208 SHUTDOWN_ON_ERROR(result); | 201 SHUTDOWN_ON_ERROR(field_name); |
| 209 | 202 result = |
| 210 Dart_ExitScope(); | 203 Dart_SetField(library, field_name, signal_watch); |
| 211 Dart_ExitIsolate(); | 204 SHUTDOWN_ON_ERROR(field_name); |
| 212 | |
| 213 return true; | 205 return true; |
| 214 } | 206 } |
| 215 | 207 |
| 216 | 208 |
| 217 const char* VmService::GetErrorMessage() { | 209 const char* VmService::GetErrorMessage() { |
| 218 return error_msg_ == NULL ? "No error." : error_msg_; | 210 return error_msg_ == NULL ? "No error." : error_msg_; |
| 219 } | 211 } |
| 220 | 212 |
| 221 | 213 |
| 222 Dart_Handle VmService::GetSource(const char* name) { | 214 Dart_Handle VmService::GetSource(const char* name) { |
| (...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 330 return url; | 322 return url; |
| 331 } | 323 } |
| 332 Dart_Handle source = GetSource(url_string); | 324 Dart_Handle source = GetSource(url_string); |
| 333 if (Dart_IsError(source)) { | 325 if (Dart_IsError(source)) { |
| 334 return source; | 326 return source; |
| 335 } | 327 } |
| 336 return Dart_LoadSource(library, url, source, 0, 0); | 328 return Dart_LoadSource(library, url, source, 0, 0); |
| 337 } | 329 } |
| 338 | 330 |
| 339 | 331 |
| 340 void VmService::ThreadMain(uword parameters) { | |
| 341 ASSERT(Dart_CurrentIsolate() == NULL); | |
| 342 Dart_Isolate service_isolate = Dart_GetServiceIsolate(NULL); | |
| 343 Dart_EnterIsolate(service_isolate); | |
| 344 Dart_EnterScope(); | |
| 345 Dart_Handle result = Dart_RunLoop(); | |
| 346 if (Dart_IsError(result)) { | |
| 347 printf("Service exited with an error:\n%s\n", Dart_GetError(result)); | |
| 348 } | |
| 349 Dart_ExitScope(); | |
| 350 Dart_ExitIsolate(); | |
| 351 } | |
| 352 | |
| 353 | |
| 354 | |
| 355 } // namespace bin | 332 } // namespace bin |
| 356 } // namespace dart | 333 } // namespace dart |
| OLD | NEW |