I found an interesting issue with wasm on 2.0.20 ...
# webassembly
d
I found an interesting issue with wasm on 2.0.20 Lets say I have the following code
Copy code
sealed class FooBar {
    object Foo : FooBar()
    object Bar : FooBar()
}

fun takeFooBar(fooBar: FooBar) {
    // TBD
}

fun main() {
    // Wasm thinks this is an Any
    val bar = if (true) {
        FooBar.Foo
    } else {
        FooBar.Bar
    }
    takeFooBar(fooBar = bar)
}
ill get an error at runtime like the following
Copy code
Uncaught (in promise) CompileError: WebAssembly.instantiateStreaming(): Compiling function #144978:"com.package..main..etc..." failed: call[2] expected type (ref null 37332), found local.get of type (ref null 2550) @+32891844
doing a
wasm-objdump
i can see that type 2550 is of type
Any
and
37332
is type FooBar I have two ways of making this work by changing the code to either
Copy code
val bar: FooBar = if (true) {
        FooBar.Foo
    } else {
        FooBar.Bar
    }
or making the FooBar instances a class
Copy code
sealed class FooBar {
    class Foo : FooBar()
    class Bar : FooBar()
}

fun takeFooBar(fooBar: FooBar) {
    // TBD
}

fun main() {
    // This works.
    val bar = if (true) {
        FooBar.Foo()
    } else {
        FooBar.Bar()
    }
    takeFooBar(fooBar = bar)
}
Is this a known issue? is there any work arounds as we use objects a lot in our code base and going through and adding types would be quite a chore.
a
mmm, interesting. Could you please create a ticket for it?
d
Yah for sure, ill get a sample project up showcasing this too
a
May I also ask you check it with 2.1.0?
d
that was my next step
ill report back
a
gratitude thank you
d
in worse news.... 2.1 fails on our project with the following
Copy code
kotlin.UninitializedPropertyAccessException: lateinit property body has not been initialized
	at org.jetbrains.kotlin.ir.declarations.impl.IrAnonymousInitializerImpl.getBody(IrAnonymousInitializerImpl.kt:37)
ill test it out on a sample i suppose. Any ideas on this error?
a
It's an internal compiler error, so, need to debug and investigate the issue on a sample project
d
Could this be because serialization is not yet pointing to 2.1?
a
Maybe, because I can't reproduce it on the playground: https://pl.kotl.in/QXZuVLM1n
d
Alright, I'll wait for them to publish a new version, hopefully not too long. 🤞