Skip to content

Implement exception handling in java-slang#96

Draft
kjw142857 wants to merge 2 commits into
mainfrom
exception-handling
Draft

Implement exception handling in java-slang#96
kjw142857 wants to merge 2 commits into
mainfrom
exception-handling

Conversation

@kjw142857

@kjw142857 kjw142857 commented Jun 16, 2026

Copy link
Copy Markdown
Contributor

Implement exception handling for the following components (in progress):

  • JVM
  • Compiler
  • Type Checker
  • Parser

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces support for compiling try-catch-finally statements, refactors the JVM's ExceptionTable to be iterable and use a dedicated class structure, and adds unit tests for exception routing. It also fixes a buffer offset calculation bug in the disassembler when reading code attributes. The reviewer identified several critical bugs in the TryStatement code generation implementation—including a pass-by-value bug with unresolved label offsets, an early return bug when catches are absent but a finally block is present, and incorrect execution paths for finally blocks—and provided a comprehensive rewrite to address these issues.

Important

The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.

Comment on lines +580 to +695
TryStatement: (node: Node, cg: CodeGenerator) => {
let maxStack = 0
const { block, catches } = node as any

// If no catches, just compile the try block
if (!catches || !catches.catchClauses || catches.catchClauses.length === 0) {
return { stackSize: compile(block, cg).stackSize, resultType: EMPTY_TYPE }
}

// mark start of protected region
const tryStart = cg.generateNewLabel()
tryStart.offset = cg.code.length

// compile try block
maxStack = Math.max(maxStack, compile(block, cg).stackSize)

// end of protected region (first instruction after try block)
const tryEnd = cg.generateNewLabel()
tryEnd.offset = cg.code.length

// jump over handlers when try completes normally
const afterHandlers = cg.generateNewLabel()
cg.addBranchInstr(OPCODE.GOTO, afterHandlers)

// For each catch clause, emit a handler and an exception table entry
for (const catchClause of catches.catchClauses) {
const handlerLabel = cg.generateNewLabel()
handlerLabel.offset = cg.code.length

// determine catch type index (constant pool)
const catchTypeNode = catchClause.catchFormalParameter.catchType
const catchTypeName = unannTypeToString(catchTypeNode.unannClassType)
let catchClassName = 'java/lang/Throwable'
try {
catchClassName = cg.symbolTable.queryClass(catchTypeName).name
} catch (e) {
catchClassName = catchTypeName.includes('/') ? catchTypeName : catchTypeName.replace(/\./g, '/')
}
const catchTypeIndex = cg.constantPoolManager.indexClassInfo(catchClassName)

// add exception table entry (startPc, endPc, handlerPc, catchType)
cg.exceptionTable.push({ startPc: tryStart.offset, endPc: tryEnd.offset, handlerPc: handlerLabel.offset, catchType: catchTypeIndex })

// create scope for catch variable
cg.symbolTable.extend()
const varName = catchClause.catchFormalParameter.variableDeclaratorId
const varTypeStr = unannTypeToString(catchTypeNode.unannClassType)
const varInfo = {
name: varName,
accessFlags: 0,
index: cg.maxLocals,
typeName: varTypeStr,
typeDescriptor: cg.symbolTable.generateFieldDescriptor(varTypeStr)
}
cg.symbolTable.insertVariableInfo(varInfo)
if (['J', 'D'].includes(varInfo.typeDescriptor)) {
cg.maxLocals += 2
} else {
cg.maxLocals++
}

// at handler entry, the exception object is on the stack; store it into the local
cg.code.push(OPCODE.ASTORE, varInfo.index)

// compile catch block statements
const catchBlock = catchClause.block
catchBlock.blockStatements.forEach((stmt: any) => {
const { stackSize } = compile(stmt, cg)
maxStack = Math.max(maxStack, stackSize)
})

// teardown catch scope
cg.symbolTable.teardown()

// after handler, jump to afterHandlers
cg.addBranchInstr(OPCODE.GOTO, afterHandlers)
}

// If finally exists, add a catch-all handler that runs finally then rethrows
const finallyNode: any = (node as any).finally
if (finallyNode) {
const catchAllLabel = cg.generateNewLabel()
catchAllLabel.offset = cg.code.length
cg.exceptionTable.push({ startPc: tryStart.offset, endPc: tryEnd.offset, handlerPc: catchAllLabel.offset, catchType: 0 })

// allocate temp local to store exception
const tempIndex = cg.maxLocals
cg.maxLocals += 1
cg.code.push(OPCODE.ASTORE, tempIndex)

// compile finally block inside catch-all
finallyNode.blockStatements.forEach((stmt: any) => {
const { stackSize } = compile(stmt, cg)
maxStack = Math.max(maxStack, stackSize)
})

// reload exception and rethrow
cg.code.push(OPCODE.ALOAD, tempIndex, OPCODE.ATHROW)

// normal finally path: compile finally once for normal/handled flows
const finallyLabel = cg.generateNewLabel()
finallyLabel.offset = cg.code.length
finallyNode.blockStatements.forEach((stmt: any) => {
const { stackSize } = compile(stmt, cg)
maxStack = Math.max(maxStack, stackSize)
})

// place after-handlers label
afterHandlers.offset = cg.code.length
} else {
// no finally: place after-handlers label
afterHandlers.offset = cg.code.length
}

return { stackSize: maxStack, resultType: EMPTY_TYPE }
},

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

critical

There are several critical correctness and code generation issues in the current implementation of TryStatement:

  1. Pass-by-value bug with catchAllLabel.offset: When pushing the catch-all entry to cg.exceptionTable, catchAllLabel.offset is 0 because it hasn't been compiled yet. Since numbers are primitive types in JavaScript/TypeScript, they are copied by value, meaning the exception table entry will permanently point to PC 0 instead of the actual catch-all handler offset.
  2. Early return bug: If there are no catch clauses but a finally block is present (e.g., try { ... } finally { ... }), the code returns early and completely ignores the finally block.
  3. Normal path finally execution: The normal path finally block is compiled but never executed because the try block jumps straight to afterHandlers (which is placed after the normal finally path).
  4. Catch block exception handling: If an exception is thrown inside a catch block, the finally block is not executed because the catch-all handler's protected region only covers the try block.
  5. Finally block exception safety: The catch-all handler should not protect the finally blocks themselves to avoid infinite loops or double execution if an exception is thrown inside a finally block.

To resolve all these issues, we can collect the exception table entries in a local array during compilation and push them to cg.exceptionTable at the end of TryStatement once all labels are fully resolved. We also inline the finally block correctly on all execution paths (normal, caught, and uncaught).

  TryStatement: (node: Node, cg: CodeGenerator) => {
    let maxStack = 0
    const { block, catches } = node as any
    const finallyNode: any = (node as any).finally

    const hasCatches = catches && catches.catchClauses && catches.catchClauses.length > 0

    if (!hasCatches && !finallyNode) {
      return { stackSize: compile(block, cg).stackSize, resultType: EMPTY_TYPE }
    }

    if (hasCatches || finallyNode) {
      maxStack = Math.max(maxStack, 1)
    }

    const localExceptionTable: Array<{
      startPc: number
      endPc: number
      handlerLabel: Label
      catchType: number
    }> = []

    // mark start of protected region
    const tryStart = cg.generateNewLabel()
    tryStart.offset = cg.code.length

    // compile try block
    maxStack = Math.max(maxStack, compile(block, cg).stackSize)

    // end of protected region (first instruction after try block)
    const tryEnd = cg.generateNewLabel()
    tryEnd.offset = cg.code.length

    const catchAllLabel = finallyNode ? cg.generateNewLabel() : null

    // If finally exists, add catch-all entry for the try block
    if (finallyNode && catchAllLabel) {
      localExceptionTable.push({
        startPc: tryStart.offset,
        endPc: tryEnd.offset,
        handlerLabel: catchAllLabel,
        catchType: 0
      })
    }

    // For normal path: run finally block if it exists
    if (finallyNode) {
      finallyNode.blockStatements.forEach((stmt: any) => {
        const { stackSize } = compile(stmt, cg)
        maxStack = Math.max(maxStack, stackSize)
      })
    }

    // jump over handlers when try completes normally
    const afterHandlers = cg.generateNewLabel()
    cg.addBranchInstr(OPCODE.GOTO, afterHandlers)

    // For each catch clause, emit a handler and an exception table entry
    if (hasCatches) {
      for (const catchClause of catches.catchClauses) {
        const handlerLabel = cg.generateNewLabel()
        handlerLabel.offset = cg.code.length

        // determine catch type index (constant pool)
        const catchTypeNode = catchClause.catchFormalParameter.catchType
        const catchTypeName = unannTypeToString(catchTypeNode.unannClassType)
        let catchClassName = 'java/lang/Throwable'
        try {
          catchClassName = cg.symbolTable.queryClass(catchTypeName).name
        } catch (e) {
          catchClassName = catchTypeName.includes('/') ? catchTypeName : catchTypeName.replace(/\./g, '/')
        }
        const catchTypeIndex = cg.constantPoolManager.indexClassInfo(catchClassName)

        // add exception table entry (startPc, endPc, handlerPc, catchType)
        localExceptionTable.push({
          startPc: tryStart.offset,
          endPc: tryEnd.offset,
          handlerLabel: handlerLabel,
          catchType: catchTypeIndex
        })

        // create scope for catch variable
        cg.symbolTable.extend()
        const varName = catchClause.catchFormalParameter.variableDeclaratorId
        const varTypeStr = unannTypeToString(catchTypeNode.unannClassType)
        const varInfo = {
          name: varName,
          accessFlags: 0,
          index: cg.maxLocals,
          typeName: varTypeStr,
          typeDescriptor: cg.symbolTable.generateFieldDescriptor(varTypeStr)
        }
        cg.symbolTable.insertVariableInfo(varInfo)
        if (['J', 'D'].includes(varInfo.typeDescriptor)) {
          cg.maxLocals += 2
        } else {
          cg.maxLocals++
        }

        // at handler entry, the exception object is on the stack; store it into the local
        cg.code.push(OPCODE.ASTORE, varInfo.index)

        const catchStartOffset = cg.code.length

        // compile catch block statements
        const catchBlock = catchClause.block
        catchBlock.blockStatements.forEach((stmt: any) => {
          const { stackSize } = compile(stmt, cg)
          maxStack = Math.max(maxStack, stackSize)
        })

        const catchEndOffset = cg.code.length

        // teardown catch scope
        cg.symbolTable.teardown()

        // If finally exists, add catch-all entry for this catch block
        if (finallyNode && catchAllLabel && catchStartOffset < catchEndOffset) {
          localExceptionTable.push({
            startPc: catchStartOffset,
            endPc: catchEndOffset,
            handlerLabel: catchAllLabel,
            catchType: 0
          })
        }

        // For caught path: run finally block if it exists
        if (finallyNode) {
          finallyNode.blockStatements.forEach((stmt: any) => {
            const { stackSize } = compile(stmt, cg)
            maxStack = Math.max(maxStack, stackSize)
          })
        }

        // after handler, jump to afterHandlers
        cg.addBranchInstr(OPCODE.GOTO, afterHandlers)
      }
    }

    // If finally exists, add a catch-all handler that runs finally then rethrows
    if (finallyNode && catchAllLabel) {
      catchAllLabel.offset = cg.code.length

      // allocate temp local to store exception
      const tempIndex = cg.maxLocals
      cg.maxLocals += 1
      cg.code.push(OPCODE.ASTORE, tempIndex)

      // compile finally block inside catch-all
      finallyNode.blockStatements.forEach((stmt: any) => {
        const { stackSize } = compile(stmt, cg)
        maxStack = Math.max(maxStack, stackSize)
      })

      // reload exception and rethrow
      cg.code.push(OPCODE.ALOAD, tempIndex, OPCODE.ATHROW)
    }

    // place after-handlers label
    afterHandlers.offset = cg.code.length

    // Now that all labels are resolved, push to cg.exceptionTable
    localExceptionTable.forEach(entry => {
      cg.exceptionTable.push({
        startPc: entry.startPc,
        endPc: entry.endPc,
        handlerPc: entry.handlerLabel.offset,
        catchType: entry.catchType
      })
    })

    return { stackSize: maxStack, resultType: EMPTY_TYPE }
  },

@github-actions

Copy link
Copy Markdown

Coverage report

St.
Category Percentage Covered / Total
🟡 Statements
72.05% (-0.45% 🔻)
7415/10292
🔴 Branches
58.99% (-0.19% 🔻)
2481/4206
🟡 Functions
68.75% (-0.29% 🔻)
1322/1923
🟡 Lines
72.89% (-0.49% 🔻)
6981/9578
Show new covered files 🐣
St.
File Statements Branches Functions Lines
🔴
... / exception-table.ts
23.08% 20% 28.57% 25%
Show files with reduced coverage 🔻
St.
File Statements Branches Functions Lines
🟡
... / code-generator.ts
63.97% (-4.3% 🔻)
59.42% (-1.62% 🔻)
62.35% (-3.08% 🔻)
64.62% (-4.48% 🔻)

Test suite run success

1135 tests passing in 64 suites.

Report generated by 🧪jest coverage report action from b1593a3

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant