1 // Copyright 2020 The PDFium Authors 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #include <utility> 6 7 #include "fxjs/gc/heap.h" 8 #include "testing/fxgc_unittest.h" 9 #include "testing/gtest/include/gtest/gtest.h" 10 #include "v8/include/cppgc/member.h" 11 #include "v8/include/cppgc/persistent.h" 12 13 namespace { 14 15 class HeapObject : public cppgc::GarbageCollected<HeapObject> { 16 public: 17 CONSTRUCT_VIA_MAKE_GARBAGE_COLLECTED; 18 Trace(cppgc::Visitor * visitor) const19 void Trace(cppgc::Visitor* visitor) const { 20 visitor->Trace(frick_); 21 visitor->Trace(frack_); 22 } 23 24 cppgc::Member<HeapObject> frick_; 25 cppgc::Member<HeapObject> frack_; 26 27 private: 28 HeapObject() = default; 29 }; 30 31 class CppObject { 32 public: 33 CppObject() = default; 34 35 cppgc::Persistent<HeapObject> click_; 36 cppgc::Persistent<HeapObject> clack_; 37 }; 38 39 } // namespace 40 41 class MoveUnitTest : public FXGCUnitTest {}; 42 TEST_F(MoveUnitTest,Member)43TEST_F(MoveUnitTest, Member) { 44 // Moving a Member<> leaves the moved-from object as null. 45 auto* obj = 46 cppgc::MakeGarbageCollected<HeapObject>(heap()->GetAllocationHandle()); 47 obj->frick_ = obj; 48 obj->frack_ = std::move(obj->frick_); 49 EXPECT_FALSE(obj->frick_); 50 EXPECT_EQ(obj, obj->frack_); 51 } 52 TEST_F(MoveUnitTest,Persistent)53TEST_F(MoveUnitTest, Persistent) { 54 // Moving a Persistent<> leaves the moved-from object as null. 55 auto* obj = 56 cppgc::MakeGarbageCollected<HeapObject>(heap()->GetAllocationHandle()); 57 CppObject outsider; 58 outsider.click_ = obj; 59 outsider.clack_ = std::move(outsider.click_); 60 EXPECT_FALSE(outsider.click_); 61 EXPECT_EQ(obj, outsider.clack_); 62 } 63