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 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
79 return NULL; | 79 return NULL; |
80 } | 80 } |
81 static ResourcesEntry* ResourcesTable() { | 81 static ResourcesEntry* ResourcesTable() { |
82 return &__service_bin_resources_[0]; | 82 return &__service_bin_resources_[0]; |
83 } | 83 } |
84 | 84 |
85 DISALLOW_ALLOCATION(); | 85 DISALLOW_ALLOCATION(); |
86 DISALLOW_IMPLICIT_CONSTRUCTORS(Resources); | 86 DISALLOW_IMPLICIT_CONSTRUCTORS(Resources); |
87 }; | 87 }; |
88 | 88 |
| 89 |
| 90 void TriggerResourceLoad(Dart_NativeArguments args) { |
| 91 Dart_Handle library = Dart_RootLibrary(); |
| 92 ASSERT(!Dart_IsError(library)); |
| 93 Dart_Handle result = VmService::LoadResources(library); |
| 94 ASSERT(!Dart_IsError(result)); |
| 95 } |
| 96 |
| 97 |
| 98 struct VmServiceIONativeEntry { |
| 99 const char* name; |
| 100 int num_arguments; |
| 101 Dart_NativeFunction function; |
| 102 }; |
| 103 |
| 104 |
| 105 static VmServiceIONativeEntry _VmServiceIONativeEntries[] = { |
| 106 {"VMServiceIO_TriggerResourceLoad", 0, TriggerResourceLoad}, |
| 107 }; |
| 108 |
| 109 |
| 110 static Dart_NativeFunction VmServiceIONativeResolver(Dart_Handle name, |
| 111 int num_arguments, |
| 112 bool* auto_setup_scope) { |
| 113 const char* function_name = NULL; |
| 114 Dart_Handle result = Dart_StringToCString(name, &function_name); |
| 115 ASSERT(!Dart_IsError(result)); |
| 116 ASSERT(function_name != NULL); |
| 117 *auto_setup_scope = true; |
| 118 intptr_t n = |
| 119 sizeof(_VmServiceIONativeEntries) / sizeof(_VmServiceIONativeEntries[0]); |
| 120 for (intptr_t i = 0; i < n; i++) { |
| 121 VmServiceIONativeEntry entry = _VmServiceIONativeEntries[i]; |
| 122 if ((strcmp(function_name, entry.name) == 0) && |
| 123 (num_arguments == entry.num_arguments)) { |
| 124 return entry.function; |
| 125 } |
| 126 } |
| 127 return NULL; |
| 128 } |
| 129 |
| 130 |
89 const char* VmService::error_msg_ = NULL; | 131 const char* VmService::error_msg_ = NULL; |
90 | 132 |
91 bool VmService::Start(const char *server_ip, intptr_t server_port) { | 133 bool VmService::Start(const char *server_ip, intptr_t server_port) { |
92 bool r = _Start(server_ip, server_port); | 134 bool r = _Start(server_ip, server_port); |
93 if (!r) { | 135 if (!r) { |
94 return r; | 136 return r; |
95 } | 137 } |
96 // Start processing messages in a new thread. | 138 // Start processing messages in a new thread. |
97 Thread::Start(ThreadMain, static_cast<uword>(NULL)); | 139 Thread::Start(ThreadMain, static_cast<uword>(NULL)); |
98 return true; | 140 return true; |
99 } | 141 } |
100 | 142 |
101 | 143 |
102 bool VmService::_Start(const char *server_ip, intptr_t server_port) { | 144 bool VmService::_Start(const char *server_ip, intptr_t server_port) { |
103 ASSERT(Dart_CurrentIsolate() == NULL); | 145 ASSERT(Dart_CurrentIsolate() == NULL); |
104 Dart_Isolate isolate = Dart_GetServiceIsolate(NULL); | 146 Dart_Isolate isolate = Dart_GetServiceIsolate(NULL); |
105 if (isolate == NULL) { | 147 if (isolate == NULL) { |
106 error_msg_ = "Internal error."; | 148 error_msg_ = "Dart_GetServiceIsolate failed."; |
107 return false; | 149 return false; |
108 } | 150 } |
109 Dart_EnterIsolate(isolate); | 151 Dart_EnterIsolate(isolate); |
110 Dart_EnterScope(); | 152 Dart_EnterScope(); |
111 // Install our own library tag handler. | 153 // Install our own library tag handler. |
112 Dart_SetLibraryTagHandler(LibraryTagHandler); | 154 Dart_SetLibraryTagHandler(LibraryTagHandler); |
113 Dart_Handle result; | 155 Dart_Handle result; |
114 Dart_Handle library = LoadScript(kVMServiceIOLibraryScriptResourceName); | 156 Dart_Handle library; |
| 157 library = LoadScript(kVMServiceIOLibraryScriptResourceName); |
115 // Expect a library. | 158 // Expect a library. |
116 ASSERT(library != Dart_Null()); | 159 ASSERT(library != Dart_Null()); |
117 SHUTDOWN_ON_ERROR(library); | 160 SHUTDOWN_ON_ERROR(library); |
118 result = Dart_FinalizeLoading(false); | 161 result = Dart_FinalizeLoading(false); |
119 ASSERT(!Dart_IsError(result)); | 162 ASSERT(!Dart_IsError(result)); |
120 Dart_ExitScope(); | 163 Dart_ExitScope(); |
121 Dart_ExitIsolate(); | 164 Dart_ExitIsolate(); |
122 bool retval = Dart_IsolateMakeRunnable(isolate); | 165 bool retval = Dart_IsolateMakeRunnable(isolate); |
123 if (!retval) { | 166 if (!retval) { |
124 Dart_EnterIsolate(isolate); | 167 Dart_EnterIsolate(isolate); |
125 Dart_ShutdownIsolate(); | 168 Dart_ShutdownIsolate(); |
126 error_msg_ = "Invalid isolate state - Unable to make it runnable."; | 169 error_msg_ = "Invalid isolate state - Unable to make it runnable."; |
127 return false; | 170 return false; |
128 } | 171 } |
129 | 172 |
130 Dart_EnterIsolate(isolate); | 173 Dart_EnterIsolate(isolate); |
131 Dart_EnterScope(); | 174 Dart_EnterScope(); |
132 library = Dart_RootLibrary(); | 175 library = Dart_RootLibrary(); |
| 176 result = Dart_SetNativeResolver(library, VmServiceIONativeResolver, NULL); |
| 177 ASSERT(!Dart_IsError(result)); |
133 // Set requested TCP port. | 178 // Set requested TCP port. |
134 DartUtils::SetStringField(library, "_ip", server_ip); | 179 DartUtils::SetStringField(library, "_ip", server_ip); |
135 // If we have a port specified, start the server immediately. | 180 // If we have a port specified, start the server immediately. |
136 bool auto_start = server_port >= 0; | 181 bool auto_start = server_port >= 0; |
137 if (server_port < 0) { | 182 if (server_port < 0) { |
138 // Adjust server_port to port 0 which will result in the first available | 183 // Adjust server_port to port 0 which will result in the first available |
139 // port when the HTTP server is started. | 184 // port when the HTTP server is started. |
140 server_port = 0; | 185 server_port = 0; |
141 } | 186 } |
142 // Set initial state. | 187 // Set initial state. |
143 DartUtils::SetIntegerField(library, "_port", server_port); | 188 DartUtils::SetIntegerField(library, "_port", server_port); |
144 Dart_SetField(library, | 189 Dart_SetField(library, |
145 DartUtils::NewString("_autoStart"), | 190 DartUtils::NewString("_autoStart"), |
146 Dart_NewBoolean(auto_start)); | 191 Dart_NewBoolean(auto_start)); |
147 // We cannot register for signals on windows. | 192 // We cannot register for signals on windows. |
148 #if defined(TARGET_OS_WINDOWS) | 193 #if defined(TARGET_OS_WINDOWS) |
149 const bool is_windows = true; | 194 Dart_Handle is_windows = Dart_True(); |
150 #else | 195 #else |
151 const bool is_windows = false; | 196 Dart_Handle is_windows = Dart_False(); |
152 #endif | 197 #endif |
153 Dart_SetField(library, | 198 Dart_SetField(library, DartUtils::NewString("_isWindows"), is_windows); |
154 DartUtils::NewString("_isWindows"), | |
155 Dart_NewBoolean(is_windows)); | |
156 | 199 |
| 200 |
| 201 // Get _getWatchSignalInternal from dart:io. |
| 202 Dart_Handle dart_io_str = Dart_NewStringFromCString(DartUtils::kIOLibURL); |
| 203 Dart_Handle io_lib = Dart_LookupLibrary(dart_io_str); |
| 204 Dart_Handle function_name = |
| 205 Dart_NewStringFromCString("_getWatchSignalInternal"); |
| 206 Dart_Handle signal_watch = Dart_Invoke(io_lib, function_name, 0, NULL); |
157 // Invoke main. | 207 // Invoke main. |
158 result = Dart_Invoke(library, DartUtils::NewString("main"), 0, NULL); | 208 result = Dart_Invoke(library, DartUtils::NewString("main"), 1, &signal_watch); |
159 SHUTDOWN_ON_ERROR(result); | |
160 // Load resources. | |
161 result = LoadResources(library); | |
162 SHUTDOWN_ON_ERROR(result); | 209 SHUTDOWN_ON_ERROR(result); |
163 | 210 |
164 Dart_ExitScope(); | 211 Dart_ExitScope(); |
165 Dart_ExitIsolate(); | 212 Dart_ExitIsolate(); |
166 | 213 |
167 return true; | 214 return true; |
168 } | 215 } |
169 | 216 |
170 | 217 |
171 const char* VmService::GetErrorMessage() { | 218 const char* VmService::GetErrorMessage() { |
(...skipping 129 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
301 printf("Service exited with an error:\n%s\n", Dart_GetError(result)); | 348 printf("Service exited with an error:\n%s\n", Dart_GetError(result)); |
302 } | 349 } |
303 Dart_ExitScope(); | 350 Dart_ExitScope(); |
304 Dart_ExitIsolate(); | 351 Dart_ExitIsolate(); |
305 } | 352 } |
306 | 353 |
307 | 354 |
308 | 355 |
309 } // namespace bin | 356 } // namespace bin |
310 } // namespace dart | 357 } // namespace dart |
OLD | NEW |