• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
27 using PtJson = panda::ecmascript::tooling::PtJson;
28 using Result = panda::ecmascript::tooling::Result;
29 using PropertyDescriptor = panda::ecmascript::tooling::PropertyDescriptor;
30 using GetHeapUsageReturns = panda::ecmascript::tooling::GetHeapUsageReturns;
31 using PropertyDescriptor = panda::ecmascript::tooling::PropertyDescriptor;
32 using DescriptorMap = std::map<int32_t, std::unique_ptr<PropertyDescriptor>>;
33 using NodeData = std::variant<int32_t, std::map<int32_t, std::map<int32_t, std::string>>,
34                               std::unique_ptr<PropertyDescriptor>, DescriptorMap>;
35 namespace OHOS::ArkCompiler::Toolchain {
36 class TreeNode {
37 public:
38     NodeData data;
39     std::vector<std::unique_ptr<TreeNode>> children;
40 
TreeNode(int32_t CallFrameId)41     TreeNode(int32_t CallFrameId) : data(CallFrameId) {}
TreeNode(const std::map<int32_t,std::map<int32_t,std::string>> & scopeInfo)42     TreeNode(const std::map<int32_t, std::map<int32_t, std::string>>& scopeInfo) : data(scopeInfo) {}
TreeNode(std::unique_ptr<PropertyDescriptor> descriptor)43     TreeNode(std::unique_ptr<PropertyDescriptor> descriptor) : data(std::move(descriptor)) {}
TreeNode(DescriptorMap && descriptorMap)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 
52 class Tree {
53 public:
Tree(int32_t rootValue)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 
67 private:
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 
75 class VariableManager final {
76 public:
VariableManager(int32_t sessionId)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 
90 private:
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