/* * Copyright (c) 2024 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 METADATA_JSON_PARSE_H #define METADATA_JSON_PARSE_H #include "cJSON.h" #include "ecmascript/dfx/hprof/rawheap_translate/utils.h" namespace rawheap_translate { struct MetaData; using JSType = uint8_t; using NodeType = uint8_t; using ObjRangeVisitor = std::function &, uint32_t)>; struct Field { std::string name; uint32_t offset; uint32_t size; Field() : offset(0), size(0) {} }; struct MetaData { std::string name; std::vector> fields; uint32_t endOffset; std::vector parents; std::string visitType; MetaData() : endOffset(0) {} bool IsArray() { return visitType == "Array"; } }; struct Layout { std::string name; uint32_t keyIndex; uint32_t valueIndex; uint32_t detailIndex; uint32_t entrySize; uint32_t headerSize; Layout() : keyIndex(0), valueIndex(0), detailIndex(0), entrySize(0), headerSize(0) {} }; class Meta { public: Meta() = default; ~Meta() { metadata_.clear(); enumsMapJSType_.clear(); enumsMapNodeType_.clear(); layout_.clear(); typeDesc_.clear(); enumsVec_.clear(); } bool Parse(const cJSON *object); void VisitObjectBody(const std::string &name, const ObjRangeVisitor &visitor, uint32_t &baseOffset); uint32_t GetNativateSize(char *obj, char *hclass); NodeType GetNodeTypeFromHClass(char *hclass); std::string GetTypeNameFromHClass(char *hclass); std::shared_ptr GetObjectLayout(const std::string &name); bool IsString(char *hclass); bool IsDictionaryMode(char *hclass); bool IsJSObject(char *hclass); bool IsGlobalEnv(char *hclass); private: bool ParseTypeEnums(const cJSON *json); bool ParseTypeList(const cJSON *json); bool ParseTypeLayout(const cJSON *json); bool ParseVersion(const cJSON *json); bool SetObjTypeBitFieldOffset(); bool SetNativatePointerBindingSizeOffset(); std::shared_ptr GetMetaData(const std::string &name); JSType GetObjTypeFromHClass(char *hclass); JSType GetObjTypeFromTypeName(const std::string &name); std::string GetTypeDesc(const std::string &name); static void IterateJSONArray(const cJSON *array, const std::function &visitor); static bool GetArray(const cJSON *json, const char *key, cJSON **value); static bool GetString(const cJSON *json, const char *key, std::string &value); static bool GetString(const cJSON *json, std::string &value); static bool GetUInt32(const cJSON *json, const char *key, uint32_t &value); static bool GetUInt32(const cJSON *json, uint32_t &value); static constexpr NodeType DEFAULT_NODETYPE = 8; std::unordered_map> metadata_ {}; std::unordered_map enumsMapJSType_ {}; std::unordered_map enumsMapNodeType_ {}; std::unordered_map> layout_ {}; std::unordered_map typeDesc_ {}; std::vector enumsVec_ {}; uint32_t objTypeBitFieldOffset_ {0}; uint32_t objTypeBitFieldSize_ {0}; uint32_t nativatePointerBindingSizeOffset_ {0}; uint32_t nativatePointerBindingSize_ {0}; }; } // namespace rawheap_translate #endif // METADATA_JSON_PARSE_H