1 /* 2 * Copyright (c) 2024 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 METADATA_JSON_PARSE_H 17 #define METADATA_JSON_PARSE_H 18 19 #include "cJSON.h" 20 #include "ecmascript/dfx/hprof/rawheap_translate/common.h" 21 #include "ecmascript/dfx/hprof/rawheap_translate/utils.h" 22 23 namespace rawheap_translate { 24 class MetaParser { 25 public: 26 MetaParser() = default; ~MetaParser()27 ~MetaParser() 28 { 29 for (auto &meta : orderedMeta_) { 30 delete meta; 31 } 32 meta_.clear(); 33 orderedMeta_.clear(); 34 } 35 36 bool Parse(const cJSON *object); 37 JSType GetJSTypeFromHClass(Node *hclass); 38 JSType GetJSTypeFromTypeName(const std::string &name); 39 NodeType GetNodeType(JSType type); 40 uint32_t GetNativateSize(Node *node, JSType type); 41 std::string GetTypeName(JSType type); 42 MetaData* GetMetaData(const std::string &name); 43 MetaData* GetMetaData(const JSType type); 44 bool IsNativePointer(JSType type); 45 bool IsString(JSType type); 46 bool IsDictionaryMode(JSType type); 47 bool IsJSObject(JSType type); 48 bool IsGlobalEnv(JSType type); 49 bool IsArray(JSType type); 50 GetBitField()51 BitField* GetBitField() 52 { 53 return &bitField_; 54 } 55 GetMetaVersion()56 const Version& GetMetaVersion() const 57 { 58 return version_; 59 } 60 61 private: 62 bool ParseTypeEnums(const cJSON *json); 63 bool ParseTypeList(const cJSON *json); 64 bool ParseTypeLayoutAndDesc(const cJSON *json); 65 bool ParseVersion(const cJSON *json); 66 void ParseParents(const cJSON *array, MetaData *metadata); 67 void ParseOffsets(const cJSON *array, MetaData *metadata); 68 void SetBitField(const std::string &metaName, const std::string &fieldName, Field &field); 69 void FillMetaData(MetaData *parent, MetaData *meta); 70 void GenerateMetaData(); 71 MetaData* FindOrCreateMetaData(const std::string &name); 72 73 static void IterateJSONArray(const cJSON *array, const std::function<void(const cJSON *)> &visitor); 74 static bool GetArray(const cJSON *json, const char *key, cJSON **value); 75 static bool GetString(const cJSON *json, const char *key, std::string &value); 76 static bool GetString(const cJSON *json, std::string &value); 77 static bool GetUInt32(const cJSON *json, const char *key, uint32_t &value); 78 static bool GetUInt32(const cJSON *json, uint32_t &value); 79 80 Version version_; 81 std::unordered_map<std::string, MetaData *> meta_ {}; 82 std::vector<MetaData *> orderedMeta_ {}; 83 DictionaryLayout dictionaryLayout_; 84 TypeRange typeRange_; 85 BitField bitField_; 86 }; 87 } // namespace rawheap_translate 88 #endif // METADATA_JSON_PARSE_H 89