Skip to content

Commit bc35552

Browse files
committed
Change where/when postStep is called
1 parent 211eedd commit bc35552

File tree

8 files changed

+44
-33
lines changed

8 files changed

+44
-33
lines changed

Common/src/main/kotlin/gay/object/hexdebug/adapter/DebugAdapter.kt

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -245,12 +245,8 @@ class DebugAdapter(val player: ServerPlayer) : IDebugProtocolServer {
245245
// stopped
246246
sendStoppedEvent(threadId, result.reason)
247247

248-
debugger(threadId)?.let { debugger ->
249-
debugger.getNextIotaToEvaluate()?.also { (iota, index) ->
250-
printDebuggerStatus(iota, index)
251-
}
252-
253-
debugger.postStep(result.reason)
248+
debugger(threadId)?.getNextIotaToEvaluate()?.also { (iota, index) ->
249+
printDebuggerStatus(iota, index)
254250
}
255251
} else if (wasPaused) {
256252
// running

Common/src/main/kotlin/gay/object/hexdebug/debugger/DebugStepResult.kt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,16 @@ data class DebugStepResult(
1313
val clientInfo: ExecutionClientView? = null,
1414
val loadedSources: Map<Source, LoadedSourceReason> = mapOf(),
1515
val startedEvaluating: Boolean = false,
16+
val skipped: Boolean = false,
1617
) {
1718
val isDone = reason == StopReason.TERMINATED
1819

1920
fun done() = copy(reason = StopReason.TERMINATED)
2021

22+
fun resumed() = copy(reason = null)
23+
24+
fun skipped() = copy(skipped = true)
25+
2126
operator fun plus(other: DebugStepResult) = copy(
2227
loadedSources = loadedSources + other.loadedSources,
2328
)

Common/src/main/kotlin/gay/object/hexdebug/debugger/HexDebugger.kt

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -139,10 +139,6 @@ class HexDebugger(
139139
return iotaToString(iota, isSource = false) to (meta?.lineIndex ?: -1)
140140
}
141141

142-
fun postStep(reason: StopReason) {
143-
currentEnv?.let { debugEnv.postStep(it, image, reason) }
144-
}
145-
146142
fun getStackFrames(): Sequence<StackFrame> {
147143
var frameId = 1
148144
var virtualFrameId = (callStack.size + 1).ceilToPow(10)
@@ -362,7 +358,9 @@ class HexDebugger(
362358
}
363359

364360
nextContinuation = nextContinuation.pushFrame(FrameEvaluate(list, false))
365-
return executeNextDebugStep(vm, doStaffMishaps = true).copy(startedEvaluating = startedEvaluating)
361+
return executeNextDebugStep(vm, doStaffMishaps = true)
362+
.copy(startedEvaluating = startedEvaluating)
363+
.also { postStep(vm, it) }
366364
}
367365

368366
/**
@@ -430,8 +428,11 @@ class HexDebugger(
430428
}
431429

432430
fun executeUntilStopped(stepType: RequestStepType? = null): DebugStepResult {
433-
val vm = getVM() ?: return DebugStepResult(null)
431+
val vm = getVM() ?: return DebugStepResult(null, skipped = true)
432+
return executeUntilStopped(vm, stepType).also { postStep(vm, it) }
433+
}
434434

435+
private fun executeUntilStopped(vm: CastingVM, stepType: RequestStepType? = null): DebugStepResult {
435436
lastRequestStepType = stepType
436437
if (stepType == RequestStepType.IN) return executeNextDebugStep(vm)
437438

@@ -500,10 +501,10 @@ class HexDebugger(
500501
): DebugStepResult {
501502
var stepResult = DebugStepResult(StopReason.STEP)
502503

503-
if (state == DebuggerState.RUNNING) return stepResult.copy(reason = null)
504+
if (state == DebuggerState.RUNNING) return stepResult.resumed().skipped()
504505

505506
var continuation = nextContinuation // bind locally so we can do smart casting
506-
if (continuation !is NotDone) return stepResult.done()
507+
if (continuation !is NotDone) return stepResult.done().skipped()
507508

508509
variablesAllocator.clear()
509510

@@ -622,7 +623,7 @@ class HexDebugger(
622623
return when (continuation) {
623624
is Done -> if (state.canResume && debugEnv.resume(vm.env, image, lastResolutionType)) {
624625
state = DebuggerState.RUNNING
625-
stepResult.copy(reason = null)
626+
stepResult.resumed()
626627
} else {
627628
// we terminate the debuggee in handleDebuggerStep
628629
state = DebuggerState.TERMINATED
@@ -632,6 +633,12 @@ class HexDebugger(
632633
}.copy(clientInfo = getClientView(vm))
633634
}
634635

636+
private fun postStep(vm: CastingVM, result: DebugStepResult) {
637+
if (!result.skipped) {
638+
debugEnv.postStep(vm.env, image, result.reason)
639+
}
640+
}
641+
635642
private fun shouldStopAtFrame(continuation: SpellContinuation) =
636643
continuation !is NotDone || shouldStopAtFrame(continuation.frame)
637644

Core/Common/src/main/java/gay/object/hexdebug/core/api/debugging/env/DebugEnvironment.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import at.petrak.hexcasting.api.casting.castables.Action;
44
import at.petrak.hexcasting.api.casting.eval.CastingEnvironment;
55
import at.petrak.hexcasting.api.casting.eval.ResolvedPatternType;
6+
import at.petrak.hexcasting.api.casting.eval.env.StaffCastEnv;
67
import at.petrak.hexcasting.api.casting.eval.sideeffects.OperatorSideEffect;
78
import at.petrak.hexcasting.api.casting.eval.vm.CastingImage;
89
import gay.object.hexdebug.core.api.HexDebugCoreAPI;
@@ -84,13 +85,14 @@ public abstract boolean resume(
8485
/**
8586
* Do whatever you like after each debugger step.
8687
* <br>
87-
* Called by the debugger after an execution step is finished, unless the debug session was
88-
* resumed or terminated as a result of the step.
88+
* This is called by the debugger after it finishes executing some number of iotas.
89+
* {@code reason} is the reason why the debugger stopped at this point, or {@code null} if it
90+
* ran out of iotas to execute and {@link DebugEnvironment#resume} returned {@code true}.
8991
*/
9092
public void postStep(
9193
@NotNull CastingEnvironment env,
9294
@NotNull CastingImage image,
93-
@NotNull StopReason reason
95+
@Nullable StopReason reason
9496
) {}
9597

9698
public void printDebugMessage(@NotNull Component message) {

plantuml/debug_api/initialization.puml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,6 @@ activate Debuggee
4040
return stopped on entry
4141

4242
Adapter ->> User: event: stopped on entry
43-
44-
Adapter -> DebugEnv ++: ""postStep(env, image, reason)""
45-
return
4643
return
4744
return
4845
deactivate Debuggee

plantuml/debug_api/nested.puml

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,12 @@ User ->> Adapter ++: request: step
3535
Debuggee2 ->> Debuggee2: start
3636
return
3737
return
38+
39+
Debugger -> DebugEnv ++: ""postStep(env, image, reason)""
40+
return
3841
return stopped on step
3942

4043
Adapter ->> User: event: stopped on step
41-
42-
Adapter -> DebugEnv ++: ""postStep(env, image, reason)""
43-
return
4444
deactivate Adapter
4545

4646
...
@@ -52,9 +52,6 @@ Debuggee2 -> Debuggee2 ++: pause
5252
return stopped on entry
5353

5454
Adapter ->> User: event: stopped on entry
55-
56-
Adapter -> DebugEnv ++: ""postStep(env, image, reason)""
57-
return
5855
return
5956
return
6057
deactivate Debuggee2

plantuml/debug_api/stepping.puml

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,12 @@ User ->> Adapter ++: request: step forward
2121
return
2222
end
2323
return
24+
25+
Debugger -> DebugEnv ++: ""postStep(env, image, reason)""
26+
return
2427
return stopped on step
2528

2629
Adapter ->> User: event: stopped on step
27-
28-
Adapter -> DebugEnv ++: ""postStep(env, image, reason)""
29-
return
3030
else caster out of range
3131
Adapter ->> User: error: out of range
3232
end
@@ -44,6 +44,9 @@ User ->> Adapter ++: request: step over final iota
4444
Debugger -> DebugEnv ++: ""resume(env, image, resolutionType)""
4545
DebugEnv ->> Debuggee ++: unpause
4646
DebugEnv --> Debugger --: ""true""
47+
48+
Debugger -> DebugEnv ++: ""postStep(env, image, reason)""
49+
return
4750
Debugger --> Adapter --: resumed
4851

4952
Adapter ->> User: event: continued
@@ -57,11 +60,12 @@ Debuggee -> API ++: ""startDebuggingIotas(...)""
5760
API -> Adapter ++: start executing
5861
Adapter -> Debugger ++: start executing
5962
return stopped on step
63+
note right
64+
""postStep"" is not called here
65+
because no iotas were executed.
66+
end note
6067

6168
Adapter ->> User: event: stopped on step
62-
63-
Adapter -> DebugEnv ++: ""postStep(env, image, reason)""
64-
return
6569
return
6670
return
6771
deactivate Debuggee

plantuml/debug_api/terminating.puml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ User ->> Adapter ++: request: continue
1515

1616
Debugger -> DebugEnv ++: ""resume(env, image, resolutionType)""
1717
return ""false""
18+
19+
Debugger -> DebugEnv ++: ""postStep(env, image, reason)""
20+
return
1821
return unable to resume
1922

2023
Adapter ->> User: event: thread exited

0 commit comments

Comments
 (0)