JS: make kotlin.Throwable = Error. See KT-15312

This commit is contained in:
Alexey Andreev
2016-12-29 13:37:30 +03:00
parent c565697e77
commit f34093db9f
18 changed files with 299 additions and 52 deletions
-47
View File
@@ -1,47 +0,0 @@
/*
* Copyright 2010-2016 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package kotlin
import kotlin.js.internal.JsError
/**
* 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.
*/
public open class Throwable(open val message: String?, open val cause: Throwable?) : JsError(message) {
constructor(message: String?) : this(message, null)
constructor(cause: Throwable?) : this(cause?.toString(), cause)
constructor() : this(null, null)
init {
var e = JsError(message)
copyOwnProperties(e, this)
val t: dynamic = this
t.name = t.constructor.name
}
}
private fun copyOwnProperties(from: dynamic, to: dynamic) {
val names: Array<String> = js("Object").getOwnPropertyNames(from);
for (name in names) {
to[name] = from[name];
}
}
+18
View File
@@ -46,4 +46,22 @@ fun subSequence(c: CharSequence, startIndex: Int, endIndex: Int): CharSequence {
else {
return c.asDynamic().`subSequence_vux9f0$`(startIndex, endIndex)
}
}
@JsName("captureStack")
internal fun captureStack(baseClass: JsClass<in Throwable>, 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 = message
throwable.cause = cause
return throwable
}