Conversation
There was a problem hiding this comment.
Code Review
This pull request refactors the switch statement code generation in src/compiler/code-generator.ts by reserving 4-byte placeholders for branch targets and patching them later with full 4-byte signed big-endian offsets. A critical issue was identified in the non-table switch (LOOKUPSWITCH) patching logic, where using caseLabels[k] to retrieve the target label can lead to index misalignment and incorrect branch targets or out-of-bounds errors. It is recommended to use caseLabelMap to look up the correct label for each case value instead.
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.
| for (let k = 0; k < caseValues.length; k++) { | ||
| const idx = caseLabelIndex[1 + k] | ||
| const off = caseLabels[k].offset - positionOffset | ||
| cg.code[idx] = (off >> 24) & 0xff | ||
| cg.code[idx + 1] = (off >> 16) & 0xff | ||
| cg.code[idx + 2] = (off >> 8) & 0xff | ||
| cg.code[idx + 3] = off & 0xff | ||
| } |
There was a problem hiding this comment.
In a LOOKUPSWITCH (non-table switch), using caseLabels[k] to retrieve the target label is incorrect. caseLabels contains one label per case group (including the default label), whereas caseValues contains one entry per individual case value (excluding the default label). If a case group has multiple labels, or if there is a default label, the indices of caseValues and caseLabels will not align, leading to incorrect branch targets or out-of-bounds errors.
Instead, you should use caseLabelMap to look up the correct label for each case value.
for (let k = 0; k < caseValues.length; k++) {
const idx = caseLabelIndex[1 + k]
const lbl = caseLabelMap.get(caseValues[k]) || defaultLabel
const off = lbl.offset - positionOffset
cg.code[idx] = (off >> 24) & 0xff
cg.code[idx + 1] = (off >> 16) & 0xff
cg.code[idx + 2] = (off >> 8) & 0xff
cg.code[idx + 3] = off & 0xff
}
Coverage report
Show files with reduced coverage 🔻
Test suite run success1134 tests passing in 64 suites. Report generated by 🧪jest coverage report action from 1b8e9a5 |
Fixes #93