1 /* 2 * Copyright (c) 2022 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_JSPANDAFILE_JS_PANDAFILE_MANAGER_H 17 #define ECMASCRIPT_JSPANDAFILE_JS_PANDAFILE_MANAGER_H 18 19 #include "ecmascript/mem/c_containers.h" 20 #include "ecmascript/jspandafile/js_pandafile.h" 21 #include "ecmascript/jspandafile/panda_file_translator.h" 22 #include "ecmascript/jspandafile/debug_info_extractor.h" 23 24 #include "libpandafile/file.h" 25 26 namespace panda { 27 namespace ecmascript { 28 class Program; 29 30 class PUBLIC_API JSPandaFileManager { 31 public: 32 static JSPandaFileManager *GetInstance(); 33 34 ~JSPandaFileManager(); 35 36 JSHandle<Program> GenerateProgram(EcmaVM *vm, const JSPandaFile *jsPandaFile, std::string_view entryPoint); 37 38 const JSPandaFile *LoadJSPandaFile(JSThread *thread, const CString &filename, std::string_view entryPoint, 39 bool needUpdate = false); 40 41 const JSPandaFile *LoadJSPandaFile(JSThread *thread, const CString &filename, std::string_view entryPoint, 42 const void *buffer, size_t size, bool needUpdate = false); 43 44 JSPandaFile *OpenJSPandaFile(const CString &filename); 45 46 JSPandaFile *NewJSPandaFile(const panda_file::File *pf, const CString &desc); 47 48 DebugInfoExtractor *GetJSPtExtractor(const JSPandaFile *jsPandaFile); 49 50 static void RemoveJSPandaFile(void *pointer); 51 52 // for debugger 53 template<typename Callback> EnumerateJSPandaFiles(Callback cb)54 void EnumerateJSPandaFiles(Callback cb) 55 { 56 // since there is a lock, so cannot mark function const 57 os::memory::LockHolder lock(jsPandaFileLock_); 58 for (const auto &iter : loadedJSPandaFiles_) { 59 if (!cb(iter.second.first)) { 60 return; 61 } 62 } 63 for (const auto &iter : oldJSPandaFiles_) { 64 if (!cb(iter.first)) { 65 return; 66 } 67 } 68 } 69 70 void InsertJSPandaFile(const JSPandaFile *jsPandaFile); 71 void ClearCache(); 72 73 const JSPandaFile *FindJSPandaFile(const CString &filename); 74 private: 75 JSPandaFileManager() = default; 76 77 class JSPandaFileAllocator { 78 public: 79 static void *AllocateBuffer(size_t size); 80 static void FreeBuffer(void *mem); 81 }; 82 83 const JSPandaFile *GenerateJSPandaFile(JSThread *thread, const panda_file::File *pf, const CString &desc, 84 std::string_view entryPoint); 85 void ReleaseJSPandaFile(const JSPandaFile *jsPandaFile); 86 const JSPandaFile *GetJSPandaFile(const panda_file::File *pf); 87 const JSPandaFile *FindJSPandaFileWithChecksum(const CString &filename, uint32_t checksum); 88 const JSPandaFile *FindJSPandaFileUnlocked(const CString &filename); 89 void IncreaseRefJSPandaFileUnlocked(const JSPandaFile *jsPandaFile); 90 void DecreaseRefJSPandaFile(const JSPandaFile *jsPandaFile); 91 void ObsoleteLoadedJSPandaFile(const CString &filename); 92 93 static void *AllocateBuffer(size_t size); 94 static void FreeBuffer(void *mem); 95 96 os::memory::RecursiveMutex jsPandaFileLock_; 97 std::unordered_map<const CString, std::pair<const JSPandaFile *, uint32_t>, CStringHash> loadedJSPandaFiles_; 98 std::unordered_map<const JSPandaFile *, uint32_t> oldJSPandaFiles_; 99 std::unordered_map<const JSPandaFile *, std::unique_ptr<DebugInfoExtractor>> extractors_; 100 101 friend class JSPandaFile; 102 }; 103 } // namespace ecmascript 104 } // namespace panda 105 #endif // ECMASCRIPT_JSPANDAFILE_JS_PANDAFILE_MANAGER_H 106