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 16 #ifndef ECMASCRIPT_TOOLING_INTERFACE_JS_DEBUGGER_MANAGER_H 17 #define ECMASCRIPT_TOOLING_INTERFACE_JS_DEBUGGER_MANAGER_H 18 19 #include "ecmascript/debugger/hot_reload_manager.h" 20 #include "ecmascript/debugger/notification_manager.h" 21 #include "ecmascript/ecma_vm.h" 22 #include "ecmascript/interpreter/frame_handler.h" 23 #include "ecmascript/js_thread.h" 24 #include "ecmascript/napi/include/jsnapi.h" 25 26 #include "libpandabase/os/library_loader.h" 27 28 namespace panda::ecmascript::tooling { 29 class ProtocolHandler; 30 class JsDebuggerManager { 31 public: 32 using LibraryHandle = os::library_loader::LibraryHandle; 33 using ObjectUpdaterFunc = 34 std::function<void(const FrameHandler *, std::string_view, Local<JSValueRef>)>; 35 using SingleStepperFunc = std::function<void()>; 36 JsDebuggerManager(const EcmaVM * vm)37 JsDebuggerManager(const EcmaVM *vm) : hotReloadManager_(vm) 38 { 39 jsThread_ = vm->GetJSThread(); 40 } 41 ~JsDebuggerManager() = default; 42 43 NO_COPY_SEMANTIC(JsDebuggerManager); 44 NO_MOVE_SEMANTIC(JsDebuggerManager); 45 GetNotificationManager()46 NotificationManager *GetNotificationManager() const 47 { 48 return const_cast<NotificationManager *>(¬ificationManager_); 49 } 50 GetHotReloadManager()51 HotReloadManager *GetHotReloadManager() const 52 { 53 return const_cast<HotReloadManager *>(&hotReloadManager_); 54 } 55 SetDebugMode(bool isDebugMode)56 void SetDebugMode(bool isDebugMode) 57 { 58 if (isDebugMode_ == isDebugMode) { 59 return; 60 } 61 62 isDebugMode_ = isDebugMode; 63 } 64 IsDebugMode()65 bool IsDebugMode() const 66 { 67 return isDebugMode_; 68 } 69 SetMixedDebugEnabled(bool enabled)70 void SetMixedDebugEnabled(bool enabled) 71 { 72 isMixedDebugEnabled_ = enabled; 73 } 74 IsMixedDebugEnabled()75 bool IsMixedDebugEnabled() const 76 { 77 return isMixedDebugEnabled_; 78 } 79 SetDebuggerHandler(ProtocolHandler * debuggerHandler)80 void SetDebuggerHandler(ProtocolHandler *debuggerHandler) 81 { 82 debuggerHandler_ = debuggerHandler; 83 } 84 GetDebuggerHandler()85 ProtocolHandler *GetDebuggerHandler() const 86 { 87 return debuggerHandler_; 88 } 89 SetDebugLibraryHandle(LibraryHandle handle)90 void SetDebugLibraryHandle(LibraryHandle handle) 91 { 92 debuggerLibraryHandle_ = std::move(handle); 93 } 94 GetDebugLibraryHandle()95 const LibraryHandle &GetDebugLibraryHandle() const 96 { 97 return debuggerLibraryHandle_; 98 } 99 SetEvalFrameHandler(std::shared_ptr<FrameHandler> frameHandler)100 void SetEvalFrameHandler(std::shared_ptr<FrameHandler> frameHandler) 101 { 102 frameHandler_ = frameHandler; 103 } 104 GetEvalFrameHandler()105 const std::shared_ptr<FrameHandler> &GetEvalFrameHandler() const 106 { 107 return frameHandler_; 108 } 109 SetLocalScopeUpdater(ObjectUpdaterFunc * updaterFunc)110 void SetLocalScopeUpdater(ObjectUpdaterFunc *updaterFunc) 111 { 112 updaterFunc_ = updaterFunc; 113 } 114 NotifyLocalScopeUpdated(std::string_view varName,Local<JSValueRef> value)115 void NotifyLocalScopeUpdated(std::string_view varName, Local<JSValueRef> value) 116 { 117 if (updaterFunc_ != nullptr) { 118 (*updaterFunc_)(frameHandler_.get(), varName, value); 119 } 120 } 121 SetStepperFunc(SingleStepperFunc * stepperFunc)122 void SetStepperFunc(SingleStepperFunc *stepperFunc) 123 { 124 stepperFunc_ = stepperFunc; 125 } 126 ClearSingleStepper()127 void ClearSingleStepper() 128 { 129 if (stepperFunc_ != nullptr) { 130 (*stepperFunc_)(); 131 } 132 } 133 134 private: 135 bool isDebugMode_ {false}; 136 bool isMixedDebugEnabled_ { false }; 137 ProtocolHandler *debuggerHandler_ {nullptr}; 138 LibraryHandle debuggerLibraryHandle_ {nullptr}; 139 ObjectUpdaterFunc *updaterFunc_ {nullptr}; 140 SingleStepperFunc *stepperFunc_ {nullptr}; 141 JSThread *jsThread_ {nullptr}; 142 std::shared_ptr<FrameHandler> frameHandler_; 143 144 NotificationManager notificationManager_; 145 HotReloadManager hotReloadManager_; 146 }; 147 } // panda::ecmascript::tooling 148 149 #endif // ECMASCRIPT_TOOLING_INTERFACE_JS_DEBUGGER_MANAGER_H