Skip to content

Commit

Permalink
Preload serializable exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
natario1 committed Jun 9, 2024
1 parent c1dbb6e commit 0f6524e
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 10 deletions.
3 changes: 3 additions & 0 deletions knee-compiler-plugin/src/main/kotlin/Init.kt
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,9 @@ sealed class InitInfo {
val serializableExceptions = mutableSetOf<IrClass>()
fun serializableException(klass: IrClass) {
serializableExceptions.add(klass) // can't be exported
// Since throwing JVM exception can be needed at any point in the KN lifecycle,
// we must preload them otherwise runtime may fail to find the class in the given environment.
preloads.add(klass.defaultType)
}

val registerNativesEntries = mutableListOf<RegisterNativesEntry>()
Expand Down
6 changes: 0 additions & 6 deletions knee-compiler-plugin/src/main/kotlin/MainBir.kt
Original file line number Diff line number Diff line change
Expand Up @@ -78,12 +78,6 @@ private fun process(context: KneeContext, codegen: KneeCodegen) {
processInit(info = initInfo, context = context, codegen = codegen)
context.log.logMessage("[*] Writing generated code in ${codegen.root.absolutePath}")
codegen.write()

/* val exportedData = (data.allEnums + data.allInterfaces + data.allClasses).joinToString {
it.source.defaultType.toString()
}
context.log.print("[*] Exporting data: $exportedData") */

}

private inline fun <T: KneeFeature<*>> List<T>.processEach(context: KneeContext, block: (T) -> Unit) {
Expand Down
8 changes: 4 additions & 4 deletions knee-runtime/src/backendMain/kotlin/Init.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,24 @@ internal var initializationData: InitializationData? = null

internal class InitializationData(
val jvm: JavaVirtualMachine,
val exceptions: Set<SerializableException>
val exceptions: MutableSet<SerializableException>
)

fun initKnee(environment: JniEnvironment, vararg modules: KneeModule) {
val vm = environment.javaVM
val id = vm.rawValue.toLong()
val oldId = kneeInitialized.getAndSet(id)
if (id != oldId) {
val exceptions = mutableSetOf<SerializableException>()
modules.forEach { it.collectExceptions(exceptions) }
initializationData = InitializationData(vm, exceptions)
initializationData = InitializationData(vm, mutableSetOf())
initSuspend(environment)
initInstances(environment)
initBoxMethods(environment)
initExceptions(environment)
initBuffers(environment)
}
val data = initializationData!!
modules.forEach {
it.collectExceptions(data.exceptions)
it.initializeIfNeeded(environment)
}
}

0 comments on commit 0f6524e

Please sign in to comment.