14514f5e3Sopenharmony_ci/*
24514f5e3Sopenharmony_ci * Copyright (c) 2023 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/jspandafile/program_object.h"
174514f5e3Sopenharmony_ci#include "ecmascript/layout_info-inl.h"
184514f5e3Sopenharmony_ci#include "ecmascript/mem/heap-inl.h"
194514f5e3Sopenharmony_ci#include "ecmascript/runtime.h"
204514f5e3Sopenharmony_ci#include "ecmascript/symbol_table.h"
214514f5e3Sopenharmony_ci#include "ecmascript/jspandafile/program_object.h"
224514f5e3Sopenharmony_ci
234514f5e3Sopenharmony_ci// class Object;
244514f5e3Sopenharmony_cinamespace panda::ecmascript {
254514f5e3Sopenharmony_civoid ObjectFactory::NewSObjectHook() const
264514f5e3Sopenharmony_ci{
274514f5e3Sopenharmony_ci    CHECK_NO_HEAP_ALLOC;
284514f5e3Sopenharmony_ci#ifndef NDEBUG
294514f5e3Sopenharmony_ci    static std::atomic<uint32_t> count = 0;
304514f5e3Sopenharmony_ci    static uint32_t frequency = vm_->GetJSOptions().GetForceSharedGCFrequency();
314514f5e3Sopenharmony_ci    static constexpr uint32_t CONCURRENT_MARK_FREQUENCY_FACTOR = 2;
324514f5e3Sopenharmony_ci    if (frequency == 0 || !vm_->GetJSOptions().EnableForceGC() || !vm_->IsInitialized() ||
334514f5e3Sopenharmony_ci        !thread_->IsAllContextsInitialized()) {
344514f5e3Sopenharmony_ci        return;
354514f5e3Sopenharmony_ci    }
364514f5e3Sopenharmony_ci    if (count++ % frequency == 0) {
374514f5e3Sopenharmony_ci        if (count % (CONCURRENT_MARK_FREQUENCY_FACTOR * frequency) == 0) {
384514f5e3Sopenharmony_ci            sHeap_->CollectGarbage<TriggerGCType::SHARED_GC, GCReason::OTHER>(thread_);
394514f5e3Sopenharmony_ci        } else if (sHeap_->CheckCanTriggerConcurrentMarking(thread_)) {
404514f5e3Sopenharmony_ci            sHeap_->TriggerConcurrentMarking<TriggerGCType::SHARED_GC, GCReason::OTHER>(thread_);
414514f5e3Sopenharmony_ci        }
424514f5e3Sopenharmony_ci        if (!ecmascript::AnFileDataManager::GetInstance()->IsEnable()) {
434514f5e3Sopenharmony_ci            sHeap_->WaitGCFinished(thread_);
444514f5e3Sopenharmony_ci            sHeap_->CollectGarbage<TriggerGCType::SHARED_FULL_GC, GCReason::OTHER>(thread_);
454514f5e3Sopenharmony_ci        }
464514f5e3Sopenharmony_ci    }
474514f5e3Sopenharmony_ci#endif
484514f5e3Sopenharmony_ci}
494514f5e3Sopenharmony_ci
504514f5e3Sopenharmony_ciJSHandle<JSHClass> ObjectFactory::CreateSFunctionClass(uint32_t size, JSType type,
514514f5e3Sopenharmony_ci                                                       const JSHandle<JSTaggedValue> &prototype,
524514f5e3Sopenharmony_ci                                                       bool isAccessor, bool setProto)
534514f5e3Sopenharmony_ci{
544514f5e3Sopenharmony_ci    const GlobalEnvConstants *globalConst = thread_->GlobalConstants();
554514f5e3Sopenharmony_ci    uint32_t fieldOrder = 0;
564514f5e3Sopenharmony_ci    ASSERT(JSFunction::LENGTH_INLINE_PROPERTY_INDEX == fieldOrder);
574514f5e3Sopenharmony_ci    PropertyAttributes attributes = PropertyAttributes::Default(false, false, false);
584514f5e3Sopenharmony_ci    attributes.SetIsAccessor(isAccessor);
594514f5e3Sopenharmony_ci    attributes.SetIsInlinedProps(true);
604514f5e3Sopenharmony_ci    attributes.SetRepresentation(Representation::TAGGED);
614514f5e3Sopenharmony_ci    JSHandle<LayoutInfo> layoutInfoHandle = CreateSLayoutInfo(JSFunction::LENGTH_OF_INLINE_PROPERTIES);
624514f5e3Sopenharmony_ci    {
634514f5e3Sopenharmony_ci        attributes.SetOffset(fieldOrder);
644514f5e3Sopenharmony_ci        layoutInfoHandle->AddKey(thread_, fieldOrder, globalConst->GetLengthString(), attributes);
654514f5e3Sopenharmony_ci        fieldOrder++;
664514f5e3Sopenharmony_ci    }
674514f5e3Sopenharmony_ci
684514f5e3Sopenharmony_ci    ASSERT(JSFunction::NAME_INLINE_PROPERTY_INDEX == fieldOrder);
694514f5e3Sopenharmony_ci    {
704514f5e3Sopenharmony_ci        attributes.SetOffset(fieldOrder);
714514f5e3Sopenharmony_ci        layoutInfoHandle->AddKey(thread_, fieldOrder,
724514f5e3Sopenharmony_ci                                 globalConst->GetHandledNameString().GetTaggedValue(), attributes);
734514f5e3Sopenharmony_ci        fieldOrder++;
744514f5e3Sopenharmony_ci    }
754514f5e3Sopenharmony_ci    if (setProto) {
764514f5e3Sopenharmony_ci        ASSERT(JSFunction::PROTOTYPE_INLINE_PROPERTY_INDEX == fieldOrder);
774514f5e3Sopenharmony_ci        {
784514f5e3Sopenharmony_ci            attributes.SetOffset(fieldOrder);
794514f5e3Sopenharmony_ci            layoutInfoHandle->AddKey(thread_, fieldOrder,
804514f5e3Sopenharmony_ci                                     globalConst->GetPrototypeString(), attributes);
814514f5e3Sopenharmony_ci            fieldOrder++;
824514f5e3Sopenharmony_ci        }
834514f5e3Sopenharmony_ci    }
844514f5e3Sopenharmony_ci    JSHandle<JSHClass> functionClass = NewSEcmaHClass(size, fieldOrder, type, prototype,
854514f5e3Sopenharmony_ci        JSHandle<JSTaggedValue>(layoutInfoHandle));
864514f5e3Sopenharmony_ci    functionClass->SetCallable(true);
874514f5e3Sopenharmony_ci    return functionClass;
884514f5e3Sopenharmony_ci}
894514f5e3Sopenharmony_ci
904514f5e3Sopenharmony_ciJSHandle<JSHClass> ObjectFactory::NewSEcmaHClass(uint32_t size, JSType type, uint32_t inlinedProps)
914514f5e3Sopenharmony_ci{
924514f5e3Sopenharmony_ci    return NewSEcmaHClass(JSHClass::Cast(thread_->GlobalConstants()->GetHClassClass().GetTaggedObject()),
934514f5e3Sopenharmony_ci                          size, type, inlinedProps);
944514f5e3Sopenharmony_ci}
954514f5e3Sopenharmony_ci
964514f5e3Sopenharmony_ciJSHandle<JSHClass> ObjectFactory::NewSEcmaHClass(JSHClass *hclass, uint32_t size, JSType type, uint32_t inlinedProps)
974514f5e3Sopenharmony_ci{
984514f5e3Sopenharmony_ci    NewSObjectHook();
994514f5e3Sopenharmony_ci    uint32_t classSize = JSHClass::SIZE;
1004514f5e3Sopenharmony_ci    auto *newClass = static_cast<JSHClass *>(sHeap_->AllocateNonMovableOrHugeObject(thread_, hclass, classSize));
1014514f5e3Sopenharmony_ci    newClass->Initialize(thread_, size, type, inlinedProps, thread_->GlobalConstants()->GetHandledEmptySLayoutInfo());
1024514f5e3Sopenharmony_ci    return JSHandle<JSHClass>(thread_, newClass);
1034514f5e3Sopenharmony_ci}
1044514f5e3Sopenharmony_ci
1054514f5e3Sopenharmony_ci// This function don't UpdateProtoClass
1064514f5e3Sopenharmony_ciJSHandle<JSHClass> ObjectFactory::NewSEcmaHClass(uint32_t size, uint32_t inlinedProps, JSType type,
1074514f5e3Sopenharmony_ci    const JSHandle<JSTaggedValue> &prototype, const JSHandle<JSTaggedValue> &layout)
1084514f5e3Sopenharmony_ci{
1094514f5e3Sopenharmony_ci    NewSObjectHook();
1104514f5e3Sopenharmony_ci    uint32_t classSize = JSHClass::SIZE;
1114514f5e3Sopenharmony_ci    auto *newClass = static_cast<JSHClass *>(sHeap_->AllocateNonMovableOrHugeObject(
1124514f5e3Sopenharmony_ci        thread_, JSHClass::Cast(thread_->GlobalConstants()->GetHClassClass().GetTaggedObject()), classSize));
1134514f5e3Sopenharmony_ci    newClass->Initialize(thread_, size, type, inlinedProps, layout);
1144514f5e3Sopenharmony_ci    JSHandle<JSHClass> hclass(thread_, newClass);
1154514f5e3Sopenharmony_ci    if (prototype->IsJSObject()) {
1164514f5e3Sopenharmony_ci        prototype->GetTaggedObject()->GetClass()->SetIsPrototype(true);
1174514f5e3Sopenharmony_ci    }
1184514f5e3Sopenharmony_ci    hclass->SetProto(thread_, prototype.GetTaggedValue());
1194514f5e3Sopenharmony_ci    hclass->SetNumberOfProps(inlinedProps);
1204514f5e3Sopenharmony_ci    hclass->SetExtensible(false);
1214514f5e3Sopenharmony_ci    return hclass;
1224514f5e3Sopenharmony_ci}
1234514f5e3Sopenharmony_ci
1244514f5e3Sopenharmony_ciJSHandle<JSHClass> ObjectFactory::NewSEcmaHClassDictMode(uint32_t size, uint32_t inlinedProps, JSType type,
1254514f5e3Sopenharmony_ci                                                         const JSHandle<JSTaggedValue> &prototype)
1264514f5e3Sopenharmony_ci{
1274514f5e3Sopenharmony_ci    NewSObjectHook();
1284514f5e3Sopenharmony_ci    uint32_t classSize = JSHClass::SIZE;
1294514f5e3Sopenharmony_ci    auto *newClass = static_cast<JSHClass *>(sHeap_->AllocateNonMovableOrHugeObject(thread_,
1304514f5e3Sopenharmony_ci        JSHClass::Cast(thread_->GlobalConstants()->GetHClassClass().GetTaggedObject()), classSize));
1314514f5e3Sopenharmony_ci    newClass->Initialize(thread_, size, type, inlinedProps, thread_->GlobalConstants()->GetHandledEmptySLayoutInfo());
1324514f5e3Sopenharmony_ci    JSHandle<JSHClass> hclass(thread_, newClass);
1334514f5e3Sopenharmony_ci    if (prototype->IsJSObject()) {
1344514f5e3Sopenharmony_ci        prototype->GetTaggedObject()->GetClass()->SetIsPrototype(true);
1354514f5e3Sopenharmony_ci    }
1364514f5e3Sopenharmony_ci    hclass->SetProto(thread_, prototype.GetTaggedValue());
1374514f5e3Sopenharmony_ci    hclass->SetNumberOfProps(0);
1384514f5e3Sopenharmony_ci    hclass->SetIsDictionaryMode(true);
1394514f5e3Sopenharmony_ci    hclass->SetExtensible(false);
1404514f5e3Sopenharmony_ci    return hclass;
1414514f5e3Sopenharmony_ci}
1424514f5e3Sopenharmony_ci
1434514f5e3Sopenharmony_ciJSHandle<JSHClass> ObjectFactory::NewSEcmaHClassClass(JSHClass *hclass, uint32_t size, JSType type)
1444514f5e3Sopenharmony_ci{
1454514f5e3Sopenharmony_ci    NewSObjectHook();
1464514f5e3Sopenharmony_ci    uint32_t classSize = JSHClass::SIZE;
1474514f5e3Sopenharmony_ci    auto *newClass = static_cast<JSHClass *>(sHeap_->AllocateClassClass(thread_, hclass, classSize));
1484514f5e3Sopenharmony_ci    newClass->Initialize(thread_, size, type, 0, thread_->GlobalConstants()->GetHandledEmptySLayoutInfo());
1494514f5e3Sopenharmony_ci    return JSHandle<JSHClass>(thread_, newClass);
1504514f5e3Sopenharmony_ci}
1514514f5e3Sopenharmony_ci
1524514f5e3Sopenharmony_ciJSHandle<JSHClass> ObjectFactory::NewSEcmaReadOnlyHClass(JSHClass *hclass, uint32_t size, JSType type,
1534514f5e3Sopenharmony_ci                                                         uint32_t inlinedProps)
1544514f5e3Sopenharmony_ci{
1554514f5e3Sopenharmony_ci    NewSObjectHook();
1564514f5e3Sopenharmony_ci    uint32_t classSize = JSHClass::SIZE;
1574514f5e3Sopenharmony_ci    auto *newClass = static_cast<JSHClass *>(sHeap_->AllocateReadOnlyOrHugeObject(thread_, hclass, classSize));
1584514f5e3Sopenharmony_ci    ASSERT(newClass != nullptr);
1594514f5e3Sopenharmony_ci    newClass->Initialize(thread_, size, type, inlinedProps, thread_->GlobalConstants()->GetHandledEmptySLayoutInfo());
1604514f5e3Sopenharmony_ci    return JSHandle<JSHClass>(thread_, newClass);
1614514f5e3Sopenharmony_ci}
1624514f5e3Sopenharmony_ci
1634514f5e3Sopenharmony_ciJSHandle<JSHClass> ObjectFactory::InitSClassClass()
1644514f5e3Sopenharmony_ci{
1654514f5e3Sopenharmony_ci    JSHandle<JSHClass> hClassHandle = NewSEcmaHClassClass(nullptr, JSHClass::SIZE, JSType::HCLASS);
1664514f5e3Sopenharmony_ci    JSHClass *hclass = reinterpret_cast<JSHClass *>(hClassHandle.GetTaggedValue().GetTaggedObject());
1674514f5e3Sopenharmony_ci    hclass->SetClass(thread_, hclass);
1684514f5e3Sopenharmony_ci    return hClassHandle;
1694514f5e3Sopenharmony_ci}
1704514f5e3Sopenharmony_ci
1714514f5e3Sopenharmony_ciJSHandle<AccessorData> ObjectFactory::NewSAccessorData()
1724514f5e3Sopenharmony_ci{
1734514f5e3Sopenharmony_ci    NewSObjectHook();
1744514f5e3Sopenharmony_ci    TaggedObject *header = sHeap_->AllocateOldOrHugeObject(
1754514f5e3Sopenharmony_ci        thread_, JSHClass::Cast(thread_->GlobalConstants()->GetAccessorDataClass().GetTaggedObject()));
1764514f5e3Sopenharmony_ci    JSHandle<AccessorData> acc(thread_, AccessorData::Cast(header));
1774514f5e3Sopenharmony_ci    acc->SetGetter(thread_, JSTaggedValue::Undefined());
1784514f5e3Sopenharmony_ci    acc->SetSetter(thread_, JSTaggedValue::Undefined());
1794514f5e3Sopenharmony_ci    return acc;
1804514f5e3Sopenharmony_ci}
1814514f5e3Sopenharmony_ci
1824514f5e3Sopenharmony_ciJSHandle<Method> ObjectFactory::NewSMethod(const JSPandaFile *jsPandaFile, MethodLiteral *methodLiteral,
1834514f5e3Sopenharmony_ci                                           JSHandle<ConstantPool> constpool, uint32_t entryIndex,
1844514f5e3Sopenharmony_ci                                           bool needSetAotFlag, bool *canFastCall)
1854514f5e3Sopenharmony_ci{
1864514f5e3Sopenharmony_ci    JSHandle<Method> method;
1874514f5e3Sopenharmony_ci    if (jsPandaFile->IsNewVersion()) {
1884514f5e3Sopenharmony_ci        method = Method::Create(thread_, jsPandaFile, methodLiteral);
1894514f5e3Sopenharmony_ci    } else {
1904514f5e3Sopenharmony_ci        method = NewSMethod(methodLiteral);
1914514f5e3Sopenharmony_ci        method->SetConstantPool(thread_, constpool);
1924514f5e3Sopenharmony_ci    }
1934514f5e3Sopenharmony_ci    if (needSetAotFlag) {
1944514f5e3Sopenharmony_ci        auto aotFileManager = thread_->GetEcmaVM()->GetAOTFileManager();
1954514f5e3Sopenharmony_ci        aotFileManager->SetAOTFuncEntry(jsPandaFile, nullptr, *method, entryIndex, canFastCall);
1964514f5e3Sopenharmony_ci    } else {
1974514f5e3Sopenharmony_ci        method->InitInterpreterStatusForCompiledMethod(thread_);
1984514f5e3Sopenharmony_ci    }
1994514f5e3Sopenharmony_ci    return method;
2004514f5e3Sopenharmony_ci}
2014514f5e3Sopenharmony_ci
2024514f5e3Sopenharmony_ciJSHandle<Method> ObjectFactory::NewSMethod(const MethodLiteral *methodLiteral, MemSpaceType spaceType)
2034514f5e3Sopenharmony_ci{
2044514f5e3Sopenharmony_ci    ASSERT(spaceType == SHARED_NON_MOVABLE || spaceType == SHARED_OLD_SPACE);
2054514f5e3Sopenharmony_ci    NewSObjectHook();
2064514f5e3Sopenharmony_ci    TaggedObject *header = nullptr;
2074514f5e3Sopenharmony_ci    if (spaceType == SHARED_NON_MOVABLE) {
2084514f5e3Sopenharmony_ci        header = sHeap_->AllocateNonMovableOrHugeObject(thread_,
2094514f5e3Sopenharmony_ci            JSHClass::Cast(thread_->GlobalConstants()->GetMethodClass().GetTaggedObject()));
2104514f5e3Sopenharmony_ci    } else {
2114514f5e3Sopenharmony_ci        header = sHeap_->AllocateOldOrHugeObject(thread_,
2124514f5e3Sopenharmony_ci            JSHClass::Cast(thread_->GlobalConstants()->GetMethodClass().GetTaggedObject()));
2134514f5e3Sopenharmony_ci    }
2144514f5e3Sopenharmony_ci    JSHandle<Method> method(thread_, header);
2154514f5e3Sopenharmony_ci    InitializeMethod(methodLiteral, method);
2164514f5e3Sopenharmony_ci    return method;
2174514f5e3Sopenharmony_ci}
2184514f5e3Sopenharmony_ci
2194514f5e3Sopenharmony_ciJSHandle<Method> ObjectFactory::NewSMethodForNativeFunction(const void *func, FunctionKind kind,
2204514f5e3Sopenharmony_ci                                                            kungfu::BuiltinsStubCSigns::ID builtinId,
2214514f5e3Sopenharmony_ci                                                            MemSpaceType spaceType)
2224514f5e3Sopenharmony_ci{
2234514f5e3Sopenharmony_ci    uint32_t numArgs = 2;  // function object and this
2244514f5e3Sopenharmony_ci    auto method = NewSMethod(nullptr, spaceType);
2254514f5e3Sopenharmony_ci    method->SetNativePointer(const_cast<void *>(func));
2264514f5e3Sopenharmony_ci    method->SetNativeBit(true);
2274514f5e3Sopenharmony_ci    if (builtinId != kungfu::BuiltinsStubCSigns::INVALID) {
2284514f5e3Sopenharmony_ci        bool isFast = kungfu::BuiltinsStubCSigns::IsFastBuiltin(builtinId);
2294514f5e3Sopenharmony_ci        method->SetFastBuiltinBit(isFast);
2304514f5e3Sopenharmony_ci        method->SetBuiltinId(static_cast<uint8_t>(builtinId));
2314514f5e3Sopenharmony_ci    }
2324514f5e3Sopenharmony_ci    method->SetNumArgsWithCallField(numArgs);
2334514f5e3Sopenharmony_ci    method->SetFunctionKind(kind);
2344514f5e3Sopenharmony_ci    return method;
2354514f5e3Sopenharmony_ci}
2364514f5e3Sopenharmony_ci
2374514f5e3Sopenharmony_ciJSHandle<JSFunction> ObjectFactory::NewSFunctionByHClass(const JSHandle<Method> &method,
2384514f5e3Sopenharmony_ci                                                         const JSHandle<JSHClass> &hclass)
2394514f5e3Sopenharmony_ci{
2404514f5e3Sopenharmony_ci    JSHandle<JSFunction> function(NewSharedOldSpaceJSObject(hclass));
2414514f5e3Sopenharmony_ci    hclass->SetCallable(true);
2424514f5e3Sopenharmony_ci    JSFunction::InitializeSFunction(thread_, function, method->GetFunctionKind());
2434514f5e3Sopenharmony_ci    function->SetMethod(thread_, method);
2444514f5e3Sopenharmony_ci    function->SetTaskConcurrentFuncFlag(0); // 0 : default value
2454514f5e3Sopenharmony_ci    if (method->IsAotWithCallField()) {
2464514f5e3Sopenharmony_ci        thread_->GetEcmaVM()->GetAOTFileManager()->
2474514f5e3Sopenharmony_ci            SetAOTFuncEntry(method->GetJSPandaFile(), *function, *method);
2484514f5e3Sopenharmony_ci    }
2494514f5e3Sopenharmony_ci    return function;
2504514f5e3Sopenharmony_ci}
2514514f5e3Sopenharmony_ci
2524514f5e3Sopenharmony_ci// new function with name/length accessor
2534514f5e3Sopenharmony_ciJSHandle<JSFunction> ObjectFactory::NewSFunctionWithAccessor(const void *func, const JSHandle<JSHClass> &hclass,
2544514f5e3Sopenharmony_ci    FunctionKind kind, kungfu::BuiltinsStubCSigns::ID builtinId, MemSpaceType spaceType)
2554514f5e3Sopenharmony_ci{
2564514f5e3Sopenharmony_ci    ASSERT(spaceType == SHARED_NON_MOVABLE || spaceType == SHARED_OLD_SPACE);
2574514f5e3Sopenharmony_ci    JSHandle<Method> method = NewSMethodForNativeFunction(func, kind, builtinId, spaceType);
2584514f5e3Sopenharmony_ci    return NewSFunctionByHClass(method, hclass);
2594514f5e3Sopenharmony_ci}
2604514f5e3Sopenharmony_ci
2614514f5e3Sopenharmony_ci// new function without name/length accessor
2624514f5e3Sopenharmony_ciJSHandle<JSFunction> ObjectFactory::NewSFunctionByHClass(const void *func, const JSHandle<JSHClass> &hclass,
2634514f5e3Sopenharmony_ci    FunctionKind kind, kungfu::BuiltinsStubCSigns::ID builtinId, MemSpaceType spaceType)
2644514f5e3Sopenharmony_ci{
2654514f5e3Sopenharmony_ci    ASSERT(spaceType == SHARED_NON_MOVABLE || spaceType == SHARED_OLD_SPACE);
2664514f5e3Sopenharmony_ci    JSHandle<Method> method = NewSMethodForNativeFunction(func, kind, builtinId, spaceType);
2674514f5e3Sopenharmony_ci    JSHandle<JSFunction> function(NewSharedOldSpaceJSObject(hclass));
2684514f5e3Sopenharmony_ci    hclass->SetCallable(true);
2694514f5e3Sopenharmony_ci    JSFunction::InitializeWithDefaultValue(thread_, function);
2704514f5e3Sopenharmony_ci    function->SetMethod(thread_, method);
2714514f5e3Sopenharmony_ci    return function;
2724514f5e3Sopenharmony_ci}
2734514f5e3Sopenharmony_ci
2744514f5e3Sopenharmony_ciTaggedObject *ObjectFactory::NewSharedOldSpaceObject(const JSHandle<JSHClass> &hclass)
2754514f5e3Sopenharmony_ci{
2764514f5e3Sopenharmony_ci    NewSObjectHook();
2774514f5e3Sopenharmony_ci    TaggedObject *header = sHeap_->AllocateOldOrHugeObject(thread_, *hclass);
2784514f5e3Sopenharmony_ci    if (header == nullptr) {
2794514f5e3Sopenharmony_ci        LOG_ECMA(FATAL) << "ObjectFactory::NewSharedOldSpaceObject:header is nullptr";
2804514f5e3Sopenharmony_ci    }
2814514f5e3Sopenharmony_ci    uint32_t inobjPropCount = hclass->GetInlinedProperties();
2824514f5e3Sopenharmony_ci    if (inobjPropCount > 0) {
2834514f5e3Sopenharmony_ci        InitializeExtraProperties(hclass, header, inobjPropCount);
2844514f5e3Sopenharmony_ci    }
2854514f5e3Sopenharmony_ci    return header;
2864514f5e3Sopenharmony_ci}
2874514f5e3Sopenharmony_ci
2884514f5e3Sopenharmony_ciJSHandle<JSObject> ObjectFactory::NewSharedOldSpaceJSObjectWithInit(const JSHandle<JSHClass> &jshclass)
2894514f5e3Sopenharmony_ci{
2904514f5e3Sopenharmony_ci    auto obj = NewSharedOldSpaceJSObject(jshclass);
2914514f5e3Sopenharmony_ci    InitializeJSObject(obj, jshclass);
2924514f5e3Sopenharmony_ci    return obj;
2934514f5e3Sopenharmony_ci}
2944514f5e3Sopenharmony_ci
2954514f5e3Sopenharmony_ciJSHandle<JSObject> ObjectFactory::NewSharedOldSpaceJSObject(const JSHandle<JSHClass> &jshclass)
2964514f5e3Sopenharmony_ci{
2974514f5e3Sopenharmony_ci    JSHandle<JSObject> obj(thread_, JSObject::Cast(NewSharedOldSpaceObject(jshclass)));
2984514f5e3Sopenharmony_ci    JSHandle<TaggedArray> emptyArray = SharedEmptyArray();
2994514f5e3Sopenharmony_ci    obj->InitializeHash();
3004514f5e3Sopenharmony_ci    obj->SetElements(thread_, emptyArray);
3014514f5e3Sopenharmony_ci    obj->SetProperties(thread_, emptyArray);
3024514f5e3Sopenharmony_ci    return obj;
3034514f5e3Sopenharmony_ci}
3044514f5e3Sopenharmony_ci
3054514f5e3Sopenharmony_ciJSHandle<JSTaggedValue> ObjectFactory::CreateSObjectWithProperties(std::vector<PropertyDescriptor> &descs)
3064514f5e3Sopenharmony_ci{
3074514f5e3Sopenharmony_ci    JSHandle<JSHClass> hclass = JSHClass::CreateSHClass(thread_, descs);
3084514f5e3Sopenharmony_ci    JSHandle<JSObject> object = NewSharedOldSpaceJSObject(hclass);
3094514f5e3Sopenharmony_ci    JSObject::SetSProperties(thread_, object, descs);
3104514f5e3Sopenharmony_ci    JSHandle<GlobalEnv> env = vm_->GetGlobalEnv();
3114514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> objFuncProto = env->GetSObjectFunctionPrototype();
3124514f5e3Sopenharmony_ci    hclass->SetPrototype(thread_, objFuncProto);
3134514f5e3Sopenharmony_ci    hclass->SetExtensible(false);
3144514f5e3Sopenharmony_ci    return JSHandle<JSTaggedValue>(object);
3154514f5e3Sopenharmony_ci}
3164514f5e3Sopenharmony_ci
3174514f5e3Sopenharmony_ciJSHandle<TaggedArray> ObjectFactory::SharedEmptyArray() const
3184514f5e3Sopenharmony_ci{
3194514f5e3Sopenharmony_ci    return JSHandle<TaggedArray>(thread_->GlobalConstants()->GetHandledEmptyArray());
3204514f5e3Sopenharmony_ci}
3214514f5e3Sopenharmony_ci
3224514f5e3Sopenharmony_ciJSHandle<TaggedArray> ObjectFactory::CopySArray(const JSHandle<TaggedArray> &old, uint32_t oldLength,
3234514f5e3Sopenharmony_ci                                                uint32_t newLength, JSTaggedValue initVal, ElementsKind kind)
3244514f5e3Sopenharmony_ci{
3254514f5e3Sopenharmony_ci    if (newLength == 0) {
3264514f5e3Sopenharmony_ci        return SharedEmptyArray();
3274514f5e3Sopenharmony_ci    }
3284514f5e3Sopenharmony_ci    if (newLength > oldLength) {
3294514f5e3Sopenharmony_ci        return ExtendSArray(old, newLength, initVal, kind);
3304514f5e3Sopenharmony_ci    }
3314514f5e3Sopenharmony_ci
3324514f5e3Sopenharmony_ci    NewObjectHook();
3334514f5e3Sopenharmony_ci    // Shared-array does not support Mutantarray yet.
3344514f5e3Sopenharmony_ci    ASSERT(!old->GetClass()->IsMutantTaggedArray());
3354514f5e3Sopenharmony_ci
3364514f5e3Sopenharmony_ci    size_t size = TaggedArray::ComputeSize(JSTaggedValue::TaggedTypeSize(), newLength);
3374514f5e3Sopenharmony_ci    JSHClass *arrayClass = JSHClass::Cast(thread_->GlobalConstants()->GetArrayClass().GetTaggedObject());
3384514f5e3Sopenharmony_ci    TaggedObject *header = sHeap_->AllocateOldOrHugeObject(thread_, arrayClass, size);
3394514f5e3Sopenharmony_ci    JSHandle<TaggedArray> newArray(thread_, header);
3404514f5e3Sopenharmony_ci    newArray->SetLength(newLength);
3414514f5e3Sopenharmony_ci    newArray->SetExtraLength(old->GetExtraLength());
3424514f5e3Sopenharmony_ci
3434514f5e3Sopenharmony_ci    for (uint32_t i = 0; i < newLength; i++) {
3444514f5e3Sopenharmony_ci        newArray->Set(thread_, i, old->Get(i));
3454514f5e3Sopenharmony_ci    }
3464514f5e3Sopenharmony_ci
3474514f5e3Sopenharmony_ci    return newArray;
3484514f5e3Sopenharmony_ci}
3494514f5e3Sopenharmony_ci
3504514f5e3Sopenharmony_ciJSHandle<TaggedArray> ObjectFactory::ExtendSArray(const JSHandle<TaggedArray> &old, uint32_t length,
3514514f5e3Sopenharmony_ci                                                  JSTaggedValue initVal, [[maybe_unused]] ElementsKind kind)
3524514f5e3Sopenharmony_ci{
3534514f5e3Sopenharmony_ci    ASSERT(length > old->GetLength());
3544514f5e3Sopenharmony_ci    NewObjectHook();
3554514f5e3Sopenharmony_ci    size_t size = TaggedArray::ComputeSize(JSTaggedValue::TaggedTypeSize(), length);
3564514f5e3Sopenharmony_ci    JSHClass *arrayClass = nullptr;
3574514f5e3Sopenharmony_ci    // Shared-array does not support Mutantarray yet.
3584514f5e3Sopenharmony_ci    ASSERT(!old->GetClass()->IsMutantTaggedArray());
3594514f5e3Sopenharmony_ci    arrayClass = JSHClass::Cast(thread_->GlobalConstants()->GetArrayClass().GetTaggedObject());
3604514f5e3Sopenharmony_ci
3614514f5e3Sopenharmony_ci    TaggedObject *header = sHeap_->AllocateOldOrHugeObject(thread_, arrayClass, size);
3624514f5e3Sopenharmony_ci    JSHandle<TaggedArray> newArray(thread_, header);
3634514f5e3Sopenharmony_ci    newArray->SetLength(length);
3644514f5e3Sopenharmony_ci    newArray->SetExtraLength(old->GetExtraLength());
3654514f5e3Sopenharmony_ci
3664514f5e3Sopenharmony_ci    uint32_t oldLength = old->GetLength();
3674514f5e3Sopenharmony_ci    for (uint32_t i = 0; i < oldLength; i++) {
3684514f5e3Sopenharmony_ci        newArray->Set(thread_, i, old->Get(i));
3694514f5e3Sopenharmony_ci    }
3704514f5e3Sopenharmony_ci    for (uint32_t i = oldLength; i < length; i++) {
3714514f5e3Sopenharmony_ci        newArray->Set(thread_, i, initVal);
3724514f5e3Sopenharmony_ci    }
3734514f5e3Sopenharmony_ci    return newArray;
3744514f5e3Sopenharmony_ci}
3754514f5e3Sopenharmony_ci
3764514f5e3Sopenharmony_ciJSHandle<TaggedArray> ObjectFactory::NewSTaggedArrayWithoutInit(uint32_t length, MemSpaceType spaceType)
3774514f5e3Sopenharmony_ci{
3784514f5e3Sopenharmony_ci    NewSObjectHook();
3794514f5e3Sopenharmony_ci    size_t size = TaggedArray::ComputeSize(JSTaggedValue::TaggedTypeSize(), length);
3804514f5e3Sopenharmony_ci    TaggedObject *header;
3814514f5e3Sopenharmony_ci    auto arrayClass = JSHClass::Cast(thread_->GlobalConstants()->GetArrayClass().GetTaggedObject());
3824514f5e3Sopenharmony_ci    switch (spaceType) {
3834514f5e3Sopenharmony_ci        case MemSpaceType::SHARED_OLD_SPACE:
3844514f5e3Sopenharmony_ci            header = sHeap_->AllocateOldOrHugeObject(thread_, arrayClass, size);
3854514f5e3Sopenharmony_ci            break;
3864514f5e3Sopenharmony_ci        case MemSpaceType::SHARED_READ_ONLY_SPACE:
3874514f5e3Sopenharmony_ci            header = sHeap_->AllocateReadOnlyOrHugeObject(thread_, arrayClass, size);
3884514f5e3Sopenharmony_ci            break;
3894514f5e3Sopenharmony_ci        default:
3904514f5e3Sopenharmony_ci            LOG_ECMA(FATAL) << "this branch is unreachable";
3914514f5e3Sopenharmony_ci            UNREACHABLE();
3924514f5e3Sopenharmony_ci    }
3934514f5e3Sopenharmony_ci    JSHandle<TaggedArray> array(thread_, header);
3944514f5e3Sopenharmony_ci    array->SetLength(length);
3954514f5e3Sopenharmony_ci    return array;
3964514f5e3Sopenharmony_ci}
3974514f5e3Sopenharmony_ci
3984514f5e3Sopenharmony_ciJSHandle<LayoutInfo> ObjectFactory::CreateSLayoutInfo(uint32_t properties)
3994514f5e3Sopenharmony_ci{
4004514f5e3Sopenharmony_ci    uint32_t arrayLength = LayoutInfo::ComputeArrayLength(properties);
4014514f5e3Sopenharmony_ci    JSHandle<LayoutInfo> layoutInfoHandle = JSHandle<LayoutInfo>::Cast(NewSTaggedArrayWithoutInit(arrayLength));
4024514f5e3Sopenharmony_ci    layoutInfoHandle->Initialize(thread_);
4034514f5e3Sopenharmony_ci    return layoutInfoHandle;
4044514f5e3Sopenharmony_ci}
4054514f5e3Sopenharmony_ci
4064514f5e3Sopenharmony_ciJSHandle<LayoutInfo> ObjectFactory::NewSEmptyLayoutInfo()
4074514f5e3Sopenharmony_ci{
4084514f5e3Sopenharmony_ci    JSHandle<LayoutInfo> layoutInfoHandle = JSHandle<LayoutInfo>::Cast(
4094514f5e3Sopenharmony_ci        NewSTaggedArrayWithoutInit(0, MemSpaceType::SHARED_READ_ONLY_SPACE));
4104514f5e3Sopenharmony_ci    layoutInfoHandle->Initialize(thread_);
4114514f5e3Sopenharmony_ci    return layoutInfoHandle;
4124514f5e3Sopenharmony_ci}
4134514f5e3Sopenharmony_ci
4144514f5e3Sopenharmony_ciJSHandle<JSSharedArray> ObjectFactory::NewJSSArray()
4154514f5e3Sopenharmony_ci{
4164514f5e3Sopenharmony_ci    JSHandle<GlobalEnv> env = vm_->GetGlobalEnv();
4174514f5e3Sopenharmony_ci    JSHandle<JSFunction> function(env->GetSharedArrayFunction());
4184514f5e3Sopenharmony_ci    return JSHandle<JSSharedArray>(NewJSObjectByConstructor(function));
4194514f5e3Sopenharmony_ci}
4204514f5e3Sopenharmony_ci
4214514f5e3Sopenharmony_ciJSHandle<TaggedArray> ObjectFactory::NewSJsonFixedArray(size_t start, size_t length,
4224514f5e3Sopenharmony_ci                                                        const std::vector<JSHandle<JSTaggedValue>> &vec)
4234514f5e3Sopenharmony_ci{
4244514f5e3Sopenharmony_ci    if (length == 0) {
4254514f5e3Sopenharmony_ci        return SharedEmptyArray();
4264514f5e3Sopenharmony_ci    }
4274514f5e3Sopenharmony_ci
4284514f5e3Sopenharmony_ci    JSHandle<TaggedArray> array = NewTaggedArrayWithoutInit(length, MemSpaceType::SHARED_OLD_SPACE);
4294514f5e3Sopenharmony_ci    array->SetExtraLength(0);
4304514f5e3Sopenharmony_ci    for (size_t i = 0; i < length; i++) {
4314514f5e3Sopenharmony_ci        array->Set(thread_, i, vec[start + i]);
4324514f5e3Sopenharmony_ci    }
4334514f5e3Sopenharmony_ci    return array;
4344514f5e3Sopenharmony_ci}
4354514f5e3Sopenharmony_ci
4364514f5e3Sopenharmony_ciJSHandle<LayoutInfo> ObjectFactory::CopyAndReSortSLayoutInfo(const JSHandle<LayoutInfo> &old, int end, int capacity)
4374514f5e3Sopenharmony_ci{
4384514f5e3Sopenharmony_ci    ASSERT(capacity >= end);
4394514f5e3Sopenharmony_ci    JSHandle<LayoutInfo> newArr = CreateSLayoutInfo(capacity);
4404514f5e3Sopenharmony_ci    Span<struct Properties> sp(old->GetProperties(), end);
4414514f5e3Sopenharmony_ci    for (int i = 0; i < end; i++) {
4424514f5e3Sopenharmony_ci        newArr->AddKey(thread_, i, sp[i].key_, PropertyAttributes(sp[i].attr_));
4434514f5e3Sopenharmony_ci    }
4444514f5e3Sopenharmony_ci    return newArr;
4454514f5e3Sopenharmony_ci}
4464514f5e3Sopenharmony_ci
4474514f5e3Sopenharmony_ciJSHandle<TaggedArray> ObjectFactory::NewSDictionaryArray(uint32_t length)
4484514f5e3Sopenharmony_ci{
4494514f5e3Sopenharmony_ci    NewSObjectHook();
4504514f5e3Sopenharmony_ci    ASSERT(length > 0);
4514514f5e3Sopenharmony_ci    size_t size = TaggedArray::ComputeSize(JSTaggedValue::TaggedTypeSize(), length);
4524514f5e3Sopenharmony_ci    auto header = sHeap_->AllocateOldOrHugeObject(
4534514f5e3Sopenharmony_ci        thread_, JSHClass::Cast(thread_->GlobalConstants()->GetDictionaryClass().GetTaggedObject()), size);
4544514f5e3Sopenharmony_ci    JSHandle<TaggedArray> array(thread_, header);
4554514f5e3Sopenharmony_ci    array->InitializeWithSpecialValue(JSTaggedValue::Undefined(), length);
4564514f5e3Sopenharmony_ci    return array;
4574514f5e3Sopenharmony_ci}
4584514f5e3Sopenharmony_ci
4594514f5e3Sopenharmony_ciJSHandle<ProfileTypeInfoCell> ObjectFactory::NewSEmptyProfileTypeInfoCell()
4604514f5e3Sopenharmony_ci{
4614514f5e3Sopenharmony_ci    NewSObjectHook();
4624514f5e3Sopenharmony_ci    auto header = sHeap_->AllocateReadOnlyOrHugeObject(thread_,
4634514f5e3Sopenharmony_ci        JSHClass::Cast(thread_->GlobalConstants()->GetProfileTypeInfoCell0Class().GetTaggedObject()));
4644514f5e3Sopenharmony_ci    JSHandle<ProfileTypeInfoCell> profileTypeInfoCell(thread_, header);
4654514f5e3Sopenharmony_ci    profileTypeInfoCell->SetValue(thread_, JSTaggedValue::Undefined());
4664514f5e3Sopenharmony_ci    profileTypeInfoCell->SetMachineCode(thread_, JSTaggedValue::Hole());
4674514f5e3Sopenharmony_ci    profileTypeInfoCell->SetHandle(thread_, JSTaggedValue::Undefined());
4684514f5e3Sopenharmony_ci    return profileTypeInfoCell;
4694514f5e3Sopenharmony_ci}
4704514f5e3Sopenharmony_ci
4714514f5e3Sopenharmony_ciJSHandle<FunctionTemplate> ObjectFactory::NewSFunctionTemplate(
4724514f5e3Sopenharmony_ci    const JSHandle<Method> &method, const JSHandle<JSTaggedValue> &module, int32_t length)
4734514f5e3Sopenharmony_ci{
4744514f5e3Sopenharmony_ci    NewSObjectHook();
4754514f5e3Sopenharmony_ci    auto globalConstants = thread_->GlobalConstants();
4764514f5e3Sopenharmony_ci    auto header = sHeap_->AllocateOldOrHugeObject(thread_,
4774514f5e3Sopenharmony_ci        JSHClass::Cast(globalConstants->GetFunctionTemplateClass().GetTaggedObject()));
4784514f5e3Sopenharmony_ci    JSHandle<FunctionTemplate> functionTemplate(thread_, header);
4794514f5e3Sopenharmony_ci    functionTemplate->SetMethod(thread_, method);
4804514f5e3Sopenharmony_ci    functionTemplate->SetModule(thread_, module);
4814514f5e3Sopenharmony_ci    functionTemplate->SetRawProfileTypeInfo(thread_, globalConstants->GetEmptyProfileTypeInfoCell(), SKIP_BARRIER);
4824514f5e3Sopenharmony_ci    functionTemplate->SetLength(length);
4834514f5e3Sopenharmony_ci    return functionTemplate;
4844514f5e3Sopenharmony_ci}
4854514f5e3Sopenharmony_ci
4864514f5e3Sopenharmony_ciJSHandle<TaggedArray> ObjectFactory::NewSEmptyArray()
4874514f5e3Sopenharmony_ci{
4884514f5e3Sopenharmony_ci    NewSObjectHook();
4894514f5e3Sopenharmony_ci    auto header = sHeap_->AllocateReadOnlyOrHugeObject(thread_,
4904514f5e3Sopenharmony_ci        JSHClass::Cast(thread_->GlobalConstants()->GetArrayClass().GetTaggedObject()), TaggedArray::SIZE);
4914514f5e3Sopenharmony_ci    JSHandle<TaggedArray> array(thread_, header);
4924514f5e3Sopenharmony_ci    array->SetLength(0);
4934514f5e3Sopenharmony_ci    array->SetExtraLength(0);
4944514f5e3Sopenharmony_ci    return array;
4954514f5e3Sopenharmony_ci}
4964514f5e3Sopenharmony_ci
4974514f5e3Sopenharmony_ciJSHandle<MutantTaggedArray> ObjectFactory::NewSEmptyMutantArray()
4984514f5e3Sopenharmony_ci{
4994514f5e3Sopenharmony_ci    NewSObjectHook();
5004514f5e3Sopenharmony_ci    auto header = sHeap_->AllocateReadOnlyOrHugeObject(thread_,
5014514f5e3Sopenharmony_ci        JSHClass::Cast(thread_->GlobalConstants()->GetMutantTaggedArrayClass().GetTaggedObject()), TaggedArray::SIZE);
5024514f5e3Sopenharmony_ci    JSHandle<MutantTaggedArray> array(thread_, header);
5034514f5e3Sopenharmony_ci    array->SetLength(0);
5044514f5e3Sopenharmony_ci    array->SetExtraLength(0);
5054514f5e3Sopenharmony_ci    return array;
5064514f5e3Sopenharmony_ci}
5074514f5e3Sopenharmony_ci
5084514f5e3Sopenharmony_ciJSHandle<JSNativePointer> ObjectFactory::NewSJSNativePointer(void *externalPointer,
5094514f5e3Sopenharmony_ci                                                             const NativePointerCallback &callBack,
5104514f5e3Sopenharmony_ci                                                             void *data,
5114514f5e3Sopenharmony_ci                                                             bool nonMovable,
5124514f5e3Sopenharmony_ci                                                             size_t nativeBindingsize,
5134514f5e3Sopenharmony_ci                                                             NativeFlag flag)
5144514f5e3Sopenharmony_ci{
5154514f5e3Sopenharmony_ci    NewSObjectHook();
5164514f5e3Sopenharmony_ci    TaggedObject *header;
5174514f5e3Sopenharmony_ci    auto jsNativePointerClass =
5184514f5e3Sopenharmony_ci        JSHClass::Cast(thread_->GlobalConstants()->GetSJSNativePointerClass().GetTaggedObject());
5194514f5e3Sopenharmony_ci    jsNativePointerClass->SetIsJSShared(true);
5204514f5e3Sopenharmony_ci    if (nonMovable) {
5214514f5e3Sopenharmony_ci        header = sHeap_->AllocateNonMovableOrHugeObject(thread_, jsNativePointerClass);
5224514f5e3Sopenharmony_ci    } else {
5234514f5e3Sopenharmony_ci        header = sHeap_->AllocateOldOrHugeObject(thread_, jsNativePointerClass);
5244514f5e3Sopenharmony_ci    }
5254514f5e3Sopenharmony_ci    JSHandle<JSNativePointer> obj(thread_, header);
5264514f5e3Sopenharmony_ci    obj->SetExternalPointer(externalPointer);
5274514f5e3Sopenharmony_ci    obj->SetDeleter(callBack);
5284514f5e3Sopenharmony_ci    obj->SetData(data);
5294514f5e3Sopenharmony_ci    uint32_t fixedNativeBindingsize = nativeBindingsize < UINT32_MAX ? nativeBindingsize
5304514f5e3Sopenharmony_ci                                                                     : UINT32_MAX;
5314514f5e3Sopenharmony_ci    obj->SetBindingSize(fixedNativeBindingsize);
5324514f5e3Sopenharmony_ci    obj->SetNativeFlag(flag);
5334514f5e3Sopenharmony_ci
5344514f5e3Sopenharmony_ci    if (callBack != nullptr) {
5354514f5e3Sopenharmony_ci        sHeap_->IncNativeSizeAfterLastGC(fixedNativeBindingsize);
5364514f5e3Sopenharmony_ci        Runtime::GetInstance()->PushToSharedNativePointerList(static_cast<JSNativePointer *>(header));
5374514f5e3Sopenharmony_ci        // In some cases, the size of JS/TS object is too small and the native binding size is too large.
5384514f5e3Sopenharmony_ci        // Check and try trigger concurrent mark here.
5394514f5e3Sopenharmony_ci        size_t nativeSizeAfterLastGC = sHeap_->GetNativeSizeAfterLastGC();
5404514f5e3Sopenharmony_ci        if (nativeSizeAfterLastGC > sHeap_->GetNativeSizeTriggerSharedGC()) {
5414514f5e3Sopenharmony_ci            sHeap_->CollectGarbage<TriggerGCType::SHARED_GC, GCReason::ALLOCATION_FAILED>(thread_);
5424514f5e3Sopenharmony_ci        } else if (sHeap_->CheckCanTriggerConcurrentMarking(thread_) &&
5434514f5e3Sopenharmony_ci            nativeSizeAfterLastGC > sHeap_->GetNativeSizeTriggerSharedCM()) {
5444514f5e3Sopenharmony_ci            sHeap_->TriggerConcurrentMarking<TriggerGCType::SHARED_GC, GCReason::ALLOCATION_LIMIT>(thread_);
5454514f5e3Sopenharmony_ci        }
5464514f5e3Sopenharmony_ci    }
5474514f5e3Sopenharmony_ci    return obj;
5484514f5e3Sopenharmony_ci}
5494514f5e3Sopenharmony_ci
5504514f5e3Sopenharmony_ciJSHandle<JSNativePointer> ObjectFactory::NewSReadOnlyJSNativePointer(void* externalPointer)
5514514f5e3Sopenharmony_ci{
5524514f5e3Sopenharmony_ci    NewSObjectHook();
5534514f5e3Sopenharmony_ci    auto jsNativePointerClass =
5544514f5e3Sopenharmony_ci        JSHClass::Cast(thread_->GlobalConstants()->GetSJSNativePointerClass().GetTaggedObject());
5554514f5e3Sopenharmony_ci    jsNativePointerClass->SetIsJSShared(true);
5564514f5e3Sopenharmony_ci    TaggedObject* header = sHeap_->AllocateReadOnlyOrHugeObject(thread_, jsNativePointerClass);
5574514f5e3Sopenharmony_ci    JSHandle<JSNativePointer> obj(thread_, header);
5584514f5e3Sopenharmony_ci    obj->SetExternalPointer(externalPointer);
5594514f5e3Sopenharmony_ci    obj->SetDeleter(nullptr);
5604514f5e3Sopenharmony_ci    obj->SetData(nullptr);
5614514f5e3Sopenharmony_ci    obj->SetBindingSize(0U);
5624514f5e3Sopenharmony_ci    obj->SetNativeFlag(NativeFlag::NO_DIV);
5634514f5e3Sopenharmony_ci    return obj;
5644514f5e3Sopenharmony_ci}
5654514f5e3Sopenharmony_ci
5664514f5e3Sopenharmony_ciJSHandle<AccessorData> ObjectFactory::NewSInternalAccessor(void *setter, void *getter)
5674514f5e3Sopenharmony_ci{
5684514f5e3Sopenharmony_ci    NewSObjectHook();
5694514f5e3Sopenharmony_ci    TaggedObject *header = sHeap_->AllocateReadOnlyOrHugeObject(thread_,
5704514f5e3Sopenharmony_ci        JSHClass::Cast(thread_->GlobalConstants()->GetInternalAccessorClass().GetTaggedObject()));
5714514f5e3Sopenharmony_ci    JSHandle<InternalAccessor> obj(thread_, InternalAccessor::Cast(header));
5724514f5e3Sopenharmony_ci
5734514f5e3Sopenharmony_ci    obj->SetSetter(reinterpret_cast<InternalAccessor::InternalSetFunc>(setter));
5744514f5e3Sopenharmony_ci    obj->SetGetter(reinterpret_cast<InternalAccessor::InternalGetFunc>(getter));
5754514f5e3Sopenharmony_ci    return JSHandle<AccessorData>::Cast(obj);
5764514f5e3Sopenharmony_ci}
5774514f5e3Sopenharmony_ci
5784514f5e3Sopenharmony_ciJSHandle<ConstantPool> ObjectFactory::NewSConstantPool(uint32_t capacity)
5794514f5e3Sopenharmony_ci{
5804514f5e3Sopenharmony_ci    NewSObjectHook();
5814514f5e3Sopenharmony_ci    size_t size = ConstantPool::ComputeSize(capacity);
5824514f5e3Sopenharmony_ci    auto header = sHeap_->AllocateOldOrHugeObject(
5834514f5e3Sopenharmony_ci        thread_, JSHClass::Cast(sHeap_->GetGlobalConst()->GetConstantPoolClass().GetTaggedObject()), size);
5844514f5e3Sopenharmony_ci    JSHandle<ConstantPool> array(thread_, header);
5854514f5e3Sopenharmony_ci    array->InitializeWithSpecialValue(thread_, JSTaggedValue::Hole(), capacity);
5864514f5e3Sopenharmony_ci    return array;
5874514f5e3Sopenharmony_ci}
5884514f5e3Sopenharmony_ci
5894514f5e3Sopenharmony_ciJSHandle<COWTaggedArray> ObjectFactory::NewSCOWTaggedArray(uint32_t length, JSTaggedValue initVal)
5904514f5e3Sopenharmony_ci{
5914514f5e3Sopenharmony_ci    NewSObjectHook();
5924514f5e3Sopenharmony_ci    ASSERT(length > 0);
5934514f5e3Sopenharmony_ci
5944514f5e3Sopenharmony_ci    size_t size = TaggedArray::ComputeSize(JSTaggedValue::TaggedTypeSize(), length);
5954514f5e3Sopenharmony_ci    auto header = sHeap_->AllocateNonMovableOrHugeObject(
5964514f5e3Sopenharmony_ci        thread_, JSHClass::Cast(sHeap_->GetGlobalConst()->GetCOWArrayClass().GetTaggedObject()), size);
5974514f5e3Sopenharmony_ci    JSHandle<COWTaggedArray> cowArray(thread_, header);
5984514f5e3Sopenharmony_ci    cowArray->InitializeWithSpecialValue(initVal, length);
5994514f5e3Sopenharmony_ci    return cowArray;
6004514f5e3Sopenharmony_ci}
6014514f5e3Sopenharmony_ci
6024514f5e3Sopenharmony_ciJSHandle<ClassLiteral> ObjectFactory::NewSClassLiteral()
6034514f5e3Sopenharmony_ci{
6044514f5e3Sopenharmony_ci    NewSObjectHook();
6054514f5e3Sopenharmony_ci    TaggedObject *header = sHeap_->AllocateOldOrHugeObject(
6064514f5e3Sopenharmony_ci        thread_, JSHClass::Cast(sHeap_->GetGlobalConst()->GetClassLiteralClass().GetTaggedObject()));
6074514f5e3Sopenharmony_ci    JSHandle<TaggedArray> emptyArray = EmptyArray();
6084514f5e3Sopenharmony_ci
6094514f5e3Sopenharmony_ci    JSHandle<ClassLiteral> classLiteral(thread_, header);
6104514f5e3Sopenharmony_ci    classLiteral->SetArray(thread_, emptyArray);
6114514f5e3Sopenharmony_ci    classLiteral->SetIsAOTUsed(false);
6124514f5e3Sopenharmony_ci
6134514f5e3Sopenharmony_ci    return classLiteral;
6144514f5e3Sopenharmony_ci}
6154514f5e3Sopenharmony_ci
6164514f5e3Sopenharmony_ciJSHandle<ClassInfoExtractor> ObjectFactory::NewSClassInfoExtractor(
6174514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> method)
6184514f5e3Sopenharmony_ci{
6194514f5e3Sopenharmony_ci    NewSObjectHook();
6204514f5e3Sopenharmony_ci    TaggedObject *header = sHeap_->AllocateOldOrHugeObject(
6214514f5e3Sopenharmony_ci        thread_, JSHClass::Cast(sHeap_->GetGlobalConst()->GetClassInfoExtractorHClass().GetTaggedObject()));
6224514f5e3Sopenharmony_ci    JSHandle<ClassInfoExtractor> obj(thread_, header);
6234514f5e3Sopenharmony_ci    obj->ClearBitField();
6244514f5e3Sopenharmony_ci    obj->SetConstructorMethod(thread_, method.GetTaggedValue());
6254514f5e3Sopenharmony_ci    JSHandle<TaggedArray> emptyArray = EmptyArray();
6264514f5e3Sopenharmony_ci    obj->SetNonStaticKeys(thread_, emptyArray, SKIP_BARRIER);
6274514f5e3Sopenharmony_ci    obj->SetNonStaticProperties(thread_, emptyArray, SKIP_BARRIER);
6284514f5e3Sopenharmony_ci    obj->SetNonStaticElements(thread_, emptyArray, SKIP_BARRIER);
6294514f5e3Sopenharmony_ci    obj->SetStaticKeys(thread_, emptyArray, SKIP_BARRIER);
6304514f5e3Sopenharmony_ci    obj->SetStaticProperties(thread_, emptyArray, SKIP_BARRIER);
6314514f5e3Sopenharmony_ci    obj->SetStaticElements(thread_, emptyArray, SKIP_BARRIER);
6324514f5e3Sopenharmony_ci    return obj;
6334514f5e3Sopenharmony_ci}
6344514f5e3Sopenharmony_ci
6354514f5e3Sopenharmony_ciJSHandle<TaggedArray> ObjectFactory::NewSOldSpaceTaggedArray(uint32_t length, JSTaggedValue initVal)
6364514f5e3Sopenharmony_ci{
6374514f5e3Sopenharmony_ci    return NewSTaggedArray(length, initVal, MemSpaceType::SHARED_OLD_SPACE);
6384514f5e3Sopenharmony_ci}
6394514f5e3Sopenharmony_ci
6404514f5e3Sopenharmony_ciJSHandle<TaggedArray> ObjectFactory::NewSTaggedArray(uint32_t length, JSTaggedValue initVal, MemSpaceType spaceType)
6414514f5e3Sopenharmony_ci{
6424514f5e3Sopenharmony_ci    NewSObjectHook();
6434514f5e3Sopenharmony_ci    if (length == 0) {
6444514f5e3Sopenharmony_ci        return EmptyArray();
6454514f5e3Sopenharmony_ci    }
6464514f5e3Sopenharmony_ci
6474514f5e3Sopenharmony_ci    size_t size = TaggedArray::ComputeSize(JSTaggedValue::TaggedTypeSize(), length);
6484514f5e3Sopenharmony_ci    TaggedObject *header = nullptr;
6494514f5e3Sopenharmony_ci    JSHClass *arrayClass = JSHClass::Cast(thread_->GlobalConstants()->GetArrayClass().GetTaggedObject());
6504514f5e3Sopenharmony_ci    switch (spaceType) {
6514514f5e3Sopenharmony_ci        case MemSpaceType::SHARED_OLD_SPACE:
6524514f5e3Sopenharmony_ci            header = sHeap_->AllocateOldOrHugeObject(thread_, arrayClass, size);
6534514f5e3Sopenharmony_ci            break;
6544514f5e3Sopenharmony_ci        case MemSpaceType::SHARED_NON_MOVABLE:
6554514f5e3Sopenharmony_ci            header = sHeap_->AllocateNonMovableOrHugeObject(thread_, arrayClass, size);
6564514f5e3Sopenharmony_ci            break;
6574514f5e3Sopenharmony_ci        default:
6584514f5e3Sopenharmony_ci            LOG_ECMA(FATAL) << "this branch is unreachable";
6594514f5e3Sopenharmony_ci            UNREACHABLE();
6604514f5e3Sopenharmony_ci    }
6614514f5e3Sopenharmony_ci
6624514f5e3Sopenharmony_ci    JSHandle<TaggedArray> array(thread_, header);
6634514f5e3Sopenharmony_ci    array->InitializeWithSpecialValue(initVal, length);
6644514f5e3Sopenharmony_ci    return array;
6654514f5e3Sopenharmony_ci}
6664514f5e3Sopenharmony_ci
6674514f5e3Sopenharmony_ciJSHandle<JSSymbol> ObjectFactory::NewSWellKnownSymbol(const JSHandle<JSTaggedValue> &name)
6684514f5e3Sopenharmony_ci{
6694514f5e3Sopenharmony_ci    NewSObjectHook();
6704514f5e3Sopenharmony_ci    TaggedObject *header = sHeap_->AllocateNonMovableOrHugeObject(
6714514f5e3Sopenharmony_ci        thread_, JSHClass::Cast(thread_->GlobalConstants()->GetSymbolClass().GetTaggedObject()));
6724514f5e3Sopenharmony_ci    JSHandle<JSSymbol> obj(thread_, JSSymbol::Cast(header));
6734514f5e3Sopenharmony_ci    obj->SetFlags(0);
6744514f5e3Sopenharmony_ci    obj->SetWellKnownSymbol();
6754514f5e3Sopenharmony_ci    obj->SetDescription(thread_, name);
6764514f5e3Sopenharmony_ci    obj->SetHashField(SymbolTable::Hash(name.GetTaggedValue()));
6774514f5e3Sopenharmony_ci    return obj;
6784514f5e3Sopenharmony_ci}
6794514f5e3Sopenharmony_ci
6804514f5e3Sopenharmony_ciJSHandle<JSSymbol> ObjectFactory::NewSPublicSymbol(const JSHandle<JSTaggedValue> &name)
6814514f5e3Sopenharmony_ci{
6824514f5e3Sopenharmony_ci    NewObjectHook();
6834514f5e3Sopenharmony_ci    TaggedObject *header = sHeap_->AllocateNonMovableOrHugeObject(
6844514f5e3Sopenharmony_ci        thread_, JSHClass::Cast(thread_->GlobalConstants()->GetSymbolClass().GetTaggedObject()));
6854514f5e3Sopenharmony_ci    JSHandle<JSSymbol> obj(thread_, JSSymbol::Cast(header));
6864514f5e3Sopenharmony_ci    obj->SetFlags(0);
6874514f5e3Sopenharmony_ci    obj->SetDescription(thread_, name);
6884514f5e3Sopenharmony_ci    obj->SetHashField(SymbolTable::Hash(name.GetTaggedValue()));
6894514f5e3Sopenharmony_ci    return obj;
6904514f5e3Sopenharmony_ci}
6914514f5e3Sopenharmony_ci
6924514f5e3Sopenharmony_ciJSHandle<JSSymbol> ObjectFactory::NewSEmptySymbol()
6934514f5e3Sopenharmony_ci{
6944514f5e3Sopenharmony_ci    NewObjectHook();
6954514f5e3Sopenharmony_ci    TaggedObject *header = sHeap_->AllocateNonMovableOrHugeObject(
6964514f5e3Sopenharmony_ci        thread_, JSHClass::Cast(thread_->GlobalConstants()->GetSymbolClass().GetTaggedObject()));
6974514f5e3Sopenharmony_ci    JSHandle<JSSymbol> obj(thread_, JSSymbol::Cast(header));
6984514f5e3Sopenharmony_ci    obj->SetDescription(thread_, JSTaggedValue::Undefined());
6994514f5e3Sopenharmony_ci    obj->SetFlags(0);
7004514f5e3Sopenharmony_ci    obj->SetHashField(0);
7014514f5e3Sopenharmony_ci    return obj;
7024514f5e3Sopenharmony_ci}
7034514f5e3Sopenharmony_ci
7044514f5e3Sopenharmony_ciJSHandle<JSSymbol> ObjectFactory::NewSWellKnownSymbolWithChar(std::string_view description)
7054514f5e3Sopenharmony_ci{
7064514f5e3Sopenharmony_ci    JSHandle<EcmaString> string = NewFromUtf8(description);
7074514f5e3Sopenharmony_ci    return NewSWellKnownSymbol(JSHandle<JSTaggedValue>(string));
7084514f5e3Sopenharmony_ci}
7094514f5e3Sopenharmony_ci
7104514f5e3Sopenharmony_ciJSHandle<JSSymbol> ObjectFactory::NewSPublicSymbolWithChar(std::string_view description)
7114514f5e3Sopenharmony_ci{
7124514f5e3Sopenharmony_ci    JSHandle<EcmaString> string = NewFromUtf8(description);
7134514f5e3Sopenharmony_ci    return NewSPublicSymbol(JSHandle<JSTaggedValue>(string));
7144514f5e3Sopenharmony_ci}
7154514f5e3Sopenharmony_ci
7164514f5e3Sopenharmony_ciJSHandle<SourceTextModule> ObjectFactory::NewSSourceTextModule()
7174514f5e3Sopenharmony_ci{
7184514f5e3Sopenharmony_ci    NewObjectHook();
7194514f5e3Sopenharmony_ci    TaggedObject *header = sHeap_->AllocateOldOrHugeObject(thread_,
7204514f5e3Sopenharmony_ci        JSHClass::Cast(thread_->GlobalConstants()->GetSourceTextModuleClass().GetTaggedObject()));
7214514f5e3Sopenharmony_ci    JSHandle<SourceTextModule> obj(thread_, header);
7224514f5e3Sopenharmony_ci    JSTaggedValue undefinedValue = thread_->GlobalConstants()->GetUndefined();
7234514f5e3Sopenharmony_ci    obj->SetEnvironment(thread_, undefinedValue);
7244514f5e3Sopenharmony_ci    obj->SetNamespace(thread_, undefinedValue);
7254514f5e3Sopenharmony_ci    obj->SetRequestedModules(thread_, undefinedValue);
7264514f5e3Sopenharmony_ci    obj->SetImportEntries(thread_, undefinedValue);
7274514f5e3Sopenharmony_ci    obj->SetLocalExportEntries(thread_, undefinedValue);
7284514f5e3Sopenharmony_ci    obj->SetIndirectExportEntries(thread_, undefinedValue);
7294514f5e3Sopenharmony_ci    obj->SetStarExportEntries(thread_, undefinedValue);
7304514f5e3Sopenharmony_ci    obj->SetNameDictionary(thread_, undefinedValue);
7314514f5e3Sopenharmony_ci    // [[CycleRoot]]: For a module not in a cycle, this would be the module itself.
7324514f5e3Sopenharmony_ci    obj->SetCycleRoot(thread_, obj);
7334514f5e3Sopenharmony_ci    obj->SetTopLevelCapability(thread_, undefinedValue);
7344514f5e3Sopenharmony_ci    obj->SetAsyncParentModules(thread_, undefinedValue);
7354514f5e3Sopenharmony_ci    obj->SetHasTLA(false);
7364514f5e3Sopenharmony_ci    obj->SetAsyncEvaluatingOrdinal(SourceTextModule::NOT_ASYNC_EVALUATED);
7374514f5e3Sopenharmony_ci    obj->SetPendingAsyncDependencies(SourceTextModule::UNDEFINED_INDEX);
7384514f5e3Sopenharmony_ci    obj->SetDFSIndex(SourceTextModule::UNDEFINED_INDEX);
7394514f5e3Sopenharmony_ci    obj->SetDFSAncestorIndex(SourceTextModule::UNDEFINED_INDEX);
7404514f5e3Sopenharmony_ci    obj->SetEvaluationError(SourceTextModule::UNDEFINED_INDEX);
7414514f5e3Sopenharmony_ci    obj->SetStatus(ModuleStatus::UNINSTANTIATED);
7424514f5e3Sopenharmony_ci    obj->SetTypes(ModuleTypes::UNKNOWN);
7434514f5e3Sopenharmony_ci    obj->SetIsNewBcVersion(false);
7444514f5e3Sopenharmony_ci    obj->SetRegisterCounts(UINT16_MAX);
7454514f5e3Sopenharmony_ci    obj->SetLazyImportStatus(ToUintPtr(nullptr));
7464514f5e3Sopenharmony_ci    obj->SetEcmaModuleFilename(ToUintPtr(nullptr));
7474514f5e3Sopenharmony_ci    obj->SetEcmaModuleRecordName(ToUintPtr(nullptr));
7484514f5e3Sopenharmony_ci    obj->SetSharedType(SharedTypes::UNSENDABLE_MODULE);
7494514f5e3Sopenharmony_ci    obj->SetSendableEnv(thread_, undefinedValue);
7504514f5e3Sopenharmony_ci    return obj;
7514514f5e3Sopenharmony_ci}
7524514f5e3Sopenharmony_ci
7534514f5e3Sopenharmony_ciJSHandle<ModuleNamespace> ObjectFactory::NewSModuleNamespace()
7544514f5e3Sopenharmony_ci{
7554514f5e3Sopenharmony_ci    NewObjectHook();
7564514f5e3Sopenharmony_ci    JSHandle<GlobalEnv> env = vm_->GetGlobalEnv();
7574514f5e3Sopenharmony_ci    JSHandle<JSHClass> hclass = JSHandle<JSHClass>::Cast(env->GetSharedModuleNamespaceClass());
7584514f5e3Sopenharmony_ci    JSHandle<JSObject> obj = NewSharedOldSpaceJSObject(hclass);
7594514f5e3Sopenharmony_ci
7604514f5e3Sopenharmony_ci    JSHandle<ModuleNamespace> moduleNamespace = JSHandle<ModuleNamespace>::Cast(obj);
7614514f5e3Sopenharmony_ci    moduleNamespace->SetModule(thread_, JSTaggedValue::Undefined());
7624514f5e3Sopenharmony_ci    moduleNamespace->SetExports(thread_, JSTaggedValue::Undefined());
7634514f5e3Sopenharmony_ci    moduleNamespace->SetDeregisterProcession(thread_, JSTaggedValue::Undefined());
7644514f5e3Sopenharmony_ci    return moduleNamespace;
7654514f5e3Sopenharmony_ci}
7664514f5e3Sopenharmony_ci
7674514f5e3Sopenharmony_ciJSHandle<ImportEntry> ObjectFactory::NewSImportEntry(const JSHandle<JSTaggedValue> &moduleRequest,
7684514f5e3Sopenharmony_ci                                                     const JSHandle<JSTaggedValue> &importName,
7694514f5e3Sopenharmony_ci                                                     const JSHandle<JSTaggedValue> &localName)
7704514f5e3Sopenharmony_ci{
7714514f5e3Sopenharmony_ci    NewObjectHook();
7724514f5e3Sopenharmony_ci    TaggedObject *header = sHeap_->AllocateOldOrHugeObject(thread_,
7734514f5e3Sopenharmony_ci        JSHClass::Cast(thread_->GlobalConstants()->GetImportEntryClass().GetTaggedObject()));
7744514f5e3Sopenharmony_ci    JSHandle<ImportEntry> obj(thread_, header);
7754514f5e3Sopenharmony_ci    obj->SetModuleRequest(thread_, moduleRequest);
7764514f5e3Sopenharmony_ci    obj->SetImportName(thread_, importName);
7774514f5e3Sopenharmony_ci    obj->SetLocalName(thread_, localName);
7784514f5e3Sopenharmony_ci    return obj;
7794514f5e3Sopenharmony_ci}
7804514f5e3Sopenharmony_ci
7814514f5e3Sopenharmony_ciJSHandle<LocalExportEntry> ObjectFactory::NewSLocalExportEntry(const JSHandle<JSTaggedValue> &exportName,
7824514f5e3Sopenharmony_ci    const JSHandle<JSTaggedValue> &localName, const uint32_t index)
7834514f5e3Sopenharmony_ci{
7844514f5e3Sopenharmony_ci    NewObjectHook();
7854514f5e3Sopenharmony_ci    TaggedObject *header = sHeap_->AllocateOldOrHugeObject(thread_,
7864514f5e3Sopenharmony_ci        JSHClass::Cast(thread_->GlobalConstants()->GetLocalExportEntryClass().GetTaggedObject()));
7874514f5e3Sopenharmony_ci    JSHandle<LocalExportEntry> obj(thread_, header);
7884514f5e3Sopenharmony_ci    obj->SetExportName(thread_, exportName);
7894514f5e3Sopenharmony_ci    obj->SetLocalName(thread_, localName);
7904514f5e3Sopenharmony_ci    obj->SetLocalIndex(index);
7914514f5e3Sopenharmony_ci    return obj;
7924514f5e3Sopenharmony_ci}
7934514f5e3Sopenharmony_ci
7944514f5e3Sopenharmony_ciJSHandle<IndirectExportEntry> ObjectFactory::NewSIndirectExportEntry(const JSHandle<JSTaggedValue> &exportName,
7954514f5e3Sopenharmony_ci                                                                     const JSHandle<JSTaggedValue> &moduleRequest,
7964514f5e3Sopenharmony_ci                                                                     const JSHandle<JSTaggedValue> &importName)
7974514f5e3Sopenharmony_ci{
7984514f5e3Sopenharmony_ci    NewObjectHook();
7994514f5e3Sopenharmony_ci    TaggedObject *header = sHeap_->AllocateOldOrHugeObject(thread_,
8004514f5e3Sopenharmony_ci        JSHClass::Cast(thread_->GlobalConstants()->GetIndirectExportEntryClass().GetTaggedObject()));
8014514f5e3Sopenharmony_ci    JSHandle<IndirectExportEntry> obj(thread_, header);
8024514f5e3Sopenharmony_ci    obj->SetExportName(thread_, exportName);
8034514f5e3Sopenharmony_ci    obj->SetModuleRequest(thread_, moduleRequest);
8044514f5e3Sopenharmony_ci    obj->SetImportName(thread_, importName);
8054514f5e3Sopenharmony_ci    return obj;
8064514f5e3Sopenharmony_ci}
8074514f5e3Sopenharmony_ci
8084514f5e3Sopenharmony_ciJSHandle<StarExportEntry> ObjectFactory::NewSStarExportEntry(const JSHandle<JSTaggedValue> &moduleRequest)
8094514f5e3Sopenharmony_ci{
8104514f5e3Sopenharmony_ci    NewObjectHook();
8114514f5e3Sopenharmony_ci    TaggedObject *header = sHeap_->AllocateOldOrHugeObject(thread_,
8124514f5e3Sopenharmony_ci        JSHClass::Cast(thread_->GlobalConstants()->GetStarExportEntryClass().GetTaggedObject()));
8134514f5e3Sopenharmony_ci    JSHandle<StarExportEntry> obj(thread_, header);
8144514f5e3Sopenharmony_ci    obj->SetModuleRequest(thread_, moduleRequest);
8154514f5e3Sopenharmony_ci    return obj;
8164514f5e3Sopenharmony_ci}
8174514f5e3Sopenharmony_ci
8184514f5e3Sopenharmony_ciJSHandle<ResolvedIndexBinding> ObjectFactory::NewSResolvedIndexBindingRecord()
8194514f5e3Sopenharmony_ci{
8204514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> undefinedValue = thread_->GlobalConstants()->GetHandledUndefined();
8214514f5e3Sopenharmony_ci    JSHandle<SourceTextModule> ecmaModule(undefinedValue);
8224514f5e3Sopenharmony_ci    int32_t index = 0;
8234514f5e3Sopenharmony_ci    return NewSResolvedIndexBindingRecord(ecmaModule, index);
8244514f5e3Sopenharmony_ci}
8254514f5e3Sopenharmony_ci
8264514f5e3Sopenharmony_ciJSHandle<ResolvedIndexBinding> ObjectFactory::NewSResolvedIndexBindingRecord(const JSHandle<SourceTextModule> &module,
8274514f5e3Sopenharmony_ci                                                                             int32_t index)
8284514f5e3Sopenharmony_ci{
8294514f5e3Sopenharmony_ci    NewObjectHook();
8304514f5e3Sopenharmony_ci    TaggedObject *header = sHeap_->AllocateOldOrHugeObject(thread_,
8314514f5e3Sopenharmony_ci        JSHClass::Cast(thread_->GlobalConstants()->GetResolvedIndexBindingClass().GetTaggedObject()));
8324514f5e3Sopenharmony_ci    JSHandle<ResolvedIndexBinding> obj(thread_, header);
8334514f5e3Sopenharmony_ci    obj->SetModule(thread_, module);
8344514f5e3Sopenharmony_ci    obj->SetIndex(index);
8354514f5e3Sopenharmony_ci    return obj;
8364514f5e3Sopenharmony_ci}
8374514f5e3Sopenharmony_ci
8384514f5e3Sopenharmony_ciJSHandle<ResolvedBinding> ObjectFactory::NewSResolvedBindingRecord()
8394514f5e3Sopenharmony_ci{
8404514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> undefinedValue = thread_->GlobalConstants()->GetHandledUndefined();
8414514f5e3Sopenharmony_ci    JSHandle<SourceTextModule> ecmaModule(undefinedValue);
8424514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> bindingName(undefinedValue);
8434514f5e3Sopenharmony_ci    return NewSResolvedBindingRecord(ecmaModule, bindingName);
8444514f5e3Sopenharmony_ci}
8454514f5e3Sopenharmony_ci
8464514f5e3Sopenharmony_ciJSHandle<ResolvedBinding> ObjectFactory::NewSResolvedBindingRecord(const JSHandle<SourceTextModule> &module,
8474514f5e3Sopenharmony_ci                                                                   const JSHandle<JSTaggedValue> &bindingName)
8484514f5e3Sopenharmony_ci{
8494514f5e3Sopenharmony_ci    NewObjectHook();
8504514f5e3Sopenharmony_ci    TaggedObject *header = sHeap_->AllocateOldOrHugeObject(thread_,
8514514f5e3Sopenharmony_ci        JSHClass::Cast(thread_->GlobalConstants()->GetResolvedBindingClass().GetTaggedObject()));
8524514f5e3Sopenharmony_ci    JSHandle<ResolvedBinding> obj(thread_, header);
8534514f5e3Sopenharmony_ci    obj->SetModule(thread_, module);
8544514f5e3Sopenharmony_ci    obj->SetBindingName(thread_, bindingName);
8554514f5e3Sopenharmony_ci    return obj;
8564514f5e3Sopenharmony_ci}
8574514f5e3Sopenharmony_ci
8584514f5e3Sopenharmony_ciJSHandle<ResolvedRecordIndexBinding> ObjectFactory::NewSResolvedRecordIndexBindingRecord()
8594514f5e3Sopenharmony_ci{
8604514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> undefinedValue = thread_->GlobalConstants()->GetHandledUndefined();
8614514f5e3Sopenharmony_ci    JSHandle<EcmaString> ecmaModule(undefinedValue);
8624514f5e3Sopenharmony_ci    JSHandle<EcmaString> fileName(undefinedValue);
8634514f5e3Sopenharmony_ci    int32_t index = 0;
8644514f5e3Sopenharmony_ci    return NewSResolvedRecordIndexBindingRecord(ecmaModule, fileName, index);
8654514f5e3Sopenharmony_ci}
8664514f5e3Sopenharmony_ci
8674514f5e3Sopenharmony_ciJSHandle<ResolvedRecordIndexBinding> ObjectFactory::NewSResolvedRecordIndexBindingRecord(
8684514f5e3Sopenharmony_ci    const JSHandle<EcmaString> &moduleRecord, const JSHandle<EcmaString> &abcFileName, int32_t index)
8694514f5e3Sopenharmony_ci{
8704514f5e3Sopenharmony_ci    NewObjectHook();
8714514f5e3Sopenharmony_ci    TaggedObject *header = sHeap_->AllocateOldOrHugeObject(thread_,
8724514f5e3Sopenharmony_ci        JSHClass::Cast(thread_->GlobalConstants()->GetResolvedRecordIndexBindingClass().GetTaggedObject()));
8734514f5e3Sopenharmony_ci    JSHandle<ResolvedRecordIndexBinding> obj(thread_, header);
8744514f5e3Sopenharmony_ci    obj->SetModuleRecord(thread_, moduleRecord);
8754514f5e3Sopenharmony_ci    obj->SetAbcFileName(thread_, abcFileName);
8764514f5e3Sopenharmony_ci    obj->SetIndex(index);
8774514f5e3Sopenharmony_ci    return obj;
8784514f5e3Sopenharmony_ci}
8794514f5e3Sopenharmony_ci
8804514f5e3Sopenharmony_ciJSHandle<ResolvedRecordBinding> ObjectFactory::NewSResolvedRecordBindingRecord()
8814514f5e3Sopenharmony_ci{
8824514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> undefinedValue = thread_->GlobalConstants()->GetHandledUndefined();
8834514f5e3Sopenharmony_ci    JSHandle<EcmaString> ecmaModule(undefinedValue);
8844514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> bindingName(undefinedValue);
8854514f5e3Sopenharmony_ci    return NewSResolvedRecordBindingRecord(ecmaModule, bindingName);
8864514f5e3Sopenharmony_ci}
8874514f5e3Sopenharmony_ci
8884514f5e3Sopenharmony_ciJSHandle<ResolvedRecordBinding> ObjectFactory::NewSResolvedRecordBindingRecord(
8894514f5e3Sopenharmony_ci    const JSHandle<EcmaString> &moduleRecord, const JSHandle<JSTaggedValue> &bindingName)
8904514f5e3Sopenharmony_ci{
8914514f5e3Sopenharmony_ci    NewObjectHook();
8924514f5e3Sopenharmony_ci    TaggedObject *header = sHeap_->AllocateOldOrHugeObject(thread_,
8934514f5e3Sopenharmony_ci        JSHClass::Cast(thread_->GlobalConstants()->GetResolvedRecordBindingClass().GetTaggedObject()));
8944514f5e3Sopenharmony_ci    JSHandle<ResolvedRecordBinding> obj(thread_, header);
8954514f5e3Sopenharmony_ci    obj->SetModuleRecord(thread_, moduleRecord);
8964514f5e3Sopenharmony_ci    obj->SetBindingName(thread_, bindingName);
8974514f5e3Sopenharmony_ci    return obj;
8984514f5e3Sopenharmony_ci}
8994514f5e3Sopenharmony_ci
9004514f5e3Sopenharmony_ciJSHandle<AOTLiteralInfo> ObjectFactory::NewSAOTLiteralInfo(uint32_t length, JSTaggedValue initVal)
9014514f5e3Sopenharmony_ci{
9024514f5e3Sopenharmony_ci    NewObjectHook();
9034514f5e3Sopenharmony_ci    size_t size = AOTLiteralInfo::ComputeSize(length);
9044514f5e3Sopenharmony_ci    auto header = sHeap_->AllocateOldOrHugeObject(thread_,
9054514f5e3Sopenharmony_ci        JSHClass::Cast(sHeap_->GetGlobalConst()->GetAOTLiteralInfoClass().GetTaggedObject()), size);
9064514f5e3Sopenharmony_ci
9074514f5e3Sopenharmony_ci    JSHandle<AOTLiteralInfo> aotLiteralInfo(thread_, header);
9084514f5e3Sopenharmony_ci    aotLiteralInfo->InitializeWithSpecialValue(initVal, length);
9094514f5e3Sopenharmony_ci    return aotLiteralInfo;
9104514f5e3Sopenharmony_ci}
9114514f5e3Sopenharmony_ci
9124514f5e3Sopenharmony_ciJSHandle<SendableEnv> ObjectFactory::NewSendableEnv(int numSlots)
9134514f5e3Sopenharmony_ci{
9144514f5e3Sopenharmony_ci    NewObjectHook();
9154514f5e3Sopenharmony_ci    size_t size = SendableEnv::ComputeSize(numSlots);
9164514f5e3Sopenharmony_ci    auto header = sHeap_->AllocateOldOrHugeObject(thread_,
9174514f5e3Sopenharmony_ci        JSHClass::Cast(sHeap_->GetGlobalConst()->GetSendableEnvClass().GetTaggedObject()), size);
9184514f5e3Sopenharmony_ci    JSHandle<SendableEnv> array(thread_, header);
9194514f5e3Sopenharmony_ci    array->InitializeWithSpecialValue(JSTaggedValue::Hole(), numSlots + SendableEnv::SENDABLE_RESERVED_ENV_LENGTH);
9204514f5e3Sopenharmony_ci    return array;
9214514f5e3Sopenharmony_ci}
9224514f5e3Sopenharmony_ci
9234514f5e3Sopenharmony_ciJSHandle<JSFunction> ObjectFactory::NewJSSendableFunction(const JSHandle<Method> &methodHandle)
9244514f5e3Sopenharmony_ci{
9254514f5e3Sopenharmony_ci    JSHandle<GlobalEnv> env = vm_->GetGlobalEnv();
9264514f5e3Sopenharmony_ci    FunctionKind kind = methodHandle->GetFunctionKind();
9274514f5e3Sopenharmony_ci    JSHandle<JSHClass> hclass;
9284514f5e3Sopenharmony_ci    switch (kind) {
9294514f5e3Sopenharmony_ci        case FunctionKind::ASYNC_FUNCTION: {
9304514f5e3Sopenharmony_ci            hclass = JSHandle<JSHClass>::Cast(env->GetSAsyncFunctionClass());
9314514f5e3Sopenharmony_ci            break;
9324514f5e3Sopenharmony_ci        }
9334514f5e3Sopenharmony_ci        case FunctionKind::BASE_CONSTRUCTOR: {
9344514f5e3Sopenharmony_ci            hclass = JSHandle<JSHClass>::Cast(env->GetSFunctionClassWithProto());
9354514f5e3Sopenharmony_ci            break;
9364514f5e3Sopenharmony_ci        }
9374514f5e3Sopenharmony_ci        default:
9384514f5e3Sopenharmony_ci            LOG_ECMA(FATAL) << "this branch is unreachable";
9394514f5e3Sopenharmony_ci            UNREACHABLE();
9404514f5e3Sopenharmony_ci    }
9414514f5e3Sopenharmony_ci
9424514f5e3Sopenharmony_ci    JSHandle<JSFunction> func = NewSFunctionByHClass(methodHandle, hclass);
9434514f5e3Sopenharmony_ci    ASSERT_NO_ABRUPT_COMPLETION(thread_);
9444514f5e3Sopenharmony_ci    return func;
9454514f5e3Sopenharmony_ci}
9464514f5e3Sopenharmony_ci}  // namespace panda::ecmascript
947