[WASM] Mark necessary methods that must be evaluated at compile-time

It includes methods and properties from Boolean, Char, String,
Enum and KCallable.
This commit is contained in:
Ivan Kylchik
2023-02-02 12:19:07 +01:00
committed by Space Team
parent 695229e288
commit dd0267f4ad
5 changed files with 28 additions and 0 deletions
@@ -19,6 +19,7 @@ public class Boolean private constructor(private val value: Boolean) : Comparabl
* Returns the inverse of this boolean.
*/
@WasmOp(WasmOp.I32_EQZ)
@kotlin.internal.IntrinsicConstEvaluation
public operator fun not(): Boolean =
implementedAsIntrinsic
@@ -27,6 +28,7 @@ public class Boolean private constructor(private val value: Boolean) : Comparabl
* this function does not perform short-circuit evaluation. Both `this` and [other] will always be evaluated.
*/
@WasmOp(WasmOp.I32_AND)
@kotlin.internal.IntrinsicConstEvaluation
public infix fun and(other: Boolean): Boolean =
implementedAsIntrinsic
@@ -35,6 +37,7 @@ public class Boolean private constructor(private val value: Boolean) : Comparabl
* this function does not perform short-circuit evaluation. Both `this` and [other] will always be evaluated.
*/
@WasmOp(WasmOp.I32_OR)
@kotlin.internal.IntrinsicConstEvaluation
public infix fun or(other: Boolean): Boolean =
implementedAsIntrinsic
@@ -42,18 +45,22 @@ public class Boolean private constructor(private val value: Boolean) : Comparabl
* Performs a logical `xor` operation between this Boolean and the [other] one.
*/
@WasmOp(WasmOp.I32_XOR)
@kotlin.internal.IntrinsicConstEvaluation
public infix fun xor(other: Boolean): Boolean =
implementedAsIntrinsic
@kotlin.internal.IntrinsicConstEvaluation
public override fun compareTo(other: Boolean): Int =
wasm_i32_compareTo(this.toInt(), other.toInt())
@kotlin.internal.IntrinsicConstEvaluation
override fun toString(): String =
if (this) "true" else "false"
override fun hashCode(): Int =
toInt()
@kotlin.internal.IntrinsicConstEvaluation
override fun equals(other: Any?): Boolean {
return if (other !is Boolean) {
false
@@ -22,9 +22,11 @@ public class Char private constructor(private val value: Char) : Comparable<Char
* Returns zero if this value is equal to the specified other value, a negative number if it's less than other,
* or a positive number if it's greater than other.
*/
@kotlin.internal.IntrinsicConstEvaluation
public override fun compareTo(other: Char): Int =
wasm_i32_compareTo(this.toInt(), other.toInt())
@kotlin.internal.IntrinsicConstEvaluation
public override fun equals(other: Any?): Boolean {
if (other is Char)
return wasm_i32_eq(this.toInt(), other.toInt())
@@ -32,14 +34,17 @@ public class Char private constructor(private val value: Char) : Comparable<Char
}
/** Adds the other Int value to this value resulting a Char. */
@kotlin.internal.IntrinsicConstEvaluation
public inline operator fun plus(other: Int): Char =
(this.toInt() + other).toChar()
/** Subtracts the other Char value from this value resulting an Int. */
@kotlin.internal.IntrinsicConstEvaluation
public inline operator fun minus(other: Char): Int =
(this.toInt() - other.toInt())
/** Subtracts the other Int value from this value resulting a Char. */
@kotlin.internal.IntrinsicConstEvaluation
public inline operator fun minus(other: Int): Char =
(this.toInt() - other).toChar()
@@ -74,34 +79,42 @@ public class Char private constructor(private val value: Char) : Comparable<Char
this until other
/** Returns the value of this character as a `Byte`. */
@kotlin.internal.IntrinsicConstEvaluation
public inline fun toByte(): Byte =
this.toInt().toByte()
/** Returns the value of this character as a `Char`. */
@kotlin.internal.IntrinsicConstEvaluation
public inline fun toChar(): Char =
this
/** Returns the value of this character as a `Short`. */
@kotlin.internal.IntrinsicConstEvaluation
public inline fun toShort(): Short =
this.toInt().toShort()
/** Returns the value of this character as a `Int`. */
@WasmNoOpCast
@kotlin.internal.IntrinsicConstEvaluation
public fun toInt(): Int =
implementedAsIntrinsic
/** Returns the value of this character as a `Long`. */
@kotlin.internal.IntrinsicConstEvaluation
public inline fun toLong(): Long =
this.toInt().toLong()
/** Returns the value of this character as a `Float`. */
@kotlin.internal.IntrinsicConstEvaluation
public inline fun toFloat(): Float =
this.toInt().toFloat()
/** Returns the value of this character as a `Double`. */
@kotlin.internal.IntrinsicConstEvaluation
public inline fun toDouble(): Double =
this.toInt().toDouble()
@kotlin.internal.IntrinsicConstEvaluation
override fun toString(): String {
val array = WasmCharArray(1)
array.set(0, this)
@@ -6,6 +6,7 @@
package kotlin
public abstract class Enum<E : Enum<E>>(
@kotlin.internal.IntrinsicConstEvaluation
public val name: String,
public val ordinal: Int
) : Comparable<E> {
@@ -14,6 +14,7 @@ import kotlin.math.min
*/
public class String internal @WasmPrimitiveConstructor constructor(
private var leftIfInSum: String?,
@kotlin.internal.IntrinsicConstEvaluation
public override val length: Int,
private var _chars: WasmCharArray,
) : Comparable<String>, CharSequence {
@@ -22,6 +23,7 @@ public class String internal @WasmPrimitiveConstructor constructor(
/**
* Returns a string obtained by concatenating this string with the string representation of the given [other] object.
*/
@kotlin.internal.IntrinsicConstEvaluation
public operator fun plus(other: Any?): String {
val right = other.toString()
return String(this, this.length + right.length, right.chars)
@@ -33,6 +35,7 @@ public class String internal @WasmPrimitiveConstructor constructor(
* If the [index] is out of bounds of this string, throws an [IndexOutOfBoundsException] except in Kotlin/JS
* where the behavior is unspecified.
*/
@kotlin.internal.IntrinsicConstEvaluation
public override fun get(index: Int): Char {
rangeCheck(index, this.length)
return chars.get(index)
@@ -73,6 +76,7 @@ public class String internal @WasmPrimitiveConstructor constructor(
return newChars.createString()
}
@kotlin.internal.IntrinsicConstEvaluation
public override fun compareTo(other: String): Int {
if (this === other) return 0
val thisChars = this.chars
@@ -89,6 +93,7 @@ public class String internal @WasmPrimitiveConstructor constructor(
return thisLength - otherLength
}
@kotlin.internal.IntrinsicConstEvaluation
public override fun equals(other: Any?): Boolean {
if (other == null) return false
if (other === this) return true
@@ -110,6 +115,7 @@ public class String internal @WasmPrimitiveConstructor constructor(
return true
}
@kotlin.internal.IntrinsicConstEvaluation
public override fun toString(): String = this
public override fun hashCode(): Int {
@@ -19,5 +19,6 @@ public actual interface KCallable<out R> {
* - property accessors: the getter for a property named "foo" will have the name "<get-foo>",
* the setter, similarly, will have the name "<set-foo>".
*/
@kotlin.internal.IntrinsicConstEvaluation
actual public val name: String
}