Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/utils/hermes-base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -650,9 +650,9 @@ export function normalizeDisassemblyLine(
// UIntSwitchImm rX, <jtOffset>, <defaultLabel>, <min>, <max>
// 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]}<jt>${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]}<jt>${m[2]}`;
if (/^\s*offset \d+$/.test(line)) line = line.replace(/\d+$/, '<jt>');
return line;
Expand Down
48 changes: 48 additions & 0 deletions tests/hermes-switch-normalization.test.ts
Original file line number Diff line number Diff line change
@@ -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<number, string>());

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, <jt>, 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, <jt>, 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',
);
});
});