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