1/*
2 * Copyright (c) 2022 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#ifndef ECMASCRIPT_PATCH_QUICK_FIX_MANAGER_H
16#define ECMASCRIPT_PATCH_QUICK_FIX_MANAGER_H
17
18#include "ecmascript/napi/include/jsnapi.h"
19#include "ecmascript/patch/patch_loader.h"
20#include "ecmascript/module/js_module_source_text.h"
21
22namespace panda::ecmascript {
23using PatchErrorCode = panda::JSNApi::PatchErrorCode;
24class QuickFixManager {
25public:
26    QuickFixManager() = default;
27    ~QuickFixManager();
28
29    void RegisterQuickFixQueryFunc(const std::function<bool(std::string baseFileName,
30                        std::string &patchFileName,
31                        uint8_t **patchBuffer,
32                        size_t &patchSize)> callBack);
33    void LoadPatchIfNeeded(JSThread *thread, const JSPandaFile *baseFile);
34    PatchErrorCode LoadPatch(JSThread *thread, const std::string &patchFileName, const std::string &baseFileName);
35    PatchErrorCode LoadPatch(JSThread *thread,
36                             const std::string &patchFileName, uint8_t *patchBuffer, size_t patchSize,
37                             const std::string &baseFileName, uint8_t *baseBuffer, size_t baseSize);
38    PatchErrorCode UnloadPatch(JSThread *thread, const std::string &patchFileName);
39    bool IsQuickFixCausedException(JSThread *thread,
40                                   const JSHandle<JSTaggedValue> &exceptionInfo,
41                                   const std::string &patchFileName);
42    JSTaggedValue CheckAndGetPatch(JSThread *thread, const JSPandaFile *baseFile, EntityId baseMethodId);
43    CString GetBaseFileName(const JSHandle<SourceTextModule> &module);
44private:
45    // check whether the callback is registered.
46    bool HasQueryQuickFixInfoFunc() const
47    {
48        return callBack_ != nullptr;
49    }
50
51    CUnorderedSet<CString> ParseStackInfo(const CString &stackInfo);
52
53    // key: baseFileName
54    CMap<CString, PatchInfo> methodInfos_ {};
55    std::function<bool(std::string baseFileName,
56                        std::string &patchFileName,
57                        uint8_t **patchBuffer,
58                        size_t &patchSize)> callBack_;
59    CMap<uint32_t, CString> baseClassInfo_ {};
60    // key: patch file name, value: base file name
61    CMap<CString, CString> patchAndBaseFileNameMap_;
62    std::set<CString> checkedFiles_ {};
63};
64}  // namespace panda::ecmascript
65#endif // ECMASCRIPT_PATCH_QUICK_FIX_MANAGER_H