diff --git a/js/js.tests/test/org/jetbrains/kotlin/js/test/semantics/BoxJsTestGenerated.java b/js/js.tests/test/org/jetbrains/kotlin/js/test/semantics/BoxJsTestGenerated.java index d7a4aafdb43..620ae5dde6b 100644 --- a/js/js.tests/test/org/jetbrains/kotlin/js/test/semantics/BoxJsTestGenerated.java +++ b/js/js.tests/test/org/jetbrains/kotlin/js/test/semantics/BoxJsTestGenerated.java @@ -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"); diff --git a/js/js.translator/testData/box/expression/evaluationOrder/throwableDelegation.kt b/js/js.translator/testData/box/expression/evaluationOrder/throwableDelegation.kt index 85338a7ca99..ebb47e3a26d 100644 --- a/js/js.translator/testData/box/expression/evaluationOrder/throwableDelegation.kt +++ b/js/js.translator/testData/box/expression/evaluationOrder/throwableDelegation.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") diff --git a/js/js.translator/testData/box/expression/try/exceptionToString.kt b/js/js.translator/testData/box/expression/try/exceptionToString.kt index ec84d0b1f8f..504daee1e02 100644 --- a/js/js.translator/testData/box/expression/try/exceptionToString.kt +++ b/js/js.translator/testData/box/expression/try/exceptionToString.kt @@ -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" } diff --git a/js/js.translator/testData/box/expression/try/exceptionToString_legacy.kt b/js/js.translator/testData/box/expression/try/exceptionToString_legacy.kt new file mode 100644 index 00000000000..299f334f2e1 --- /dev/null +++ b/js/js.translator/testData/box/expression/try/exceptionToString_legacy.kt @@ -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" +} diff --git a/libraries/stdlib/js-ir/runtime/coreRuntime.kt b/libraries/stdlib/js-ir/runtime/coreRuntime.kt index 3c99ecbaea9..1e091cf010a 100644 --- a/libraries/stdlib/js-ir/runtime/coreRuntime.kt +++ b/libraries/stdlib/js-ir/runtime/coreRuntime.kt @@ -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() @@ -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_) }