Skip to content

Commit 3304e24

Browse files
committed
weird abi mappings for compatibility between new noir and old artifacts
1 parent 5f846e2 commit 3304e24

1 file changed

Lines changed: 50 additions & 2 deletions

File tree

  • yarn-project/stdlib/src/abi

yarn-project/stdlib/src/abi/abi.ts

Lines changed: 50 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,16 @@ export const FunctionDebugMetadataSchema = z.object({
224224
acir_locations: z.record(z.number()),
225225
brillig_locations: z.record(z.record(z.number())),
226226
}),
227-
files: z.record(z.object({ source: z.string(), path: z.string() })),
227+
files: z.preprocess(
228+
fillMissingFunctionLocations,
229+
z.record(
230+
z.object({
231+
source: z.string(),
232+
path: z.string(),
233+
function_locations: z.array(z.object({ start: z.number(), name: z.string() })),
234+
}),
235+
),
236+
) as z.ZodType<DebugFileMap>,
228237
}) satisfies z.ZodType<FunctionDebugMetadata>;
229238

230239
/** The artifact entry of a function. */
@@ -305,6 +314,14 @@ export interface ProgramDebugInfo {
305314
debug_infos: Array<DebugInfo>;
306315
}
307316

317+
/** The range a function occupies in a file. */
318+
export type FunctionLocation = {
319+
/** The byte where the function starts. */
320+
start: number;
321+
/** The name of the function. */
322+
name: string;
323+
};
324+
308325
/** Maps a file ID to its metadata for debugging purposes. */
309326
export type DebugFileMap = Record<
310327
FileId,
@@ -313,9 +330,30 @@ export type DebugFileMap = Record<
313330
source: string;
314331
/** The path of the file. */
315332
path: string;
333+
/** The range each function occupies in the file. */
334+
function_locations: FunctionLocation[];
316335
}
317336
>;
318337

338+
/**
339+
* Fills missing `function_locations` on each entry of a file map with an empty array.
340+
* Kept for backwards compatibility with artifacts compiled before `function_locations` was introduced.
341+
*/
342+
function fillMissingFunctionLocations(val: unknown): unknown {
343+
if (val && typeof val === 'object') {
344+
for (const entry of Object.values(val as Record<string, unknown>)) {
345+
if (
346+
entry &&
347+
typeof entry === 'object' &&
348+
(entry as { function_locations?: unknown }).function_locations === undefined
349+
) {
350+
(entry as { function_locations: FunctionLocation[] }).function_locations = [];
351+
}
352+
}
353+
}
354+
return val;
355+
}
356+
319357
/** Type representing a field layout in the storage of a contract. */
320358
export type FieldLayout = {
321359
/** Slot in which the field is stored. */
@@ -367,7 +405,17 @@ export const ContractArtifactSchema = zodFor<ContractArtifact>()(
367405
globals: z.record(z.array(AbiValueSchema)),
368406
}),
369407
storageLayout: z.record(z.object({ slot: schemas.Fr })),
370-
fileMap: z.record(z.coerce.number(), z.object({ source: z.string(), path: z.string() })),
408+
fileMap: z.preprocess(
409+
fillMissingFunctionLocations,
410+
z.record(
411+
z.coerce.number(),
412+
z.object({
413+
source: z.string(),
414+
path: z.string(),
415+
function_locations: z.array(z.object({ start: z.number(), name: z.string() })),
416+
}),
417+
),
418+
),
371419
}),
372420
);
373421

0 commit comments

Comments
 (0)