From c32b84efd12c0ac0ffa1cd855ee41760ca3f0a34 Mon Sep 17 00:00:00 2001 From: Ilya Matveev Date: Mon, 4 Sep 2017 18:31:46 +0300 Subject: [PATCH] stdlib: Move exit function to porting layer --- backend.native/tests/runtime/basic/exit.kt | 2 ++ .../groovy/org/jetbrains/kotlin/KonanTest.groovy | 5 +++++ runtime/src/launcher/js/launcher.js | 7 +++++++ runtime/src/main/cpp/Natives.cpp | 2 +- runtime/src/main/cpp/Porting.cpp | 16 +++++++++++++++- runtime/src/main/cpp/Porting.h | 1 + 6 files changed, 31 insertions(+), 2 deletions(-) diff --git a/backend.native/tests/runtime/basic/exit.kt b/backend.native/tests/runtime/basic/exit.kt index 42d062ea051..74359a40cf7 100644 --- a/backend.native/tests/runtime/basic/exit.kt +++ b/backend.native/tests/runtime/basic/exit.kt @@ -2,4 +2,6 @@ import kotlin.system.* fun main(args: Array) { exitProcess(42) + @Suppress("UNREACHABLE_CODE") + throw RuntimeException("Exit function call returned normally") } \ No newline at end of file diff --git a/buildSrc/plugins/src/main/groovy/org/jetbrains/kotlin/KonanTest.groovy b/buildSrc/plugins/src/main/groovy/org/jetbrains/kotlin/KonanTest.groovy index c0a44db40e6..cda5728d065 100644 --- a/buildSrc/plugins/src/main/groovy/org/jetbrains/kotlin/KonanTest.groovy +++ b/buildSrc/plugins/src/main/groovy/org/jetbrains/kotlin/KonanTest.groovy @@ -214,6 +214,11 @@ fun handleExceptionContinuation(x: (Throwable) -> Unit): Continuation = ob @TaskAction void executeTest() { + if (!enabled) { + println "Test is disabled: $name" + return + } + createOutputDirectory() def program = buildExePath() def suffix = targetManager.programSuffix diff --git a/runtime/src/launcher/js/launcher.js b/runtime/src/launcher/js/launcher.js index f32c2673014..fa87141f1a5 100644 --- a/runtime/src/launcher/js/launcher.js +++ b/runtime/src/launcher/js/launcher.js @@ -42,6 +42,9 @@ if (isBrowser()) { }, flush: function () { this.print(this.stdout); + }, + exit: function(status) { + throw Error("Kotlin process called exit (" + status + ")"); } }; } else { @@ -49,6 +52,7 @@ if (isBrowser()) { write: write, print: print, flush: function() {}, + exit: quit }; } @@ -132,6 +136,9 @@ var konan_dependencies = { Konan_abort: function(pointer) { throw new Error("Konan_abort(" + utf8decode(toString(pointer)) + ")"); }, + Konan_exit: function(status) { + runtime.exit(status); + }, Konan_js_arg_size: function(index) { if (index >= global_arguments.length) return -1; return global_arguments[index].length + 1; // + 1 for trailing zero. diff --git a/runtime/src/main/cpp/Natives.cpp b/runtime/src/main/cpp/Natives.cpp index cc34af90846..4cb73818fcf 100644 --- a/runtime/src/main/cpp/Natives.cpp +++ b/runtime/src/main/cpp/Natives.cpp @@ -67,7 +67,7 @@ void Kotlin_interop_free(void* ptr) { } void Kotlin_system_exitProcess(KInt status) { - exit(status); + konan::exit(status); } } // extern "C" diff --git a/runtime/src/main/cpp/Porting.cpp b/runtime/src/main/cpp/Porting.cpp index 0920e0c22d0..4158125e6aa 100644 --- a/runtime/src/main/cpp/Porting.cpp +++ b/runtime/src/main/cpp/Porting.cpp @@ -31,6 +31,11 @@ #include "Porting.h" +#ifdef KONAN_WASM +extern "C" void Konan_abort(const char*); +extern "C" void Konan_exit(int32_t status); +#endif + namespace konan { // Console operations. @@ -90,6 +95,16 @@ void abort() { ::abort(); } +#ifdef KONAN_WASM +void exit(int32_t status) { + Konan_exit(status); +} +#else +void exit(int32_t status) { + ::exit(status); +} +#endif + // String/byte operations. // memcpy/memmove are not here intentionally, as frequently implemented/optimized // by C compiler. @@ -235,7 +250,6 @@ long getpagesize() { extern "C" { #ifdef KONAN_WASM - extern void Konan_abort(const char*); // TODO: get rid of these. void _ZNKSt3__220__vector_base_commonILb1EE20__throw_length_errorEv(void) { diff --git a/runtime/src/main/cpp/Porting.h b/runtime/src/main/cpp/Porting.h index 568e6af69a9..b683bcab6d4 100644 --- a/runtime/src/main/cpp/Porting.h +++ b/runtime/src/main/cpp/Porting.h @@ -31,6 +31,7 @@ uint32_t consoleReadUtf8(void* utf8, uint32_t maxSizeBytes); // Process control. void abort(); +void exit(int32_t status); // String/byte operations. // memcpy/memmove/memcmp are not here intentionally, as frequently implemented/optimized