From a6cc7cdc23981070a8058d0e07a1a59804e0bdf0 Mon Sep 17 00:00:00 2001 From: Ivan Kylchik Date: Tue, 21 Jan 2020 13:28:14 +0300 Subject: [PATCH] Rewrite exception handler to catch null pointer exception in js ir For now null check works as !! operator called in jvm. So it throw KotlinNullPointerException, but js ir require NullPointerException --- .../backend/common/interpreter/IrInterpreter.kt | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/compiler/ir/backend.common/src/org/jetbrains/kotlin/backend/common/interpreter/IrInterpreter.kt b/compiler/ir/backend.common/src/org/jetbrains/kotlin/backend/common/interpreter/IrInterpreter.kt index 45bef7d0cfa..4921b2b2026 100644 --- a/compiler/ir/backend.common/src/org/jetbrains/kotlin/backend/common/interpreter/IrInterpreter.kt +++ b/compiler/ir/backend.common/src/org/jetbrains/kotlin/backend/common/interpreter/IrInterpreter.kt @@ -118,8 +118,17 @@ class IrInterpreter(irModule: IrModuleFragment) { } } catch (e: Exception) { // TODO catch Throwable // catch exception from JVM such as: ArithmeticException, StackOverflowError and others - val irExceptionClass = irExceptions.firstOrNull { it.name.asString() == e::class.java.simpleName } - irExceptionClass?.let { data.pushReturnValue(Wrapper(e, it)) } ?: throw AssertionError("Cannot handle exception $e") + val exceptionName = e::class.java.simpleName + val irExceptionClass = irExceptions.firstOrNull { it.name.asString() == exceptionName } + + if (irExceptionClass == null && exceptionName == "KotlinNullPointerException") { + // this block is used to replace jvm null pointer exception with common one + // TODO figure something better + data.pushReturnValue(Wrapper(e, irExceptions.first { it.name.asString() == "NullPointerException" })) + } else { + irExceptionClass?.let { data.pushReturnValue(Wrapper(e, it)) } ?: throw AssertionError("Cannot handle exception $e") + } + return Code.EXCEPTION } }