[IR] Drop treatFloatInSpecialWay interpreter's parameter

Replaced it with more common `platform` parameter.
This commit is contained in:
Ivan Kylchik
2023-05-25 17:27:03 +02:00
committed by Space Team
parent 19b0ff8fb4
commit 40aea531e1
4 changed files with 14 additions and 11 deletions
@@ -425,8 +425,8 @@ class Fir2IrConverter(
val intrinsicConstEvaluation = languageVersionSettings?.supportsFeature(LanguageFeature.IntrinsicConstEvaluation) == true
val configuration = IrInterpreterConfiguration(
platform = targetPlatform,
printOnlyExceptionMessage = true,
treatFloatInSpecialWay = targetPlatform.isJs()
)
val interpreter = IrInterpreter(IrInterpreterEnvironment(irModuleFragment.irBuiltins, configuration))
val mode = if (intrinsicConstEvaluation) EvaluationMode.ONLY_INTRINSIC_CONST else EvaluationMode.ONLY_BUILTINS
@@ -23,6 +23,7 @@ import org.jetbrains.kotlin.ir.types.*
import org.jetbrains.kotlin.ir.util.*
import org.jetbrains.kotlin.ir.util.hasAnnotation
import org.jetbrains.kotlin.name.FqName
import org.jetbrains.kotlin.platform.isJs
import java.lang.invoke.MethodHandle
internal interface CallInterceptor {
@@ -166,7 +167,7 @@ internal class DefaultCallInterceptor(override val interpreter: IrInterpreter) :
}
private fun interpretBuiltinFunction(signature: Signature): Any? {
if (environment.configuration.treatFloatInSpecialWay) {
if (environment.configuration.platform.isJs()) {
if (signature.name == "toString") return signature.args[0].value.specialToStringForJs()
if (signature.name == "toFloat") signature.name = "toDouble"
signature.args.filter { it.type == "Float" }.forEach {
@@ -5,20 +5,22 @@
package org.jetbrains.kotlin.ir.interpreter
import org.jetbrains.kotlin.platform.TargetPlatform
/**
* @param platform is used to apply unique platform rules. For example, for JS we must handle `Float` values in special way.
* @param maxStack describes the maximum allowed call stack size
* @param maxCommands describes the maximum allowed number of simple instructions performed
* @param createNonCompileTimeObjects
* 'true' - interpreter will construct object and initialize its properties despite the fact it is not marked as compile time;
* 'false' - interpreter will create a representation of empty object, that can be used to get const properties
* @param treatFloatInSpecialWay interpret float const as if it was a double and avoid `toFloat` evaluation if possible
*/
// TODO maybe create some sort of builder
data class IrInterpreterConfiguration(
val platform: TargetPlatform? = null,
val maxStack: Int = 10_000,
val maxCommands: Int = 1_000_000,
val createNonCompileTimeObjects: Boolean = false,
val printOnlyExceptionMessage: Boolean = false,
val collapseStackTraceFromJDK: Boolean = true,
val treatFloatInSpecialWay: Boolean = false,
)
@@ -21,6 +21,7 @@ import org.jetbrains.kotlin.ir.types.*
import org.jetbrains.kotlin.ir.util.defaultType
import org.jetbrains.kotlin.ir.util.isSubclassOf
import org.jetbrains.kotlin.ir.util.properties
import org.jetbrains.kotlin.platform.isJs
class IrInterpreterEnvironment(
val irBuiltIns: IrBuiltIns,
@@ -117,13 +118,12 @@ class IrInterpreterEnvironment(
val end = original.endOffset
val type = original.type.makeNotNull()
return when (state) {
is Primitive<*> ->
when {
configuration.treatFloatInSpecialWay && state.value is Float -> IrConstImpl.float(start, end, type, state.value)
configuration.treatFloatInSpecialWay && state.value is Double -> IrConstImpl.double(start, end, type, state.value)
state.value == null || type.isPrimitiveType() || type.isString() -> state.value.toIrConst(type, start, end)
else -> original // TODO support for arrays
}
is Primitive<*> -> when {
configuration.platform.isJs() && state.value is Float -> IrConstImpl.float(start, end, type, state.value)
configuration.platform.isJs() && state.value is Double -> IrConstImpl.double(start, end, type, state.value)
state.value == null || type.isPrimitiveType() || type.isString() -> state.value.toIrConst(type, start, end)
else -> original // TODO support for arrays
}
is ExceptionState -> {
val message = if (configuration.printOnlyExceptionMessage) state.getShortDescription() else "\n" + state.getFullDescription()
IrErrorExpressionImpl(original.startOffset, original.endOffset, original.type, message)