Skip to content

Commit

Permalink
replace i31 stack result fun with exception variant
Browse files Browse the repository at this point in the history
  • Loading branch information
CharlieTap committed Jan 16, 2025
1 parent dde3162 commit a1837b0
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 35 deletions.
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
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.extendSigned
import io.github.charlietap.chasm.executor.runtime.ext.extendUnsigned
import io.github.charlietap.chasm.executor.runtime.ext.popI31Reference
import io.github.charlietap.chasm.executor.runtime.instruction.AggregateInstruction
import io.github.charlietap.chasm.executor.runtime.value.NumberValue
import io.github.charlietap.chasm.executor.runtime.value.ReferenceValue

internal fun I31GetSignedExecutor(
context: ExecutionContext,
Expand All @@ -16,19 +14,19 @@ internal fun I31GetSignedExecutor(
I31GetExecutor(
context = context,
signedExtension = true,
i31SignedExtender = ReferenceValue.I31::extendSigned,
i31UnsignedExtender = ReferenceValue.I31::extendUnsigned,
i31SignedExtender = UInt::extendSigned,
i31UnsignedExtender = UInt::extendUnsigned,
)

internal inline fun I31GetExecutor(
context: ExecutionContext,
signedExtension: Boolean,
crossinline i31SignedExtender: (ReferenceValue.I31) -> Int,
crossinline i31UnsignedExtender: (ReferenceValue.I31) -> Int,
crossinline i31SignedExtender: (UInt) -> Int,
crossinline i31UnsignedExtender: (UInt) -> Int,
) {

val (stack) = context
val value = stack.popI31Reference().bind()
val value = stack.popI31Reference()

val extended = if (signedExtension) {
i31SignedExtender(value)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import io.github.charlietap.chasm.executor.runtime.execution.ExecutionContext
import io.github.charlietap.chasm.executor.runtime.ext.extendSigned
import io.github.charlietap.chasm.executor.runtime.ext.extendUnsigned
import io.github.charlietap.chasm.executor.runtime.instruction.AggregateInstruction
import io.github.charlietap.chasm.executor.runtime.value.ReferenceValue

internal fun I31GetUnsignedExecutor(
context: ExecutionContext,
Expand All @@ -13,6 +12,6 @@ internal fun I31GetUnsignedExecutor(
I31GetExecutor(
context = context,
signedExtension = false,
i31SignedExtender = ReferenceValue.I31::extendSigned,
i31UnsignedExtender = ReferenceValue.I31::extendUnsigned,
i31SignedExtender = UInt::extendSigned,
i31UnsignedExtender = UInt::extendUnsigned,
)
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package io.github.charlietap.chasm.executor.invoker.instruction.aggregate

import io.github.charlietap.chasm.executor.invoker.fixture.executionContext
import io.github.charlietap.chasm.executor.runtime.value.ReferenceValue
import io.github.charlietap.chasm.fixture.executor.runtime.stack
import io.github.charlietap.chasm.fixture.executor.runtime.value.i31ReferenceValue
import io.github.charlietap.chasm.fixture.executor.runtime.value.i32
Expand All @@ -22,12 +21,12 @@ class I31GetExecutorTest {

stack.push(i31)

val i31SignedExtender: (ReferenceValue.I31) -> Int = { input ->
assertEquals(i31, input)
val i31SignedExtender: (UInt) -> Int = { input ->
assertEquals(i31.value, input)
i32
}

val i31UnsignedExtender: (ReferenceValue.I31) -> Int = { _ ->
val i31UnsignedExtender: (UInt) -> Int = { _ ->
fail("Unsigned extender should not be called in this scenario")
}

Expand All @@ -48,12 +47,12 @@ class I31GetExecutorTest {

stack.push(i31)

val i31SignedExtender: (ReferenceValue.I31) -> Int = { _ ->
val i31SignedExtender: (UInt) -> Int = { _ ->
fail("Unsigned extender should not be called in this scenario")
}

val i31UnsignedExtender: (ReferenceValue.I31) -> Int = { input ->
assertEquals(i31, input)
val i31UnsignedExtender: (UInt) -> Int = { input ->
assertEquals(i31.value, input)
i32
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,8 @@ sealed interface InvocationError : ModuleTrapError {

data object ArrayReferenceExpected : InvocationError

data object I31ReferenceExpected : InvocationError

data object UnexpectedReferenceValue : InvocationError

data object UndefinedDefaultBottomType : InvocationError
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,12 @@ inline fun Stack.peekReference(): ReferenceValue {
}
}

inline fun Stack.popI31Reference(): Result<ReferenceValue.I31, InvocationError.MissingStackValue> {
return (popValue() as? ReferenceValue.I31)?.let {
Ok(it)
} ?: Err(InvocationError.MissingStackValue)
inline fun Stack.popI31Reference(): UInt {
return try {
(popValue() as ReferenceValue.I31).value
} catch (_: ClassCastException) {
throw InvocationException(InvocationError.I31ReferenceExpected)
}
}

inline fun Stack.popArrayReference(): ArrayInstance {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package io.github.charlietap.chasm.executor.runtime.ext

inline fun UInt.extendSigned(): Int {
val signBit = 1 shl 30
return if ((this and signBit.toUInt()) != 0u) {
this.toInt() or (Int.MIN_VALUE)
} else {
this.toInt()
}
}

inline fun UInt.extendUnsigned(): Int = this.toInt()

0 comments on commit a1837b0

Please sign in to comment.