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