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#include "tooling/client/manager/stack_manager.h"
17#include "common/log_wrapper.h"
18#include "tooling/client/session/session.h"
19
20namespace OHOS::ArkCompiler::Toolchain {
21void StackManager::SetCallFrames(std::map<int32_t, std::unique_ptr<CallFrame>> callFrames)
22{
23    for (auto &callFrame : callFrames) {
24        callFrames_[callFrame.first] = std::move(callFrame.second);
25    }
26}
27
28void StackManager::ShowCallFrames()
29{
30    std::cout << std::endl;
31    for (const auto &callFrame : callFrames_) {
32        if (callFrame.second->GetFunctionName().empty()) {
33            callFrame.second->SetFunctionName("<anonymous function>");
34        }
35        std::cout << callFrame.first << ". " << callFrame.second->GetFunctionName() << "(), "
36                  << callFrame.second->GetUrl() << ": " << callFrame.second->GetLocation()->GetLine() << std::endl;
37    }
38}
39
40std::map<int32_t, std::map<int32_t, std::string>> StackManager::GetScopeChainInfo()
41{
42    std::map<int32_t, std::map<int32_t, std::string>> scopeInfos;
43    for (const auto &callFram : callFrames_) {
44        int32_t callFramId = callFram.second->GetCallFrameId();
45        for (const auto &scope : *(callFram.second->GetScopeChain())) {
46            scopeInfos[callFramId][scope->GetObject()->GetObjectId()] = scope->GetType();
47        }
48    }
49    return scopeInfos;
50}
51
52void StackManager::ClearCallFrame()
53{
54    callFrames_.clear();
55}
56
57void StackManager::PrintScopeChainInfo(const std::map<int32_t, std::map<int32_t, std::string>>& scopeInfos)
58{
59    for (const auto& [callFrameId, scopes] : scopeInfos) {
60        std::cout << "CallFrame ID: " << callFrameId << std::endl;
61        for (const auto& [objectId, type] : scopes) {
62            std::cout << "  Object ID: " << objectId << ", Type: " << type << std::endl;
63        }
64        std::cout << "-----------------------" << std::endl;
65    }
66}
67}