Native: don't expect that startCoroutineUninterceptedOrReturn receiver is FunctionN+1
Previous implementation of startCoroutineUninterceptedOrReturn expected that the receiver (i.e. the suspend function object to start) of type SuspendFunctionN (i.e. suspend function type with arity = N) is also an instance of FunctionN+1 (i.e. regular function type with arity = N + 1) with proper convention. While compiler tries to achieve that (for better compatibility with Kotlin/JVM, by generating additional supertypes and methods for classes implementing suspend function types), this doesn't work e.g. for implementations defined in Swift. Fix this by adding fallbacks for cases when the receiver is not a FunctionN+1, while keeping the fastpath for default FunctionN+1 cases. ^KT-51043 Fixed
This commit is contained in:
committed by
Space
parent
15fe2c2c5c
commit
728fe918fe
@@ -222,4 +222,25 @@ suspend fun invoke1(block: suspend (Any?) -> Any?, argument: Any?): Any? = block
|
||||
fun getKSuspendCallableReference0(): KSuspendFunction0<String> = ::suspendCallableReference0Target
|
||||
fun getKSuspendCallableReference1(): KSuspendFunction1<String, String> = ::suspendCallableReference1Target
|
||||
|
||||
@Throws(Throwable::class)
|
||||
fun startCoroutineUninterceptedOrReturn(fn: suspend () -> Any?, resultHolder: ResultHolder<Any?>) =
|
||||
fn.startCoroutineUninterceptedOrReturn(ResultHolderCompletion(resultHolder))
|
||||
|
||||
@Throws(Throwable::class)
|
||||
fun startCoroutineUninterceptedOrReturn(fn: suspend Any?.() -> Any?, receiver: Any?, resultHolder: ResultHolder<Any?>) =
|
||||
fn.startCoroutineUninterceptedOrReturn(receiver, ResultHolderCompletion(resultHolder))
|
||||
|
||||
@Throws(Throwable::class)
|
||||
@Suppress("INVISIBLE_MEMBER")
|
||||
fun startCoroutineUninterceptedOrReturn(fn: suspend Any?.(Any?) -> Any?, receiver: Any?, param: Any?, resultHolder: ResultHolder<Any?>) =
|
||||
fn.startCoroutineUninterceptedOrReturn(receiver, param, ResultHolderCompletion(resultHolder))
|
||||
|
||||
@Throws(Throwable::class)
|
||||
fun createCoroutineUninterceptedAndResume(fn: suspend () -> Any?, resultHolder: ResultHolder<Any?>) =
|
||||
fn.createCoroutine(ResultHolderCompletion(resultHolder)).resume(Unit)
|
||||
|
||||
@Throws(Throwable::class)
|
||||
fun createCoroutineUninterceptedAndResume(fn: suspend Any?.() -> Any?, receiver: Any?, resultHolder: ResultHolder<Any?>) =
|
||||
fn.createCoroutine(receiver, ResultHolderCompletion(resultHolder)).resume(Unit)
|
||||
|
||||
fun gc() = kotlin.native.internal.GC.collect()
|
||||
|
||||
@@ -653,12 +653,69 @@ private func testSuspendFunctionSwiftImpl() throws {
|
||||
try assertNil(error)
|
||||
}
|
||||
|
||||
#if NO_GENERICS
|
||||
typealias AnyResultHolder = ResultHolder
|
||||
#else
|
||||
typealias AnyResultHolder = ResultHolder<AnyObject>
|
||||
#endif
|
||||
|
||||
private extension AnyResultHolder {
|
||||
func getSuccessfulResult() throws -> Any? {
|
||||
try assertEquals(actual: completed, expected: 1)
|
||||
try assertNil(exception)
|
||||
return result
|
||||
}
|
||||
}
|
||||
|
||||
// Reported in https://youtrack.jetbrains.com/issue/KT-51043
|
||||
private func testSuspendFunction0SwiftImplStartInKotlin() throws {
|
||||
let resultHolder = AnyResultHolder()
|
||||
try CoroutinesKt.startCoroutineUninterceptedOrReturn(fn: SuspendFunction0SwiftImpl(), resultHolder: resultHolder)
|
||||
try assertEquals(actual: resultHolder.getSuccessfulResult() as? String, expected: "Swift")
|
||||
}
|
||||
|
||||
private func testSuspendFunction1SwiftImplStartInKotlin() throws {
|
||||
let resultHolder = AnyResultHolder()
|
||||
try CoroutinesKt.startCoroutineUninterceptedOrReturn(fn: SuspendFunction1SwiftImpl(), receiver: "receiver", resultHolder: resultHolder)
|
||||
try assertEquals(actual: resultHolder.getSuccessfulResult() as? String, expected: "receiver Swift")
|
||||
}
|
||||
|
||||
private func testSuspendFunction2SwiftImplStartInKotlin() throws {
|
||||
let resultHolder = AnyResultHolder()
|
||||
try CoroutinesKt.startCoroutineUninterceptedOrReturn(fn: SuspendFunction2SwiftImpl(), receiver: "receiver", param: "param", resultHolder: resultHolder)
|
||||
try assertEquals(actual: resultHolder.getSuccessfulResult() as? String, expected: "receiver param Swift")
|
||||
}
|
||||
|
||||
private func testSuspendFunction0SwiftImplCreateInKotlin() throws {
|
||||
let resultHolder = AnyResultHolder()
|
||||
try CoroutinesKt.createCoroutineUninterceptedAndResume(fn: SuspendFunction0SwiftImpl(), resultHolder: resultHolder)
|
||||
try assertEquals(actual: resultHolder.getSuccessfulResult() as? String, expected: "Swift")
|
||||
}
|
||||
|
||||
private func testSuspendFunction1SwiftImplCreateInKotlin() throws {
|
||||
let resultHolder = AnyResultHolder()
|
||||
try CoroutinesKt.createCoroutineUninterceptedAndResume(fn: SuspendFunction1SwiftImpl(), receiver: "receiver", resultHolder: resultHolder)
|
||||
try assertEquals(actual: resultHolder.getSuccessfulResult() as? String, expected: "receiver Swift")
|
||||
}
|
||||
|
||||
private class SuspendFunction0SwiftImpl : KotlinSuspendFunction0 {
|
||||
func invoke(completionHandler: (Any?, Error?) -> Void) {
|
||||
completionHandler("Swift", nil)
|
||||
}
|
||||
}
|
||||
|
||||
private class SuspendFunction1SwiftImpl : KotlinSuspendFunction1 {
|
||||
func invoke(p1: Any?, completionHandler: (Any?, Error?) -> Void) {
|
||||
completionHandler("\(p1 ?? "nil") Swift", nil)
|
||||
}
|
||||
}
|
||||
|
||||
private class SuspendFunction2SwiftImpl : KotlinSuspendFunction2 {
|
||||
func invoke(p1: Any?, p2: Any?, completionHandler: (Any?, Error?) -> Void) {
|
||||
completionHandler("\(p1 ?? "nil") \(p2 ?? "nil") Swift", nil)
|
||||
}
|
||||
}
|
||||
|
||||
class CoroutinesTests : SimpleTestProvider {
|
||||
override init() {
|
||||
super.init()
|
||||
@@ -675,5 +732,10 @@ class CoroutinesTests : SimpleTestProvider {
|
||||
test("TestSuspendFunctionType", testSuspendFunctionType)
|
||||
test("TestKSuspendFunctionType", testSuspendFunctionType)
|
||||
test("TestSuspendFunctionSwiftImpl", testSuspendFunctionSwiftImpl)
|
||||
test("TestSuspendFunction0SwiftImplStartInKotlin", testSuspendFunction0SwiftImplStartInKotlin)
|
||||
test("TestSuspendFunction1SwiftImplStartInKotlin", testSuspendFunction1SwiftImplStartInKotlin)
|
||||
test("TestSuspendFunction2SwiftImplStartInKotlin", testSuspendFunction2SwiftImplStartInKotlin)
|
||||
test("TestSuspendFunction0SwiftImplCreateInKotlin", testSuspendFunction0SwiftImplCreateInKotlin)
|
||||
test("TestSuspendFunction1SwiftImplCreateInKotlin", testSuspendFunction1SwiftImplCreateInKotlin)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -382,6 +382,31 @@ __attribute__((swift_name("CoroutinesKt")))
|
||||
+ (void)invoke1Block:(id<KtKotlinSuspendFunction1>)block argument:(id _Nullable)argument completionHandler:(void (^)(id _Nullable_result, NSError * _Nullable))completionHandler __attribute__((swift_name("invoke1(block:argument:completionHandler:)")));
|
||||
+ (id<KtKotlinKSuspendFunction0>)getKSuspendCallableReference0 __attribute__((swift_name("getKSuspendCallableReference0()")));
|
||||
+ (id<KtKotlinKSuspendFunction1>)getKSuspendCallableReference1 __attribute__((swift_name("getKSuspendCallableReference1()")));
|
||||
|
||||
/**
|
||||
* @note This method converts all Kotlin exceptions to errors.
|
||||
*/
|
||||
+ (id _Nullable)startCoroutineUninterceptedOrReturnFn:(id<KtKotlinSuspendFunction0>)fn resultHolder:(KtResultHolder<id> *)resultHolder error:(NSError * _Nullable * _Nullable)error __attribute__((swift_name("startCoroutineUninterceptedOrReturn(fn:resultHolder:)"))) __attribute__((swift_error(nonnull_error)));
|
||||
|
||||
/**
|
||||
* @note This method converts all Kotlin exceptions to errors.
|
||||
*/
|
||||
+ (id _Nullable)startCoroutineUninterceptedOrReturnFn:(id<KtKotlinSuspendFunction1>)fn receiver:(id _Nullable)receiver resultHolder:(KtResultHolder<id> *)resultHolder error:(NSError * _Nullable * _Nullable)error __attribute__((swift_name("startCoroutineUninterceptedOrReturn(fn:receiver:resultHolder:)"))) __attribute__((swift_error(nonnull_error)));
|
||||
|
||||
/**
|
||||
* @note This method converts all Kotlin exceptions to errors.
|
||||
*/
|
||||
+ (id _Nullable)startCoroutineUninterceptedOrReturnFn:(id<KtKotlinSuspendFunction2>)fn receiver:(id _Nullable)receiver param:(id _Nullable)param resultHolder:(KtResultHolder<id> *)resultHolder error:(NSError * _Nullable * _Nullable)error __attribute__((swift_name("startCoroutineUninterceptedOrReturn(fn:receiver:param:resultHolder:)"))) __attribute__((swift_error(nonnull_error)));
|
||||
|
||||
/**
|
||||
* @note This method converts all Kotlin exceptions to errors.
|
||||
*/
|
||||
+ (BOOL)createCoroutineUninterceptedAndResumeFn:(id<KtKotlinSuspendFunction0>)fn resultHolder:(KtResultHolder<id> *)resultHolder error:(NSError * _Nullable * _Nullable)error __attribute__((swift_name("createCoroutineUninterceptedAndResume(fn:resultHolder:)")));
|
||||
|
||||
/**
|
||||
* @note This method converts all Kotlin exceptions to errors.
|
||||
*/
|
||||
+ (BOOL)createCoroutineUninterceptedAndResumeFn:(id<KtKotlinSuspendFunction1>)fn receiver:(id _Nullable)receiver resultHolder:(KtResultHolder<id> *)resultHolder error:(NSError * _Nullable * _Nullable)error __attribute__((swift_name("createCoroutineUninterceptedAndResume(fn:receiver:resultHolder:)")));
|
||||
+ (void)gc __attribute__((swift_name("gc()")));
|
||||
@end
|
||||
|
||||
|
||||
@@ -382,6 +382,31 @@ __attribute__((swift_name("CoroutinesKt")))
|
||||
+ (void)invoke1Block:(id<KtKotlinSuspendFunction1>)block argument:(id _Nullable)argument completionHandler:(void (^)(id _Nullable_result, NSError * _Nullable))completionHandler __attribute__((swift_name("invoke1(block:argument:completionHandler:)")));
|
||||
+ (id<KtKotlinKSuspendFunction0>)getKSuspendCallableReference0 __attribute__((swift_name("getKSuspendCallableReference0()")));
|
||||
+ (id<KtKotlinKSuspendFunction1>)getKSuspendCallableReference1 __attribute__((swift_name("getKSuspendCallableReference1()")));
|
||||
|
||||
/**
|
||||
* @note This method converts all Kotlin exceptions to errors.
|
||||
*/
|
||||
+ (id _Nullable)startCoroutineUninterceptedOrReturnFn:(id<KtKotlinSuspendFunction0>)fn resultHolder:(KtResultHolder<id> *)resultHolder error:(NSError * _Nullable * _Nullable)error __attribute__((swift_name("startCoroutineUninterceptedOrReturn(fn:resultHolder:)"))) __attribute__((swift_error(nonnull_error)));
|
||||
|
||||
/**
|
||||
* @note This method converts all Kotlin exceptions to errors.
|
||||
*/
|
||||
+ (id _Nullable)startCoroutineUninterceptedOrReturnFn:(id<KtKotlinSuspendFunction1>)fn receiver:(id _Nullable)receiver resultHolder:(KtResultHolder<id> *)resultHolder error:(NSError * _Nullable * _Nullable)error __attribute__((swift_name("startCoroutineUninterceptedOrReturn(fn:receiver:resultHolder:)"))) __attribute__((swift_error(nonnull_error)));
|
||||
|
||||
/**
|
||||
* @note This method converts all Kotlin exceptions to errors.
|
||||
*/
|
||||
+ (id _Nullable)startCoroutineUninterceptedOrReturnFn:(id<KtKotlinSuspendFunction2>)fn receiver:(id _Nullable)receiver param:(id _Nullable)param resultHolder:(KtResultHolder<id> *)resultHolder error:(NSError * _Nullable * _Nullable)error __attribute__((swift_name("startCoroutineUninterceptedOrReturn(fn:receiver:param:resultHolder:)"))) __attribute__((swift_error(nonnull_error)));
|
||||
|
||||
/**
|
||||
* @note This method converts all Kotlin exceptions to errors.
|
||||
*/
|
||||
+ (BOOL)createCoroutineUninterceptedAndResumeFn:(id<KtKotlinSuspendFunction0>)fn resultHolder:(KtResultHolder<id> *)resultHolder error:(NSError * _Nullable * _Nullable)error __attribute__((swift_name("createCoroutineUninterceptedAndResume(fn:resultHolder:)")));
|
||||
|
||||
/**
|
||||
* @note This method converts all Kotlin exceptions to errors.
|
||||
*/
|
||||
+ (BOOL)createCoroutineUninterceptedAndResumeFn:(id<KtKotlinSuspendFunction1>)fn receiver:(id _Nullable)receiver resultHolder:(KtResultHolder<id> *)resultHolder error:(NSError * _Nullable * _Nullable)error __attribute__((swift_name("createCoroutineUninterceptedAndResume(fn:receiver:resultHolder:)")));
|
||||
+ (void)gc __attribute__((swift_name("gc()")));
|
||||
@end
|
||||
|
||||
|
||||
@@ -382,6 +382,31 @@ __attribute__((swift_name("CoroutinesKt")))
|
||||
+ (void)invoke1Block:(id<KtKotlinSuspendFunction1>)block argument:(id _Nullable)argument completionHandler:(void (^)(id _Nullable_result, NSError * _Nullable))completionHandler __attribute__((swift_name("invoke1(block:argument:completionHandler:)")));
|
||||
+ (id<KtKotlinKSuspendFunction0>)getKSuspendCallableReference0 __attribute__((swift_name("getKSuspendCallableReference0()")));
|
||||
+ (id<KtKotlinKSuspendFunction1>)getKSuspendCallableReference1 __attribute__((swift_name("getKSuspendCallableReference1()")));
|
||||
|
||||
/**
|
||||
* @note This method converts all Kotlin exceptions to errors.
|
||||
*/
|
||||
+ (id _Nullable)startCoroutineUninterceptedOrReturnFn:(id<KtKotlinSuspendFunction0>)fn resultHolder:(KtResultHolder *)resultHolder error:(NSError * _Nullable * _Nullable)error __attribute__((swift_name("startCoroutineUninterceptedOrReturn(fn:resultHolder:)"))) __attribute__((swift_error(nonnull_error)));
|
||||
|
||||
/**
|
||||
* @note This method converts all Kotlin exceptions to errors.
|
||||
*/
|
||||
+ (id _Nullable)startCoroutineUninterceptedOrReturnFn:(id<KtKotlinSuspendFunction1>)fn receiver:(id _Nullable)receiver resultHolder:(KtResultHolder *)resultHolder error:(NSError * _Nullable * _Nullable)error __attribute__((swift_name("startCoroutineUninterceptedOrReturn(fn:receiver:resultHolder:)"))) __attribute__((swift_error(nonnull_error)));
|
||||
|
||||
/**
|
||||
* @note This method converts all Kotlin exceptions to errors.
|
||||
*/
|
||||
+ (id _Nullable)startCoroutineUninterceptedOrReturnFn:(id<KtKotlinSuspendFunction2>)fn receiver:(id _Nullable)receiver param:(id _Nullable)param resultHolder:(KtResultHolder *)resultHolder error:(NSError * _Nullable * _Nullable)error __attribute__((swift_name("startCoroutineUninterceptedOrReturn(fn:receiver:param:resultHolder:)"))) __attribute__((swift_error(nonnull_error)));
|
||||
|
||||
/**
|
||||
* @note This method converts all Kotlin exceptions to errors.
|
||||
*/
|
||||
+ (BOOL)createCoroutineUninterceptedAndResumeFn:(id<KtKotlinSuspendFunction0>)fn resultHolder:(KtResultHolder *)resultHolder error:(NSError * _Nullable * _Nullable)error __attribute__((swift_name("createCoroutineUninterceptedAndResume(fn:resultHolder:)")));
|
||||
|
||||
/**
|
||||
* @note This method converts all Kotlin exceptions to errors.
|
||||
*/
|
||||
+ (BOOL)createCoroutineUninterceptedAndResumeFn:(id<KtKotlinSuspendFunction1>)fn receiver:(id _Nullable)receiver resultHolder:(KtResultHolder *)resultHolder error:(NSError * _Nullable * _Nullable)error __attribute__((swift_name("createCoroutineUninterceptedAndResume(fn:receiver:resultHolder:)")));
|
||||
+ (void)gc __attribute__((swift_name("gc()")));
|
||||
@end
|
||||
|
||||
|
||||
+59
-3
@@ -25,7 +25,25 @@ import kotlin.native.internal.*
|
||||
@kotlin.internal.InlineOnly
|
||||
public actual inline fun <T> (suspend () -> T).startCoroutineUninterceptedOrReturn(
|
||||
completion: Continuation<T>
|
||||
): Any? = (this as Function1<Continuation<T>, Any?>).invoke(completion)
|
||||
): Any? {
|
||||
val function = this as? Function1<Continuation<T>, Any?>
|
||||
return if (function != null)
|
||||
function.invoke(completion)
|
||||
else
|
||||
startCoroutineUninterceptedOrReturnFallback(this, completion)
|
||||
}
|
||||
|
||||
@Suppress("UNCHECKED_CAST")
|
||||
@PublishedApi
|
||||
internal fun <T> startCoroutineUninterceptedOrReturnFallback(
|
||||
function: suspend () -> T,
|
||||
completion: Continuation<T>
|
||||
): Any? {
|
||||
// Unlike `function`, `wrapper` class is generated and lowered entirely by Kotlin compiler,
|
||||
// so the cast below will succeed.
|
||||
val wrapper: suspend () -> T = { function() }
|
||||
return (wrapper as Function1<Continuation<T>, Any?>).invoke(completion)
|
||||
}
|
||||
|
||||
/**
|
||||
* Starts an unintercepted coroutine with receiver type [R] and result type [T] and executes it until its first suspension.
|
||||
@@ -44,7 +62,26 @@ public actual inline fun <T> (suspend () -> T).startCoroutineUninterceptedOrRetu
|
||||
public actual inline fun <R, T> (suspend R.() -> T).startCoroutineUninterceptedOrReturn(
|
||||
receiver: R,
|
||||
completion: Continuation<T>
|
||||
): Any? = (this as Function2<R, Continuation<T>, Any?>).invoke(receiver, completion)
|
||||
): Any? {
|
||||
val function = this as? Function2<R, Continuation<T>, Any?>
|
||||
return if (function != null)
|
||||
function.invoke(receiver, completion)
|
||||
else
|
||||
startCoroutineUninterceptedOrReturnFallback(this, receiver, completion)
|
||||
}
|
||||
|
||||
@Suppress("UNCHECKED_CAST")
|
||||
@PublishedApi
|
||||
internal fun <R, T> startCoroutineUninterceptedOrReturnFallback(
|
||||
function: suspend R.() -> T,
|
||||
receiver: R,
|
||||
completion: Continuation<T>
|
||||
): Any? {
|
||||
// Unlike `function`, `wrapper` class is generated and lowered entirely by Kotlin compiler,
|
||||
// so the cast below will succeed.
|
||||
val wrapper: suspend R.() -> T = { this.function() }
|
||||
return (wrapper as Function2<R, Continuation<T>, Any?>).invoke(receiver, completion)
|
||||
}
|
||||
|
||||
@Suppress("UNCHECKED_CAST")
|
||||
@kotlin.internal.InlineOnly
|
||||
@@ -52,7 +89,26 @@ internal actual inline fun <R, P, T> (suspend R.(P) -> T).startCoroutineUninterc
|
||||
receiver: R,
|
||||
param: P,
|
||||
completion: Continuation<T>
|
||||
): Any? = (this as Function3<R, P, Continuation<T>, Any?>).invoke(receiver, param, completion)
|
||||
): Any? {
|
||||
val function = this as? Function3<R, P, Continuation<T>, Any?>
|
||||
return if (function != null)
|
||||
function.invoke(receiver, param, completion)
|
||||
else
|
||||
startCoroutineUninterceptedOrReturnFallback(this, receiver, param, completion)
|
||||
}
|
||||
|
||||
@Suppress("UNCHECKED_CAST")
|
||||
internal fun <R, P, T> startCoroutineUninterceptedOrReturnFallback(
|
||||
function: suspend R.(P) -> T,
|
||||
receiver: R,
|
||||
param: P,
|
||||
completion: Continuation<T>
|
||||
): Any? {
|
||||
// Unlike `function`, `wrapper` class is generated and lowered entirely by Kotlin compiler,
|
||||
// so the cast below will succeed.
|
||||
val wrapper: suspend R.(P) -> T = { this.function(it) }
|
||||
return (wrapper as Function3<R, P, Continuation<T>, Any?>).invoke(receiver, param, completion)
|
||||
}
|
||||
|
||||
private object CoroutineSuspendedMarker
|
||||
|
||||
|
||||
Reference in New Issue
Block a user