JS_IR: Support 'CHECK_NOT_NULL' intrinsic
This commit is contained in:
@@ -6,7 +6,6 @@
|
|||||||
package org.jetbrains.kotlin.ir.backend.js
|
package org.jetbrains.kotlin.ir.backend.js
|
||||||
|
|
||||||
import org.jetbrains.kotlin.builtins.PrimitiveType
|
import org.jetbrains.kotlin.builtins.PrimitiveType
|
||||||
import org.jetbrains.kotlin.ir.backend.js.ir.JsIrBuilder
|
|
||||||
import org.jetbrains.kotlin.ir.backend.js.utils.Namer
|
import org.jetbrains.kotlin.ir.backend.js.utils.Namer
|
||||||
import org.jetbrains.kotlin.ir.builders.declarations.addFunction
|
import org.jetbrains.kotlin.ir.builders.declarations.addFunction
|
||||||
import org.jetbrains.kotlin.ir.builders.declarations.addTypeParameter
|
import org.jetbrains.kotlin.ir.builders.declarations.addTypeParameter
|
||||||
@@ -21,6 +20,7 @@ import org.jetbrains.kotlin.ir.types.defaultType
|
|||||||
import org.jetbrains.kotlin.ir.types.isLong
|
import org.jetbrains.kotlin.ir.types.isLong
|
||||||
import org.jetbrains.kotlin.ir.util.constructors
|
import org.jetbrains.kotlin.ir.util.constructors
|
||||||
import org.jetbrains.kotlin.ir.util.findDeclaration
|
import org.jetbrains.kotlin.ir.util.findDeclaration
|
||||||
|
import org.jetbrains.kotlin.ir.util.kotlinPackageFqn
|
||||||
import org.jetbrains.kotlin.name.FqName
|
import org.jetbrains.kotlin.name.FqName
|
||||||
import org.jetbrains.kotlin.name.Name
|
import org.jetbrains.kotlin.name.Name
|
||||||
import org.jetbrains.kotlin.psi2ir.findSingleFunction
|
import org.jetbrains.kotlin.psi2ir.findSingleFunction
|
||||||
@@ -201,6 +201,8 @@ class JsIntrinsics(private val irBuiltIns: IrBuiltIns, val context: JsIrBackendC
|
|||||||
val returnIfSuspended = getInternalFunction("returnIfSuspended")
|
val returnIfSuspended = getInternalFunction("returnIfSuspended")
|
||||||
val getContinuation = getInternalFunction("getContinuation")
|
val getContinuation = getInternalFunction("getContinuation")
|
||||||
|
|
||||||
|
val jsEnsureNonNull = getFunctionInKotlinPackage("ensureNotNull")
|
||||||
|
|
||||||
// Arrays:
|
// Arrays:
|
||||||
val array = context.symbolTable.referenceClass(irBuiltIns.builtIns.array)
|
val array = context.symbolTable.referenceClass(irBuiltIns.builtIns.array)
|
||||||
|
|
||||||
@@ -282,6 +284,9 @@ class JsIntrinsics(private val irBuiltIns: IrBuiltIns, val context: JsIrBackendC
|
|||||||
private fun getInternalWithoutPackage(name: String) =
|
private fun getInternalWithoutPackage(name: String) =
|
||||||
context.symbolTable.referenceSimpleFunction(context.getFunctions(FqName(name)).single())
|
context.symbolTable.referenceSimpleFunction(context.getFunctions(FqName(name)).single())
|
||||||
|
|
||||||
|
private fun getFunctionInKotlinPackage(name: String) =
|
||||||
|
context.symbolTable.referenceSimpleFunction(context.getFunctions(kotlinPackageFqn.child(Name.identifier(name))).single())
|
||||||
|
|
||||||
private fun getInternalClassWithoutPackage(fqName: String) =
|
private fun getInternalClassWithoutPackage(fqName: String) =
|
||||||
context.symbolTable.referenceClass(context.getClass(FqName(fqName)))
|
context.symbolTable.referenceClass(context.getClass(FqName(fqName)))
|
||||||
|
|
||||||
|
|||||||
+7
-6
@@ -14,17 +14,18 @@ import org.jetbrains.kotlin.name.Name
|
|||||||
|
|
||||||
class ExceptionHelperCallsTransformer(private val context: JsIrBackendContext) : CallsTransformer {
|
class ExceptionHelperCallsTransformer(private val context: JsIrBackendContext) : CallsTransformer {
|
||||||
|
|
||||||
private fun referenceFunction(fqn: FqName) = context.getFunctions(fqn).singleOrNull()?.let {
|
private fun referenceFunction(fqn: FqName) =
|
||||||
context.symbolTable.referenceSimpleFunction(it)
|
context.getFunctions(fqn).singleOrNull()?.let {
|
||||||
}
|
context.symbolTable.referenceSimpleFunction(it)
|
||||||
|
} ?: throw AssertionError("Function not found: $fqn")
|
||||||
|
|
||||||
private val helperMapping = mapOf(
|
private val helperMapping = mapOf(
|
||||||
// context.irBuiltIns.throwNpeSymbol to referenceFunction(kotlinPackageFqn.child(Name.identifier("THROW_NPE"))), -- TODO checkNotNullSymbol
|
context.irBuiltIns.checkNotNullSymbol to referenceFunction(kotlinPackageFqn.child(Name.identifier("ensureNotNull"))),
|
||||||
context.irBuiltIns.throwCceSymbol to referenceFunction(kotlinPackageFqn.child(Name.identifier("THROW_CCE"))),
|
context.irBuiltIns.throwCceSymbol to referenceFunction(kotlinPackageFqn.child(Name.identifier("THROW_CCE"))),
|
||||||
context.irBuiltIns.throwIseSymbol to referenceFunction(kotlinPackageFqn.child(Name.identifier("THROW_ISE"))),
|
context.irBuiltIns.throwIseSymbol to referenceFunction(kotlinPackageFqn.child(Name.identifier("THROW_ISE"))),
|
||||||
context.irBuiltIns.noWhenBranchMatchedExceptionSymbol to referenceFunction(kotlinPackageFqn.child(Name.identifier("noWhenBranchMatchedException")))
|
context.irBuiltIns.noWhenBranchMatchedExceptionSymbol to referenceFunction(kotlinPackageFqn.child(Name.identifier("noWhenBranchMatchedException")))
|
||||||
)
|
)
|
||||||
|
|
||||||
|
override fun transformFunctionAccess(call: IrFunctionAccessExpression) =
|
||||||
override fun transformFunctionAccess(call: IrFunctionAccessExpression) = helperMapping[call.symbol]?.let { irCall(call, it) } ?: call
|
helperMapping[call.symbol]?.let { irCall(call, it) } ?: call
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -21,3 +21,5 @@ fun THROW_CCE(): Nothing {
|
|||||||
fun THROW_NPE(): Nothing {
|
fun THROW_NPE(): Nothing {
|
||||||
throw NullPointerException()
|
throw NullPointerException()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun <T:Any> ensureNotNull(v: T?): T = if (v == null) THROW_NPE() else v
|
||||||
Reference in New Issue
Block a user