Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(670)

Side by Side Diff: runtime/vm/isolate.cc

Issue 13452007: Add new Dart API call Dart_MakeIsolateRunnable(). This would allow an (Closed) Base URL: http://dart.googlecode.com/svn/branches/bleeding_edge/dart/
Patch Set: Created 7 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« runtime/lib/isolate.cc ('K') | « runtime/vm/isolate.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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/isolate.h" 5 #include "vm/isolate.h"
6 6
7 #include "include/dart_api.h" 7 #include "include/dart_api.h"
8 #include "platform/assert.h" 8 #include "platform/assert.h"
9 #include "platform/json.h" 9 #include "platform/json.h"
10 #include "lib/mirrors.h" 10 #include "lib/mirrors.h"
(...skipping 319 matching lines...) Expand 10 before | Expand all | Expand 10 after
330 simulator_(NULL), 330 simulator_(NULL),
331 long_jump_base_(NULL), 331 long_jump_base_(NULL),
332 timer_list_(), 332 timer_list_(),
333 deopt_id_(0), 333 deopt_id_(0),
334 ic_data_array_(Array::null()), 334 ic_data_array_(Array::null()),
335 mutex_(new Mutex()), 335 mutex_(new Mutex()),
336 stack_limit_(0), 336 stack_limit_(0),
337 saved_stack_limit_(0), 337 saved_stack_limit_(0),
338 message_handler_(NULL), 338 message_handler_(NULL),
339 spawn_data_(0), 339 spawn_data_(0),
340 is_runnable_(false),
340 gc_prologue_callbacks_(), 341 gc_prologue_callbacks_(),
341 gc_epilogue_callbacks_(), 342 gc_epilogue_callbacks_(),
342 deopt_cpu_registers_copy_(NULL), 343 deopt_cpu_registers_copy_(NULL),
343 deopt_fpu_registers_copy_(NULL), 344 deopt_fpu_registers_copy_(NULL),
344 deopt_frame_copy_(NULL), 345 deopt_frame_copy_(NULL),
345 deopt_frame_copy_size_(0), 346 deopt_frame_copy_size_(0),
346 deferred_objects_(NULL), 347 deferred_objects_(NULL),
347 stacktrace_(NULL), 348 stacktrace_(NULL),
348 stack_frame_index_(-1) { 349 stack_frame_index_(-1) {
349 } 350 }
(...skipping 123 matching lines...) Expand 10 before | Expand all | Expand 10 after
473 mutex_->Lock(); 474 mutex_->Lock();
474 ASSERT((interrupt_bits & ~kInterruptsMask) == 0); // Must fit in mask. 475 ASSERT((interrupt_bits & ~kInterruptsMask) == 0); // Must fit in mask.
475 if (stack_limit_ == saved_stack_limit_) { 476 if (stack_limit_ == saved_stack_limit_) {
476 stack_limit_ = (~static_cast<uword>(0)) & ~kInterruptsMask; 477 stack_limit_ = (~static_cast<uword>(0)) & ~kInterruptsMask;
477 } 478 }
478 stack_limit_ |= interrupt_bits; 479 stack_limit_ |= interrupt_bits;
479 mutex_->Unlock(); 480 mutex_->Unlock();
480 } 481 }
481 482
482 483
484 bool Isolate::MakeRunnable() {
485 ASSERT(Isolate::Current() == NULL);
486 // Can't use MutexLocker here because MutexLocker is
487 // a StackResource, which requires a current isolate.
488 mutex_->Lock();
489 // Check if we are in a valid state to make the isolate runnable.
490 if (is_runnable_ == true) {
491 mutex_->Unlock();
492 return false; // Already runnable.
493 }
494 // Set the isolate as runnable and if we are being spawned schedule
495 // isolate on thread pool for execution.
496 is_runnable_ = true;
497 IsolateSpawnState* state = reinterpret_cast<IsolateSpawnState*>(spawn_data());
498 if (state != NULL) {
499 ASSERT(this == state->isolate());
500 Run();
501 }
502 mutex_->Unlock();
503 return true;
504 }
505
506
507 static void StoreError(Isolate* isolate, const Object& obj) {
508 ASSERT(obj.IsError());
509 isolate->object_store()->set_sticky_error(Error::Cast(obj));
510 }
511
512
513 static bool RunIsolate(uword parameter) {
514 Isolate* isolate = reinterpret_cast<Isolate*>(parameter);
515 IsolateSpawnState* state = NULL;
516 {
517 MutexLocker ml(isolate->mutex());
518 state = reinterpret_cast<IsolateSpawnState*>(isolate->spawn_data());
519 isolate->set_spawn_data(0);
520 }
521 {
522 StartIsolateScope start_scope(isolate);
523 StackZone zone(isolate);
524 HandleScope handle_scope(isolate);
525 if (!ClassFinalizer::FinalizePendingClasses()) {
526 // Error is in sticky error already.
527 return false;
528 }
529
530 Object& result = Object::Handle();
531 result = state->ResolveFunction();
532 delete state;
533 state = NULL;
534 if (result.IsError()) {
535 StoreError(isolate, result);
536 return false;
537 }
538 ASSERT(result.IsFunction());
539 Function& func = Function::Handle(isolate);
540 func ^= result.raw();
541 result = DartEntry::InvokeFunction(func, Object::empty_array());
542 if (result.IsError()) {
543 StoreError(isolate, result);
544 return false;
545 }
546 }
547 return true;
548 }
549
550
551 static void ShutdownIsolate(uword parameter) {
552 Isolate* isolate = reinterpret_cast<Isolate*>(parameter);
553 {
554 // Print the error if there is one. This may execute dart code to
555 // print the exception object, so we need to use a StartIsolateScope.
556 StartIsolateScope start_scope(isolate);
557 StackZone zone(isolate);
558 HandleScope handle_scope(isolate);
559 Error& error = Error::Handle();
560 error = isolate->object_store()->sticky_error();
561 if (!error.IsNull()) {
562 OS::PrintErr("in ShutdownIsolate: %s\n", error.ToErrorCString());
563 }
564 }
565 {
566 // Shut the isolate down.
567 SwitchIsolateScope switch_scope(isolate);
568 Dart::ShutdownIsolate();
569 }
570 }
571
572
573 void Isolate::Run() {
574 message_handler()->Run(Dart::thread_pool(),
575 RunIsolate,
576 ShutdownIsolate,
577 reinterpret_cast<uword>(this));
578 }
579
580
483 uword Isolate::GetAndClearInterrupts() { 581 uword Isolate::GetAndClearInterrupts() {
484 MutexLocker ml(mutex_); 582 MutexLocker ml(mutex_);
485 if (stack_limit_ == saved_stack_limit_) { 583 if (stack_limit_ == saved_stack_limit_) {
486 return 0; // No interrupt was requested. 584 return 0; // No interrupt was requested.
487 } 585 }
488 uword interrupt_bits = stack_limit_ & kInterruptsMask; 586 uword interrupt_bits = stack_limit_ & kInterruptsMask;
489 stack_limit_ = saved_stack_limit_; 587 stack_limit_ = saved_stack_limit_;
490 return interrupt_bits; 588 return interrupt_bits;
491 } 589 }
492 590
(...skipping 378 matching lines...) Expand 10 before | Expand all | Expand 10 after
871 if (frame_index >= 0) { 969 if (frame_index >= 0) {
872 return isolate->GetStatusStackFrame(frame_index); 970 return isolate->GetStatusStackFrame(frame_index);
873 } 971 }
874 } 972 }
875 973
876 // TODO(tball): "/isolate/<handle>/stacktrace/<frame-index>"/disassemble" 974 // TODO(tball): "/isolate/<handle>/stacktrace/<frame-index>"/disassemble"
877 975
878 return NULL; // Unimplemented query. 976 return NULL; // Unimplemented query.
879 } 977 }
880 978
979
980 static char* GetRootScriptUri(Isolate* isolate) {
981 const Library& library =
982 Library::Handle(isolate->object_store()->root_library());
983 ASSERT(!library.IsNull());
984 const String& script_name = String::Handle(library.url());
985 return isolate->current_zone()->MakeCopyOfString(script_name.ToCString());
986 }
987
988
989 IsolateSpawnState::IsolateSpawnState(const Function& func,
990 const Function& callback_func)
991 : isolate_(NULL),
992 script_url_(NULL),
993 library_url_(NULL),
994 function_name_(NULL),
995 exception_callback_name_(NULL) {
996 script_url_ = strdup(GetRootScriptUri(Isolate::Current()));
997 const Class& cls = Class::Handle(func.Owner());
998 ASSERT(cls.IsTopLevel());
999 const Library& lib = Library::Handle(cls.library());
1000 const String& lib_url = String::Handle(lib.url());
1001 library_url_ = strdup(lib_url.ToCString());
1002
1003 const String& func_name = String::Handle(func.name());
1004 function_name_ = strdup(func_name.ToCString());
1005 if (!callback_func.IsNull()) {
1006 const String& callback_name = String::Handle(callback_func.name());
1007 exception_callback_name_ = strdup(callback_name.ToCString());
1008 } else {
1009 exception_callback_name_ = strdup("_unhandledExceptionCallback");
1010 }
1011 }
1012
1013
1014 IsolateSpawnState::IsolateSpawnState(const char* script_url)
1015 : isolate_(NULL),
1016 library_url_(NULL),
1017 function_name_(NULL),
1018 exception_callback_name_(NULL) {
1019 script_url_ = strdup(script_url);
1020 library_url_ = NULL;
1021 function_name_ = strdup("main");
1022 exception_callback_name_ = strdup("_unhandledExceptionCallback");
1023 }
1024
1025
1026 IsolateSpawnState::~IsolateSpawnState() {
1027 free(script_url_);
1028 free(library_url_);
1029 free(function_name_);
1030 free(exception_callback_name_);
1031 }
1032
1033
1034 RawObject* IsolateSpawnState::ResolveFunction() {
1035 // Resolve the library.
1036 Library& lib = Library::Handle();
1037 if (library_url()) {
1038 const String& lib_url = String::Handle(String::New(library_url()));
1039 lib = Library::LookupLibrary(lib_url);
1040 if (lib.IsNull() || lib.IsError()) {
1041 const String& msg = String::Handle(String::NewFormatted(
1042 "Unable to find library '%s'.", library_url()));
1043 return LanguageError::New(msg);
1044 }
1045 } else {
1046 lib = isolate()->object_store()->root_library();
1047 }
1048 ASSERT(!lib.IsNull());
1049
1050 // Resolve the function.
1051 const String& func_name =
1052 String::Handle(String::New(function_name()));
1053 const Function& func = Function::Handle(lib.LookupLocalFunction(func_name));
1054 if (func.IsNull()) {
1055 const String& msg = String::Handle(String::NewFormatted(
1056 "Unable to resolve function '%s' in library '%s'.",
1057 function_name(), (library_url() ? library_url() : script_url())));
1058 return LanguageError::New(msg);
1059 }
1060 return func.raw();
1061 }
1062
1063
1064 void IsolateSpawnState::Cleanup() {
1065 SwitchIsolateScope switch_scope(isolate());
1066 Dart::ShutdownIsolate();
1067 }
1068
881 } // namespace dart 1069 } // namespace dart
OLDNEW
« runtime/lib/isolate.cc ('K') | « runtime/vm/isolate.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698