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, std::string_view entryPoint, 37 bool needUpdate = false); 38 39 std::shared_ptr<JSPandaFile> LoadJSPandaFile(JSThread *thread, const CString &filename, std::string_view entryPoint, 40 const void *buffer, size_t size, bool needUpdate = false); 41 42 // load pandafile from secure mem 43 std::shared_ptr<JSPandaFile> LoadJSPandaFileSecure(JSThread *thread, const CString &filename, 44 std::string_view entryPoint, uint8_t *buffer, size_t size, 45 bool needUpdate = false); 46 47 std::shared_ptr<JSPandaFile> OpenJSPandaFile(const CString &filename); 48 49 std::shared_ptr<JSPandaFile> OpenJSPandaFile(const CString &filename, const CString &desc); 50 51 std::shared_ptr<JSPandaFile> OpenJSPandaFileFromBuffer(uint8_t *buffer, size_t size, const CString &filename); 52 53 std::shared_ptr<JSPandaFile> NewJSPandaFile(const panda_file::File *pf, const CString &desc); 54 55 DebugInfoExtractor *GetJSPtExtractor(const JSPandaFile *jsPandaFile); 56 57 DebugInfoExtractor *GetJSPtExtractorAndExtract(const JSPandaFile *jsPandaFile); 58 59 DebugInfoExtractor *CpuProfilerGetJSPtExtractor(const JSPandaFile *jsPandaFile); 60 61 // for debugger 62 template<typename Callback> EnumerateJSPandaFiles(Callback cb)63 void EnumerateJSPandaFiles(Callback cb) 64 { 65 LockHolder lock(jsPandaFileLock_); 66 for (const auto &item : loadedJSPandaFiles_) { 67 if (!cb(item.second.first)) { 68 return; 69 } 70 } 71 for (const auto &item : oldJSPandaFiles_) { 72 if (!cb(item.first)) { 73 return; 74 } 75 } 76 } 77 78 std::shared_ptr<JSPandaFile> FindJSPandaFileByNormalizedName(const CString &normalizedName); 79 std::shared_ptr<JSPandaFile> FindJSPandaFile(const CString &filename); 80 std::shared_ptr<JSPandaFile> FindMergedJSPandaFile(); 81 void AddJSPandaFileVm(const EcmaVM *vm, const std::shared_ptr<JSPandaFile> &jsPandaFile); 82 void RemoveJSPandaFileVm(const EcmaVM *vm, const JSPandaFile *jsPandaFile); 83 void ClearNameMap(); 84 private: 85 JSPandaFileManager() = default; 86 87 class JSPandaFileAllocator { 88 public: 89 static void *AllocateBuffer(size_t size); 90 static void FreeBuffer(void *mem); 91 }; 92 93 std::shared_ptr<JSPandaFile> GenerateJSPandaFile(JSThread *thread, const panda_file::File *pf, const CString &desc, 94 std::string_view entryPoint); 95 std::shared_ptr<JSPandaFile> GetJSPandaFile(const panda_file::File *pf); 96 std::shared_ptr<JSPandaFile> FindJSPandaFileWithChecksum(const CString &filename, uint32_t checksum); 97 std::shared_ptr<JSPandaFile> FindJSPandaFileUnlocked(const CString &filename); 98 void InsertJSPandaFileVmUnlocked(const EcmaVM *vm, const std::shared_ptr<JSPandaFile> &jsPandaFile); 99 void ObsoleteLoadedJSPandaFile(const CString &filename); 100 101 static void *AllocateBuffer(size_t size); 102 static void FreeBuffer(void *mem); 103 104 RecursiveMutex jsPandaFileLock_; 105 // JSPandaFile was hold by ecma vm list. 106 using JSPandaFileVmsPair = std::pair<std::shared_ptr<JSPandaFile>, std::set<const EcmaVM *>>; 107 std::unordered_map<const CString, JSPandaFileVmsPair, CStringHash> loadedJSPandaFiles_; 108 // for plugin update. 109 std::unordered_map<std::shared_ptr<JSPandaFile>, std::set<const EcmaVM *>> oldJSPandaFiles_; 110 std::unordered_map<const JSPandaFile *, std::unique_ptr<DebugInfoExtractor>> extractors_; 111 112 friend class JSPandaFile; 113 }; 114 } // namespace ecmascript 115 } // namespace panda 116 #endif // ECMASCRIPT_JSPANDAFILE_JS_PANDAFILE_MANAGER_H 117