diff --git a/runtime/src/main/kotlin/konan/internal/Boxing.kt b/runtime/src/main/kotlin/konan/internal/Boxing.kt index 59534ea2fc6..7dc4f6fbdc3 100644 --- a/runtime/src/main/kotlin/konan/internal/Boxing.kt +++ b/runtime/src/main/kotlin/konan/internal/Boxing.kt @@ -20,6 +20,76 @@ class BooleanBox(val value: Boolean) : Comparable { fun boxBoolean(value: Boolean) = BooleanBox(value) +class CharBox(val value: Char) : Comparable { + override fun equals(other: Any?): Boolean { + if (other !is CharBox) { + return false + } + + return this.value == other.value + } + + override fun hashCode() = value.hashCode() + + override fun toString() = value.toString() + + override fun compareTo(other: Char): Int = value.compareTo(other) +} + +fun boxChar(value: Char) = CharBox(value) + +class ByteBox(val value: Byte) : Number(), Comparable { + override fun equals(other: Any?): Boolean { + if (other !is ByteBox) { + return false + } + + return this.value == other.value + } + + override fun hashCode() = value.hashCode() + + override fun toString() = value.toString() + + override fun compareTo(other: Byte): Int = value.compareTo(other) + + override fun toByte() = value.toByte() + override fun toChar() = value.toChar() + override fun toShort() = value.toShort() + override fun toInt() = value.toInt() + override fun toLong() = value.toLong() + override fun toFloat() = value.toFloat() + override fun toDouble() = value.toDouble() +} + +fun boxByte(value: Byte) = ByteBox(value) + +class ShortBox(val value: Short) : Number(), Comparable { + override fun equals(other: Any?): Boolean { + if (other !is ShortBox) { + return false + } + + return this.value == other.value + } + + override fun hashCode() = value.hashCode() + + override fun toString() = value.toString() + + override fun compareTo(other: Short): Int = value.compareTo(other) + + override fun toByte() = value.toByte() + override fun toChar() = value.toChar() + override fun toShort() = value.toShort() + override fun toInt() = value.toInt() + override fun toLong() = value.toLong() + override fun toFloat() = value.toFloat() + override fun toDouble() = value.toDouble() +} + +fun boxShort(value: Short) = ShortBox(value) + class IntBox(val value: Int) : Number(), Comparable { override fun equals(other: Any?): Boolean { if (other !is IntBox) { @@ -46,4 +116,80 @@ class IntBox(val value: Int) : Number(), Comparable { fun boxInt(value: Int) = IntBox(value) -// TODO: support other boxes. \ No newline at end of file +class LongBox(val value: Long) : Number(), Comparable { + override fun equals(other: Any?): Boolean { + if (other !is LongBox) { + return false + } + + return this.value == other.value + } + + override fun hashCode() = value.hashCode() + + override fun toString() = value.toString() + + override fun compareTo(other: Long): Int = value.compareTo(other) + + override fun toByte() = value.toByte() + override fun toChar() = value.toChar() + override fun toShort() = value.toShort() + override fun toInt() = value.toInt() + override fun toLong() = value.toLong() + override fun toFloat() = value.toFloat() + override fun toDouble() = value.toDouble() +} + +fun boxLong(value: Long) = LongBox(value) + +class FloatBox(val value: Float) : Number(), Comparable { + override fun equals(other: Any?): Boolean { + if (other !is FloatBox) { + return false + } + + return this.value == other.value + } + + override fun hashCode() = value.hashCode() + + override fun toString() = value.toString() + + override fun compareTo(other: Float): Int = value.compareTo(other) + + override fun toByte() = value.toByte() + override fun toChar() = value.toChar() + override fun toShort() = value.toShort() + override fun toInt() = value.toInt() + override fun toLong() = value.toLong() + override fun toFloat() = value.toFloat() + override fun toDouble() = value.toDouble() +} + +fun boxFloat(value: Float) = FloatBox(value) + +class DoubleBox(val value: Double) : Number(), Comparable { + override fun equals(other: Any?): Boolean { + if (other !is DoubleBox) { + return false + } + + return this.value == other.value + } + + override fun hashCode() = value.hashCode() + + override fun toString() = value.toString() + + override fun compareTo(other: Double): Int = value.compareTo(other) + + override fun toByte() = value.toByte() + override fun toChar() = value.toChar() + override fun toShort() = value.toShort() + override fun toInt() = value.toInt() + override fun toLong() = value.toLong() + override fun toFloat() = value.toFloat() + override fun toDouble() = value.toDouble() +} + +fun boxDouble(value: Double) = DoubleBox(value)