1/*
2 * Copyright (c) 2023 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_TOOLING_CLIENT_MANAGER_VARIABLE_MANAGER_H
17#define ECMASCRIPT_TOOLING_CLIENT_MANAGER_VARIABLE_MANAGER_H
18
19#include <iostream>
20#include <map>
21
22#include "tooling/client/manager/stack_manager.h"
23#include "tooling/base/pt_json.h"
24#include "tooling/base/pt_returns.h"
25#include "tooling/base/pt_types.h"
26
27using PtJson = panda::ecmascript::tooling::PtJson;
28using Result = panda::ecmascript::tooling::Result;
29using PropertyDescriptor = panda::ecmascript::tooling::PropertyDescriptor;
30using GetHeapUsageReturns = panda::ecmascript::tooling::GetHeapUsageReturns;
31using PropertyDescriptor = panda::ecmascript::tooling::PropertyDescriptor;
32using DescriptorMap = std::map<int32_t, std::unique_ptr<PropertyDescriptor>>;
33using NodeData = std::variant<int32_t, std::map<int32_t, std::map<int32_t, std::string>>,
34                              std::unique_ptr<PropertyDescriptor>, DescriptorMap>;
35namespace OHOS::ArkCompiler::Toolchain {
36class TreeNode {
37public:
38    NodeData data;
39    std::vector<std::unique_ptr<TreeNode>> children;
40
41    TreeNode(int32_t CallFrameId) : data(CallFrameId) {}
42    TreeNode(const std::map<int32_t, std::map<int32_t, std::string>>& scopeInfo) : data(scopeInfo) {}
43    TreeNode(std::unique_ptr<PropertyDescriptor> descriptor) : data(std::move(descriptor)) {}
44    TreeNode(DescriptorMap&& descriptorMap) : data(std::move(descriptorMap)) {}
45
46    void AddChild(std::unique_ptr<PropertyDescriptor> descriptor);
47    void AddChild(DescriptorMap descriptorMap);
48    void AddChild(std::unique_ptr<TreeNode> child);
49    void Print(int depth = 0) const;
50};
51
52class Tree {
53public:
54    Tree(int32_t rootValue) : root_(std::make_unique<TreeNode>(rootValue)) {}
55    Tree(const std::map<int32_t, std::map<int32_t, std::string>>& dataMap, int32_t index);
56
57    void Clear();
58    void Print() const;
59    TreeNode* GetRoot() const;
60    TreeNode* FindNodeWithObjectId(int32_t objectId) const;
61    void AddVariableNode(TreeNode* parentNode, std::unique_ptr<PropertyDescriptor> descriptor);
62    void AddObjectNode(TreeNode* parentNode, std::unique_ptr<PropertyDescriptor> descriptor);
63    TreeNode* FindNodeWithCondition() const;
64    void PrintRootAndImmediateChildren() const;
65    int32_t FindObjectByIndex(int32_t index) const;
66
67private:
68    std::unique_ptr<TreeNode> root_ {};
69    int32_t index_ {1};
70    TreeNode* FindNodeWithObjectIdRecursive(TreeNode* node, int32_t objectId) const;
71    TreeNode* FindNodeWithInnerKeyZero(TreeNode* node) const;
72    int32_t FindObjectByIndexRecursive(const TreeNode* node, int32_t index) const;
73};
74
75class VariableManager final {
76public:
77    VariableManager(int32_t sessionId) : sessionId_(sessionId) {}
78    void SetHeapUsageInfo(std::unique_ptr<GetHeapUsageReturns> heapUsageReturns);
79    void ShowHeapUsageInfo() const;
80    void ShowVariableInfos() const;
81    void ClearVariableInfo();
82    void InitializeTree(std::map<int32_t, std::map<int32_t, std::string>> dataMap, int index = 0);
83    void PrintVariableInfo();
84    TreeNode* FindNodeWithObjectId(int32_t objectId);
85    int32_t FindObjectIdWithIndex(int index);
86    void AddVariableInfo(TreeNode *parentNode, std::unique_ptr<PropertyDescriptor> variableInfo);
87    TreeNode* FindNodeObjectZero();
88    void Printinfo() const;
89
90private:
91    [[maybe_unused]] int32_t sessionId_;
92    GetHeapUsageReturns heapUsageInfo_ {};
93    Tree variableInfo_ {0};
94    VariableManager(const VariableManager&) = delete;
95    VariableManager& operator=(const VariableManager&) = delete;
96};
97} // OHOS::ArkCompiler::Toolchain
98#endif