From 274ea213f2e28bd5d7ef734dcfee0249d64e6b89 Mon Sep 17 00:00:00 2001 From: Romain Guy Date: Thu, 24 Oct 2024 09:30:38 -0700 Subject: [PATCH] Fix bytecode parsing with empty methods --- .../kotlin/explorer/bytecode/ByteCodeParser.kt | 7 +++---- .../dev/romainguy/kotlin/explorer/code/CodeTextArea.kt | 10 ++++++---- .../dev/romainguy/kotlin/explorer/code/DataModels.kt | 5 ++++- 3 files changed, 13 insertions(+), 9 deletions(-) diff --git a/src/jvmMain/kotlin/dev/romainguy/kotlin/explorer/bytecode/ByteCodeParser.kt b/src/jvmMain/kotlin/dev/romainguy/kotlin/explorer/bytecode/ByteCodeParser.kt index 2567502f..53bd66a9 100644 --- a/src/jvmMain/kotlin/dev/romainguy/kotlin/explorer/bytecode/ByteCodeParser.kt +++ b/src/jvmMain/kotlin/dev/romainguy/kotlin/explorer/bytecode/ByteCodeParser.kt @@ -82,7 +82,6 @@ class ByteCodeParser { Error(e) } } - } private fun PeekingIterator.readClass(classHeader: String): Class { @@ -107,9 +106,9 @@ private fun PeekingIterator.readMethod(): Method { val match = MethodRegex.matchEntire(next()) ?: throw IllegalStateException("Expected method but got '${peek()}'") val header = match.getValue("header") - if (next().trim() != "Code:") { - throw IllegalStateException("Expected 'Code:' but got '${peek()}'") - } + // A method can have no code + if (next().trim() != "Code:") return Method(header, InstructionSet(ISA.ByteCode, emptyList())) + val instructions = readInstructions() val lineNumbers = readLineNumbers() diff --git a/src/jvmMain/kotlin/dev/romainguy/kotlin/explorer/code/CodeTextArea.kt b/src/jvmMain/kotlin/dev/romainguy/kotlin/explorer/code/CodeTextArea.kt index 7e945e6d..8847342f 100644 --- a/src/jvmMain/kotlin/dev/romainguy/kotlin/explorer/code/CodeTextArea.kt +++ b/src/jvmMain/kotlin/dev/romainguy/kotlin/explorer/code/CodeTextArea.kt @@ -56,10 +56,12 @@ class CodeTextArea( if (sourceTextArea != null) { addMouseListener(object : MouseAdapter() { override fun mouseClicked(event: MouseEvent) { - val codeLine = getLineOfOffset(viewToModel2D(event.point)) - val line = code?.getSourceLine(codeLine) ?: return - if (line == -1) return - sourceTextArea.gotoLine(this@CodeTextArea, line - 1) + if (isSyncLinesEnabled) { + val codeLine = getLineOfOffset(viewToModel2D(event.point)) + val line = code?.getSourceLine(codeLine) ?: return + if (line == -1) return + sourceTextArea.gotoLine(this@CodeTextArea, line - 1) + } } }) } diff --git a/src/jvmMain/kotlin/dev/romainguy/kotlin/explorer/code/DataModels.kt b/src/jvmMain/kotlin/dev/romainguy/kotlin/explorer/code/DataModels.kt index 069bacff..c6fe6d34 100644 --- a/src/jvmMain/kotlin/dev/romainguy/kotlin/explorer/code/DataModels.kt +++ b/src/jvmMain/kotlin/dev/romainguy/kotlin/explorer/code/DataModels.kt @@ -103,5 +103,8 @@ data class Instruction( data class MethodReference(val address: Int, val name: String) fun List.withLineNumbers(lineNumbers: IntIntMap): List { - return map { it.copy(lineNumber = lineNumbers.getOrDefault(it.address, -1)) } + return map { + val line = lineNumbers.getOrDefault(it.address, -1) + if (line != -1) it.copy(lineNumber = line) else it + } }