Skip to content

Commit d2ad3db

Browse files
committed
Use the debuggee's casting env for the evaluator instead of StaffCastEnv
1 parent bc35552 commit d2ad3db

File tree

3 files changed

+26
-28
lines changed

3 files changed

+26
-28
lines changed

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

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -172,15 +172,15 @@ class DebugAdapter(val player: ServerPlayer) : IDebugProtocolServer {
172172
})
173173
}
174174

175-
fun evaluate(threadId: Int, env: CastingEnvironment, pattern: HexPattern) =
176-
evaluate(threadId, env, PatternIota(pattern))
175+
fun evaluate(threadId: Int, pattern: HexPattern) =
176+
evaluate(threadId, PatternIota(pattern))
177177

178-
fun evaluate(threadId: Int, env: CastingEnvironment, iota: Iota) =
179-
evaluate(threadId, env, SpellList.LList(listOf(iota)))
178+
fun evaluate(threadId: Int, iota: Iota) =
179+
evaluate(threadId, SpellList.LList(listOf(iota)))
180180

181-
fun evaluate(threadId: Int, env: CastingEnvironment, list: SpellList) =
181+
fun evaluate(threadId: Int, list: SpellList) =
182182
inRangeDebugger(threadId)?.let {
183-
val result = it.evaluate(env, list) ?: return null
183+
val result = it.evaluate(list) ?: return null
184184
if (result.startedEvaluating) {
185185
setEvaluatorState(threadId, EvaluatorItem.EvalState.MODIFIED)
186186
}
@@ -253,6 +253,8 @@ class DebugAdapter(val player: ServerPlayer) : IDebugProtocolServer {
253253
remoteProxy.continued(ContinuedEventArguments().also {
254254
it.threadId = threadId
255255
})
256+
257+
closeEvaluator(threadId)
256258
}
257259

258260
return view

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

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ import gay.`object`.hexdebug.utils.toHexpatternSource
2626
import org.eclipse.lsp4j.debug.*
2727
import java.util.*
2828
import kotlin.math.min
29-
import kotlin.reflect.jvm.jvmName
3029
import org.eclipse.lsp4j.debug.LoadedSourceEventArgumentsReason as LoadedSourceReason
3130

3231
class HexDebugger(
@@ -37,7 +36,7 @@ class HexDebugger(
3736
var state = DebuggerState.RUNNING
3837
private set
3938

40-
private var currentEnv: CastingEnvironment? = null
39+
private var env: CastingEnvironment? = null
4140
set(value) {
4241
field = value
4342
(value as? IDebugEnvAccessor)?.`debugEnv$hexdebug` = debugEnv
@@ -48,9 +47,6 @@ class HexDebugger(
4847

4948
private var lastRequestStepType: RequestStepType? = null
5049

51-
// TODO: delegate to debug env?
52-
val envName: String? get() = currentEnv?.let { it::class.simpleName ?: it::class.jvmName }
53-
5450
val sessionId get() = debugEnv.sessionId
5551

5652
private val initArgs by sharedState::initArgs
@@ -87,7 +83,7 @@ class HexDebugger(
8783

8884
private val nextFrame get() = (nextContinuation as? NotDone)?.frame
8985

90-
private fun getVM(env: CastingEnvironment? = null) = (env ?: currentEnv)?.let { CastingVM(image, it) }
86+
private fun getVM() = env?.let { CastingVM(image, it) }
9187

9288
private fun registerNewSource(frame: ContinuationFrame): Source? = getIotas(frame)?.let(::registerNewSource)
9389

@@ -250,7 +246,7 @@ class HexDebugger(
250246
}
251247

252248
private fun getRavenmind(): Iota {
253-
val env = currentEnv
249+
val env = env
254250
return if (env != null && image.userData.contains(HexAPI.RAVENMIND_USERDATA)) {
255251
IotaType.deserialize(image.userData.getCompound(HexAPI.RAVENMIND_USERDATA), env.world)
256252
} else {
@@ -339,14 +335,14 @@ class HexDebugger(
339335
/**
340336
* Use [DebugAdapter.evaluate][gay.object.hexdebug.adapter.DebugAdapter.evaluate] instead.
341337
*/
342-
internal fun evaluate(env: CastingEnvironment, list: SpellList): DebugStepResult? {
343-
val vm = getVM(env) ?: return null
344-
(env as IDebugEnvAccessor).`debugEnv$hexdebug` = debugEnv
338+
internal fun evaluate(list: SpellList): DebugStepResult? {
339+
val vm = getVM() ?: return null
340+
(vm.env as IDebugEnvAccessor).`debugEnv$hexdebug` = debugEnv
345341

346342
if (state == DebuggerState.CAUGHT_MISHAP) {
347343
// manually trigger the mishap sound
348344
// TODO: this feels scuffed.
349-
env.postExecution(
345+
vm.env.postExecution(
350346
CastResult(NullIota(), nextContinuation, null, listOf(), lastResolutionType, HexEvalSounds.MISHAP)
351347
)
352348
return DebugStepResult(StopReason.EXCEPTION, clientInfo = getClientView(vm))
@@ -360,7 +356,7 @@ class HexDebugger(
360356
nextContinuation = nextContinuation.pushFrame(FrameEvaluate(list, false))
361357
return executeNextDebugStep(vm, doStaffMishaps = true)
362358
.copy(startedEvaluating = startedEvaluating)
363-
.also { postStep(vm, it) }
359+
.also(::postStep)
364360
}
365361

366362
/**
@@ -375,6 +371,7 @@ class HexDebugger(
375371
}
376372
evaluatorResetData = null
377373
evaluatorUIPatterns.clear()
374+
postStep(DebugStepResult(StopReason.STEP))
378375
}
379376

380377
fun startExecuting(env: CastingEnvironment, iotas: List<Iota>, image: CastingImage?): DebugStepResult? {
@@ -383,11 +380,11 @@ class HexDebugger(
383380
}
384381

385382
// if currentEnv is null, we haven't executed anything yet
386-
val isStarting = currentEnv == null
383+
val isStarting = this.env == null
387384
val isPausing = state == DebuggerState.PAUSING
388385

389386
state = DebuggerState.PAUSED
390-
currentEnv = env
387+
this.env = env
391388
image?.let { this.image = it }
392389

393390
var newContinuation: SpellContinuation = Done
@@ -429,7 +426,7 @@ class HexDebugger(
429426

430427
fun executeUntilStopped(stepType: RequestStepType? = null): DebugStepResult {
431428
val vm = getVM() ?: return DebugStepResult(null, skipped = true)
432-
return executeUntilStopped(vm, stepType).also { postStep(vm, it) }
429+
return executeUntilStopped(vm, stepType).also(::postStep)
433430
}
434431

435432
private fun executeUntilStopped(vm: CastingVM, stepType: RequestStepType? = null): DebugStepResult {
@@ -633,9 +630,10 @@ class HexDebugger(
633630
}.copy(clientInfo = getClientView(vm))
634631
}
635632

636-
private fun postStep(vm: CastingVM, result: DebugStepResult) {
637-
if (!result.skipped) {
638-
debugEnv.postStep(vm.env, image, result.reason)
633+
private fun postStep(result: DebugStepResult) {
634+
val env = env
635+
if (!result.skipped && env != null) {
636+
debugEnv.postStep(env, image, result.reason)
639637
}
640638
}
641639

@@ -758,7 +756,7 @@ class HexDebugger(
758756
}
759757

760758
private fun iotaToString(iota: Iota, isSource: Boolean = false): String {
761-
val env = currentEnv ?: return "" // FIXME: hack
759+
val env = env ?: return "" // FIXME: hack
762760
return if (isSource) {
763761
iota.toHexpatternSource(env)
764762
} else {

Common/src/main/kotlin/gay/object/hexdebug/items/EvaluatorItem.kt

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package gay.`object`.hexdebug.items
22

33
import at.petrak.hexcasting.api.casting.ParticleSpray
4-
import at.petrak.hexcasting.api.casting.eval.env.StaffCastEnv
54
import at.petrak.hexcasting.api.utils.asTranslatedComponent
65
import at.petrak.hexcasting.common.items.ItemStaff
76
import at.petrak.hexcasting.common.lib.HexSounds
@@ -126,8 +125,7 @@ class EvaluatorItem(
126125
return
127126
}
128127

129-
val env = StaffCastEnv(sender, msg.handUsed)
130-
val clientInfo = debugAdapter.evaluate(threadId, env, msg.pattern) ?: return
128+
val clientInfo = debugAdapter.evaluate(threadId, msg.pattern) ?: return
131129

132130
debugger.evaluatorUIPatterns.clear()
133131
if (!clientInfo.isStackClear) {

0 commit comments

Comments
 (0)