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