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 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
85 return NULL; | 85 return NULL; |
86 } | 86 } |
87 static ResourcesEntry* ResourcesTable() { | 87 static ResourcesEntry* ResourcesTable() { |
88 return &__service_bin_resources_[0]; | 88 return &__service_bin_resources_[0]; |
89 } | 89 } |
90 | 90 |
91 DISALLOW_ALLOCATION(); | 91 DISALLOW_ALLOCATION(); |
92 DISALLOW_IMPLICIT_CONSTRUCTORS(Resources); | 92 DISALLOW_IMPLICIT_CONSTRUCTORS(Resources); |
93 }; | 93 }; |
94 | 94 |
95 | |
96 void NotifyServerState(Dart_NativeArguments args) { | 95 void NotifyServerState(Dart_NativeArguments args) { |
97 Dart_EnterScope(); | 96 Dart_EnterScope(); |
98 const char* uri_chars; | 97 const char* uri_chars; |
99 Dart_Handle uri_arg = Dart_GetNativeArgument(args, 0); | 98 Dart_Handle uri_arg = Dart_GetNativeArgument(args, 0); |
100 if (Dart_IsError(uri_arg)) { | 99 if (Dart_IsError(uri_arg)) { |
101 VmService::SetServerAddress(""); | 100 VmService::SetServerAddress(""); |
102 Dart_ExitScope(); | 101 Dart_ExitScope(); |
103 return; | 102 return; |
104 } | 103 } |
105 Dart_Handle result = Dart_StringToCString(uri_arg, &uri_chars); | 104 Dart_Handle result = Dart_StringToCString(uri_arg, &uri_chars); |
106 if (Dart_IsError(result)) { | 105 if (Dart_IsError(result)) { |
107 VmService::SetServerAddress(""); | 106 VmService::SetServerAddress(""); |
108 Dart_ExitScope(); | 107 Dart_ExitScope(); |
109 return; | 108 return; |
110 } | 109 } |
111 VmService::SetServerAddress(uri_chars); | 110 VmService::SetServerAddress(uri_chars); |
112 Dart_ExitScope(); | 111 Dart_ExitScope(); |
113 } | 112 } |
114 | 113 |
115 | |
116 static void Shutdown(Dart_NativeArguments args) { | 114 static void Shutdown(Dart_NativeArguments args) { |
117 // NO-OP. | 115 // NO-OP. |
118 } | 116 } |
119 | 117 |
120 | |
121 struct VmServiceIONativeEntry { | 118 struct VmServiceIONativeEntry { |
122 const char* name; | 119 const char* name; |
123 int num_arguments; | 120 int num_arguments; |
124 Dart_NativeFunction function; | 121 Dart_NativeFunction function; |
125 }; | 122 }; |
126 | 123 |
127 | |
128 static VmServiceIONativeEntry _VmServiceIONativeEntries[] = { | 124 static VmServiceIONativeEntry _VmServiceIONativeEntries[] = { |
129 {"VMServiceIO_NotifyServerState", 1, NotifyServerState}, | 125 {"VMServiceIO_NotifyServerState", 1, NotifyServerState}, |
130 {"VMServiceIO_Shutdown", 0, Shutdown}, | 126 {"VMServiceIO_Shutdown", 0, Shutdown}, |
131 }; | 127 }; |
132 | 128 |
133 | |
134 static Dart_NativeFunction VmServiceIONativeResolver(Dart_Handle name, | 129 static Dart_NativeFunction VmServiceIONativeResolver(Dart_Handle name, |
135 int num_arguments, | 130 int num_arguments, |
136 bool* auto_setup_scope) { | 131 bool* auto_setup_scope) { |
137 const char* function_name = NULL; | 132 const char* function_name = NULL; |
138 Dart_Handle result = Dart_StringToCString(name, &function_name); | 133 Dart_Handle result = Dart_StringToCString(name, &function_name); |
139 ASSERT(!Dart_IsError(result)); | 134 ASSERT(!Dart_IsError(result)); |
140 ASSERT(function_name != NULL); | 135 ASSERT(function_name != NULL); |
141 *auto_setup_scope = true; | 136 *auto_setup_scope = true; |
142 intptr_t n = | 137 intptr_t n = |
143 sizeof(_VmServiceIONativeEntries) / sizeof(_VmServiceIONativeEntries[0]); | 138 sizeof(_VmServiceIONativeEntries) / sizeof(_VmServiceIONativeEntries[0]); |
144 for (intptr_t i = 0; i < n; i++) { | 139 for (intptr_t i = 0; i < n; i++) { |
145 VmServiceIONativeEntry entry = _VmServiceIONativeEntries[i]; | 140 VmServiceIONativeEntry entry = _VmServiceIONativeEntries[i]; |
146 if ((strcmp(function_name, entry.name) == 0) && | 141 if ((strcmp(function_name, entry.name) == 0) && |
147 (num_arguments == entry.num_arguments)) { | 142 (num_arguments == entry.num_arguments)) { |
148 return entry.function; | 143 return entry.function; |
149 } | 144 } |
150 } | 145 } |
151 return NULL; | 146 return NULL; |
152 } | 147 } |
153 | 148 |
154 | |
155 const char* VmService::error_msg_ = NULL; | 149 const char* VmService::error_msg_ = NULL; |
156 char VmService::server_uri_[kServerUriStringBufferSize]; | 150 char VmService::server_uri_[kServerUriStringBufferSize]; |
157 | 151 |
158 | |
159 bool VmService::LoadForGenPrecompiled(void* vmservice_kernel) { | 152 bool VmService::LoadForGenPrecompiled(void* vmservice_kernel) { |
160 Dart_Handle result; | 153 Dart_Handle result; |
161 Dart_SetLibraryTagHandler(LibraryTagHandler); | 154 Dart_SetLibraryTagHandler(LibraryTagHandler); |
162 Dart_Handle library; | 155 Dart_Handle library; |
163 if (vmservice_kernel != NULL) { | 156 if (vmservice_kernel != NULL) { |
164 library = Dart_LoadLibrary( | 157 library = Dart_LoadLibrary( |
165 Dart_NewStringFromCString(kVMServiceIOLibraryUri), Dart_Null(), | 158 Dart_NewStringFromCString(kVMServiceIOLibraryUri), Dart_Null(), |
166 reinterpret_cast<Dart_Handle>(vmservice_kernel), 0, 0); | 159 reinterpret_cast<Dart_Handle>(vmservice_kernel), 0, 0); |
167 } else { | 160 } else { |
168 library = LookupOrLoadLibrary(kVMServiceIOLibraryScriptResourceName); | 161 library = LookupOrLoadLibrary(kVMServiceIOLibraryScriptResourceName); |
169 } | 162 } |
170 ASSERT(library != Dart_Null()); | 163 ASSERT(library != Dart_Null()); |
171 SHUTDOWN_ON_ERROR(library); | 164 SHUTDOWN_ON_ERROR(library); |
172 result = Dart_SetNativeResolver(library, VmServiceIONativeResolver, NULL); | 165 result = Dart_SetNativeResolver(library, VmServiceIONativeResolver, NULL); |
173 SHUTDOWN_ON_ERROR(result); | 166 SHUTDOWN_ON_ERROR(result); |
174 result = Dart_FinalizeLoading(false); | 167 result = Dart_FinalizeLoading(false); |
175 SHUTDOWN_ON_ERROR(result); | 168 SHUTDOWN_ON_ERROR(result); |
176 return true; | 169 return true; |
177 } | 170 } |
178 | 171 |
179 | |
180 bool VmService::Setup(const char* server_ip, | 172 bool VmService::Setup(const char* server_ip, |
181 intptr_t server_port, | 173 intptr_t server_port, |
182 bool running_precompiled, | 174 bool running_precompiled, |
183 bool dev_mode_server, | 175 bool dev_mode_server, |
184 bool trace_loading) { | 176 bool trace_loading) { |
185 Dart_Isolate isolate = Dart_CurrentIsolate(); | 177 Dart_Isolate isolate = Dart_CurrentIsolate(); |
186 ASSERT(isolate != NULL); | 178 ASSERT(isolate != NULL); |
187 SetServerAddress(""); | 179 SetServerAddress(""); |
188 | 180 |
189 Dart_Handle result; | 181 Dart_Handle result; |
(...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
284 SHUTDOWN_ON_ERROR(function_name); | 276 SHUTDOWN_ON_ERROR(function_name); |
285 Dart_Handle signal_watch = Dart_Invoke(io_lib, function_name, 0, NULL); | 277 Dart_Handle signal_watch = Dart_Invoke(io_lib, function_name, 0, NULL); |
286 SHUTDOWN_ON_ERROR(signal_watch); | 278 SHUTDOWN_ON_ERROR(signal_watch); |
287 Dart_Handle field_name = Dart_NewStringFromCString("_signalWatch"); | 279 Dart_Handle field_name = Dart_NewStringFromCString("_signalWatch"); |
288 SHUTDOWN_ON_ERROR(field_name); | 280 SHUTDOWN_ON_ERROR(field_name); |
289 result = Dart_SetField(library, field_name, signal_watch); | 281 result = Dart_SetField(library, field_name, signal_watch); |
290 SHUTDOWN_ON_ERROR(field_name); | 282 SHUTDOWN_ON_ERROR(field_name); |
291 return true; | 283 return true; |
292 } | 284 } |
293 | 285 |
294 | |
295 const char* VmService::GetErrorMessage() { | 286 const char* VmService::GetErrorMessage() { |
296 return (error_msg_ == NULL) ? "No error." : error_msg_; | 287 return (error_msg_ == NULL) ? "No error." : error_msg_; |
297 } | 288 } |
298 | 289 |
299 | |
300 void VmService::SetServerAddress(const char* server_uri) { | 290 void VmService::SetServerAddress(const char* server_uri) { |
301 if (server_uri == NULL) { | 291 if (server_uri == NULL) { |
302 server_uri = ""; | 292 server_uri = ""; |
303 } | 293 } |
304 const intptr_t server_uri_len = strlen(server_uri); | 294 const intptr_t server_uri_len = strlen(server_uri); |
305 if (server_uri_len >= (kServerUriStringBufferSize - 1)) { | 295 if (server_uri_len >= (kServerUriStringBufferSize - 1)) { |
306 FATAL1("vm-service: Server URI exceeded length: %s\n", server_uri); | 296 FATAL1("vm-service: Server URI exceeded length: %s\n", server_uri); |
307 } | 297 } |
308 strncpy(server_uri_, server_uri, kServerUriStringBufferSize); | 298 strncpy(server_uri_, server_uri, kServerUriStringBufferSize); |
309 server_uri_[kServerUriStringBufferSize - 1] = '\0'; | 299 server_uri_[kServerUriStringBufferSize - 1] = '\0'; |
310 } | 300 } |
311 | 301 |
312 | |
313 Dart_Handle VmService::GetSource(const char* name) { | 302 Dart_Handle VmService::GetSource(const char* name) { |
314 const intptr_t kBufferSize = 512; | 303 const intptr_t kBufferSize = 512; |
315 char buffer[kBufferSize]; | 304 char buffer[kBufferSize]; |
316 snprintf(&buffer[0], kBufferSize - 1, "%s/%s", kLibrarySourceNamePrefix, | 305 snprintf(&buffer[0], kBufferSize - 1, "%s/%s", kLibrarySourceNamePrefix, |
317 name); | 306 name); |
318 const char* vmservice_source = NULL; | 307 const char* vmservice_source = NULL; |
319 int r = Resources::ResourceLookup(buffer, &vmservice_source); | 308 int r = Resources::ResourceLookup(buffer, &vmservice_source); |
320 if (r == Resources::kNoSuchInstance) { | 309 if (r == Resources::kNoSuchInstance) { |
321 FATAL1("vm-service: Could not find embedded source file: %s ", buffer); | 310 FATAL1("vm-service: Could not find embedded source file: %s ", buffer); |
322 } | 311 } |
323 ASSERT(r != Resources::kNoSuchInstance); | 312 ASSERT(r != Resources::kNoSuchInstance); |
324 return Dart_NewStringFromCString(vmservice_source); | 313 return Dart_NewStringFromCString(vmservice_source); |
325 } | 314 } |
326 | 315 |
327 | |
328 Dart_Handle VmService::LoadScript(const char* name) { | 316 Dart_Handle VmService::LoadScript(const char* name) { |
329 Dart_Handle uri = Dart_NewStringFromCString(kVMServiceIOLibraryUri); | 317 Dart_Handle uri = Dart_NewStringFromCString(kVMServiceIOLibraryUri); |
330 Dart_Handle source = GetSource(name); | 318 Dart_Handle source = GetSource(name); |
331 return Dart_LoadScript(uri, Dart_Null(), source, 0, 0); | 319 return Dart_LoadScript(uri, Dart_Null(), source, 0, 0); |
332 } | 320 } |
333 | 321 |
334 | |
335 Dart_Handle VmService::LookupOrLoadLibrary(const char* name) { | 322 Dart_Handle VmService::LookupOrLoadLibrary(const char* name) { |
336 Dart_Handle uri = Dart_NewStringFromCString(kVMServiceIOLibraryUri); | 323 Dart_Handle uri = Dart_NewStringFromCString(kVMServiceIOLibraryUri); |
337 Dart_Handle library = Dart_LookupLibrary(uri); | 324 Dart_Handle library = Dart_LookupLibrary(uri); |
338 if (!Dart_IsLibrary(library)) { | 325 if (!Dart_IsLibrary(library)) { |
339 Dart_Handle source = GetSource(name); | 326 Dart_Handle source = GetSource(name); |
340 library = Dart_LoadLibrary(uri, Dart_Null(), source, 0, 0); | 327 library = Dart_LoadLibrary(uri, Dart_Null(), source, 0, 0); |
341 } | 328 } |
342 return library; | 329 return library; |
343 } | 330 } |
344 | 331 |
345 | |
346 Dart_Handle VmService::LoadSource(Dart_Handle library, const char* name) { | 332 Dart_Handle VmService::LoadSource(Dart_Handle library, const char* name) { |
347 Dart_Handle uri = Dart_NewStringFromCString(name); | 333 Dart_Handle uri = Dart_NewStringFromCString(name); |
348 Dart_Handle source = GetSource(name); | 334 Dart_Handle source = GetSource(name); |
349 return Dart_LoadSource(library, uri, Dart_Null(), source, 0, 0); | 335 return Dart_LoadSource(library, uri, Dart_Null(), source, 0, 0); |
350 } | 336 } |
351 | 337 |
352 | |
353 Dart_Handle VmService::LibraryTagHandler(Dart_LibraryTag tag, | 338 Dart_Handle VmService::LibraryTagHandler(Dart_LibraryTag tag, |
354 Dart_Handle library, | 339 Dart_Handle library, |
355 Dart_Handle url) { | 340 Dart_Handle url) { |
356 if (!Dart_IsLibrary(library)) { | 341 if (!Dart_IsLibrary(library)) { |
357 return Dart_NewApiError("not a library"); | 342 return Dart_NewApiError("not a library"); |
358 } | 343 } |
359 if (!Dart_IsString(url)) { | 344 if (!Dart_IsString(url)) { |
360 return Dart_NewApiError("url is not a string"); | 345 return Dart_NewApiError("url is not a string"); |
361 } | 346 } |
362 const char* url_string = NULL; | 347 const char* url_string = NULL; |
(...skipping 16 matching lines...) Expand all Loading... |
379 // url is already canonicalized. | 364 // url is already canonicalized. |
380 return url; | 365 return url; |
381 } | 366 } |
382 Dart_Handle source = GetSource(url_string); | 367 Dart_Handle source = GetSource(url_string); |
383 if (Dart_IsError(source)) { | 368 if (Dart_IsError(source)) { |
384 return source; | 369 return source; |
385 } | 370 } |
386 return Dart_LoadSource(library, url, Dart_Null(), source, 0, 0); | 371 return Dart_LoadSource(library, url, Dart_Null(), source, 0, 0); |
387 } | 372 } |
388 | 373 |
389 | |
390 } // namespace bin | 374 } // namespace bin |
391 } // namespace dart | 375 } // namespace dart |
OLD | NEW |