From dd0267f4ad4ef9532528f3d7e4acf1f5f6d2692f Mon Sep 17 00:00:00 2001 From: Ivan Kylchik Date: Thu, 2 Feb 2023 12:19:07 +0100 Subject: [PATCH] [WASM] Mark necessary methods that must be evaluated at compile-time It includes methods and properties from Boolean, Char, String, Enum and KCallable. --- libraries/stdlib/wasm/builtins/kotlin/Boolean.kt | 7 +++++++ libraries/stdlib/wasm/builtins/kotlin/Char.kt | 13 +++++++++++++ libraries/stdlib/wasm/builtins/kotlin/Enum.kt | 1 + libraries/stdlib/wasm/builtins/kotlin/String.kt | 6 ++++++ .../stdlib/wasm/src/kotlin/reflect/KCallable.kt | 1 + 5 files changed, 28 insertions(+) diff --git a/libraries/stdlib/wasm/builtins/kotlin/Boolean.kt b/libraries/stdlib/wasm/builtins/kotlin/Boolean.kt index bbb20648e44..c76144d6402 100644 --- a/libraries/stdlib/wasm/builtins/kotlin/Boolean.kt +++ b/libraries/stdlib/wasm/builtins/kotlin/Boolean.kt @@ -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 diff --git a/libraries/stdlib/wasm/builtins/kotlin/Char.kt b/libraries/stdlib/wasm/builtins/kotlin/Char.kt index 24d9d9cbbe0..71612243a43 100644 --- a/libraries/stdlib/wasm/builtins/kotlin/Char.kt +++ b/libraries/stdlib/wasm/builtins/kotlin/Char.kt @@ -22,9 +22,11 @@ public class Char private constructor(private val value: Char) : Comparable>( + @kotlin.internal.IntrinsicConstEvaluation public val name: String, public val ordinal: Int ) : Comparable { diff --git a/libraries/stdlib/wasm/builtins/kotlin/String.kt b/libraries/stdlib/wasm/builtins/kotlin/String.kt index 8908ef777d4..85d08d82abc 100644 --- a/libraries/stdlib/wasm/builtins/kotlin/String.kt +++ b/libraries/stdlib/wasm/builtins/kotlin/String.kt @@ -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, 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 { diff --git a/libraries/stdlib/wasm/src/kotlin/reflect/KCallable.kt b/libraries/stdlib/wasm/src/kotlin/reflect/KCallable.kt index 15a8c0ec4f2..f35305ebf48 100644 --- a/libraries/stdlib/wasm/src/kotlin/reflect/KCallable.kt +++ b/libraries/stdlib/wasm/src/kotlin/reflect/KCallable.kt @@ -19,5 +19,6 @@ public actual interface KCallable { * - property accessors: the getter for a property named "foo" will have the name "", * the setter, similarly, will have the name "". */ + @kotlin.internal.IntrinsicConstEvaluation actual public val name: String }