Skip to content

"Uncaught TypeError: Cannot read property 'context' of undefined" when compiling to JS #1145

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
ghost opened this issue Apr 25, 2019 · 11 comments

Comments

@ghost
Copy link

ghost commented Apr 25, 2019

seems similar to this, but the link is dead: #352

import kotlinx.coroutines.*

val pass=Unit

class Brainbot(program:String){ 
    private var prog=bbcompiler.Brainbot2BBCL(program)
    var BBCL=prog.show_data()
    private var running=false
    private var coderun : Deferred<Int>? = null

    suspend fun run(input:Int):Int? = coroutineScope{
        var x:Int?=null
        if(running)
            throw Exception("already running")
        running=true
        coderun=async{runProgram(prog, input).result}
        coderun?.await()
    }

    suspend fun run(input:Int?):Int?
    {
        if(input==null)
            return run(0)
        return run(input)
    }

    fun halt()
    {
        if(!running)
            throw Exception("Not running!")
        coderun?.cancel()
        running=false
    }
}

Error:

libs.js:166 Uncaught TypeError: Cannot read property 'context' of undefined
    at Coroutine$run.Jb (libs.js:166)
    at new Coroutine$run (brainbot.js:1526)
    at Object.run (brainbot.js:1587)
    at HTMLButtonElement.onclick (index.html:19)

I might be doing something wrong or this might be a bug

Is there any workaround for this

@elizarov
Copy link
Contributor

What version of Kotlin Compiler and library are you using?

@ghost
Copy link
Author

ghost commented Apr 27, 2019

Kotlin 1.3.30, Coroutines 1.2.0

btw I ended up solving this by using Javascript Workers instead, for anyone reaching here from Google

@elizarov
Copy link
Contributor

Do you have a self-contained example with problem to file a bug for JS compiler? It does not seem to be related to #325, though, since thing code does not seem to have any suspend properties or do you use them somewhere else?

@qwwdfsad
Copy link
Member

Closing as outdated

@systemkern
Copy link

systemkern commented Jul 6, 2019

I hope this helps some googler since this ticket is the top result for the error message
I found a very ugly workaround which works (for my purposes)

Using Kotlin 1.3.41 and Coroutines 1.3.20M2

An example Kotlin function:

suspend fun doStuff(/*note! no function arguments here*/) {
    println("printed from the Frontend Kotlin File")
}

will get compiled to this

function doStuff(continuation_0, suspended) { 
    var instance = new Coroutine$main(continuation_0);
    if (suspended)
      return instance;
    else
      return instance.doResume(null);
  }

The second to last parameter of a suspend function in Javascript is apparently a coroutine continuation,
Afaik this is just a proxy function handling the coroutine logic - since the actual logic will reside in another (anonymous) function

While debugging you can see that the continuation_0 is undefined in java, so passing Kotlin's EmptyContinuation to the function works (at least in some cases)

myapp.doStuff(kotlin.kotlin.coroutines.js.internal.EmptyContinuation);

Cheers and all the best,
Rainer

@systemkern
Copy link

systemkern commented Jul 6, 2019

Actually, there is a much much better way

Wrap your Kotlin function in a javascript promise

fun doStuff(): Promise<String> = //or Promise<Unit> for voids
    GlobalScope.promise { 
        callToActualSuspendFunction() // return a String
    }
output.doStuff().then(function(returnValue) {
    alert (returnValue);
});

source: https://discuss.kotlinlang.org/t/how-do-i-call-coroutines-from-javascript/12098/2

@qwwdfsad
Copy link
Member

I hope this helps some googler since this ticket is the top result for the error message

How did you get this error message? By trying to invoke suspend function from javascript and passing null to the last parameter?

@systemkern
Copy link

Hi @qwwdfsad,

How did you get this error message? By trying to invoke suspend function from javascript and passing null to the last parameter?

Well, probably yes (my JS-Fu is not at 100% ;-)).
I called doStuff() as it was defined in Kotlin - so without any parameters. Thus, since the JS Version has parameters, they would all be null
cheers

@boyvanduuren
Copy link

I reproduced the issue in https://github.com/boyvanduuren/reproducer. The kotlin is already compiled to JS in there, but you can recompile if you want. The issue is demonstrated by running node runme.js in the root dir of the project.

@elizarov
Copy link
Contributor

Thanks. I've reposted it here: https://youtrack.jetbrains.com/issue/KT-32708

@simon-alithya
Copy link

Hi everyone,
Today i try this solution but i continue to have
i wrap my common function in the kotlin/js with promise like you show but i still have issue ?
some one have try it in his side ?

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

No branches or pull requests

5 participants