diff --git a/compiler/ir/backend.js/src/org/jetbrains/kotlin/ir/backend/js/lower/calls/CallsLowering.kt b/compiler/ir/backend.js/src/org/jetbrains/kotlin/ir/backend/js/lower/calls/CallsLowering.kt index 04b99b12f72..15c79599051 100644 --- a/compiler/ir/backend.js/src/org/jetbrains/kotlin/ir/backend/js/lower/calls/CallsLowering.kt +++ b/compiler/ir/backend.js/src/org/jetbrains/kotlin/ir/backend/js/lower/calls/CallsLowering.kt @@ -12,7 +12,6 @@ import org.jetbrains.kotlin.ir.declarations.IrFile import org.jetbrains.kotlin.ir.declarations.IrFunction import org.jetbrains.kotlin.ir.expressions.IrCall import org.jetbrains.kotlin.ir.expressions.IrExpression -import org.jetbrains.kotlin.ir.types.isLong import org.jetbrains.kotlin.ir.util.hasAnnotation import org.jetbrains.kotlin.ir.visitors.IrElementTransformerVoid import org.jetbrains.kotlin.ir.visitors.transformChildrenVoid @@ -26,7 +25,8 @@ class CallsLowering(val context: JsIrBackendContext) : FileLoweringPass { PrimitiveContainerMemberCallTransformer(context), MethodsOfAnyCallsTransformer(context), ReflectionCallsTransformer(context), - EnumIntrinsicsTransformer(context) + EnumIntrinsicsTransformer(context), + ExceptionHelperCallsTransformer(context) ) override fun lower(irFile: IrFile) { diff --git a/compiler/ir/backend.js/src/org/jetbrains/kotlin/ir/backend/js/lower/calls/ExceptionHelperCallsTransformer.kt b/compiler/ir/backend.js/src/org/jetbrains/kotlin/ir/backend/js/lower/calls/ExceptionHelperCallsTransformer.kt new file mode 100644 index 00000000000..7038d28547f --- /dev/null +++ b/compiler/ir/backend.js/src/org/jetbrains/kotlin/ir/backend/js/lower/calls/ExceptionHelperCallsTransformer.kt @@ -0,0 +1,30 @@ +/* + * Copyright 2010-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license + * that can be found in the license/LICENSE.txt file. + */ + +package org.jetbrains.kotlin.ir.backend.js.lower.calls + +import org.jetbrains.kotlin.ir.backend.js.JsIrBackendContext +import org.jetbrains.kotlin.ir.backend.js.ir.irCall +import org.jetbrains.kotlin.ir.expressions.IrCall +import org.jetbrains.kotlin.ir.util.kotlinPackageFqn +import org.jetbrains.kotlin.name.FqName +import org.jetbrains.kotlin.name.Name + +class ExceptionHelperCallsTransformer(private val context: JsIrBackendContext) : CallsTransformer { + + private fun referenceFunction(fqn: FqName) = context.getFunctions(fqn).singleOrNull()?.let { + context.symbolTable.referenceSimpleFunction(it) + } + + private val helperMapping = mapOf( + context.irBuiltIns.throwNpeSymbol to referenceFunction(kotlinPackageFqn.child(Name.identifier("THROW_NPE"))), + context.irBuiltIns.throwCceSymbol to referenceFunction(kotlinPackageFqn.child(Name.identifier("THROW_CCE"))), + context.irBuiltIns.throwIseSymbol to referenceFunction(kotlinPackageFqn.child(Name.identifier("THROW_ISE"))), + context.irBuiltIns.noWhenBranchMatchedExceptionSymbol to referenceFunction(kotlinPackageFqn.child(Name.identifier("noWhenBranchMatchedException"))) + ) + + + override fun transformCall(call: IrCall) = helperMapping[call.symbol]?.let { irCall(call, it) } ?: call +} diff --git a/libraries/stdlib/js/irRuntime/hacks.kt b/libraries/stdlib/js/irRuntime/hacks.kt index 83ca0ab0c34..0d11c077e90 100644 --- a/libraries/stdlib/js/irRuntime/hacks.kt +++ b/libraries/stdlib/js/irRuntime/hacks.kt @@ -48,6 +48,9 @@ public inline fun longArrayOf(vararg a: Long) = a internal fun throwUninitializedPropertyAccessException(name: String): Nothing = throw UninitializedPropertyAccessException("lateinit property $name has not been initialized") +internal fun noWhenBranchMatchedException(): Nothing = throw NoWhenBranchMatchedException() + + fun THROW_ISE() { throw IllegalStateException() }