Skip to content

Commit

Permalink
replace stack array reference result function with exception variant
Browse files Browse the repository at this point in the history
  • Loading branch information
CharlieTap committed Jan 16, 2025
1 parent 8d2ccd8 commit dde3162
Show file tree
Hide file tree
Showing 8 changed files with 15 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,9 @@ internal inline fun ArrayCopyExecutor(

val elementsToCopy = stack.popI32()
val sourceOffset = stack.popI32()
val srcReference = stack.popArrayReference().bind()
val source = stack.popArrayReference()
val destinationOffset = stack.popI32()
val destReference = stack.popArrayReference().bind()

val source = srcReference.instance
val destination = destReference.instance
val destination = stack.popArrayReference()

if (destinationOffset + elementsToCopy > destination.fields.size) {
Err(InvocationError.Trap.TrapEncountered).bind()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,11 @@ internal inline fun ArrayFillExecutor(
crossinline definedTypeExpander: DefinedTypeExpander,
crossinline fieldPacker: FieldPacker,
) {

val (stack) = context
val elementsToFill = stack.popI32()
val fillValue = stack.popValue()
val arrayElementOffset = stack.popI32()
val arrayReference = stack.popArrayReference().bind()
val arrayInstance = arrayReference.instance
val arrayInstance = stack.popArrayReference()

if (arrayElementOffset + elementsToFill > arrayInstance.fields.size) {
Err(InvocationError.Trap.TrapEncountered).bind()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,7 @@ internal inline fun ArrayGetExecutor(
val fieldType = arrayType.fieldType

val fieldIndex = stack.popI32()
val arrayRef = stack.popArrayReference().bind()

val arrayInstance = arrayRef.instance
val arrayInstance = stack.popArrayReference()

val fieldValue = arrayInstance.field(Index.FieldIndex(fieldIndex.toUInt())).bind()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,7 @@ internal fun ArrayInitDataExecutor(
val byteArrayOffset = stack.popI32()
val arrayOffset = stack.popI32()

val arrayReference = stack.popArrayReference().bind()
val arrayInstance = arrayReference.instance
val arrayInstance = stack.popArrayReference()

val arrayElementSizeInBytes = arrayType.fieldType
.bitWidth()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,7 @@ internal inline fun ArrayInitElementExecutor(
val elementsToCopy = stack.popI32()
val sourceOffsetInElementSegment = stack.popI32()
val destinationOffsetInArray = stack.popI32()

val arrayReference = stack.popArrayReference().bind()
val arrayInstance = arrayReference.instance
val arrayInstance = stack.popArrayReference()

if (
(destinationOffsetInArray + elementsToCopy > arrayInstance.fields.size) ||
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package io.github.charlietap.chasm.executor.invoker.instruction.aggregate

import io.github.charlietap.chasm.executor.invoker.ext.bind
import io.github.charlietap.chasm.executor.runtime.execution.ExecutionContext
import io.github.charlietap.chasm.executor.runtime.ext.popArrayReference
import io.github.charlietap.chasm.executor.runtime.instruction.AggregateInstruction
Expand All @@ -11,8 +10,7 @@ internal fun ArrayLenExecutor(
instruction: AggregateInstruction.ArrayLen,
) {
val (stack) = context
val arrayReference = stack.popArrayReference().bind()
val arrayInstance = arrayReference.instance
val arrayInstance = stack.popArrayReference()

stack.push(NumberValue.I32(arrayInstance.fields.size))
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,8 @@ internal inline fun ArraySetExecutor(
val value = stack.popValue()

val fieldIndex = stack.popI32()
val arrayReference = stack.popArrayReference().bind()
val arrayInstance = stack.popArrayReference()

val arrayInstance = arrayReference.instance
val fieldValue = fieldPacker(value, arrayType.fieldType).bind()

if (fieldIndex !in arrayInstance.fields.indices) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import io.github.charlietap.chasm.executor.runtime.Stack
import io.github.charlietap.chasm.executor.runtime.error.InvocationError
import io.github.charlietap.chasm.executor.runtime.exception.ExceptionHandler
import io.github.charlietap.chasm.executor.runtime.exception.InvocationException
import io.github.charlietap.chasm.executor.runtime.instance.ArrayInstance
import io.github.charlietap.chasm.executor.runtime.instance.StructInstance
import io.github.charlietap.chasm.executor.runtime.store.Address
import io.github.charlietap.chasm.executor.runtime.value.ExecutionValue
Expand Down Expand Up @@ -106,10 +107,12 @@ inline fun Stack.popI31Reference(): Result<ReferenceValue.I31, InvocationError.M
} ?: Err(InvocationError.MissingStackValue)
}

inline fun Stack.popArrayReference(): Result<ReferenceValue.Array, InvocationError.MissingStackValue> {
return (popValue() as? ReferenceValue.Array)?.let {
Ok(it)
} ?: Err(InvocationError.MissingStackValue)
inline fun Stack.popArrayReference(): ArrayInstance {
return try {
(popValue() as ReferenceValue.Array).instance
} catch (_: ClassCastException) {
throw InvocationException(InvocationError.ArrayReferenceExpected)
}
}

inline fun Stack.popStructReference(): StructInstance {
Expand Down

0 comments on commit dde3162

Please sign in to comment.