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