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 "vm/service.h" | 5 #include "vm/service.h" |
6 | 6 |
7 #include "include/dart_api.h" | 7 #include "include/dart_api.h" |
8 #include "platform/globals.h" | 8 #include "platform/globals.h" |
9 | 9 |
10 #include "vm/compiler.h" | 10 #include "vm/compiler.h" |
11 #include "vm/coverage.h" | 11 #include "vm/coverage.h" |
12 #include "vm/cpu.h" | 12 #include "vm/cpu.h" |
13 #include "vm/dart_api_impl.h" | 13 #include "vm/dart_api_impl.h" |
14 #include "vm/dart_entry.h" | 14 #include "vm/dart_entry.h" |
15 #include "vm/debugger.h" | 15 #include "vm/debugger.h" |
16 #include "vm/isolate.h" | 16 #include "vm/isolate.h" |
| 17 #include "vm/lockers.h" |
17 #include "vm/message.h" | 18 #include "vm/message.h" |
18 #include "vm/message_handler.h" | 19 #include "vm/message_handler.h" |
19 #include "vm/native_entry.h" | 20 #include "vm/native_entry.h" |
20 #include "vm/native_arguments.h" | 21 #include "vm/native_arguments.h" |
21 #include "vm/object.h" | 22 #include "vm/object.h" |
22 #include "vm/object_graph.h" | 23 #include "vm/object_graph.h" |
23 #include "vm/object_id_ring.h" | 24 #include "vm/object_id_ring.h" |
24 #include "vm/object_store.h" | 25 #include "vm/object_store.h" |
25 #include "vm/parser.h" | 26 #include "vm/parser.h" |
26 #include "vm/port.h" | 27 #include "vm/port.h" |
(...skipping 213 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
240 static void SendRootServiceMessage(Dart_NativeArguments args) { | 241 static void SendRootServiceMessage(Dart_NativeArguments args) { |
241 NativeArguments* arguments = reinterpret_cast<NativeArguments*>(args); | 242 NativeArguments* arguments = reinterpret_cast<NativeArguments*>(args); |
242 Isolate* isolate = arguments->isolate(); | 243 Isolate* isolate = arguments->isolate(); |
243 StackZone zone(isolate); | 244 StackZone zone(isolate); |
244 HANDLESCOPE(isolate); | 245 HANDLESCOPE(isolate); |
245 GET_NON_NULL_NATIVE_ARGUMENT(Instance, message, arguments->NativeArgAt(0)); | 246 GET_NON_NULL_NATIVE_ARGUMENT(Instance, message, arguments->NativeArgAt(0)); |
246 Service::HandleRootMessage(message); | 247 Service::HandleRootMessage(message); |
247 } | 248 } |
248 | 249 |
249 | 250 |
| 251 class ScopeStopwatch : public ValueObject { |
| 252 public: |
| 253 explicit ScopeStopwatch(const char* name) : name_(name) { |
| 254 start_ = OS::GetCurrentTimeMicros(); |
| 255 } |
| 256 |
| 257 int64_t GetElapsed() const { |
| 258 int64_t end = OS::GetCurrentTimeMicros(); |
| 259 ASSERT(end >= start_); |
| 260 return end - start_; |
| 261 } |
| 262 |
| 263 ~ScopeStopwatch() { |
| 264 int64_t elapsed = GetElapsed(); |
| 265 OS::Print("[%" Pd "] %s took %" Pd64 " micros.\n", |
| 266 OS::ProcessId(), name_, elapsed); |
| 267 } |
| 268 |
| 269 private: |
| 270 const char* name_; |
| 271 int64_t start_; |
| 272 }; |
| 273 |
| 274 |
| 275 Dart_Port Service::WaitForLoadPort() { |
| 276 MonitorLocker ml(monitor_); |
| 277 |
| 278 while (initializing_ && (load_port_ == ILLEGAL_PORT)) { |
| 279 ml.Wait(); |
| 280 } |
| 281 |
| 282 return load_port_; |
| 283 } |
| 284 |
| 285 |
| 286 Dart_Port Service::LoadPort() { |
| 287 MonitorLocker ml(monitor_); |
| 288 return load_port_; |
| 289 } |
| 290 |
| 291 |
| 292 void Service::SetLoadPort(Dart_Port port) { |
| 293 MonitorLocker ml(monitor_); |
| 294 load_port_ = port; |
| 295 } |
| 296 |
| 297 |
250 void Service::SetEventMask(uint32_t mask) { | 298 void Service::SetEventMask(uint32_t mask) { |
251 event_mask_ = mask; | 299 event_mask_ = mask; |
252 } | 300 } |
253 | 301 |
254 | 302 |
255 static void SetEventMask(Dart_NativeArguments args) { | 303 static void SetEventMask(Dart_NativeArguments args) { |
256 NativeArguments* arguments = reinterpret_cast<NativeArguments*>(args); | 304 NativeArguments* arguments = reinterpret_cast<NativeArguments*>(args); |
257 Isolate* isolate = arguments->isolate(); | 305 Isolate* isolate = arguments->isolate(); |
258 StackZone zone(isolate); | 306 StackZone zone(isolate); |
259 HANDLESCOPE(isolate); | 307 HANDLESCOPE(isolate); |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
317 const SendPort& send_port = SendPort::Handle(SendPort::New(port_id)); | 365 const SendPort& send_port = SendPort::Handle(SendPort::New(port_id)); |
318 const String& name = String::Handle(String::New(isolate->name())); | 366 const String& name = String::Handle(String::New(isolate->name())); |
319 ASSERT(!name.IsNull()); | 367 ASSERT(!name.IsNull()); |
320 const Array& args = Array::Handle(Array::New(3)); | 368 const Array& args = Array::Handle(Array::New(3)); |
321 ASSERT(!args.IsNull()); | 369 ASSERT(!args.IsNull()); |
322 args.SetAt(0, port_int); | 370 args.SetAt(0, port_int); |
323 args.SetAt(1, send_port); | 371 args.SetAt(1, send_port); |
324 args.SetAt(2, name); | 372 args.SetAt(2, name); |
325 Object& r = Object::Handle(service_isolate_); | 373 Object& r = Object::Handle(service_isolate_); |
326 r = DartEntry::InvokeFunction(register_function_, args); | 374 r = DartEntry::InvokeFunction(register_function_, args); |
| 375 if (FLAG_trace_service) { |
| 376 OS::Print("Isolate %s %" Pd64 " registered with service \n", |
| 377 name.ToCString(), |
| 378 port_id); |
| 379 } |
327 ASSERT(!r.IsError()); | 380 ASSERT(!r.IsError()); |
328 } | 381 } |
329 | 382 |
330 private: | 383 private: |
331 Function& register_function_; | 384 Function& register_function_; |
332 Isolate* service_isolate_; | 385 Isolate* service_isolate_; |
333 }; | 386 }; |
334 | 387 |
335 | 388 |
336 static Dart_Port ExtractPort(Isolate* isolate, Dart_Handle receivePort) { | 389 static Dart_Port ExtractPort(Isolate* isolate, Dart_Handle receivePort) { |
337 const ReceivePort& rp = Api::UnwrapReceivePortHandle(isolate, receivePort); | 390 const ReceivePort& rp = Api::UnwrapReceivePortHandle(isolate, receivePort); |
338 if (rp.IsNull()) { | 391 if (rp.IsNull()) { |
339 return ILLEGAL_PORT; | 392 return ILLEGAL_PORT; |
340 } | 393 } |
341 return rp.Id(); | 394 return rp.Id(); |
342 } | 395 } |
343 | 396 |
344 | 397 |
345 static void OnStart(Dart_NativeArguments args) { | 398 static void OnStart(Dart_NativeArguments args) { |
346 NativeArguments* arguments = reinterpret_cast<NativeArguments*>(args); | 399 NativeArguments* arguments = reinterpret_cast<NativeArguments*>(args); |
347 Isolate* isolate = arguments->isolate(); | 400 Isolate* isolate = arguments->isolate(); |
348 StackZone zone(isolate); | 401 StackZone zone(isolate); |
349 HANDLESCOPE(isolate); | 402 HANDLESCOPE(isolate); |
350 { | 403 { |
| 404 if (FLAG_trace_service) { |
| 405 OS::Print("Booting dart:vmservice library\n"); |
| 406 } |
351 // Boot the dart:vmservice library. | 407 // Boot the dart:vmservice library. |
352 Dart_EnterScope(); | 408 Dart_EnterScope(); |
353 Dart_Handle url_str = | 409 Dart_Handle url_str = |
354 Dart_NewStringFromCString(Symbols::Name(Symbols::kDartVMServiceId)); | 410 Dart_NewStringFromCString(Symbols::Name(Symbols::kDartVMServiceId)); |
355 Dart_Handle library = Dart_LookupLibrary(url_str); | 411 Dart_Handle library = Dart_LookupLibrary(url_str); |
356 ASSERT(Dart_IsLibrary(library)); | 412 ASSERT(Dart_IsLibrary(library)); |
357 Dart_Handle result = | 413 Dart_Handle result = |
358 Dart_Invoke(library, Dart_NewStringFromCString("boot"), 0, NULL); | 414 Dart_Invoke(library, Dart_NewStringFromCString("boot"), 0, NULL); |
359 ASSERT(!Dart_IsError(result)); | 415 ASSERT(!Dart_IsError(result)); |
360 Dart_Port port = ExtractPort(isolate, result); | 416 Dart_Port port = ExtractPort(isolate, result); |
361 ASSERT(port != ILLEGAL_PORT); | 417 ASSERT(port != ILLEGAL_PORT); |
362 Service::set_port(port); | 418 Service::set_port(port); |
363 Dart_ExitScope(); | 419 Dart_ExitScope(); |
364 } | 420 } |
| 421 |
365 { | 422 { |
| 423 if (FLAG_trace_service) { |
| 424 OS::Print("Registering running isolates\n"); |
| 425 } |
366 // Register running isolates with service. | 426 // Register running isolates with service. |
367 RegisterRunningIsolatesVisitor register_isolates(isolate); | 427 RegisterRunningIsolatesVisitor register_isolates(isolate); |
368 Isolate::VisitIsolates(®ister_isolates); | 428 Isolate::VisitIsolates(®ister_isolates); |
369 } | 429 } |
370 } | 430 } |
371 | 431 |
372 | 432 |
373 struct VmServiceNativeEntry { | 433 struct VmServiceNativeEntry { |
374 const char* name; | 434 const char* name; |
375 int num_arguments; | 435 int num_arguments; |
(...skipping 25 matching lines...) Expand all Loading... |
401 for (intptr_t i = 0; i < n; i++) { | 461 for (intptr_t i = 0; i < n; i++) { |
402 VmServiceNativeEntry entry = _VmServiceNativeEntries[i]; | 462 VmServiceNativeEntry entry = _VmServiceNativeEntries[i]; |
403 if ((strcmp(function_name, entry.name) == 0) && | 463 if ((strcmp(function_name, entry.name) == 0) && |
404 (num_arguments == entry.num_arguments)) { | 464 (num_arguments == entry.num_arguments)) { |
405 return entry.function; | 465 return entry.function; |
406 } | 466 } |
407 } | 467 } |
408 return NULL; | 468 return NULL; |
409 } | 469 } |
410 | 470 |
411 | 471 const char* Service::kServiceIsolateName = "vm-service"; |
412 EmbedderServiceHandler* Service::isolate_service_handler_head_ = NULL; | 472 EmbedderServiceHandler* Service::isolate_service_handler_head_ = NULL; |
413 EmbedderServiceHandler* Service::root_service_handler_head_ = NULL; | 473 EmbedderServiceHandler* Service::root_service_handler_head_ = NULL; |
414 Isolate* Service::service_isolate_ = NULL; | 474 Isolate* Service::service_isolate_ = NULL; |
415 Dart_LibraryTagHandler Service::embedder_provided_handler_ = NULL; | |
416 Dart_Port Service::port_ = ILLEGAL_PORT; | 475 Dart_Port Service::port_ = ILLEGAL_PORT; |
| 476 Dart_Port Service::load_port_ = ILLEGAL_PORT; |
| 477 Monitor* Service::monitor_ = NULL; |
| 478 bool Service::initializing_ = true; |
417 uint32_t Service::event_mask_ = 0; | 479 uint32_t Service::event_mask_ = 0; |
418 | 480 |
419 Isolate* Service::GetServiceIsolate(void* callback_data) { | |
420 if (service_isolate_ != NULL) { | |
421 // Already initialized, return service isolate. | |
422 return service_isolate_; | |
423 } | |
424 Dart_ServiceIsolateCreateCalback create_callback = | |
425 Isolate::ServiceCreateCallback(); | |
426 if (create_callback == NULL) { | |
427 return NULL; | |
428 } | |
429 Isolate::SetCurrent(NULL); | |
430 char* error = NULL; | |
431 Isolate* isolate = | |
432 reinterpret_cast<Isolate*>(create_callback(callback_data, &error)); | |
433 if (isolate == NULL) { | |
434 return NULL; | |
435 } | |
436 StartIsolateScope isolate_scope(isolate); | |
437 { | |
438 // Install the dart:vmservice library. | |
439 StackZone zone(isolate); | |
440 HANDLESCOPE(isolate); | |
441 Library& library = | |
442 Library::Handle(isolate, isolate->object_store()->root_library()); | |
443 // Isolate is empty. | |
444 ASSERT(library.IsNull()); | |
445 // Grab embedder tag handler. | |
446 embedder_provided_handler_ = isolate->library_tag_handler(); | |
447 // Temporarily install our own. | |
448 isolate->set_library_tag_handler(LibraryTagHandler); | |
449 // Get script resource. | |
450 const char* resource = NULL; | |
451 const char* path = "/vmservice.dart"; | |
452 intptr_t r = Resources::ResourceLookup(path, &resource); | |
453 ASSERT(r != Resources::kNoSuchInstance); | |
454 ASSERT(resource != NULL); | |
455 const String& source_str = String::Handle( | |
456 String::FromUTF8(reinterpret_cast<const uint8_t*>(resource), r)); | |
457 ASSERT(!source_str.IsNull()); | |
458 const String& url_str = String::Handle(Symbols::DartVMService().raw()); | |
459 library ^= Library::LookupLibrary(url_str); | |
460 ASSERT(library.IsNull()); | |
461 // Setup library. | |
462 library = Library::New(url_str); | |
463 library.Register(); | |
464 const Script& script = Script::Handle( | |
465 isolate, Script::New(url_str, source_str, RawScript::kLibraryTag)); | |
466 library.SetLoadInProgress(); | |
467 Dart_EnterScope(); // Need to enter scope for tag handler. | |
468 const Error& error = Error::Handle(isolate, | |
469 Compiler::Compile(library, script)); | |
470 ASSERT(error.IsNull()); | |
471 Dart_Handle result = Dart_FinalizeLoading(false); | |
472 ASSERT(!Dart_IsError(result)); | |
473 Dart_ExitScope(); | |
474 | 481 |
475 // Install embedder default library tag handler again. | 482 bool Service::IsServiceIsolateName(const char* name) { |
476 isolate->set_library_tag_handler(embedder_provided_handler_); | 483 ASSERT(name != NULL); |
477 embedder_provided_handler_ = NULL; | 484 return strcmp(name, kServiceIsolateName) == 0; |
478 library.set_native_entry_resolver(VmServiceNativeResolver); | |
479 } | |
480 service_isolate_ = reinterpret_cast<Isolate*>(isolate); | |
481 return service_isolate_; | |
482 } | 485 } |
483 | 486 |
484 | 487 |
485 bool Service::SendIsolateStartupMessage() { | 488 bool Service::SendIsolateStartupMessage() { |
486 if (!IsRunning()) { | 489 if (!IsRunning()) { |
487 return false; | 490 return false; |
488 } | 491 } |
489 Isolate* isolate = Isolate::Current(); | 492 Isolate* isolate = Isolate::Current(); |
| 493 if (IsServiceIsolate(isolate)) { |
| 494 return false; |
| 495 } |
490 ASSERT(isolate != NULL); | 496 ASSERT(isolate != NULL); |
491 HANDLESCOPE(isolate); | 497 HANDLESCOPE(isolate); |
492 const String& name = String::Handle(String::New(isolate->name())); | 498 const String& name = String::Handle(String::New(isolate->name())); |
493 ASSERT(!name.IsNull()); | 499 ASSERT(!name.IsNull()); |
494 const Array& list = Array::Handle( | 500 const Array& list = Array::Handle( |
495 MakeServiceControlMessage(Dart_GetMainPortId(), | 501 MakeServiceControlMessage(Dart_GetMainPortId(), |
496 VM_SERVICE_ISOLATE_STARTUP_MESSAGE_ID, | 502 VM_SERVICE_ISOLATE_STARTUP_MESSAGE_ID, |
497 name)); | 503 name)); |
498 ASSERT(!list.IsNull()); | 504 ASSERT(!list.IsNull()); |
499 uint8_t* data = NULL; | 505 uint8_t* data = NULL; |
500 MessageWriter writer(&data, &allocator, false); | 506 MessageWriter writer(&data, &allocator, false); |
501 writer.WriteMessage(list); | 507 writer.WriteMessage(list); |
502 intptr_t len = writer.BytesWritten(); | 508 intptr_t len = writer.BytesWritten(); |
503 if (FLAG_trace_service) { | 509 if (FLAG_trace_service) { |
504 OS::Print("Isolate %s %" Pd64 " registered with service \n", | 510 OS::Print("Isolate %s %" Pd64 " registered with service \n", |
505 name.ToCString(), | 511 name.ToCString(), |
506 Dart_GetMainPortId()); | 512 Dart_GetMainPortId()); |
507 } | 513 } |
508 return PortMap::PostMessage( | 514 return PortMap::PostMessage( |
509 new Message(port_, data, len, Message::kNormalPriority)); | 515 new Message(port_, data, len, Message::kNormalPriority)); |
510 } | 516 } |
511 | 517 |
512 | 518 |
513 bool Service::SendIsolateShutdownMessage() { | 519 bool Service::SendIsolateShutdownMessage() { |
514 if (!IsRunning()) { | 520 if (!IsRunning()) { |
515 return false; | 521 return false; |
516 } | 522 } |
517 Isolate* isolate = Isolate::Current(); | 523 Isolate* isolate = Isolate::Current(); |
| 524 if (IsServiceIsolate(isolate)) { |
| 525 return false; |
| 526 } |
518 ASSERT(isolate != NULL); | 527 ASSERT(isolate != NULL); |
519 HANDLESCOPE(isolate); | 528 HANDLESCOPE(isolate); |
520 const String& name = String::Handle(String::New(isolate->name())); | 529 const String& name = String::Handle(String::New(isolate->name())); |
521 ASSERT(!name.IsNull()); | 530 ASSERT(!name.IsNull()); |
522 const Array& list = Array::Handle( | 531 const Array& list = Array::Handle( |
523 MakeServiceControlMessage(Dart_GetMainPortId(), | 532 MakeServiceControlMessage(Dart_GetMainPortId(), |
524 VM_SERVICE_ISOLATE_SHUTDOWN_MESSAGE_ID, | 533 VM_SERVICE_ISOLATE_SHUTDOWN_MESSAGE_ID, |
525 name)); | 534 name)); |
526 ASSERT(!list.IsNull()); | 535 ASSERT(!list.IsNull()); |
527 uint8_t* data = NULL; | 536 uint8_t* data = NULL; |
(...skipping 25 matching lines...) Expand all Loading... |
553 const uint8_t* str = Resources::Resource(i); | 562 const uint8_t* str = Resources::Resource(i); |
554 intptr_t length = Resources::Length(i); | 563 intptr_t length = Resources::Length(i); |
555 return Dart_NewStringFromUTF8(str, length); | 564 return Dart_NewStringFromUTF8(str, length); |
556 } | 565 } |
557 i++; | 566 i++; |
558 } | 567 } |
559 return Dart_Null(); | 568 return Dart_Null(); |
560 } | 569 } |
561 | 570 |
562 | 571 |
563 Dart_Handle Service::LibraryTagHandler(Dart_LibraryTag tag, Dart_Handle library, | 572 Dart_Handle Service::LibraryTagHandler(Dart_LibraryTag tag, |
| 573 Dart_Handle library, |
564 Dart_Handle url) { | 574 Dart_Handle url) { |
565 if (!Dart_IsLibrary(library)) { | 575 if (tag == Dart_kCanonicalizeUrl) { |
566 return Dart_NewApiError("not a library"); | 576 // url is already canonicalized. |
| 577 return url; |
567 } | 578 } |
568 if (!Dart_IsString(url)) { | 579 if (tag != Dart_kSourceTag) { |
569 return Dart_NewApiError("url is not a string"); | 580 FATAL("Service::LibraryTagHandler encountered an unexpected tag."); |
570 } | 581 } |
| 582 ASSERT(tag == Dart_kSourceTag); |
571 const char* url_string = NULL; | 583 const char* url_string = NULL; |
572 Dart_Handle result = Dart_StringToCString(url, &url_string); | 584 Dart_Handle result = Dart_StringToCString(url, &url_string); |
573 if (Dart_IsError(result)) { | 585 if (Dart_IsError(result)) { |
574 return result; | 586 return result; |
575 } | 587 } |
576 if (tag == Dart_kImportTag) { | |
577 // Embedder handles all requests for external libraries. | |
578 if (embedder_provided_handler_ == NULL) { | |
579 return Dart_NewApiError("Unable to import module as no library tag " | |
580 "handler has been provided by embedder"); | |
581 } | |
582 return embedder_provided_handler_(tag, library, url); | |
583 } | |
584 ASSERT((tag == Dart_kSourceTag) || (tag == Dart_kCanonicalizeUrl)); | |
585 if (tag == Dart_kCanonicalizeUrl) { | |
586 // url is already canonicalized. | |
587 return url; | |
588 } | |
589 Dart_Handle source = GetSource(url_string); | 588 Dart_Handle source = GetSource(url_string); |
590 if (Dart_IsError(source)) { | 589 if (Dart_IsError(source)) { |
591 return source; | 590 return source; |
592 } | 591 } |
593 return Dart_LoadSource(library, url, source, 0, 0); | 592 return Dart_LoadSource(library, url, source, 0, 0); |
594 } | 593 } |
595 | 594 |
596 | 595 |
| 596 void Service::MaybeInjectVMServiceLibrary(Isolate* isolate) { |
| 597 if (service_isolate_ != NULL) { |
| 598 // Service isolate already exists. |
| 599 return; |
| 600 } |
| 601 if (!Service::IsServiceIsolateName(isolate->name())) { |
| 602 // Not service isolate. |
| 603 return; |
| 604 } |
| 605 service_isolate_ = isolate; |
| 606 ASSERT(isolate != NULL); |
| 607 |
| 608 StackZone zone(isolate); |
| 609 HANDLESCOPE(isolate); |
| 610 |
| 611 // Register dart:vmservice library. |
| 612 const String& url_str = String::Handle(Symbols::DartVMService().raw()); |
| 613 const Library& library = Library::Handle(Library::New(url_str)); |
| 614 library.Register(); |
| 615 library.set_native_entry_resolver(VmServiceNativeResolver); |
| 616 |
| 617 // Temporarily install our library tag handler. |
| 618 isolate->set_library_tag_handler(LibraryTagHandler); |
| 619 |
| 620 // Get script source. |
| 621 const char* resource = NULL; |
| 622 const char* path = "/vmservice.dart"; |
| 623 intptr_t r = Resources::ResourceLookup(path, &resource); |
| 624 ASSERT(r != Resources::kNoSuchInstance); |
| 625 ASSERT(resource != NULL); |
| 626 const String& source_str = String::Handle( |
| 627 String::FromUTF8(reinterpret_cast<const uint8_t*>(resource), r)); |
| 628 ASSERT(!source_str.IsNull()); |
| 629 const Script& script = Script::Handle( |
| 630 isolate, Script::New(url_str, source_str, RawScript::kLibraryTag)); |
| 631 |
| 632 // Compile script. |
| 633 Dart_EnterScope(); // Need to enter scope for tag handler. |
| 634 library.SetLoadInProgress(); |
| 635 const Error& error = Error::Handle(isolate, |
| 636 Compiler::Compile(library, script)); |
| 637 ASSERT(error.IsNull()); |
| 638 Dart_Handle result = Dart_FinalizeLoading(false); |
| 639 ASSERT(!Dart_IsError(result)); |
| 640 Dart_ExitScope(); |
| 641 |
| 642 // Uninstall our library tag handler. |
| 643 isolate->set_library_tag_handler(NULL); |
| 644 } |
| 645 |
| 646 |
| 647 void Service::FinishedInitializing() { |
| 648 MonitorLocker ml(monitor_); |
| 649 initializing_ = false; |
| 650 ml.NotifyAll(); |
| 651 } |
| 652 |
| 653 |
| 654 static void ShutdownIsolate(uword parameter) { |
| 655 Isolate* isolate = reinterpret_cast<Isolate*>(parameter); |
| 656 { |
| 657 // Print the error if there is one. This may execute dart code to |
| 658 // print the exception object, so we need to use a StartIsolateScope. |
| 659 StartIsolateScope start_scope(isolate); |
| 660 StackZone zone(isolate); |
| 661 HandleScope handle_scope(isolate); |
| 662 Error& error = Error::Handle(); |
| 663 error = isolate->object_store()->sticky_error(); |
| 664 if (!error.IsNull()) { |
| 665 OS::PrintErr("Service shutting down: %s\n", error.ToErrorCString()); |
| 666 } |
| 667 Dart::RunShutdownCallback(); |
| 668 } |
| 669 { |
| 670 // Shut the isolate down. |
| 671 SwitchIsolateScope switch_scope(isolate); |
| 672 Dart::ShutdownIsolate(); |
| 673 } |
| 674 } |
| 675 |
| 676 |
| 677 class RunServiceTask : public ThreadPool::Task { |
| 678 public: |
| 679 virtual void Run() { |
| 680 ASSERT(Isolate::Current() == NULL); |
| 681 char* error = NULL; |
| 682 Isolate* isolate = NULL; |
| 683 |
| 684 Dart_IsolateCreateCallback create_callback = Isolate::CreateCallback(); |
| 685 if (create_callback == NULL) { |
| 686 Service::FinishedInitializing(); |
| 687 return; |
| 688 } |
| 689 |
| 690 isolate = |
| 691 reinterpret_cast<Isolate*>(create_callback(Service::kServiceIsolateName, |
| 692 NULL, |
| 693 NULL, |
| 694 NULL, |
| 695 &error)); |
| 696 Isolate::SetCurrent(NULL); |
| 697 |
| 698 if (isolate == NULL) { |
| 699 OS::PrintErr("Service startup error: %s\n", error); |
| 700 Service::FinishedInitializing(); |
| 701 return; |
| 702 } |
| 703 |
| 704 RunMain(isolate); |
| 705 |
| 706 Service::FinishedInitializing(); |
| 707 |
| 708 isolate->message_handler()->Run(Dart::thread_pool(), |
| 709 NULL, |
| 710 ShutdownIsolate, |
| 711 reinterpret_cast<uword>(isolate)); |
| 712 } |
| 713 |
| 714 protected: |
| 715 void RunMain(Isolate* isolate) { |
| 716 StartIsolateScope iso_scope(isolate); |
| 717 StackZone zone(isolate); |
| 718 HANDLESCOPE(isolate); |
| 719 // Invoke main which will return the loadScriptPort. |
| 720 const Library& root_library = |
| 721 Library::Handle(isolate, isolate->object_store()->root_library()); |
| 722 if (root_library.IsNull()) { |
| 723 // Service isolate is not supported by embedder. |
| 724 return; |
| 725 } |
| 726 ASSERT(!root_library.IsNull()); |
| 727 const String& entry_name = String::Handle(isolate, String::New("main")); |
| 728 ASSERT(!entry_name.IsNull()); |
| 729 const Function& entry = |
| 730 Function::Handle(isolate, |
| 731 root_library.LookupFunctionAllowPrivate(entry_name)); |
| 732 if (entry.IsNull()) { |
| 733 // Service isolate is not supported by embedder. |
| 734 return; |
| 735 } |
| 736 ASSERT(!entry.IsNull()); |
| 737 const Object& result = |
| 738 Object::Handle(isolate, |
| 739 DartEntry::InvokeFunction(entry, |
| 740 Object::empty_array())); |
| 741 ASSERT(!result.IsNull()); |
| 742 if (result.IsError()) { |
| 743 // Service isolate did not initialize properly. |
| 744 return; |
| 745 } |
| 746 ASSERT(result.IsReceivePort()); |
| 747 const ReceivePort& rp = ReceivePort::Cast(result); |
| 748 Service::SetLoadPort(rp.Id()); |
| 749 } |
| 750 }; |
| 751 |
| 752 |
| 753 void Service::RunService() { |
| 754 ASSERT(monitor_ == NULL); |
| 755 monitor_ = new Monitor(); |
| 756 ASSERT(monitor_ != NULL); |
| 757 Dart::thread_pool()->Run(new RunServiceTask()); |
| 758 } |
| 759 |
597 // A handler for a per-isolate request. | 760 // A handler for a per-isolate request. |
598 // | 761 // |
599 // If a handler returns true, the reply is complete and ready to be | 762 // If a handler returns true, the reply is complete and ready to be |
600 // posted. If a handler returns false, then it is responsible for | 763 // posted. If a handler returns false, then it is responsible for |
601 // posting the reply (this can be used for asynchronous delegation of | 764 // posting the reply (this can be used for asynchronous delegation of |
602 // the response handling). | 765 // the response handling). |
603 typedef bool (*IsolateMessageHandler)(Isolate* isolate, JSONStream* stream); | 766 typedef bool (*IsolateMessageHandler)(Isolate* isolate, JSONStream* stream); |
604 | 767 |
605 struct IsolateMessageHandlerEntry { | 768 struct IsolateMessageHandlerEntry { |
606 const char* command; | 769 const char* command; |
(...skipping 2173 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2780 while (current != NULL) { | 2943 while (current != NULL) { |
2781 if (strcmp(name, current->name()) == 0) { | 2944 if (strcmp(name, current->name()) == 0) { |
2782 return current; | 2945 return current; |
2783 } | 2946 } |
2784 current = current->next(); | 2947 current = current->next(); |
2785 } | 2948 } |
2786 return NULL; | 2949 return NULL; |
2787 } | 2950 } |
2788 | 2951 |
2789 } // namespace dart | 2952 } // namespace dart |
OLD | NEW |