question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Kotlin/JS generated code references undefined variable when @Serializable

See original GitHub issue

I have a simple class that I’m trying to make Serializable, but even just adding @Serializable on the class causes the generated JS code to throw an exception.

Here’s the class:

@Serializable
class MyClass(val questions: List<String>, val finaleQuestions: List<String>){

    val rounds: List<String>

    init {
        rounds = mutableListOf<String>().also { mutableRounds ->
            questions.forEach {
                mutableRounds.add(it)
            }
        }
    }
}

var myClass = MyClass(listOf("a"), listOf("b"))

Here’s the code generated when @Serializable is present:

function MyClass(questions, finaleQuestions) {
    MyClass$Companion_getInstance();
    this.questions = questions;
    this.finaleQuestions = finaleQuestions;
    this.currentRoundCounter = 0;
    this.rounds = null;
    var $receiver = ArrayList_init();
    var tmp$;
    tmp$ = this$MyClass.questions.iterator(); // <-- throws "this$MyClass is not defined"
    while (tmp$.hasNext()) {
      var element = tmp$.next();
      closure$mutableRounds.add_11rb$(element); //<-- this is also different btw
    }
    this.rounds = $receiver;
  } 

Code generated without @Serializable (works):

function MyClass(questions, finaleQuestions) {
    this.questions = questions;
    this.finaleQuestions = finaleQuestions;
    this.rounds = null;
    var $receiver = ArrayList_init();
    var tmp$;
    tmp$ = this.questions.iterator(); // <-- difference here
    while (tmp$.hasNext()) {
      var element = tmp$.next();
      $receiver.add_11rb$(element);
    }
    this.rounds = $receiver;
  }

This is using id 'kotlin2js' version '1.3.0-rc-190' and compile "org.jetbrains.kotlinx:kotlinx-serialization-runtime-js:0.8.3-rc13"

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:4
  • Comments:8 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
mschirmachercommented, Mar 7, 2019

Stumbled across this bug today, too. It took me half the day to find out that this code breaks the js-code:

init {
        children.forEach { it.parent = this}
    }

While this code doesn’t

init {
        for (child in children) {
            child.parent = this
        }
    }

This is really annoying 😦

0reactions
sandwwraithcommented, Jul 19, 2021

It should be fixed in the IR in recent Kotlin versions (legacy compiler still affected). If problem happens in IR, please reopen

Read more comments on GitHub >

github_iconTop Results From Across the Web

Kotlin to JS Iterator is not a function - Stack Overflow
The object missing the iterator method was deserialized from a Json string. If this is the case for you, then making sure that...
Read more >
Migrating our Kotlin/JS app to the new IR compiler
Let's look at some of the symptoms our CodeQuiz app exhibited when first running the application with IR, and let's correct the related...
Read more >
KJS: .d.ts generation not working for enum classes : KT-37916
I think 1-3 are all important for working with an enum class. Typescript code should be able to get an enum value from...
Read more >
So I m trying to use JS gt Kotlin But every time I try and u
... you briefly describe the way you use Kotlin/JS generated code from JS? ... that the minified version of Kotlin.js cannot be used...
Read more >
Building a Back Office with Kotlin JS & Firebase - Etienne Caron
You'll find the code backing all my kotlin-js examples under the ... the .js and .html files generated under ~/build/distributions to our ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found