[JS IR] Review remarks

- Move origin to common place
- Add comments and todo about solution
- Remove MODULE directive from tests
- Add test with in-place using of js function
This commit is contained in:
Ilya Goncharov
2021-05-14 13:27:08 +03:00
committed by TeamCityServer
parent 507516e44d
commit 74d1812461
10 changed files with 66 additions and 10 deletions
@@ -386,11 +386,19 @@ fun usefulDeclarations(roots: Iterable<IrDeclaration>, context: JsIrBackendConte
val ref = expression.getTypeArgument(0)!!.classifierOrFail.owner as IrDeclaration
ref.enqueue("intrinsic: jsClass")
referencedJsClasses += ref
if (expression.origin == ClassReferenceLowering.Companion.CLASS_REFERENCE) {
// When class reference provided as parameter to external function
// It can be instantiated by external JS script
// Need to leave constructor for this
// https://youtrack.jetbrains.com/issue/KT-46672
// TODO: Possibly solution with origin is not so good
// There is option with applying this hack to jsGetKClass
if (expression.origin == JsLoweredDeclarationOrigin.CLASS_REFERENCE) {
// Maybe we need to filter primary constructor
// Although at this time, we should have only primary constructor
(ref as IrClass)
.constructors
.forEach {
it.enqueue("intrinsic: jsClass constructor")
it.enqueue("intrinsic: jsClass (constructor)")
}
}
}
@@ -17,4 +17,5 @@ object JsLoweredDeclarationOrigin : IrDeclarationOrigin {
object BRIDGE_WITHOUT_STABLE_NAME : IrDeclarationOriginImpl("BRIDGE_WITHOUT_STABLE_NAME")
object OBJECT_GET_INSTANCE_FUNCTION : IrDeclarationOriginImpl("OBJECT_GET_INSTANCE_FUNCTION")
object JS_SHADOWED_EXPORT : IrDeclarationOriginImpl("JS_SHADOWED_EXPORT")
object CLASS_REFERENCE : IrStatementOriginImpl("CLASS_REFERENCE")
}
@@ -9,6 +9,7 @@ import org.jetbrains.kotlin.backend.common.BodyLoweringPass
import org.jetbrains.kotlin.backend.common.IrElementTransformerVoidWithContext
import org.jetbrains.kotlin.backend.common.ir.Symbols
import org.jetbrains.kotlin.ir.backend.js.JsIrBackendContext
import org.jetbrains.kotlin.ir.backend.js.JsLoweredDeclarationOrigin
import org.jetbrains.kotlin.ir.backend.js.ir.JsIrBuilder
import org.jetbrains.kotlin.ir.backend.js.utils.toJsArrayLiteral
import org.jetbrains.kotlin.ir.declarations.*
@@ -130,7 +131,7 @@ class ClassReferenceLowering(val context: JsIrBackendContext) : BodyLoweringPass
JsIrBuilder.buildCall(
intrinsics.jsClass,
typeArguments = listOf(type),
origin = CLASS_REFERENCE
origin = JsLoweredDeclarationOrigin.CLASS_REFERENCE
)
private fun buildCall(name: IrSimpleFunctionSymbol, vararg args: IrExpression): IrExpression =
@@ -256,9 +257,5 @@ class ClassReferenceLowering(val context: JsIrBackendContext) : BodyLoweringPass
}
})
}
companion object {
object CLASS_REFERENCE : IrStatementOriginImpl("CLASS_REFERENCE")
}
}
@@ -7136,6 +7136,11 @@ public class IrBoxJsES6TestGenerated extends AbstractIrBoxJsES6Test {
runTest("js/js.translator/testData/box/reflection/kClassWithJsCall.kt");
}
@TestMetadata("kJsClassWithJsCall.kt")
public void testKJsClassWithJsCall() throws Exception {
runTest("js/js.translator/testData/box/reflection/kJsClassWithJsCall.kt");
}
@TestMetadata("kTypeWithJsCall.kt")
public void testKTypeWithJsCall() throws Exception {
runTest("js/js.translator/testData/box/reflection/kTypeWithJsCall.kt");
@@ -7136,6 +7136,11 @@ public class IrBoxJsTestGenerated extends AbstractIrBoxJsTest {
runTest("js/js.translator/testData/box/reflection/kClassWithJsCall.kt");
}
@TestMetadata("kJsClassWithJsCall.kt")
public void testKJsClassWithJsCall() throws Exception {
runTest("js/js.translator/testData/box/reflection/kJsClassWithJsCall.kt");
}
@TestMetadata("kTypeWithJsCall.kt")
public void testKTypeWithJsCall() throws Exception {
runTest("js/js.translator/testData/box/reflection/kTypeWithJsCall.kt");
@@ -7161,6 +7161,11 @@ public class BoxJsTestGenerated extends AbstractBoxJsTest {
runTest("js/js.translator/testData/box/reflection/kClassWithJsCall.kt");
}
@TestMetadata("kJsClassWithJsCall.kt")
public void testKJsClassWithJsCall() throws Exception {
runTest("js/js.translator/testData/box/reflection/kJsClassWithJsCall.kt");
}
@TestMetadata("kTypeWithJsCall.kt")
public void testKTypeWithJsCall() throws Exception {
runTest("js/js.translator/testData/box/reflection/kTypeWithJsCall.kt");
@@ -1,6 +1,5 @@
import kotlin.reflect.KClass
// MODULE: main
// FILE: main.kt
external abstract open class A(
o: String
@@ -1,6 +1,5 @@
import kotlin.reflect.KClass
// MODULE: main
// FILE: main.kt
external abstract open class A(
o: String
@@ -0,0 +1,38 @@
import kotlin.reflect.KClass
// FILE: main.kt
external abstract open class A(
o: String
) {
abstract val k: String
fun test(): String
}
class B(
o: String
) : A(o) {
override val k = "K"
}
external fun test(
klazz: Any
) : B
fun box(): String {
return test(B::class.js).test()
}
// FILE: test.js
function test(classType) {
return new classType("O")
}
function A(o) {
this.o = o
}
A.prototype.test = function() {
return this.o + this.k
}
@@ -4,7 +4,6 @@ import kotlin.reflect.KClass
import kotlin.reflect.KType
import kotlin.reflect.typeOf
// MODULE: main
// FILE: main.kt
external abstract open class A(
o: String