14514f5e3Sopenharmony_ci/* 24514f5e3Sopenharmony_ci * Copyright (c) 2021-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/ecma_vm.h" 174514f5e3Sopenharmony_ci 184514f5e3Sopenharmony_ci#include "ecmascript/builtins/builtins_ark_tools.h" 194514f5e3Sopenharmony_ci#include "ecmascript/pgo_profiler/pgo_profiler_manager.h" 204514f5e3Sopenharmony_ci#ifdef ARK_SUPPORT_INTL 214514f5e3Sopenharmony_ci#include "ecmascript/builtins/builtins_collator.h" 224514f5e3Sopenharmony_ci#include "ecmascript/builtins/builtins_date_time_format.h" 234514f5e3Sopenharmony_ci#include "ecmascript/builtins/builtins_number_format.h" 244514f5e3Sopenharmony_ci#endif 254514f5e3Sopenharmony_ci#include "ecmascript/builtins/builtins_global.h" 264514f5e3Sopenharmony_ci#include "ecmascript/builtins/builtins_object.h" 274514f5e3Sopenharmony_ci#include "ecmascript/builtins/builtins_promise_handler.h" 284514f5e3Sopenharmony_ci#include "ecmascript/builtins/builtins_proxy.h" 294514f5e3Sopenharmony_ci#include "ecmascript/jit/jit_task.h" 304514f5e3Sopenharmony_ci#if defined(ECMASCRIPT_SUPPORT_CPUPROFILER) 314514f5e3Sopenharmony_ci#include "ecmascript/dfx/cpu_profiler/cpu_profiler.h" 324514f5e3Sopenharmony_ci#endif 334514f5e3Sopenharmony_ci#if !WIN_OR_MAC_OR_IOS_PLATFORM 344514f5e3Sopenharmony_ci#include "ecmascript/dfx/hprof/heap_profiler.h" 354514f5e3Sopenharmony_ci#include "ecmascript/dfx/hprof/heap_profiler_interface.h" 364514f5e3Sopenharmony_ci#endif 374514f5e3Sopenharmony_ci#include "ecmascript/dfx/tracing/tracing.h" 384514f5e3Sopenharmony_ci#include "ecmascript/dfx/vmstat/function_call_timer.h" 394514f5e3Sopenharmony_ci#include "ecmascript/mem/shared_heap/shared_concurrent_marker.h" 404514f5e3Sopenharmony_ci#include "ecmascript/module/module_logger.h" 414514f5e3Sopenharmony_ci#include "ecmascript/pgo_profiler/pgo_trace.h" 424514f5e3Sopenharmony_ci#include "ecmascript/snapshot/mem/snapshot.h" 434514f5e3Sopenharmony_ci#include "ecmascript/stubs/runtime_stubs.h" 444514f5e3Sopenharmony_ci#include "ecmascript/ohos/jit_tools.h" 454514f5e3Sopenharmony_ci#include "ecmascript/ohos/aot_tools.h" 464514f5e3Sopenharmony_ci#include "ecmascript/checkpoint/thread_state_transition.h" 474514f5e3Sopenharmony_ci#include "ecmascript/mem/heap-inl.h" 484514f5e3Sopenharmony_ci 494514f5e3Sopenharmony_ci#if defined(PANDA_TARGET_OHOS) && !defined(STANDALONE_MODE) 504514f5e3Sopenharmony_ci#include "parameters.h" 514514f5e3Sopenharmony_ci#endif 524514f5e3Sopenharmony_ci 534514f5e3Sopenharmony_cinamespace panda::ecmascript { 544514f5e3Sopenharmony_ciusing RandomGenerator = base::RandomGenerator; 554514f5e3Sopenharmony_ciusing PGOProfilerManager = pgo::PGOProfilerManager; 564514f5e3Sopenharmony_ciusing JitTools = ohos::JitTools; 574514f5e3Sopenharmony_ciAOTFileManager *JsStackInfo::loader = nullptr; 584514f5e3Sopenharmony_ciJSRuntimeOptions *JsStackInfo::options = nullptr; 594514f5e3Sopenharmony_cibool EcmaVM::multiThreadCheck_ = false; 604514f5e3Sopenharmony_cibool EcmaVM::errorInfoEnhanced_ = false; 614514f5e3Sopenharmony_ci 624514f5e3Sopenharmony_ciEcmaVM *EcmaVM::Create(const JSRuntimeOptions &options) 634514f5e3Sopenharmony_ci{ 644514f5e3Sopenharmony_ci Runtime::CreateIfFirstVm(options); 654514f5e3Sopenharmony_ci auto heapType = options.IsWorker() ? EcmaParamConfiguration::HeapType::WORKER_HEAP : 664514f5e3Sopenharmony_ci EcmaParamConfiguration::HeapType::DEFAULT_HEAP; 674514f5e3Sopenharmony_ci size_t heapSize = options.GetHeapSize(); 684514f5e3Sopenharmony_ci#if defined(PANDA_TARGET_OHOS) && !defined(STANDALONE_MODE) 694514f5e3Sopenharmony_ci switch (heapType) { 704514f5e3Sopenharmony_ci case EcmaParamConfiguration::HeapType::WORKER_HEAP: 714514f5e3Sopenharmony_ci heapSize = OHOS::system::GetUintParameter<size_t>("persist.ark.heap.workersize", 0) * 1_MB; 724514f5e3Sopenharmony_ci break; 734514f5e3Sopenharmony_ci default: 744514f5e3Sopenharmony_ci heapSize = OHOS::system::GetUintParameter<size_t>("persist.ark.heap.defaultsize", 0) * 1_MB; 754514f5e3Sopenharmony_ci break; 764514f5e3Sopenharmony_ci } 774514f5e3Sopenharmony_ci#endif 784514f5e3Sopenharmony_ci auto config = EcmaParamConfiguration(heapType, 794514f5e3Sopenharmony_ci MemMapAllocator::GetInstance()->GetCapacity(), 804514f5e3Sopenharmony_ci heapSize); 814514f5e3Sopenharmony_ci JSRuntimeOptions newOptions = options; 824514f5e3Sopenharmony_ci // only define SUPPORT_ENABLE_ASM_INTERP can enable asm-interpreter 834514f5e3Sopenharmony_ci#if !defined(SUPPORT_ENABLE_ASM_INTERP) 844514f5e3Sopenharmony_ci newOptions.SetEnableAsmInterpreter(false); 854514f5e3Sopenharmony_ci#endif 864514f5e3Sopenharmony_ci auto vm = new EcmaVM(newOptions, config); 874514f5e3Sopenharmony_ci auto jsThread = JSThread::Create(vm); 884514f5e3Sopenharmony_ci vm->thread_ = jsThread; 894514f5e3Sopenharmony_ci Runtime::GetInstance()->InitializeIfFirstVm(vm); 904514f5e3Sopenharmony_ci if (JsStackInfo::loader == nullptr) { 914514f5e3Sopenharmony_ci JsStackInfo::loader = vm->GetAOTFileManager(); 924514f5e3Sopenharmony_ci } 934514f5e3Sopenharmony_ci if (JsStackInfo::options == nullptr) { 944514f5e3Sopenharmony_ci JsStackInfo::options = &(vm->GetJSOptions()); 954514f5e3Sopenharmony_ci } 964514f5e3Sopenharmony_ci#if defined(PANDA_TARGET_OHOS) && !defined(STANDALONE_MODE) 974514f5e3Sopenharmony_ci int arkProperties = OHOS::system::GetIntParameter<int>("persist.ark.properties", -1); 984514f5e3Sopenharmony_ci vm->GetJSOptions().SetArkProperties(arkProperties); 994514f5e3Sopenharmony_ci#endif 1004514f5e3Sopenharmony_ci return vm; 1014514f5e3Sopenharmony_ci} 1024514f5e3Sopenharmony_ci 1034514f5e3Sopenharmony_ci// static 1044514f5e3Sopenharmony_cibool EcmaVM::Destroy(EcmaVM *vm) 1054514f5e3Sopenharmony_ci{ 1064514f5e3Sopenharmony_ci if (UNLIKELY(vm == nullptr)) { 1074514f5e3Sopenharmony_ci return false; 1084514f5e3Sopenharmony_ci } 1094514f5e3Sopenharmony_ci delete vm; 1104514f5e3Sopenharmony_ci Runtime::DestroyIfLastVm(); 1114514f5e3Sopenharmony_ci return true; 1124514f5e3Sopenharmony_ci} 1134514f5e3Sopenharmony_ci 1144514f5e3Sopenharmony_civoid EcmaVM::PreFork() 1154514f5e3Sopenharmony_ci{ 1164514f5e3Sopenharmony_ci heap_->CompactHeapBeforeFork(); 1174514f5e3Sopenharmony_ci heap_->AdjustSpaceSizeForAppSpawn(); 1184514f5e3Sopenharmony_ci heap_->GetReadOnlySpace()->SetReadOnly(); 1194514f5e3Sopenharmony_ci heap_->DisableParallelGC(); 1204514f5e3Sopenharmony_ci SetPostForked(false); 1214514f5e3Sopenharmony_ci 1224514f5e3Sopenharmony_ci auto sHeap = SharedHeap::GetInstance(); 1234514f5e3Sopenharmony_ci sHeap->CompactHeapBeforeFork(thread_); 1244514f5e3Sopenharmony_ci sHeap->DisableParallelGC(thread_); 1254514f5e3Sopenharmony_ci} 1264514f5e3Sopenharmony_ci 1274514f5e3Sopenharmony_civoid EcmaVM::PostFork() 1284514f5e3Sopenharmony_ci{ 1294514f5e3Sopenharmony_ci RandomGenerator::InitRandom(GetAssociatedJSThread()); 1304514f5e3Sopenharmony_ci heap_->SetHeapMode(HeapMode::SHARE); 1314514f5e3Sopenharmony_ci GetAssociatedJSThread()->PostFork(); 1324514f5e3Sopenharmony_ci Taskpool::GetCurrentTaskpool()->Initialize(); 1334514f5e3Sopenharmony_ci SetPostForked(true); 1344514f5e3Sopenharmony_ci LOG_ECMA(INFO) << "multi-thread check enabled: " << GetThreadCheckStatus(); 1354514f5e3Sopenharmony_ci SignalAllReg(); 1364514f5e3Sopenharmony_ci SharedHeap::GetInstance()->EnableParallelGC(GetJSOptions()); 1374514f5e3Sopenharmony_ci DaemonThread::GetInstance()->StartRunning(); 1384514f5e3Sopenharmony_ci heap_->EnableParallelGC(); 1394514f5e3Sopenharmony_ci options_.SetPgoForceDump(false); 1404514f5e3Sopenharmony_ci std::string bundleName = PGOProfilerManager::GetInstance()->GetBundleName(); 1414514f5e3Sopenharmony_ci pgo::PGOTrace::GetInstance()->SetEnable(ohos::AotTools::GetPgoTraceEnable()); 1424514f5e3Sopenharmony_ci AotCrashInfo::GetInstance().SetOptionPGOProfiler(&options_, bundleName); 1434514f5e3Sopenharmony_ci ResetPGOProfiler(); 1444514f5e3Sopenharmony_ci processStartRealtime_ = InitializeStartRealTime(); 1454514f5e3Sopenharmony_ci 1464514f5e3Sopenharmony_ci Jit::GetInstance()->SetJitEnablePostFork(this, bundleName); 1474514f5e3Sopenharmony_ci#ifdef ENABLE_POSTFORK_FORCEEXPAND 1484514f5e3Sopenharmony_ci heap_->NotifyPostFork(); 1494514f5e3Sopenharmony_ci heap_->NotifyFinishColdStartSoon(); 1504514f5e3Sopenharmony_ci#endif 1514514f5e3Sopenharmony_ci ModuleLogger *moduleLogger = thread_->GetCurrentEcmaContext()->GetModuleLogger(); 1524514f5e3Sopenharmony_ci if (moduleLogger != nullptr) { 1534514f5e3Sopenharmony_ci moduleLogger->PostModuleLoggerTask(thread_->GetThreadId(), this); 1544514f5e3Sopenharmony_ci } 1554514f5e3Sopenharmony_ci#if defined(PANDA_TARGET_OHOS) && !defined(STANDALONE_MODE) 1564514f5e3Sopenharmony_ci int arkProperties = OHOS::system::GetIntParameter<int>("persist.ark.properties", -1); 1574514f5e3Sopenharmony_ci GetJSOptions().SetArkProperties(arkProperties); 1584514f5e3Sopenharmony_ci#endif 1594514f5e3Sopenharmony_ci auto startIdleMonitor = JSNApi::GetStartIdleMonitorCallback(); 1604514f5e3Sopenharmony_ci if (startIdleMonitor != nullptr) { 1614514f5e3Sopenharmony_ci startIdleMonitor(); 1624514f5e3Sopenharmony_ci } 1634514f5e3Sopenharmony_ci} 1644514f5e3Sopenharmony_ci 1654514f5e3Sopenharmony_ciEcmaVM::EcmaVM(JSRuntimeOptions options, EcmaParamConfiguration config) 1664514f5e3Sopenharmony_ci : nativeAreaAllocator_(std::make_unique<NativeAreaAllocator>()), 1674514f5e3Sopenharmony_ci heapRegionAllocator_(std::make_unique<HeapRegionAllocator>()), 1684514f5e3Sopenharmony_ci chunk_(nativeAreaAllocator_.get()), 1694514f5e3Sopenharmony_ci ecmaParamConfiguration_(std::move(config)) 1704514f5e3Sopenharmony_ci{ 1714514f5e3Sopenharmony_ci options_ = std::move(options); 1724514f5e3Sopenharmony_ci LOG_ECMA(DEBUG) << "multi-thread check enabled: " << GetThreadCheckStatus(); 1734514f5e3Sopenharmony_ci icEnabled_ = options_.EnableIC(); 1744514f5e3Sopenharmony_ci optionalLogEnabled_ = options_.EnableOptionalLog(); 1754514f5e3Sopenharmony_ci options_.ParseAsmInterOption(); 1764514f5e3Sopenharmony_ci SetEnableOsr(options_.IsEnableOSR() && options_.IsEnableJIT() && options_.GetEnableAsmInterpreter()); 1774514f5e3Sopenharmony_ci processStartRealtime_ = InitializeStartRealTime(); 1784514f5e3Sopenharmony_ci} 1794514f5e3Sopenharmony_ci 1804514f5e3Sopenharmony_ci// for jit 1814514f5e3Sopenharmony_ciEcmaVM::EcmaVM() 1824514f5e3Sopenharmony_ci : nativeAreaAllocator_(std::make_unique<NativeAreaAllocator>()), 1834514f5e3Sopenharmony_ci heapRegionAllocator_(nullptr), 1844514f5e3Sopenharmony_ci chunk_(nativeAreaAllocator_.get()) {} 1854514f5e3Sopenharmony_ci 1864514f5e3Sopenharmony_civoid EcmaVM::InitializeForJit(JitThread *jitThread) 1874514f5e3Sopenharmony_ci{ 1884514f5e3Sopenharmony_ci thread_ = jitThread; 1894514f5e3Sopenharmony_ci stringTable_ = Runtime::GetInstance()->GetEcmaStringTable(); 1904514f5e3Sopenharmony_ci ASSERT(stringTable_); 1914514f5e3Sopenharmony_ci // ObjectFactory only sypport alloc string in sharedheap 1924514f5e3Sopenharmony_ci factory_ = chunk_.New<ObjectFactory>(thread_, nullptr, SharedHeap::GetInstance()); 1934514f5e3Sopenharmony_ci SetIsJitCompileVM(true); 1944514f5e3Sopenharmony_ci} 1954514f5e3Sopenharmony_ci 1964514f5e3Sopenharmony_civoid EcmaVM::InitializePGOProfiler() 1974514f5e3Sopenharmony_ci{ 1984514f5e3Sopenharmony_ci bool isEnablePGOProfiler = IsEnablePGOProfiler(); 1994514f5e3Sopenharmony_ci if (pgoProfiler_ == nullptr) { 2004514f5e3Sopenharmony_ci pgoProfiler_ = PGOProfilerManager::GetInstance()->Build(this, isEnablePGOProfiler); 2014514f5e3Sopenharmony_ci } 2024514f5e3Sopenharmony_ci pgo::PGOTrace::GetInstance()->SetEnable(options_.GetPGOTrace() || ohos::AotTools::GetPgoTraceEnable()); 2034514f5e3Sopenharmony_ci thread_->SetPGOProfilerEnable(isEnablePGOProfiler); 2044514f5e3Sopenharmony_ci} 2054514f5e3Sopenharmony_ci 2064514f5e3Sopenharmony_civoid EcmaVM::ResetPGOProfiler() 2074514f5e3Sopenharmony_ci{ 2084514f5e3Sopenharmony_ci if (pgoProfiler_ != nullptr) { 2094514f5e3Sopenharmony_ci bool isEnablePGOProfiler = IsEnablePGOProfiler(); 2104514f5e3Sopenharmony_ci PGOProfilerManager::GetInstance()->Reset(pgoProfiler_, isEnablePGOProfiler); 2114514f5e3Sopenharmony_ci thread_->SetPGOProfilerEnable(isEnablePGOProfiler); 2124514f5e3Sopenharmony_ci thread_->CheckOrSwitchPGOStubs(); 2134514f5e3Sopenharmony_ci thread_->SetEnableForceIC(ecmascript::pgo::PGOProfilerManager::GetInstance()->IsEnableForceIC()); 2144514f5e3Sopenharmony_ci } 2154514f5e3Sopenharmony_ci} 2164514f5e3Sopenharmony_ci 2174514f5e3Sopenharmony_civoid EcmaVM::DisablePGOProfilerWithAOTFile(const std::string &aotFileName) 2184514f5e3Sopenharmony_ci{ 2194514f5e3Sopenharmony_ci if (AOTFileManager::AOTFileExist(aotFileName, AOTFileManager::FILE_EXTENSION_AN) || 2204514f5e3Sopenharmony_ci AOTFileManager::AOTFileExist(aotFileName, AOTFileManager::FILE_EXTENSION_AI)) { 2214514f5e3Sopenharmony_ci options_.SetEnablePGOProfiler(false); 2224514f5e3Sopenharmony_ci PGOProfilerManager::GetInstance()->SetDisablePGO(true); 2234514f5e3Sopenharmony_ci ResetPGOProfiler(); 2244514f5e3Sopenharmony_ci } 2254514f5e3Sopenharmony_ci} 2264514f5e3Sopenharmony_ci 2274514f5e3Sopenharmony_cibool EcmaVM::IsEnablePGOProfiler() const 2284514f5e3Sopenharmony_ci{ 2294514f5e3Sopenharmony_ci if (options_.IsWorker()) { 2304514f5e3Sopenharmony_ci return PGOProfilerManager::GetInstance()->IsEnable(); 2314514f5e3Sopenharmony_ci } 2324514f5e3Sopenharmony_ci return options_.GetEnableAsmInterpreter() && options_.IsEnablePGOProfiler(); 2334514f5e3Sopenharmony_ci} 2344514f5e3Sopenharmony_ci 2354514f5e3Sopenharmony_cibool EcmaVM::IsEnableElementsKind() const 2364514f5e3Sopenharmony_ci{ 2374514f5e3Sopenharmony_ci return options_.GetEnableAsmInterpreter() && options_.IsEnableElementsKind(); 2384514f5e3Sopenharmony_ci} 2394514f5e3Sopenharmony_ci 2404514f5e3Sopenharmony_cibool EcmaVM::IsEnableFastJit() const 2414514f5e3Sopenharmony_ci{ 2424514f5e3Sopenharmony_ci return GetJit()->IsEnableFastJit(); 2434514f5e3Sopenharmony_ci} 2444514f5e3Sopenharmony_ci 2454514f5e3Sopenharmony_cibool EcmaVM::IsEnableBaselineJit() const 2464514f5e3Sopenharmony_ci{ 2474514f5e3Sopenharmony_ci return GetJit()->IsEnableBaselineJit(); 2484514f5e3Sopenharmony_ci} 2494514f5e3Sopenharmony_ci 2504514f5e3Sopenharmony_ciuint32_t EcmaVM::GetTid() const 2514514f5e3Sopenharmony_ci{ 2524514f5e3Sopenharmony_ci return thread_->GetThreadId(); 2534514f5e3Sopenharmony_ci} 2544514f5e3Sopenharmony_ci 2554514f5e3Sopenharmony_ciJit *EcmaVM::GetJit() const 2564514f5e3Sopenharmony_ci{ 2574514f5e3Sopenharmony_ci return Jit::GetInstance(); 2584514f5e3Sopenharmony_ci} 2594514f5e3Sopenharmony_ci 2604514f5e3Sopenharmony_cibool EcmaVM::Initialize() 2614514f5e3Sopenharmony_ci{ 2624514f5e3Sopenharmony_ci ECMA_BYTRACE_NAME(HITRACE_TAG_ARK, "EcmaVM::Initialize"); 2634514f5e3Sopenharmony_ci stringTable_ = Runtime::GetInstance()->GetEcmaStringTable(); 2644514f5e3Sopenharmony_ci InitializePGOProfiler(); 2654514f5e3Sopenharmony_ci Taskpool::GetCurrentTaskpool()->Initialize(); 2664514f5e3Sopenharmony_ci#ifndef PANDA_TARGET_WINDOWS 2674514f5e3Sopenharmony_ci RuntimeStubs::Initialize(thread_); 2684514f5e3Sopenharmony_ci#endif 2694514f5e3Sopenharmony_ci heap_ = new Heap(this); 2704514f5e3Sopenharmony_ci heap_->Initialize(); 2714514f5e3Sopenharmony_ci gcStats_ = chunk_.New<GCStats>(heap_, options_.GetLongPauseTime()); 2724514f5e3Sopenharmony_ci gcKeyStats_ = chunk_.New<GCKeyStats>(heap_, gcStats_); 2734514f5e3Sopenharmony_ci factory_ = chunk_.New<ObjectFactory>(thread_, heap_, SharedHeap::GetInstance()); 2744514f5e3Sopenharmony_ci if (UNLIKELY(factory_ == nullptr)) { 2754514f5e3Sopenharmony_ci LOG_FULL(FATAL) << "alloc factory_ failed"; 2764514f5e3Sopenharmony_ci UNREACHABLE(); 2774514f5e3Sopenharmony_ci } 2784514f5e3Sopenharmony_ci debuggerManager_ = new tooling::JsDebuggerManager(this); 2794514f5e3Sopenharmony_ci aotFileManager_ = new AOTFileManager(this); 2804514f5e3Sopenharmony_ci auto context = new EcmaContext(thread_); 2814514f5e3Sopenharmony_ci thread_->PushContext(context); 2824514f5e3Sopenharmony_ci [[maybe_unused]] EcmaHandleScope scope(thread_); 2834514f5e3Sopenharmony_ci thread_->SetReadyForGCIterating(true); 2844514f5e3Sopenharmony_ci thread_->SetSharedMarkStatus(DaemonThread::GetInstance()->GetSharedMarkStatus()); 2854514f5e3Sopenharmony_ci snapshotEnv_ = new SnapshotEnv(this); 2864514f5e3Sopenharmony_ci context->Initialize(); 2874514f5e3Sopenharmony_ci snapshotEnv_->AddGlobalConstToMap(); 2884514f5e3Sopenharmony_ci thread_->SetGlueGlobalEnv(reinterpret_cast<GlobalEnv *>(context->GetGlobalEnv().GetTaggedType())); 2894514f5e3Sopenharmony_ci thread_->SetGlobalObject(GetGlobalEnv()->GetGlobalObject()); 2904514f5e3Sopenharmony_ci thread_->SetCurrentEcmaContext(context); 2914514f5e3Sopenharmony_ci GenerateInternalNativeMethods(); 2924514f5e3Sopenharmony_ci quickFixManager_ = new QuickFixManager(); 2934514f5e3Sopenharmony_ci if (options_.GetEnableAsmInterpreter()) { 2944514f5e3Sopenharmony_ci thread_->GetCurrentEcmaContext()->LoadStubFile(); 2954514f5e3Sopenharmony_ci } 2964514f5e3Sopenharmony_ci if (options_.EnableEdenGC()) { 2974514f5e3Sopenharmony_ci heap_->EnableEdenGC(); 2984514f5e3Sopenharmony_ci } 2994514f5e3Sopenharmony_ci 3004514f5e3Sopenharmony_ci callTimer_ = new FunctionCallTimer(); 3014514f5e3Sopenharmony_ci strategy_ = new ThroughputJSObjectResizingStrategy(); 3024514f5e3Sopenharmony_ci if (IsEnableFastJit() || IsEnableBaselineJit()) { 3034514f5e3Sopenharmony_ci Jit::GetInstance()->ConfigJit(this); 3044514f5e3Sopenharmony_ci } 3054514f5e3Sopenharmony_ci initialized_ = true; 3064514f5e3Sopenharmony_ci return true; 3074514f5e3Sopenharmony_ci} 3084514f5e3Sopenharmony_ci 3094514f5e3Sopenharmony_ciEcmaVM::~EcmaVM() 3104514f5e3Sopenharmony_ci{ 3114514f5e3Sopenharmony_ci if (isJitCompileVM_) { 3124514f5e3Sopenharmony_ci if (factory_ != nullptr) { 3134514f5e3Sopenharmony_ci chunk_.Delete(factory_); 3144514f5e3Sopenharmony_ci factory_ = nullptr; 3154514f5e3Sopenharmony_ci } 3164514f5e3Sopenharmony_ci stringTable_ = nullptr; 3174514f5e3Sopenharmony_ci thread_ = nullptr; 3184514f5e3Sopenharmony_ci return; 3194514f5e3Sopenharmony_ci } 3204514f5e3Sopenharmony_ci#if ECMASCRIPT_ENABLE_THREAD_STATE_CHECK 3214514f5e3Sopenharmony_ci if (UNLIKELY(!thread_->IsInRunningStateOrProfiling())) { 3224514f5e3Sopenharmony_ci LOG_ECMA(FATAL) << "Destruct VM must be in jsthread running state"; 3234514f5e3Sopenharmony_ci UNREACHABLE(); 3244514f5e3Sopenharmony_ci } 3254514f5e3Sopenharmony_ci#endif 3264514f5e3Sopenharmony_ci initialized_ = false; 3274514f5e3Sopenharmony_ci#if defined(ECMASCRIPT_SUPPORT_CPUPROFILER) 3284514f5e3Sopenharmony_ci if (profiler_ != nullptr) { 3294514f5e3Sopenharmony_ci if (profiler_->GetOutToFile()) { 3304514f5e3Sopenharmony_ci DFXJSNApi::StopCpuProfilerForFile(this); 3314514f5e3Sopenharmony_ci } else { 3324514f5e3Sopenharmony_ci DFXJSNApi::StopCpuProfilerForInfo(this); 3334514f5e3Sopenharmony_ci } 3344514f5e3Sopenharmony_ci } 3354514f5e3Sopenharmony_ci if (profiler_ != nullptr) { 3364514f5e3Sopenharmony_ci delete profiler_; 3374514f5e3Sopenharmony_ci profiler_ = nullptr; 3384514f5e3Sopenharmony_ci } 3394514f5e3Sopenharmony_ci#endif 3404514f5e3Sopenharmony_ci#if defined(ECMASCRIPT_SUPPORT_HEAPPROFILER) 3414514f5e3Sopenharmony_ci DeleteHeapProfile(); 3424514f5e3Sopenharmony_ci#endif 3434514f5e3Sopenharmony_ci if (IsEnableFastJit() || IsEnableBaselineJit()) { 3444514f5e3Sopenharmony_ci GetJit()->ClearTaskWithVm(this); 3454514f5e3Sopenharmony_ci } 3464514f5e3Sopenharmony_ci // clear c_address: c++ pointer delete 3474514f5e3Sopenharmony_ci ClearBufferData(); 3484514f5e3Sopenharmony_ci heap_->WaitAllTasksFinished(); 3494514f5e3Sopenharmony_ci Taskpool::GetCurrentTaskpool()->Destroy(thread_->GetThreadId()); 3504514f5e3Sopenharmony_ci 3514514f5e3Sopenharmony_ci if (pgoProfiler_ != nullptr) { 3524514f5e3Sopenharmony_ci PGOProfilerManager::GetInstance()->Destroy(pgoProfiler_); 3534514f5e3Sopenharmony_ci pgoProfiler_ = nullptr; 3544514f5e3Sopenharmony_ci } 3554514f5e3Sopenharmony_ci 3564514f5e3Sopenharmony_ci#if ECMASCRIPT_ENABLE_FUNCTION_CALL_TIMER 3574514f5e3Sopenharmony_ci DumpCallTimeInfo(); 3584514f5e3Sopenharmony_ci#endif 3594514f5e3Sopenharmony_ci 3604514f5e3Sopenharmony_ci#if defined(ECMASCRIPT_SUPPORT_TRACING) 3614514f5e3Sopenharmony_ci if (tracing_) { 3624514f5e3Sopenharmony_ci DFXJSNApi::StopTracing(this); 3634514f5e3Sopenharmony_ci } 3644514f5e3Sopenharmony_ci#endif 3654514f5e3Sopenharmony_ci 3664514f5e3Sopenharmony_ci thread_->GetCurrentEcmaContext()->GetModuleManager()->NativeObjDestory(); 3674514f5e3Sopenharmony_ci 3684514f5e3Sopenharmony_ci if (!isBundlePack_) { 3694514f5e3Sopenharmony_ci std::shared_ptr<JSPandaFile> jsPandaFile = JSPandaFileManager::GetInstance()->FindJSPandaFile(assetPath_); 3704514f5e3Sopenharmony_ci if (jsPandaFile != nullptr) { 3714514f5e3Sopenharmony_ci jsPandaFile->DeleteParsedConstpoolVM(this); 3724514f5e3Sopenharmony_ci } 3734514f5e3Sopenharmony_ci } 3744514f5e3Sopenharmony_ci 3754514f5e3Sopenharmony_ci if (gcStats_ != nullptr) { 3764514f5e3Sopenharmony_ci if (options_.EnableGCStatsPrint()) { 3774514f5e3Sopenharmony_ci gcStats_->PrintStatisticResult(); 3784514f5e3Sopenharmony_ci } 3794514f5e3Sopenharmony_ci chunk_.Delete(gcStats_); 3804514f5e3Sopenharmony_ci gcStats_ = nullptr; 3814514f5e3Sopenharmony_ci } 3824514f5e3Sopenharmony_ci 3834514f5e3Sopenharmony_ci if (gcKeyStats_ != nullptr) { 3844514f5e3Sopenharmony_ci chunk_.Delete(gcKeyStats_); 3854514f5e3Sopenharmony_ci gcKeyStats_ = nullptr; 3864514f5e3Sopenharmony_ci } 3874514f5e3Sopenharmony_ci 3884514f5e3Sopenharmony_ci if (JsStackInfo::loader == aotFileManager_) { 3894514f5e3Sopenharmony_ci JsStackInfo::loader = nullptr; 3904514f5e3Sopenharmony_ci } 3914514f5e3Sopenharmony_ci 3924514f5e3Sopenharmony_ci if (heap_ != nullptr) { 3934514f5e3Sopenharmony_ci heap_->Destroy(); 3944514f5e3Sopenharmony_ci delete heap_; 3954514f5e3Sopenharmony_ci heap_ = nullptr; 3964514f5e3Sopenharmony_ci } 3974514f5e3Sopenharmony_ci 3984514f5e3Sopenharmony_ci SharedHeap *sHeap = SharedHeap::GetInstance(); 3994514f5e3Sopenharmony_ci const Heap *heap = Runtime::GetInstance()->GetMainThread()->GetEcmaVM()->GetHeap(); 4004514f5e3Sopenharmony_ci if (IsWorkerThread() && Runtime::SharedGCRequest()) { 4014514f5e3Sopenharmony_ci // destory workervm to release mem. 4024514f5e3Sopenharmony_ci thread_->SetReadyForGCIterating(false); 4034514f5e3Sopenharmony_ci if (sHeap->CheckCanTriggerConcurrentMarking(thread_)) { 4044514f5e3Sopenharmony_ci sHeap->TriggerConcurrentMarking<TriggerGCType::SHARED_GC, GCReason::WORKER_DESTRUCTION>(thread_); 4054514f5e3Sopenharmony_ci } else if (heap && !heap->InSensitiveStatus() && !sHeap->GetConcurrentMarker()->IsEnabled()) { 4064514f5e3Sopenharmony_ci sHeap->CollectGarbage<TriggerGCType::SHARED_GC, GCReason::WORKER_DESTRUCTION>(thread_); 4074514f5e3Sopenharmony_ci } 4084514f5e3Sopenharmony_ci } 4094514f5e3Sopenharmony_ci 4104514f5e3Sopenharmony_ci if (debuggerManager_ != nullptr) { 4114514f5e3Sopenharmony_ci delete debuggerManager_; 4124514f5e3Sopenharmony_ci debuggerManager_ = nullptr; 4134514f5e3Sopenharmony_ci } 4144514f5e3Sopenharmony_ci 4154514f5e3Sopenharmony_ci if (aotFileManager_ != nullptr) { 4164514f5e3Sopenharmony_ci delete aotFileManager_; 4174514f5e3Sopenharmony_ci aotFileManager_ = nullptr; 4184514f5e3Sopenharmony_ci } 4194514f5e3Sopenharmony_ci 4204514f5e3Sopenharmony_ci if (factory_ != nullptr) { 4214514f5e3Sopenharmony_ci chunk_.Delete(factory_); 4224514f5e3Sopenharmony_ci factory_ = nullptr; 4234514f5e3Sopenharmony_ci } 4244514f5e3Sopenharmony_ci 4254514f5e3Sopenharmony_ci if (stringTable_ != nullptr) { 4264514f5e3Sopenharmony_ci stringTable_ = nullptr; 4274514f5e3Sopenharmony_ci } 4284514f5e3Sopenharmony_ci 4294514f5e3Sopenharmony_ci if (quickFixManager_ != nullptr) { 4304514f5e3Sopenharmony_ci delete quickFixManager_; 4314514f5e3Sopenharmony_ci quickFixManager_ = nullptr; 4324514f5e3Sopenharmony_ci } 4334514f5e3Sopenharmony_ci 4344514f5e3Sopenharmony_ci if (snapshotEnv_ != nullptr) { 4354514f5e3Sopenharmony_ci snapshotEnv_->ClearEnvMap(); 4364514f5e3Sopenharmony_ci delete snapshotEnv_; 4374514f5e3Sopenharmony_ci snapshotEnv_ = nullptr; 4384514f5e3Sopenharmony_ci } 4394514f5e3Sopenharmony_ci 4404514f5e3Sopenharmony_ci if (callTimer_ != nullptr) { 4414514f5e3Sopenharmony_ci delete callTimer_; 4424514f5e3Sopenharmony_ci callTimer_ = nullptr; 4434514f5e3Sopenharmony_ci } 4444514f5e3Sopenharmony_ci 4454514f5e3Sopenharmony_ci if (strategy_ != nullptr) { 4464514f5e3Sopenharmony_ci delete strategy_; 4474514f5e3Sopenharmony_ci strategy_ = nullptr; 4484514f5e3Sopenharmony_ci } 4494514f5e3Sopenharmony_ci 4504514f5e3Sopenharmony_ci if (thread_ != nullptr) { 4514514f5e3Sopenharmony_ci delete thread_; 4524514f5e3Sopenharmony_ci thread_ = nullptr; 4534514f5e3Sopenharmony_ci } 4544514f5e3Sopenharmony_ci} 4554514f5e3Sopenharmony_ci 4564514f5e3Sopenharmony_ciJSHandle<GlobalEnv> EcmaVM::GetGlobalEnv() const 4574514f5e3Sopenharmony_ci{ 4584514f5e3Sopenharmony_ci return thread_->GetCurrentEcmaContext()->GetGlobalEnv(); 4594514f5e3Sopenharmony_ci} 4604514f5e3Sopenharmony_ci 4614514f5e3Sopenharmony_civoid EcmaVM::CheckThread() const 4624514f5e3Sopenharmony_ci{ 4634514f5e3Sopenharmony_ci // Exclude GC thread 4644514f5e3Sopenharmony_ci if (thread_ == nullptr) { 4654514f5e3Sopenharmony_ci LOG_FULL(FATAL) << "Fatal: ecma_vm has been destructed! vm address is: " << this; 4664514f5e3Sopenharmony_ci UNREACHABLE(); 4674514f5e3Sopenharmony_ci } 4684514f5e3Sopenharmony_ci if (!Taskpool::GetCurrentTaskpool()->IsDaemonThreadOrInThreadPool(std::this_thread::get_id()) && 4694514f5e3Sopenharmony_ci thread_->GetThreadId() != JSThread::GetCurrentThreadId() && !thread_->IsCrossThreadExecutionEnable()) { 4704514f5e3Sopenharmony_ci LOG_FULL(FATAL) << "Fatal: ecma_vm cannot run in multi-thread!" 4714514f5e3Sopenharmony_ci << " thread:" << thread_->GetThreadId() 4724514f5e3Sopenharmony_ci << " currentThread:" << JSThread::GetCurrentThreadId(); 4734514f5e3Sopenharmony_ci UNREACHABLE(); 4744514f5e3Sopenharmony_ci } 4754514f5e3Sopenharmony_ci} 4764514f5e3Sopenharmony_ci 4774514f5e3Sopenharmony_ciJSThread *EcmaVM::GetAndFastCheckJSThread() const 4784514f5e3Sopenharmony_ci{ 4794514f5e3Sopenharmony_ci if (thread_ == nullptr) { 4804514f5e3Sopenharmony_ci LOG_FULL(FATAL) << "Fatal: ecma_vm has been destructed! vm address is: " << this; 4814514f5e3Sopenharmony_ci } 4824514f5e3Sopenharmony_ci if (thread_->GetThreadId() != JSThread::GetCurrentThreadId() && !thread_->IsCrossThreadExecutionEnable()) { 4834514f5e3Sopenharmony_ci LOG_FULL(FATAL) << "Fatal: ecma_vm cannot run in multi-thread!" 4844514f5e3Sopenharmony_ci << " thread:" << thread_->GetThreadId() 4854514f5e3Sopenharmony_ci << " currentThread:" << JSThread::GetCurrentThreadId(); 4864514f5e3Sopenharmony_ci } 4874514f5e3Sopenharmony_ci return thread_; 4884514f5e3Sopenharmony_ci} 4894514f5e3Sopenharmony_ci 4904514f5e3Sopenharmony_cibool EcmaVM::CheckSingleThread() const 4914514f5e3Sopenharmony_ci{ 4924514f5e3Sopenharmony_ci if (thread_ == nullptr) { 4934514f5e3Sopenharmony_ci LOG_FULL(FATAL) << "Fatal: ecma_vm has been destructed! vm address is: " << this; 4944514f5e3Sopenharmony_ci return false; 4954514f5e3Sopenharmony_ci } 4964514f5e3Sopenharmony_ci if (thread_->GetThreadId() != JSThread::GetCurrentThreadId()) { 4974514f5e3Sopenharmony_ci LOG_FULL(FATAL) << "Fatal: ecma_vm cannot run in multi-thread!" 4984514f5e3Sopenharmony_ci << " thread:" << thread_->GetThreadId() 4994514f5e3Sopenharmony_ci << " currentThread:" << JSThread::GetCurrentThreadId(); 5004514f5e3Sopenharmony_ci return false; 5014514f5e3Sopenharmony_ci } 5024514f5e3Sopenharmony_ci return true; 5034514f5e3Sopenharmony_ci} 5044514f5e3Sopenharmony_ci 5054514f5e3Sopenharmony_ciJSTaggedValue EcmaVM::FastCallAot(size_t actualNumArgs, JSTaggedType *args, const JSTaggedType *prevFp) 5064514f5e3Sopenharmony_ci{ 5074514f5e3Sopenharmony_ci INTERPRETER_TRACE(thread_, ExecuteAot); 5084514f5e3Sopenharmony_ci ASSERT(thread_->IsInManagedState()); 5094514f5e3Sopenharmony_ci auto entry = thread_->GetRTInterface(kungfu::RuntimeStubCSigns::ID_OptimizedFastCallEntry); 5104514f5e3Sopenharmony_ci // entry of aot 5114514f5e3Sopenharmony_ci auto res = reinterpret_cast<FastCallAotEntryType>(entry)(thread_->GetGlueAddr(), 5124514f5e3Sopenharmony_ci actualNumArgs, 5134514f5e3Sopenharmony_ci args, 5144514f5e3Sopenharmony_ci reinterpret_cast<uintptr_t>(prevFp)); 5154514f5e3Sopenharmony_ci return res; 5164514f5e3Sopenharmony_ci} 5174514f5e3Sopenharmony_ci 5184514f5e3Sopenharmony_civoid EcmaVM::CheckStartCpuProfiler() 5194514f5e3Sopenharmony_ci{ 5204514f5e3Sopenharmony_ci#if defined(ECMASCRIPT_SUPPORT_CPUPROFILER) 5214514f5e3Sopenharmony_ci if (options_.EnableCpuProfilerColdStartMainThread() && options_.GetArkBundleName().compare(bundleName_) == 0 && 5224514f5e3Sopenharmony_ci !options_.IsWorker() && profiler_ == nullptr) { 5234514f5e3Sopenharmony_ci std::string fileName = options_.GetArkBundleName() + ".cpuprofile"; 5244514f5e3Sopenharmony_ci if (!builtins::BuiltinsArkTools::CreateFile(fileName)) { 5254514f5e3Sopenharmony_ci LOG_ECMA(ERROR) << "createFile failed " << fileName; 5264514f5e3Sopenharmony_ci return; 5274514f5e3Sopenharmony_ci } else { 5284514f5e3Sopenharmony_ci DFXJSNApi::StartCpuProfilerForFile(this, fileName, CpuProfiler::INTERVAL_OF_INNER_START); 5294514f5e3Sopenharmony_ci return; 5304514f5e3Sopenharmony_ci } 5314514f5e3Sopenharmony_ci } 5324514f5e3Sopenharmony_ci 5334514f5e3Sopenharmony_ci if (options_.EnableCpuProfilerColdStartWorkerThread() && options_.GetArkBundleName().compare(bundleName_) == 0 && 5344514f5e3Sopenharmony_ci options_.IsWorker() && profiler_ == nullptr) { 5354514f5e3Sopenharmony_ci std::string fileName = options_.GetArkBundleName() + "_" 5364514f5e3Sopenharmony_ci + std::to_string(thread_->GetThreadId()) + ".cpuprofile"; 5374514f5e3Sopenharmony_ci if (!builtins::BuiltinsArkTools::CreateFile(fileName)) { 5384514f5e3Sopenharmony_ci LOG_ECMA(ERROR) << "createFile failed " << fileName; 5394514f5e3Sopenharmony_ci return; 5404514f5e3Sopenharmony_ci } else { 5414514f5e3Sopenharmony_ci DFXJSNApi::StartCpuProfilerForFile(this, fileName, CpuProfiler::INTERVAL_OF_INNER_START); 5424514f5e3Sopenharmony_ci return; 5434514f5e3Sopenharmony_ci } 5444514f5e3Sopenharmony_ci } 5454514f5e3Sopenharmony_ci#endif 5464514f5e3Sopenharmony_ci} 5474514f5e3Sopenharmony_ci 5484514f5e3Sopenharmony_ciJSHandle<JSTaggedValue> EcmaVM::GetAndClearEcmaUncaughtException() const 5494514f5e3Sopenharmony_ci{ 5504514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> exceptionHandle = GetEcmaUncaughtException(); 5514514f5e3Sopenharmony_ci thread_->ClearException(); // clear for ohos app 5524514f5e3Sopenharmony_ci return exceptionHandle; 5534514f5e3Sopenharmony_ci} 5544514f5e3Sopenharmony_ci 5554514f5e3Sopenharmony_ciJSHandle<JSTaggedValue> EcmaVM::GetEcmaUncaughtException() const 5564514f5e3Sopenharmony_ci{ 5574514f5e3Sopenharmony_ci if (!thread_->HasPendingException()) { 5584514f5e3Sopenharmony_ci return JSHandle<JSTaggedValue>(); 5594514f5e3Sopenharmony_ci } 5604514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> exceptionHandle(thread_, thread_->GetException()); 5614514f5e3Sopenharmony_ci return exceptionHandle; 5624514f5e3Sopenharmony_ci} 5634514f5e3Sopenharmony_ci 5644514f5e3Sopenharmony_civoid EcmaVM::PrintAOTSnapShotStats() 5654514f5e3Sopenharmony_ci{ 5664514f5e3Sopenharmony_ci static constexpr int nameRightAdjustment = 30; 5674514f5e3Sopenharmony_ci static constexpr int numberRightAdjustment = 30; 5684514f5e3Sopenharmony_ci LOG_ECMA(ERROR) << std::right << std::setw(nameRightAdjustment) << "AOT Snapshot Genre" 5694514f5e3Sopenharmony_ci << std::setw(numberRightAdjustment) << "Count"; 5704514f5e3Sopenharmony_ci LOG_ECMA(ERROR) << "=========================================================================="; 5714514f5e3Sopenharmony_ci for (const auto &iter: aotSnapShotStatsMap_) { 5724514f5e3Sopenharmony_ci LOG_ECMA(ERROR) << std::right << std::setw(nameRightAdjustment) << iter.first 5734514f5e3Sopenharmony_ci << std::setw(numberRightAdjustment) << iter.second; 5744514f5e3Sopenharmony_ci } 5754514f5e3Sopenharmony_ci LOG_ECMA(ERROR) << "=========================================================================="; 5764514f5e3Sopenharmony_ci aotSnapShotStatsMap_.clear(); 5774514f5e3Sopenharmony_ci} 5784514f5e3Sopenharmony_ci 5794514f5e3Sopenharmony_civoid EcmaVM::PrintJSErrorInfo(const JSHandle<JSTaggedValue> &exceptionInfo) const 5804514f5e3Sopenharmony_ci{ 5814514f5e3Sopenharmony_ci EcmaContext::PrintJSErrorInfo(thread_, exceptionInfo); 5824514f5e3Sopenharmony_ci} 5834514f5e3Sopenharmony_ci 5844514f5e3Sopenharmony_civoid EcmaVM::ProcessNativeDelete(const WeakRootVisitor& visitor) 5854514f5e3Sopenharmony_ci{ 5864514f5e3Sopenharmony_ci heap_->ProcessNativeDelete(visitor); 5874514f5e3Sopenharmony_ci} 5884514f5e3Sopenharmony_ci 5894514f5e3Sopenharmony_civoid EcmaVM::ProcessReferences(const WeakRootVisitor& visitor) 5904514f5e3Sopenharmony_ci{ 5914514f5e3Sopenharmony_ci heap_->ProcessReferences(visitor); 5924514f5e3Sopenharmony_ci GetPGOProfiler()->ProcessReferences(visitor); 5934514f5e3Sopenharmony_ci} 5944514f5e3Sopenharmony_ci 5954514f5e3Sopenharmony_civoid EcmaVM::PushToNativePointerList(JSNativePointer* pointer, Concurrent isConcurrent) 5964514f5e3Sopenharmony_ci{ 5974514f5e3Sopenharmony_ci heap_->PushToNativePointerList(pointer, isConcurrent == Concurrent::YES); 5984514f5e3Sopenharmony_ci} 5994514f5e3Sopenharmony_ci 6004514f5e3Sopenharmony_civoid EcmaVM::RemoveFromNativePointerList(JSNativePointer* pointer) 6014514f5e3Sopenharmony_ci{ 6024514f5e3Sopenharmony_ci heap_->RemoveFromNativePointerList(pointer); 6034514f5e3Sopenharmony_ci} 6044514f5e3Sopenharmony_ci 6054514f5e3Sopenharmony_civoid EcmaVM::PushToDeregisterModuleList(const CString &module) 6064514f5e3Sopenharmony_ci{ 6074514f5e3Sopenharmony_ci deregisterModuleList_.emplace_back(module); 6084514f5e3Sopenharmony_ci} 6094514f5e3Sopenharmony_ci 6104514f5e3Sopenharmony_civoid EcmaVM::RemoveFromDeregisterModuleList(CString module) 6114514f5e3Sopenharmony_ci{ 6124514f5e3Sopenharmony_ci auto iter = std::find(deregisterModuleList_.begin(), deregisterModuleList_.end(), module); 6134514f5e3Sopenharmony_ci if (iter != deregisterModuleList_.end()) { 6144514f5e3Sopenharmony_ci deregisterModuleList_.erase(iter); 6154514f5e3Sopenharmony_ci } 6164514f5e3Sopenharmony_ci} 6174514f5e3Sopenharmony_ci 6184514f5e3Sopenharmony_cibool EcmaVM::ContainInDeregisterModuleList(CString module) 6194514f5e3Sopenharmony_ci{ 6204514f5e3Sopenharmony_ci return (std::find(deregisterModuleList_.begin(), deregisterModuleList_.end(), module) 6214514f5e3Sopenharmony_ci != deregisterModuleList_.end()); 6224514f5e3Sopenharmony_ci} 6234514f5e3Sopenharmony_ci 6244514f5e3Sopenharmony_civoid EcmaVM::ClearBufferData() 6254514f5e3Sopenharmony_ci{ 6264514f5e3Sopenharmony_ci heap_->ClearNativePointerList(); 6274514f5e3Sopenharmony_ci thread_->GetCurrentEcmaContext()->ClearBufferData(); 6284514f5e3Sopenharmony_ci internalNativeMethods_.clear(); 6294514f5e3Sopenharmony_ci workerList_.clear(); 6304514f5e3Sopenharmony_ci deregisterModuleList_.clear(); 6314514f5e3Sopenharmony_ci} 6324514f5e3Sopenharmony_ci 6334514f5e3Sopenharmony_civoid EcmaVM::CollectGarbage(TriggerGCType gcType, panda::ecmascript::GCReason reason) const 6344514f5e3Sopenharmony_ci{ 6354514f5e3Sopenharmony_ci heap_->CollectGarbage(gcType, reason); 6364514f5e3Sopenharmony_ci} 6374514f5e3Sopenharmony_ci 6384514f5e3Sopenharmony_civoid EcmaVM::Iterate(const RootVisitor &v, const RootRangeVisitor &rv, VMRootVisitType type) 6394514f5e3Sopenharmony_ci{ 6404514f5e3Sopenharmony_ci rv(Root::ROOT_VM, ObjectSlot(ToUintPtr(&internalNativeMethods_.front())), 6414514f5e3Sopenharmony_ci ObjectSlot(ToUintPtr(&internalNativeMethods_.back()) + JSTaggedValue::TaggedTypeSize())); 6424514f5e3Sopenharmony_ci if (!WIN_OR_MAC_OR_IOS_PLATFORM && snapshotEnv_!= nullptr) { 6434514f5e3Sopenharmony_ci snapshotEnv_->Iterate(v, type); 6444514f5e3Sopenharmony_ci } 6454514f5e3Sopenharmony_ci if (pgoProfiler_ != nullptr) { 6464514f5e3Sopenharmony_ci pgoProfiler_->Iterate(v); 6474514f5e3Sopenharmony_ci } 6484514f5e3Sopenharmony_ci if (aotFileManager_) { 6494514f5e3Sopenharmony_ci aotFileManager_->Iterate(v); 6504514f5e3Sopenharmony_ci } 6514514f5e3Sopenharmony_ci} 6524514f5e3Sopenharmony_ci 6534514f5e3Sopenharmony_ci#if defined(ECMASCRIPT_SUPPORT_HEAPPROFILER) 6544514f5e3Sopenharmony_civoid EcmaVM::DeleteHeapProfile() 6554514f5e3Sopenharmony_ci{ 6564514f5e3Sopenharmony_ci if (heapProfile_ == nullptr) { 6574514f5e3Sopenharmony_ci return; 6584514f5e3Sopenharmony_ci } 6594514f5e3Sopenharmony_ci delete heapProfile_; 6604514f5e3Sopenharmony_ci heapProfile_ = nullptr; 6614514f5e3Sopenharmony_ci} 6624514f5e3Sopenharmony_ci 6634514f5e3Sopenharmony_ciHeapProfilerInterface *EcmaVM::GetHeapProfile() 6644514f5e3Sopenharmony_ci{ 6654514f5e3Sopenharmony_ci if (heapProfile_ != nullptr) { 6664514f5e3Sopenharmony_ci return heapProfile_; 6674514f5e3Sopenharmony_ci } 6684514f5e3Sopenharmony_ci return nullptr; 6694514f5e3Sopenharmony_ci} 6704514f5e3Sopenharmony_ci 6714514f5e3Sopenharmony_ciHeapProfilerInterface *EcmaVM::GetOrNewHeapProfile() 6724514f5e3Sopenharmony_ci{ 6734514f5e3Sopenharmony_ci if (heapProfile_ != nullptr) { 6744514f5e3Sopenharmony_ci return heapProfile_; 6754514f5e3Sopenharmony_ci } 6764514f5e3Sopenharmony_ci heapProfile_ = new HeapProfiler(this); 6774514f5e3Sopenharmony_ci ASSERT(heapProfile_ != nullptr); 6784514f5e3Sopenharmony_ci return heapProfile_; 6794514f5e3Sopenharmony_ci} 6804514f5e3Sopenharmony_ci 6814514f5e3Sopenharmony_civoid EcmaVM::StartHeapTracking() 6824514f5e3Sopenharmony_ci{ 6834514f5e3Sopenharmony_ci heap_->StartHeapTracking(); 6844514f5e3Sopenharmony_ci} 6854514f5e3Sopenharmony_ci 6864514f5e3Sopenharmony_civoid EcmaVM::StopHeapTracking() 6874514f5e3Sopenharmony_ci{ 6884514f5e3Sopenharmony_ci heap_->StopHeapTracking(); 6894514f5e3Sopenharmony_ci} 6904514f5e3Sopenharmony_ci#endif 6914514f5e3Sopenharmony_ci 6924514f5e3Sopenharmony_ci// NOLINTNEXTLINE(modernize-avoid-c-arrays) 6934514f5e3Sopenharmony_civoid *EcmaVM::InternalMethodTable[] = { 6944514f5e3Sopenharmony_ci reinterpret_cast<void *>(builtins::BuiltinsGlobal::CallJsBoundFunction), 6954514f5e3Sopenharmony_ci reinterpret_cast<void *>(builtins::BuiltinsGlobal::CallJsProxy), 6964514f5e3Sopenharmony_ci reinterpret_cast<void *>(builtins::BuiltinsObject::CreateDataPropertyOnObjectFunctions), 6974514f5e3Sopenharmony_ci#ifdef ARK_SUPPORT_INTL 6984514f5e3Sopenharmony_ci reinterpret_cast<void *>(builtins::BuiltinsCollator::AnonymousCollator), 6994514f5e3Sopenharmony_ci reinterpret_cast<void *>(builtins::BuiltinsDateTimeFormat::AnonymousDateTimeFormat), 7004514f5e3Sopenharmony_ci reinterpret_cast<void *>(builtins::BuiltinsNumberFormat::NumberFormatInternalFormatNumber), 7014514f5e3Sopenharmony_ci#endif 7024514f5e3Sopenharmony_ci reinterpret_cast<void *>(builtins::BuiltinsProxy::InvalidateProxyFunction), 7034514f5e3Sopenharmony_ci reinterpret_cast<void *>(builtins::BuiltinsPromiseHandler::AsyncAwaitFulfilled), 7044514f5e3Sopenharmony_ci reinterpret_cast<void *>(builtins::BuiltinsPromiseHandler::AsyncAwaitRejected), 7054514f5e3Sopenharmony_ci reinterpret_cast<void *>(builtins::BuiltinsPromiseHandler::ResolveElementFunction), 7064514f5e3Sopenharmony_ci reinterpret_cast<void *>(builtins::BuiltinsPromiseHandler::Resolve), 7074514f5e3Sopenharmony_ci reinterpret_cast<void *>(builtins::BuiltinsPromiseHandler::Reject), 7084514f5e3Sopenharmony_ci reinterpret_cast<void *>(builtins::BuiltinsPromiseHandler::Executor), 7094514f5e3Sopenharmony_ci reinterpret_cast<void *>(builtins::BuiltinsPromiseHandler::AnyRejectElementFunction), 7104514f5e3Sopenharmony_ci reinterpret_cast<void *>(builtins::BuiltinsPromiseHandler::AllSettledResolveElementFunction), 7114514f5e3Sopenharmony_ci reinterpret_cast<void *>(builtins::BuiltinsPromiseHandler::AllSettledRejectElementFunction), 7124514f5e3Sopenharmony_ci reinterpret_cast<void *>(builtins::BuiltinsPromiseHandler::ThenFinally), 7134514f5e3Sopenharmony_ci reinterpret_cast<void *>(builtins::BuiltinsPromiseHandler::CatchFinally), 7144514f5e3Sopenharmony_ci reinterpret_cast<void *>(builtins::BuiltinsPromiseHandler::valueThunkFunction), 7154514f5e3Sopenharmony_ci reinterpret_cast<void *>(builtins::BuiltinsPromiseHandler::throwerFunction), 7164514f5e3Sopenharmony_ci reinterpret_cast<void *>(JSAsyncGeneratorObject::ProcessorFulfilledFunc), 7174514f5e3Sopenharmony_ci reinterpret_cast<void *>(JSAsyncGeneratorObject::ProcessorRejectedFunc), 7184514f5e3Sopenharmony_ci reinterpret_cast<void *>(JSAsyncFromSyncIterator::AsyncFromSyncIterUnwarpFunction), 7194514f5e3Sopenharmony_ci reinterpret_cast<void *>(SourceTextModule::AsyncModuleFulfilledFunc), 7204514f5e3Sopenharmony_ci reinterpret_cast<void *>(SourceTextModule::AsyncModuleRejectedFunc) 7214514f5e3Sopenharmony_ci}; 7224514f5e3Sopenharmony_ci 7234514f5e3Sopenharmony_civoid EcmaVM::GenerateInternalNativeMethods() 7244514f5e3Sopenharmony_ci{ 7254514f5e3Sopenharmony_ci size_t length = static_cast<size_t>(MethodIndex::METHOD_END); 7264514f5e3Sopenharmony_ci constexpr uint32_t numArgs = 2; // function object and this 7274514f5e3Sopenharmony_ci for (size_t i = 0; i < length; i++) { 7284514f5e3Sopenharmony_ci auto method = factory_->NewSMethod(nullptr, MemSpaceType::SHARED_NON_MOVABLE); 7294514f5e3Sopenharmony_ci method->SetNativePointer(InternalMethodTable[i]); 7304514f5e3Sopenharmony_ci method->SetNativeBit(true); 7314514f5e3Sopenharmony_ci method->SetNumArgsWithCallField(numArgs); 7324514f5e3Sopenharmony_ci method->SetFunctionKind(FunctionKind::NORMAL_FUNCTION); 7334514f5e3Sopenharmony_ci internalNativeMethods_.emplace_back(method.GetTaggedValue()); 7344514f5e3Sopenharmony_ci } 7354514f5e3Sopenharmony_ci // cache to global constants shared because context may change 7364514f5e3Sopenharmony_ci CacheToGlobalConstants(GetMethodByIndex(MethodIndex::BUILTINS_GLOBAL_CALL_JS_BOUND_FUNCTION), 7374514f5e3Sopenharmony_ci ConstantIndex::BOUND_FUNCTION_METHOD_INDEX); 7384514f5e3Sopenharmony_ci CacheToGlobalConstants(GetMethodByIndex(MethodIndex::BUILTINS_GLOBAL_CALL_JS_PROXY), 7394514f5e3Sopenharmony_ci ConstantIndex::PROXY_METHOD_INDEX); 7404514f5e3Sopenharmony_ci} 7414514f5e3Sopenharmony_ci 7424514f5e3Sopenharmony_civoid EcmaVM::CacheToGlobalConstants(JSTaggedValue value, ConstantIndex idx) 7434514f5e3Sopenharmony_ci{ 7444514f5e3Sopenharmony_ci auto thread = GetJSThread(); 7454514f5e3Sopenharmony_ci auto context = thread->GetCurrentEcmaContext(); 7464514f5e3Sopenharmony_ci auto constants = const_cast<GlobalEnvConstants *>(context->GlobalConstants()); 7474514f5e3Sopenharmony_ci constants->SetConstant(idx, value); 7484514f5e3Sopenharmony_ci} 7494514f5e3Sopenharmony_ci 7504514f5e3Sopenharmony_ciJSTaggedValue EcmaVM::GetMethodByIndex(MethodIndex idx) 7514514f5e3Sopenharmony_ci{ 7524514f5e3Sopenharmony_ci auto index = static_cast<uint8_t>(idx); 7534514f5e3Sopenharmony_ci ASSERT(index < internalNativeMethods_.size()); 7544514f5e3Sopenharmony_ci return internalNativeMethods_[index]; 7554514f5e3Sopenharmony_ci} 7564514f5e3Sopenharmony_ci 7574514f5e3Sopenharmony_civoid EcmaVM::TriggerConcurrentCallback(JSTaggedValue result, JSTaggedValue hint) 7584514f5e3Sopenharmony_ci{ 7594514f5e3Sopenharmony_ci if (concurrentCallback_ == nullptr) { 7604514f5e3Sopenharmony_ci LOG_ECMA(DEBUG) << "Only trigger concurrent callback in taskpool thread"; 7614514f5e3Sopenharmony_ci return; 7624514f5e3Sopenharmony_ci } 7634514f5e3Sopenharmony_ci 7644514f5e3Sopenharmony_ci bool success = true; 7654514f5e3Sopenharmony_ci if (result.IsJSPromise()) { 7664514f5e3Sopenharmony_ci // Async concurrent will return Promise 7674514f5e3Sopenharmony_ci auto promise = JSPromise::Cast(result.GetTaggedObject()); 7684514f5e3Sopenharmony_ci auto status = promise->GetPromiseState(); 7694514f5e3Sopenharmony_ci if (status == PromiseState::PENDING) { 7704514f5e3Sopenharmony_ci result = JSHandle<JSTaggedValue>::Cast(factory_->GetJSError( 7714514f5e3Sopenharmony_ci ErrorType::ERROR, "Can't return Promise in pending state", StackCheck::NO)).GetTaggedValue(); 7724514f5e3Sopenharmony_ci } else { 7734514f5e3Sopenharmony_ci result = promise->GetPromiseResult(); 7744514f5e3Sopenharmony_ci } 7754514f5e3Sopenharmony_ci 7764514f5e3Sopenharmony_ci if (status != PromiseState::FULFILLED) { 7774514f5e3Sopenharmony_ci success = false; 7784514f5e3Sopenharmony_ci } 7794514f5e3Sopenharmony_ci } 7804514f5e3Sopenharmony_ci 7814514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> functionValue(thread_, hint); 7824514f5e3Sopenharmony_ci if (!functionValue->IsJSFunction()) { 7834514f5e3Sopenharmony_ci LOG_ECMA(ERROR) << "TriggerConcurrentCallback hint is not function"; 7844514f5e3Sopenharmony_ci return; 7854514f5e3Sopenharmony_ci } 7864514f5e3Sopenharmony_ci JSHandle<JSFunction> functionInfo(functionValue); 7874514f5e3Sopenharmony_ci if (!functionInfo->GetTaskConcurrentFuncFlag()) { 7884514f5e3Sopenharmony_ci LOG_ECMA(INFO) << "Function is not Concurrent Function"; 7894514f5e3Sopenharmony_ci return; 7904514f5e3Sopenharmony_ci } 7914514f5e3Sopenharmony_ci 7924514f5e3Sopenharmony_ci void *taskInfo = reinterpret_cast<void*>(thread_->GetTaskInfo()); 7934514f5e3Sopenharmony_ci // clear the taskInfo when return, which can prevent the callback to get it 7944514f5e3Sopenharmony_ci thread_->SetTaskInfo(reinterpret_cast<uintptr_t>(nullptr)); 7954514f5e3Sopenharmony_ci auto localResultRef = JSNApiHelper::ToLocal<JSValueRef>(JSHandle<JSTaggedValue>(thread_, result)); 7964514f5e3Sopenharmony_ci ThreadNativeScope nativeScope(thread_); 7974514f5e3Sopenharmony_ci concurrentCallback_(localResultRef, success, taskInfo, concurrentData_); 7984514f5e3Sopenharmony_ci} 7994514f5e3Sopenharmony_ci 8004514f5e3Sopenharmony_civoid EcmaVM::DumpCallTimeInfo() 8014514f5e3Sopenharmony_ci{ 8024514f5e3Sopenharmony_ci if (callTimer_ != nullptr) { 8034514f5e3Sopenharmony_ci callTimer_->PrintAllStats(); 8044514f5e3Sopenharmony_ci } 8054514f5e3Sopenharmony_ci} 8064514f5e3Sopenharmony_ci 8074514f5e3Sopenharmony_civoid EcmaVM::WorkersetInfo(EcmaVM *workerVm) 8084514f5e3Sopenharmony_ci{ 8094514f5e3Sopenharmony_ci LockHolder lock(mutex_); 8104514f5e3Sopenharmony_ci auto thread = workerVm->GetJSThread(); 8114514f5e3Sopenharmony_ci if (thread != nullptr) { 8124514f5e3Sopenharmony_ci auto tid = thread->GetThreadId(); 8134514f5e3Sopenharmony_ci if (tid != 0) { 8144514f5e3Sopenharmony_ci workerList_.emplace(tid, workerVm); 8154514f5e3Sopenharmony_ci } 8164514f5e3Sopenharmony_ci } 8174514f5e3Sopenharmony_ci} 8184514f5e3Sopenharmony_ci 8194514f5e3Sopenharmony_ciEcmaVM *EcmaVM::GetWorkerVm(uint32_t tid) 8204514f5e3Sopenharmony_ci{ 8214514f5e3Sopenharmony_ci LockHolder lock(mutex_); 8224514f5e3Sopenharmony_ci EcmaVM *workerVm = nullptr; 8234514f5e3Sopenharmony_ci if (!workerList_.empty()) { 8244514f5e3Sopenharmony_ci auto iter = workerList_.find(tid); 8254514f5e3Sopenharmony_ci if (iter != workerList_.end()) { 8264514f5e3Sopenharmony_ci workerVm = iter->second; 8274514f5e3Sopenharmony_ci } 8284514f5e3Sopenharmony_ci } 8294514f5e3Sopenharmony_ci return workerVm; 8304514f5e3Sopenharmony_ci} 8314514f5e3Sopenharmony_ci 8324514f5e3Sopenharmony_cibool EcmaVM::DeleteWorker(EcmaVM *workerVm) 8334514f5e3Sopenharmony_ci{ 8344514f5e3Sopenharmony_ci LockHolder lock(mutex_); 8354514f5e3Sopenharmony_ci auto thread = workerVm->GetJSThread(); 8364514f5e3Sopenharmony_ci if (thread != nullptr) { 8374514f5e3Sopenharmony_ci auto tid = thread->GetThreadId(); 8384514f5e3Sopenharmony_ci if (tid == 0) { 8394514f5e3Sopenharmony_ci return false; 8404514f5e3Sopenharmony_ci } 8414514f5e3Sopenharmony_ci auto iter = workerList_.find(tid); 8424514f5e3Sopenharmony_ci if (iter != workerList_.end()) { 8434514f5e3Sopenharmony_ci workerList_.erase(iter); 8444514f5e3Sopenharmony_ci return true; 8454514f5e3Sopenharmony_ci } 8464514f5e3Sopenharmony_ci return false; 8474514f5e3Sopenharmony_ci } 8484514f5e3Sopenharmony_ci return false; 8494514f5e3Sopenharmony_ci} 8504514f5e3Sopenharmony_ci 8514514f5e3Sopenharmony_cibool EcmaVM::SuspendWorkerVm(uint32_t tid) 8524514f5e3Sopenharmony_ci{ 8534514f5e3Sopenharmony_ci LockHolder lock(mutex_); 8544514f5e3Sopenharmony_ci if (!workerList_.empty()) { 8554514f5e3Sopenharmony_ci auto iter = workerList_.find(tid); 8564514f5e3Sopenharmony_ci if (iter != workerList_.end()) { 8574514f5e3Sopenharmony_ci return DFXJSNApi::SuspendVM(iter->second); 8584514f5e3Sopenharmony_ci } 8594514f5e3Sopenharmony_ci } 8604514f5e3Sopenharmony_ci return false; 8614514f5e3Sopenharmony_ci} 8624514f5e3Sopenharmony_ci 8634514f5e3Sopenharmony_civoid EcmaVM::ResumeWorkerVm(uint32_t tid) 8644514f5e3Sopenharmony_ci{ 8654514f5e3Sopenharmony_ci LockHolder lock(mutex_); 8664514f5e3Sopenharmony_ci if (!workerList_.empty()) { 8674514f5e3Sopenharmony_ci auto iter = workerList_.find(tid); 8684514f5e3Sopenharmony_ci if (iter != workerList_.end()) { 8694514f5e3Sopenharmony_ci DFXJSNApi::ResumeVM(iter->second); 8704514f5e3Sopenharmony_ci } 8714514f5e3Sopenharmony_ci } 8724514f5e3Sopenharmony_ci} 8734514f5e3Sopenharmony_ci 8744514f5e3Sopenharmony_ci/* This moduleName is a readOnly variable for napi, represent which abc is running in current vm. 8754514f5e3Sopenharmony_ci* Get Current recordName: bundleName/moduleName/ets/xxx/xxx 8764514f5e3Sopenharmony_ci* pkg_modules@xxx/xxx/xxx 8774514f5e3Sopenharmony_ci* Get Current fileName: /data/storage/el1/bundle/moduleName/ets/modules.abc 8784514f5e3Sopenharmony_ci* output: moduleName: moduleName 8794514f5e3Sopenharmony_ci* if needRecordName then fileName is: moduleName/ets/modules.abc 8804514f5e3Sopenharmony_ci*/ 8814514f5e3Sopenharmony_cistd::pair<std::string, std::string> EcmaVM::GetCurrentModuleInfo(bool needRecordName) 8824514f5e3Sopenharmony_ci{ 8834514f5e3Sopenharmony_ci std::pair<CString, CString> moduleInfo = EcmaInterpreter::GetCurrentEntryPoint(thread_); 8844514f5e3Sopenharmony_ci RETURN_VALUE_IF_ABRUPT_COMPLETION(thread_, std::make_pair("", "")); 8854514f5e3Sopenharmony_ci CString recordName = moduleInfo.first; 8864514f5e3Sopenharmony_ci CString fileName = moduleInfo.second; 8874514f5e3Sopenharmony_ci LOG_FULL(INFO) << "Current recordName is " << recordName <<", current fileName is " << fileName; 8884514f5e3Sopenharmony_ci if (needRecordName) { 8894514f5e3Sopenharmony_ci if (fileName.length() > ModulePathHelper::BUNDLE_INSTALL_PATH_LEN && 8904514f5e3Sopenharmony_ci fileName.find(ModulePathHelper::BUNDLE_INSTALL_PATH) == 0) { 8914514f5e3Sopenharmony_ci fileName = fileName.substr(ModulePathHelper::BUNDLE_INSTALL_PATH_LEN); 8924514f5e3Sopenharmony_ci } else { 8934514f5e3Sopenharmony_ci LOG_FULL(ERROR) << " GetCurrentModuleName Fail, fileName is " << fileName; 8944514f5e3Sopenharmony_ci } 8954514f5e3Sopenharmony_ci return std::make_pair(recordName.c_str(), fileName.c_str()); 8964514f5e3Sopenharmony_ci } 8974514f5e3Sopenharmony_ci CString moduleName; 8984514f5e3Sopenharmony_ci if (IsNormalizedOhmUrlPack()) { 8994514f5e3Sopenharmony_ci moduleName = ModulePathHelper::GetModuleNameWithNormalizedName(recordName); 9004514f5e3Sopenharmony_ci } else { 9014514f5e3Sopenharmony_ci moduleName = ModulePathHelper::GetModuleName(recordName); 9024514f5e3Sopenharmony_ci } 9034514f5e3Sopenharmony_ci if (moduleName.empty()) { 9044514f5e3Sopenharmony_ci LOG_FULL(ERROR) << " GetCurrentModuleName Fail, recordName is " << recordName; 9054514f5e3Sopenharmony_ci } 9064514f5e3Sopenharmony_ci return std::make_pair(moduleName.c_str(), fileName.c_str()); 9074514f5e3Sopenharmony_ci} 9084514f5e3Sopenharmony_ci 9094514f5e3Sopenharmony_civoid EcmaVM::SetHmsModuleList(const std::vector<panda::HmsMap> &list) 9104514f5e3Sopenharmony_ci{ 9114514f5e3Sopenharmony_ci for (size_t i = 0; i < list.size(); i++) { 9124514f5e3Sopenharmony_ci HmsMap hmsMap = list[i]; 9134514f5e3Sopenharmony_ci hmsModuleList_.emplace(hmsMap.originalPath.c_str(), hmsMap); 9144514f5e3Sopenharmony_ci } 9154514f5e3Sopenharmony_ci} 9164514f5e3Sopenharmony_ci 9174514f5e3Sopenharmony_ciCString EcmaVM::GetHmsModule(const CString &module) const 9184514f5e3Sopenharmony_ci{ 9194514f5e3Sopenharmony_ci auto it = hmsModuleList_.find(module); 9204514f5e3Sopenharmony_ci if (it == hmsModuleList_.end()) { 9214514f5e3Sopenharmony_ci LOG_ECMA(FATAL) << " Get Hms Module failed"; 9224514f5e3Sopenharmony_ci } 9234514f5e3Sopenharmony_ci HmsMap hmsMap = it->second; 9244514f5e3Sopenharmony_ci return hmsMap.targetPath.c_str(); 9254514f5e3Sopenharmony_ci} 9264514f5e3Sopenharmony_ci 9274514f5e3Sopenharmony_cibool EcmaVM::IsHmsModule(const CString &moduleStr) const 9284514f5e3Sopenharmony_ci{ 9294514f5e3Sopenharmony_ci if (hmsModuleList_.empty()) { 9304514f5e3Sopenharmony_ci return false; 9314514f5e3Sopenharmony_ci } 9324514f5e3Sopenharmony_ci auto it = hmsModuleList_.find(moduleStr); 9334514f5e3Sopenharmony_ci if (it == hmsModuleList_.end()) { 9344514f5e3Sopenharmony_ci return false; 9354514f5e3Sopenharmony_ci } 9364514f5e3Sopenharmony_ci return true; 9374514f5e3Sopenharmony_ci} 9384514f5e3Sopenharmony_ci 9394514f5e3Sopenharmony_civoid EcmaVM::SetpkgContextInfoList(const CMap<CString, CMap<CString, CVector<CString>>> &list) 9404514f5e3Sopenharmony_ci{ 9414514f5e3Sopenharmony_ci pkgContextInfoList_ = list; 9424514f5e3Sopenharmony_ci} 9434514f5e3Sopenharmony_ci 9444514f5e3Sopenharmony_ci// Initialize IcuData Path 9454514f5e3Sopenharmony_civoid EcmaVM::InitializeIcuData(const JSRuntimeOptions &options) 9464514f5e3Sopenharmony_ci{ 9474514f5e3Sopenharmony_ci std::string icuPath = options.GetIcuDataPath(); 9484514f5e3Sopenharmony_ci if (icuPath == "default") { 9494514f5e3Sopenharmony_ci#if !WIN_OR_MAC_OR_IOS_PLATFORM && !defined(PANDA_TARGET_LINUX) 9504514f5e3Sopenharmony_ci SetHwIcuDirectory(); 9514514f5e3Sopenharmony_ci#endif 9524514f5e3Sopenharmony_ci } else { 9534514f5e3Sopenharmony_ci std::string absPath; 9544514f5e3Sopenharmony_ci if (ecmascript::RealPath(icuPath, absPath)) { 9554514f5e3Sopenharmony_ci u_setDataDirectory(absPath.c_str()); 9564514f5e3Sopenharmony_ci } 9574514f5e3Sopenharmony_ci } 9584514f5e3Sopenharmony_ci} 9594514f5e3Sopenharmony_ci 9604514f5e3Sopenharmony_ci// Initialize Process StartRealTime 9614514f5e3Sopenharmony_ciint EcmaVM::InitializeStartRealTime() 9624514f5e3Sopenharmony_ci{ 9634514f5e3Sopenharmony_ci int startRealTime = 0; 9644514f5e3Sopenharmony_ci struct timespec timespro = {0, 0}; 9654514f5e3Sopenharmony_ci struct timespec timessys = {0, 0}; 9664514f5e3Sopenharmony_ci auto res = clock_gettime(CLOCK_PROCESS_CPUTIME_ID, ×pro); 9674514f5e3Sopenharmony_ci if (res) { 9684514f5e3Sopenharmony_ci return startRealTime; 9694514f5e3Sopenharmony_ci } 9704514f5e3Sopenharmony_ci auto res1 = clock_gettime(CLOCK_MONOTONIC, ×sys); 9714514f5e3Sopenharmony_ci if (res1) { 9724514f5e3Sopenharmony_ci return startRealTime; 9734514f5e3Sopenharmony_ci } 9744514f5e3Sopenharmony_ci 9754514f5e3Sopenharmony_ci int whenpro = int(timespro.tv_sec * 1000) + int(timespro.tv_nsec / 1000000); 9764514f5e3Sopenharmony_ci int whensys = int(timessys.tv_sec * 1000) + int(timessys.tv_nsec / 1000000); 9774514f5e3Sopenharmony_ci startRealTime = (whensys - whenpro); 9784514f5e3Sopenharmony_ci return startRealTime; 9794514f5e3Sopenharmony_ci} 9804514f5e3Sopenharmony_ci} // namespace panda::ecmascript 981