[JS IR] Generate jsClass function inside the compiler instead of relying on declaration inside stdlib

We want to make its type parameter reified to do it we have to make it inline and non-external.
But we don't want to inline it in call sites and want prevent using it anywhere except places generated by the compiler.
In user code, including stdlib, we should use `K::class.js` instead.
Separately we have to intrinsify `K::class.js` to reduce overhead.
This commit is contained in:
Zalim Bashorov
2020-12-17 01:35:38 +03:00
parent df6635085b
commit d7e3f826bb
2 changed files with 19 additions and 12 deletions
@@ -17,11 +17,9 @@ import org.jetbrains.kotlin.ir.declarations.IrSimpleFunction
import org.jetbrains.kotlin.ir.declarations.impl.IrExternalPackageFragmentImpl
import org.jetbrains.kotlin.ir.descriptors.IrBuiltIns
import org.jetbrains.kotlin.ir.symbols.IrSimpleFunctionSymbol
import org.jetbrains.kotlin.ir.types.IrType
import org.jetbrains.kotlin.ir.types.defaultType
import org.jetbrains.kotlin.ir.types.*
import org.jetbrains.kotlin.ir.types.impl.IrSimpleTypeBuilder
import org.jetbrains.kotlin.ir.types.impl.buildSimpleType
import org.jetbrains.kotlin.ir.types.isLong
import org.jetbrains.kotlin.ir.util.constructors
import org.jetbrains.kotlin.ir.util.findDeclaration
import org.jetbrains.kotlin.ir.util.kotlinPackageFqn
@@ -169,7 +167,9 @@ class JsIntrinsics(private val irBuiltIns: IrBuiltIns, val context: JsIrBackendC
val jsGetContinuation = getInternalFunction("getContinuation")
val jsGetKClass = getInternalWithoutPackage("getKClass")
val jsGetKClassFromExpression = getInternalWithoutPackage("getKClassFromExpression")
val jsClass = getInternalFunction("jsClass")
private val jsClassClassSymbol = getInternalClassWithoutPackage("kotlin.js.JsClass")
val jsClass = defineJsClassIntrinsic().symbol
val jsNumberRangeToNumber = getInternalFunction("numberRangeToNumber")
val jsNumberRangeToLong = getInternalFunction("numberRangeToLong")
@@ -448,6 +448,21 @@ class JsIntrinsics(private val irBuiltIns: IrBuiltIns, val context: JsIrBackendC
}
}
private fun defineJsClassIntrinsic(): IrSimpleFunction {
return irFactory.addFunction(externalPackageFragment) {
name = Name.identifier("jsClass")
origin = JsLoweredDeclarationOrigin.JS_INTRINSICS_STUB
isInline = true
}.apply {
val typeParameter = addTypeParameter {
name = Name.identifier("T")
isReified = true
superTypes += irBuiltIns.anyType
}
returnType = jsClassClassSymbol.typeWithParameters(listOf(typeParameter))
}
}
private fun unOp(name: String, returnType: IrType = irBuiltIns.anyNType) =
irBuiltIns.run { defineOperator(name, returnType, listOf(anyNType)) }
@@ -1,8 +0,0 @@
/*
* Copyright 2010-2020 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
package kotlin.js
internal inline fun <reified T : Any> jsClass(): JsClass<T> = throw NotImplementedError("Implemented as intrinsic")