14514f5e3Sopenharmony_ci/*
24514f5e3Sopenharmony_ci * Copyright (c) 2021 Huawei Device Co., Ltd.
34514f5e3Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
44514f5e3Sopenharmony_ci * you may not use this file except in compliance with the License.
54514f5e3Sopenharmony_ci * You may obtain a copy of the License at
64514f5e3Sopenharmony_ci *
74514f5e3Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
84514f5e3Sopenharmony_ci *
94514f5e3Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
104514f5e3Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
114514f5e3Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
124514f5e3Sopenharmony_ci * See the License for the specific language governing permissions and
134514f5e3Sopenharmony_ci * limitations under the License.
144514f5e3Sopenharmony_ci */
154514f5e3Sopenharmony_ci
164514f5e3Sopenharmony_ci#include "ecmascript/ecma_vm.h"
174514f5e3Sopenharmony_ci#include "ecmascript/global_env.h"
184514f5e3Sopenharmony_ci#include "ecmascript/js_handle.h"
194514f5e3Sopenharmony_ci#include "ecmascript/object_factory.h"
204514f5e3Sopenharmony_ci#include "ecmascript/tagged_array-inl.h"
214514f5e3Sopenharmony_ci#include "ecmascript/tests/ecma_test_common.h"
224514f5e3Sopenharmony_ci
234514f5e3Sopenharmony_ciusing namespace panda::ecmascript;
244514f5e3Sopenharmony_ci
254514f5e3Sopenharmony_cinamespace panda::test {
264514f5e3Sopenharmony_ciclass WeakRefOldGCTest : public BaseTestWithScope<false> {
274514f5e3Sopenharmony_ci};
284514f5e3Sopenharmony_ci
294514f5e3Sopenharmony_cistatic JSObject *JSObjectTestCreate(JSThread *thread)
304514f5e3Sopenharmony_ci{
314514f5e3Sopenharmony_ci    [[maybe_unused]] ecmascript::EcmaHandleScope scope(thread);
324514f5e3Sopenharmony_ci    EcmaVM *ecmaVM = thread->GetEcmaVM();
334514f5e3Sopenharmony_ci    auto globalEnv = ecmaVM->GetGlobalEnv();
344514f5e3Sopenharmony_ci    JSFunction *jsFunc = globalEnv->GetObjectFunction().GetObject<JSFunction>();
354514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> jsFunc1(thread, jsFunc);
364514f5e3Sopenharmony_ci    JSHandle<JSObject> newObj =
374514f5e3Sopenharmony_ci        ecmaVM->GetFactory()->NewJSObjectByConstructor(JSHandle<JSFunction>(jsFunc1), jsFunc1);
384514f5e3Sopenharmony_ci    return *newObj;
394514f5e3Sopenharmony_ci}
404514f5e3Sopenharmony_ci
414514f5e3Sopenharmony_cistatic TaggedArray *ArrayTestCreate(JSThread *thread)
424514f5e3Sopenharmony_ci{
434514f5e3Sopenharmony_ci    [[maybe_unused]] ecmascript::EcmaHandleScope scope(thread);
444514f5e3Sopenharmony_ci    // 2 : test case
454514f5e3Sopenharmony_ci    JSHandle<TaggedArray> array = thread->GetEcmaVM()->GetFactory()->NewTaggedArray(2);
464514f5e3Sopenharmony_ci    return *array;
474514f5e3Sopenharmony_ci}
484514f5e3Sopenharmony_ci
494514f5e3Sopenharmony_ciHWTEST_F_L0(WeakRefOldGCTest, ArrayNonMovable)
504514f5e3Sopenharmony_ci{
514514f5e3Sopenharmony_ci    auto vm = thread->GetEcmaVM();
524514f5e3Sopenharmony_ci    auto array = vm->GetFactory()->NewTaggedArray(2, JSTaggedValue::Undefined(), true);
534514f5e3Sopenharmony_ci    JSHandle<JSObject> newObj1(thread, JSObjectTestCreate(thread));
544514f5e3Sopenharmony_ci    array->Set(thread, 0, newObj1.GetTaggedValue());
554514f5e3Sopenharmony_ci
564514f5e3Sopenharmony_ci    JSObject *newObj2 = JSObjectTestCreate(thread);
574514f5e3Sopenharmony_ci    JSTaggedValue value(newObj2);
584514f5e3Sopenharmony_ci    value.CreateWeakRef();
594514f5e3Sopenharmony_ci    array->Set(thread, 1, value);
604514f5e3Sopenharmony_ci    EXPECT_EQ(newObj1.GetTaggedValue(), array->Get(0));
614514f5e3Sopenharmony_ci    EXPECT_EQ(value, array->Get(1));
624514f5e3Sopenharmony_ci    vm->CollectGarbage(TriggerGCType::OLD_GC);
634514f5e3Sopenharmony_ci    EXPECT_EQ(newObj1.GetTaggedValue(), array->Get(0));
644514f5e3Sopenharmony_ci    EXPECT_EQ(JSTaggedValue::Undefined(), array->Get(1));
654514f5e3Sopenharmony_ci}
664514f5e3Sopenharmony_ci
674514f5e3Sopenharmony_ciHWTEST_F_L0(WeakRefOldGCTest, ArrayUndefined)
684514f5e3Sopenharmony_ci{
694514f5e3Sopenharmony_ci    EcmaTestCommon::ArrayUndefinedGcCommon(thread, TriggerGCType::OLD_GC);
704514f5e3Sopenharmony_ci}
714514f5e3Sopenharmony_ci
724514f5e3Sopenharmony_ciHWTEST_F_L0(WeakRefOldGCTest, ArrayKeep)
734514f5e3Sopenharmony_ci{
744514f5e3Sopenharmony_ci    EcmaTestCommon::ArrayKeepGcCommon(thread, TriggerGCType::OLD_GC);
754514f5e3Sopenharmony_ci}
764514f5e3Sopenharmony_ci
774514f5e3Sopenharmony_ciHWTEST_F_L0(WeakRefOldGCTest, ObjectUndefined)
784514f5e3Sopenharmony_ci{
794514f5e3Sopenharmony_ci    JSHandle<JSObject> newObj1(thread, JSObjectTestCreate(thread));
804514f5e3Sopenharmony_ci    JSTaggedValue array(ArrayTestCreate(thread));
814514f5e3Sopenharmony_ci    array.CreateWeakRef();
824514f5e3Sopenharmony_ci    newObj1->SetElements(thread, array);
834514f5e3Sopenharmony_ci    EXPECT_EQ(newObj1->GetElements(), array);
844514f5e3Sopenharmony_ci    thread->GetEcmaVM()->CollectGarbage(TriggerGCType::OLD_GC);
854514f5e3Sopenharmony_ci    EXPECT_EQ(newObj1->GetElements(), JSTaggedValue::Undefined());
864514f5e3Sopenharmony_ci}
874514f5e3Sopenharmony_ci
884514f5e3Sopenharmony_ciHWTEST_F_L0(WeakRefOldGCTest, ObjectKeep)
894514f5e3Sopenharmony_ci{
904514f5e3Sopenharmony_ci    JSHandle<JSObject> newObj1(thread, JSObjectTestCreate(thread));
914514f5e3Sopenharmony_ci    JSHandle<TaggedArray> array(thread, ArrayTestCreate(thread));
924514f5e3Sopenharmony_ci    JSTaggedValue value = array.GetTaggedValue();
934514f5e3Sopenharmony_ci    value.CreateWeakRef();
944514f5e3Sopenharmony_ci    newObj1->SetElements(thread, value);
954514f5e3Sopenharmony_ci    EXPECT_EQ(newObj1->GetElements(), value);
964514f5e3Sopenharmony_ci    thread->GetEcmaVM()->CollectGarbage(TriggerGCType::OLD_GC);
974514f5e3Sopenharmony_ci    value = array.GetTaggedValue();
984514f5e3Sopenharmony_ci    value.CreateWeakRef();
994514f5e3Sopenharmony_ci    EXPECT_EQ(newObj1->GetElements(), value);
1004514f5e3Sopenharmony_ci}
1014514f5e3Sopenharmony_ci}  // namespace panda::test
102