Kotlin/JS generated code references undefined variable when @Serializable
See original GitHub issueI 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:
- Created 5 years ago
- Reactions:4
- Comments:8 (3 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Stumbled across this bug today, too. It took me half the day to find out that this code breaks the js-code:
While this code doesn’t
This is really annoying 😦
It should be fixed in the IR in recent Kotlin versions (legacy compiler still affected). If problem happens in IR, please reopen