• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2024 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef ECMASCRIPT_ECMA_VM_H
17 #define ECMASCRIPT_ECMA_VM_H
18 
19 #include <mutex>
20 
21 #include "ecmascript/base/config.h"
22 #include "ecmascript/builtins/builtins_method_index.h"
23 #include "ecmascript/js_runtime_options.h"
24 #include "ecmascript/mem/c_containers.h"
25 #include "ecmascript/mem/c_string.h"
26 #include "ecmascript/mem/gc_stats.h"
27 #include "ecmascript/mem/gc_key_stats.h"
28 #include "ecmascript/mem/heap_region_allocator.h"
29 #include "ecmascript/napi/include/dfx_jsnapi.h"
30 #include "ecmascript/napi/include/jsnapi.h"
31 #include "ecmascript/taskpool/taskpool.h"
32 
33 namespace panda {
34 class JSNApi;
35 struct HmsMap;
36 namespace panda_file {
37 class File;
38 }  // namespace panda_file
39 
40 namespace ecmascript {
41 class GlobalEnv;
42 class ObjectFactory;
43 class RegExpParserCache;
44 class EcmaRuntimeStat;
45 class Heap;
46 class HeapTracker;
47 class JSNativePointer;
48 class Program;
49 class GCStats;
50 class GCKeyStats;
51 class CpuProfiler;
52 class Tracing;
53 class RegExpExecResultCache;
54 class JSPromise;
55 enum class PromiseRejectionEvent : uint8_t;
56 enum class Concurrent { YES, NO };
57 class JSPandaFileManager;
58 class JSPandaFile;
59 class EcmaStringTable;
60 class SnapshotEnv;
61 class SnapshotSerialize;
62 class SnapshotProcessor;
63 class JSThread;
64 
65 namespace pgo {
66     class PGOProfiler;
67 } // namespace pgo
68 
69 using PGOProfiler = pgo::PGOProfiler;
70 #if !WIN_OR_MAC_OR_IOS_PLATFORM
71 class HeapProfilerInterface;
72 class HeapProfiler;
73 #endif
74 namespace job {
75 class MicroJobQueue;
76 }  // namespace job
77 
78 namespace tooling {
79 class JsDebuggerManager;
80 }  // namespace tooling
81 
82 template<typename T>
83 class JSHandle;
84 class JSArrayBuffer;
85 class JSFunction;
86 class SourceTextModule;
87 class Program;
88 class AOTFileManager;
89 class SlowRuntimeStub;
90 class RequireManager;
91 class QuickFixManager;
92 class ConstantPool;
93 class FunctionCallTimer;
94 class EcmaStringTable;
95 class JSObjectResizingStrategy;
96 class Jit;
97 class JitThread;
98 
99 using NativePtrGetter = void* (*)(void* info);
100 using SourceMapCallback = std::function<std::string(const std::string& rawStack)>;
101 using SourceMapTranslateCallback = std::function<bool(std::string& url, int& line, int& column)>;
102 using ResolveBufferCallback =
103     std::function<bool(std::string dirPath, uint8_t **buff, size_t *buffSize, std::string &errorMsg)>;
104 using UnloadNativeModuleCallback = std::function<bool(const std::string &moduleKey)>;
105 using RequestAotCallback =
106     std::function<int32_t(const std::string &bundleName, const std::string &moduleName, int32_t triggerMode)>;
107 using SearchHapPathCallBack = std::function<bool(const std::string moduleName, std::string &hapPath)>;
108 using DeviceDisconnectCallback = std::function<bool()>;
109 using UncatchableErrorHandler = std::function<void(panda::TryCatch&)>;
110 
111 class EcmaVM {
112 public:
113     static EcmaVM *Create(const JSRuntimeOptions &options);
114 
115     static bool Destroy(EcmaVM *vm);
116 
117     EcmaVM(JSRuntimeOptions options, EcmaParamConfiguration config);
118 
119     EcmaVM();
120 
121     ~EcmaVM();
122 
SetLoop(void * loop)123     void SetLoop(void *loop)
124     {
125         loop_ = loop;
126     }
127 
GetLoop()128     void *GetLoop() const
129     {
130         return loop_;
131     }
132 
IsInitialized()133     bool IsInitialized() const
134     {
135         return initialized_;
136     }
137 
SetPostForked(bool isPostForked)138     void SetPostForked(bool isPostForked)
139     {
140         isPostForked_ = isPostForked;
141     }
142 
IsPostForked()143     bool IsPostForked() const
144     {
145         return isPostForked_;
146     }
147 
IsAsynTranslateClasses()148     bool IsAsynTranslateClasses()
149     {
150         if (!GetJSOptions().IsAsyncLoadAbcTest()) {
151             return IsPostForked() && GetJSOptions().IsAsyncLoadAbc();
152         }
153         return GetJSOptions().IsAsyncLoadAbc();
154     }
155 
GetFactory()156     ObjectFactory *GetFactory() const
157     {
158         return factory_;
159     }
160 
161     void InitializePGOProfiler();
162     void ResetPGOProfiler();
163     void DisablePGOProfilerWithAOTFile(const std::string &aotFileName);
164 
165     bool PUBLIC_API IsEnablePGOProfiler() const;
166     bool PUBLIC_API IsEnableElementsKind() const;
167 
168     bool Initialize();
169     void InitializeForJit(JitThread *thread);
170 
GetEcmaGCStats()171     GCStats *GetEcmaGCStats() const
172     {
173         return gcStats_;
174     }
175 
GetEcmaGCKeyStats()176     GCKeyStats *GetEcmaGCKeyStats() const
177     {
178         return gcKeyStats_;
179     }
180 
GetAssociatedJSThread()181     JSThread *GetAssociatedJSThread() const
182     {
183         return thread_;
184     }
185 
GetJSOptions()186     JSRuntimeOptions &GetJSOptions()
187     {
188         return options_;
189     }
190 
GetEcmaParamConfiguration()191     const EcmaParamConfiguration &GetEcmaParamConfiguration() const
192     {
193         return ecmaParamConfiguration_;
194     }
195 
196     JSHandle<GlobalEnv> PUBLIC_API GetGlobalEnv() const;
197 
ConstCast(const EcmaVM * vm)198     static EcmaVM *ConstCast(const EcmaVM *vm)
199     {
200         return const_cast<EcmaVM *>(vm);
201     }
202 
203     void PUBLIC_API CheckThread() const;
204     bool CheckSingleThread() const;
205 
GetThreadCheckStatus()206     ARK_INLINE bool GetThreadCheckStatus() const
207     {
208         return options_.EnableThreadCheck() || EcmaVM::GetMultiThreadCheck();
209     }
210 
GetJSThread()211     ARK_INLINE JSThread *GetJSThread() const
212     {
213         // default enable multi-thread check in asan
214 #ifdef ECMASCRIPT_ENABLE_ASAN_THREAD_CHECK
215         CheckThread();
216 #else
217         if (GetThreadCheckStatus()) {
218             CheckThread();
219         }
220 #endif
221         return thread_;
222     }
223 
GetJSThreadNoCheck()224     JSThread *GetJSThreadNoCheck() const
225     {
226         return thread_;
227     }
228 
ICEnabled()229     bool ICEnabled() const
230     {
231         return icEnabled_;
232     }
233 
234     void PushToNativePointerList(JSNativePointer *pointer, Concurrent isConcurrent = Concurrent::NO);
235     void RemoveFromNativePointerList(JSNativePointer *pointer);
236     void PushToDeregisterModuleList(const CString &module);
237     void RemoveFromDeregisterModuleList(CString module);
238     bool ContainInDeregisterModuleList(CString module);
239     JSHandle<ecmascript::JSTaggedValue> GetAndClearEcmaUncaughtException() const;
240     JSHandle<ecmascript::JSTaggedValue> GetEcmaUncaughtException() const;
IsOptionalLogEnabled()241     bool IsOptionalLogEnabled() const
242     {
243         return optionalLogEnabled_;
244     }
245 
246     void Iterate(const RootVisitor &v, const RootRangeVisitor &rv, VMRootVisitType type);
247 
GetHeap()248     const Heap *GetHeap() const
249     {
250         return heap_;
251     }
252     void CollectGarbage(TriggerGCType gcType, GCReason reason = GCReason::OTHER) const;
253 
GetNativeAreaAllocator()254     NativeAreaAllocator *GetNativeAreaAllocator() const
255     {
256         return nativeAreaAllocator_.get();
257     }
258 
GetHeapRegionAllocator()259     HeapRegionAllocator *GetHeapRegionAllocator() const
260     {
261         return heapRegionAllocator_.get();
262     }
263 
GetChunk()264     Chunk *GetChunk() const
265     {
266         return const_cast<Chunk *>(&chunk_);
267     }
268     void ProcessNativeDelete(const WeakRootVisitor &visitor);
269     void ProcessReferences(const WeakRootVisitor &visitor);
270 
271     void PushToSharedNativePointerList(JSNativePointer *pointer);
272     void ProcessSharedNativeDelete(const WeakRootVisitor &visitor);
273 
GetSnapshotEnv()274     SnapshotEnv *GetSnapshotEnv() const
275     {
276         return snapshotEnv_;
277     }
278 
GetJsDebuggerManager()279     tooling::JsDebuggerManager *GetJsDebuggerManager() const
280     {
281         return debuggerManager_;
282     }
283 
SetDeviceDisconnectCallback(DeviceDisconnectCallback cb)284     void SetDeviceDisconnectCallback(DeviceDisconnectCallback cb)
285     {
286         deviceDisconnectCallback_ = cb;
287     }
288 
GetDeviceDisconnectCallback()289     DeviceDisconnectCallback GetDeviceDisconnectCallback() const
290     {
291         return deviceDisconnectCallback_;
292     }
293 
SetEnableForceGC(bool enable)294     void SetEnableForceGC(bool enable)
295     {
296         options_.SetEnableForceGC(enable);
297     }
298 
SetNativePtrGetter(NativePtrGetter cb)299     void SetNativePtrGetter(NativePtrGetter cb)
300     {
301         nativePtrGetter_ = cb;
302     }
303 
GetNativePtrGetter()304     NativePtrGetter GetNativePtrGetter() const
305     {
306         return nativePtrGetter_;
307     }
308 
SetSourceMapCallback(SourceMapCallback cb)309     void SetSourceMapCallback(SourceMapCallback cb)
310     {
311         sourceMapCallback_ = cb;
312     }
313 
GetSourceMapCallback()314     SourceMapCallback GetSourceMapCallback() const
315     {
316         return sourceMapCallback_;
317     }
318 
SetSourceMapTranslateCallback(SourceMapTranslateCallback cb)319     void SetSourceMapTranslateCallback(SourceMapTranslateCallback cb)
320     {
321         sourceMapTranslateCallback_ = cb;
322     }
323 
GetSourceMapTranslateCallback()324     SourceMapTranslateCallback GetSourceMapTranslateCallback() const
325     {
326         return sourceMapTranslateCallback_;
327     }
328 
SetResolveBufferCallback(ResolveBufferCallback cb)329     void SetResolveBufferCallback(ResolveBufferCallback cb)
330     {
331         resolveBufferCallback_ = cb;
332     }
333 
GetResolveBufferCallback()334     ResolveBufferCallback GetResolveBufferCallback() const
335     {
336         return resolveBufferCallback_;
337     }
338 
SetSearchHapPathCallBack(SearchHapPathCallBack cb)339     void SetSearchHapPathCallBack(SearchHapPathCallBack cb)
340     {
341         SearchHapPathCallBack_ = cb;
342     }
343 
GetSearchHapPathCallBack()344     SearchHapPathCallBack GetSearchHapPathCallBack() const
345     {
346         return SearchHapPathCallBack_;
347     }
348 
SetUnloadNativeModuleCallback(const UnloadNativeModuleCallback & cb)349     void SetUnloadNativeModuleCallback(const UnloadNativeModuleCallback &cb)
350     {
351         unloadNativeModuleCallback_ = cb;
352     }
353 
GetUnloadNativeModuleCallback()354     UnloadNativeModuleCallback GetUnloadNativeModuleCallback() const
355     {
356         return unloadNativeModuleCallback_;
357     }
358 
SetConcurrentCallback(ConcurrentCallback callback,void * data)359     void SetConcurrentCallback(ConcurrentCallback callback, void *data)
360     {
361         concurrentCallback_ = callback;
362         concurrentData_ = data;
363     }
364 
365     void TriggerConcurrentCallback(JSTaggedValue result, JSTaggedValue hint);
366 
367     void WorkersetInfo(EcmaVM *workerVm);
368 
369     EcmaVM *GetWorkerVm(uint32_t tid);
370 
371     bool DeleteWorker(EcmaVM *workerVm);
372 
373     bool SuspendWorkerVm(uint32_t tid);
374 
375     void ResumeWorkerVm(uint32_t tid);
376 
377     template<typename Callback>
EnumerateWorkerVm(Callback cb)378     void EnumerateWorkerVm(Callback cb)
379     {
380         // since there is a lock, so cannot mark function const
381         LockHolder lock(mutex_);
382         for (const auto &item : workerList_) {
383             cb(item.second);
384         }
385     }
386 
IsWorkerThread()387     bool IsWorkerThread() const
388     {
389         return options_.IsWorker();
390     }
391 
IsRestrictedWorkerThread()392     bool IsRestrictedWorkerThread() const
393     {
394         return options_.IsRestrictedWorker();
395     }
396 
IsBundlePack()397     bool IsBundlePack() const
398     {
399         return isBundlePack_;
400     }
401 
SetIsBundlePack(bool value)402     void SetIsBundlePack(bool value)
403     {
404         isBundlePack_ = value;
405     }
406 
407     // UnifiedOhmUrlPack means app compiles ohmurl using old format like "@bundle:",
408     // or new unified rules like "@normalize:"
409     // if pkgContextInfoList is empty, means use old ohmurl packing.
IsNormalizedOhmUrlPack()410     bool IsNormalizedOhmUrlPack() const
411     {
412         return !pkgContextInfoList_.empty();
413     }
414 
SetPkgNameList(const CMap<CString,CString> & list)415     void SetPkgNameList(const CMap<CString, CString> &list)
416     {
417         pkgNameList_ = list;
418     }
419 
GetPkgNameList()420     CMap<CString, CString> GetPkgNameList() const
421     {
422         return pkgNameList_;
423     }
424 
GetPkgName(const CString & moduleName)425     inline CString GetPkgName(const CString &moduleName) const
426     {
427         auto it = pkgNameList_.find(moduleName);
428         if (it == pkgNameList_.end()) {
429             LOG_ECMA(INFO) << " Get Pkg Name failed";
430             return moduleName;
431         }
432         return it->second;
433     }
434 
GetPkgContextInfoLit()435     inline CMap<CString, CMap<CString, CVector<CString>>> GetPkgContextInfoLit() const
436     {
437         return pkgContextInfoList_;
438     }
439 
GetPkgNameWithAlias(const CString & alias)440     inline CString GetPkgNameWithAlias(const CString &alias) const
441     {
442         auto it = pkgAliasList_.find(alias);
443         if (it == pkgAliasList_.end()) {
444             return alias;
445         }
446         return it->second;
447     }
448 
SetPkgAliasList(const CMap<CString,CString> & list)449     void SetPkgAliasList(const CMap<CString, CString> &list)
450     {
451         pkgAliasList_ = list;
452     }
453 
GetPkgAliasList()454     CMap<CString, CString> GetPkgAliasList() const
455     {
456         return pkgAliasList_;
457     }
458 
SetMockModuleList(const std::map<std::string,std::string> & list)459     void SetMockModuleList(const std::map<std::string, std::string> &list)
460     {
461         for (auto it = list.begin(); it != list.end(); ++it) {
462             mockModuleList_.emplace(it->first.c_str(), it->second.c_str());
463         }
464     }
465 
IsMockModule(const CString & moduleStr)466     inline bool IsMockModule(const CString &moduleStr) const
467     {
468         if (mockModuleList_.empty()) {
469             return false;
470         }
471         auto it = mockModuleList_.find(moduleStr);
472         if (it == mockModuleList_.end()) {
473             return false;
474         }
475         return true;
476     }
477 
GetMockModule(const CString & module)478     inline CString GetMockModule(const CString &module) const
479     {
480         auto it = mockModuleList_.find(module);
481         if (it == mockModuleList_.end()) {
482             LOG_ECMA(FATAL) << " Get Mock Module failed";
483         }
484         return it->second;
485     }
486 
487 #if defined(ECMASCRIPT_SUPPORT_HEAPPROFILER)
488     void DeleteHeapProfile();
489     HeapProfilerInterface *GetHeapProfile();
SetHeapProfile(HeapProfilerInterface * heapProfile)490     void  SetHeapProfile(HeapProfilerInterface *heapProfile) { heapProfile_ = heapProfile; }
491     HeapProfilerInterface *GetOrNewHeapProfile();
492     void StartHeapTracking();
493     void StopHeapTracking();
494 #endif
495 
SetAssetPath(const CString & assetPath)496     void SetAssetPath(const CString &assetPath)
497     {
498         assetPath_ = assetPath;
499     }
500 
GetAssetPath()501     CString GetAssetPath() const
502     {
503         return assetPath_;
504     }
505 
SetBundleName(const CString & bundleName)506     inline void SetBundleName(const CString &bundleName)
507     {
508         bundleName_ = bundleName;
509     }
510 
GetBundleName()511     CString GetBundleName() const
512     {
513         return bundleName_;
514     }
515 
SetModuleName(const CString & moduleName)516     void SetModuleName(const CString &moduleName)
517     {
518         moduleName_ = moduleName;
519     }
520 
GetModuleName()521     CString GetModuleName() const
522     {
523         return moduleName_;
524     }
525 
526     std::pair<std::string, std::string> GetCurrentModuleInfo(bool needRecordName = false);
527 
528     void SetHmsModuleList(const std::vector<panda::HmsMap> &list);
529 
530     bool IsHmsModule(const CString &moduleStr) const;
531 
532     CString GetHmsModule(const CString &module) const;
533 
534     void SetpkgContextInfoList(const CMap<CString, CMap<CString, CVector<CString>>> &list);
535 
536 #if defined(ECMASCRIPT_SUPPORT_CPUPROFILER)
GetProfiler()537     CpuProfiler *GetProfiler() const
538     {
539         return profiler_;
540     }
541 
SetProfiler(CpuProfiler * profiler)542     void SetProfiler(CpuProfiler *profiler)
543     {
544         profiler_ = profiler;
545     }
546 #endif
547 
548 #if defined(ECMASCRIPT_SUPPORT_TRACING)
GetTracing()549     Tracing *GetTracing() const
550     {
551         return tracing_;
552     }
553 
SetTracing(Tracing * tracing)554     void SetTracing(Tracing *tracing)
555     {
556         tracing_ = tracing;
557     }
558 #endif
559 
GetPGOProfiler()560     std::shared_ptr<PGOProfiler> GetPGOProfiler() const
561     {
562         return pgoProfiler_;
563     }
564 
565     void PreFork();
566     void PostFork();
567 
568     // For Internal Native MethodLiteral.
569     JSTaggedValue GetMethodByIndex(MethodIndex idx);
570 
GetQuickFixManager()571     QuickFixManager *GetQuickFixManager() const
572     {
573         return quickFixManager_;
574     }
575 
576     JSTaggedValue FastCallAot(size_t actualNumArgs, JSTaggedType *args, const JSTaggedType *prevFp);
577 
RegisterUncatchableErrorHandler(const UncatchableErrorHandler & uncatchableErrorHandler)578     void RegisterUncatchableErrorHandler(const UncatchableErrorHandler &uncatchableErrorHandler)
579     {
580         uncatchableErrorHandler_ = uncatchableErrorHandler;
581     }
582 
583     // handle uncatchable errors, such as oom
HandleUncatchableError()584     void HandleUncatchableError()
585     {
586         if (uncatchableErrorHandler_ != nullptr) {
587             panda::TryCatch trycatch(this);
588             uncatchableErrorHandler_(trycatch);
589         }
590         LOG_ECMA_MEM(FATAL) << "Out of Memory";
591     }
592 
593     void DumpCallTimeInfo();
594 
GetCallTimer()595     FunctionCallTimer *GetCallTimer() const
596     {
597         return callTimer_;
598     }
599 
GetEcmaStringTable()600     EcmaStringTable *GetEcmaStringTable() const
601     {
602         ASSERT(stringTable_ != nullptr);
603         return stringTable_;
604     }
605 
IncreaseCallDepth()606     void IncreaseCallDepth()
607     {
608         callDepth_++;
609     }
610 
DecreaseCallDepth()611     void DecreaseCallDepth()
612     {
613         ASSERT(callDepth_ > 0);
614         callDepth_--;
615     }
616 
IsTopLevelCallDepth()617     bool IsTopLevelCallDepth()
618     {
619         return callDepth_ == 0;
620     }
621 
SetProfilerState(bool state)622     void SetProfilerState(bool state)
623     {
624         isProfiling_ = state;
625     }
626 
GetProfilerState()627     bool GetProfilerState()
628     {
629         return isProfiling_;
630     }
631 
GetJSObjectResizingStrategy()632     JSObjectResizingStrategy *GetJSObjectResizingStrategy()
633     {
634         return strategy_;
635     }
636 
GetWorkList()637     CMap<uint32_t, EcmaVM *> GetWorkList() const
638     {
639         return workerList_;
640     }
641 
GetProcessStartRealtime()642     int GetProcessStartRealtime() const
643     {
644         return processStartRealtime_;
645     }
646 
SetProcessStartRealtime(int value)647     void SetProcessStartRealtime(int value)
648     {
649         processStartRealtime_ = value;
650     }
651 
652     Jit *GetJit() const;
653     bool PUBLIC_API IsEnableFastJit() const;
654     bool PUBLIC_API IsEnableBaselineJit() const;
655 
IsEnableOsr()656     bool IsEnableOsr() const
657     {
658         return isEnableOsr_;
659     }
660 
SetEnableOsr(bool state)661     void SetEnableOsr(bool state)
662     {
663         isEnableOsr_ = state;
664     }
665 
GetAOTFileManager()666     AOTFileManager *GetAOTFileManager() const
667     {
668         return aotFileManager_;
669     }
670 
671     uint32_t GetTid() const;
672 
GetConcurrentNativePointerCallbacks()673     std::vector<NativePointerCallbackData> &GetConcurrentNativePointerCallbacks()
674     {
675         return concurrentNativeCallbacks_;
676     }
677 
GetAsyncNativePointerCallbacksPack()678     AsyncNativeCallbacksPack &GetAsyncNativePointerCallbacksPack()
679     {
680         return asyncNativeCallbacksPack_;
681     }
682 
SetIsJitCompileVM(bool isJitCompileVM)683     void SetIsJitCompileVM(bool isJitCompileVM)
684     {
685         isJitCompileVM_ = isJitCompileVM;
686     }
687 
IsJitCompileVM()688     bool IsJitCompileVM() const
689     {
690         return isJitCompileVM_;
691     }
692 
SetMultiThreadCheck(bool multiThreadCheck)693     static void SetMultiThreadCheck(bool multiThreadCheck)
694     {
695         multiThreadCheck_ = multiThreadCheck;
696     }
697 
GetMultiThreadCheck()698     PUBLIC_API static bool GetMultiThreadCheck()
699     {
700         return multiThreadCheck_;
701     }
702 
SetErrorInfoEnhance(bool errorInfoEnhance)703     static void SetErrorInfoEnhance(bool errorInfoEnhance)
704     {
705         errorInfoEnhanced_ = errorInfoEnhance;
706     }
707 
GetErrorInfoEnhance()708     static bool GetErrorInfoEnhance()
709     {
710         return errorInfoEnhanced_;
711     }
712 
713     static void InitializeIcuData(const JSRuntimeOptions &options);
714 
715     static int InitializeStartRealTime();
716 
GetSharedNativePointerCallbacks()717     std::vector<std::pair<NativePointerCallback, std::pair<void *, void *>>> &GetSharedNativePointerCallbacks()
718     {
719         return sharedNativePointerCallbacks_;
720     }
721 #if ECMASCRIPT_ENABLE_SCOPE_LOCK_STAT
ResetScopeLockStats()722     void ResetScopeLockStats()
723     {
724         enterThreadManagedScopeCount_ = 0;
725         enterJsiNativeScopeCount_ = 0;
726         enterFastNativeScopeCount_ = 0;
727         updateThreadStateTransCount_ = 0;
728         stringTableLockCount_ = 0;
729     }
730 
IsCollectingScopeLockStats()731     bool IsCollectingScopeLockStats() const
732     {
733         return isCollectingScopeLockStats_;
734     }
735 
StartCollectingScopeLockStats()736     void StartCollectingScopeLockStats()
737     {
738         isCollectingScopeLockStats_ = true;
739     }
740 
StopCollectingScopeLockStats()741     void StopCollectingScopeLockStats()
742     {
743         isCollectingScopeLockStats_ = false;
744     }
745 
GetEnterThreadManagedScopeCount()746     int GetEnterThreadManagedScopeCount() const
747     {
748         return enterThreadManagedScopeCount_;
749     }
750 
IncreaseEnterThreadManagedScopeCount()751     void IncreaseEnterThreadManagedScopeCount()
752     {
753         enterThreadManagedScopeCount_++;
754     }
755 
GetEnterFastNativeScopeCount()756     int GetEnterFastNativeScopeCount() const
757     {
758         return enterFastNativeScopeCount_;
759     }
760 
IncreaseEnterFastNativeScopeCount()761     void IncreaseEnterFastNativeScopeCount()
762     {
763         enterFastNativeScopeCount_++;
764     }
765 
GetEnterJsiNativeScopeCount()766     int GetEnterJsiNativeScopeCount() const
767     {
768         return enterJsiNativeScopeCount_;
769     }
770 
IncreaseEnterJsiNativeScopeCount()771     void IncreaseEnterJsiNativeScopeCount()
772     {
773         enterJsiNativeScopeCount_++;
774     }
775 
GetUpdateThreadStateTransCount()776     int GetUpdateThreadStateTransCount() const
777     {
778         return updateThreadStateTransCount_;
779     }
780 
IncreaseUpdateThreadStateTransCount()781     void IncreaseUpdateThreadStateTransCount()
782     {
783         updateThreadStateTransCount_++;
784     }
785 
GetStringTableLockCount()786     int GetStringTableLockCount() const
787     {
788         return stringTableLockCount_;
789     }
790 
IncreaseStringTableLockCount()791     void IncreaseStringTableLockCount()
792     {
793         stringTableLockCount_++;
794     }
795 #endif
796 
GetEnableJitLogSkip()797     bool GetEnableJitLogSkip() const
798     {
799         return enableJitLogSkip_;
800     }
801 
SetEnableJitLogSkip(bool flag)802     void SetEnableJitLogSkip(bool flag)
803     {
804         enableJitLogSkip_ = flag;
805     }
806 
807     void AddAOTSnapShotStats(std::string tag, uint32_t count = 1)
808     {
809         aotSnapShotStatsMap_[tag] += count;
810     }
811 
812     void PUBLIC_API PrintAOTSnapShotStats();
813 
814 protected:
815 
816     void PrintJSErrorInfo(const JSHandle<JSTaggedValue> &exceptionInfo) const;
817 
818 private:
819     void ClearBufferData();
820     void CheckStartCpuProfiler();
821 
822     // For Internal Native MethodLiteral.
823     void GenerateInternalNativeMethods();
824     void CacheToGlobalConstants(JSTaggedValue value, ConstantIndex constant);
825 
826     NO_MOVE_SEMANTIC(EcmaVM);
827     NO_COPY_SEMANTIC(EcmaVM);
828 
829     // VM startup states.
830     JSRuntimeOptions options_;
831     bool icEnabled_ {true};
832     bool initialized_ {false};
833     bool isPostForked_ {false};
834     GCStats *gcStats_ {nullptr};
835     GCKeyStats *gcKeyStats_ {nullptr};
836     EcmaStringTable *stringTable_ {nullptr};
837     PUBLIC_API static bool multiThreadCheck_;
838     static bool errorInfoEnhanced_;
839 
840     // VM memory management.
841     std::unique_ptr<NativeAreaAllocator> nativeAreaAllocator_;
842     std::unique_ptr<HeapRegionAllocator> heapRegionAllocator_;
843     Chunk chunk_;
844     Heap *heap_ {nullptr};
845     ObjectFactory *factory_ {nullptr};
846 
847     std::vector<NativePointerCallbackData> concurrentNativeCallbacks_ {};
848     AsyncNativeCallbacksPack asyncNativeCallbacksPack_;
849     std::vector<std::pair<NativePointerCallback, std::pair<void *, void *>>> sharedNativePointerCallbacks_ {};
850     // VM execution states.
851     JSThread *thread_ {nullptr};
852 
853     CUnorderedMap<std::string, uint32_t> aotSnapShotStatsMap_;
854 
855     // VM resources.
856     SnapshotEnv *snapshotEnv_ {nullptr};
857     bool optionalLogEnabled_ {false};
858     // Debugger
859     tooling::JsDebuggerManager *debuggerManager_ {nullptr};
860     // isBundle means app compile mode is JSBundle
861     bool isBundlePack_ {true};
862 #if !WIN_OR_MAC_OR_IOS_PLATFORM
863     HeapProfilerInterface *heapProfile_ {nullptr};
864 #endif
865     CString assetPath_;
866     CString bundleName_;
867     CString moduleName_;
868     CList<CString> deregisterModuleList_;
869     CMap<CString, CString> mockModuleList_;
870     std::map<CString, HmsMap> hmsModuleList_;
871     CMap<CString, CString> pkgNameList_;
872     CMap<CString, CMap<CString, CVector<CString>>> pkgContextInfoList_;
873     CMap<CString, CString> pkgAliasList_;
874     NativePtrGetter nativePtrGetter_ {nullptr};
875     SourceMapCallback sourceMapCallback_ {nullptr};
876     SourceMapTranslateCallback sourceMapTranslateCallback_ {nullptr};
877     void *loop_ {nullptr};
878 
879     // resolve path to get abc's buffer
880     ResolveBufferCallback resolveBufferCallback_ {nullptr};
881 
882     // delete the native module and dlclose so from NativeModuleManager
883     UnloadNativeModuleCallback unloadNativeModuleCallback_ {nullptr};
884 
885     // Concurrent taskpool callback and data
886     ConcurrentCallback concurrentCallback_ {nullptr};
887     void *concurrentData_ {nullptr};
888 
889     // serch happath callback
890     SearchHapPathCallBack SearchHapPathCallBack_ {nullptr};
891 
892     // vm parameter configurations
893     EcmaParamConfiguration ecmaParamConfiguration_;
894 #if defined(ECMASCRIPT_SUPPORT_CPUPROFILER)
895     CpuProfiler *profiler_ {nullptr};
896 #endif
897 #if defined(ECMASCRIPT_SUPPORT_TRACING)
898     Tracing *tracing_ {nullptr};
899 #endif
900     FunctionCallTimer *callTimer_ {nullptr};
901     JSObjectResizingStrategy *strategy_ {nullptr};
902 
903     // For Native MethodLiteral
904     static void *InternalMethodTable[static_cast<uint8_t>(MethodIndex::METHOD_END)];
905     CVector<JSTaggedValue> internalNativeMethods_;
906 
907     // For repair patch.
908     QuickFixManager *quickFixManager_ {nullptr};
909 
910     // PGO Profiler
911     std::shared_ptr<PGOProfiler> pgoProfiler_ {nullptr};
912 
913     //AOT File Manager
914     AOTFileManager *aotFileManager_ {nullptr};
915 
916     // c++ call js
917     size_t callDepth_ {0};
918 
919     bool isProfiling_ {false};
920 
921     DeviceDisconnectCallback deviceDisconnectCallback_ {nullptr};
922 
923     UncatchableErrorHandler uncatchableErrorHandler_ {nullptr};
924 
925     friend class Snapshot;
926     friend class SnapshotProcessor;
927     friend class ObjectFactory;
928     friend class ValueSerializer;
929     friend class panda::JSNApi;
930     friend class JSPandaFileExecutor;
931     friend class EcmaContext;
932     friend class JitVM;
933     CMap<uint32_t, EcmaVM *> workerList_ {};
934     Mutex mutex_;
935     bool isEnableOsr_ {false};
936     bool isJitCompileVM_ {false};
937     bool enableJitLogSkip_ = true;
938 
939     // process StartRealTime
940     int processStartRealtime_ = 0;
941 
942 #if ECMASCRIPT_ENABLE_SCOPE_LOCK_STAT
943     // Stats for Thread-State-Transition and String-Table Locks
944     bool isCollectingScopeLockStats_ = false;
945     int enterThreadManagedScopeCount_ = 0;
946     int enterFastNativeScopeCount_ = 0;
947     int enterJsiNativeScopeCount_ = 0;
948     int updateThreadStateTransCount_ = 0;
949     int stringTableLockCount_ = 0;
950 #endif
951 };
952 }  // namespace ecmascript
953 }  // namespace panda
954 
955 #endif
956