Skip to content

Commit dce3099

Browse files
committed
Always return non-null DebugStepResult, include client view in result, use enum for reason
1 parent 94a8e80 commit dce3099

File tree

4 files changed

+53
-58
lines changed

4 files changed

+53
-58
lines changed

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

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ open class DebugAdapter(val player: ServerPlayer) : IDebugProtocolServer {
113113

114114
fun evaluate(list: SpellList) = debugger?.let {
115115
val result = it.evaluate(list)
116-
if (result?.startedEvaluating == true) {
116+
if (result.startedEvaluating) {
117117
setEvaluatorState(ItemEvaluator.EvalState.MODIFIED)
118118
}
119119
handleDebuggerStep(result)
@@ -141,12 +141,12 @@ open class DebugAdapter(val player: ServerPlayer) : IDebugProtocolServer {
141141
?: ResponseError(ResponseErrorCode.InternalError, e.toString(), e.stackTraceToString())
142142
}
143143

144-
private fun handleDebuggerStep(result: DebugStepResult?): ExecutionClientView? {
145-
val view = debugger?.getClientView()?.also {
144+
private fun handleDebuggerStep(result: DebugStepResult): ExecutionClientView? {
145+
val view = result.clientInfo?.also {
146146
IXplatAbstractions.INSTANCE.sendPacketToPlayer(player, MsgNewSpellPatternS2C(it, -1))
147147
}
148148

149-
if (result == null) {
149+
if (result.isDone) {
150150
terminate(null)
151151
return view
152152
}
@@ -158,7 +158,7 @@ open class DebugAdapter(val player: ServerPlayer) : IDebugProtocolServer {
158158
})
159159
}
160160

161-
sendStoppedEvent(result.reason)
161+
sendStoppedEvent(result.reason.value)
162162
return view
163163
}
164164

@@ -233,29 +233,29 @@ open class DebugAdapter(val player: ServerPlayer) : IDebugProtocolServer {
233233

234234
override fun configurationDone(args: ConfigurationDoneArguments?): CompletableFuture<Void> {
235235
knownPlayers.add(player.uuid)
236-
handleDebuggerStep(debugger?.start())
236+
debugger?.start()?.also(::handleDebuggerStep)
237237
return futureOf()
238238
}
239239

240240
// stepping
241241

242242
override fun next(args: NextArguments?): CompletableFuture<Void> {
243-
handleDebuggerStep(debugger?.executeUntilStopped(RequestStepType.OVER))
243+
debugger?.executeUntilStopped(RequestStepType.OVER)?.also(::handleDebuggerStep)
244244
return futureOf()
245245
}
246246

247247
override fun continue_(args: ContinueArguments?): CompletableFuture<ContinueResponse> {
248-
handleDebuggerStep(debugger?.executeUntilStopped())
248+
debugger?.executeUntilStopped()?.also(::handleDebuggerStep)
249249
return futureOf()
250250
}
251251

252252
override fun stepIn(args: StepInArguments?): CompletableFuture<Void> {
253-
handleDebuggerStep(debugger?.executeOnce())
253+
debugger?.executeOnce()?.also(::handleDebuggerStep)
254254
return futureOf()
255255
}
256256

257257
override fun stepOut(args: StepOutArguments?): CompletableFuture<Void> {
258-
handleDebuggerStep(debugger?.executeUntilStopped(RequestStepType.OUT))
258+
debugger?.executeUntilStopped(RequestStepType.OUT)?.also(::handleDebuggerStep)
259259
return futureOf()
260260
}
261261

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,20 @@
11
package gay.`object`.hexdebug.debugger
22

3+
import at.petrak.hexcasting.api.casting.eval.ExecutionClientView
34
import org.eclipse.lsp4j.debug.Source
45
import org.eclipse.lsp4j.debug.LoadedSourceEventArgumentsReason as LoadedSourceReason
56

67
data class DebugStepResult(
7-
val reason: String,
8+
val reason: StopReason,
89
val type: DebugStepType? = null,
10+
val clientInfo: ExecutionClientView? = null,
911
val loadedSources: Map<Source, LoadedSourceReason> = mapOf(),
1012
val startedEvaluating: Boolean = false,
1113
) {
14+
val isDone = reason == StopReason.TERMINATED
15+
16+
fun done() = copy(reason = StopReason.TERMINATED)
17+
1218
operator fun plus(other: DebugStepResult) = copy(
1319
loadedSources = loadedSources + other.loadedSources,
1420
)
Lines changed: 14 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,26 @@
11
package gay.`object`.hexdebug.debugger
22

3+
enum class StopReason(val value: String) {
4+
STEP("step"),
5+
BREAKPOINT("breakpoint"),
6+
EXCEPTION("exception"),
7+
ENTRY("entry"),
8+
TERMINATED("terminated"),
9+
}
10+
311
enum class DebugStepType {
412
IN,
513
OUT,
614
JUMP,
715
ESCAPE,
816
}
917

10-
enum class SourceBreakpointMode {
11-
EVALUATED,
12-
ESCAPED,
13-
ALL;
14-
15-
val label get() = when (this) {
16-
EVALUATED -> "Evaluated"
17-
ESCAPED -> "Escaped"
18-
ALL -> "All"
19-
}
20-
21-
val description get() = when (this) {
22-
EVALUATED -> "Stop if this iota would be evaluated. (default)"
23-
ESCAPED -> "Stop if this iota would be escaped."
24-
ALL -> "Always stop at this iota."
25-
}
18+
enum class SourceBreakpointMode(val label: String, val description: String) {
19+
EVALUATED("Evaluated", "Stop if this iota would be evaluated. (default)"),
20+
ESCAPED("Escaped", "Stop if this iota would be escaped."),
21+
ALL("All", "Always stop at this iota."),
2622
}
2723

28-
enum class ExceptionBreakpointType {
29-
UNCAUGHT_MISHAPS;
30-
31-
val label get() = when (this) {
32-
UNCAUGHT_MISHAPS -> "Uncaught Mishaps"
33-
}
34-
35-
val isDefault get() = when (this) {
36-
UNCAUGHT_MISHAPS -> true
37-
}
24+
enum class ExceptionBreakpointType(val label: String, val isDefault: Boolean) {
25+
UNCAUGHT_MISHAPS("Uncaught Mishaps", true),
3826
}

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

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -359,7 +359,7 @@ class HexDebugger(
359359
}
360360
}
361361

362-
fun getClientView(): ExecutionClientView {
362+
private fun getClientView(vm: CastingVM): ExecutionClientView {
363363
val (stackDescs, ravenmind) = vm.generateDescs()
364364
val isStackClear = nextContinuation is Done // only close the window if we're done evaluating
365365
return ExecutionClientView(isStackClear, lastResolutionType, stackDescs, ravenmind)
@@ -368,13 +368,13 @@ class HexDebugger(
368368
/**
369369
* Use [DebugAdapter.evaluate][gay.object.hexdebug.adapter.DebugAdapter.evaluate] instead.
370370
*/
371-
internal fun evaluate(list: SpellList): DebugStepResult? {
371+
internal fun evaluate(list: SpellList): DebugStepResult {
372372
val startedEvaluating = evaluatorResetData == null
373373
if (startedEvaluating) {
374374
evaluatorResetData = Pair(nextContinuation, vm.image)
375375
}
376376
nextContinuation = nextContinuation.pushFrame(FrameEvaluate(list, false))
377-
return executeOnce()?.copy(startedEvaluating = startedEvaluating)
377+
return executeOnce().copy(startedEvaluating = startedEvaluating)
378378
}
379379

380380
/**
@@ -389,35 +389,36 @@ class HexDebugger(
389389
evaluatorUIPatterns.clear()
390390
}
391391

392-
fun start(): DebugStepResult? {
393-
val loadedSources = mapOf(initialSource to LoadedSourceReason.NEW)
392+
fun start(): DebugStepResult {
394393
return if (launchArgs.stopOnEntry) {
395-
DebugStepResult("entry", loadedSources = loadedSources)
394+
DebugStepResult(StopReason.ENTRY)
396395
} else if (isAtBreakpoint()) {
397-
DebugStepResult("breakpoint", loadedSources = loadedSources)
396+
DebugStepResult(StopReason.BREAKPOINT)
398397
} else {
399-
executeUntilStopped()?.withLoadedSources(loadedSources)
400-
}
398+
executeUntilStopped()
399+
}.withLoadedSource(initialSource, LoadedSourceReason.NEW)
401400
}
402401

403-
fun executeUntilStopped(stepType: RequestStepType? = null): DebugStepResult? {
402+
fun executeUntilStopped(stepType: RequestStepType? = null): DebugStepResult {
404403
var lastResult: DebugStepResult? = null
405404
var isEscaping: Boolean? = null
406405
var stepDepth = 0
407406
var shouldStop = false
408407
var hitBreakpoint = false
409408

410409
while (true) {
411-
var result = executeOnce(exactlyOnce = true) ?: return null
412-
if (lastResult != null) result += lastResult
410+
val result = executeOnce(exactlyOnce = true).let {
411+
if (it.isDone) return it
412+
lastResult?.plus(it) ?: it
413+
}
413414
lastResult = result
414415

415416
if (isAtBreakpoint()) {
416417
hitBreakpoint = true
417418
}
418419

419420
if (hitBreakpoint && shouldStopAtFrame(nextContinuation)) {
420-
return result.copy(reason = "breakpoint")
421+
return result.copy(reason = StopReason.BREAKPOINT)
421422
}
422423

423424
// if stepType is null, we should ONLY stop on breakpoints
@@ -452,11 +453,11 @@ class HexDebugger(
452453
}
453454

454455
// Copy of CastingVM.queueExecuteAndWrapIotas to allow stepping by one pattern at a time.
455-
fun executeOnce(exactlyOnce: Boolean = false): DebugStepResult? {
456-
var continuation = nextContinuation // bind locally so we can do smart casting
457-
if (continuation !is NotDone) return null
456+
fun executeOnce(exactlyOnce: Boolean = false): DebugStepResult {
457+
var stepResult = DebugStepResult(StopReason.STEP)
458458

459-
var stepResult = DebugStepResult("step")
459+
var continuation = nextContinuation // bind locally so we can do smart casting
460+
if (continuation !is NotDone) return stepResult.done()
460461

461462
variablesAllocator.clear()
462463

@@ -485,7 +486,7 @@ class HexDebugger(
485486
val (newContinuation, preMishapImage) = if (castResult.resolutionType.success) {
486487
Pair(castResult.continuation, null)
487488
} else if (ExceptionBreakpointType.UNCAUGHT_MISHAPS in exceptionBreakpoints) {
488-
stepResult = stepResult.copy(reason = "exception")
489+
stepResult = stepResult.copy(reason = StopReason.EXCEPTION)
489490
Pair(castResult.continuation.pushFrame(FrameBreakpoint.fatal()), vm.image)
490491
} else {
491492
Pair(Done, vm.image)
@@ -558,10 +559,10 @@ class HexDebugger(
558559

559560
nextContinuation = continuation
560561

561-
return when (continuation) {
562-
is NotDone -> stepResult
563-
is Done -> null
562+
if (continuation is Done) {
563+
stepResult = stepResult.done()
564564
}
565+
return stepResult.copy(clientInfo = getClientView(vm))
565566
}
566567

567568
private fun shouldStopAtFrame(continuation: SpellContinuation) =

0 commit comments

Comments
 (0)