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 21 namespace panda::ecmascript { 22 using PatchErrorCode = panda::JSNApi::PatchErrorCode; 23 class QuickFixManager { 24 public: 25 QuickFixManager() = default; 26 ~QuickFixManager(); 27 28 void RegisterQuickFixQueryFunc(const std::function<bool(std::string baseFileName, 29 std::string &patchFileName, 30 void **patchBuffer, 31 size_t &patchSize)> callBack); 32 void LoadPatchIfNeeded(JSThread *thread, const JSPandaFile *baseFile); 33 PatchErrorCode LoadPatch(JSThread *thread, const std::string &patchFileName, const std::string &baseFileName); 34 PatchErrorCode LoadPatch(JSThread *thread, 35 const std::string &patchFileName, const void *patchBuffer, size_t patchSize, 36 const std::string &baseFileName, const void *baseBuffer, size_t baseSize); 37 PatchErrorCode UnloadPatch(JSThread *thread, const std::string &patchFileName); 38 bool IsQuickFixCausedException(JSThread *thread, 39 const JSHandle<JSTaggedValue> &exceptionInfo, 40 const std::string &patchFileName); 41 JSTaggedValue CheckAndGetPatch(JSThread *thread, const JSPandaFile *baseFile, EntityId baseMethodId); 42 43 private: 44 // check whether the callback is registered. HasQueryQuickFixInfoFunc()45 bool HasQueryQuickFixInfoFunc() const 46 { 47 return callBack_ != nullptr; 48 } 49 50 CUnorderedSet<CString> ParseStackInfo(const CString &stackInfo); 51 52 // key: baseFileName 53 CMap<CString, PatchInfo> methodInfos_ {}; 54 std::function<bool(std::string baseFileName, 55 std::string &patchFileName, 56 void **patchBuffer, 57 size_t &patchSize)> callBack_; 58 }; 59 } // namespace panda::ecmascript 60 #endif // ECMASCRIPT_PATCH_QUICK_FIX_MANAGER_H