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 to WASM - by lazy init does not work

See original GitHub issue

Another Kotlin oddity, but I have managed to track the exception down to the following by lazy. I am using the shadowJar, so I would have expected the Kotlin stdlib to be included, although this seems to be something reflection based from standard Java…

Here is the command line I am using:

java -jar bytecoder-cli-2019-06-13-executable.jar -classpath=./build/libs/art-trimmed-all.jar -mainclass=art.EntryKotlin -builddirectory=build -backend=wasm -minify=false -optimizationlevel=ALL -filenameprefix=main -maximumwasmpages=1024

and the code that generates the exception below.

class EntryKotlin {
    val me: String by lazy {
        "Hola"
    }

    init {
        println("new entry kotlin")
        //val nada = Class.forName("")
        Main()
    }

    companion object {
        @JvmStatic
        fun main(args: Array<String>?) {
            EntryKotlin()
        }
    }
}
Exception in thread "main" java.lang.reflect.InvocationTargetException
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:498)
        at org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java:48)
        at org.springframework.boot.loader.Launcher.launch(Launcher.java:87)
        at org.springframework.boot.loader.Launcher.launch(Launcher.java:50)
        at org.springframework.boot.loader.JarLauncher.main(JarLauncher.java:51)
Caused by: java.lang.RuntimeException: Error linking class art.EntryKotlin
        at de.mirkosertic.bytecoder.core.BytecodeLinkerContext.resolveClass(BytecodeLinkerContext.java:111)
        at de.mirkosertic.bytecoder.backend.CompileTarget.compile(CompileTarget.java:81)
        at de.mirkosertic.bytecoder.cli.BytecoderCLI.main(BytecoderCLI.java:111)
        ... 8 more
Caused by: java.lang.RuntimeException: Error linking class kotlin.jvm.internal.Reflection
        at de.mirkosertic.bytecoder.core.BytecodeLinkerContext.resolveClass(BytecodeLinkerContext.java:111)
        at de.mirkosertic.bytecoder.core.BytecodeInstructionINVOKESTATIC.performLinking(BytecodeInstructionINVOKESTATIC.java:33)
        at de.mirkosertic.bytecoder.core.BytecodeLinkedClass.resolveMethodSignatureAndBody(BytecodeLinkedClass.java:400)
        at de.mirkosertic.bytecoder.core.BytecodeLinkedClass.resolveStaticMethod(BytecodeLinkedClass.java:370)
        at de.mirkosertic.bytecoder.core.BytecodeLinkedClass.resolveClassInitializer(BytecodeLinkedClass.java:148)
        at de.mirkosertic.bytecoder.core.BytecodeLinkerContext.resolveClass(BytecodeLinkerContext.java:98)
        ... 10 more
Caused by: java.lang.IllegalStateException: Cannot find static method forName in java/lang/Class with signature java.lang.Class(java.lang.String)
        at de.mirkosertic.bytecoder.core.BytecodeInstructionINVOKESTATIC.performLinking(BytecodeInstructionINVOKESTATIC.java:35)
        at de.mirkosertic.bytecoder.core.BytecodeLinkedClass.resolveMethodSignatureAndBody(BytecodeLinkedClass.java:400)
        at de.mirkosertic.bytecoder.core.BytecodeLinkedClass.resolveStaticMethod(BytecodeLinkedClass.java:370)
        at de.mirkosertic.bytecoder.core.BytecodeLinkedClass.resolveClassInitializer(BytecodeLinkedClass.java:148)
        at de.mirkosertic.bytecoder.core.BytecodeLinkerContext.resolveClass(BytecodeLinkerContext.java:98)
        ... 15 more

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:19 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
WhiteHexagoncommented, Apr 3, 2020

more good news, the by lazy is now working 😃 thanks!

btw the .wasm file has jumped by 560KB to 720KB , but maybe that is the GC stuff for the new version? Anyway tomorrow I try the new llvm target.

0reactions
WhiteHexagoncommented, Apr 23, 2020

I missed the notification about this, but that is great news! I shall get back to testing it then 😃

Read more comments on GitHub >

github_iconTop Results From Across the Web

Property initialization using "by lazy" vs. "lateinit"
Unlike lazy initialization, lateinit allows the compiler to recognize that the value of the non-null property is not stored in the constructor ...
Read more >
Lazy Initialization in Kotlin - Baeldung
The concept of 'lazy initialization' was designed to prevent unnecessary initialization of objects. In Java, creating an object in a lazy and ...
Read more >
Why is 'by lazy' not happy with in-construction property ...
The problem is that the compiler cannot figure out when x is accessed within the lazy's lambda. Does lazy call the lambda immediately...
Read more >
Kotlin to WebAssembly - The Ultimate Guide to ... - Educative.io
With WebAssembly, you can compile code written in many different languages—including C, C++, Rust, Go, C#, Python, Java, and Kotlin—to the WASM binary ......
Read more >
Kotlin: Write Once, Run (Actually) Everywhere - InfoQ
So that lazy pattern, the pattern of lazy initialization is something that you can repeat over and over and over again. But what...
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