Skip to content

Commit ebd9df8

Browse files
Alwinfyvgskye
authored andcommitted
Fix FrameForEach to use Vec
1 parent 533753e commit ebd9df8

File tree

2 files changed

+14
-14
lines changed

2 files changed

+14
-14
lines changed

Common/src/main/java/at/petrak/hexcasting/api/casting/eval/vm/FrameForEach.kt

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import at.petrak.hexcasting.api.utils.NBTBuilder
99
import at.petrak.hexcasting.api.utils.getList
1010
import at.petrak.hexcasting.api.utils.hasList
1111
import at.petrak.hexcasting.api.utils.serializeToNBT
12+
import at.petrak.hexcasting.api.utils.Vec
1213
import at.petrak.hexcasting.common.lib.hex.HexEvalSounds
1314
import at.petrak.hexcasting.common.lib.hex.HexIotaTypes
1415
import net.minecraft.nbt.CompoundTag
@@ -28,14 +29,13 @@ data class FrameForEach(
2829
val data: SpellList,
2930
val code: SpellList,
3031
val baseStack: List<Iota>?,
31-
val acc: MutableList<Iota>
32+
val acc: Vec<Iota>
3233
) : ContinuationFrame {
3334

3435
/** When halting, we add the stack state at halt to the stack accumulator, then return the original pre-Thoth stack, plus the accumulator. */
3536
override fun breakDownwards(stack: List<Iota>): Pair<Boolean, List<Iota>> {
3637
val newStack = baseStack?.toMutableList() ?: mutableListOf()
37-
acc.addAll(stack)
38-
newStack.add(ListIota(acc))
38+
newStack.add(ListIota(acc.appendAll(stack).toList()))
3939
return true to newStack
4040
}
4141

@@ -46,27 +46,26 @@ data class FrameForEach(
4646
harness: CastingVM
4747
): CastResult {
4848
// If this isn't the very first Thoth step (i.e. no Thoth computations run yet)...
49-
val stack = if (baseStack == null) {
49+
val (stack, nextAcc) = if (baseStack == null) {
5050
// init stack to the harness stack...
51-
harness.image.stack.toList()
51+
harness.image.stack.toList() to acc
5252
} else {
5353
// else save the stack to the accumulator and reuse the saved base stack.
54-
acc.addAll(harness.image.stack)
55-
baseStack
54+
baseStack to acc.appendAll(harness.image.stack)
5655
}
5756

5857
// If we still have data to process...
5958
val (stackTop, newImage, newCont) = if (data.nonEmpty) {
6059
// push the next datum to the top of the stack,
6160
val cont2 = continuation
6261
// put the next Thoth object back on the stack for the next Thoth cycle,
63-
.pushFrame(FrameForEach(data.cdr, code, stack, acc))
62+
.pushFrame(FrameForEach(data.cdr, code, stack, nextAcc))
6463
// and prep the Thoth'd code block for evaluation.
6564
.pushFrame(FrameEvaluate(code, true))
6665
Triple(data.car, harness.image.withUsedOp(), cont2)
6766
} else {
6867
// Else, dump our final list onto the stack.
69-
Triple(ListIota(acc), harness.image, continuation)
68+
Triple(ListIota(acc.toList()), harness.image, continuation)
7069
}
7170
val tStack = stack.toMutableList()
7271
tStack.add(stackTop)
@@ -86,10 +85,10 @@ data class FrameForEach(
8685
"code" %= code.serializeToNBT()
8786
if (baseStack != null)
8887
"base" %= baseStack.serializeToNBT()
89-
"accumulator" %= acc.serializeToNBT()
88+
"accumulator" %= acc.toList().serializeToNBT()
9089
}
9190

92-
override fun size() = data.size() + code.size() + acc.size + (baseStack?.size ?: 0)
91+
override fun size() = data.size() + code.size() + acc.length + (baseStack?.size ?: 0)
9392

9493
override val type: ContinuationFrame.Type<*> = TYPE
9594

@@ -104,10 +103,10 @@ data class FrameForEach(
104103
HexIotaTypes.LIST.deserialize(tag.getList("base", Tag.TAG_COMPOUND), world)!!.list.toList()
105104
else
106105
null,
107-
HexIotaTypes.LIST.deserialize(
106+
Vec.ofIterable(HexIotaTypes.LIST.deserialize(
108107
tag.getList("accumulator", Tag.TAG_COMPOUND),
109108
world
110-
)!!.list.toMutableList()
109+
)!!.list)
111110
)
112111
}
113112

Common/src/main/java/at/petrak/hexcasting/common/casting/actions/eval/OpForEach.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import at.petrak.hexcasting.api.casting.eval.vm.FrameForEach
88
import at.petrak.hexcasting.api.casting.eval.vm.SpellContinuation
99
import at.petrak.hexcasting.api.casting.getList
1010
import at.petrak.hexcasting.api.casting.mishaps.MishapNotEnoughArgs
11+
import at.petrak.hexcasting.api.utils.Vec
1112
import at.petrak.hexcasting.common.lib.hex.HexEvalSounds
1213

1314
object OpForEach : Action {
@@ -22,7 +23,7 @@ object OpForEach : Action {
2223
stack.removeLastOrNull()
2324
stack.removeLastOrNull()
2425

25-
val frame = FrameForEach(datums, instrs, null, mutableListOf())
26+
val frame = FrameForEach(datums, instrs, null, Vec.empty())
2627
val image2 = image.withUsedOp().copy(stack = stack)
2728

2829
return OperationResult(image2, listOf(), continuation.pushFrame(frame), HexEvalSounds.THOTH)

0 commit comments

Comments
 (0)