[JS IR BE] Don't render null messages in Throwable

Set message property to 'undefined' to make Error.prototype.toString
skip it
This commit is contained in:
Svyatoslav Kuzmich
2019-07-03 17:21:37 +03:00
parent 94aebd0101
commit a2625c7bc8
5 changed files with 78 additions and 13 deletions
@@ -2692,6 +2692,11 @@ public class BoxJsTestGenerated extends AbstractBoxJsTest {
runTest("js/js.translator/testData/box/expression/try/exceptionToString.kt");
}
@TestMetadata("exceptionToString_legacy.kt")
public void testExceptionToString_legacy() throws Exception {
runTest("js/js.translator/testData/box/expression/try/exceptionToString_legacy.kt");
}
@TestMetadata("kt22053.kt")
public void testKt22053() throws Exception {
runTest("js/js.translator/testData/box/expression/try/kt22053.kt");
@@ -23,10 +23,10 @@ class MyException2(i1: String, i2: String, m: String? = null, t: Throwable? = nu
fun box(): String {
check(MyException1("1", "2"), "MyException1: null")
check(MyException1("1", "2"), "MyException1")
check(MyException1("3", "4", "aaa"), "MyException1: aaa")
check(MyException1("5", "6", t = Throwable("bbb")), "MyException1: Throwable: bbb")
check(MyException2("7", "8"), "MyException2: null")
check(MyException2("7", "8"), "MyException2")
check(MyException2("9", "0", "ccc"), "MyException2: ccc")
check(MyException2("A", "B", t = Throwable("ddd")), "MyException2: Throwable: ddd")
@@ -1,3 +1,4 @@
// IGNORE_BACKEND: JS
// EXPECTED_REACHABLE_NODES: 1298
package foo
@@ -8,8 +9,10 @@ class MyException3: Throwable {
constructor(m: String? = null) : super(m) {}
}
// TODO: add direct inheritors of Throwable:
// - with cause only, in the primary constructor
class MyException4(c: Throwable? = null): Throwable(c)
class MyException5: Throwable {
constructor(c: Throwable? = null) : super(c) {}
}
fun check(e: Throwable, expectedString: String) {
try {
@@ -21,22 +24,28 @@ fun check(e: Throwable, expectedString: String) {
}
fun box(): String {
check(Throwable(), "Throwable: null")
check(Throwable(), "Throwable")
check(Throwable("ccc"), "Throwable: ccc")
check(Throwable(Throwable("ddd")), "Throwable: Throwable: ddd")
check(Exception(), "Exception: null")
check(Exception(), "Exception")
check(Exception("bbb"), "Exception: bbb")
check(Exception(Exception("ccc")), "Exception: Exception: ccc")
check(AssertionError(), "AssertionError: null")
check(AssertionError(null), "AssertionError: null")
check(AssertionError(), "AssertionError")
check(AssertionError(null), "AssertionError")
check(AssertionError("bbb"), "AssertionError: bbb")
check(AssertionError(Exception("ccc")), "AssertionError: Exception: ccc")
check(MyException(), "MyException: null")
check(MyException(), "MyException")
check(MyException("aaa"), "MyException: aaa")
check(MyException2(), "MyException2: null")
check(MyException2(), "MyException2")
check(MyException2("aaa"), "MyException2: aaa")
check(MyException3(), "MyException3: null")
check(MyException3(), "MyException3")
check(MyException3("aaa"), "MyException3: aaa")
check(MyException4(), "MyException4")
check(MyException4(AssertionError()), "MyException4: AssertionError")
check(MyException5(), "MyException5")
check(MyException5(MyException5()), "MyException5: MyException5")
return "OK"
}
@@ -0,0 +1,51 @@
// DONT_TARGET_EXACT_BACKEND: JS_IR
// EXPECTED_REACHABLE_NODES: 1298
package foo
class MyException(m: String? = null): Exception(m)
class MyException2(m: String? = null): Throwable(m)
class MyException3: Throwable {
constructor(m: String? = null) : super(m) {}
}
class MyException4(c: Throwable? = null): Throwable(c)
class MyException5: Throwable {
constructor(c: Throwable? = null) : super(c) {}
}
fun check(e: Throwable, expectedString: String) {
try {
throw e
}
catch (e: Throwable) {
assertEquals(expectedString, e.toString())
}
}
fun box(): String {
check(Throwable(), "Throwable: null")
check(Throwable("ccc"), "Throwable: ccc")
check(Throwable(Throwable("ddd")), "Throwable: Throwable: ddd")
check(Exception(), "Exception: null")
check(Exception("bbb"), "Exception: bbb")
check(Exception(Exception("ccc")), "Exception: Exception: ccc")
check(AssertionError(), "AssertionError: null")
check(AssertionError(null), "AssertionError: null")
check(AssertionError("bbb"), "AssertionError: bbb")
check(AssertionError(Exception("ccc")), "AssertionError: Exception: ccc")
check(MyException(), "MyException: null")
check(MyException("aaa"), "MyException: aaa")
check(MyException2(), "MyException2: null")
check(MyException2("aaa"), "MyException2: aaa")
check(MyException3(), "MyException3: null")
check(MyException3("aaa"), "MyException3: aaa")
check(MyException4(), "MyException4")
check(MyException4(AssertionError()), "MyException4: AssertionError: null")
check(MyException5(), "MyException5")
check(MyException5(MyException5()), "MyException5: MyException5")
return "OK"
}
@@ -87,7 +87,7 @@ internal fun captureStack(instance: Throwable) {
internal fun newThrowable(message: String?, cause: Throwable?): Throwable {
val throwable = js("new Error()")
throwable.message = message ?: cause?.toString()
throwable.message = message ?: cause?.toString() ?: undefined
throwable.cause = cause
throwable.name = "Throwable"
return throwable.unsafeCast<Throwable>()
@@ -95,7 +95,7 @@ internal fun newThrowable(message: String?, cause: Throwable?): Throwable {
internal fun extendThrowable(this_: dynamic, message: String?, cause: Throwable?) {
js("Error").call(this_)
this_.message = message ?: cause?.toString()
this_.message = message ?: cause?.toString() ?: undefined
this_.cause = cause
captureStack(this_)
}