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/ecma_vm.h"
17 #include "ecmascript/global_env.h"
18 #include "ecmascript/js_handle.h"
19 #include "ecmascript/object_factory.h"
20 #include "ecmascript/tagged_array-inl.h"
21 #include "ecmascript/tests/ecma_test_common.h"
22
23 using namespace panda::ecmascript;
24
25 namespace panda::test {
26 class WeakRefSemiGCTest : public BaseTestWithScope<false> {
27 };
28
ArrayTestCreate(JSThread *thread)29 static TaggedArray *ArrayTestCreate(JSThread *thread)
30 {
31 [[maybe_unused]] ecmascript::EcmaHandleScope scope(thread);
32 // 2 : test case
33 JSHandle<TaggedArray> array = thread->GetEcmaVM()->GetFactory()->NewTaggedArray(2);
34 return *array;
35 }
36
HWTEST_F_L0(WeakRefSemiGCTest, ArrayUndefined)37 HWTEST_F_L0(WeakRefSemiGCTest, ArrayUndefined)
38 {
39 EcmaTestCommon::ArrayUndefinedGcCommon(thread, TriggerGCType::YOUNG_GC);
40 }
41
HWTEST_F_L0(WeakRefSemiGCTest, ArrayKeep)42 HWTEST_F_L0(WeakRefSemiGCTest, ArrayKeep)
43 {
44 EcmaTestCommon::ArrayKeepGcCommon(thread, TriggerGCType::YOUNG_GC);
45 }
46
HWTEST_F_L0(WeakRefSemiGCTest, ObjectUndefined)47 HWTEST_F_L0(WeakRefSemiGCTest, ObjectUndefined)
48 {
49 JSHandle<JSObject> newObj1(thread, EcmaContainerCommon::JSObjectTestCreate(thread));
50 JSTaggedValue array(ArrayTestCreate(thread));
51 array.CreateWeakRef();
52 newObj1->SetElements(thread, array);
53 EXPECT_EQ(newObj1->GetElements(), array);
54 thread->GetEcmaVM()->CollectGarbage(TriggerGCType::YOUNG_GC);
55 EXPECT_EQ(newObj1->GetElements(), JSTaggedValue::Undefined());
56 }
57
HWTEST_F_L0(WeakRefSemiGCTest, ObjectKeep)58 HWTEST_F_L0(WeakRefSemiGCTest, ObjectKeep)
59 {
60 JSHandle<JSObject> newObj1(thread, EcmaContainerCommon::JSObjectTestCreate(thread));
61 JSHandle<TaggedArray> array(thread, ArrayTestCreate(thread));
62 JSTaggedValue value = array.GetTaggedValue();
63 value.CreateWeakRef();
64 newObj1->SetElements(thread, value);
65 EXPECT_EQ(newObj1->GetElements(), value);
66 thread->GetEcmaVM()->CollectGarbage(TriggerGCType::YOUNG_GC);
67 value = array.GetTaggedValue();
68 value.CreateWeakRef();
69 EXPECT_EQ(newObj1->GetElements(), value);
70 }
71 } // namespace panda::test
72