Is there any way to force some code to be run non-...
# javascript
m
Is there any way to force some code to be run non-lazily when the JS file is loaded? Even when using
-Xir-property-lazy-initialization
. With
-Xir-per-module
I need to know if a certain module was already loaded. The only which comes to mind is to set a global variable when loading that module, and check if that variable set from other modules.
s
Currently there are two custom things that run during module load with `-Xir-property-lazy-initialization`: • top-level main function • tests annotated with kotlin.test.Test Either should work
Copy code
import kotlin.test.Test

class Foo {
    @Test
    fun foo() {
        thisModuleIsLoaded = true
    }
}

// At most one main function per module
fun main() {
    thisModuleIsLoaded = true
}
🙏 1
m
@Svyatoslav Kuzmich [JB] unfortunately it doesn’t work with “top-level main function” when also using
-Xir-per-module
. The
main
functions of modules get dead-code-eliminated. I can force-keep it with with
@JsExport
but then I run into another issue because multiple modules export a symbol with the same name `main`:
Copy code
IllegalStateException: IrSimpleFunctionPublicSymbolImpl for public /main|-4284757841571462650[0] is already bound: FUN name:main visibility:public modality:FINAL <> () returnType:<Uninitialized>
Any other ideas how to run code when such a module loads?
Oh I found a workaround. These
main
funs need to be
private
😮
@JsExport private fun main()