• 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_H
17 #define ECMASCRIPT_JSPANDAFILE_JS_PANDAFILE_H
18 
19 #include "ecmascript/class_linker/panda_file_translator.h"
20 #include "ecmascript/jspandafile/constpool_value.h"
21 #include "ecmascript/mem/c_containers.h"
22 #include "ecmascript/tooling/backend/js_pt_extractor.h"
23 #include "libpandafile/file.h"
24 #include "libpandabase/utils/logger.h"
25 
26 namespace panda {
27 namespace panda_file {
28 class File;
29 }  // namespace panda_file
30 
31 namespace ecmascript {
32 #define ENTRY_FUNCTION_NAME "func_main_0"
33 
34 class JSPandaFile {
35 public:
36     JSPandaFile(const panda_file::File *pf, const CString &descriptor);
37     ~JSPandaFile();
38 
39     tooling::JSPtExtractor *GetJSPtExtractor();
40 
GetJSPandaFileDesc()41     const CString &GetJSPandaFileDesc() const
42     {
43         return desc_;
44     }
45 
GetPandaFile()46     const panda_file::File *GetPandaFile() const
47     {
48         return pf_;
49     }
50 
GetMethods()51     JSMethod *GetMethods() const
52     {
53         return methods_;
54     }
55 
SetMethodToMap(JSMethod * method)56     void SetMethodToMap(JSMethod *method)
57     {
58         if (method != nullptr) {
59             methodMap_.emplace(method->GetFileId().GetOffset(), method);
60         }
61     }
62 
GetNumMethods()63     uint32_t GetNumMethods() const
64     {
65         return numMethods_;
66     }
67 
GetConstpoolIndex()68     uint32_t GetConstpoolIndex() const
69     {
70         return constpoolIndex_;
71     }
72 
GetMainMethodIndex()73     uint32_t GetMainMethodIndex() const
74     {
75         return mainMethodIndex_;
76     }
77 
GetConstpoolMap()78     const std::unordered_map<uint32_t, uint64_t> &GetConstpoolMap() const
79     {
80         return constpoolMap_;
81     }
82 
83     uint32_t GetOrInsertConstantPool(ConstPoolType type, uint32_t offset);
84 
UpdateMainMethodIndex(uint32_t mainMethodIndex)85     void UpdateMainMethodIndex(uint32_t mainMethodIndex)
86     {
87         mainMethodIndex_ = mainMethodIndex;
88     }
89 
90     const JSMethod *FindMethods(uint32_t offset) const;
91 
GetClasses()92     Span<const uint32_t> GetClasses() const
93     {
94         return pf_->GetClasses();
95     }
96 
97 private:
98     void InitMethods();
99 
100     uint32_t constpoolIndex_ {0};
101     std::unordered_map<uint32_t, uint64_t> constpoolMap_;
102     uint32_t numMethods_ {0};
103     uint32_t mainMethodIndex_ {0};
104     JSMethod *methods_ {nullptr};
105     CUnorderedMap<uint32_t, JSMethod *> methodMap_;
106     const panda_file::File *pf_ {nullptr};
107     std::unique_ptr<tooling::JSPtExtractor> JSPtExtractor_;
108     CString desc_;
109 };
110 }  // namespace ecmascript
111 }  // namespace panda
112 #endif // ECMASCRIPT_JSPANDAFILE_JS_PANDAFILE_H
113