diff --git a/kotlin-native/runtime/src/main/cpp/Memory.h b/kotlin-native/runtime/src/main/cpp/Memory.h index bdda5f37744..07f2ead314a 100644 --- a/kotlin-native/runtime/src/main/cpp/Memory.h +++ b/kotlin-native/runtime/src/main/cpp/Memory.h @@ -498,6 +498,14 @@ private: bool reentrant_; }; +class CurrentFrameGuard : Pinned { +public: + CurrentFrameGuard() : frame_(getCurrentFrame()) {} + ~CurrentFrameGuard() { SetCurrentFrame(reinterpret_cast(frame_)); } +private: + FrameOverlay* frame_; +}; + template ALWAYS_INLINE inline R CallWithThreadState(R(*function)(Args...), Args... args) { ThreadStateGuard guard(state); diff --git a/kotlin-native/runtime/src/main/cpp/Worker.cpp b/kotlin-native/runtime/src/main/cpp/Worker.cpp index 48885f8208a..6b4d252b41a 100644 --- a/kotlin-native/runtime/src/main/cpp/Worker.cpp +++ b/kotlin-native/runtime/src/main/cpp/Worker.cpp @@ -1106,21 +1106,18 @@ JobKind Worker::processQueueElement(bool blocking) { ObjHolder argumentHolder; ObjHolder resultHolder; KRef argument = AdoptStablePointer(job.regularJob.argument, argumentHolder.slot()); - #if !KONAN_NO_EXCEPTIONS - FrameOverlay* currentFrame = getCurrentFrame(); - #else - #error "Exceptions aren't supported!" - #endif try { #if KONAN_OBJC_INTEROP konan::AutoreleasePool autoreleasePool; #endif - job.regularJob.function(argument, resultHolder.slot()); + { + CurrentFrameGuard guard; + job.regularJob.function(argument, resultHolder.slot()); + } argumentHolder.clear(); // Transfer the result. result = transfer(&resultHolder, job.regularJob.transferMode); } catch (ExceptionObjHolder& e) { - SetCurrentFrame(reinterpret_cast(currentFrame)); ok = false; switch (exceptionHandling()) { case WorkerExceptionHandling::kIgnore: