Skip to content

Commit

Permalink
refactor: execute eval in the same thread as lua
Browse files Browse the repository at this point in the history
  • Loading branch information
sssooonnnggg committed Dec 6, 2024
1 parent 9ae9917 commit 831e3b4
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 25 deletions.
2 changes: 1 addition & 1 deletion debugger/src/internal/breakpoint.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#include <internal/breakpoint.h>
#include <internal/log.h>
#include <internal/utils.h>
#include "internal/utils/lua_utils.h"
#include <internal/utils/lua_utils.h>

namespace luau::debugger {

Expand Down
67 changes: 49 additions & 18 deletions debugger/src/internal/debug_bridge.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,10 +99,7 @@ void DebugBridge::onDebugBreak(lua_State* L,

session_->send(event);

break_vm_ = L;
resume_ = false;
resume_cv_.wait(lock, [this] { return resume_; });
break_vm_ = nullptr;
mainThreadWait(L, lock);
}

std::string DebugBridge::stopReasonToString(BreakReason reason) const {
Expand Down Expand Up @@ -408,9 +405,12 @@ void DebugBridge::enableDebugStep(bool enable) {
}

void DebugBridge::resumeInternal() {
std::unique_lock<std::mutex> lock(break_mutex_);
DEBUGGER_LOG_INFO("[resume] Resume execution");
resume_ = true;
{
std::unique_lock<std::mutex> lock(break_mutex_);
DEBUGGER_LOG_INFO("[resume] Resume execution");
resume_ = true;
}

resume_cv_.notify_one();
}

Expand All @@ -422,17 +422,21 @@ ResponseOrError<EvaluateResponse> DebugBridge::evaluate(
if (!request.context.has_value())
return Error{"Evaluate request must have context"};

auto context = request.context.value();
DEBUGGER_LOG_INFO("[evaluate] Evaluate context: {}", context);

if (context == "repl")
return evaluateRepl(request);
else if (context == "watch")
return evaluateWatch(request);
else {
DEBUGGER_LOG_ERROR("[evaluate] Invalid evaluate context: {}", context);
return Error{"Invalid evaluate context"};
}
ResponseOrError<EvaluateResponse> response;
executeInMainThread([&] {
auto context = request.context.value();
DEBUGGER_LOG_INFO("[evaluate] Evaluate context: {}", context);

if (context == "repl")
response = evaluateRepl(request);
else if (context == "watch")
response = evaluateWatch(request);
else {
DEBUGGER_LOG_ERROR("[evaluate] Invalid evaluate context: {}", context);
response = Error{"Invalid evaluate context"};
}
});
return response;
}

ResponseOrError<EvaluateResponse> DebugBridge::evaluateRepl(
Expand Down Expand Up @@ -577,4 +581,31 @@ std::vector<StackFrame> DebugBridge::updateStackFrames() {
return frames;
}

void DebugBridge::mainThreadWait(lua_State* L,
std::unique_lock<std::mutex>& lock) {
break_vm_ = L;
resume_ = false;
while (!resume_) {
resume_cv_.wait(lock, [this] { return resume_ || main_fn_ != nullptr; });

// Execute main_fn_ if it's not empty
if (main_fn_ != nullptr) {
main_fn_();
main_fn_ = nullptr;
resume_cv_.notify_one();
}
}
break_vm_ = nullptr;
}

void DebugBridge::executeInMainThread(std::function<void()> fn) {
DEBUGGER_ASSERT(isDebugBreak());

// Fill the main_fn_ and wait for it to be executed
std::unique_lock<std::mutex> lock(break_mutex_);
main_fn_ = std::move(fn);
resume_cv_.notify_one();
resume_cv_.wait(lock, [this] { return main_fn_ == nullptr; });
}

} // namespace luau::debugger
5 changes: 4 additions & 1 deletion debugger/src/internal/debug_bridge.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ class DebugBridge final {
StackTraceResponse getStackTrace();

// Called from **DAP** client to get scopes for a frame
void extracted(int& level, lua_State*& L);
ScopesResponse getScopes(int level);

// Called from **DAP** client to get variable by variable reference
Expand Down Expand Up @@ -133,6 +132,9 @@ class DebugBridge final {

std::vector<StackFrame> updateStackFrames();

void mainThreadWait(lua_State* L, std::unique_lock<std::mutex>& lock);
void executeInMainThread(std::function<void()> fn);

private:
friend class LuaStatics;

Expand All @@ -145,6 +147,7 @@ class DebugBridge final {

lua_State* break_vm_ = nullptr;
std::mutex break_mutex_;
std::function<void()> main_fn_;
bool resume_ = false;
std::condition_variable resume_cv_;

Expand Down
5 changes: 2 additions & 3 deletions debugger/src/internal/utils/dap_utils.h
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
#pragma once
#include <nlohmann_json_serializer.h>
#include <concepts>

#include <dap/protocol.h>
#include <dap/typeof.h>
#include <nlohmann_json_serializer.h>
#include <concepts>
#include <exception>

#include <Luau/BytecodeBuilder.h>
#include <Luau/Common.h>
Expand Down
4 changes: 2 additions & 2 deletions debugger/src/internal/variable.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
#include <format>
#include "internal/scope.h"
#include "internal/utils/lua_utils.h"

#include <lapi.h>
#include <lobject.h>
Expand All @@ -9,7 +7,9 @@
#include <lualib.h>

#include <internal/log.h>
#include <internal/scope.h>
#include <internal/utils.h>
#include <internal/utils/lua_utils.h>
#include <internal/variable.h>
#include <internal/variable_registry.h>

Expand Down

0 comments on commit 831e3b4

Please sign in to comment.