55 * LICENSE file in the root directory of this source tree.
66 */
77
8- #ifndef HERMES_ASYNCDEBUGGERAPI_H
9- #define HERMES_ASYNCDEBUGGERAPI_H
8+ #pragma once
109
1110#ifdef HERMES_ENABLE_DEBUGGER
1211
@@ -68,8 +67,6 @@ using DebuggerEventCallback = std::function<void(
6867 HermesRuntime &runtime,
6968 AsyncDebuggerAPI &asyncDebugger,
7069 DebuggerEventType event)>;
71- using DebuggerEventCallbackID = uint32_t ;
72- constexpr const uint32_t kInvalidDebuggerEventCallbackID = 0 ;
7370using InterruptCallback = std::function<void (HermesRuntime &runtime)>;
7471using EvalCompleteCallback = std::function<
7572 void (HermesRuntime &runtime, const debugger::EvalResult &result)>;
@@ -82,36 +79,34 @@ using EvalCompleteCallback = std::function<
8279// / functions that are safe to call on any thread. All other functions must be
8380// / called on the runtime thread.
8481class HERMES_EXPORT AsyncDebuggerAPI : private debugger::EventObserver {
85- // / Hide the constructor so users can only construct via static create
86- // / methods.
87- AsyncDebuggerAPI (HermesRuntime &runtime);
88-
8982 public:
90- // / Creates an AsyncDebuggerAPI for use with the provided HermesRuntime. This
91- // / should be called and created at the same time as creating HermesRuntime.
92- static std::unique_ptr<AsyncDebuggerAPI> create (HermesRuntime &runtime);
83+ // / Constructs an AsyncDebuggerAPI for use with the provided HermesRuntime.
84+ // / This should be called and created at the same time as creating
85+ // / HermesRuntime.
86+ explicit AsyncDebuggerAPI (HermesRuntime &runtime);
9387
9488 // / Must be destroyed on the runtime thread or when you're sure nothing is
9589 // / interacting with the runtime. Must be destroyed before destroying
9690 // / HermesRuntime.
9791 ~AsyncDebuggerAPI () override ;
9892
99- // / Add a callback function to invoke when the runtime pauses due to various
93+ // / Set a callback function to invoke when the runtime pauses due to various
10094 // / conditions such as hitting a "debugger;" statement. Can be called from any
101- // / thread. If there are no DebuggerEventCallback, then any reason that might
95+ // / thread. If there is no DebuggerEventCallback, then any reason that might
10296 // / trigger a pause, such as a "debugger;" statement or breakpoints, will not
103- // / actually pause and will simply continue execution. Any caller that adds an
97+ // / actually pause and will simply continue execution. Any caller that sets an
10498 // / event callback cannot just be observing events and never call
10599 // / \p resumeFromPaused in any of its code paths. The caller must either
106100 // / expose UI enabling human action for controlling the debugger, or it must
107101 // / have programmatic logic that controls the debugger via
108102 // / \p resumeFromPaused.
109- DebuggerEventCallbackID addDebuggerEventCallback_TS (
110- DebuggerEventCallback callback);
103+ // /
104+ // / The provided callback must be non-empty. Use \p
105+ // / clearDebuggerEventCallback_TS to clear the callback.
106+ void setDebuggerEventCallback_TS (DebuggerEventCallback callback);
111107
112- // / Remove a previously added callback function. If there is no callback
113- // / registered using the provided \p id, the function does nothing.
114- void removeDebuggerEventCallback_TS (DebuggerEventCallbackID id);
108+ // / Clear the debugger event callback. Can be called from any thread.
109+ void clearDebuggerEventCallback_TS ();
115110
116111 // / Whether the runtime is currently paused waiting for the next action.
117112 // / Should only be called from the runtime thread.
@@ -120,7 +115,7 @@ class HERMES_EXPORT AsyncDebuggerAPI : private debugger::EventObserver {
120115 // / Whether the runtime is currently paused for any reason (e.g. script
121116 // / parsed, running interrupts, or waiting for a command).
122117 // / Should only be called from the runtime thread.
123- bool isPaused ();
118+ bool isPaused () const ;
124119
125120 // / Provide the next action to perform. Should only be called from the runtime
126121 // / thread and only if the next command is expected to be set.
@@ -145,11 +140,6 @@ class HERMES_EXPORT AsyncDebuggerAPI : private debugger::EventObserver {
145140 debugger::Command didPause (debugger::Debugger &debugger) override ;
146141
147142 private:
148- struct EventCallbackEntry {
149- DebuggerEventCallbackID id;
150- DebuggerEventCallback callback;
151- };
152-
153143 // / This function infinite loops and uses \p signal_ to block the runtime
154144 // / thread. It gets woken up if new InterruptCallback is queued or if
155145 // / DebuggerEventCallback changes.
@@ -163,11 +153,8 @@ class HERMES_EXPORT AsyncDebuggerAPI : private debugger::EventObserver {
163153 // / to run all interrupts, but will stop if any interrupt sets a next command.
164154 void runInterrupts (bool ignoreNextCommand = true );
165155
166- // / Returns the next DebuggerEventCallback to execute if any.
167- std::optional<DebuggerEventCallback> takeNextEventCallback ();
168-
169- // / Runs every DebuggerEventCallback that has been registered.
170- void runEventCallbacks (DebuggerEventType event);
156+ // / Runs the DebuggerEventCallback that has been registered (if any).
157+ void runEventCallback (DebuggerEventType event);
171158
172159 HermesRuntime &runtime_;
173160
@@ -186,19 +173,8 @@ class HERMES_EXPORT AsyncDebuggerAPI : private debugger::EventObserver {
186173 // / calls to didPause.
187174 bool inDidPause_ = false ;
188175
189- // / Next ID to use when adding a DebuggerEventCallback.
190- uint32_t nextEventCallbackID_ TSA_GUARDED_BY (mutex_);
191-
192- // / Callback functions to invoke to notify events in \p didPause. Using
193- // / std::list which requires O(N) search when removing an element, but removal
194- // / should be a rare event. So the choice of using std::list is to optimize
195- // / for typical usage.
196- std::list<EventCallbackEntry> eventCallbacks_ TSA_GUARDED_BY (mutex_){};
197-
198- // / Iterator for eventCallbacks_. Used to traverse through the list when
199- // / running the callbacks.
200- std::list<EventCallbackEntry>::iterator eventCallbackIterator_
201- TSA_GUARDED_BY (mutex_);
176+ // / The debugger event callback to invoke when the runtime pauses.
177+ DebuggerEventCallback eventCallback_ TSA_GUARDED_BY (mutex_){};
202178
203179 // / Queue of interrupt callback functions to invoke.
204180 std::queue<InterruptCallback> interruptCallbacks_ TSA_GUARDED_BY (mutex_){};
@@ -257,32 +233,25 @@ using DebuggerEventCallback = std::function<void(
257233 HermesRuntime &runtime,
258234 AsyncDebuggerAPI &asyncDebugger,
259235 DebuggerEventType event)>;
260- using DebuggerEventCallbackID = uint32_t ;
261- constexpr const uint32_t kInvalidDebuggerEventCallbackID = 0 ;
262236using InterruptCallback = std::function<void (HermesRuntime &runtime)>;
263237using EvalCompleteCallback = std::function<
264238 void (HermesRuntime &runtime, const debugger::EvalResult &result)>;
265239
266240class HERMES_EXPORT AsyncDebuggerAPI {
267241 public:
268- static std::unique_ptr<AsyncDebuggerAPI> create (HermesRuntime &runtime) {
269- return nullptr ;
270- }
242+ explicit AsyncDebuggerAPI (HermesRuntime &) {}
271243
272244 ~AsyncDebuggerAPI () {}
273245
274- DebuggerEventCallbackID addDebuggerEventCallback_TS (
275- DebuggerEventCallback callback) {
276- return kInvalidDebuggerEventCallbackID ;
277- }
246+ void setDebuggerEventCallback_TS (DebuggerEventCallback callback) {}
278247
279- void removeDebuggerEventCallback_TS (DebuggerEventCallbackID id ) {}
248+ void clearDebuggerEventCallback_TS ( ) {}
280249
281250 bool isWaitingForCommand () {
282251 return false ;
283252 }
284253
285- bool isPaused () {
254+ bool isPaused () const {
286255 return false ;
287256 }
288257
@@ -305,5 +274,3 @@ class HERMES_EXPORT AsyncDebuggerAPI {
305274} // namespace facebook
306275
307276#endif // !HERMES_ENABLE_DEBUGGER
308-
309- #endif // HERMES_ASYNCDEBUGGERAPI_H
0 commit comments