JS: support unsigned constants inside string templates

This commit is contained in:
Anton Bannykh
2018-08-17 14:25:23 +03:00
committed by Ilya Gorbunov
parent 6103df0b31
commit 2663d9751a
3 changed files with 24 additions and 2 deletions
@@ -365,7 +365,7 @@ private class ConstantExpressionEvaluatorVisitor(
return when (constantValue) {
is ErrorValue, is EnumValue -> return null
is NullValue -> StringValue("null")
else -> StringValue(constantValue.value.toString())
else -> StringValue(constantValue.stringTemplateValue())
}.wrap(compileTimeConstant.parameters)
}
@@ -1,13 +1,18 @@
// WITH_UNSIGNED
// IGNORE_BACKEND: JVM_IR, JS_IR, JS
// IGNORE_BACKEND: JVM_IR, JS_IR
const val MAX_BYTE: UByte = 0xFFu
const val HUNDRED: UByte = 100u
const val MAX_LONG: ULong = ULong.MAX_VALUE
fun box(): String {
val maxByteStringSingle = "$MAX_BYTE"
if (maxByteStringSingle != MAX_BYTE.toString() || maxByteStringSingle != "255") return "Fail 1: $maxByteStringSingle"
val maxLongStringSingle = "$MAX_LONG"
if (maxLongStringSingle != MAX_LONG.toString() || maxLongStringSingle != "18446744073709551615") return "Fail 1.5: $maxLongStringSingle"
val twoHundredUByte = "${(HUNDRED * 2u).toUByte()}"
if (twoHundredUByte != "200") return "Fail 2: $twoHundredUByte"
@@ -37,6 +37,8 @@ abstract class ConstantValue<out T>(open val value: T) {
override fun hashCode(): Int = value?.hashCode() ?: 0
override fun toString(): String = value.toString()
open fun stringTemplateValue(): String = value.toString()
}
abstract class IntegerValueConstant<out T> protected constructor(value: T) : ConstantValue<T>(value)
@@ -200,6 +202,8 @@ class UByteValue(byteValue: Byte) : UnsignedValueConstant<Byte>(byteValue) {
override fun <R, D> accept(visitor: AnnotationArgumentVisitor<R, D>, data: D): R = visitor.visitUByteValue(this, data)
override fun toString() = "$value.toUByte()"
override fun stringTemplateValue(): String = (value.toInt() and 0xFF).toString()
}
class UShortValue(shortValue: Short) : UnsignedValueConstant<Short>(shortValue) {
@@ -211,6 +215,8 @@ class UShortValue(shortValue: Short) : UnsignedValueConstant<Short>(shortValue)
override fun <R, D> accept(visitor: AnnotationArgumentVisitor<R, D>, data: D): R = visitor.visitUShortValue(this, data)
override fun toString() = "$value.toUShort()"
override fun stringTemplateValue(): String = (value.toInt() and 0xFFFF).toString()
}
class UIntValue(intValue: Int) : UnsignedValueConstant<Int>(intValue) {
@@ -222,6 +228,8 @@ class UIntValue(intValue: Int) : UnsignedValueConstant<Int>(intValue) {
override fun <R, D> accept(visitor: AnnotationArgumentVisitor<R, D>, data: D) = visitor.visitUIntValue(this, data)
override fun toString() = "$value.toUInt()"
override fun stringTemplateValue(): String = (value.toLong() and 0xFFFFFFFFL).toString()
}
class ULongValue(longValue: Long) : UnsignedValueConstant<Long>(longValue) {
@@ -233,4 +241,13 @@ class ULongValue(longValue: Long) : UnsignedValueConstant<Long>(longValue) {
override fun <R, D> accept(visitor: AnnotationArgumentVisitor<R, D>, data: D): R = visitor.visitULongValue(this, data)
override fun toString() = "$value.toULong()"
override fun stringTemplateValue(): String {
if (value >= 0) return value.toString()
val div10 = (value ushr 1) / 5
val mod10 = value - 10 * div10
return "$div10$mod10"
}
}