• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 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 
18 #include "ecmascript/js_finalization_registry.h"
19 #include "ecmascript/ecma_vm.h"
20 #include "ecmascript/global_env.h"
21 #include "ecmascript/jobs/micro_job_queue.h"
22 #include "ecmascript/js_array.h"
23 #include "ecmascript/js_array_iterator.h"
24 
25 #include "ecmascript/js_handle.h"
26 #include "ecmascript/js_hclass.h"
27 #include "ecmascript/js_object-inl.h"
28 #include "ecmascript/js_tagged_value-inl.h"
29 #include "ecmascript/js_tagged_value.h"
30 #include "ecmascript/js_thread.h"
31 
32 #include "ecmascript/object_factory.h"
33 #include "ecmascript/object_operator.h"
34 #include "ecmascript/tests/test_helper.h"
35 
36 using namespace panda::ecmascript;
37 using namespace panda::ecmascript::builtins;
38 using BuiltinsBase = panda::ecmascript::base::BuiltinsBase;
39 static int testValue = 0;
40 
41 namespace panda::test {
42 class BuiltinsFinalizationRegistryTest : public testing::Test {
43 public:
SetUpTestCase()44     static void SetUpTestCase()
45     {
46         GTEST_LOG_(INFO) << "SetUpTestCase";
47     }
48 
TearDownTestCase()49     static void TearDownTestCase()
50     {
51         GTEST_LOG_(INFO) << "TearDownCase";
52     }
53 
SetUp()54     void SetUp() override
55     {
56         TestHelper::CreateEcmaVMWithScope(instance, thread, scope);
57     }
58 
TearDown()59     void TearDown() override
60     {
61         TestHelper::DestroyEcmaVMWithScope(instance, scope);
62     }
63 
64     EcmaVM *instance {nullptr};
65     EcmaHandleScope *scope {nullptr};
66     JSThread *thread {nullptr};
67 
68     class TestClass : public base::BuiltinsBase {
69     public:
cleanupCallback()70         static JSTaggedValue cleanupCallback()
71         {
72             ++testValue;
73             return JSTaggedValue::Undefined();
74         }
75     };
76 };
77 
CreateFinalizationRegistryConstructor(JSThread * thread)78 JSTaggedValue CreateFinalizationRegistryConstructor(JSThread *thread)
79 {
80     ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
81     JSHandle<GlobalEnv> env = thread->GetEcmaVM()->GetGlobalEnv();
82 
83     JSHandle<JSFunction> finalizationRegistry(env->GetBuiltinsFinalizationRegistryFunction());
84     JSHandle<JSFunction> handleFunc = factory->NewJSFunction(
85         env, reinterpret_cast<void *>(BuiltinsFinalizationRegistryTest::TestClass::cleanupCallback));
86 
87     auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue(*finalizationRegistry), 6);
88     ecmaRuntimeCallInfo->SetFunction(finalizationRegistry.GetTaggedValue());
89     ecmaRuntimeCallInfo->SetThis(JSTaggedValue::Undefined());
90     ecmaRuntimeCallInfo->SetCallArg(0, handleFunc.GetTaggedValue());
91 
92     [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo);
93     JSTaggedValue res = BuiltinsFinalizationRegistry::FinalizationRegistryConstructor(ecmaRuntimeCallInfo);
94     TestHelper::TearDownFrame(thread, prev);
95     return res;
96 }
97 
98 // new FinalizationRegistry (cleanupCallback)
HWTEST_F_L0(BuiltinsFinalizationRegistryTest,FinalizationRegistryConstructor)99 HWTEST_F_L0(BuiltinsFinalizationRegistryTest, FinalizationRegistryConstructor)
100 {
101     ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
102     JSHandle<GlobalEnv> env = thread->GetEcmaVM()->GetGlobalEnv();
103 
104     JSHandle<JSFunction> finalizationRegistry(env->GetBuiltinsFinalizationRegistryFunction());
105     JSHandle<JSFunction> handleFunc = factory->NewJSFunction(env, reinterpret_cast<void *>(TestClass::cleanupCallback));
106 
107     auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue(*finalizationRegistry), 6);
108     ecmaRuntimeCallInfo->SetFunction(finalizationRegistry.GetTaggedValue());
109     ecmaRuntimeCallInfo->SetThis(JSTaggedValue::Undefined());
110     ecmaRuntimeCallInfo->SetCallArg(0, handleFunc.GetTaggedValue());
111 
112     [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo);
113     JSTaggedValue result = BuiltinsFinalizationRegistry::FinalizationRegistryConstructor(ecmaRuntimeCallInfo);
114     ASSERT_TRUE(result.IsECMAObject());
115 }
116 
117 // finalizationRegistry.Register(target, heldValue)
HWTEST_F_L0(BuiltinsFinalizationRegistryTest,Register0)118 HWTEST_F_L0(BuiltinsFinalizationRegistryTest, Register0)
119 {
120     testValue = 0;
121     ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
122     JSHandle<GlobalEnv> env = thread->GetEcmaVM()->GetGlobalEnv();
123     JSHandle<JSTaggedValue> objectFunc = env->GetObjectFunction();
124 
125     JSTaggedValue result = CreateFinalizationRegistryConstructor(thread);
126     JSHandle<JSFinalizationRegistry> jsfinalizationRegistry(thread, result);
127     JSHandle<JSTaggedValue> target(factory->NewJSObjectByConstructor(JSHandle<JSFunction>(objectFunc), objectFunc));
128     JSHandle<JSTaggedValue> key(factory->NewFromASCII("1"));
129     JSHandle<JSTaggedValue> value(thread, JSTaggedValue(1));
130     JSObject::SetProperty(thread, target, key, value);
131 
132     auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 8);
133     ecmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined());
134     ecmaRuntimeCallInfo->SetThis(jsfinalizationRegistry.GetTaggedValue());
135     ecmaRuntimeCallInfo->SetCallArg(0, target.GetTaggedValue());
136     ecmaRuntimeCallInfo->SetCallArg(1, JSTaggedValue(10));
137 
138     [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo);
139     BuiltinsFinalizationRegistry::Register(ecmaRuntimeCallInfo);
140     ASSERT_EQ(testValue, 0);
141 }
142 
143 // finalizationRegistry.Register(target, heldValue [ , unregisterToken ])
HWTEST_F_L0(BuiltinsFinalizationRegistryTest,Register1)144 HWTEST_F_L0(BuiltinsFinalizationRegistryTest, Register1)
145 {
146     testValue = 0;
147     ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
148     JSHandle<GlobalEnv> env = thread->GetEcmaVM()->GetGlobalEnv();
149     JSHandle<JSTaggedValue> objectFunc = env->GetObjectFunction();
150 
151     JSTaggedValue result = CreateFinalizationRegistryConstructor(thread);
152     JSHandle<JSFinalizationRegistry> jsfinalizationRegistry(thread, result);
153     JSHandle<JSTaggedValue> target(factory->NewJSObjectByConstructor(JSHandle<JSFunction>(objectFunc), objectFunc));
154     JSHandle<JSTaggedValue> key(factory->NewFromASCII("1"));
155     JSHandle<JSTaggedValue> value(thread, JSTaggedValue(1));
156     JSObject::SetProperty(thread, target, key, value);
157 
158     auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 10);
159     ecmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined());
160     ecmaRuntimeCallInfo->SetThis(jsfinalizationRegistry.GetTaggedValue());
161     ecmaRuntimeCallInfo->SetCallArg(0, target.GetTaggedValue());
162     ecmaRuntimeCallInfo->SetCallArg(1, JSTaggedValue(10));
163     ecmaRuntimeCallInfo->SetCallArg(2, target.GetTaggedValue());
164 
165     [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo);
166     BuiltinsFinalizationRegistry::Register(ecmaRuntimeCallInfo);
167     ASSERT_EQ(testValue, 0);
168 }
169 
170 // finalizationRegistry.Register(target, heldValue [ , unregisterToken ])
HWTEST_F_L0(BuiltinsFinalizationRegistryTest,Register2)171 HWTEST_F_L0(BuiltinsFinalizationRegistryTest, Register2)
172 {
173     testValue = 0;
174     EcmaVM *vm = thread->GetEcmaVM();
175     ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
176     JSHandle<GlobalEnv> env = thread->GetEcmaVM()->GetGlobalEnv();
177     JSHandle<JSTaggedValue> objectFunc = env->GetObjectFunction();
178 
179     JSTaggedValue result = CreateFinalizationRegistryConstructor(thread);
180     JSHandle<JSFinalizationRegistry> jsfinalizationRegistry(thread, result);
181     vm->SetEnableForceGC(false);
182     JSTaggedValue target = JSTaggedValue::Undefined();
183     {
184         [[maybe_unused]] EcmaHandleScope handleScope(thread);
185         auto obj =
186             factory->NewJSObjectByConstructor(JSHandle<JSFunction>(objectFunc), objectFunc);
187         target = obj.GetTaggedValue();
188         auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 10);
189         ecmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined());
190         ecmaRuntimeCallInfo->SetThis(jsfinalizationRegistry.GetTaggedValue());
191         ecmaRuntimeCallInfo->SetCallArg(0, target);
192         ecmaRuntimeCallInfo->SetCallArg(1, JSTaggedValue(10));
193         ecmaRuntimeCallInfo->SetCallArg(2, target);
194 
195         [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo);
196         BuiltinsFinalizationRegistry::Register(ecmaRuntimeCallInfo);
197         TestHelper::TearDownFrame(thread, prev);
198     }
199     vm->CollectGarbage(TriggerGCType::FULL_GC);
200     if (!thread->HasPendingException()) {
201         job::MicroJobQueue::ExecutePendingJob(thread, vm->GetJSThread()->GetCurrentEcmaContext()->GetMicroJobQueue());
202     }
203     vm->SetEnableForceGC(true);
204     ASSERT_EQ(testValue, 1);
205 }
206 
207 // finalizationRegistry.Register(target, heldValue [ , unregisterToken ])
HWTEST_F_L0(BuiltinsFinalizationRegistryTest,Register3)208 HWTEST_F_L0(BuiltinsFinalizationRegistryTest, Register3)
209 {
210     testValue = 0;
211     EcmaVM *vm = thread->GetEcmaVM();
212     ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
213     JSHandle<GlobalEnv> env = thread->GetEcmaVM()->GetGlobalEnv();
214     JSHandle<JSTaggedValue> objectFunc = env->GetObjectFunction();
215 
216     JSTaggedValue result = CreateFinalizationRegistryConstructor(thread);
217     JSHandle<JSFinalizationRegistry> jsfinalizationRegistry(thread, result);
218 
219     vm->SetEnableForceGC(false);
220     JSTaggedValue target = JSTaggedValue::Undefined();
221     JSTaggedValue target1 = JSTaggedValue::Undefined();
222     {
223         [[maybe_unused]] EcmaHandleScope handleScope(thread);
224         auto obj =
225             factory->NewJSObjectByConstructor(JSHandle<JSFunction>(objectFunc), objectFunc);
226         auto obj1 =
227             factory->NewJSObjectByConstructor(JSHandle<JSFunction>(objectFunc), objectFunc);
228         target = obj.GetTaggedValue();
229         target1 = obj1.GetTaggedValue();
230         auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 10);
231         ecmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined());
232         ecmaRuntimeCallInfo->SetThis(jsfinalizationRegistry.GetTaggedValue());
233         ecmaRuntimeCallInfo->SetCallArg(0, target);
234         ecmaRuntimeCallInfo->SetCallArg(1, JSTaggedValue(10));
235         ecmaRuntimeCallInfo->SetCallArg(2, target);
236 
237         [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo);
238         BuiltinsFinalizationRegistry::Register(ecmaRuntimeCallInfo);
239         TestHelper::TearDownFrame(thread, prev);
240 
241         auto ecmaRuntimeCallInfo1 = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 10);
242         ecmaRuntimeCallInfo1->SetFunction(JSTaggedValue::Undefined());
243         ecmaRuntimeCallInfo1->SetThis(jsfinalizationRegistry.GetTaggedValue());
244         ecmaRuntimeCallInfo1->SetCallArg(0, target1);
245         ecmaRuntimeCallInfo1->SetCallArg(1, JSTaggedValue(10));
246         ecmaRuntimeCallInfo1->SetCallArg(2, target1);
247 
248         [[maybe_unused]] auto prev1 = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo1);
249         BuiltinsFinalizationRegistry::Register(ecmaRuntimeCallInfo1);
250         TestHelper::TearDownFrame(thread, prev1);
251     }
252     vm->CollectGarbage(TriggerGCType::FULL_GC);
253     if (!thread->HasPendingException()) {
254         job::MicroJobQueue::ExecutePendingJob(thread, vm->GetJSThread()->GetCurrentEcmaContext()->GetMicroJobQueue());
255     }
256     vm->SetEnableForceGC(true);
257     ASSERT_EQ(testValue, 2);
258 }
259 
260 // finalizationRegistry.Register(target, heldValue [ , unregisterToken ])
HWTEST_F_L0(BuiltinsFinalizationRegistryTest,Register4)261 HWTEST_F_L0(BuiltinsFinalizationRegistryTest, Register4)
262 {
263     testValue = 0;
264     EcmaVM *vm = thread->GetEcmaVM();
265     ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
266     JSHandle<GlobalEnv> env = thread->GetEcmaVM()->GetGlobalEnv();
267     JSHandle<JSTaggedValue> objectFunc = env->GetObjectFunction();
268 
269     JSTaggedValue result = CreateFinalizationRegistryConstructor(thread);
270     JSHandle<JSFinalizationRegistry> jsfinalizationRegistry(thread, result);
271     JSTaggedValue result1 = CreateFinalizationRegistryConstructor(thread);
272     JSHandle<JSFinalizationRegistry> jsfinalizationRegistry1(thread, result1);
273     vm->SetEnableForceGC(false);
274     JSTaggedValue target = JSTaggedValue::Undefined();
275     JSTaggedValue target1 = JSTaggedValue::Undefined();
276     {
277         [[maybe_unused]] EcmaHandleScope handleScope(thread);
278         auto obj =
279             factory->NewJSObjectByConstructor(JSHandle<JSFunction>(objectFunc), objectFunc);
280         auto obj1 =
281             factory->NewJSObjectByConstructor(JSHandle<JSFunction>(objectFunc), objectFunc);
282         target = obj.GetTaggedValue();
283         target1 = obj1.GetTaggedValue();
284         auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 10);
285         ecmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined());
286         ecmaRuntimeCallInfo->SetThis(jsfinalizationRegistry.GetTaggedValue());
287         ecmaRuntimeCallInfo->SetCallArg(0, target);
288         ecmaRuntimeCallInfo->SetCallArg(1, JSTaggedValue(10));
289         ecmaRuntimeCallInfo->SetCallArg(2, target);
290 
291         [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo);
292         BuiltinsFinalizationRegistry::Register(ecmaRuntimeCallInfo);
293         TestHelper::TearDownFrame(thread, prev);
294 
295         auto ecmaRuntimeCallInfo1 = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 10);
296         ecmaRuntimeCallInfo1->SetFunction(JSTaggedValue::Undefined());
297         ecmaRuntimeCallInfo1->SetThis(jsfinalizationRegistry1.GetTaggedValue());
298         ecmaRuntimeCallInfo1->SetCallArg(0, target1);
299         ecmaRuntimeCallInfo1->SetCallArg(1, JSTaggedValue(10));
300         ecmaRuntimeCallInfo1->SetCallArg(2, target1);
301 
302         [[maybe_unused]] auto prev1 = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo1);
303         BuiltinsFinalizationRegistry::Register(ecmaRuntimeCallInfo1);
304         TestHelper::TearDownFrame(thread, prev1);
305     }
306     vm->CollectGarbage(TriggerGCType::FULL_GC);
307     if (!thread->HasPendingException()) {
308         job::MicroJobQueue::ExecutePendingJob(thread, vm->GetJSThread()->GetCurrentEcmaContext()->GetMicroJobQueue());
309     }
310     vm->SetEnableForceGC(true);
311     ASSERT_EQ(testValue, 2);
312 }
313 
314 // finalizationRegistry.Register(target, heldValue [ , unregisterToken ])
HWTEST_F_L0(BuiltinsFinalizationRegistryTest,Register5)315 HWTEST_F_L0(BuiltinsFinalizationRegistryTest, Register5)
316 {
317     testValue = 0;
318     EcmaVM *vm = thread->GetEcmaVM();
319     ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
320     JSHandle<GlobalEnv> env = thread->GetEcmaVM()->GetGlobalEnv();
321     JSHandle<JSTaggedValue> objectFunc = env->GetObjectFunction();
322 
323     JSTaggedValue result = CreateFinalizationRegistryConstructor(thread);
324     JSHandle<JSFinalizationRegistry> jsfinalizationRegistry(thread, result);
325     vm->SetEnableForceGC(false);
326     JSTaggedValue target = JSTaggedValue::Undefined();
327     JSTaggedValue target1 = JSTaggedValue::Undefined();
328     {
329         [[maybe_unused]] EcmaHandleScope handleScope(thread);
330         auto obj =
331             factory->NewJSObjectByConstructor(JSHandle<JSFunction>(objectFunc), objectFunc);
332         auto obj1 =
333             factory->NewJSObjectByConstructor(JSHandle<JSFunction>(objectFunc), objectFunc);
334         target = obj.GetTaggedValue();
335         target1 = obj1.GetTaggedValue();
336         auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 10);
337         ecmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined());
338         ecmaRuntimeCallInfo->SetThis(jsfinalizationRegistry.GetTaggedValue());
339         ecmaRuntimeCallInfo->SetCallArg(0, target);
340         ecmaRuntimeCallInfo->SetCallArg(1, JSTaggedValue(10));
341         ecmaRuntimeCallInfo->SetCallArg(2, target);
342 
343         [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo);
344         BuiltinsFinalizationRegistry::Register(ecmaRuntimeCallInfo);
345         TestHelper::TearDownFrame(thread, prev);
346 
347         auto ecmaRuntimeCallInfo1 = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 10);
348         ecmaRuntimeCallInfo1->SetFunction(JSTaggedValue::Undefined());
349         ecmaRuntimeCallInfo1->SetThis(jsfinalizationRegistry.GetTaggedValue());
350         ecmaRuntimeCallInfo1->SetCallArg(0, target1);
351         ecmaRuntimeCallInfo1->SetCallArg(1, JSTaggedValue(10));
352         ecmaRuntimeCallInfo1->SetCallArg(2, target);
353 
354         [[maybe_unused]] auto prev1 = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo1);
355         BuiltinsFinalizationRegistry::Register(ecmaRuntimeCallInfo1);
356         TestHelper::TearDownFrame(thread, prev1);
357     }
358     vm->CollectGarbage(TriggerGCType::FULL_GC);
359     if (!thread->HasPendingException()) {
360         job::MicroJobQueue::ExecutePendingJob(thread, vm->GetJSThread()->GetCurrentEcmaContext()->GetMicroJobQueue());
361     }
362     vm->SetEnableForceGC(true);
363     ASSERT_EQ(testValue, 2);
364 }
365 
366 // finalizationRegistry.Unregister(unregisterToken ])
HWTEST_F_L0(BuiltinsFinalizationRegistryTest,Unregister1)367 HWTEST_F_L0(BuiltinsFinalizationRegistryTest, Unregister1)
368 {
369     testValue = 0;
370     ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
371     JSHandle<GlobalEnv> env = thread->GetEcmaVM()->GetGlobalEnv();
372     JSHandle<JSTaggedValue> objectFunc = env->GetObjectFunction();
373 
374     JSTaggedValue result = CreateFinalizationRegistryConstructor(thread);
375     JSHandle<JSFinalizationRegistry> jsfinalizationRegistry(thread, result);
376     JSHandle<JSTaggedValue> target(factory->NewJSObjectByConstructor(JSHandle<JSFunction>(objectFunc), objectFunc));
377     JSHandle<JSTaggedValue> key(factory->NewFromASCII("1"));
378     JSHandle<JSTaggedValue> value(thread, JSTaggedValue(1));
379     JSObject::SetProperty(thread, target, key, value);
380 
381     auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 10);
382     ecmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined());
383     ecmaRuntimeCallInfo->SetThis(jsfinalizationRegistry.GetTaggedValue());
384     ecmaRuntimeCallInfo->SetCallArg(0, target.GetTaggedValue());
385     ecmaRuntimeCallInfo->SetCallArg(1, JSTaggedValue(10));
386     ecmaRuntimeCallInfo->SetCallArg(2, target.GetTaggedValue());
387 
388     [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo);
389     BuiltinsFinalizationRegistry::Register(ecmaRuntimeCallInfo);
390     TestHelper::TearDownFrame(thread, prev);
391 
392     auto ecmaRuntimeCallInfo1 = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 6);
393     ecmaRuntimeCallInfo1->SetFunction(JSTaggedValue::Undefined());
394     ecmaRuntimeCallInfo1->SetThis(jsfinalizationRegistry.GetTaggedValue());
395     ecmaRuntimeCallInfo1->SetCallArg(0, target.GetTaggedValue());
396 
397     [[maybe_unused]] auto prev1 = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo1);
398     BuiltinsFinalizationRegistry::Unregister(ecmaRuntimeCallInfo1);
399     ASSERT_EQ(testValue, 0);
400 }
401 
HWTEST_F_L0(BuiltinsFinalizationRegistryTest,Unregister2)402 HWTEST_F_L0(BuiltinsFinalizationRegistryTest, Unregister2)
403 {
404     testValue = 0;
405     EcmaVM *vm = thread->GetEcmaVM();
406     ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
407     JSHandle<GlobalEnv> env = thread->GetEcmaVM()->GetGlobalEnv();
408     JSHandle<JSTaggedValue> objectFunc = env->GetObjectFunction();
409 
410     JSTaggedValue result = CreateFinalizationRegistryConstructor(thread);
411     JSHandle<JSFinalizationRegistry> jsfinalizationRegistry(thread, result);
412     vm->SetEnableForceGC(false);
413     JSTaggedValue target = JSTaggedValue::Undefined();
414     {
415         [[maybe_unused]] EcmaHandleScope handleScope(thread);
416         auto obj =
417             factory->NewJSObjectByConstructor(JSHandle<JSFunction>(objectFunc), objectFunc);
418         target = obj.GetTaggedValue();
419         auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 10);
420         ecmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined());
421         ecmaRuntimeCallInfo->SetThis(jsfinalizationRegistry.GetTaggedValue());
422         ecmaRuntimeCallInfo->SetCallArg(0, target);
423         ecmaRuntimeCallInfo->SetCallArg(1, JSTaggedValue(10));
424         ecmaRuntimeCallInfo->SetCallArg(2, target);
425 
426         [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo);
427         BuiltinsFinalizationRegistry::Register(ecmaRuntimeCallInfo);
428 
429         auto ecmaRuntimeCallInfo1 = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 6);
430         ecmaRuntimeCallInfo1->SetFunction(JSTaggedValue::Undefined());
431         ecmaRuntimeCallInfo1->SetThis(jsfinalizationRegistry.GetTaggedValue());
432         ecmaRuntimeCallInfo1->SetCallArg(0, target);
433 
434         BuiltinsFinalizationRegistry::Unregister(ecmaRuntimeCallInfo1);
435         TestHelper::TearDownFrame(thread, prev);
436     }
437     vm->CollectGarbage(TriggerGCType::FULL_GC);
438     if (!thread->HasPendingException()) {
439         job::MicroJobQueue::ExecutePendingJob(thread, vm->GetJSThread()->GetCurrentEcmaContext()->GetMicroJobQueue());
440     }
441     vm->SetEnableForceGC(true);
442     ASSERT_EQ(testValue, 0);
443 }
444 
445 // finalizationRegistry.Register(target, heldValue [ , unregisterToken ]) target and unregisterToken Symbol
HWTEST_F_L0(BuiltinsFinalizationRegistryTest,RegisterTargetSymbol)446 HWTEST_F_L0(BuiltinsFinalizationRegistryTest, RegisterTargetSymbol)
447 {
448     testValue = 0;
449     EcmaVM *vm = thread->GetEcmaVM();
450 
451     JSTaggedValue result = CreateFinalizationRegistryConstructor(thread);
452     JSHandle<JSFinalizationRegistry> jsfinalizationRegistry(thread, result);
453 
454     vm->SetEnableForceGC(false);
455     JSTaggedValue target = JSTaggedValue::Undefined();
456     JSTaggedValue target1 = JSTaggedValue::Undefined();
457     {
458         [[maybe_unused]] EcmaHandleScope handleScope(thread);
459         JSHandle<JSSymbol> symbol1 = thread->GetEcmaVM()->GetFactory()->NewJSSymbol();
460         JSHandle<JSSymbol> symbol2 = thread->GetEcmaVM()->GetFactory()->NewJSSymbol();
461         target = symbol1.GetTaggedValue();
462         target1 = symbol2.GetTaggedValue();
463         auto ecmaRuntimeCallInfo =
464             TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 10); // 10 means 3 call args
465         ecmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined());
466         ecmaRuntimeCallInfo->SetThis(jsfinalizationRegistry.GetTaggedValue());
467         ecmaRuntimeCallInfo->SetCallArg(0, target);
468         ecmaRuntimeCallInfo->SetCallArg(1, JSTaggedValue(10));
469         ecmaRuntimeCallInfo->SetCallArg(2, target); // 2 means the unregisterToken arg
470 
471         [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo);
472         BuiltinsFinalizationRegistry::Register(ecmaRuntimeCallInfo);
473         TestHelper::TearDownFrame(thread, prev);
474 
475         auto ecmaRuntimeCallInfo1 =
476             TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 10); // 10 means 3 call args
477         ecmaRuntimeCallInfo1->SetFunction(JSTaggedValue::Undefined());
478         ecmaRuntimeCallInfo1->SetThis(jsfinalizationRegistry.GetTaggedValue());
479         ecmaRuntimeCallInfo1->SetCallArg(0, target1);
480         ecmaRuntimeCallInfo1->SetCallArg(1, JSTaggedValue(10));
481         ecmaRuntimeCallInfo1->SetCallArg(2, target1); // 2 means the unregisterToken arg
482 
483         [[maybe_unused]] auto prev1 = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo1);
484         BuiltinsFinalizationRegistry::Register(ecmaRuntimeCallInfo1);
485         TestHelper::TearDownFrame(thread, prev1);
486     }
487     vm->CollectGarbage(TriggerGCType::FULL_GC);
488     if (!thread->HasPendingException()) {
489         job::MicroJobQueue::ExecutePendingJob(thread, vm->GetJSThread()->GetCurrentEcmaContext()->GetMicroJobQueue());
490     }
491     vm->SetEnableForceGC(true);
492     ASSERT_EQ(testValue, 2);
493 }
494 
495 // finalizationRegistry.Unregister(unregisterToken) unregisterToken Symbol
HWTEST_F_L0(BuiltinsFinalizationRegistryTest,UnregisterTokenSymbol)496 HWTEST_F_L0(BuiltinsFinalizationRegistryTest, UnregisterTokenSymbol)
497 {
498     testValue = 0;
499     EcmaVM *vm = thread->GetEcmaVM();
500 
501     JSTaggedValue result = CreateFinalizationRegistryConstructor(thread);
502     JSHandle<JSFinalizationRegistry> jsfinalizationRegistry(thread, result);
503     vm->SetEnableForceGC(false);
504     JSTaggedValue target = JSTaggedValue::Undefined();
505     {
506         [[maybe_unused]] EcmaHandleScope handleScope(thread);
507         JSHandle<JSSymbol> symbol = thread->GetEcmaVM()->GetFactory()->NewJSSymbol();
508         target = symbol.GetTaggedValue();
509         auto ecmaRuntimeCallInfo =
510             TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 10); // 10 means 3 call args
511         ecmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined());
512         ecmaRuntimeCallInfo->SetThis(jsfinalizationRegistry.GetTaggedValue());
513         ecmaRuntimeCallInfo->SetCallArg(0, target);
514         ecmaRuntimeCallInfo->SetCallArg(1, JSTaggedValue(10));
515         ecmaRuntimeCallInfo->SetCallArg(2, target);
516 
517         [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo);
518         BuiltinsFinalizationRegistry::Register(ecmaRuntimeCallInfo);
519 
520         auto ecmaRuntimeCallInfo1 =
521             TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 6); // 6 means 1 call args
522         ecmaRuntimeCallInfo1->SetFunction(JSTaggedValue::Undefined());
523         ecmaRuntimeCallInfo1->SetThis(jsfinalizationRegistry.GetTaggedValue());
524         ecmaRuntimeCallInfo1->SetCallArg(0, target);
525 
526         BuiltinsFinalizationRegistry::Unregister(ecmaRuntimeCallInfo1);
527         TestHelper::TearDownFrame(thread, prev);
528     }
529     vm->CollectGarbage(TriggerGCType::FULL_GC);
530     if (!thread->HasPendingException()) {
531         job::MicroJobQueue::ExecutePendingJob(thread, vm->GetJSThread()->GetCurrentEcmaContext()->GetMicroJobQueue());
532     }
533     vm->SetEnableForceGC(true);
534     ASSERT_EQ(testValue, 0);
535 }
536 }  // namespace panda::test
537