[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. * Returns the inverse of this boolean.
*/ */
@WasmOp(WasmOp.I32_EQZ) @WasmOp(WasmOp.I32_EQZ)
@kotlin.internal.IntrinsicConstEvaluation
public operator fun not(): Boolean = public operator fun not(): Boolean =
implementedAsIntrinsic 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. * this function does not perform short-circuit evaluation. Both `this` and [other] will always be evaluated.
*/ */
@WasmOp(WasmOp.I32_AND) @WasmOp(WasmOp.I32_AND)
@kotlin.internal.IntrinsicConstEvaluation
public infix fun and(other: Boolean): Boolean = public infix fun and(other: Boolean): Boolean =
implementedAsIntrinsic 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. * this function does not perform short-circuit evaluation. Both `this` and [other] will always be evaluated.
*/ */
@WasmOp(WasmOp.I32_OR) @WasmOp(WasmOp.I32_OR)
@kotlin.internal.IntrinsicConstEvaluation
public infix fun or(other: Boolean): Boolean = public infix fun or(other: Boolean): Boolean =
implementedAsIntrinsic 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. * Performs a logical `xor` operation between this Boolean and the [other] one.
*/ */
@WasmOp(WasmOp.I32_XOR) @WasmOp(WasmOp.I32_XOR)
@kotlin.internal.IntrinsicConstEvaluation
public infix fun xor(other: Boolean): Boolean = public infix fun xor(other: Boolean): Boolean =
implementedAsIntrinsic implementedAsIntrinsic
@kotlin.internal.IntrinsicConstEvaluation
public override fun compareTo(other: Boolean): Int = public override fun compareTo(other: Boolean): Int =
wasm_i32_compareTo(this.toInt(), other.toInt()) wasm_i32_compareTo(this.toInt(), other.toInt())
@kotlin.internal.IntrinsicConstEvaluation
override fun toString(): String = override fun toString(): String =
if (this) "true" else "false" if (this) "true" else "false"
override fun hashCode(): Int = override fun hashCode(): Int =
toInt() toInt()
@kotlin.internal.IntrinsicConstEvaluation
override fun equals(other: Any?): Boolean { override fun equals(other: Any?): Boolean {
return if (other !is Boolean) { return if (other !is Boolean) {
false 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, * 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. * or a positive number if it's greater than other.
*/ */
@kotlin.internal.IntrinsicConstEvaluation
public override fun compareTo(other: Char): Int = public override fun compareTo(other: Char): Int =
wasm_i32_compareTo(this.toInt(), other.toInt()) wasm_i32_compareTo(this.toInt(), other.toInt())
@kotlin.internal.IntrinsicConstEvaluation
public override fun equals(other: Any?): Boolean { public override fun equals(other: Any?): Boolean {
if (other is Char) if (other is Char)
return wasm_i32_eq(this.toInt(), other.toInt()) 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. */ /** Adds the other Int value to this value resulting a Char. */
@kotlin.internal.IntrinsicConstEvaluation
public inline operator fun plus(other: Int): Char = public inline operator fun plus(other: Int): Char =
(this.toInt() + other).toChar() (this.toInt() + other).toChar()
/** Subtracts the other Char value from this value resulting an Int. */ /** Subtracts the other Char value from this value resulting an Int. */
@kotlin.internal.IntrinsicConstEvaluation
public inline operator fun minus(other: Char): Int = public inline operator fun minus(other: Char): Int =
(this.toInt() - other.toInt()) (this.toInt() - other.toInt())
/** Subtracts the other Int value from this value resulting a Char. */ /** Subtracts the other Int value from this value resulting a Char. */
@kotlin.internal.IntrinsicConstEvaluation
public inline operator fun minus(other: Int): Char = public inline operator fun minus(other: Int): Char =
(this.toInt() - other).toChar() (this.toInt() - other).toChar()
@@ -74,34 +79,42 @@ public class Char private constructor(private val value: Char) : Comparable<Char
this until other this until other
/** Returns the value of this character as a `Byte`. */ /** Returns the value of this character as a `Byte`. */
@kotlin.internal.IntrinsicConstEvaluation
public inline fun toByte(): Byte = public inline fun toByte(): Byte =
this.toInt().toByte() this.toInt().toByte()
/** Returns the value of this character as a `Char`. */ /** Returns the value of this character as a `Char`. */
@kotlin.internal.IntrinsicConstEvaluation
public inline fun toChar(): Char = public inline fun toChar(): Char =
this this
/** Returns the value of this character as a `Short`. */ /** Returns the value of this character as a `Short`. */
@kotlin.internal.IntrinsicConstEvaluation
public inline fun toShort(): Short = public inline fun toShort(): Short =
this.toInt().toShort() this.toInt().toShort()
/** Returns the value of this character as a `Int`. */ /** Returns the value of this character as a `Int`. */
@WasmNoOpCast @WasmNoOpCast
@kotlin.internal.IntrinsicConstEvaluation
public fun toInt(): Int = public fun toInt(): Int =
implementedAsIntrinsic implementedAsIntrinsic
/** Returns the value of this character as a `Long`. */ /** Returns the value of this character as a `Long`. */
@kotlin.internal.IntrinsicConstEvaluation
public inline fun toLong(): Long = public inline fun toLong(): Long =
this.toInt().toLong() this.toInt().toLong()
/** Returns the value of this character as a `Float`. */ /** Returns the value of this character as a `Float`. */
@kotlin.internal.IntrinsicConstEvaluation
public inline fun toFloat(): Float = public inline fun toFloat(): Float =
this.toInt().toFloat() this.toInt().toFloat()
/** Returns the value of this character as a `Double`. */ /** Returns the value of this character as a `Double`. */
@kotlin.internal.IntrinsicConstEvaluation
public inline fun toDouble(): Double = public inline fun toDouble(): Double =
this.toInt().toDouble() this.toInt().toDouble()
@kotlin.internal.IntrinsicConstEvaluation
override fun toString(): String { override fun toString(): String {
val array = WasmCharArray(1) val array = WasmCharArray(1)
array.set(0, this) array.set(0, this)
@@ -6,6 +6,7 @@
package kotlin package kotlin
public abstract class Enum<E : Enum<E>>( public abstract class Enum<E : Enum<E>>(
@kotlin.internal.IntrinsicConstEvaluation
public val name: String, public val name: String,
public val ordinal: Int public val ordinal: Int
) : Comparable<E> { ) : Comparable<E> {
@@ -14,6 +14,7 @@ import kotlin.math.min
*/ */
public class String internal @WasmPrimitiveConstructor constructor( public class String internal @WasmPrimitiveConstructor constructor(
private var leftIfInSum: String?, private var leftIfInSum: String?,
@kotlin.internal.IntrinsicConstEvaluation
public override val length: Int, public override val length: Int,
private var _chars: WasmCharArray, private var _chars: WasmCharArray,
) : Comparable<String>, CharSequence { ) : 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. * 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 { public operator fun plus(other: Any?): String {
val right = other.toString() val right = other.toString()
return String(this, this.length + right.length, right.chars) 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 * If the [index] is out of bounds of this string, throws an [IndexOutOfBoundsException] except in Kotlin/JS
* where the behavior is unspecified. * where the behavior is unspecified.
*/ */
@kotlin.internal.IntrinsicConstEvaluation
public override fun get(index: Int): Char { public override fun get(index: Int): Char {
rangeCheck(index, this.length) rangeCheck(index, this.length)
return chars.get(index) return chars.get(index)
@@ -73,6 +76,7 @@ public class String internal @WasmPrimitiveConstructor constructor(
return newChars.createString() return newChars.createString()
} }
@kotlin.internal.IntrinsicConstEvaluation
public override fun compareTo(other: String): Int { public override fun compareTo(other: String): Int {
if (this === other) return 0 if (this === other) return 0
val thisChars = this.chars val thisChars = this.chars
@@ -89,6 +93,7 @@ public class String internal @WasmPrimitiveConstructor constructor(
return thisLength - otherLength return thisLength - otherLength
} }
@kotlin.internal.IntrinsicConstEvaluation
public override fun equals(other: Any?): Boolean { public override fun equals(other: Any?): Boolean {
if (other == null) return false if (other == null) return false
if (other === this) return true if (other === this) return true
@@ -110,6 +115,7 @@ public class String internal @WasmPrimitiveConstructor constructor(
return true return true
} }
@kotlin.internal.IntrinsicConstEvaluation
public override fun toString(): String = this public override fun toString(): String = this
public override fun hashCode(): Int { 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>", * - property accessors: the getter for a property named "foo" will have the name "<get-foo>",
* the setter, similarly, will have the name "<set-foo>". * the setter, similarly, will have the name "<set-foo>".
*/ */
@kotlin.internal.IntrinsicConstEvaluation
actual public val name: String actual public val name: String
} }