1 /* 2 * Copyright (c) 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_MODULE_JS_SHARED_MODULE_MANAGER_H 17 #define ECMASCRIPT_MODULE_JS_SHARED_MODULE_MANAGER_H 18 19 #include "ecmascript/js_tagged_value-inl.h" 20 #include "ecmascript/jspandafile/js_pandafile.h" 21 #include "ecmascript/module/js_module_manager.h" 22 #include "ecmascript/module/js_module_source_text.h" 23 #include "ecmascript/module/js_shared_module.h" 24 #include "ecmascript/module/module_manager_map.h" 25 #include "ecmascript/napi/jsnapi_helper.h" 26 #include "ecmascript/tagged_dictionary.h" 27 28 namespace panda::ecmascript { 29 struct StateVisit { 30 Mutex mutex; 31 ConditionVariable cv; 32 uint32_t threadId; 33 }; 34 class SharedModuleManager { 35 public: 36 static PUBLIC_API SharedModuleManager *GetInstance(); 37 Initialize()38 void Initialize() {}; 39 Destroy()40 void Destroy() 41 { 42 resolvedSharedModules_.Clear(); 43 } 44 45 void Iterate(RootVisitor &v); 46 47 StateVisit &FindModuleMutexWithLock(JSThread *thread, const JSHandle<SourceTextModule> &module); 48 49 bool SearchInSModuleManager(JSThread *thread, const CString &recordName); 50 51 JSHandle<SourceTextModule> GetSModule(JSThread *thread, const CString &recordName); 52 53 JSHandle<SourceTextModule> PUBLIC_API TransferFromLocalToSharedModuleMapAndGetInsertedSModule( 54 JSThread *thread, const JSHandle<SourceTextModule> &module); 55 56 bool IsInstantiatedSModule(JSThread *thread, const JSHandle<SourceTextModule> &module); 57 58 JSHandle<JSTaggedValue> GenerateFuncModule(JSThread *thread, const JSPandaFile *jsPandaFile, 59 const CString &entryPoint, ClassKind classKind = ClassKind::NON_SENDABLE); 60 61 JSHandle<ModuleNamespace> SModuleNamespaceCreate(JSThread *thread, const JSHandle<JSTaggedValue> &module, 62 const JSHandle<TaggedArray> &exports); 63 64 void AddToResolvedModulesAndCreateSharedModuleMutex(JSThread *thread, const CString &recordName, 65 JSTaggedValue module); 66 AddResolveImportedSModule(const CString & recordName,JSTaggedValue module)67 inline bool AddResolveImportedSModule(const CString &recordName, JSTaggedValue module) 68 { 69 return resolvedSharedModules_.Emplace(recordName, module); 70 } 71 UpdateResolveImportedSModule(const CString & recordName,JSTaggedValue module)72 inline void UpdateResolveImportedSModule(const CString &recordName, JSTaggedValue module) 73 { 74 resolvedSharedModules_.Insert(recordName, module); 75 } 76 void SharedNativeObjDestory(); 77 GetSharedMutex()78 RecursiveMutex& GetSharedMutex() 79 { 80 return sharedMutex_; 81 } 82 GetResolvedSharedModulesSize()83 inline uint32_t GetResolvedSharedModulesSize() 84 { 85 return resolvedSharedModules_.Size(); 86 } 87 AddSharedSerializeModule(JSThread * thread,JSHandle<TaggedArray> serializerArray,uint32_t idx)88 void AddSharedSerializeModule(JSThread *thread, JSHandle<TaggedArray> serializerArray, uint32_t idx) 89 { 90 resolvedSharedModules_.ForEach( 91 [thread, &idx, &serializerArray](auto it) { serializerArray->Set(thread, idx++, it->second.Read()); }); 92 } 93 94 private: 95 SharedModuleManager() = default; 96 ~SharedModuleManager() = default; 97 98 NO_COPY_SEMANTIC(SharedModuleManager); 99 NO_MOVE_SEMANTIC(SharedModuleManager); 100 101 bool SearchInSModuleManagerUnsafe(const CString &recordName); 102 103 JSHandle<SourceTextModule> GetSModuleUnsafe(JSThread *thread, const CString &recordName); 104 105 bool TryInsertInSModuleManager(JSThread *thread, const CString &recordName, 106 const JSHandle<SourceTextModule> &moduleRecord); 107 108 static constexpr uint32_t DEAULT_DICTIONART_CAPACITY = 4; 109 ModuleManagerMap<CString> resolvedSharedModules_; 110 CMap<CString, StateVisit> sharedModuleMutex_; 111 Mutex mutex_; 112 RecursiveMutex sharedMutex_; 113 114 friend class SourceTextModule; 115 friend class ModuleManager; 116 }; 117 } // namespace panda::ecmascript 118 #endif // ECMASCRIPT_MODULE_JS_SHARED_MODULE_MANAGER_H 119