From 6a666637396f035559d193cb73aed9d63901ab2e Mon Sep 17 00:00:00 2001 From: romanart Date: Fri, 12 Oct 2018 15:43:33 +0300 Subject: [PATCH] Update IR Runtime --- .../kotlin/js/test/BasicIrBoxTest.kt | 6 - libraries/stdlib/js/irRuntime/core.kt | 25 ++++- libraries/stdlib/js/irRuntime/exceptions.kt | 105 ------------------ libraries/stdlib/js/irRuntime/hacks.kt | 15 +++ 4 files changed, 39 insertions(+), 112 deletions(-) delete mode 100644 libraries/stdlib/js/irRuntime/exceptions.kt diff --git a/js/js.tests/test/org/jetbrains/kotlin/js/test/BasicIrBoxTest.kt b/js/js.tests/test/org/jetbrains/kotlin/js/test/BasicIrBoxTest.kt index b101101da62..a7ec89ea920 100644 --- a/js/js.tests/test/org/jetbrains/kotlin/js/test/BasicIrBoxTest.kt +++ b/js/js.tests/test/org/jetbrains/kotlin/js/test/BasicIrBoxTest.kt @@ -43,9 +43,6 @@ private val runtimeSourcesCommon = listOfKtFilesFrom( // TODO: Support Int.pow "libraries/stdlib/js/src/kotlin/random/PlatformRandom.kt", - // TODO: Unify exceptions - "libraries/stdlib/common/src/kotlin/ExceptionsH.kt", - // Fails with: EXPERIMENTAL_IS_NOT_ENABLED "libraries/stdlib/common/src/kotlin/annotations/Annotations.kt", @@ -62,9 +59,6 @@ private val runtimeSourcesCommon = listOfKtFilesFrom( "libraries/stdlib/js/src/kotlin/dom", "libraries/stdlib/js/src/kotlin/browser", - // TODO: Unify exceptions - "libraries/stdlib/js/src/kotlin/exceptions.kt", - // TODO: fix compilation issues in arrayPlusCollection // Replaced with irRuntime/kotlinHacks.kt "libraries/stdlib/js/src/kotlin/kotlin.kt", diff --git a/libraries/stdlib/js/irRuntime/core.kt b/libraries/stdlib/js/irRuntime/core.kt index dca15ae1154..78e15a08609 100644 --- a/libraries/stdlib/js/irRuntime/core.kt +++ b/libraries/stdlib/js/irRuntime/core.kt @@ -85,4 +85,27 @@ fun getNumberHashCode(obj: dynamic) = js(""" } """).unsafeCast() -fun identityHashCode(obj: dynamic): Int = getObjectHashCode(obj) \ No newline at end of file +fun identityHashCode(obj: dynamic): Int = getObjectHashCode(obj) + + +@JsName("captureStack") +internal fun captureStack(instance: Throwable) { + if (js("Error").captureStackTrace) { + js("Error").captureStackTrace(instance, instance::class.js); + } else { + instance.asDynamic().stack = js("new Error()").stack; + } +} + +@JsName("newThrowable") +internal fun newThrowable(message: String?, cause: Throwable?): Throwable { + val throwable = js("new Error()") + throwable.message = if (message == null) { + if (cause != null) js("cause.toString()") else null + } else { + message + } + throwable.cause = cause + throwable.name = "Throwable" + return throwable +} \ No newline at end of file diff --git a/libraries/stdlib/js/irRuntime/exceptions.kt b/libraries/stdlib/js/irRuntime/exceptions.kt deleted file mode 100644 index 4a8c9c7941c..00000000000 --- a/libraries/stdlib/js/irRuntime/exceptions.kt +++ /dev/null @@ -1,105 +0,0 @@ -/* - * 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 kotlin - -open class Error(override val message: String?, override val cause: Throwable?) : Throwable() { - constructor() : this(null, null) - constructor(_message: String?) : this(_message, null) - constructor(_cause: Throwable?) : this(null, _cause) -} - -open class Exception(override val message: String?, override val cause: Throwable?) : Throwable() { - constructor() : this(null, null) - constructor(_message: String?) : this(_message, null) - constructor(_cause: Throwable?) : this(null, _cause) -} - -open class RuntimeException(message: String?, cause: Throwable?) : Exception(message, cause) { - constructor() : this(null, null) - constructor(message: String?) : this(message, null) - constructor(cause: Throwable?) : this(null, cause) -} - -open class IllegalArgumentException(message: String?, cause: Throwable?) : RuntimeException(message, cause) { - constructor() : this(null, null) - constructor(message: String?) : this(message, null) - constructor(cause: Throwable?) : this(null, cause) -} - -open class IllegalStateException(message: String?, cause: Throwable?) : RuntimeException(message, cause) { - constructor() : this(null, null) - constructor(message: String?) : this(message, null) - constructor(cause: Throwable?) : this(null, cause) -} - -open class ClassCastException(message: String?, cause: Throwable?) : RuntimeException(message, cause) { - constructor() : this(null, null) - constructor(message: String?) : this(message, null) -} - -open class NullPointerException(message: String?, cause: Throwable?) : RuntimeException(message, cause) { - constructor() : this(null, null) - constructor(message: String?) : this(message, null) -} - -open class IndexOutOfBoundsException(message: String?, cause: Throwable?) : RuntimeException(message, cause) { - constructor() : this(null, null) - constructor(message: String?) : this(message, null) -} - -open class AssertionError(message: String?, cause: Throwable?) : Exception(message, cause) { - constructor() : this(null, null) - constructor(message: Any?) : this(message?.toString(), message as? Throwable) - -} - -open class UnsupportedOperationException(message: String?, cause: Throwable?) : RuntimeException(message, cause) { - constructor() : this(null, null) - constructor(message: String?) : this(message, null) - constructor(cause: Throwable?) : this(null, cause) -} - -open class NoSuchElementException(message: String?, cause: Throwable?) : RuntimeException(message, cause) { - constructor() : this(null, null) - constructor(message: String?) : this(message, null) -} - -open class NumberFormatException(message: String?) : IllegalArgumentException(message) { - constructor() : this(null) -} - -public open class NoWhenBranchMatchedException(message: String?, cause: Throwable?) : RuntimeException(message, cause) { - constructor() : this(null, null) - constructor(message: String?) : this(message, null) - constructor(cause: Throwable?) : this(undefined, cause) -} - -open class UninitializedPropertyAccessException(message: String?, cause: Throwable?) : RuntimeException(message, cause) { - constructor() : this(null, null) - constructor(message: String?) : this(message, null) - constructor(cause: Throwable?) : this(undefined, cause) -} - -@SinceKotlin("1.3") -open class ArithmeticException constructor(message: String?) : RuntimeException(message) { - constructor() : this(null) -} - -// TODO: fix function names to satisfy style convention (depends on built-in names) -fun THROW_ISE() { - throw IllegalStateException() -} -fun THROW_CCE() { - throw ClassCastException() -} -fun THROW_NPE() { - throw NullPointerException() -} - -fun error(s: String): Nothing = throw IllegalStateException(s, null) - -@PublishedApi -internal fun throwUninitializedPropertyAccessException(name: String): Nothing = - throw UninitializedPropertyAccessException("lateinit property $name has not been initialized") \ No newline at end of file diff --git a/libraries/stdlib/js/irRuntime/hacks.kt b/libraries/stdlib/js/irRuntime/hacks.kt index a3865d504d2..83ca0ab0c34 100644 --- a/libraries/stdlib/js/irRuntime/hacks.kt +++ b/libraries/stdlib/js/irRuntime/hacks.kt @@ -42,3 +42,18 @@ public inline fun floatArrayOf(vararg a: Float) = a public inline fun doubleArrayOf(vararg a: Double) = a public inline fun longArrayOf(vararg a: Long) = a + + +@PublishedApi +internal fun throwUninitializedPropertyAccessException(name: String): Nothing = + throw UninitializedPropertyAccessException("lateinit property $name has not been initialized") + +fun THROW_ISE() { + throw IllegalStateException() +} +fun THROW_CCE() { + throw ClassCastException() +} +fun THROW_NPE() { + throw NullPointerException() +} \ No newline at end of file