I have a Kotlin multiplatform project aiming to serialize/deserialize and exchange some objects between jvm and browser target applications. Because Kotlin/js has no full reflective support, the full classname of the kotlin class is not known in the browser (though it is in the JVM). To that end, I want to extend the $metadata$ object that Kotlin attaches to each js-'compiled' class with the respective full class name by traversing the package tree. The code in and by itself runs and works, however I am having trouble getting the full scope of all classes loaded, as I only ever get all classes loaded inside the module I defined the traversing function in.
After some deeper dig, I partially understood, that the modules loaded by the js-compiled Kotlin only ever have access to the modules they themselves include. Is there a way to (still) access all loaded modules, so I can extend their respective class metadata, without explicitly passing them as explicit arguments?
I have already found this answer which does not seem applicable, as there is no require.s value: require.js: Access all loaded modules
To make it clear, I want to do something like this:
private var initialized = false
/**
* Initialize package scopes and map classes to names.
*/
fun initScopes() {
if (! initialized) {
initialized = true
val mdl = js("module")
val moduleExports = mdl.exports
for (moduleName in Object().keys(moduleExports)) {
if (moduleName is String) {
val module = moduleExports[moduleName] ?: continue
traversePackages("", moduleName, module as Any) // this will find all classes from any root package.
}
}
}
}
Since this function is located in a project I want to use in other projects, those projects have a larger scope respectively. So when I call initScopes from those packages, I want them to contain all loaded classes there. I could pass module as argument, I suppose, but I can't do so in my JVM target, because dynamic does not exist there.
On a sidenote: I know there already exist serialization solutions, but I have specific use cases I cannot solve using existing ones, which is why I am building my own solution.