[JS IR BE] Support String companion object
This commit is contained in:
+13
-7
@@ -154,14 +154,20 @@ class JsIrBackendContext(
|
||||
|
||||
val functions = (0..22).map { symbolTable.referenceClass(builtIns.getFunction(it)) }
|
||||
|
||||
val primitiveCompanionObjects = PrimitiveType.NUMBER_TYPES
|
||||
.asSequence()
|
||||
.filter { it.name != "LONG" && it.name != "CHAR" } // skip due to they have own explicit companions
|
||||
.map {
|
||||
it.typeName to symbolTable.lazyWrapper.referenceClass(
|
||||
getClass(JS_INTERNAL_PACKAGE_FQNAME.child(Name.identifier("${it.typeName.identifier}CompanionObject")))
|
||||
private fun primitivesWithImplicitCompanionObject(): List<Name> {
|
||||
val numbers = PrimitiveType.NUMBER_TYPES
|
||||
.filter { it.name != "LONG" && it.name != "CHAR" } // skip due to they have own explicit companions
|
||||
.map { it.typeName }
|
||||
|
||||
return numbers + listOf(Name.identifier("String"))
|
||||
}
|
||||
|
||||
val primitiveCompanionObjects = primitivesWithImplicitCompanionObject()
|
||||
.associate {
|
||||
it to symbolTable.lazyWrapper.referenceClass(
|
||||
getClass(JS_INTERNAL_PACKAGE_FQNAME.child(Name.identifier("${it.identifier}CompanionObject")))
|
||||
)
|
||||
}.toMap()
|
||||
}
|
||||
|
||||
val suspendFunctions = (0..22).map { symbolTable.referenceClass(builtIns.getSuspendFunction(it)) }
|
||||
|
||||
|
||||
+2
-1
@@ -13,6 +13,7 @@ import org.jetbrains.kotlin.ir.expressions.IrExpression
|
||||
import org.jetbrains.kotlin.ir.expressions.IrGetObjectValue
|
||||
import org.jetbrains.kotlin.ir.expressions.impl.IrGetObjectValueImpl
|
||||
import org.jetbrains.kotlin.ir.types.isPrimitiveType
|
||||
import org.jetbrains.kotlin.ir.types.isString
|
||||
import org.jetbrains.kotlin.ir.util.defaultType
|
||||
import org.jetbrains.kotlin.ir.visitors.IrElementTransformerVoid
|
||||
import org.jetbrains.kotlin.ir.visitors.transformChildrenVoid
|
||||
@@ -26,7 +27,7 @@ class PrimitiveCompanionLowering(val context: JsIrBackendContext) : FileLowering
|
||||
val declaration = symbol.owner
|
||||
if (!declaration.isCompanion) return expression
|
||||
val parent = declaration.parent as IrClass
|
||||
if (!parent.defaultType.isPrimitiveType()) return expression
|
||||
if (!parent.defaultType.isPrimitiveType() && !parent.defaultType.isString()) return expression
|
||||
val actualCompanion = context.primitiveCompanionObjects[parent.name] ?: return expression
|
||||
return expression.run { IrGetObjectValueImpl(startOffset, endOffset, actualCompanion.owner.defaultType, actualCompanion) }
|
||||
}
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
// IGNORE_BACKEND: JS_IR
|
||||
package foo
|
||||
|
||||
fun box(): String {
|
||||
|
||||
Reference in New Issue
Block a user