diff --git a/src/utils/hermes-base.ts b/src/utils/hermes-base.ts index c782121..836807a 100644 --- a/src/utils/hermes-base.ts +++ b/src/utils/hermes-base.ts @@ -650,9 +650,9 @@ export function normalizeDisassemblyLine( // UIntSwitchImm rX, , , , // Folding only the first shape let a shifted UIntSwitchImm offset read as a // real difference and drop an otherwise good delta build. - m = /^(\s*(?:String|UInt)?SwitchImm r\d+, \d+, )\d+(, .*)$/.exec(line); + m = /^(\s*StringSwitchImm r\d+, \d+, )\d+(, L\d+, \d+)$/.exec(line); if (m) line = `${m[1]}${m[2]}`; - m = /^(\s*(?:String|UInt)?SwitchImm r\d+, )\d+(, L\d+, .*)$/.exec(line); + m = /^(\s*UIntSwitchImm r\d+, )\d+(, L\d+, \d+, \d+)$/.exec(line); if (m) line = `${m[1]}${m[2]}`; if (/^\s*offset \d+$/.test(line)) line = line.replace(/\d+$/, ''); return line; diff --git a/tests/hermes-switch-normalization.test.ts b/tests/hermes-switch-normalization.test.ts new file mode 100644 index 0000000..7d688bc --- /dev/null +++ b/tests/hermes-switch-normalization.test.ts @@ -0,0 +1,48 @@ +import { describe, expect, test } from 'bun:test'; +import { normalizeDisassemblyLine } from '../src/utils/hermes-base'; + +const normalize = (line: string) => + normalizeDisassemblyLine(line, new Map()); + +describe('Hermes switch jump-table normalization', () => { + test('normalizes only the StringSwitchImm jump-table offset', () => { + const baseline = normalize(' StringSwitchImm r13, 2, 4024, L146, 150'); + expect(baseline).toBe(' StringSwitchImm r13, 2, , L146, 150'); + expect(normalize(' StringSwitchImm r13, 2, 4025, L146, 150')).toBe( + baseline, + ); + expect(normalize(' StringSwitchImm r13, 3, 4024, L146, 150')).not.toBe( + baseline, + ); + expect(normalize(' StringSwitchImm r13, 2, 4024, L147, 150')).not.toBe( + baseline, + ); + expect(normalize(' StringSwitchImm r13, 2, 4024, L146, 151')).not.toBe( + baseline, + ); + }); + + test('normalizes only the UIntSwitchImm jump-table offset', () => { + const baseline = normalize(' UIntSwitchImm r40, 5937, L3, 0, 31'); + expect(baseline).toBe(' UIntSwitchImm r40, , L3, 0, 31'); + expect(normalize(' UIntSwitchImm r40, 5938, L3, 0, 31')).toBe(baseline); + expect(normalize(' UIntSwitchImm r40, 5937, L4, 0, 31')).not.toBe( + baseline, + ); + expect(normalize(' UIntSwitchImm r40, 5937, L3, 1, 31')).not.toBe( + baseline, + ); + expect(normalize(' UIntSwitchImm r40, 5937, L3, 0, 32')).not.toBe( + baseline, + ); + }); + + test('does not fold unsupported or malformed switch shapes', () => { + expect(normalize(' SwitchImm r1, 2, 3, L4, 5')).toBe( + ' SwitchImm r1, 2, 3, L4, 5', + ); + expect(normalize(' UIntSwitchImm r40, 5937, 3, 0, 31')).toBe( + ' UIntSwitchImm r40, 5937, 3, 0, 31', + ); + }); +});