1/*
2 * Copyright (c) 2023-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 ECMASCRIPT_DEBUGGER_DROPFRAME_MANAGER_H
17#define ECMASCRIPT_DEBUGGER_DROPFRAME_MANAGER_H
18
19#include "ecmascript/js_handle.h"
20#include "ecmascript/js_thread.h"
21#include "ecmascript/method.h"
22#include "libpandafile/bytecode_instruction.h"
23
24namespace panda::ecmascript::tooling {
25enum class MethodType : uint8_t {
26    OTHER_METHOD = 0,
27    NORMAL_METHOD,
28    SENDABLE_METHOD
29};
30
31class DropframeManager {
32public:
33    DropframeManager() = default;
34    ~DropframeManager() = default;
35    NO_COPY_SEMANTIC(DropframeManager);
36    NO_MOVE_SEMANTIC(DropframeManager);
37
38    void MethodEntry(JSThread *thread, JSHandle<Method> method, JSHandle<JSTaggedValue> env);
39    void MethodExit(JSThread *thread, JSHandle<Method> method);
40    void DropLastFrame(JSThread *thread);
41    uint32_t GetPromiseQueueSizeRecordOfTopFrame();
42    bool CheckIsSendableMethod();
43private:
44    bool IsNewlexenvOpcode(BytecodeInstruction::Opcode op);
45    bool IsStlexvarOpcode(BytecodeInstruction::Opcode op);
46    std::pair<uint16_t, uint16_t> ReadStlexvarParams(const uint8_t *pc, BytecodeInstruction::Opcode op);
47
48    void NewLexModifyRecordLevel();
49    void EmplaceLexModifyRecord(JSThread *thread, JSTaggedValue env, uint16_t slot, JSTaggedValue value);
50    uint32_t GetLexModifyRecordLevel();
51    std::vector<std::tuple<JSHandle<JSTaggedValue>, uint16_t, JSHandle<JSTaggedValue>>> GetLexModifyRecordOfTopFrame();
52    void RemoveLexModifyRecordOfTopFrame(JSThread *thread);
53    void MergeLexModifyRecordOfTopFrame(JSThread *thread);
54
55    void PushPromiseQueueSizeRecord(JSThread *thread);
56    void PopPromiseQueueSizeRecord();
57
58    void PushMethodInfo(std::tuple<JSPandaFile*, panda_file::File::EntityId> methodInfo);
59    bool CheckExitMethodInfo(std::tuple<JSPandaFile*, panda_file::File::EntityId> methodInfo);
60    void PopMethodInfo();
61
62    void PushMethodType(MethodType  methodType);
63    void PopMethodType();
64
65    void AddLexPropertiesToRecord(JSThread *thread, BytecodeInstruction &bcIns, uint16_t &newEnvCount,
66        std::set<std::pair<uint16_t, uint16_t>> &modifiedLexVarPos, JSHandle<JSTaggedValue> envHandle);
67
68    std::stack<std::vector<std::tuple<JSHandle<JSTaggedValue>, uint16_t, JSHandle<JSTaggedValue>>>> modifiedLexVar_;
69    std::stack<uint32_t> promiseQueueSizeRecord_;
70    std::stack<MethodType> methodType_;
71    std::stack<std::tuple<JSPandaFile*, panda_file::File::EntityId>> methodInfo_;
72
73    friend class DropframeManagerFriendTest;
74};
75}
76
77#endif  // ECMASCRIPT_DEBUGGER_DROPFRAME_MANAGER_H