Skip to content

Commit c8080d9

Browse files
committed
Step support.
Ability to step to the next line which will force the debugger to break after execution even if that line doesn't have a breakpoint.
1 parent 1882cee commit c8080d9

File tree

7 files changed

+55
-2
lines changed

7 files changed

+55
-2
lines changed

Assets/Scripts/Language/Debugger.gd

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ enum MESSAGE {
3838
SNIPPET_END,
3939
BREAK,
4040
RESUME,
41+
STEP,
4142
STOP
4243
}
4344

@@ -223,6 +224,8 @@ func OnServerDataReceived(Data: String) -> void:
223224
match (Type):
224225
MESSAGE.RESUME:
225226
Runtime.Resume()
227+
MESSAGE.STEP:
228+
Runtime.Step()
226229
MESSAGE.STOP:
227230
Runtime.Stop()
228231

@@ -276,6 +279,10 @@ func Resume() -> void:
276279
DispatchToServer(MESSAGE.RESUME, "")
277280

278281

282+
func Step() -> void:
283+
DispatchToServer(MESSAGE.STEP, "")
284+
285+
279286
func Stop() -> void:
280287
DispatchToServer(MESSAGE.STOP, "")
281288

Assets/Scripts/Language/Runtime.gd

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,10 @@ func Resume() -> void:
205205
Code.Resume()
206206

207207

208+
func Step() -> void:
209+
Code.Step()
210+
211+
208212
func Compile(InSnippet: SnippetData) -> Reference:
209213
if not InSnippet:
210214
return null

Assets/Scripts/Language/VirtualMachine.gd

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,8 @@ func _process(_delta: float) -> void:
6363
Guard.unlock()
6464

6565
if State == STATE.BREAK:
66-
emit_signal("OnBreak", LineBreak)
6766
State = STATE.PAUSED
67+
emit_signal("OnBreak", LineBreak)
6868

6969

7070
func ToLua(Code: String) -> ParserResult:
@@ -142,6 +142,19 @@ func Resume() -> void:
142142
Guard.unlock()
143143

144144

145+
func Step() -> void:
146+
if not ActiveVM:
147+
return
148+
149+
if State != STATE.PAUSED:
150+
return
151+
152+
Guard.lock()
153+
ActiveVM.Step()
154+
State = STATE.RUNNING
155+
Guard.unlock()
156+
157+
145158
func UpdateBreakpoints(Breakpoints: Array) -> void:
146159
if not ActiveVM:
147160
return

Native/Snippet/Lua/LuaVM.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ void LuaVM::_register_methods()
7777
register_method((char*)"Reset", &LuaVM::Reset);
7878
register_method((char*)"Resume", &LuaVM::Resume);
7979
register_method((char*)"Stop", &LuaVM::Stop);
80+
register_method((char*)"Step", &LuaVM::Step);
8081
register_method((char*)"AttachDebugger", &LuaVM::AttachDebugger);
8182
register_method((char*)"GetDebugger", &LuaVM::GetDebugger);
8283
register_signal<LuaVM>((char*)"OnPrint", "Contents", GODOT_VARIANT_TYPE_STRING);
@@ -321,6 +322,17 @@ void LuaVM::Stop()
321322
// Owning thread object must call wait_to_finish().
322323
}
323324

325+
void LuaVM::Step()
326+
{
327+
if (!Debugger.is_valid())
328+
{
329+
return;
330+
}
331+
332+
Debugger->SetStep(true);
333+
Resume();
334+
}
335+
324336
void LuaVM::AttachDebugger()
325337
{
326338
if (State == nullptr)

Native/Snippet/Lua/LuaVM.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ class LuaVM : public Reference
7272
void Reset();
7373
void Resume();
7474
void Stop();
75+
void Step();
7576
void AttachDebugger();
7677
Ref<LuaVMDebugger> GetDebugger() const;
7778

Native/Snippet/Lua/LuaVMDebugger.cpp

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,8 +149,10 @@ void LuaVMDebugger::OnHook(lua_State *State, lua_Debug *Ar)
149149
int Index = Ar->currentline - 1;
150150

151151
// If we hit a breakpoint, grab all needed data.
152-
if (Breakpoints.has(Index))
152+
if (Breakpoints.has(Index) || VM->GetDebugger()->ShouldStep())
153153
{
154+
VM->GetDebugger()->SetStep(false);
155+
154156
// Grab all valid local variables.
155157
VM->GetDebugger()->ClearVariables();
156158
const char *Name = nullptr;
@@ -250,6 +252,7 @@ LuaVMDebugger::~LuaVMDebugger()
250252

251253
void LuaVMDebugger::_init()
252254
{
255+
Step = false;
253256
}
254257

255258
void LuaVMDebugger::SetBreakpoints(Array InBreakpoints)
@@ -297,4 +300,14 @@ void LuaVMDebugger::Unhook(lua_State *State)
297300
lua_sethook(State, nullptr, 0, 0);
298301
}
299302

303+
void LuaVMDebugger::SetStep(bool InStep)
304+
{
305+
Step = InStep;
306+
}
307+
308+
bool LuaVMDebugger::ShouldStep() const
309+
{
310+
return Step;
311+
}
312+
300313
}

Native/Snippet/Lua/LuaVMDebugger.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ class LuaVMDebugger : public Reference
4141
private:
4242
Array Breakpoints;
4343
Dictionary Variables;
44+
bool Step;
4445

4546
static void OnHook(lua_State *State, lua_Debug *Ar);
4647

@@ -60,6 +61,8 @@ class LuaVMDebugger : public Reference
6061
void ClearVariables();
6162
void Hook(lua_State *State);
6263
void Unhook(lua_State *State);
64+
void SetStep(bool InStep);
65+
bool ShouldStep() const;
6366
};
6467

6568
}

0 commit comments

Comments
 (0)