Skip to content

Fix switch statement issue#97

Open
kjw142857 wants to merge 1 commit into
mainfrom
switch
Open

Fix switch statement issue#97
kjw142857 wants to merge 1 commit into
mainfrom
switch

Conversation

@kjw142857

Copy link
Copy Markdown
Contributor

Fixes #93

@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 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.

Comment on lines +1547 to +1554
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
}

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

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
        }

@github-actions

Copy link
Copy Markdown

Coverage report

St.
Category Percentage Covered / Total
🟡 Statements
72.27% (-0.23% 🔻)
7408/10250
🔴 Branches
59.15% (-0.03% 🔻)
2480/4193
🟡 Functions 69.04% 1320/1912
🟡 Lines
73.13% (-0.24% 🔻)
6974/9536
Show files with reduced coverage 🔻
St.
File Statements Branches Functions Lines
🟡
... / code-generator.ts
65.74% (-2.53% 🔻)
60.7% (-0.33% 🔻)
65.43%
66.55% (-2.56% 🔻)

Test suite run success

1134 tests passing in 64 suites.

Report generated by 🧪jest coverage report action from 1b8e9a5

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.

Fix switching logic for JVM

1 participant