1 /*
2 * Copyright (c) 2022 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "ecmascript/builtins/builtins_finalization_registry.h"
17 #include "ecmascript/global_env.h"
18 #include "ecmascript/linked_hash_table.h"
19 #include "ecmascript/jobs/micro_job_queue.h"
20 #include "ecmascript/js_finalization_registry.h"
21 #include "ecmascript/tests/test_helper.h"
22
23 using namespace panda;
24 using namespace panda::ecmascript;
25 using namespace panda::ecmascript::builtins;
26
27 static int testValue = 0;
28
29 namespace panda::test {
30 class JSFinalizationRegistryTest : public testing::Test {
31 public:
SetUpTestCase()32 static void SetUpTestCase()
33 {
34 GTEST_LOG_(INFO) << "SetUpTestCase";
35 }
36
TearDownTestCase()37 static void TearDownTestCase()
38 {
39 GTEST_LOG_(INFO) << "TearDownCase";
40 }
41
SetUp()42 void SetUp() override
43 {
44 TestHelper::CreateEcmaVMWithScope(instance, thread, scope);
45 }
46
TearDown()47 void TearDown() override
48 {
49 TestHelper::DestroyEcmaVMWithScope(instance, scope);
50 }
51
52 EcmaVM *instance {nullptr};
53 ecmascript::EcmaHandleScope *scope {nullptr};
54 JSThread *thread {nullptr};
55
56 class TestClass : public base::BuiltinsBase {
57 public:
callbackTest()58 static JSTaggedValue callbackTest()
59 {
60 testValue++;
61 return JSTaggedValue::Undefined();
62 }
63 };
64 };
65
CreateFinalizationRegistry(JSThread * thread)66 static JSHandle<JSTaggedValue> CreateFinalizationRegistry(JSThread *thread)
67 {
68 auto vm = thread->GetEcmaVM();
69 auto factory = vm->GetFactory();
70 auto env = vm->GetGlobalEnv();
71
72 JSHandle<JSFunction> finaRegFunc(env->GetBuiltinsFinalizationRegistryFunction());
73 JSHandle<JSFunction> callbackFunc = factory->NewJSFunction(env, reinterpret_cast<void *>(
74 JSFinalizationRegistryTest::TestClass::callbackTest));
75 auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue(*finaRegFunc), 6);
76 ecmaRuntimeCallInfo->SetFunction(finaRegFunc.GetTaggedValue());
77 ecmaRuntimeCallInfo->SetThis(JSTaggedValue::Undefined());
78 ecmaRuntimeCallInfo->SetCallArg(0, callbackFunc.GetTaggedValue());
79
80 auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo);
81 JSTaggedValue finalizationRegistry =
82 BuiltinsFinalizationRegistry::FinalizationRegistryConstructor(ecmaRuntimeCallInfo);
83 JSHandle<JSTaggedValue> finalizationRegistryHandle(thread, finalizationRegistry);
84 TestHelper::TearDownFrame(thread, prev);
85 return finalizationRegistryHandle;
86 }
87
88 /**
89 * @tc.name: Register
90 * @tc.desc: Register the target object to the JSFinalizationRegistry object, and call the callback method after the
91 * target object is garbage collected. And a unregistration token, which is passed to the unregister method
92 * when the finalizer is no longer needed. The held value, which is used to represent that object when
93 * cleaning it up in the finalizer.
94 * @tc.type: FUNC
95 * @tc.require:
96 */
HWTEST_F_L0(JSFinalizationRegistryTest,Register_001)97 HWTEST_F_L0(JSFinalizationRegistryTest, Register_001)
98 {
99 testValue = 0;
100 auto vm = thread->GetEcmaVM();
101 auto factory = vm->GetFactory();
102 auto env = vm->GetGlobalEnv();
103
104 JSHandle<JSTaggedValue> objectFunc = env->GetObjectFunction();
105 JSHandle<JSTaggedValue> target(factory->NewJSObjectByConstructor(JSHandle<JSFunction>(objectFunc), objectFunc));
106 JSHandle<JSTaggedValue> key(factory->NewFromASCII("key1"));
107 JSHandle<JSTaggedValue> value(thread, JSTaggedValue(1));
108 JSObject::SetProperty(thread, target, key, value);
109 JSHandle<JSTaggedValue> heldValue(thread, JSTaggedValue(100));
110 JSHandle<JSTaggedValue> unregisterToken(thread, JSTaggedValue::Undefined());
111 JSHandle<JSTaggedValue> constructor = CreateFinalizationRegistry(thread);
112 JSHandle<JSFinalizationRegistry> finaRegObj(thread, constructor.GetTaggedValue());
113
114 // If unregisterToken is undefined, use vector to store
115 JSHandle<CellRecord> cellRecord = factory->NewCellRecord();
116 cellRecord->SetToWeakRefTarget(thread, target.GetTaggedValue());
117 cellRecord->SetHeldValue(thread, heldValue);
118 JSHandle<JSTaggedValue> cell(cellRecord);
119 JSHandle<CellRecordVector> expectNoUnregister(thread, finaRegObj->GetNoUnregister());
120 expectNoUnregister = CellRecordVector::Append(thread, expectNoUnregister, cell);
121
122 JSFinalizationRegistry::Register(thread, target, heldValue, unregisterToken, finaRegObj);
123 JSHandle<JSTaggedValue> noUnregister(thread, finaRegObj->GetNoUnregister());
124 JSHandle<JSTaggedValue> finRegLists = env->GetFinRegLists();
125 EXPECT_EQ(finRegLists.GetTaggedValue().GetRawData(),
126 JSHandle<JSTaggedValue>::Cast(finaRegObj).GetTaggedValue().GetRawData());
127 EXPECT_EQ(expectNoUnregister.GetTaggedValue().GetRawData(), noUnregister.GetTaggedValue().GetRawData());
128 EXPECT_EQ(testValue, 0);
129 }
130
HWTEST_F_L0(JSFinalizationRegistryTest,Register_002)131 HWTEST_F_L0(JSFinalizationRegistryTest, Register_002)
132 {
133 testValue = 0;
134 auto vm = thread->GetEcmaVM();
135 auto factory = vm->GetFactory();
136 auto env = vm->GetGlobalEnv();
137
138 JSHandle<JSTaggedValue> objectFunc = env->GetObjectFunction();
139 JSHandle<JSTaggedValue> target(factory->NewJSObjectByConstructor(JSHandle<JSFunction>(objectFunc), objectFunc));
140 JSHandle<JSTaggedValue> key(factory->NewFromASCII("key2"));
141 JSHandle<JSTaggedValue> value(thread, JSTaggedValue(2));
142 JSObject::SetProperty(thread, target, key, value);
143 JSHandle<JSTaggedValue> heldValue(thread, JSTaggedValue(100));
144 JSHandle<JSTaggedValue> unregisterToken = target;
145 JSHandle<JSTaggedValue> constructor = CreateFinalizationRegistry(thread);
146 JSHandle<JSFinalizationRegistry> finaRegObj(thread, constructor.GetTaggedValue());
147
148 // If unregisterToken is not undefined, use hash map to store to facilitate subsequent delete operations
149 JSHandle<CellRecord> cellRecord = factory->NewCellRecord();
150 cellRecord->SetToWeakRefTarget(thread, target.GetTaggedValue());
151 cellRecord->SetHeldValue(thread, heldValue);
152 JSHandle<JSTaggedValue> cell(cellRecord);
153 JSHandle<CellRecordVector> array = JSHandle<CellRecordVector>(CellRecordVector::Create(thread));
154 array = CellRecordVector::Append(thread, array, cell);
155 JSHandle<JSTaggedValue> arrayValue(array);
156 JSHandle<LinkedHashMap> expectMaybeUnregister(thread, finaRegObj->GetMaybeUnregister());
157 expectMaybeUnregister = LinkedHashMap::SetWeakRef(thread, expectMaybeUnregister, unregisterToken, arrayValue);
158
159 JSFinalizationRegistry::Register(thread, target, heldValue, unregisterToken, finaRegObj);
160 JSHandle<JSTaggedValue> maybeUnregister(thread, finaRegObj->GetMaybeUnregister());
161 EXPECT_EQ(expectMaybeUnregister.GetTaggedValue().GetRawData(), maybeUnregister.GetTaggedValue().GetRawData());
162
163 JSHandle<JSTaggedValue> finRegLists = env->GetFinRegLists();
164 EXPECT_EQ(finRegLists.GetTaggedValue().GetRawData(),
165 JSHandle<JSTaggedValue>::Cast(finaRegObj).GetTaggedValue().GetRawData());
166 EXPECT_EQ(testValue, 0);
167 }
168
HWTEST_F_L0(JSFinalizationRegistryTest,Register_003)169 HWTEST_F_L0(JSFinalizationRegistryTest, Register_003)
170 {
171 testValue = 0;
172 auto vm = thread->GetEcmaVM();
173 auto factory = vm->GetFactory();
174 auto env = vm->GetGlobalEnv();
175
176 JSHandle<JSTaggedValue> objectFunc = env->GetObjectFunction();
177 vm->SetEnableForceGC(false);
178 JSHandle<JSTaggedValue> target(thread, JSTaggedValue::Undefined());
179 {
180 [[maybe_unused]] EcmaHandleScope handleScope(thread);
181 auto obj = factory->NewJSObjectByConstructor(JSHandle<JSFunction>(objectFunc), objectFunc);
182 target = JSHandle<JSTaggedValue>::Cast(obj);
183 JSHandle<JSTaggedValue> heldValue(thread, JSTaggedValue(100));
184 JSHandle<JSTaggedValue> unregisterToken(thread, JSTaggedValue::Undefined());
185 JSHandle<JSTaggedValue> constructor = CreateFinalizationRegistry(thread);
186 JSHandle<JSFinalizationRegistry> finaRegObj(thread, constructor.GetTaggedValue());
187
188 JSFinalizationRegistry::Register(thread, target, heldValue, unregisterToken, finaRegObj);
189 EXPECT_EQ(testValue, 0);
190 }
191 vm->CollectGarbage(TriggerGCType::FULL_GC);
192 if (!thread->HasPendingException()) {
193 job::MicroJobQueue::ExecutePendingJob(thread, thread->GetCurrentEcmaContext()->GetMicroJobQueue());
194 }
195 vm->SetEnableForceGC(true);
196 EXPECT_EQ(testValue, 1);
197 }
198
HWTEST_F_L0(JSFinalizationRegistryTest,Register_004)199 HWTEST_F_L0(JSFinalizationRegistryTest, Register_004)
200 {
201 testValue = 0;
202 auto vm = thread->GetEcmaVM();
203 auto factory = vm->GetFactory();
204 auto env = vm->GetGlobalEnv();
205
206 JSHandle<JSTaggedValue> objectFunc = env->GetObjectFunction();
207 vm->SetEnableForceGC(false);
208 JSHandle<JSTaggedValue> target1(thread, JSTaggedValue::Undefined());
209 JSHandle<JSTaggedValue> target2(thread, JSTaggedValue::Undefined());
210 {
211 [[maybe_unused]] EcmaHandleScope handleScope(thread);
212 auto obj = factory->NewJSObjectByConstructor(JSHandle<JSFunction>(objectFunc), objectFunc);
213 target1 = JSHandle<JSTaggedValue>::Cast(obj);
214 target2 = JSHandle<JSTaggedValue>::Cast(obj);
215 JSHandle<JSTaggedValue> heldValue(thread, JSTaggedValue(100));
216 JSHandle<JSTaggedValue> unregisterToken = JSHandle<JSTaggedValue>::Cast(obj);
217 JSHandle<JSTaggedValue> constructor = CreateFinalizationRegistry(thread);
218 JSHandle<JSFinalizationRegistry> finaRegObj1(thread, constructor.GetTaggedValue());
219 JSHandle<JSFinalizationRegistry> finaRegObj2(thread, constructor.GetTaggedValue());
220
221 JSFinalizationRegistry::Register(thread, target1, heldValue, unregisterToken, finaRegObj1);
222 JSFinalizationRegistry::Register(thread, target2, heldValue, unregisterToken, finaRegObj2);
223 EXPECT_EQ(testValue, 0);
224 }
225 vm->CollectGarbage(TriggerGCType::FULL_GC);
226 if (!thread->HasPendingException()) {
227 job::MicroJobQueue::ExecutePendingJob(thread, thread->GetCurrentEcmaContext()->GetMicroJobQueue());
228 }
229 vm->SetEnableForceGC(true);
230 EXPECT_EQ(testValue, 2);
231 }
232
233 /**
234 * @tc.name: Unregister
235 * @tc.desc: Unregister the JSFinalizationRegistry object from the finalization registry lists.
236 * @tc.type: FUNC
237 * @tc.require:
238 */
HWTEST_F_L0(JSFinalizationRegistryTest,Unregister_001)239 HWTEST_F_L0(JSFinalizationRegistryTest, Unregister_001)
240 {
241 testValue = 0;
242 auto vm = thread->GetEcmaVM();
243 auto factory = vm->GetFactory();
244 auto env = vm->GetGlobalEnv();
245
246 JSHandle<JSTaggedValue> objectFunc = env->GetObjectFunction();
247 vm->SetEnableForceGC(false);
248 JSHandle<JSTaggedValue> target(thread, JSTaggedValue::Undefined());
249 {
250 [[maybe_unused]] EcmaHandleScope handleScope(thread);
251 auto obj = factory->NewJSObjectByConstructor(JSHandle<JSFunction>(objectFunc), objectFunc);
252 target = JSHandle<JSTaggedValue>::Cast(obj);
253 JSHandle<JSTaggedValue> heldValue(thread, JSTaggedValue(100));
254 JSHandle<JSTaggedValue> unregisterToken = target;
255 JSHandle<JSTaggedValue> constructor = CreateFinalizationRegistry(thread);
256 JSHandle<JSFinalizationRegistry> finaRegObj(thread, constructor.GetTaggedValue());
257
258 JSFinalizationRegistry::Register(thread, target, heldValue, unregisterToken, finaRegObj);
259 JSFinalizationRegistry::Unregister(thread, target, finaRegObj);
260 EXPECT_EQ(testValue, 0);
261 }
262 vm->CollectGarbage(TriggerGCType::FULL_GC);
263 if (!thread->HasPendingException()) {
264 job::MicroJobQueue::ExecutePendingJob(thread, thread->GetCurrentEcmaContext()->GetMicroJobQueue());
265 }
266 vm->SetEnableForceGC(true);
267 EXPECT_EQ(testValue, 0);
268 }
269
HWTEST_F_L0(JSFinalizationRegistryTest,Unregister_002)270 HWTEST_F_L0(JSFinalizationRegistryTest, Unregister_002)
271 {
272 testValue = 0;
273 auto vm = thread->GetEcmaVM();
274 auto factory = vm->GetFactory();
275 auto env = vm->GetGlobalEnv();
276
277 JSHandle<JSTaggedValue> objectFunc = env->GetObjectFunction();
278 vm->SetEnableForceGC(false);
279 JSHandle<JSTaggedValue> target1(thread, JSTaggedValue::Undefined());
280 JSHandle<JSTaggedValue> target2(thread, JSTaggedValue::Undefined());
281 {
282 [[maybe_unused]] EcmaHandleScope handleScope(thread);
283 auto obj = factory->NewJSObjectByConstructor(JSHandle<JSFunction>(objectFunc), objectFunc);
284 target1 = JSHandle<JSTaggedValue>::Cast(obj);
285 target2 = JSHandle<JSTaggedValue>::Cast(obj);
286 JSHandle<JSTaggedValue> heldValue(thread, JSTaggedValue(100));
287 JSHandle<JSTaggedValue> unregisterToken = JSHandle<JSTaggedValue>::Cast(obj);
288 JSHandle<JSTaggedValue> constructor = CreateFinalizationRegistry(thread);
289 JSHandle<JSFinalizationRegistry> finaRegObj1(thread, constructor.GetTaggedValue());
290 JSHandle<JSFinalizationRegistry> finaRegObj2(thread, constructor.GetTaggedValue());
291
292 // only second finalization is be registered
293 JSFinalizationRegistry::Register(thread, target1, heldValue, unregisterToken, finaRegObj1);
294 JSFinalizationRegistry::Unregister(thread, target1, finaRegObj1);
295
296 JSFinalizationRegistry::Register(thread, target2, heldValue, unregisterToken, finaRegObj2);
297 EXPECT_EQ(testValue, 0);
298 }
299 vm->CollectGarbage(TriggerGCType::FULL_GC);
300 if (!thread->HasPendingException()) {
301 job::MicroJobQueue::ExecutePendingJob(thread, thread->GetCurrentEcmaContext()->GetMicroJobQueue());
302 }
303 vm->SetEnableForceGC(true);
304 // only trigger second finalization callback
305 EXPECT_EQ(testValue, 1);
306 }
307
308 /**
309 * @tc.name: CheckAndCall
310 * @tc.desc: Check whether each JSFinalizationRegistry object in finalization registry lists has been cleared. If so,
311 * clear the JSFinalizationRegistry object from the lists.
312 * @tc.type: FUNC
313 * @tc.require:
314 */
HWTEST_F_L0(JSFinalizationRegistryTest,CheckAndCall)315 HWTEST_F_L0(JSFinalizationRegistryTest, CheckAndCall)
316 {
317 testValue = 0;
318 auto vm = thread->GetEcmaVM();
319 auto factory = vm->GetFactory();
320 auto env = vm->GetGlobalEnv();
321
322 JSHandle<JSTaggedValue> objectFunc = env->GetObjectFunction();
323 JSHandle<JSTaggedValue> target(thread, JSTaggedValue::Undefined());
324 JSHandle<JSTaggedValue> constructor = CreateFinalizationRegistry(thread);
325 JSHandle<JSFinalizationRegistry> finaRegObj(thread, constructor.GetTaggedValue());
326 JSHandle<JSTaggedValue> finRegLists(thread, JSTaggedValue::Undefined());
327 vm->SetEnableForceGC(false);
328 {
329 [[maybe_unused]] EcmaHandleScope handleScope(thread);
330 auto obj = factory->NewJSObjectByConstructor(JSHandle<JSFunction>(objectFunc), objectFunc);
331 target = JSHandle<JSTaggedValue>::Cast(obj);
332 JSHandle<JSTaggedValue> heldValue(thread, JSTaggedValue(100));
333 JSHandle<JSTaggedValue> unregisterToken(thread, JSTaggedValue::Undefined());
334 JSFinalizationRegistry::Register(thread, target, heldValue, unregisterToken, finaRegObj);
335 EXPECT_EQ(testValue, 0);
336 }
337 finRegLists = env->GetFinRegLists();
338 EXPECT_EQ(finRegLists.GetTaggedValue(), JSHandle<JSTaggedValue>::Cast(finaRegObj).GetTaggedValue());
339 vm->CollectGarbage(TriggerGCType::FULL_GC);
340 if (!thread->HasPendingException()) {
341 job::MicroJobQueue::ExecutePendingJob(thread, thread->GetCurrentEcmaContext()->GetMicroJobQueue());
342 }
343 vm->SetEnableForceGC(true);
344 EXPECT_EQ(testValue, 1);
345 // If all objects registered in the current JSFinalizationRegistry are garbage collected,
346 // clear the JSFinalizationRegistry from the list
347 JSFinalizationRegistry::CheckAndCall(thread);
348 finRegLists = env->GetFinRegLists();
349 EXPECT_EQ(finRegLists.GetTaggedValue(), JSTaggedValue::Undefined());
350 }
351
352 /**
353 * @tc.name: CleanupFinalizationRegistry
354 * @tc.desc: Check current JSFinalizationRegistry, While contains any record cell such that cell and WeakRefTarget is
355 * empty, than remove cell from JSFinalizationRegistry and return whether there are still objects that have
356 * not been cleaned up.
357 * @tc.type: FUNC
358 * @tc.require:
359 */
HWTEST_F_L0(JSFinalizationRegistryTest,CleanupFinalizationRegistry)360 HWTEST_F_L0(JSFinalizationRegistryTest, CleanupFinalizationRegistry)
361 {
362 testValue = 0;
363 auto vm = thread->GetEcmaVM();
364 auto factory = vm->GetFactory();
365 auto env = vm->GetGlobalEnv();
366
367 JSHandle<JSTaggedValue> objectFunc = env->GetObjectFunction();
368 JSHandle<JSTaggedValue> target(thread, JSTaggedValue::Undefined());
369 JSHandle<JSTaggedValue> constructor = CreateFinalizationRegistry(thread);
370 JSHandle<JSFinalizationRegistry> finaRegObj(thread, constructor.GetTaggedValue());
371 bool isCleared = false;
372 vm->SetEnableForceGC(false);
373 {
374 [[maybe_unused]] EcmaHandleScope handleScope(thread);
375 auto obj = factory->NewJSObjectByConstructor(JSHandle<JSFunction>(objectFunc), objectFunc);
376 target = JSHandle<JSTaggedValue>::Cast(obj);
377 JSHandle<JSTaggedValue> heldValue(thread, JSTaggedValue(100));
378 JSHandle<JSTaggedValue> unregisterToken(thread, JSTaggedValue::Undefined());
379 JSFinalizationRegistry::Register(thread, target, heldValue, unregisterToken, finaRegObj);
380 EXPECT_EQ(testValue, 0);
381
382 isCleared = JSFinalizationRegistry::CleanupFinalizationRegistry(thread, finaRegObj);
383 EXPECT_FALSE(isCleared);
384 }
385 vm->CollectGarbage(TriggerGCType::FULL_GC);
386 if (!thread->HasPendingException()) {
387 job::MicroJobQueue::ExecutePendingJob(thread, thread->GetCurrentEcmaContext()->GetMicroJobQueue());
388 }
389 vm->SetEnableForceGC(true);
390 EXPECT_EQ(testValue, 1);
391 isCleared = JSFinalizationRegistry::CleanupFinalizationRegistry(thread, finaRegObj);
392 EXPECT_TRUE(isCleared);
393 }
394
395 /**
396 * @tc.name: CleanFinRegLists
397 * @tc.desc: Clear the JSFinalizationRegistry object from the finalization registry lists.
398 * @tc.type: FUNC
399 * @tc.require:
400 */
HWTEST_F_L0(JSFinalizationRegistryTest,CleanFinRegLists)401 HWTEST_F_L0(JSFinalizationRegistryTest, CleanFinRegLists)
402 {
403 testValue = 0;
404 auto vm = thread->GetEcmaVM();
405 auto factory = vm->GetFactory();
406 auto env = vm->GetGlobalEnv();
407
408 JSHandle<JSTaggedValue> objectFunc = env->GetObjectFunction();
409 JSHandle<JSTaggedValue> target(factory->NewJSObjectByConstructor(JSHandle<JSFunction>(objectFunc), objectFunc));
410 JSHandle<JSTaggedValue> key(factory->NewFromASCII("key3"));
411 JSHandle<JSTaggedValue> value(thread, JSTaggedValue(3));
412 JSObject::SetProperty(thread, target, key, value);
413 JSHandle<JSTaggedValue> heldValue(thread, JSTaggedValue(100));
414 JSHandle<JSTaggedValue> unregisterToken(thread, JSTaggedValue::Undefined());
415 JSHandle<JSTaggedValue> constructor = CreateFinalizationRegistry(thread);
416 JSHandle<JSFinalizationRegistry> finaRegObj(thread, constructor.GetTaggedValue());
417
418 JSFinalizationRegistry::Register(thread, target, heldValue, unregisterToken, finaRegObj);
419 JSHandle<JSTaggedValue> finRegLists = env->GetFinRegLists();
420 EXPECT_EQ(finRegLists.GetTaggedValue(), JSHandle<JSTaggedValue>::Cast(finaRegObj).GetTaggedValue());
421 EXPECT_EQ(testValue, 0);
422
423 JSFinalizationRegistry::CleanFinRegLists(thread, finaRegObj);
424 finRegLists = env->GetFinRegLists();
425 EXPECT_EQ(finRegLists.GetTaggedValue(), JSTaggedValue::Undefined());
426 }
427
428 /**
429 * @tc.name: AddFinRegLists
430 * @tc.desc: Add a JSFinalizationRegistry object to the finalization registry lists.
431 * @tc.type: FUNC
432 * @tc.require:
433 */
HWTEST_F_L0(JSFinalizationRegistryTest,AddFinRegLists)434 HWTEST_F_L0(JSFinalizationRegistryTest, AddFinRegLists)
435 {
436 testValue = 0;
437 auto vm = thread->GetEcmaVM();
438 auto factory = vm->GetFactory();
439 auto env = vm->GetGlobalEnv();
440
441 JSHandle<JSTaggedValue> objectFunc = env->GetObjectFunction();
442 vm->SetEnableForceGC(false);
443 JSHandle<JSTaggedValue> target(thread, JSTaggedValue::Undefined());
444 {
445 [[maybe_unused]] EcmaHandleScope handleScope(thread);
446 auto obj = factory->NewJSObjectByConstructor(JSHandle<JSFunction>(objectFunc), objectFunc);
447 target = JSHandle<JSTaggedValue>::Cast(obj);
448 JSHandle<JSTaggedValue> heldValue(thread, JSTaggedValue(100));
449 JSHandle<JSTaggedValue> unregisterToken(thread, JSTaggedValue::Undefined());
450 JSHandle<JSTaggedValue> constructor = CreateFinalizationRegistry(thread);
451 JSHandle<JSFinalizationRegistry> finaRegObj(thread, constructor.GetTaggedValue());
452
453 JSHandle<CellRecord> cellRecord = factory->NewCellRecord();
454 cellRecord->SetToWeakRefTarget(thread, target.GetTaggedValue());
455 cellRecord->SetHeldValue(thread, heldValue);
456 JSHandle<JSTaggedValue> cell(cellRecord);
457 JSHandle<CellRecordVector> noUnregister(thread, finaRegObj->GetNoUnregister());
458 noUnregister = CellRecordVector::Append(thread, noUnregister, cell);
459 finaRegObj->SetNoUnregister(thread, noUnregister);
460 JSFinalizationRegistry::AddFinRegLists(thread, finaRegObj);
461 JSHandle<JSTaggedValue> finRegLists = env->GetFinRegLists();
462 EXPECT_EQ(finRegLists.GetTaggedValue(), JSHandle<JSTaggedValue>::Cast(finaRegObj).GetTaggedValue());
463 EXPECT_EQ(testValue, 0);
464 }
465 vm->CollectGarbage(TriggerGCType::FULL_GC);
466 if (!thread->HasPendingException()) {
467 job::MicroJobQueue::ExecutePendingJob(thread, thread->GetCurrentEcmaContext()->GetMicroJobQueue());
468 }
469 vm->SetEnableForceGC(true);
470 EXPECT_EQ(testValue, 1);
471 }
472 } // namespace panda::test