[JS IR BE] Simplify throwable lowering.

- Make kotlin.Throwable class extenral
- Add runtime function 'extendThrowable' to use instead of delegating
  constructors to Throwable
This commit is contained in:
Svyatoslav Kuzmich
2019-07-02 17:43:13 +03:00
parent 6d1f893a5a
commit 918d470e1b
12 changed files with 188 additions and 448 deletions
-1
View File
@@ -28,7 +28,6 @@ task prepareNativeBuiltinsSources(type: Sync) {
include "Nothing.kt"
include "Number.kt"
include "String.kt"
include "Throwable.kt"
}
into nativeBuiltinsSrcDir
@@ -0,0 +1,23 @@
/*
* Copyright 2010-2019 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
/**
* The base class for all errors and exceptions. Only instances of this class can be thrown or caught.
*
* @param message the detail message string.
* @param cause the cause of this throwable.
*/
@JsName("Error")
public external open class Throwable {
open val message: String?
open val cause: Throwable?
constructor(message: String?, cause: Throwable?)
constructor(message: String?)
constructor(cause: Throwable?)
constructor()
}
@@ -77,7 +77,6 @@ fun getStringHashCode(str: String): Int {
fun identityHashCode(obj: Any?): Int = getObjectHashCode(obj)
@JsName("captureStack")
internal fun captureStack(instance: Throwable) {
if (js("Error").captureStackTrace != null) {
js("Error").captureStackTrace(instance, instance::class.js)
@@ -86,17 +85,19 @@ internal fun captureStack(instance: Throwable) {
}
}
@JsName("newThrowable")
internal fun newThrowable(message: String?, cause: Throwable?): Throwable {
val throwable = js("new Error()")
throwable.message = if (message == null) {
if (cause != null) (cause.asDynamic().toString)() else null
} else {
message
}
throwable.message = message ?: cause?.toString()
throwable.cause = cause
throwable.name = "Throwable"
return throwable
return throwable.unsafeCast<Throwable>()
}
internal fun extendThrowable(this_: dynamic, message: String?, cause: Throwable?) {
js("Error").call(this_)
this_.message = message ?: cause?.toString()
this_.cause = cause
captureStack(this_)
}
internal fun <T, R> boxIntrinsic(x: T): R = error("Should be lowered")