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/js_handle.h"
184514f5e3Sopenharmony_ci#include "ecmascript/mem/mem_common.h"
194514f5e3Sopenharmony_ci#include "ecmascript/mem/space.h"
204514f5e3Sopenharmony_ci#include "ecmascript/object_factory.h"
214514f5e3Sopenharmony_ci#include "ecmascript/tagged_array-inl.h"
224514f5e3Sopenharmony_ci#include "ecmascript/tests/test_helper.h"
234514f5e3Sopenharmony_ci
244514f5e3Sopenharmony_ciusing namespace panda::ecmascript;
254514f5e3Sopenharmony_ci
264514f5e3Sopenharmony_cinamespace panda::test {
274514f5e3Sopenharmony_ciclass ThrowOOMErrorTest : 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_ciHWTEST_F_L0(ThrowOOMErrorTest, ThrowNonMovableOOMError)
384514f5e3Sopenharmony_ci{
394514f5e3Sopenharmony_ci#ifdef NDEBUG
404514f5e3Sopenharmony_ci    static constexpr size_t SIZE = 100_KB / 8;
414514f5e3Sopenharmony_ci    [[maybe_unused]] ecmascript::EcmaHandleScope scope(thread);
424514f5e3Sopenharmony_ci    for (int i = 0; i < 800; i++) {
434514f5e3Sopenharmony_ci        [[maybe_unused]] JSHandle<TaggedArray> array =
444514f5e3Sopenharmony_ci            thread->GetEcmaVM()->GetFactory()->NewTaggedArray(SIZE, JSTaggedValue::Hole(), MemSpaceType::NON_MOVABLE);
454514f5e3Sopenharmony_ci    }
464514f5e3Sopenharmony_ci
474514f5e3Sopenharmony_ci    EXPECT_TRUE(thread->HasPendingException());
484514f5e3Sopenharmony_ci    JSType errorType = thread->GetException().GetTaggedObject()->GetClass()->GetObjectType();
494514f5e3Sopenharmony_ci    EXPECT_EQ(errorType, JSType::JS_OOM_ERROR);
504514f5e3Sopenharmony_ci#endif
514514f5e3Sopenharmony_ci}
524514f5e3Sopenharmony_ci
534514f5e3Sopenharmony_ciHWTEST_F_L0(ThrowOOMErrorTest, ThrowOldSpaceMergeOOMError)
544514f5e3Sopenharmony_ci{
554514f5e3Sopenharmony_ci    auto ecmaVm = thread->GetEcmaVM();
564514f5e3Sopenharmony_ci    auto heap = const_cast<Heap *>(ecmaVm->GetHeap());
574514f5e3Sopenharmony_ci    auto oldSpace = heap->GetOldSpace();
584514f5e3Sopenharmony_ci    oldSpace->SetMaximumCapacity(6_MB);
594514f5e3Sopenharmony_ci
604514f5e3Sopenharmony_ci    static constexpr size_t SIZE = 100_KB / 8;
614514f5e3Sopenharmony_ci    [[maybe_unused]] ecmascript::EcmaHandleScope scope(thread);
624514f5e3Sopenharmony_ci
634514f5e3Sopenharmony_ci    for (int i = 0; i < 46; i++) {
644514f5e3Sopenharmony_ci        [[maybe_unused]] JSHandle<TaggedArray> array =
654514f5e3Sopenharmony_ci            thread->GetEcmaVM()->GetFactory()->NewTaggedArray(SIZE, JSTaggedValue::Hole(), MemSpaceType::OLD_SPACE);
664514f5e3Sopenharmony_ci    }
674514f5e3Sopenharmony_ci
684514f5e3Sopenharmony_ci    EXPECT_TRUE(!thread->HasPendingException());
694514f5e3Sopenharmony_ci
704514f5e3Sopenharmony_ci    for (int i = 0; i < 4; i++) {
714514f5e3Sopenharmony_ci        [[maybe_unused]] JSHandle<TaggedArray> array =
724514f5e3Sopenharmony_ci            thread->GetEcmaVM()->GetFactory()->NewTaggedArray(SIZE, JSTaggedValue::Hole(), MemSpaceType::SEMI_SPACE);
734514f5e3Sopenharmony_ci    }
744514f5e3Sopenharmony_ci
754514f5e3Sopenharmony_ci    heap->CollectGarbage(TriggerGCType::YOUNG_GC);
764514f5e3Sopenharmony_ci    heap->Prepare();
774514f5e3Sopenharmony_ci    heap->CollectGarbage(TriggerGCType::YOUNG_GC);
784514f5e3Sopenharmony_ci    heap->Prepare();
794514f5e3Sopenharmony_ci
804514f5e3Sopenharmony_ci    EXPECT_TRUE(thread->HasPendingException());
814514f5e3Sopenharmony_ci    JSType errorType = thread->GetException().GetTaggedObject()->GetClass()->GetObjectType();
824514f5e3Sopenharmony_ci    EXPECT_EQ(errorType, JSType::JS_OOM_ERROR);
834514f5e3Sopenharmony_ci}
844514f5e3Sopenharmony_ci}  // namespace panda::test
85