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/jspandafile/js_pandafile.h" 20 #include "ecmascript/jspandafile/panda_file_translator.h" 21 #include "ecmascript/jspandafile/debug_info_extractor.h" 22 #include "ecmascript/platform/mutex.h" 23 24 namespace panda { 25 namespace ecmascript { 26 class Program; 27 28 class PUBLIC_API JSPandaFileManager { 29 public: 30 static JSPandaFileManager *GetInstance(); 31 32 ~JSPandaFileManager(); 33 34 JSHandle<Program> GenerateProgram(EcmaVM *vm, const JSPandaFile *jsPandaFile, std::string_view entryPoint); 35 36 std::shared_ptr<JSPandaFile> LoadJSPandaFile(JSThread *thread, const CString &filename, 37 std::string_view entryPoint, bool needUpdate = false, 38 const ExecuteTypes &executeType = ExecuteTypes::STATIC); 39 40 std::shared_ptr<JSPandaFile> LoadJSPandaFile(JSThread *thread, const CString &filename, std::string_view entryPoint, 41 const void *buffer, size_t size, bool needUpdate = false); 42 43 // load pandafile from secure mem 44 std::shared_ptr<JSPandaFile> LoadJSPandaFileSecure(JSThread *thread, const CString &filename, 45 std::string_view entryPoint, uint8_t *buffer, size_t size, 46 bool needUpdate = false); 47 48 std::shared_ptr<JSPandaFile> OpenJSPandaFile(const CString &filename); 49 50 std::shared_ptr<JSPandaFile> OpenJSPandaFile(const CString &filename, const CString &desc); 51 52 std::shared_ptr<JSPandaFile> OpenJSPandaFileFromBuffer(uint8_t *buffer, size_t size, const CString &filename); 53 54 std::shared_ptr<JSPandaFile> NewJSPandaFile(const panda_file::File *pf, const CString &desc); 55 56 DebugInfoExtractor *GetJSPtExtractor(const JSPandaFile *jsPandaFile); 57 58 DebugInfoExtractor *GetJSPtExtractorAndExtract(const JSPandaFile *jsPandaFile); 59 60 DebugInfoExtractor *CpuProfilerGetJSPtExtractor(const JSPandaFile *jsPandaFile); 61 62 bool CheckFilePath(JSThread *thread, const CString &fileName); 63 64 // for debugger 65 template<typename Callback> EnumerateJSPandaFiles(Callback cb)66 void EnumerateJSPandaFiles(Callback cb) 67 { 68 LockHolder lock(jsPandaFileLock_); 69 for (const auto &item : loadedJSPandaFiles_) { 70 if (!cb(item.second)) { 71 return; 72 } 73 } 74 for (const auto &item : oldJSPandaFiles_) { 75 if (!cb(item)) { 76 return; 77 } 78 } 79 } 80 81 template<typename Callback> EnumerateNonVirtualJSPandaFiles(Callback cb)82 void EnumerateNonVirtualJSPandaFiles(Callback cb) 83 { 84 LockHolder lock(jsPandaFileLock_); 85 for (const auto &item : loadedJSPandaFiles_) { 86 if (!cb(item.second)) { 87 return; 88 } 89 } 90 for (const auto &item : oldJSPandaFiles_) { 91 if (!cb(item)) { 92 return; 93 } 94 } 95 } 96 std::shared_ptr<JSPandaFile> FindJSPandaFileByNormalizedName(const CString &normalizedName); 97 std::shared_ptr<JSPandaFile> FindJSPandaFileByMapBase(uintptr_t mapBase); 98 std::shared_ptr<JSPandaFile> FindJSPandaFile(const CString &filename); 99 std::shared_ptr<JSPandaFile> FindMergedJSPandaFile(); 100 void AddJSPandaFile(const std::shared_ptr<JSPandaFile> &jsPandaFile); 101 void RemoveJSPandaFile(const JSPandaFile *jsPandaFile); 102 void ClearNameMap(); 103 private: 104 JSPandaFileManager() = default; 105 106 class JSPandaFileAllocator { 107 public: 108 static void *AllocateBuffer(size_t size); 109 static void FreeBuffer(void *mem); 110 }; 111 112 std::shared_ptr<JSPandaFile> GenerateJSPandaFile(JSThread *thread, const panda_file::File *pf, const CString &desc, 113 std::string_view entryPoint); 114 std::shared_ptr<JSPandaFile> GetJSPandaFile(const panda_file::File *pf); 115 std::shared_ptr<JSPandaFile> FindJSPandaFileWithChecksum(const CString &filename, uint32_t checksum); 116 std::shared_ptr<JSPandaFile> FindJSPandaFileUnlocked(const CString &filename); 117 std::shared_ptr<JSPandaFile> GenerateJSPandafileFromBufferCache(JSThread *thread, 118 const CString &filename, 119 std::string_view entryPoint); 120 void ObsoleteLoadedJSPandaFile(const CString &filename); 121 122 static void *AllocateBuffer(size_t size, bool isBundlePack, CreateMode mode); 123 static void FreeBuffer(void *mem, size_t size, bool isBundlePack, CreateMode mode); 124 125 RecursiveMutex jsPandaFileLock_; 126 // JSPandaFile was shared by all vm. 127 std::unordered_map<const CString, std::shared_ptr<JSPandaFile>, CStringHash> loadedJSPandaFiles_; 128 // for plugin update. 129 std::set<std::shared_ptr<JSPandaFile>> oldJSPandaFiles_; 130 std::unordered_map<const JSPandaFile *, std::unique_ptr<DebugInfoExtractor>> extractors_; 131 132 friend class JSPandaFile; 133 }; 134 } // namespace ecmascript 135 } // namespace panda 136 #endif // ECMASCRIPT_JSPANDAFILE_JS_PANDAFILE_MANAGER_H 137