Skip to content

Commit d766779

Browse files
sunnylqmclaude
andcommitted
refactor(hermes): one width-suffix fold and one function-header test
The pretty (hermes-base) and raw (hermes-raw) dump readers each had their own operand-width suffix fold and function-header regex; hermes-raw now exports both. The two line splitters stay separate: only the raw one strips CR, and unifying them would change the pretty pass on CRLF output, which no test or CI job exercises. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent b668781 commit d766779

2 files changed

Lines changed: 21 additions & 18 deletions

File tree

‎src/utils/hermes-base.ts‎

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,12 @@ import {
3737
LiteralResolver,
3838
literalBuffersFromBytes,
3939
} from './hermes-literals';
40-
import { auditRawHermesBytecode, hermesSemanticData } from './hermes-raw';
40+
import {
41+
auditRawHermesBytecode,
42+
foldWidthSuffix,
43+
hermesSemanticData,
44+
isFunctionHeader,
45+
} from './hermes-raw';
4146
import { hermesTimeout } from './hermes-timeout';
4247
import { t } from './i18n';
4348
import { webFetch } from './runtime';
@@ -1252,15 +1257,6 @@ function isLetter(code: number): boolean {
12521257
}
12531258

12541259
/** strip one operand-width suffix (LongIndex, Long, Short) off an opcode */
1255-
function foldWidthSuffix(opcode: string): string {
1256-
for (const suffix of ['LongIndex', 'Long', 'Short']) {
1257-
if (opcode.length > suffix.length && opcode.endsWith(suffix)) {
1258-
return opcode.slice(0, -suffix.length);
1259-
}
1260-
}
1261-
return opcode;
1262-
}
1263-
12641260
/** Split a readable stream into lines without readline (Bun's readline async iterator misbehaves when pulled manually). */
12651261
async function* streamLines(
12661262
stream: NodeJS.ReadableStream,
@@ -1334,10 +1330,6 @@ const DUMP_STDERR_KEEP = 4 * 1024;
13341330
/** a diagnostic line is cut to this many characters */
13351331
const DETAIL_LINE_MAX = 120;
13361332

1337-
function isFunctionHeader(line: string): boolean {
1338-
return /^(?:Function|NCFunction|Constructor)</.test(line);
1339-
}
1340-
13411333
/**
13421334
* Structured reader over one `hermesc -dump-bytecode -pretty-disassemble`
13431335
* stream. Everything before the functions is either the string table (kept

‎src/utils/hermes-raw.ts‎

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -275,8 +275,19 @@ interface RawInstruction {
275275
}
276276

277277
/** Remove encoding-width suffixes; operand values remain part of the audit. */
278-
function foldWidth(opcode: string): string {
279-
return opcode.replace(/(?:LongIndex|Long|Short)$/, '');
278+
/** The opcode without its operand-width suffix (`GetByIdLong` → `GetById`). */
279+
export function foldWidthSuffix(opcode: string): string {
280+
for (const suffix of ['LongIndex', 'Long', 'Short']) {
281+
if (opcode.length > suffix.length && opcode.endsWith(suffix)) {
282+
return opcode.slice(0, -suffix.length);
283+
}
284+
}
285+
return opcode;
286+
}
287+
288+
/** The first line of a function in either dump. */
289+
export function isFunctionHeader(line: string): boolean {
290+
return /^(?:Function|NCFunction|Constructor)</.test(line);
280291
}
281292

282293
/**
@@ -382,7 +393,7 @@ export function normalizeRawHermesFunction(
382393
const literal = new LiteralResolver(buffers, data.strings);
383394
const output = [fn.metadata];
384395
for (const inst of instructions) {
385-
const op = foldWidth(inst.opcode);
396+
const op = foldWidthSuffix(inst.opcode);
386397
const values = inst.operands.map((o) => o.value);
387398
const refs = STRING_OPERANDS[op] ?? [];
388399
const operands: unknown[] = inst.operands.map((operand, i) => {
@@ -568,7 +579,7 @@ export async function auditRawHermesBytecode(
568579
const normalize = (body: string[]) =>
569580
normalizeRawHermesFunction(body, data[side], index++, buffers[side]);
570581
for await (const line of linesOf(children[side].stdout!)) {
571-
if (/^(?:Function|NCFunction|Constructor)</.test(line)) {
582+
if (isFunctionHeader(line)) {
572583
if (lines) yield normalize(lines);
573584
lines = [];
574585
} else if (

0 commit comments

Comments
 (0)