From 6268df392f49f03b52079e04c603155a665f70f6 Mon Sep 17 00:00:00 2001 From: yanzhiqi1 Date: Tue, 19 Aug 2025 11:36:05 +0800 Subject: [PATCH] modify JSFinalizationRegistry testcase Issue: #ICTLYJ Signed-off-by: yanzhiqi1 Change-Id: Ia0d2767952353a568007066790c2c4d04e23d6d3 --- ecmascript/dfx/hprof/tests/heap_dump_test.cpp | 48 +++++++++++-- ecmascript/dump.cpp | 70 ------------------- 2 files changed, 43 insertions(+), 75 deletions(-) diff --git a/ecmascript/dfx/hprof/tests/heap_dump_test.cpp b/ecmascript/dfx/hprof/tests/heap_dump_test.cpp index 577780c479..5c691b5eb1 100644 --- a/ecmascript/dfx/hprof/tests/heap_dump_test.cpp +++ b/ecmascript/dfx/hprof/tests/heap_dump_test.cpp @@ -16,6 +16,7 @@ #include #include #include "ecmascript/base/number_helper.h" +#include "ecmascript/builtins/builtins_finalization_registry.h" #include "ecmascript/dfx/hprof/heap_snapshot.h" #include "ecmascript/dfx/hprof/heap_profiler.h" #include "ecmascript/dfx/hprof/heap_root_visitor.h" @@ -59,6 +60,7 @@ namespace panda::test { using namespace panda::ecmascript; +using namespace panda::ecmascript::builtins; using ErrorType = base::ErrorType; class HeapDumpTest : public testing::Test { @@ -723,14 +725,50 @@ public: return jsWeakRef; } + static JSTaggedValue callbackTest() + { + return JSTaggedValue::Undefined(); + } + // JS_FINALIZATION_REGISTRY JSHandle NewJSFinalizationRegistry() { - JSHandle proto = instance->GetGlobalEnv()->GetFunctionPrototype(); - JSHandle jsFinalizationRegistryObject = - NewObject(JSFinalizationRegistry::SIZE, JSType::JS_FINALIZATION_REGISTRY, proto); - JSHandle jsFinalizationRegistry = - JSHandle::Cast(jsFinalizationRegistryObject); + auto factory = instance->GetFactory(); + auto env = instance->GetGlobalEnv(); + auto thread = instance->GetJSThread(); + + JSHandle finalRegFunc(env->GetBuiltinsFinalizationRegistryFunction()); + JSHandle callbackFunc = factory->NewJSFunction(env, reinterpret_cast(callbackTest)); + auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue(*finalRegFunc), 6); + ecmaRuntimeCallInfo->SetFunction(finalRegFunc.GetTaggedValue()); + ecmaRuntimeCallInfo->SetThis(JSTaggedValue::Undefined()); + ecmaRuntimeCallInfo->SetCallArg(0, callbackFunc.GetTaggedValue()); + + auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo); + JSTaggedValue finalizationRegistry = + BuiltinsFinalizationRegistry::FinalizationRegistryConstructor(ecmaRuntimeCallInfo); + JSHandle finalizationRegistryHandle(thread, finalizationRegistry); + TestHelper::TearDownFrame(thread, prev); + + JSHandle objectFunc = env->GetObjectFunction(); + JSHandle target( + factory->NewJSObjectByConstructor(JSHandle(objectFunc), objectFunc)); + JSHandle key(factory->NewFromASCII("key2")); + JSHandle value(thread, JSTaggedValue(2)); + JSObject::SetProperty(thread, target, key, value); + JSHandle heldValue(thread, JSTaggedValue(100)); + JSHandle unregisterToken = target; + + JSHandle cellRecord = factory->NewCellRecord(); + cellRecord->SetToWeakRefTarget(thread, target.GetTaggedValue()); + cellRecord->SetHeldValue(thread, heldValue); + JSHandle cell(cellRecord); + JSHandle array = JSHandle(CellRecordVector::Create(thread)); + array = CellRecordVector::Append(thread, array, cell); + JSHandle arrayValue(array); + JSHandle jsFinalizationRegistry(thread, finalizationRegistryHandle.GetTaggedValue()); + JSFinalizationRegistry::Register(thread, target, heldValue, unregisterToken, jsFinalizationRegistry); + return jsFinalizationRegistry; } diff --git a/ecmascript/dump.cpp b/ecmascript/dump.cpp index 7f7a8dea63..f41e79c73b 100644 --- a/ecmascript/dump.cpp +++ b/ecmascript/dump.cpp @@ -1892,9 +1892,7 @@ void JSAPITreeMap::Dump(const JSThread *thread, std::ostream &os) const void JSAPITreeMap::DumpForSnapshot(const JSThread *thread, std::vector &vec) const { if (!(GetTreeMap(thread).IsInvalidValue())) { - TaggedTreeMap *map = TaggedTreeMap::Cast(GetTreeMap(thread).GetTaggedObject()); vec.emplace_back("treemap", GetTreeMap(thread)); - map->DumpForSnapshot(thread, vec); } JSObject::DumpForSnapshot(thread, vec); @@ -1919,11 +1917,7 @@ void JSAPITreeMapIterator::Dump(const JSThread *thread, std::ostream &os) const void JSAPITreeMapIterator::DumpForSnapshot(const JSThread *thread, std::vector &vec) const { if (!(GetIteratedMap(thread).IsInvalidValue())) { - TaggedTreeMap *map = - TaggedTreeMap::Cast(JSAPITreeMap::Cast(GetIteratedMap(thread).GetTaggedObject())-> - GetTreeMap(thread).GetTaggedObject()); vec.emplace_back("iteratedmap", GetIteratedMap(thread)); - map->DumpForSnapshot(thread, vec); } vec.emplace_back(CString("NextIndex"), JSTaggedValue(GetNextIndex())); @@ -2016,9 +2010,7 @@ void JSAPITreeSet::Dump(const JSThread *thread, std::ostream &os) const void JSAPITreeSet::DumpForSnapshot(const JSThread *thread, std::vector &vec) const { if (!(GetTreeSet(thread).IsInvalidValue())) { - TaggedTreeSet *set = TaggedTreeSet::Cast(GetTreeSet(thread).GetTaggedObject()); vec.emplace_back("treeset", GetTreeSet(thread)); - set->DumpForSnapshot(thread, vec); } JSObject::DumpForSnapshot(thread, vec); @@ -2043,11 +2035,7 @@ void JSAPITreeSetIterator::Dump(const JSThread *thread, std::ostream &os) const void JSAPITreeSetIterator::DumpForSnapshot(const JSThread *thread, std::vector &vec) const { if (!(GetIteratedSet(thread).IsInvalidValue())) { - TaggedTreeSet *set = - TaggedTreeSet::Cast(JSAPITreeSet::Cast(GetIteratedSet(thread).GetTaggedObject())-> - GetTreeSet(thread).GetTaggedObject()); vec.emplace_back("iteratedset", GetIteratedSet(thread)); - set->DumpForSnapshot(thread, vec); } vec.emplace_back(CString("NextIndex"), JSTaggedValue(GetNextIndex())); @@ -2115,9 +2103,7 @@ void JSAPIPlainArrayIterator::Dump(const JSThread *thread, std::ostream &os) con void JSAPIPlainArrayIterator::DumpForSnapshot(const JSThread *thread, std::vector &vec) const { if (!(GetIteratedPlainArray(thread).IsInvalidValue())) { - JSAPIPlainArray *array = JSAPIPlainArray::Cast(GetIteratedPlainArray(thread).GetTaggedObject()); vec.emplace_back("iteratedplainarray", GetIteratedPlainArray(thread)); - array->DumpForSnapshot(thread, vec); } vec.emplace_back(CString("NextIndex"), JSTaggedValue(GetNextIndex())); @@ -2397,9 +2383,7 @@ void JSAPIHashMap::Dump(const JSThread *thread, std::ostream &os) const void JSAPIHashMap::DumpForSnapshot(const JSThread *thread, std::vector &vec) const { if (!(GetTable(thread).IsInvalidValue())) { - TaggedHashArray *map = TaggedHashArray::Cast(GetTable(thread).GetTaggedObject()); vec.emplace_back("hashmap", GetTable(thread)); - map->DumpForSnapshot(thread, vec); } JSObject::DumpForSnapshot(thread, vec); @@ -2417,9 +2401,7 @@ void JSAPIHashSet::Dump(const JSThread *thread, std::ostream &os) const void JSAPIHashSet::DumpForSnapshot(const JSThread *thread, std::vector &vec) const { if (!(GetTable(thread).IsInvalidValue())) { - TaggedHashArray *set = TaggedHashArray::Cast(GetTable(thread).GetTaggedObject()); vec.emplace_back("hashset", GetTable(thread)); - set->DumpForSnapshot(thread, vec); } JSObject::DumpForSnapshot(thread, vec); @@ -2491,9 +2473,7 @@ void JSAPIList::Dump(const JSThread *thread, std::ostream &os) const void JSAPIList::DumpForSnapshot(const JSThread *thread, std::vector &vec) const { if (!(GetSingleList(thread).IsInvalidValue())) { - TaggedSingleList *list = TaggedSingleList::Cast(GetSingleList(thread).GetTaggedObject()); vec.emplace_back("singleList", GetSingleList(thread)); - list->DumpForSnapshot(thread, vec); } JSObject::DumpForSnapshot(thread, vec); } @@ -2512,9 +2492,7 @@ void JSAPIListIterator::Dump(const JSThread *thread, std::ostream &os) const void JSAPIListIterator::DumpForSnapshot(const JSThread *thread, std::vector &vec) const { if (!(GetIteratedList(thread).IsInvalidValue())) { - TaggedSingleList *list = TaggedSingleList::Cast(GetIteratedList(thread).GetTaggedObject()); vec.emplace_back("iteratedlist", GetIteratedList(thread)); - list->DumpForSnapshot(thread, vec); } vec.emplace_back(CString("NextIndex"), JSTaggedValue(GetNextIndex())); JSObject::DumpForSnapshot(thread, vec); @@ -2533,9 +2511,7 @@ void JSAPILinkedList::Dump(const JSThread *thread, std::ostream &os) const void JSAPILinkedList::DumpForSnapshot(const JSThread *thread, std::vector &vec) const { if (!(GetDoubleList(thread).IsInvalidValue())) { - TaggedDoubleList *list = TaggedDoubleList::Cast(GetDoubleList(thread).GetTaggedObject()); vec.emplace_back("doubleList", GetDoubleList(thread)); - list->DumpForSnapshot(thread, vec); } JSObject::DumpForSnapshot(thread, vec); @@ -2555,9 +2531,7 @@ void JSAPILinkedListIterator::Dump(const JSThread *thread, std::ostream &os) con void JSAPILinkedListIterator::DumpForSnapshot(const JSThread *thread, std::vector &vec) const { if (!(GetIteratedLinkedList(thread).IsInvalidValue())) { - TaggedDoubleList *linkedList = TaggedDoubleList::Cast(GetIteratedLinkedList(thread).GetTaggedObject()); vec.emplace_back("iteratedlist", GetIteratedLinkedList(thread)); - linkedList->DumpForSnapshot(thread, vec); } vec.emplace_back(CString("NextIndex"), JSTaggedValue(GetNextIndex())); @@ -4926,9 +4900,7 @@ void LinkedNode::DumpForSnapshot(const JSThread *thread, std::vector { JSTaggedValue next = GetNext(thread); if (!next.IsUndefined() && !next.IsHole() && !next.IsNull()) { - LinkedNode *nextNode = LinkedNode::Cast(next.GetTaggedObject()); vec.emplace_back(CString("Next"), next); - nextNode->DumpForSnapshot(thread, vec); } vec.emplace_back(CString("Key"), GetKey(thread)); JSTaggedValue key = GetKey(thread); @@ -5026,9 +4998,7 @@ void JSDate::DumpForSnapshot(const JSThread *thread, std::vector &vec void JSMap::DumpForSnapshot(const JSThread *thread, std::vector &vec) const { if (!(GetLinkedMap(thread).IsInvalidValue())) { - LinkedHashMap *map = LinkedHashMap::Cast(GetLinkedMap(thread).GetTaggedObject()); vec.emplace_back("linkedmap", GetLinkedMap(thread)); - map->DumpForSnapshot(thread, vec); } JSObject::DumpForSnapshot(thread, vec); @@ -5036,10 +5006,8 @@ void JSMap::DumpForSnapshot(const JSThread *thread, std::vector &vec) void JSSharedMap::DumpForSnapshot(const JSThread *thread, std::vector &vec) const { - LinkedHashMap *map = LinkedHashMap::Cast(GetLinkedMap(thread).GetTaggedObject()); vec.emplace_back("linkedmap", GetLinkedMap(thread)); vec.emplace_back("ModRecord", JSTaggedValue(GetModRecord())); - map->DumpForSnapshot(thread, vec); JSObject::DumpForSnapshot(thread, vec); } @@ -5057,9 +5025,7 @@ void JSForInIterator::DumpForSnapshot(const JSThread *thread, std::vector &vec) const { if (!(GetIteratedMap(thread).IsInvalidValue())) { - LinkedHashMap *map = LinkedHashMap::Cast(GetIteratedMap(thread).GetTaggedObject()); vec.emplace_back("iteratedmap", GetIteratedMap(thread)); - map->DumpForSnapshot(thread, vec); } vec.emplace_back(CString("NextIndex"), JSTaggedValue(GetNextIndex())); @@ -5070,9 +5036,7 @@ void JSMapIterator::DumpForSnapshot(const JSThread *thread, std::vector &vec) const { JSSharedMap *iteratedMap = JSSharedMap::Cast(GetIteratedMap(thread).GetTaggedObject()); - LinkedHashMap *map = LinkedHashMap::Cast(iteratedMap->GetLinkedMap(thread).GetTaggedObject()); vec.emplace_back("iteratedmap", GetIteratedMap(thread)); - map->DumpForSnapshot(thread, vec); vec.emplace_back(CString("NextIndex"), JSTaggedValue(GetNextIndex())); vec.emplace_back(CString("IterationKind"), JSTaggedValue(static_cast(GetIterationKind()))); JSObject::DumpForSnapshot(thread, vec); @@ -5081,9 +5045,7 @@ void JSSharedMapIterator::DumpForSnapshot(const JSThread *thread, std::vector &vec) const { if (!(GetLinkedSet(thread).IsInvalidValue())) { - LinkedHashSet *set = LinkedHashSet::Cast(GetLinkedSet(thread).GetTaggedObject()); vec.emplace_back("linkedset", GetLinkedSet(thread)); - set->DumpForSnapshot(thread, vec); } JSObject::DumpForSnapshot(thread, vec); @@ -5091,10 +5053,8 @@ void JSSet::DumpForSnapshot(const JSThread *thread, std::vector &vec) void JSSharedSet::DumpForSnapshot(const JSThread *thread, std::vector &vec) const { - LinkedHashSet *set = LinkedHashSet::Cast(GetLinkedSet(thread).GetTaggedObject()); vec.emplace_back("linkedset", GetLinkedSet(thread)); vec.emplace_back("ModRecord", JSTaggedValue(GetModRecord())); - set->DumpForSnapshot(thread, vec); JSObject::DumpForSnapshot(thread, vec); } @@ -5102,9 +5062,7 @@ void JSSharedSet::DumpForSnapshot(const JSThread *thread, std::vector void JSWeakMap::DumpForSnapshot(const JSThread *thread, std::vector &vec) const { if (!(GetLinkedMap(thread).IsInvalidValue())) { - LinkedHashMap *map = LinkedHashMap::Cast(GetLinkedMap(thread).GetTaggedObject()); vec.emplace_back("linkedmap", GetLinkedMap(thread)); - map->DumpForSnapshot(thread, vec); } JSObject::DumpForSnapshot(thread, vec); @@ -5113,9 +5071,7 @@ void JSWeakMap::DumpForSnapshot(const JSThread *thread, std::vector & void JSWeakSet::DumpForSnapshot(const JSThread *thread, std::vector &vec) const { if (!(GetLinkedSet(thread).IsInvalidValue())) { - LinkedHashSet *set = LinkedHashSet::Cast(GetLinkedSet(thread).GetTaggedObject()); vec.emplace_back("linkeset", GetLinkedSet(thread)); - set->DumpForSnapshot(thread, vec); } JSObject::DumpForSnapshot(thread, vec); @@ -5151,9 +5107,7 @@ void CellRecord::DumpForSnapshot(const JSThread *thread, std::vector void JSSetIterator::DumpForSnapshot(const JSThread *thread, std::vector &vec) const { if (!(GetIteratedSet(thread).IsInvalidValue())) { - LinkedHashSet *set = LinkedHashSet::Cast(GetIteratedSet(thread).GetTaggedObject()); vec.emplace_back("iteratedset", GetIteratedSet(thread)); - set->DumpForSnapshot(thread, vec); } vec.emplace_back(CString("NextIndex"), JSTaggedValue(GetNextIndex())); @@ -5164,9 +5118,7 @@ void JSSetIterator::DumpForSnapshot(const JSThread *thread, std::vector &vec) const { JSSharedSet *iteratedSet = JSSharedSet::Cast(GetIteratedSet(thread).GetTaggedObject()); - LinkedHashSet *set = LinkedHashSet::Cast(iteratedSet->GetLinkedSet(thread).GetTaggedObject()); vec.emplace_back("iteratedset", GetIteratedSet(thread)); - set->DumpForSnapshot(thread, vec); vec.emplace_back(CString("NextIndex"), JSTaggedValue(GetNextIndex())); vec.emplace_back(CString("IterationKind"), JSTaggedValue(static_cast(GetIterationKind()))); JSObject::DumpForSnapshot(thread, vec); @@ -5193,9 +5145,7 @@ void JSAPIArrayList::DumpForSnapshot(const JSThread *thread, std::vector &vec) const { if (!(GetIteratedArrayList(thread).IsInvalidValue())) { - JSAPIArrayList *arraylist = JSAPIArrayList::Cast(GetIteratedArrayList(thread).GetTaggedObject()); vec.emplace_back("iteratedlist", GetIteratedArrayList(thread)); - arraylist->DumpForSnapshot(thread, vec); } vec.emplace_back(CString("NextIndex"), JSTaggedValue(GetNextIndex())); JSObject::DumpForSnapshot(thread, vec); @@ -5221,10 +5171,7 @@ void JSAPILightWeightMap::DumpForSnapshot(const JSThread *thread, std::vector &vec) const { if (!(GetIteratedLightWeightMap(thread).IsInvalidValue())) { - JSAPILightWeightMap *map = - JSAPILightWeightMap::Cast(GetIteratedLightWeightMap(thread).GetTaggedObject()); vec.emplace_back("iteratedmap", GetIteratedLightWeightMap(thread)); - map->DumpForSnapshot(thread, vec); } vec.emplace_back(CString("NextIndex"), JSTaggedValue(GetNextIndex())); @@ -5242,9 +5189,7 @@ void JSAPIQueue::DumpForSnapshot(const JSThread *thread, std::vector void JSAPIQueueIterator::DumpForSnapshot(const JSThread *thread, std::vector &vec) const { if (!(GetIteratedQueue(thread).IsInvalidValue())) { - JSAPIQueue *queue = JSAPIQueue::Cast(GetIteratedQueue(thread).GetTaggedObject()); vec.emplace_back("iteratedqueue", GetIteratedQueue(thread)); - queue->DumpForSnapshot(thread, vec); } vec.emplace_back(CString("NextIndex"), JSTaggedValue(GetNextIndex())); @@ -5259,9 +5204,7 @@ void JSAPIDeque::DumpForSnapshot(const JSThread *thread, std::vector void JSAPIDequeIterator::DumpForSnapshot(const JSThread *thread, std::vector &vec) const { if (!(GetIteratedDeque(thread).IsInvalidValue())) { - JSAPIDeque *deque = JSAPIDeque::Cast(GetIteratedDeque(thread).GetTaggedObject()); vec.emplace_back("iterateddeque", GetIteratedDeque(thread)); - deque->DumpForSnapshot(thread, vec); } vec.emplace_back(CString("NextIndex"), JSTaggedValue(GetNextIndex())); @@ -5286,10 +5229,7 @@ void JSAPILightWeightSet::DumpForSnapshot(const JSThread *thread, std::vector &vec) const { if (!(GetIteratedLightWeightSet(thread).IsInvalidValue())) { - JSAPILightWeightSet *set = - JSAPILightWeightSet::Cast(GetIteratedLightWeightSet(thread).GetTaggedObject()); vec.emplace_back("iteratedset", GetIteratedLightWeightSet(thread)); - set->DumpForSnapshot(thread, vec); } vec.emplace_back(CString("NextIndex"), JSTaggedValue(GetNextIndex())); @@ -5305,9 +5245,7 @@ void JSAPIStack::DumpForSnapshot(const JSThread *thread, std::vector void JSAPIStackIterator::DumpForSnapshot(const JSThread *thread, std::vector &vec) const { if (!(GetIteratedStack(thread).IsInvalidValue())) { - JSAPIStack *stack = JSAPIStack::Cast(GetIteratedStack(thread).GetTaggedObject()); vec.emplace_back("iteratedstack", GetIteratedStack(thread)); - stack->DumpForSnapshot(thread, vec); } vec.emplace_back(CString("NextIndex"), JSTaggedValue(GetNextIndex())); @@ -5317,9 +5255,7 @@ void JSAPIStackIterator::DumpForSnapshot(const JSThread *thread, std::vector &vec) const { if (!(GetIteratedArray(thread).IsInvalidValue())) { - JSArray *array = JSArray::Cast(GetIteratedArray(thread).GetTaggedObject()); vec.emplace_back("iteratedarray", GetIteratedArray(thread)); - array->DumpForSnapshot(thread, vec); } vec.emplace_back(CString("NextIndex"), JSTaggedValue(GetNextIndex())); @@ -5330,9 +5266,7 @@ void JSArrayIterator::DumpForSnapshot(const JSThread *thread, std::vector &vec) const { - JSSharedArray *array = JSSharedArray::Cast(GetIteratedArray(thread).GetTaggedObject()); vec.emplace_back("iteratedarray", GetIteratedArray(thread)); - array->DumpForSnapshot(thread, vec); vec.emplace_back(CString("NextIndex"), JSTaggedValue(GetNextIndex())); vec.emplace_back(CString("IterationKind"), JSTaggedValue(static_cast(GetIterationKind()))); JSObject::DumpForSnapshot(thread, vec); @@ -5346,9 +5280,7 @@ void JSAPIVector::DumpForSnapshot(const JSThread *thread, std::vector void JSAPIVectorIterator::DumpForSnapshot(const JSThread *thread, std::vector &vec) const { if (!(GetIteratedVector(thread).IsInvalidValue())) { - JSAPIVector *vector = JSAPIVector::Cast(GetIteratedVector(thread).GetTaggedObject()); vec.emplace_back("iteratedvector", GetIteratedVector(thread)); - vector->DumpForSnapshot(thread, vec); } vec.emplace_back(CString("NextIndex"), JSTaggedValue(GetNextIndex())); @@ -5362,9 +5294,7 @@ void JSAPIBitVector::DumpForSnapshot(const JSThread *thread, std::vector &vec) const { - JSAPIVector *vector = JSAPIVector::Cast(GetIteratedBitVector(thread).GetTaggedObject()); vec.emplace_back("iteratedbitvector", GetIteratedBitVector(thread)); - vector->DumpForSnapshot(thread, vec); vec.emplace_back(CString("NextIndex"), JSTaggedValue(GetNextIndex())); JSObject::DumpForSnapshot(thread, vec); } -- Gitee