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

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

Issue 9531001: Implement weak references sets and provide an embedding API. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: minor changes to prepare for review Created 8 years, 9 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
OLDNEW
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2012, 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 "platform/assert.h" 6 #include "platform/assert.h"
7 #include "platform/utils.h" 7 #include "platform/utils.h"
8 #include "vm/class_finalizer.h" 8 #include "vm/class_finalizer.h"
9 #include "vm/dart_api_impl.h" 9 #include "vm/dart_api_impl.h"
10 #include "vm/dart_api_state.h" 10 #include "vm/dart_api_state.h"
(...skipping 1353 matching lines...) Expand 10 before | Expand all | Expand 10 after
1364 EXPECT_VALID(weak_ref); 1364 EXPECT_VALID(weak_ref);
1365 EXPECT(*peer == 0); 1365 EXPECT(*peer == 0);
1366 Isolate::Current()->heap()->CollectGarbage(Heap::kOld); 1366 Isolate::Current()->heap()->CollectGarbage(Heap::kOld);
1367 EXPECT(*peer == 0); 1367 EXPECT(*peer == 0);
1368 Isolate::Current()->heap()->CollectGarbage(Heap::kNew); 1368 Isolate::Current()->heap()->CollectGarbage(Heap::kNew);
1369 EXPECT(*peer == 42); 1369 EXPECT(*peer == 42);
1370 delete peer; 1370 delete peer;
1371 Dart_DeletePersistentHandle(weak_ref); 1371 Dart_DeletePersistentHandle(weak_ref);
1372 } 1372 }
1373 1373
1374
1375 TEST_CASE(ObjectGroups) {
1376 Dart_Handle strong = Dart_Null();
1377 EXPECT(Dart_IsNull(strong));
1378
1379 Dart_Handle weak1 = Dart_Null();
1380 EXPECT(Dart_IsNull(weak1));
1381
1382 Dart_Handle weak2 = Dart_Null();
1383 EXPECT(Dart_IsNull(weak2));
1384
1385 Dart_Handle weak3 = Dart_Null();
1386 EXPECT(Dart_IsNull(weak3));
1387
1388 Dart_Handle weak4 = Dart_Null();
1389 EXPECT(Dart_IsNull(weak4));
1390
1391 Dart_EnterScope();
1392 {
1393 DARTSCOPE(Isolate::Current());
1394 String& str = String::Handle();
1395
1396 str ^= String::New("strongly reachable", Heap::kOld);
1397 strong = Dart_NewPersistentHandle(Api::NewLocalHandle(str));
1398 EXPECT_VALID(strong);
1399 EXPECT(!Dart_IsNull(strong));
1400
1401 str ^= String::New("weakly reachable 1", Heap::kOld);
1402 weak1 = Dart_NewWeakPersistentHandle(Api::NewLocalHandle(str), NULL, NULL);
1403 EXPECT_VALID(weak1);
1404 EXPECT(!Dart_IsNull(weak1));
1405
1406 str ^= String::New("weakly reachable 2", Heap::kOld);
1407 weak2 = Dart_NewWeakPersistentHandle(Api::NewLocalHandle(str), NULL, NULL);
1408 EXPECT_VALID(weak2);
1409 EXPECT(!Dart_IsNull(weak2));
1410
1411 str ^= String::New("weakly reachable 3", Heap::kOld);
1412 weak3 = Dart_NewWeakPersistentHandle(Api::NewLocalHandle(str), NULL, NULL);
1413 EXPECT_VALID(weak3);
1414 EXPECT(!Dart_IsNull(weak3));
1415
1416 str ^= String::New("weakly reachable 4", Heap::kOld);
1417 weak4 = Dart_NewWeakPersistentHandle(Api::NewLocalHandle(str), NULL, NULL);
1418 EXPECT_VALID(weak4);
1419 EXPECT(!Dart_IsNull(weak4));
1420 }
1421 Dart_ExitScope();
1422
1423 EXPECT_VALID(strong);
1424
1425 EXPECT_VALID(weak1);
1426 EXPECT_VALID(weak2);
1427 EXPECT_VALID(weak3);
1428 EXPECT_VALID(weak4);
1429
1430 Isolate::Current()->heap()->CollectGarbage(Heap::kNew);
1431
1432 // New space collection should not affect old space objects
1433 EXPECT(!Dart_IsNull(weak1));
1434 EXPECT(!Dart_IsNull(weak2));
1435 EXPECT(!Dart_IsNull(weak3));
1436 EXPECT(!Dart_IsNull(weak4));
1437
1438 {
1439 Dart_Handle array1[] = { weak1, strong };
1440 EXPECT_VALID(Dart_NewWeakReferenceSet(array1, ARRAY_SIZE(array1),
1441 array1, ARRAY_SIZE(array1)));
1442
1443 Dart_Handle array2[] = { weak2, weak1 };
1444 EXPECT_VALID(Dart_NewWeakReferenceSet(array2, ARRAY_SIZE(array2),
1445 array2, ARRAY_SIZE(array2)));
1446
1447 Dart_Handle array3[] = { weak3, weak2 };
1448 EXPECT_VALID(Dart_NewWeakReferenceSet(array3, ARRAY_SIZE(array3),
1449 array3, ARRAY_SIZE(array3)));
1450
1451 Dart_Handle array4[] = { weak4, weak3 };
1452 EXPECT_VALID(Dart_NewWeakReferenceSet(array4, ARRAY_SIZE(array4),
1453 array4, ARRAY_SIZE(array4)));
1454
1455 Isolate::Current()->heap()->CollectGarbage(Heap::kOld);
1456 }
1457
1458 // All weak references should be preserved.
1459 EXPECT(!Dart_IsNull(weak1));
1460 EXPECT(!Dart_IsNull(weak2));
1461 EXPECT(!Dart_IsNull(weak3));
1462 EXPECT(!Dart_IsNull(weak4));
1463
1464 {
1465 Dart_Handle array1[] = { weak1, strong };
1466 EXPECT_VALID(Dart_NewWeakReferenceSet(array1, ARRAY_SIZE(array1),
1467 array1, ARRAY_SIZE(array1)));
1468
1469 Dart_Handle array2[] = { weak2, weak1 };
1470 EXPECT_VALID(Dart_NewWeakReferenceSet(array2, ARRAY_SIZE(array2),
1471 array2, ARRAY_SIZE(array2)));
1472
1473 Dart_Handle array3[] = { weak2 };
1474 EXPECT_VALID(Dart_NewWeakReferenceSet(array3, ARRAY_SIZE(array3),
1475 array3, ARRAY_SIZE(array3)));
1476
1477 // Strong reference to weak3 to retain weak3 and weak4.
1478 Dart_Handle weak3_strong_ref = Dart_NewPersistentHandle(weak3);
1479 EXPECT_VALID(weak3_strong_ref);
1480
1481 Dart_Handle array4[] = { weak4, weak3 };
1482 EXPECT_VALID(Dart_NewWeakReferenceSet(array4, ARRAY_SIZE(array4),
1483 array4, ARRAY_SIZE(array4)));
1484
1485 Isolate::Current()->heap()->CollectGarbage(Heap::kOld);
1486
1487 // Delete strong reference to weak3.
1488 Dart_DeletePersistentHandle(weak3_strong_ref);
1489 }
1490
1491 // All weak references should be preserved.
1492 EXPECT(!Dart_IsNull(weak1));
1493 EXPECT(!Dart_IsNull(weak2));
1494 EXPECT(!Dart_IsNull(weak3));
1495 EXPECT(!Dart_IsNull(weak4));
1496
1497 {
1498 Dart_Handle array1[] = { weak1, strong };
1499 EXPECT_VALID(Dart_NewWeakReferenceSet(array1, ARRAY_SIZE(array1),
1500 array1, ARRAY_SIZE(array1)));
1501
1502 Dart_Handle array2[] = { weak2, weak1 };
1503 EXPECT_VALID(Dart_NewWeakReferenceSet(array2, ARRAY_SIZE(array2),
1504 array2, ARRAY_SIZE(array2)));
1505
1506 Dart_Handle array3[] = { weak2 };
1507 EXPECT_VALID(Dart_NewWeakReferenceSet(array3, ARRAY_SIZE(array3),
1508 array3, ARRAY_SIZE(array3)));
1509
1510 Dart_Handle array4[] = { weak4, weak3 };
1511 EXPECT_VALID(Dart_NewWeakReferenceSet(array4, ARRAY_SIZE(array4),
1512 array4, ARRAY_SIZE(array4)));
1513
1514 Isolate::Current()->heap()->CollectGarbage(Heap::kOld);
1515 }
1516
1517 // Only weak1 and weak2 should be preserved.
1518 EXPECT(!Dart_IsNull(weak1));
1519 EXPECT(!Dart_IsNull(weak2));
1520 EXPECT(Dart_IsNull(weak3));
1521 EXPECT(Dart_IsNull(weak4));
1522
1523 {
1524 Dart_Handle array1[] = { weak1, strong };
1525 EXPECT_VALID(Dart_NewWeakReferenceSet(array1, ARRAY_SIZE(array1),
1526 array1, ARRAY_SIZE(array1)));
1527
1528 Dart_Handle array2[] = { weak2, weak3 };
1529 EXPECT_VALID(Dart_NewWeakReferenceSet(array2, ARRAY_SIZE(array2),
1530 array2, ARRAY_SIZE(array2)));
1531
1532 Isolate::Current()->heap()->CollectGarbage(Heap::kOld);
1533 }
1534
1535 // Only weak1 should be preserved, weak3 should not preserve weak2.
1536 EXPECT(!Dart_IsNull(weak1));
1537 EXPECT(Dart_IsNull(weak2));
1538 EXPECT(Dart_IsNull(weak3));
1539 EXPECT(Dart_IsNull(weak4));
siva 2012/03/02 18:30:33 weak3 and weak4 were already NULL before this test
cshapiro 2012/03/03 00:03:23 Yes, weak3 and weak4 were null before the GC on li
1540
1541 {
1542 Dart_Handle array1[] = { strong, weak2, weak3, weak4 };
1543 EXPECT_VALID(Dart_NewWeakReferenceSet(array1, ARRAY_SIZE(array1),
1544 array1, ARRAY_SIZE(array1)));
1545
1546 Dart_Handle array2[] = { weak1 };
1547 EXPECT_VALID(Dart_NewWeakReferenceSet(array2, ARRAY_SIZE(array2),
1548 array2, ARRAY_SIZE(array2)));
1549
1550 Isolate::Current()->heap()->CollectGarbage(Heap::kOld);
1551 }
1552
1553 // No weak references should be preserved.
siva 2012/03/02 18:30:33 Why should weak2, weak3 and weak4 not be preserved
cshapiro 2012/03/03 00:03:23 The are still null so they should remain null.
1554 EXPECT(Dart_IsNull(weak1));
1555 EXPECT(Dart_IsNull(weak2));
1556 EXPECT(Dart_IsNull(weak3));
1557 EXPECT(Dart_IsNull(weak4));
siva 2012/03/02 18:30:33 I am little confused here, weak3, weak4, weak2 wer
cshapiro 2012/03/03 00:03:23 Let me add some comments and see if that makes the
1558 }
1559
1560
1561 TEST_CASE(ImplicitReferences) {
1562 Dart_Handle strong = Dart_Null();
1563 EXPECT(Dart_IsNull(strong));
1564
1565 Dart_Handle weak1 = Dart_Null();
1566 EXPECT(Dart_IsNull(weak1));
1567
1568 Dart_Handle weak2 = Dart_Null();
1569 EXPECT(Dart_IsNull(weak2));
1570
1571 Dart_Handle weak3 = Dart_Null();
1572 EXPECT(Dart_IsNull(weak3));
1573
1574
1575 Dart_EnterScope();
1576 {
1577 DARTSCOPE(Isolate::Current());
1578 String& str = String::Handle();
1579
1580 str ^= String::New("strongly reachable", Heap::kOld);
1581 strong = Dart_NewPersistentHandle(Api::NewLocalHandle(str));
1582 EXPECT(!Dart_IsNull(strong));
1583 EXPECT_VALID(strong);
1584
1585 str ^= String::New("weakly reachable 1", Heap::kOld);
1586 weak1 = Dart_NewWeakPersistentHandle(Api::NewLocalHandle(str), NULL, NULL);
1587 EXPECT(!Dart_IsNull(weak1));
1588 EXPECT_VALID(weak1);
1589
1590 str ^= String::New("weakly reachable 2", Heap::kOld);
1591 weak2 = Dart_NewWeakPersistentHandle(Api::NewLocalHandle(str), NULL, NULL);
1592 EXPECT(!Dart_IsNull(weak2));
1593 EXPECT_VALID(weak2);
1594
1595 str ^= String::New("weakly reachable 3", Heap::kOld);
1596 weak3 = Dart_NewWeakPersistentHandle(Api::NewLocalHandle(str), NULL, NULL);
1597 EXPECT(!Dart_IsNull(weak3));
1598 EXPECT_VALID(weak3);
1599 }
1600 Dart_ExitScope();
1601
1602 EXPECT_VALID(strong);
1603
1604 EXPECT_VALID(weak1);
1605 EXPECT_VALID(weak2);
1606 EXPECT_VALID(weak3);
1607
1608 Isolate::Current()->heap()->CollectGarbage(Heap::kNew);
1609
1610 // New space collection should not affect old space objects
1611 EXPECT(!Dart_IsNull(weak1));
1612 EXPECT(!Dart_IsNull(weak2));
1613 EXPECT(!Dart_IsNull(weak3));
1614
1615 // A strongly referenced key should preserve all the values.
1616 {
1617 Dart_Handle keys[] = { strong };
1618 Dart_Handle values[] = { weak1, weak2, weak3 };
1619 EXPECT_VALID(Dart_NewWeakReferenceSet(keys, ARRAY_SIZE(keys),
1620 values, ARRAY_SIZE(values)));
1621
1622 Isolate::Current()->heap()->CollectGarbage(Heap::kOld);
1623 }
1624
1625 // All weak references should be preserved.
1626 EXPECT(!Dart_IsNull(weak1));
1627 EXPECT(!Dart_IsNull(weak2));
1628 EXPECT(!Dart_IsNull(weak3));
1629
1630 // Key membership does not imply a strong reference.
1631 {
1632 Dart_Handle keys[] = { strong, weak3 };
1633 Dart_Handle values[] = { weak1, weak2 };
1634 EXPECT_VALID(Dart_NewWeakReferenceSet(keys, ARRAY_SIZE(keys),
1635 values, ARRAY_SIZE(values)));
1636
1637 Isolate::Current()->heap()->CollectGarbage(Heap::kOld);
1638 }
1639
1640 // All weak references except weak3 should be preserved.
1641 EXPECT(!Dart_IsNull(weak1));
1642 EXPECT(!Dart_IsNull(weak2));
1643 EXPECT(Dart_IsNull(weak3));
1644 }
1645
1374 #endif 1646 #endif
1375 1647
1376 1648
1377 // Unit test for creating multiple scopes and local handles within them. 1649 // Unit test for creating multiple scopes and local handles within them.
1378 // Ensure that the local handles get all cleaned out when exiting the 1650 // Ensure that the local handles get all cleaned out when exiting the
1379 // scope. 1651 // scope.
1380 UNIT_TEST_CASE(LocalHandles) { 1652 UNIT_TEST_CASE(LocalHandles) {
1381 TestCase::CreateTestIsolate(); 1653 TestCase::CreateTestIsolate();
1382 Isolate* isolate = Isolate::Current(); 1654 Isolate* isolate = Isolate::Current();
1383 EXPECT(isolate != NULL); 1655 EXPECT(isolate != NULL);
(...skipping 2349 matching lines...) Expand 10 before | Expand all | Expand 10 after
3733 // We should have received the expected number of interrupts. 4005 // We should have received the expected number of interrupts.
3734 EXPECT_EQ(kInterruptCount, interrupt_count); 4006 EXPECT_EQ(kInterruptCount, interrupt_count);
3735 4007
3736 // Give the spawned thread enough time to properly exit. 4008 // Give the spawned thread enough time to properly exit.
3737 Isolate::SetInterruptCallback(saved); 4009 Isolate::SetInterruptCallback(saved);
3738 } 4010 }
3739 4011
3740 #endif // defined(TARGET_ARCH_IA32) || defined(TARGET_ARCH_X64). 4012 #endif // defined(TARGET_ARCH_IA32) || defined(TARGET_ARCH_X64).
3741 4013
3742 } // namespace dart 4014 } // namespace dart
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698