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/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 "vm/code_observers.h" | 10 #include "vm/code_observers.h" |
(...skipping 759 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
770 bool is_spawn_uri = state->is_spawn_uri(); | 770 bool is_spawn_uri = state->is_spawn_uri(); |
771 if (result.IsError()) { | 771 if (result.IsError()) { |
772 StoreError(isolate, result); | 772 StoreError(isolate, result); |
773 return false; | 773 return false; |
774 } | 774 } |
775 ASSERT(result.IsFunction()); | 775 ASSERT(result.IsFunction()); |
776 Function& func = Function::Handle(isolate); | 776 Function& func = Function::Handle(isolate); |
777 func ^= result.raw(); | 777 func ^= result.raw(); |
778 func = func.ImplicitClosureFunction(); | 778 func = func.ImplicitClosureFunction(); |
779 | 779 |
| 780 const Array& capabilities = Array::Handle(Array::New(2)); |
| 781 Capability& capability = Capability::Handle(); |
| 782 capability = Capability::New(isolate->pause_capability()); |
| 783 capabilities.SetAt(0, capability); |
| 784 capability = Capability::New(isolate->terminate_capability()); |
| 785 capabilities.SetAt(1, capability); |
| 786 |
780 // Instead of directly invoking the entry point we call '_startIsolate' with | 787 // Instead of directly invoking the entry point we call '_startIsolate' with |
781 // the entry point as argument. The '_startIsolate' function will | 788 // the entry point as argument. |
782 // communicate with the spawner to receive the initial message before it | |
783 // executes the real entry point. | |
784 // Since this function ("RunIsolate") is used for both Isolate.spawn and | 789 // Since this function ("RunIsolate") is used for both Isolate.spawn and |
785 // Isolate.spawnUri we also send a boolean flag as argument so that the | 790 // Isolate.spawnUri we also send a boolean flag as argument so that the |
786 // "_startIsolate" function can act corresponding to how the isolate was | 791 // "_startIsolate" function can act corresponding to how the isolate was |
787 // created. | 792 // created. |
788 const Array& args = Array::Handle(Array::New(2)); | 793 const Array& args = Array::Handle(Array::New(7)); |
789 args.SetAt(0, Instance::Handle(func.ImplicitStaticClosure())); | 794 args.SetAt(0, SendPort::Handle(SendPort::New(state->parent_port()))); |
790 args.SetAt(1, is_spawn_uri ? Bool::True() : Bool::False()); | 795 args.SetAt(1, Instance::Handle(func.ImplicitStaticClosure())); |
| 796 args.SetAt(2, Instance::Handle(state->BuildArgs())); |
| 797 args.SetAt(3, Instance::Handle(state->BuildMessage())); |
| 798 args.SetAt(4, is_spawn_uri ? Bool::True() : Bool::False()); |
| 799 args.SetAt(5, ReceivePort::Handle( |
| 800 ReceivePort::New(isolate->main_port(), true /* control port */))); |
| 801 args.SetAt(6, capabilities); |
791 | 802 |
792 const Library& lib = Library::Handle(Library::IsolateLibrary()); | 803 const Library& lib = Library::Handle(Library::IsolateLibrary()); |
793 const String& entry_name = String::Handle(String::New("_startIsolate")); | 804 const String& entry_name = String::Handle(String::New("_startIsolate")); |
794 const Function& entry_point = | 805 const Function& entry_point = |
795 Function::Handle(lib.LookupLocalFunction(entry_name)); | 806 Function::Handle(lib.LookupLocalFunction(entry_name)); |
796 ASSERT(entry_point.IsFunction() && !entry_point.IsNull()); | 807 ASSERT(entry_point.IsFunction() && !entry_point.IsNull()); |
797 | 808 |
798 result = DartEntry::InvokeFunction(entry_point, args); | 809 result = DartEntry::InvokeFunction(entry_point, args); |
799 if (result.IsError()) { | 810 if (result.IsError()) { |
800 StoreError(isolate, result); | 811 StoreError(isolate, result); |
(...skipping 307 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1108 if (stack->Length() > 0) { | 1119 if (stack->Length() > 0) { |
1109 JSONObject jsframe(&jsobj, "topFrame"); | 1120 JSONObject jsframe(&jsobj, "topFrame"); |
1110 | 1121 |
1111 ActivationFrame* frame = stack->FrameAt(0); | 1122 ActivationFrame* frame = stack->FrameAt(0); |
1112 frame->PrintToJSONObject(&jsobj); | 1123 frame->PrintToJSONObject(&jsobj); |
1113 // TODO(turnidge): Implement depth differently -- differentiate | 1124 // TODO(turnidge): Implement depth differently -- differentiate |
1114 // inlined frames. | 1125 // inlined frames. |
1115 jsobj.AddProperty("depth", (intptr_t)0); | 1126 jsobj.AddProperty("depth", (intptr_t)0); |
1116 } | 1127 } |
1117 jsobj.AddProperty("livePorts", message_handler()->live_ports()); | 1128 jsobj.AddProperty("livePorts", message_handler()->live_ports()); |
1118 jsobj.AddProperty("controlPorts", message_handler()->control_ports()); | |
1119 jsobj.AddProperty("pauseOnExit", message_handler()->pause_on_exit()); | 1129 jsobj.AddProperty("pauseOnExit", message_handler()->pause_on_exit()); |
1120 | 1130 |
1121 // TODO(turnidge): Make the debugger support paused_on_start/exit. | 1131 // TODO(turnidge): Make the debugger support paused_on_start/exit. |
1122 if (message_handler()->paused_on_start()) { | 1132 if (message_handler()->paused_on_start()) { |
1123 ASSERT(debugger()->PauseEvent() == NULL); | 1133 ASSERT(debugger()->PauseEvent() == NULL); |
1124 DebuggerEvent pauseEvent(this, DebuggerEvent::kIsolateCreated); | 1134 DebuggerEvent pauseEvent(this, DebuggerEvent::kIsolateCreated); |
1125 jsobj.AddProperty("pauseEvent", &pauseEvent); | 1135 jsobj.AddProperty("pauseEvent", &pauseEvent); |
1126 } else if (message_handler()->paused_on_exit()) { | 1136 } else if (message_handler()->paused_on_exit()) { |
1127 ASSERT(debugger()->PauseEvent() == NULL); | 1137 ASSERT(debugger()->PauseEvent() == NULL); |
1128 DebuggerEvent pauseEvent(this, DebuggerEvent::kIsolateShutdown); | 1138 DebuggerEvent pauseEvent(this, DebuggerEvent::kIsolateShutdown); |
(...skipping 174 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1303 | 1313 |
1304 | 1314 |
1305 template<class T> | 1315 template<class T> |
1306 T* Isolate::AllocateReusableHandle() { | 1316 T* Isolate::AllocateReusableHandle() { |
1307 T* handle = reinterpret_cast<T*>(reusable_handles_.AllocateScopedHandle()); | 1317 T* handle = reinterpret_cast<T*>(reusable_handles_.AllocateScopedHandle()); |
1308 T::initializeHandle(handle, T::null()); | 1318 T::initializeHandle(handle, T::null()); |
1309 return handle; | 1319 return handle; |
1310 } | 1320 } |
1311 | 1321 |
1312 | 1322 |
1313 IsolateSpawnState::IsolateSpawnState(const Function& func) | 1323 static uint8_t* allocator(uint8_t* ptr, intptr_t old_size, intptr_t new_size) { |
| 1324 void* new_ptr = realloc(reinterpret_cast<void*>(ptr), new_size); |
| 1325 return reinterpret_cast<uint8_t*>(new_ptr); |
| 1326 } |
| 1327 |
| 1328 |
| 1329 static void SerializeObject(const Instance& obj, |
| 1330 uint8_t** obj_data, |
| 1331 intptr_t* obj_len) { |
| 1332 MessageWriter writer(obj_data, &allocator); |
| 1333 writer.WriteMessage(obj); |
| 1334 *obj_len = writer.BytesWritten(); |
| 1335 } |
| 1336 |
| 1337 |
| 1338 static RawInstance* DeserializeObject(Isolate* isolate, |
| 1339 uint8_t* obj_data, |
| 1340 intptr_t obj_len) { |
| 1341 if (obj_data == NULL) { |
| 1342 return Instance::null(); |
| 1343 } |
| 1344 SnapshotReader reader(obj_data, obj_len, Snapshot::kMessage, isolate); |
| 1345 const Object& obj = Object::Handle(isolate, reader.ReadObject()); |
| 1346 ASSERT(!obj.IsError()); |
| 1347 Instance& instance = Instance::Handle(isolate); |
| 1348 instance ^= obj.raw(); // Can't use Instance::Cast because may be null. |
| 1349 return instance.raw(); |
| 1350 } |
| 1351 |
| 1352 |
| 1353 IsolateSpawnState::IsolateSpawnState(Dart_Port parent_port, |
| 1354 const Function& func, |
| 1355 const Instance& message) |
1314 : isolate_(NULL), | 1356 : isolate_(NULL), |
| 1357 parent_port_(parent_port), |
1315 script_url_(NULL), | 1358 script_url_(NULL), |
1316 library_url_(NULL), | 1359 library_url_(NULL), |
1317 class_name_(NULL), | 1360 class_name_(NULL), |
1318 function_name_(NULL), | 1361 function_name_(NULL), |
1319 exception_callback_name_(NULL) { | 1362 exception_callback_name_(NULL), |
| 1363 serialized_args_(NULL), |
| 1364 serialized_args_len_(0), |
| 1365 serialized_message_(NULL), |
| 1366 serialized_message_len_(0) { |
1320 script_url_ = NULL; | 1367 script_url_ = NULL; |
1321 const Class& cls = Class::Handle(func.Owner()); | 1368 const Class& cls = Class::Handle(func.Owner()); |
1322 const Library& lib = Library::Handle(cls.library()); | 1369 const Library& lib = Library::Handle(cls.library()); |
1323 const String& lib_url = String::Handle(lib.url()); | 1370 const String& lib_url = String::Handle(lib.url()); |
1324 library_url_ = strdup(lib_url.ToCString()); | 1371 library_url_ = strdup(lib_url.ToCString()); |
1325 | 1372 |
1326 const String& func_name = String::Handle(func.name()); | 1373 const String& func_name = String::Handle(func.name()); |
1327 function_name_ = strdup(func_name.ToCString()); | 1374 function_name_ = strdup(func_name.ToCString()); |
1328 if (!cls.IsTopLevel()) { | 1375 if (!cls.IsTopLevel()) { |
1329 const String& class_name = String::Handle(cls.Name()); | 1376 const String& class_name = String::Handle(cls.Name()); |
1330 class_name_ = strdup(class_name.ToCString()); | 1377 class_name_ = strdup(class_name.ToCString()); |
1331 } | 1378 } |
1332 exception_callback_name_ = strdup("_unhandledExceptionCallback"); | 1379 exception_callback_name_ = strdup("_unhandledExceptionCallback"); |
| 1380 SerializeObject(message, &serialized_message_, &serialized_message_len_); |
1333 } | 1381 } |
1334 | 1382 |
1335 | 1383 |
1336 IsolateSpawnState::IsolateSpawnState(const char* script_url) | 1384 IsolateSpawnState::IsolateSpawnState(Dart_Port parent_port, |
| 1385 const char* script_url, |
| 1386 const Instance& args, |
| 1387 const Instance& message) |
1337 : isolate_(NULL), | 1388 : isolate_(NULL), |
| 1389 parent_port_(parent_port), |
1338 library_url_(NULL), | 1390 library_url_(NULL), |
1339 class_name_(NULL), | 1391 class_name_(NULL), |
1340 function_name_(NULL), | 1392 function_name_(NULL), |
1341 exception_callback_name_(NULL) { | 1393 exception_callback_name_(NULL), |
| 1394 serialized_args_(NULL), |
| 1395 serialized_args_len_(0), |
| 1396 serialized_message_(NULL), |
| 1397 serialized_message_len_(0) { |
1342 script_url_ = strdup(script_url); | 1398 script_url_ = strdup(script_url); |
1343 library_url_ = NULL; | 1399 library_url_ = NULL; |
1344 function_name_ = strdup("main"); | 1400 function_name_ = strdup("main"); |
1345 exception_callback_name_ = strdup("_unhandledExceptionCallback"); | 1401 exception_callback_name_ = strdup("_unhandledExceptionCallback"); |
| 1402 SerializeObject(args, &serialized_args_, &serialized_args_len_); |
| 1403 SerializeObject(message, &serialized_message_, &serialized_message_len_); |
1346 } | 1404 } |
1347 | 1405 |
1348 | 1406 |
1349 IsolateSpawnState::~IsolateSpawnState() { | 1407 IsolateSpawnState::~IsolateSpawnState() { |
1350 free(script_url_); | 1408 free(script_url_); |
1351 free(library_url_); | 1409 free(library_url_); |
1352 free(function_name_); | 1410 free(function_name_); |
1353 free(class_name_); | 1411 free(class_name_); |
1354 free(exception_callback_name_); | 1412 free(exception_callback_name_); |
| 1413 free(serialized_args_); |
| 1414 free(serialized_message_); |
1355 } | 1415 } |
1356 | 1416 |
1357 | 1417 |
1358 RawObject* IsolateSpawnState::ResolveFunction() { | 1418 RawObject* IsolateSpawnState::ResolveFunction() { |
1359 // Resolve the library. | 1419 // Resolve the library. |
1360 Library& lib = Library::Handle(); | 1420 Library& lib = Library::Handle(); |
1361 if (library_url()) { | 1421 if (library_url()) { |
1362 const String& lib_url = String::Handle(String::New(library_url())); | 1422 const String& lib_url = String::Handle(String::New(library_url())); |
1363 lib = Library::LookupLibrary(lib_url); | 1423 lib = Library::LookupLibrary(lib_url); |
1364 if (lib.IsNull() || lib.IsError()) { | 1424 if (lib.IsNull() || lib.IsError()) { |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1401 const String& msg = String::Handle(String::NewFormatted( | 1461 const String& msg = String::Handle(String::NewFormatted( |
1402 "Unable to resolve static method '%s.%s' in library '%s'.", | 1462 "Unable to resolve static method '%s.%s' in library '%s'.", |
1403 class_name(), function_name(), | 1463 class_name(), function_name(), |
1404 (library_url() != NULL ? library_url() : script_url()))); | 1464 (library_url() != NULL ? library_url() : script_url()))); |
1405 return LanguageError::New(msg); | 1465 return LanguageError::New(msg); |
1406 } | 1466 } |
1407 return func.raw(); | 1467 return func.raw(); |
1408 } | 1468 } |
1409 | 1469 |
1410 | 1470 |
| 1471 RawInstance* IsolateSpawnState::BuildArgs() { |
| 1472 return DeserializeObject(isolate_, serialized_args_, serialized_args_len_); |
| 1473 } |
| 1474 |
| 1475 |
| 1476 RawInstance* IsolateSpawnState::BuildMessage() { |
| 1477 return DeserializeObject(isolate_, |
| 1478 serialized_message_, serialized_message_len_); |
| 1479 } |
| 1480 |
| 1481 |
1411 void IsolateSpawnState::Cleanup() { | 1482 void IsolateSpawnState::Cleanup() { |
1412 SwitchIsolateScope switch_scope(I); | 1483 SwitchIsolateScope switch_scope(I); |
1413 Dart::ShutdownIsolate(); | 1484 Dart::ShutdownIsolate(); |
1414 } | 1485 } |
1415 | 1486 |
1416 } // namespace dart | 1487 } // namespace dart |
OLD | NEW |