Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Preload serializable exceptions #5

Merged
merged 1 commit into from
Jun 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
}
}