Implement exception handling in java-slang#96
Conversation
There was a problem hiding this comment.
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.
| 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 } | ||
| }, |
There was a problem hiding this comment.
There are several critical correctness and code generation issues in the current implementation of TryStatement:
- Pass-by-value bug with
catchAllLabel.offset: When pushing the catch-all entry tocg.exceptionTable,catchAllLabel.offsetis0because 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 PC0instead of the actual catch-all handler offset. - Early return bug: If there are no catch clauses but a
finallyblock is present (e.g.,try { ... } finally { ... }), the code returns early and completely ignores thefinallyblock. - 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). - 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.
- 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 }
},
Coverage report
Show new covered files 🐣
Show files with reduced coverage 🔻
Test suite run success1135 tests passing in 64 suites. Report generated by 🧪jest coverage report action from b1593a3 |
Implement exception handling for the following components (in progress):