[K/N] Make exception handling in initializers more consistent with jvm
^KT-57091
This commit is contained in:
committed by
Space Team
parent
0506d39d8a
commit
dc2e072af2
@@ -44,7 +44,7 @@ void RUNTIME_NORETURN ThrowIllegalArgumentException();
|
||||
void RUNTIME_NORETURN ThrowIllegalStateException();
|
||||
void RUNTIME_NORETURN ThrowInvalidMutabilityException(KConstRef where);
|
||||
void RUNTIME_NORETURN ThrowIncorrectDereferenceException();
|
||||
void RUNTIME_NORETURN ThrowFileFailedToInitializeException();
|
||||
void RUNTIME_NORETURN ThrowFileFailedToInitializeException(KRef reason);
|
||||
void RUNTIME_NORETURN ThrowIllegalObjectSharingException(KConstNativePtr typeInfo, KConstNativePtr address);
|
||||
void RUNTIME_NORETURN ThrowFreezingException(KRef toFreeze, KRef blocker);
|
||||
// Prints out message of Throwable.
|
||||
|
||||
@@ -464,14 +464,14 @@ static void CallInitGlobalAwaitInitialized(int *state) {
|
||||
localState = atomicGetAcquire(state);
|
||||
} while (localState != FILE_INITIALIZED && localState != FILE_FAILED_TO_INITIALIZE);
|
||||
}
|
||||
if (localState == FILE_FAILED_TO_INITIALIZE) ThrowFileFailedToInitializeException();
|
||||
if (localState == FILE_FAILED_TO_INITIALIZE) ThrowFileFailedToInitializeException(nullptr);
|
||||
}
|
||||
|
||||
NO_INLINE void CallInitGlobalPossiblyLock(int* state, void (*init)()) {
|
||||
int localState = atomicGetAcquire(state);
|
||||
if (localState == FILE_INITIALIZED) return;
|
||||
if (localState == FILE_FAILED_TO_INITIALIZE)
|
||||
ThrowFileFailedToInitializeException();
|
||||
ThrowFileFailedToInitializeException(nullptr);
|
||||
int threadId = konan::currentThreadId();
|
||||
if ((localState & 3) == FILE_BEING_INITIALIZED) {
|
||||
if ((localState & ~3) != (threadId << 2)) {
|
||||
@@ -485,10 +485,13 @@ NO_INLINE void CallInitGlobalPossiblyLock(int* state, void (*init)()) {
|
||||
init();
|
||||
#else
|
||||
try {
|
||||
CurrentFrameGuard guard;
|
||||
init();
|
||||
} catch (...) {
|
||||
} catch (ExceptionObjHolder& e) {
|
||||
ObjHolder holder;
|
||||
auto *exception = Kotlin_getExceptionObject(&e, holder.slot());
|
||||
atomicSetRelease(state, FILE_FAILED_TO_INITIALIZE);
|
||||
throw;
|
||||
ThrowFileFailedToInitializeException(exception);
|
||||
}
|
||||
#endif
|
||||
atomicSetRelease(state, FILE_INITIALIZED);
|
||||
@@ -499,16 +502,19 @@ NO_INLINE void CallInitGlobalPossiblyLock(int* state, void (*init)()) {
|
||||
|
||||
void CallInitThreadLocal(int volatile* globalState, int* localState, void (*init)()) {
|
||||
if (*localState == FILE_FAILED_TO_INITIALIZE || (globalState != nullptr && *globalState == FILE_FAILED_TO_INITIALIZE))
|
||||
ThrowFileFailedToInitializeException();
|
||||
ThrowFileFailedToInitializeException(nullptr);
|
||||
*localState = FILE_INITIALIZED;
|
||||
#if KONAN_NO_EXCEPTIONS
|
||||
init();
|
||||
#else
|
||||
try {
|
||||
CurrentFrameGuard guard;
|
||||
init();
|
||||
} catch(...) {
|
||||
} catch(ExceptionObjHolder& e) {
|
||||
ObjHolder holder;
|
||||
auto *exception = Kotlin_getExceptionObject(&e, holder.slot());
|
||||
*localState = FILE_FAILED_TO_INITIALIZE;
|
||||
throw;
|
||||
ThrowFileFailedToInitializeException(exception);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
@@ -35,15 +35,6 @@ public class IncorrectDereferenceException : RuntimeException {
|
||||
constructor(message: String) : super(message)
|
||||
}
|
||||
|
||||
/**
|
||||
* Exception thrown when there was an error during file initalization.
|
||||
*/
|
||||
@ExperimentalStdlibApi
|
||||
public class FileFailedToInitializeException : RuntimeException {
|
||||
constructor() : super()
|
||||
|
||||
constructor(message: String) : super(message)
|
||||
}
|
||||
|
||||
/**
|
||||
* Typealias describing custom exception reporting hook.
|
||||
|
||||
@@ -107,10 +107,20 @@ internal fun ThrowIncorrectDereferenceException() {
|
||||
"Trying to access top level value not marked as @ThreadLocal or @SharedImmutable from non-main thread")
|
||||
}
|
||||
|
||||
internal class FileFailedToInitializeException(message: String?, cause: Throwable?) : Error(message, cause)
|
||||
|
||||
@ExportForCppRuntime
|
||||
@OptIn(ExperimentalStdlibApi::class)
|
||||
internal fun ThrowFileFailedToInitializeException() {
|
||||
throw FileFailedToInitializeException("There was an error during file initialization")
|
||||
internal fun ThrowFileFailedToInitializeException(reason: Throwable?) {
|
||||
if (reason is Error) {
|
||||
throw reason
|
||||
} else {
|
||||
// https://youtrack.jetbrains.com/issue/KT-57134
|
||||
// TODO: align exact exception hierarchy with jvm
|
||||
// in jvm it's NoClassDefFound if reason is null, i.e. this is already failed class
|
||||
// and ExceptionInInitializerError if it's non-null
|
||||
throw FileFailedToInitializeException("There was an error during file or class initialization", reason)
|
||||
}
|
||||
}
|
||||
|
||||
internal class IrLinkageError(message: String?) : Error(message)
|
||||
|
||||
Reference in New Issue
Block a user