Skip to content

JS Compiler Bug in Delegated Suspend Functions #352

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
FLUXparticle opened this issue May 4, 2018 · 2 comments
Closed

JS Compiler Bug in Delegated Suspend Functions #352

FLUXparticle opened this issue May 4, 2018 · 2 comments

Comments

@FLUXparticle
Copy link

There seems to be a bug in the kotlin2js compiler. When a suspend function is part of a delegate, the suspend function is called without continuation object. Here is a simple example: (tested with version 1.2.41)

interface SuspendInterface {

    suspend fun hello()

}

object SuspendObject : SuspendInterface {

    override suspend fun hello() {
        delay(1000)
        println("Hello from Kotlin Coroutines!")
    }

}

class SuspendDelegate(suspendInterface: SuspendInterface) :
        SuspendInterface by suspendInterface

fun main(args: Array<String>) {
    println("Hello World!")
    launch {
        SuspendObject.hello()
        SuspendDelegate(SuspendObject).hello()
    }
}

The first call to hello() works as expected. The second one throws an exception:

TypeError: Cannot read property 'context' of undefined
    at Coroutine$hello.CoroutineImpl (kotlin.js:30956)
    at new Coroutine$hello (suspend.js:35)
    at SuspendObject.hello (suspend.js:28)
    at SuspendDelegate.hello (suspend.js:91)
    at Coroutine$main$lambda.doResume (suspend.js:131)
    at Coroutine$main$lambda.CoroutineImpl.doResumeWrapper_0 (kotlin.js:30976)
    at Coroutine$main$lambda.CoroutineImpl.resume_11rb$ (kotlin.js:30965)
    at Coroutine$hello.CoroutineImpl.doResumeWrapper_0 (kotlin.js:30978)
    at Coroutine$hello.CoroutineImpl.resume_11rb$ (kotlin.js:30965)
    at resumeMode (kotlinx-coroutines-core.js:602)
@qwwdfsad
Copy link
Member

qwwdfsad commented May 4, 2018

Thank you for the report.
This is a known bug KT-23094 in kotlin2js compiler (not in kotlinx.coroutines).

As a workaround you can use handwritten delegation like we do https://github.com/Kotlin/kotlinx.coroutines/blob/develop/common/kotlinx-coroutines-core-common/src/main/kotlin/kotlinx/coroutines/experimental/channels/ChannelCoroutine.kt#L31

@systemkern
Copy link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants