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 "include/dart_api.h" | 5 #include "include/dart_api.h" |
6 #include "include/dart_mirrors_api.h" | 6 #include "include/dart_mirrors_api.h" |
7 #include "include/dart_native_api.h" | 7 #include "include/dart_native_api.h" |
8 | 8 |
9 #include "platform/assert.h" | 9 #include "platform/assert.h" |
10 #include "vm/class_finalizer.h" | 10 #include "vm/class_finalizer.h" |
(...skipping 374 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
385 } | 385 } |
386 if (ClassFinalizer::ProcessPendingClasses()) { | 386 if (ClassFinalizer::ProcessPendingClasses()) { |
387 return Api::Success(); | 387 return Api::Success(); |
388 } | 388 } |
389 ASSERT(isolate->object_store()->sticky_error() != Object::null()); | 389 ASSERT(isolate->object_store()->sticky_error() != Object::null()); |
390 return Api::NewHandle(isolate, isolate->object_store()->sticky_error()); | 390 return Api::NewHandle(isolate, isolate->object_store()->sticky_error()); |
391 } | 391 } |
392 | 392 |
393 | 393 |
394 Dart_Isolate Api::CastIsolate(Isolate* isolate) { | 394 Dart_Isolate Api::CastIsolate(Isolate* isolate) { |
395 return reinterpret_cast<Dart_Isolate>(isolate); | 395 if (isolate == NULL) { |
| 396 return DART_ILLEGAL_ISOLATE; |
| 397 } |
| 398 return static_cast<Dart_Isolate>(isolate->main_port()); |
396 } | 399 } |
397 | 400 |
398 | 401 |
| 402 Isolate* Api::CastIsolate(Dart_Isolate isolate) { |
| 403 return PortMap::GetIsolate(static_cast<Dart_Port>(isolate)); |
| 404 } |
| 405 |
| 406 |
399 Dart_Handle Api::NewError(const char* format, ...) { | 407 Dart_Handle Api::NewError(const char* format, ...) { |
400 Isolate* isolate = Isolate::Current(); | 408 Isolate* isolate = Isolate::Current(); |
401 DARTSCOPE(isolate); | 409 DARTSCOPE(isolate); |
402 CHECK_CALLBACK_STATE(isolate); | 410 CHECK_CALLBACK_STATE(isolate); |
403 | 411 |
404 va_list args; | 412 va_list args; |
405 va_start(args, format); | 413 va_start(args, format); |
406 intptr_t len = OS::VSNPrint(NULL, 0, format, args); | 414 intptr_t len = OS::VSNPrint(NULL, 0, format, args); |
407 va_end(args); | 415 va_end(args); |
408 | 416 |
(...skipping 890 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1299 const Error& error_obj = | 1307 const Error& error_obj = |
1300 Error::Handle(isolate, | 1308 Error::Handle(isolate, |
1301 Dart::InitializeIsolate(snapshot, callback_data)); | 1309 Dart::InitializeIsolate(snapshot, callback_data)); |
1302 if (error_obj.IsNull()) { | 1310 if (error_obj.IsNull()) { |
1303 #if defined(DART_NO_SNAPSHOT) | 1311 #if defined(DART_NO_SNAPSHOT) |
1304 if (FLAG_check_function_fingerprints) { | 1312 if (FLAG_check_function_fingerprints) { |
1305 Library::CheckFunctionFingerprints(); | 1313 Library::CheckFunctionFingerprints(); |
1306 } | 1314 } |
1307 #endif // defined(DART_NO_SNAPSHOT). | 1315 #endif // defined(DART_NO_SNAPSHOT). |
1308 START_TIMER(isolate, time_total_runtime); | 1316 START_TIMER(isolate, time_total_runtime); |
1309 return reinterpret_cast<Dart_Isolate>(isolate); | 1317 return Api::CastIsolate(isolate); |
1310 } | 1318 } |
1311 *error = strdup(error_obj.ToErrorCString()); | 1319 *error = strdup(error_obj.ToErrorCString()); |
1312 } | 1320 } |
1313 Dart::ShutdownIsolate(); | 1321 Dart::ShutdownIsolate(); |
1314 return reinterpret_cast<Dart_Isolate>(NULL); | 1322 return DART_ILLEGAL_ISOLATE; |
1315 } | 1323 } |
1316 | 1324 |
1317 | 1325 |
1318 DART_EXPORT void Dart_ShutdownIsolate() { | 1326 DART_EXPORT void Dart_ShutdownIsolate() { |
1319 Isolate* isolate = Isolate::Current(); | 1327 Isolate* isolate = Isolate::Current(); |
1320 CHECK_ISOLATE(isolate); | 1328 CHECK_ISOLATE(isolate); |
1321 { | 1329 { |
1322 StackZone zone(isolate); | 1330 StackZone zone(isolate); |
1323 HandleScope handle_scope(isolate); | 1331 HandleScope handle_scope(isolate); |
1324 Dart::RunShutdownCallback(); | 1332 Dart::RunShutdownCallback(); |
(...skipping 10 matching lines...) Expand all Loading... |
1335 | 1343 |
1336 DART_EXPORT void* Dart_CurrentIsolateData() { | 1344 DART_EXPORT void* Dart_CurrentIsolateData() { |
1337 Isolate* isolate = Isolate::Current(); | 1345 Isolate* isolate = Isolate::Current(); |
1338 CHECK_ISOLATE(isolate); | 1346 CHECK_ISOLATE(isolate); |
1339 return isolate->init_callback_data(); | 1347 return isolate->init_callback_data(); |
1340 } | 1348 } |
1341 | 1349 |
1342 | 1350 |
1343 DART_EXPORT void* Dart_IsolateData(Dart_Isolate isolate) { | 1351 DART_EXPORT void* Dart_IsolateData(Dart_Isolate isolate) { |
1344 TRACE_API_CALL(CURRENT_FUNC); | 1352 TRACE_API_CALL(CURRENT_FUNC); |
1345 if (isolate == NULL) { | 1353 Isolate* iso = Api::CastIsolate(isolate); |
1346 FATAL1("%s expects argument 'isolate' to be non-null.", CURRENT_FUNC); | 1354 if (iso == NULL) { |
| 1355 return NULL; |
1347 } | 1356 } |
1348 // TODO(16615): Validate isolate parameter. | |
1349 Isolate* iso = reinterpret_cast<Isolate*>(isolate); | |
1350 return iso->init_callback_data(); | 1357 return iso->init_callback_data(); |
1351 } | 1358 } |
1352 | 1359 |
1353 | 1360 |
1354 DART_EXPORT Dart_Handle Dart_DebugName() { | 1361 DART_EXPORT Dart_Handle Dart_DebugName() { |
1355 Isolate* isolate = Isolate::Current(); | 1362 Isolate* isolate = Isolate::Current(); |
1356 DARTSCOPE(isolate); | 1363 DARTSCOPE(isolate); |
1357 return Api::NewHandle(isolate, String::New(isolate->name())); | 1364 return Api::NewHandle(isolate, String::New(isolate->name())); |
1358 } | 1365 } |
1359 | 1366 |
1360 | 1367 |
1361 | 1368 |
1362 DART_EXPORT void Dart_EnterIsolate(Dart_Isolate isolate) { | 1369 DART_EXPORT bool Dart_EnterIsolate(Dart_Isolate isolate) { |
1363 CHECK_NO_ISOLATE(Isolate::Current()); | 1370 CHECK_NO_ISOLATE(Isolate::Current()); |
1364 // TODO(16615): Validate isolate parameter. | 1371 Isolate* iso = Api::CastIsolate(isolate); |
1365 Isolate* iso = reinterpret_cast<Isolate*>(isolate); | 1372 if (iso == NULL) { |
| 1373 return false; |
| 1374 } |
1366 if (iso->mutator_thread() != NULL) { | 1375 if (iso->mutator_thread() != NULL) { |
1367 FATAL("Multiple mutators within one isolate is not supported."); | 1376 FATAL("Multiple mutators within one isolate is not supported."); |
| 1377 return false; |
1368 } | 1378 } |
1369 Thread::EnsureInit(); | 1379 Thread::EnsureInit(); |
1370 Thread::EnterIsolate(iso); | 1380 Thread::EnterIsolate(iso); |
| 1381 return true; |
1371 } | 1382 } |
1372 | 1383 |
1373 | 1384 |
1374 DART_EXPORT void Dart_IsolateBlocked() { | 1385 DART_EXPORT void Dart_IsolateBlocked() { |
1375 Isolate* isolate = Isolate::Current(); | 1386 Isolate* isolate = Isolate::Current(); |
1376 CHECK_ISOLATE(isolate); | 1387 CHECK_ISOLATE(isolate); |
1377 IsolateProfilerData* profiler_data = isolate->profiler_data(); | 1388 IsolateProfilerData* profiler_data = isolate->profiler_data(); |
1378 if (profiler_data == NULL) { | 1389 if (profiler_data == NULL) { |
1379 return; | 1390 return; |
1380 } | 1391 } |
(...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1477 Api::NewError("%s expects the isolate to have a script loaded in it.", | 1488 Api::NewError("%s expects the isolate to have a script loaded in it.", |
1478 CURRENT_FUNC); | 1489 CURRENT_FUNC); |
1479 } | 1490 } |
1480 ScriptSnapshotWriter writer(buffer, ApiReallocate); | 1491 ScriptSnapshotWriter writer(buffer, ApiReallocate); |
1481 writer.WriteScriptSnapshot(library); | 1492 writer.WriteScriptSnapshot(library); |
1482 *size = writer.BytesWritten(); | 1493 *size = writer.BytesWritten(); |
1483 return Api::Success(); | 1494 return Api::Success(); |
1484 } | 1495 } |
1485 | 1496 |
1486 | 1497 |
1487 DART_EXPORT void Dart_InterruptIsolate(Dart_Isolate isolate) { | 1498 DART_EXPORT bool Dart_InterruptIsolate(Dart_Isolate isolate) { |
1488 TRACE_API_CALL(CURRENT_FUNC); | 1499 TRACE_API_CALL(CURRENT_FUNC); |
1489 if (isolate == NULL) { | 1500 Isolate* iso = Api::CastIsolate(isolate); |
1490 FATAL1("%s expects argument 'isolate' to be non-null.", CURRENT_FUNC); | 1501 if (iso == NULL) { |
| 1502 return false; |
1491 } | 1503 } |
1492 // TODO(16615): Validate isolate parameter. | |
1493 Isolate* iso = reinterpret_cast<Isolate*>(isolate); | |
1494 iso->ScheduleInterrupts(Isolate::kApiInterrupt); | 1504 iso->ScheduleInterrupts(Isolate::kApiInterrupt); |
1495 // Can't use Dart_Post() since there isn't a current isolate. | 1505 // Can't use Dart_Post() since there isn't a current isolate. |
1496 Dart_CObject api_null = { Dart_CObject_kNull , { 0 } }; | 1506 Dart_CObject api_null = { Dart_CObject_kNull , { 0 } }; |
1497 Dart_PostCObject(iso->main_port(), &api_null); | 1507 return Dart_PostCObject(iso->main_port(), &api_null); |
1498 } | 1508 } |
1499 | 1509 |
1500 | 1510 |
1501 DART_EXPORT bool Dart_IsolateMakeRunnable(Dart_Isolate isolate) { | 1511 DART_EXPORT bool Dart_IsolateMakeRunnable(Dart_Isolate isolate) { |
1502 CHECK_NO_ISOLATE(Isolate::Current()); | 1512 CHECK_NO_ISOLATE(Isolate::Current()); |
1503 if (isolate == NULL) { | 1513 Isolate* iso = Api::CastIsolate(isolate); |
1504 FATAL1("%s expects argument 'isolate' to be non-null.", CURRENT_FUNC); | 1514 if (iso == NULL) { |
| 1515 return false; |
1505 } | 1516 } |
1506 // TODO(16615): Validate isolate parameter. | |
1507 Isolate* iso = reinterpret_cast<Isolate*>(isolate); | |
1508 if (iso->object_store()->root_library() == Library::null()) { | 1517 if (iso->object_store()->root_library() == Library::null()) { |
1509 // The embedder should have called Dart_LoadScript by now. | 1518 // The embedder should have called Dart_LoadScript by now. |
1510 return false; | 1519 return false; |
1511 } | 1520 } |
1512 return iso->MakeRunnable(); | 1521 return iso->MakeRunnable(); |
1513 } | 1522 } |
1514 | 1523 |
1515 | 1524 |
1516 // --- Messages and Ports --- | 1525 // --- Messages and Ports --- |
1517 | 1526 |
(...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1612 | 1621 |
1613 static uint8_t* allocator(uint8_t* ptr, intptr_t old_size, intptr_t new_size) { | 1622 static uint8_t* allocator(uint8_t* ptr, intptr_t old_size, intptr_t new_size) { |
1614 void* new_ptr = realloc(reinterpret_cast<void*>(ptr), new_size); | 1623 void* new_ptr = realloc(reinterpret_cast<void*>(ptr), new_size); |
1615 return reinterpret_cast<uint8_t*>(new_ptr); | 1624 return reinterpret_cast<uint8_t*>(new_ptr); |
1616 } | 1625 } |
1617 | 1626 |
1618 | 1627 |
1619 DART_EXPORT bool Dart_Post(Dart_Port port_id, Dart_Handle handle) { | 1628 DART_EXPORT bool Dart_Post(Dart_Port port_id, Dart_Handle handle) { |
1620 Isolate* isolate = Isolate::Current(); | 1629 Isolate* isolate = Isolate::Current(); |
1621 DARTSCOPE(isolate); | 1630 DARTSCOPE(isolate); |
1622 if (port_id == ILLEGAL_PORT) { | 1631 if (port_id == DART_ILLEGAL_PORT) { |
1623 return false; | 1632 return false; |
1624 } | 1633 } |
1625 const Object& object = Object::Handle(isolate, Api::UnwrapHandle(handle)); | 1634 const Object& object = Object::Handle(isolate, Api::UnwrapHandle(handle)); |
1626 uint8_t* data = NULL; | 1635 uint8_t* data = NULL; |
1627 MessageWriter writer(&data, &allocator, false); | 1636 MessageWriter writer(&data, &allocator, false); |
1628 writer.WriteMessage(object); | 1637 writer.WriteMessage(object); |
1629 intptr_t len = writer.BytesWritten(); | 1638 intptr_t len = writer.BytesWritten(); |
1630 return PortMap::PostMessage(new Message( | 1639 return PortMap::PostMessage(new Message( |
1631 port_id, data, len, Message::kNormalPriority)); | 1640 port_id, data, len, Message::kNormalPriority)); |
1632 } | 1641 } |
1633 | 1642 |
1634 | 1643 |
1635 DART_EXPORT Dart_Handle Dart_NewSendPort(Dart_Port port_id) { | 1644 DART_EXPORT Dart_Handle Dart_NewSendPort(Dart_Port port_id) { |
1636 Isolate* isolate = Isolate::Current(); | 1645 Isolate* isolate = Isolate::Current(); |
1637 DARTSCOPE(isolate); | 1646 DARTSCOPE(isolate); |
1638 CHECK_CALLBACK_STATE(isolate); | 1647 CHECK_CALLBACK_STATE(isolate); |
1639 if (port_id == ILLEGAL_PORT) { | 1648 if (port_id == DART_ILLEGAL_PORT) { |
1640 return Api::NewError("%s: illegal port_id %" Pd64 ".", | 1649 return Api::NewError("%s: illegal port_id %" Pd64 ".", |
1641 CURRENT_FUNC, | 1650 CURRENT_FUNC, |
1642 port_id); | 1651 port_id); |
1643 } | 1652 } |
1644 return Api::NewHandle(isolate, SendPort::New(port_id)); | 1653 return Api::NewHandle(isolate, SendPort::New(port_id)); |
1645 } | 1654 } |
1646 | 1655 |
1647 | 1656 |
1648 DART_EXPORT Dart_Handle Dart_SendPortGetId(Dart_Handle port, | 1657 DART_EXPORT Dart_Handle Dart_SendPortGetId(Dart_Handle port, |
1649 Dart_Port* port_id) { | 1658 Dart_Port* port_id) { |
(...skipping 3894 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
5544 RawObject* raw_obj = obj.raw(); | 5553 RawObject* raw_obj = obj.raw(); |
5545 isolate->heap()->SetPeer(raw_obj, peer); | 5554 isolate->heap()->SetPeer(raw_obj, peer); |
5546 } | 5555 } |
5547 return Api::Success(); | 5556 return Api::Success(); |
5548 } | 5557 } |
5549 | 5558 |
5550 | 5559 |
5551 // --- Service support --- | 5560 // --- Service support --- |
5552 | 5561 |
5553 DART_EXPORT bool Dart_IsServiceIsolate(Dart_Isolate isolate) { | 5562 DART_EXPORT bool Dart_IsServiceIsolate(Dart_Isolate isolate) { |
5554 Isolate* iso = reinterpret_cast<Isolate*>(isolate); | 5563 Isolate* iso = Api::CastIsolate(isolate); |
| 5564 if (iso == NULL) { |
| 5565 return false; |
| 5566 } |
5555 return ServiceIsolate::IsServiceIsolate(iso); | 5567 return ServiceIsolate::IsServiceIsolate(iso); |
5556 } | 5568 } |
5557 | 5569 |
5558 | 5570 |
5559 DART_EXPORT Dart_Port Dart_ServiceWaitForLoadPort() { | 5571 DART_EXPORT Dart_Port Dart_ServiceWaitForLoadPort() { |
5560 return ServiceIsolate::WaitForLoadPort(); | 5572 return ServiceIsolate::WaitForLoadPort(); |
5561 } | 5573 } |
5562 | 5574 |
5563 | 5575 |
5564 DART_EXPORT void Dart_RegisterIsolateServiceRequestCallback( | 5576 DART_EXPORT void Dart_RegisterIsolateServiceRequestCallback( |
5565 const char* name, | 5577 const char* name, |
5566 Dart_ServiceRequestCallback callback, | 5578 Dart_ServiceRequestCallback callback, |
5567 void* user_data) { | 5579 void* user_data) { |
5568 Service::RegisterIsolateEmbedderCallback(name, callback, user_data); | 5580 Service::RegisterIsolateEmbedderCallback(name, callback, user_data); |
5569 } | 5581 } |
5570 | 5582 |
5571 | 5583 |
5572 DART_EXPORT void Dart_RegisterRootServiceRequestCallback( | 5584 DART_EXPORT void Dart_RegisterRootServiceRequestCallback( |
5573 const char* name, | 5585 const char* name, |
5574 Dart_ServiceRequestCallback callback, | 5586 Dart_ServiceRequestCallback callback, |
5575 void* user_data) { | 5587 void* user_data) { |
5576 Service::RegisterRootEmbedderCallback(name, callback, user_data); | 5588 Service::RegisterRootEmbedderCallback(name, callback, user_data); |
5577 } | 5589 } |
5578 | 5590 |
5579 } // namespace dart | 5591 } // namespace dart |
OLD | NEW |