14514f5e3Sopenharmony_ci/*
24514f5e3Sopenharmony_ci * Copyright (c) 2024 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/runtime.h"
174514f5e3Sopenharmony_ci#include <memory>
184514f5e3Sopenharmony_ci
194514f5e3Sopenharmony_ci#include "ecmascript/checkpoint/thread_state_transition.h"
204514f5e3Sopenharmony_ci#include "ecmascript/jit/jit.h"
214514f5e3Sopenharmony_ci#include "ecmascript/jspandafile/program_object.h"
224514f5e3Sopenharmony_ci#include "ecmascript/mem/heap-inl.h"
234514f5e3Sopenharmony_ci#include "ecmascript/mem/mem_map_allocator.h"
244514f5e3Sopenharmony_cinamespace panda::ecmascript {
254514f5e3Sopenharmony_ciusing PGOProfilerManager = pgo::PGOProfilerManager;
264514f5e3Sopenharmony_ci
274514f5e3Sopenharmony_ciint32_t Runtime::vmCount_ = 0;
284514f5e3Sopenharmony_ciint32_t Runtime::destroyCount_ = 0;
294514f5e3Sopenharmony_cibool Runtime::firstVmCreated_ = false;
304514f5e3Sopenharmony_ciMutex *Runtime::vmCreationLock_ = new Mutex();
314514f5e3Sopenharmony_ciRuntime *Runtime::instance_ = nullptr;
324514f5e3Sopenharmony_ci
334514f5e3Sopenharmony_ciRuntime *Runtime::GetInstance()
344514f5e3Sopenharmony_ci{
354514f5e3Sopenharmony_ci    ASSERT(instance_ != nullptr);
364514f5e3Sopenharmony_ci    return instance_;
374514f5e3Sopenharmony_ci}
384514f5e3Sopenharmony_ci
394514f5e3Sopenharmony_ciRuntime::~Runtime()
404514f5e3Sopenharmony_ci{
414514f5e3Sopenharmony_ci    LockHolder lock(constpoolLock_);
424514f5e3Sopenharmony_ci    auto iter = globalSharedConstpools_.begin();
434514f5e3Sopenharmony_ci    while (iter != globalSharedConstpools_.end()) {
444514f5e3Sopenharmony_ci        LOG_ECMA(INFO) << "remove js pandafile by vm destruct, file:" << iter->first->GetJSPandaFileDesc();
454514f5e3Sopenharmony_ci        JSPandaFileManager::GetInstance()->RemoveJSPandaFile(iter->first);
464514f5e3Sopenharmony_ci        iter->second.clear();
474514f5e3Sopenharmony_ci        iter++;
484514f5e3Sopenharmony_ci    }
494514f5e3Sopenharmony_ci    globalSharedConstpools_.clear();
504514f5e3Sopenharmony_ci    delete externalRegisteredStringTable_;
514514f5e3Sopenharmony_ci    externalRegisteredStringTable_ = nullptr;
524514f5e3Sopenharmony_ci}
534514f5e3Sopenharmony_ci
544514f5e3Sopenharmony_civoid Runtime::CreateIfFirstVm(const JSRuntimeOptions &options)
554514f5e3Sopenharmony_ci{
564514f5e3Sopenharmony_ci    LockHolder lock(*vmCreationLock_);
574514f5e3Sopenharmony_ci    if (!firstVmCreated_) {
584514f5e3Sopenharmony_ci        Log::Initialize(options);
594514f5e3Sopenharmony_ci        EcmaVM::InitializeIcuData(options);
604514f5e3Sopenharmony_ci        MemMapAllocator::GetInstance()->Initialize(ecmascript::DEFAULT_REGION_SIZE);
614514f5e3Sopenharmony_ci        PGOProfilerManager::GetInstance()->Initialize(options.GetPGOProfilerPath(),
624514f5e3Sopenharmony_ci                                                      options.GetPGOHotnessThreshold());
634514f5e3Sopenharmony_ci        PGOProfilerManager::GetInstance()->SetEnableForceIC(options.IsEnableForceIC());
644514f5e3Sopenharmony_ci        ASSERT(instance_ == nullptr);
654514f5e3Sopenharmony_ci        instance_ = new Runtime();
664514f5e3Sopenharmony_ci        SharedHeap::CreateNewInstance();
674514f5e3Sopenharmony_ci        DaemonThread::CreateNewInstance();
684514f5e3Sopenharmony_ci        firstVmCreated_ = true;
694514f5e3Sopenharmony_ci    }
704514f5e3Sopenharmony_ci}
714514f5e3Sopenharmony_ci
724514f5e3Sopenharmony_civoid Runtime::InitializeIfFirstVm(EcmaVM *vm)
734514f5e3Sopenharmony_ci{
744514f5e3Sopenharmony_ci    {
754514f5e3Sopenharmony_ci        LockHolder lock(*vmCreationLock_);
764514f5e3Sopenharmony_ci        if (++vmCount_ == 1) {
774514f5e3Sopenharmony_ci            ThreadManagedScope managedScope(vm->GetAssociatedJSThread());
784514f5e3Sopenharmony_ci            PreInitialization(vm);
794514f5e3Sopenharmony_ci            bool isEnableFastJit = vm->GetJSOptions().IsEnableJIT() && vm->GetJSOptions().GetEnableAsmInterpreter();
804514f5e3Sopenharmony_ci            bool isEnableBaselineJit =
814514f5e3Sopenharmony_ci                vm->GetJSOptions().IsEnableBaselineJIT() && vm->GetJSOptions().GetEnableAsmInterpreter();
824514f5e3Sopenharmony_ci            Jit::GetInstance()->SetEnableOrDisable(vm->GetJSOptions(), isEnableFastJit, isEnableBaselineJit);
834514f5e3Sopenharmony_ci            vm->Initialize();
844514f5e3Sopenharmony_ci            PostInitialization(vm);
854514f5e3Sopenharmony_ci        }
864514f5e3Sopenharmony_ci    }
874514f5e3Sopenharmony_ci    if (!vm->IsInitialized()) {
884514f5e3Sopenharmony_ci        ThreadManagedScope managedScope(vm->GetAssociatedJSThread());
894514f5e3Sopenharmony_ci        vm->Initialize();
904514f5e3Sopenharmony_ci    }
914514f5e3Sopenharmony_ci}
924514f5e3Sopenharmony_ci
934514f5e3Sopenharmony_civoid Runtime::PreInitialization(const EcmaVM *vm)
944514f5e3Sopenharmony_ci{
954514f5e3Sopenharmony_ci    mainThread_ = vm->GetAssociatedJSThread();
964514f5e3Sopenharmony_ci    mainThread_->SetMainThread();
974514f5e3Sopenharmony_ci    nativeAreaAllocator_ = std::make_unique<NativeAreaAllocator>();
984514f5e3Sopenharmony_ci    heapRegionAllocator_ = std::make_unique<HeapRegionAllocator>();
994514f5e3Sopenharmony_ci    stringTable_ = std::make_unique<EcmaStringTable>();
1004514f5e3Sopenharmony_ci    SharedHeap::GetInstance()->Initialize(nativeAreaAllocator_.get(), heapRegionAllocator_.get(),
1014514f5e3Sopenharmony_ci        const_cast<EcmaVM*>(vm)->GetJSOptions(), DaemonThread::GetInstance());
1024514f5e3Sopenharmony_ci}
1034514f5e3Sopenharmony_ci
1044514f5e3Sopenharmony_civoid Runtime::PostInitialization(const EcmaVM *vm)
1054514f5e3Sopenharmony_ci{
1064514f5e3Sopenharmony_ci    // Use the main thread's globalconst after it has initialized,
1074514f5e3Sopenharmony_ci    // and copy shared parts to other thread's later.
1084514f5e3Sopenharmony_ci    sharedConstInited_ = true;
1094514f5e3Sopenharmony_ci    globalEnv_ = vm->GetGlobalEnv().GetTaggedValue();
1104514f5e3Sopenharmony_ci    globalConst_.CopySharedConstantsFrom(mainThread_->GlobalConstants());
1114514f5e3Sopenharmony_ci    SharedHeap::GetInstance()->PostInitialization(&globalConst_, const_cast<EcmaVM*>(vm)->GetJSOptions());
1124514f5e3Sopenharmony_ci    SharedModuleManager::GetInstance()->Initialize();
1134514f5e3Sopenharmony_ci}
1144514f5e3Sopenharmony_ci
1154514f5e3Sopenharmony_civoid Runtime::DestroyIfLastVm()
1164514f5e3Sopenharmony_ci{
1174514f5e3Sopenharmony_ci    LockHolder lock(*vmCreationLock_);
1184514f5e3Sopenharmony_ci    if (--vmCount_ <= 0) {
1194514f5e3Sopenharmony_ci        SharedModuleManager::GetInstance()->SharedNativeObjDestory();
1204514f5e3Sopenharmony_ci        SharedHeap::GetInstance()->WaitAllTasksFinishedAfterAllJSThreadEliminated();
1214514f5e3Sopenharmony_ci        DaemonThread::DestroyInstance();
1224514f5e3Sopenharmony_ci        SharedHeap::DestroyInstance();
1234514f5e3Sopenharmony_ci        AnFileDataManager::GetInstance()->SafeDestroyAllData();
1244514f5e3Sopenharmony_ci        MemMapAllocator::GetInstance()->Finalize();
1254514f5e3Sopenharmony_ci        PGOProfilerManager::GetInstance()->Destroy();
1264514f5e3Sopenharmony_ci        SharedModuleManager::GetInstance()->Destroy();
1274514f5e3Sopenharmony_ci        Jit::GetInstance()->Destroy();
1284514f5e3Sopenharmony_ci        ASSERT(instance_ != nullptr);
1294514f5e3Sopenharmony_ci        delete instance_;
1304514f5e3Sopenharmony_ci        instance_ = nullptr;
1314514f5e3Sopenharmony_ci        firstVmCreated_ = false;
1324514f5e3Sopenharmony_ci    }
1334514f5e3Sopenharmony_ci}
1344514f5e3Sopenharmony_ci
1354514f5e3Sopenharmony_civoid Runtime::RegisterThread(JSThread* newThread)
1364514f5e3Sopenharmony_ci{
1374514f5e3Sopenharmony_ci    LockHolder lock(threadsLock_);
1384514f5e3Sopenharmony_ci    ASSERT(std::find(threads_.begin(), threads_.end(), newThread) == threads_.end());
1394514f5e3Sopenharmony_ci    threads_.emplace_back(newThread);
1404514f5e3Sopenharmony_ci    // send all current suspended requests to the new thread
1414514f5e3Sopenharmony_ci    for (uint32_t i = 0; i < suspendNewCount_; i++) {
1424514f5e3Sopenharmony_ci        newThread->SuspendThread(true);
1434514f5e3Sopenharmony_ci    }
1444514f5e3Sopenharmony_ci}
1454514f5e3Sopenharmony_ci
1464514f5e3Sopenharmony_ci// Note: currently only called when thread is to be destroyed.
1474514f5e3Sopenharmony_civoid Runtime::UnregisterThread(JSThread* thread)
1484514f5e3Sopenharmony_ci{
1494514f5e3Sopenharmony_ci    LockHolder lock(threadsLock_);
1504514f5e3Sopenharmony_ci    ASSERT(std::find(threads_.begin(), threads_.end(), thread) != threads_.end());
1514514f5e3Sopenharmony_ci    ASSERT(!thread->IsInRunningState());
1524514f5e3Sopenharmony_ci    threads_.remove(thread);
1534514f5e3Sopenharmony_ci}
1544514f5e3Sopenharmony_ci
1554514f5e3Sopenharmony_civoid Runtime::SuspendAll(JSThread *current)
1564514f5e3Sopenharmony_ci{
1574514f5e3Sopenharmony_ci    ASSERT(current != nullptr);
1584514f5e3Sopenharmony_ci    ASSERT(!current->IsInRunningState());
1594514f5e3Sopenharmony_ci#ifndef NDEBUG
1604514f5e3Sopenharmony_ci    ASSERT(!current->HasLaunchedSuspendAll());
1614514f5e3Sopenharmony_ci    current->LaunchSuspendAll();
1624514f5e3Sopenharmony_ci#endif
1634514f5e3Sopenharmony_ci    SuspendAllThreadsImpl(current);
1644514f5e3Sopenharmony_ci}
1654514f5e3Sopenharmony_ci
1664514f5e3Sopenharmony_civoid Runtime::ResumeAll(JSThread *current)
1674514f5e3Sopenharmony_ci{
1684514f5e3Sopenharmony_ci    ASSERT(current != nullptr);
1694514f5e3Sopenharmony_ci    ASSERT(!current->IsInRunningState());
1704514f5e3Sopenharmony_ci#ifndef NDEBUG
1714514f5e3Sopenharmony_ci    ASSERT(current->HasLaunchedSuspendAll());
1724514f5e3Sopenharmony_ci    current->CompleteSuspendAll();
1734514f5e3Sopenharmony_ci#endif
1744514f5e3Sopenharmony_ci    ResumeAllThreadsImpl(current);
1754514f5e3Sopenharmony_ci}
1764514f5e3Sopenharmony_ci
1774514f5e3Sopenharmony_civoid Runtime::SuspendAllThreadsImpl(JSThread *current)
1784514f5e3Sopenharmony_ci{
1794514f5e3Sopenharmony_ci    SuspendBarrier barrier;
1804514f5e3Sopenharmony_ci    for (uint32_t iterCount = 1U;; ++iterCount) {
1814514f5e3Sopenharmony_ci        {
1824514f5e3Sopenharmony_ci            LockHolder lock(threadsLock_);
1834514f5e3Sopenharmony_ci            if (suspendNewCount_ == 0) {
1844514f5e3Sopenharmony_ci                suspendNewCount_++;
1854514f5e3Sopenharmony_ci                if (threads_.size() == 1) {
1864514f5e3Sopenharmony_ci                    ASSERT(current == mainThread_ || current->IsDaemonThread());
1874514f5e3Sopenharmony_ci                    return;
1884514f5e3Sopenharmony_ci                }
1894514f5e3Sopenharmony_ci                if (threads_.size() < 1) {
1904514f5e3Sopenharmony_ci                    return;
1914514f5e3Sopenharmony_ci                }
1924514f5e3Sopenharmony_ci                barrier.Initialize(threads_.size() - 1);
1934514f5e3Sopenharmony_ci                for (const auto& thread: threads_) {
1944514f5e3Sopenharmony_ci                    if (thread == current) {
1954514f5e3Sopenharmony_ci                        continue;
1964514f5e3Sopenharmony_ci                    }
1974514f5e3Sopenharmony_ci                    thread->SuspendThread(+1, &barrier);
1984514f5e3Sopenharmony_ci                    // The two flags, SUSPEND_REQUEST and ACTIVE_BARRIER, are set by Suspend-Thread guarded by
1994514f5e3Sopenharmony_ci                    // suspendLock_, so the target thread-I may do not see these two flags in time. As a result, it
2004514f5e3Sopenharmony_ci                    // can switch its state freely without responding to the ACTIVE_BARRIER flag and the
2014514f5e3Sopenharmony_ci                    // suspend-thread will always wait it. However, as long as it sees the flags, the actions of
2024514f5e3Sopenharmony_ci                    // passing barrier will be triggered. When the thread-I switches from NON_RUNNING to RUNNING,
2034514f5e3Sopenharmony_ci                    // it will firstly pass the barrier and then be blocked by the SUSPEND_REQUEST flag. If the
2044514f5e3Sopenharmony_ci                    // thread-I switches from RUNNING to NON_RUNNING, it will switch the state and then act on the
2054514f5e3Sopenharmony_ci                    // barrier. If the thread-I go to checkpoint in RUNNING state, it will act on the barrier
2064514f5e3Sopenharmony_ci                    // and be blocked by SUSPEND_REQUEST flag.
2074514f5e3Sopenharmony_ci                    if (thread->IsSuspended()) {
2084514f5e3Sopenharmony_ci                        // Because of the multi-threads situation, currently thread-I may be in RUNNING state or is
2094514f5e3Sopenharmony_ci                        // goding to be RUNNING state even inside this branch. In both scenarios, for instance of
2104514f5e3Sopenharmony_ci                        // RUNNING state, according to the modification order of atomic-variable stateAndFlags_,
2114514f5e3Sopenharmony_ci                        // thread-I will see the SUSPEND_REQUEST and ACTIVE_BARRIER and act on them before switching
2124514f5e3Sopenharmony_ci                        // to RUNNING. Besides, notice the using of suspendLock_ inside PassSuspendBarrier(), there
2134514f5e3Sopenharmony_ci                        // is not data-race for passing barrier.
2144514f5e3Sopenharmony_ci                        thread->PassSuspendBarrier();
2154514f5e3Sopenharmony_ci                    }
2164514f5e3Sopenharmony_ci                }
2174514f5e3Sopenharmony_ci                break;
2184514f5e3Sopenharmony_ci            }
2194514f5e3Sopenharmony_ci        }
2204514f5e3Sopenharmony_ci        if (iterCount < MAX_SUSPEND_RETRIES) {
2214514f5e3Sopenharmony_ci            LockHolder lock(threadsLock_);
2224514f5e3Sopenharmony_ci            if (suspendNewCount_ != 0) {
2234514f5e3Sopenharmony_ci                // Someone has already suspended all threads.
2244514f5e3Sopenharmony_ci                // Wait until it finishes.
2254514f5e3Sopenharmony_ci                threadSuspendCondVar_.Wait(&threadsLock_);
2264514f5e3Sopenharmony_ci            }
2274514f5e3Sopenharmony_ci        } else {
2284514f5e3Sopenharmony_ci            LOG_ECMA(FATAL) << "Too many SuspendAll retries!";
2294514f5e3Sopenharmony_ci            UNREACHABLE();
2304514f5e3Sopenharmony_ci        }
2314514f5e3Sopenharmony_ci    }
2324514f5e3Sopenharmony_ci    barrier.Wait();
2334514f5e3Sopenharmony_ci}
2344514f5e3Sopenharmony_ci
2354514f5e3Sopenharmony_civoid Runtime::ResumeAllThreadsImpl(JSThread *current)
2364514f5e3Sopenharmony_ci{
2374514f5e3Sopenharmony_ci    LockHolder lock(threadsLock_);
2384514f5e3Sopenharmony_ci    if (suspendNewCount_ > 0) {
2394514f5e3Sopenharmony_ci        suspendNewCount_--;
2404514f5e3Sopenharmony_ci    }
2414514f5e3Sopenharmony_ci    if (suspendNewCount_ == 0) {
2424514f5e3Sopenharmony_ci        // Signal to waiting to suspend threads
2434514f5e3Sopenharmony_ci        threadSuspendCondVar_.Signal();
2444514f5e3Sopenharmony_ci    }
2454514f5e3Sopenharmony_ci    for (const auto& thread : threads_) {
2464514f5e3Sopenharmony_ci        if (thread != current) {
2474514f5e3Sopenharmony_ci            thread->ResumeThread(true);
2484514f5e3Sopenharmony_ci        }
2494514f5e3Sopenharmony_ci    }
2504514f5e3Sopenharmony_ci}
2514514f5e3Sopenharmony_ci
2524514f5e3Sopenharmony_civoid Runtime::IterateSerializeRoot(const RootVisitor &v)
2534514f5e3Sopenharmony_ci{
2544514f5e3Sopenharmony_ci    LockHolder lock(serializeLock_);
2554514f5e3Sopenharmony_ci    for (auto &it : serializeRootMap_) {
2564514f5e3Sopenharmony_ci        it.second->Iterate(v);
2574514f5e3Sopenharmony_ci    }
2584514f5e3Sopenharmony_ci}
2594514f5e3Sopenharmony_ci
2604514f5e3Sopenharmony_cibool Runtime::HasCachedConstpool(const JSPandaFile *jsPandaFile)
2614514f5e3Sopenharmony_ci{
2624514f5e3Sopenharmony_ci    LockHolder lock(constpoolLock_);
2634514f5e3Sopenharmony_ci    return globalSharedConstpools_.find(jsPandaFile) != globalSharedConstpools_.end();
2644514f5e3Sopenharmony_ci}
2654514f5e3Sopenharmony_ci
2664514f5e3Sopenharmony_ciJSTaggedValue Runtime::FindConstpool(const JSPandaFile *jsPandaFile, int32_t index)
2674514f5e3Sopenharmony_ci{
2684514f5e3Sopenharmony_ci    LockHolder lock(constpoolLock_);
2694514f5e3Sopenharmony_ci    return FindConstpoolUnlocked(jsPandaFile, index);
2704514f5e3Sopenharmony_ci}
2714514f5e3Sopenharmony_ci
2724514f5e3Sopenharmony_ciJSTaggedValue Runtime::FindConstpoolUnlocked(const JSPandaFile *jsPandaFile, int32_t index)
2734514f5e3Sopenharmony_ci{
2744514f5e3Sopenharmony_ci    auto iter = globalSharedConstpools_.find(jsPandaFile);
2754514f5e3Sopenharmony_ci    if (iter == globalSharedConstpools_.end()) {
2764514f5e3Sopenharmony_ci        return JSTaggedValue::Hole();
2774514f5e3Sopenharmony_ci    }
2784514f5e3Sopenharmony_ci    auto constpoolIter = iter->second.find(index);
2794514f5e3Sopenharmony_ci    if (constpoolIter == iter->second.end()) {
2804514f5e3Sopenharmony_ci        return JSTaggedValue::Hole();
2814514f5e3Sopenharmony_ci    }
2824514f5e3Sopenharmony_ci    return constpoolIter->second;
2834514f5e3Sopenharmony_ci}
2844514f5e3Sopenharmony_ci
2854514f5e3Sopenharmony_ciJSHandle<ConstantPool> Runtime::AddOrUpdateConstpool(const JSPandaFile *jsPandaFile,
2864514f5e3Sopenharmony_ci                                                     JSHandle<ConstantPool> constpool,
2874514f5e3Sopenharmony_ci                                                     int32_t index)
2884514f5e3Sopenharmony_ci{
2894514f5e3Sopenharmony_ci    LockHolder lock(constpoolLock_);
2904514f5e3Sopenharmony_ci    if (globalSharedConstpools_.find(jsPandaFile) == globalSharedConstpools_.end()) {
2914514f5e3Sopenharmony_ci        globalSharedConstpools_[jsPandaFile] = CMap<int32_t, JSTaggedValue>();
2924514f5e3Sopenharmony_ci    }
2934514f5e3Sopenharmony_ci    auto &constpoolMap = globalSharedConstpools_[jsPandaFile];
2944514f5e3Sopenharmony_ci    auto iter = constpoolMap.find(index);
2954514f5e3Sopenharmony_ci    if (iter == constpoolMap.end()) {
2964514f5e3Sopenharmony_ci        int32_t constpoolIndex = GetAndIncreaseSharedConstpoolCount();
2974514f5e3Sopenharmony_ci        constpool->SetUnsharedConstpoolIndex(JSTaggedValue(constpoolIndex));
2984514f5e3Sopenharmony_ci        constpoolMap.insert({index, constpool.GetTaggedValue()});
2994514f5e3Sopenharmony_ci        return constpool;
3004514f5e3Sopenharmony_ci    }
3014514f5e3Sopenharmony_ci    return JSHandle<ConstantPool>(reinterpret_cast<uintptr_t>(&iter->second));
3024514f5e3Sopenharmony_ci}
3034514f5e3Sopenharmony_ci
3044514f5e3Sopenharmony_cistd::optional<std::reference_wrapper<CMap<int32_t, JSTaggedValue>>> Runtime::FindConstpools(
3054514f5e3Sopenharmony_ci    const JSPandaFile *jsPandaFile)
3064514f5e3Sopenharmony_ci{
3074514f5e3Sopenharmony_ci    LockHolder lock(constpoolLock_);
3084514f5e3Sopenharmony_ci    auto iter = globalSharedConstpools_.find(jsPandaFile);
3094514f5e3Sopenharmony_ci    if (iter == globalSharedConstpools_.end()) {
3104514f5e3Sopenharmony_ci        return std::nullopt;
3114514f5e3Sopenharmony_ci    }
3124514f5e3Sopenharmony_ci    return iter->second;
3134514f5e3Sopenharmony_ci}
3144514f5e3Sopenharmony_ci
3154514f5e3Sopenharmony_civoid Runtime::ProcessNativeDeleteInSharedGC(const WeakRootVisitor &visitor)
3164514f5e3Sopenharmony_ci{
3174514f5e3Sopenharmony_ci    // No need lock here, only shared gc will sweep shared constpool, meanwhile other threads are suspended.
3184514f5e3Sopenharmony_ci    auto iterator = globalSharedConstpools_.begin();
3194514f5e3Sopenharmony_ci    ECMA_BYTRACE_NAME(HITRACE_TAG_ARK, "Constpools:" + std::to_string(globalSharedConstpools_.size()));
3204514f5e3Sopenharmony_ci    while (iterator != globalSharedConstpools_.end()) {
3214514f5e3Sopenharmony_ci        auto &constpools = iterator->second;
3224514f5e3Sopenharmony_ci        auto constpoolIter = constpools.begin();
3234514f5e3Sopenharmony_ci        while (constpoolIter != constpools.end()) {
3244514f5e3Sopenharmony_ci            JSTaggedValue constpoolVal = constpoolIter->second;
3254514f5e3Sopenharmony_ci            if (constpoolVal.IsHeapObject()) {
3264514f5e3Sopenharmony_ci                TaggedObject *obj = constpoolVal.GetTaggedObject();
3274514f5e3Sopenharmony_ci                auto fwd = visitor(obj);
3284514f5e3Sopenharmony_ci                if (fwd == nullptr) {
3294514f5e3Sopenharmony_ci                    int32_t constpoolIndex =
3304514f5e3Sopenharmony_ci                        ConstantPool::Cast(constpoolVal.GetTaggedObject())->GetUnsharedConstpoolIndex();
3314514f5e3Sopenharmony_ci                    ASSERT(0 <= constpoolIndex && constpoolIndex != ConstantPool::CONSTPOOL_TYPE_FLAG &&
3324514f5e3Sopenharmony_ci                        constpoolIndex < UNSHARED_CONSTANTPOOL_COUNT);
3334514f5e3Sopenharmony_ci                    EraseUnusedConstpool(iterator->first, constpoolIter->first, constpoolIndex);
3344514f5e3Sopenharmony_ci                    constpoolIter = constpools.erase(constpoolIter);
3354514f5e3Sopenharmony_ci                    // when shared constpool is not referenced by any objects,
3364514f5e3Sopenharmony_ci                    // global unshared constpool count can be reuse.
3374514f5e3Sopenharmony_ci                    freeSharedConstpoolIndex_.insert(constpoolIndex);
3384514f5e3Sopenharmony_ci                    continue;
3394514f5e3Sopenharmony_ci                }
3404514f5e3Sopenharmony_ci                if (fwd != reinterpret_cast<TaggedObject *>(obj)) {
3414514f5e3Sopenharmony_ci                    constpoolIter->second = JSTaggedValue(fwd);
3424514f5e3Sopenharmony_ci                }
3434514f5e3Sopenharmony_ci            }
3444514f5e3Sopenharmony_ci            ++constpoolIter;
3454514f5e3Sopenharmony_ci        }
3464514f5e3Sopenharmony_ci        if (constpools.size() == 0) {
3474514f5e3Sopenharmony_ci            LOG_ECMA(INFO) << "remove js pandafile by gc, file:" << iterator->first->GetJSPandaFileDesc();
3484514f5e3Sopenharmony_ci            JSPandaFileManager::GetInstance()->RemoveJSPandaFile(iterator->first);
3494514f5e3Sopenharmony_ci            iterator = globalSharedConstpools_.erase(iterator);
3504514f5e3Sopenharmony_ci        } else {
3514514f5e3Sopenharmony_ci            ++iterator;
3524514f5e3Sopenharmony_ci        }
3534514f5e3Sopenharmony_ci    }
3544514f5e3Sopenharmony_ci}
3554514f5e3Sopenharmony_ci
3564514f5e3Sopenharmony_civoid Runtime::EraseUnusedConstpool(const JSPandaFile *jsPandaFile, int32_t index, int32_t constpoolIndex)
3574514f5e3Sopenharmony_ci{
3584514f5e3Sopenharmony_ci    GCIterateThreadList([jsPandaFile, index, constpoolIndex](JSThread* thread) {
3594514f5e3Sopenharmony_ci        ASSERT(!thread->IsInRunningState());
3604514f5e3Sopenharmony_ci        auto context = thread->GetCurrentEcmaContext();
3614514f5e3Sopenharmony_ci        context->EraseUnusedConstpool(jsPandaFile, index, constpoolIndex);
3624514f5e3Sopenharmony_ci    });
3634514f5e3Sopenharmony_ci}
3644514f5e3Sopenharmony_ci
3654514f5e3Sopenharmony_civoid Runtime::ProcessSharedNativeDelete(const WeakRootVisitor &visitor)
3664514f5e3Sopenharmony_ci{
3674514f5e3Sopenharmony_ci    SharedHeap::GetInstance()->ProcessSharedNativeDelete(visitor);
3684514f5e3Sopenharmony_ci}
3694514f5e3Sopenharmony_ci
3704514f5e3Sopenharmony_civoid Runtime::PushToSharedNativePointerList(JSNativePointer *pointer)
3714514f5e3Sopenharmony_ci{
3724514f5e3Sopenharmony_ci    SharedHeap::GetInstance()->PushToSharedNativePointerList(pointer);
3734514f5e3Sopenharmony_ci}
3744514f5e3Sopenharmony_ci
3754514f5e3Sopenharmony_civoid Runtime::InvokeSharedNativePointerCallbacks()
3764514f5e3Sopenharmony_ci{
3774514f5e3Sopenharmony_ci    auto &callbacks = GetSharedNativePointerCallbacks();
3784514f5e3Sopenharmony_ci    while (!callbacks.empty()) {
3794514f5e3Sopenharmony_ci        auto callbackPair = callbacks.back();
3804514f5e3Sopenharmony_ci        callbacks.pop_back();
3814514f5e3Sopenharmony_ci        ASSERT(callbackPair.first != nullptr && callbackPair.second.first != nullptr &&
3824514f5e3Sopenharmony_ci               callbackPair.second.second != nullptr);
3834514f5e3Sopenharmony_ci        auto callback = callbackPair.first;
3844514f5e3Sopenharmony_ci        (*callback)(nullptr, callbackPair.second.first, callbackPair.second.second);
3854514f5e3Sopenharmony_ci    }
3864514f5e3Sopenharmony_ci}
3874514f5e3Sopenharmony_ci}  // namespace panda::ecmascript
388