Chromium Code Reviews| OLD | NEW |
|---|---|
| 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/json.h" | 7 #include "platform/json.h" |
| 8 #include "platform/utils.h" | 8 #include "platform/utils.h" |
| 9 #include "vm/class_finalizer.h" | 9 #include "vm/class_finalizer.h" |
| 10 #include "vm/dart_api_impl.h" | 10 #include "vm/dart_api_impl.h" |
| (...skipping 1400 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1411 | 1411 |
| 1412 | 1412 |
| 1413 static void WeakPersistentHandlePeerFinalizer(Dart_Handle handle, void* peer) { | 1413 static void WeakPersistentHandlePeerFinalizer(Dart_Handle handle, void* peer) { |
| 1414 *static_cast<int*>(peer) = 42; | 1414 *static_cast<int*>(peer) = 42; |
| 1415 } | 1415 } |
| 1416 | 1416 |
| 1417 | 1417 |
| 1418 TEST_CASE(WeakPersistentHandleCallback) { | 1418 TEST_CASE(WeakPersistentHandleCallback) { |
| 1419 Dart_Handle weak_ref = Dart_Null(); | 1419 Dart_Handle weak_ref = Dart_Null(); |
| 1420 EXPECT(Dart_IsNull(weak_ref)); | 1420 EXPECT(Dart_IsNull(weak_ref)); |
| 1421 int* peer = new int(); | 1421 int peer = 0; |
| 1422 { | 1422 { |
| 1423 Dart_EnterScope(); | 1423 Dart_EnterScope(); |
| 1424 Dart_Handle obj = Dart_NewString("new string"); | 1424 Dart_Handle obj = Dart_NewString("new string"); |
| 1425 EXPECT_VALID(obj); | 1425 EXPECT_VALID(obj); |
| 1426 weak_ref = Dart_NewWeakPersistentHandle(obj, peer, | 1426 weak_ref = Dart_NewWeakPersistentHandle(obj, &peer, |
| 1427 WeakPersistentHandlePeerFinalizer); | 1427 WeakPersistentHandlePeerFinalizer); |
| 1428 Dart_ExitScope(); | 1428 Dart_ExitScope(); |
| 1429 } | 1429 } |
| 1430 EXPECT_VALID(weak_ref); | 1430 EXPECT_VALID(weak_ref); |
| 1431 EXPECT(*peer == 0); | 1431 EXPECT(peer == 0); |
| 1432 Isolate::Current()->heap()->CollectGarbage(Heap::kOld); | 1432 Isolate::Current()->heap()->CollectGarbage(Heap::kOld); |
| 1433 EXPECT(*peer == 0); | 1433 EXPECT(peer == 0); |
| 1434 GCTestHelper::CollectNewSpace(Heap::kIgnoreApiCallbacks); | 1434 GCTestHelper::CollectNewSpace(Heap::kIgnoreApiCallbacks); |
| 1435 EXPECT(*peer == 42); | 1435 EXPECT(peer == 42); |
| 1436 delete peer; | |
| 1437 Dart_DeletePersistentHandle(weak_ref); | 1436 Dart_DeletePersistentHandle(weak_ref); |
| 1438 } | 1437 } |
| 1439 | 1438 |
| 1440 | 1439 |
| 1440 TEST_CASE(WeakPersistentHandleNoCallback) { | |
| 1441 Dart_Handle weak_ref = Dart_Null(); | |
| 1442 EXPECT(Dart_IsNull(weak_ref)); | |
| 1443 int peer = 0; | |
| 1444 { | |
| 1445 Dart_EnterScope(); | |
| 1446 Dart_Handle obj = Dart_NewString("new string"); | |
| 1447 EXPECT_VALID(obj); | |
| 1448 weak_ref = Dart_NewWeakPersistentHandle(obj, &peer, | |
| 1449 WeakPersistentHandlePeerFinalizer); | |
| 1450 Dart_ExitScope(); | |
| 1451 } | |
| 1452 Dart_DeletePersistentHandle(weak_ref); | |
|
hausner
2012/09/07 22:19:42
Maybe add a comment that the finalizer should not
cshapiro
2012/09/07 22:32:22
Thanks, that is a good idea. Done.
| |
| 1453 EXPECT_VALID(weak_ref); | |
| 1454 EXPECT(peer == 0); | |
| 1455 Isolate::Current()->heap()->CollectGarbage(Heap::kOld); | |
| 1456 EXPECT(peer == 0); | |
| 1457 GCTestHelper::CollectNewSpace(Heap::kIgnoreApiCallbacks); | |
| 1458 EXPECT(peer == 0); | |
| 1459 } | |
| 1460 | |
| 1461 | |
| 1441 TEST_CASE(ObjectGroups) { | 1462 TEST_CASE(ObjectGroups) { |
| 1442 Dart_Handle strong = Dart_Null(); | 1463 Dart_Handle strong = Dart_Null(); |
| 1443 EXPECT(Dart_IsNull(strong)); | 1464 EXPECT(Dart_IsNull(strong)); |
| 1444 | 1465 |
| 1445 Dart_Handle weak1 = Dart_Null(); | 1466 Dart_Handle weak1 = Dart_Null(); |
| 1446 EXPECT(Dart_IsNull(weak1)); | 1467 EXPECT(Dart_IsNull(weak1)); |
| 1447 | 1468 |
| 1448 Dart_Handle weak2 = Dart_Null(); | 1469 Dart_Handle weak2 = Dart_Null(); |
| 1449 EXPECT(Dart_IsNull(weak2)); | 1470 EXPECT(Dart_IsNull(weak2)); |
| 1450 | 1471 |
| (...skipping 5041 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 6492 EXPECT(Dart_IsString(str)); | 6513 EXPECT(Dart_IsString(str)); |
| 6493 len = -1; | 6514 len = -1; |
| 6494 EXPECT_VALID(Dart_StringLength(str, &len)); | 6515 EXPECT_VALID(Dart_StringLength(str, &len)); |
| 6495 EXPECT_EQ(0, len); | 6516 EXPECT_EQ(0, len); |
| 6496 } | 6517 } |
| 6497 | 6518 |
| 6498 | 6519 |
| 6499 #endif // defined(TARGET_ARCH_IA32) || defined(TARGET_ARCH_X64). | 6520 #endif // defined(TARGET_ARCH_IA32) || defined(TARGET_ARCH_X64). |
| 6500 | 6521 |
| 6501 } // namespace dart | 6522 } // namespace dart |
| OLD | NEW |