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#include "ecmascript/ecma_vm.h"
164514f5e3Sopenharmony_ci#include "ecmascript/global_env.h"
174514f5e3Sopenharmony_ci#include "ecmascript/js_handle.h"
184514f5e3Sopenharmony_ci#include "ecmascript/mem/space.h"
194514f5e3Sopenharmony_ci#include "ecmascript/mem/verification.h"
204514f5e3Sopenharmony_ci#include "ecmascript/object_factory.h"
214514f5e3Sopenharmony_ci#include "ecmascript/tagged_array-inl.h"
224514f5e3Sopenharmony_ci#include "ecmascript/tests/ecma_test_common.h"
234514f5e3Sopenharmony_ci
244514f5e3Sopenharmony_ciusing namespace panda::ecmascript;
254514f5e3Sopenharmony_ci
264514f5e3Sopenharmony_cinamespace panda::test {
274514f5e3Sopenharmony_ciclass HugeObjectTest : public BaseTestWithScope<false> {
284514f5e3Sopenharmony_cipublic:
294514f5e3Sopenharmony_ci    void SetUp() override
304514f5e3Sopenharmony_ci    {
314514f5e3Sopenharmony_ci        TestHelper::CreateEcmaVMWithScope(instance, thread, scope);
324514f5e3Sopenharmony_ci        thread->GetEcmaVM()->SetEnableForceGC(false);
334514f5e3Sopenharmony_ci        const_cast<Heap *>(thread->GetEcmaVM()->GetHeap())->SetMarkType(MarkType::MARK_FULL);
344514f5e3Sopenharmony_ci    }
354514f5e3Sopenharmony_ci};
364514f5e3Sopenharmony_ci
374514f5e3Sopenharmony_cistatic TaggedArray *LargeArrayTestCreate(JSThread *thread)
384514f5e3Sopenharmony_ci{
394514f5e3Sopenharmony_ci    [[maybe_unused]] ecmascript::EcmaHandleScope scope(thread);
404514f5e3Sopenharmony_ci    static constexpr size_t SIZE = 1ULL << 20UL;
414514f5e3Sopenharmony_ci    JSHandle<TaggedArray> array = thread->GetEcmaVM()->GetFactory()->NewTaggedArray(SIZE);
424514f5e3Sopenharmony_ci    return *array;
434514f5e3Sopenharmony_ci}
444514f5e3Sopenharmony_ci
454514f5e3Sopenharmony_ciHWTEST_F_L0(HugeObjectTest, LargeArrayKeep)
464514f5e3Sopenharmony_ci{
474514f5e3Sopenharmony_ci    TaggedArray *array = LargeArrayTestCreate(thread);
484514f5e3Sopenharmony_ci    EXPECT_TRUE(array != nullptr);
494514f5e3Sopenharmony_ci    JSHandle<TaggedArray> arrayHandle(thread, array);
504514f5e3Sopenharmony_ci    JSHandle<JSObject> newObj(thread, EcmaContainerCommon::JSObjectTestCreate(thread));
514514f5e3Sopenharmony_ci    arrayHandle->Set(thread, 0, newObj.GetTaggedValue());
524514f5e3Sopenharmony_ci    auto ecmaVm = thread->GetEcmaVM();
534514f5e3Sopenharmony_ci    EXPECT_EQ(*arrayHandle, reinterpret_cast<TaggedObject *>(array));
544514f5e3Sopenharmony_ci    ecmaVm->CollectGarbage(TriggerGCType::YOUNG_GC);   // Trigger GC.
554514f5e3Sopenharmony_ci    ecmaVm->CollectGarbage(TriggerGCType::OLD_GC);  // Trigger GC.
564514f5e3Sopenharmony_ci    EXPECT_EQ(*newObj, array->Get(0).GetTaggedObject());
574514f5e3Sopenharmony_ci    EXPECT_EQ(*arrayHandle, reinterpret_cast<TaggedObject *>(array));
584514f5e3Sopenharmony_ci}
594514f5e3Sopenharmony_ci
604514f5e3Sopenharmony_ciHWTEST_F_L0(HugeObjectTest, MultipleArrays)
614514f5e3Sopenharmony_ci{
624514f5e3Sopenharmony_ci    auto ecmaVm = thread->GetEcmaVM();
634514f5e3Sopenharmony_ci    auto heap = ecmaVm->GetHeap();
644514f5e3Sopenharmony_ci    {
654514f5e3Sopenharmony_ci        [[maybe_unused]] ecmascript::EcmaHandleScope scope(thread);
664514f5e3Sopenharmony_ci        for (int i = 0; i <= 14; i++) {
674514f5e3Sopenharmony_ci            JSHandle<TaggedArray> array1(thread, LargeArrayTestCreate(thread));
684514f5e3Sopenharmony_ci        }
694514f5e3Sopenharmony_ci    }
704514f5e3Sopenharmony_ci
714514f5e3Sopenharmony_ci    {
724514f5e3Sopenharmony_ci        [[maybe_unused]] ecmascript::EcmaHandleScope scope(thread);
734514f5e3Sopenharmony_ci        for (int i = 0; i <= 14; i++) {
744514f5e3Sopenharmony_ci            JSHandle<TaggedArray> array2(thread, LargeArrayTestCreate(thread));
754514f5e3Sopenharmony_ci        }
764514f5e3Sopenharmony_ci    }
774514f5e3Sopenharmony_ci
784514f5e3Sopenharmony_ci    {
794514f5e3Sopenharmony_ci        [[maybe_unused]] ecmascript::EcmaHandleScope scope(thread);
804514f5e3Sopenharmony_ci        for (int i = 0; i <= 14; i++) {
814514f5e3Sopenharmony_ci            JSHandle<TaggedArray> array2(thread, LargeArrayTestCreate(thread));
824514f5e3Sopenharmony_ci        }
834514f5e3Sopenharmony_ci    }
844514f5e3Sopenharmony_ci    size_t failCount = 0;
854514f5e3Sopenharmony_ci    VerifyObjectVisitor objVerifier(heap, &failCount);
864514f5e3Sopenharmony_ci    heap->GetHugeObjectSpace()->IterateOverObjects(objVerifier);  // newspace reference the old space
874514f5e3Sopenharmony_ci    EXPECT_EQ(failCount, 0U);
884514f5e3Sopenharmony_ci}
894514f5e3Sopenharmony_ci}  // namespace panda::test
90