Index: runtime/bin/vmservice_impl.cc |
diff --git a/runtime/bin/vmservice_impl.cc b/runtime/bin/vmservice_impl.cc |
index 18f06c1c3c02f446abe6527511e188de646bad62..1b9654bd1051eb25c6b65824d147e23aaa2e1ed3 100644 |
--- a/runtime/bin/vmservice_impl.cc |
+++ b/runtime/bin/vmservice_impl.cc |
@@ -11,6 +11,7 @@ |
#include "bin/isolate_data.h" |
#include "bin/platform.h" |
#include "bin/thread.h" |
+#include "bin/utils.h" |
#include "platform/json.h" |
namespace dart { |
@@ -94,6 +95,32 @@ void TriggerResourceLoad(Dart_NativeArguments args) { |
} |
+void NotifyServerState(Dart_NativeArguments args) { |
+ Dart_EnterScope(); |
+ const char* ip_chars; |
+ Dart_Handle ip_arg = Dart_GetNativeArgument(args, 0); |
+ if (Dart_IsError(ip_arg)) { |
+ VmService::SetServerIPAndPort("", 0); |
+ Dart_ExitScope(); |
+ return; |
+ } |
+ Dart_Handle result = Dart_StringToCString(ip_arg, &ip_chars); |
+ if (Dart_IsError(result)) { |
+ VmService::SetServerIPAndPort("", 0); |
+ Dart_ExitScope(); |
+ return; |
+ } |
+ Dart_Handle port_arg = Dart_GetNativeArgument(args, 1); |
+ if (Dart_IsError(port_arg)) { |
+ VmService::SetServerIPAndPort("", 0); |
+ Dart_ExitScope(); |
+ return; |
+ } |
+ int64_t port = DartUtils::GetInt64ValueCheckRange(port_arg, 0, 65535); |
+ VmService::SetServerIPAndPort(ip_chars, port); |
+ Dart_ExitScope(); |
+} |
+ |
struct VmServiceIONativeEntry { |
const char* name; |
int num_arguments; |
@@ -103,6 +130,7 @@ struct VmServiceIONativeEntry { |
static VmServiceIONativeEntry _VmServiceIONativeEntries[] = { |
{"VMServiceIO_TriggerResourceLoad", 0, TriggerResourceLoad}, |
+ {"VMServiceIO_NotifyServerState", 2, NotifyServerState}, |
}; |
@@ -128,37 +156,36 @@ static Dart_NativeFunction VmServiceIONativeResolver(Dart_Handle name, |
const char* VmService::error_msg_ = NULL; |
+char VmService::server_ip_[kServerIpStringBufferSize]; |
+intptr_t VmService::server_port_ = 0; |
-bool VmService::Start(const char *server_ip, intptr_t server_port) { |
- bool r = _Start(server_ip, server_port); |
- if (!r) { |
- return r; |
- } |
- // Start processing messages in a new thread. |
- Thread::Start(ThreadMain, static_cast<uword>(NULL)); |
- return true; |
-} |
+bool VmService::Setup(const char* server_ip, intptr_t server_port) { |
+ Dart_Isolate isolate = Dart_CurrentIsolate(); |
+ ASSERT(isolate != NULL); |
+ SetServerIPAndPort("", 0); |
+ Dart_Handle result; |
-bool VmService::_Start(const char *server_ip, intptr_t server_port) { |
- ASSERT(Dart_CurrentIsolate() == NULL); |
- Dart_Isolate isolate = Dart_GetServiceIsolate(NULL); |
- if (isolate == NULL) { |
- error_msg_ = "Dart_GetServiceIsolate failed."; |
- return false; |
- } |
- Dart_EnterIsolate(isolate); |
- Dart_EnterScope(); |
- // Install our own library tag handler. |
+ Dart_Handle builtin_lib = |
+ Builtin::LoadAndCheckLibrary(Builtin::kBuiltinLibrary); |
+ SHUTDOWN_ON_ERROR(builtin_lib); |
+ |
+ // Prepare for script loading by setting up the 'print' and 'timer' |
+ // closures and setting up 'package root' for URI resolution. |
+ result = DartUtils::PrepareForScriptLoading(NULL, true, builtin_lib); |
+ SHUTDOWN_ON_ERROR(result); |
+ |
+ // Load main script. |
Dart_SetLibraryTagHandler(LibraryTagHandler); |
- Dart_Handle result; |
- Dart_Handle library; |
- library = LoadScript(kVMServiceIOLibraryScriptResourceName); |
- // Expect a library. |
+ Dart_Handle library = LoadScript(kVMServiceIOLibraryScriptResourceName); |
ASSERT(library != Dart_Null()); |
SHUTDOWN_ON_ERROR(library); |
+ result = Dart_SetNativeResolver(library, VmServiceIONativeResolver, NULL); |
+ SHUTDOWN_ON_ERROR(result); |
result = Dart_FinalizeLoading(false); |
- ASSERT(!Dart_IsError(result)); |
+ SHUTDOWN_ON_ERROR(result); |
+ |
+ // Make runnable. |
Dart_ExitScope(); |
Dart_ExitIsolate(); |
bool retval = Dart_IsolateMakeRunnable(isolate); |
@@ -168,13 +195,13 @@ bool VmService::_Start(const char *server_ip, intptr_t server_port) { |
error_msg_ = "Invalid isolate state - Unable to make it runnable."; |
return false; |
} |
- |
Dart_EnterIsolate(isolate); |
Dart_EnterScope(); |
+ |
library = Dart_RootLibrary(); |
- result = Dart_SetNativeResolver(library, VmServiceIONativeResolver, NULL); |
- ASSERT(!Dart_IsError(result)); |
- // Set requested TCP port. |
+ SHUTDOWN_ON_ERROR(library); |
+ |
+ // Set HTTP server state. |
DartUtils::SetStringField(library, "_ip", server_ip); |
// If we have a port specified, start the server immediately. |
bool auto_start = server_port >= 0; |
@@ -183,33 +210,37 @@ bool VmService::_Start(const char *server_ip, intptr_t server_port) { |
// port when the HTTP server is started. |
server_port = 0; |
} |
- // Set initial state. |
DartUtils::SetIntegerField(library, "_port", server_port); |
- Dart_SetField(library, |
- DartUtils::NewString("_autoStart"), |
- Dart_NewBoolean(auto_start)); |
- // We cannot register for signals on windows. |
+ result = Dart_SetField(library, |
+ DartUtils::NewString("_autoStart"), |
+ Dart_NewBoolean(auto_start)); |
+ SHUTDOWN_ON_ERROR(result); |
+ |
+ // Are we running on Windows? |
#if defined(TARGET_OS_WINDOWS) |
Dart_Handle is_windows = Dart_True(); |
#else |
Dart_Handle is_windows = Dart_False(); |
#endif |
- Dart_SetField(library, DartUtils::NewString("_isWindows"), is_windows); |
- |
+ result = |
+ Dart_SetField(library, DartUtils::NewString("_isWindows"), is_windows); |
+ SHUTDOWN_ON_ERROR(result); |
// Get _getWatchSignalInternal from dart:io. |
Dart_Handle dart_io_str = Dart_NewStringFromCString(DartUtils::kIOLibURL); |
+ SHUTDOWN_ON_ERROR(dart_io_str); |
Dart_Handle io_lib = Dart_LookupLibrary(dart_io_str); |
+ SHUTDOWN_ON_ERROR(io_lib); |
Dart_Handle function_name = |
Dart_NewStringFromCString("_getWatchSignalInternal"); |
+ SHUTDOWN_ON_ERROR(function_name); |
Dart_Handle signal_watch = Dart_Invoke(io_lib, function_name, 0, NULL); |
- // Invoke main. |
- result = Dart_Invoke(library, DartUtils::NewString("main"), 1, &signal_watch); |
- SHUTDOWN_ON_ERROR(result); |
- |
- Dart_ExitScope(); |
- Dart_ExitIsolate(); |
- |
+ SHUTDOWN_ON_ERROR(signal_watch); |
+ Dart_Handle field_name = Dart_NewStringFromCString("_signalWatch"); |
+ SHUTDOWN_ON_ERROR(field_name); |
+ result = |
+ Dart_SetField(library, field_name, signal_watch); |
+ SHUTDOWN_ON_ERROR(field_name); |
return true; |
} |
@@ -219,6 +250,16 @@ const char* VmService::GetErrorMessage() { |
} |
+void VmService::SetServerIPAndPort(const char* ip, intptr_t port) { |
+ if (ip == NULL) { |
+ ip = ""; |
+ } |
+ strncpy(server_ip_, ip, kServerIpStringBufferSize); |
+ server_ip_[kServerIpStringBufferSize - 1] = '\0'; |
+ server_port_ = port; |
+} |
+ |
+ |
Dart_Handle VmService::GetSource(const char* name) { |
const intptr_t kBufferSize = 512; |
char buffer[kBufferSize]; |
@@ -337,20 +378,5 @@ Dart_Handle VmService::LibraryTagHandler(Dart_LibraryTag tag, |
} |
-void VmService::ThreadMain(uword parameters) { |
- ASSERT(Dart_CurrentIsolate() == NULL); |
- Dart_Isolate service_isolate = Dart_GetServiceIsolate(NULL); |
- Dart_EnterIsolate(service_isolate); |
- Dart_EnterScope(); |
- Dart_Handle result = Dart_RunLoop(); |
- if (Dart_IsError(result)) { |
- printf("Service exited with an error:\n%s\n", Dart_GetError(result)); |
- } |
- Dart_ExitScope(); |
- Dart_ExitIsolate(); |
-} |
- |
- |
- |
} // namespace bin |
} // namespace dart |