/* * Copyright (c) 2022 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #ifndef ECMASCRIPT_JSPANDAFILE_JS_PANDAFILE_MANAGER_H #define ECMASCRIPT_JSPANDAFILE_JS_PANDAFILE_MANAGER_H #include "ecmascript/jspandafile/js_pandafile.h" #include "ecmascript/jspandafile/panda_file_translator.h" #include "ecmascript/jspandafile/debug_info_extractor.h" #include "ecmascript/platform/mutex.h" namespace panda { namespace ecmascript { class Program; class PUBLIC_API JSPandaFileManager { public: static JSPandaFileManager *GetInstance(); ~JSPandaFileManager(); JSHandle GenerateProgram(EcmaVM *vm, const JSPandaFile *jsPandaFile, std::string_view entryPoint); template std::shared_ptr LoadJSPandaFile(JSThread *thread, const CString &filename, std::string_view entryPoint, bool needUpdate = false, const ExecuteTypes &executeType = ExecuteTypes::STATIC); std::shared_ptr LoadJSPandaFile(JSThread *thread, const CString &filename, std::string_view entryPoint, const void *buffer, size_t size, bool needUpdate = false); // load pandafile from secure mem std::shared_ptr LoadJSPandaFileSecure(JSThread *thread, const CString &filename, std::string_view entryPoint, uint8_t *buffer, size_t size, bool needUpdate = false, void *fileMapper = nullptr); std::shared_ptr LoadInsecureJSPandaFile(JSThread *thread, const CString &filename, std::string_view entryPoint); std::shared_ptr OpenJSPandaFile(const CString &filename); std::shared_ptr OpenJSPandaFile(const CString &filename, const CString &desc); std::shared_ptr OpenJSPandaFileFromBuffer(uint8_t *buffer, size_t size, const CString &filename); std::shared_ptr NewJSPandaFile(const panda_file::File *pf, const CString &desc); DebugInfoExtractor *GetJSPtExtractor(const JSPandaFile *jsPandaFile); DebugInfoExtractor *GetJSPtExtractorAndExtract(const JSPandaFile *jsPandaFile); DebugInfoExtractor *CpuProfilerGetJSPtExtractor(const JSPandaFile *jsPandaFile); template bool CheckFilePath(JSThread *thread, const CString &fileName); // for debugger template void EnumerateJSPandaFiles(Callback cb) { LockHolder lock(jsPandaFileLock_); for (const auto &item : loadedJSPandaFiles_) { if (!cb(item.second)) { return; } } for (const auto &item : oldJSPandaFiles_) { if (!cb(item)) { return; } } } template void EnumerateNonVirtualJSPandaFiles(Callback cb) { LockHolder lock(jsPandaFileLock_); for (const auto &item : loadedJSPandaFiles_) { if (!cb(item.second)) { return; } } for (const auto &item : oldJSPandaFiles_) { if (!cb(item)) { return; } } } std::shared_ptr FindJSPandaFileByNormalizedName(const CString &normalizedName); std::shared_ptr FindJSPandaFileByMapBase(uintptr_t mapBase); std::shared_ptr FindJSPandaFile(const CString &filename); void AddJSPandaFile(const std::shared_ptr &jsPandaFile); void RemoveJSPandaFile(const JSPandaFile *jsPandaFile); void ClearNameMap(); std::unordered_set> GetHapJSPandaFiles() { std::unordered_set> hapJSPandaFiles; LockHolder lock(jsPandaFileLock_); for (const auto &item : loadedJSPandaFiles_) { if (!item.second->IsBundlePack() && item.second->IsHapPath()) { hapJSPandaFiles.emplace(item.second); } } return hapJSPandaFiles; } private: JSPandaFileManager() = default; class JSPandaFileAllocator { public: static void *AllocateBuffer(size_t size); static void FreeBuffer(void *mem); }; std::shared_ptr GenerateJSPandaFile(JSThread *thread, const panda_file::File *pf, const CString &desc, std::string_view entryPoint, void *fileMapper = nullptr); std::shared_ptr GetJSPandaFile(const panda_file::File *pf); std::shared_ptr FindJSPandaFileWithChecksum(const CString &filename, uint32_t checksum); std::shared_ptr FindJSPandaFileUnlocked(const CString &filename); std::shared_ptr GenerateJSPandafileFromBufferCache(JSThread *thread, const CString &filename, std::string_view entryPoint); void ObsoleteLoadedJSPandaFile(const CString &filename); static void *AllocateBuffer(size_t size, bool isBundlePack, CreateMode mode); static void FreeBuffer(void *mem, size_t size, bool isBundlePack, CreateMode mode); static bool UseSnapshot(JSThread *thread, JSPandaFile *jsPandaFile); RecursiveMutex jsPandaFileLock_; // JSPandaFile was shared by all vm. std::unordered_map, CStringHash> loadedJSPandaFiles_; // for plugin update. std::set> oldJSPandaFiles_; std::unordered_map> extractors_; friend class JSPandaFile; }; } // namespace ecmascript } // namespace panda #endif // ECMASCRIPT_JSPANDAFILE_JS_PANDAFILE_MANAGER_H