Skip to content

Commit

Permalink
refactor: update CairoRunner for compilation artifacts
Browse files Browse the repository at this point in the history
  • Loading branch information
zmalatrax committed Jul 12, 2024
1 parent 21707c9 commit 4c2c395
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 10 deletions.
5 changes: 4 additions & 1 deletion src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,11 @@ program
return path;
})
)
.option(
'--cairo <FUNCTION>',
'Run a Cairo program, executing the given function'
)
.option('-s, --silent', 'silent all logs')
.option('--cairo', 'Run a Cairo program')
.option('--no-relocate', 'do not relocate memory')
.addOption(
new Option(
Expand Down
6 changes: 6 additions & 0 deletions src/errors/cairoRunner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,9 @@ class CairoRunnerError extends Error {}
export class EmptyRelocatedMemory extends CairoRunnerError {}

export class CairoZeroHintsNotSupported extends CairoRunnerError {}

export class UndefinedEntrypoint extends CairoRunnerError {
constructor(name: string) {
super(`The function to be executed doesn't exist: ${name}`);
}
}
14 changes: 10 additions & 4 deletions src/runners/cairoRunner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import * as fs from 'fs';
import {
CairoZeroHintsNotSupported,
EmptyRelocatedMemory,
UndefinedEntrypoint,
} from 'errors/cairoRunner';

import { Felt } from 'primitives/felt';
Expand Down Expand Up @@ -74,12 +75,17 @@ export class CairoRunner {
return new CairoRunner(program, program.data, mainOffset, builtins);
}

static fromCairoProgram(program: CairoProgram): CairoRunner {
static fromCairoProgram(
program: CairoProgram,
fn_name: string = 'main'
): CairoRunner {
const fn = program.entry_points_by_function[fn_name];
if (!fn) throw new UndefinedEntrypoint(fn_name);
return new CairoRunner(
program,
program.bytecode,
program.entrypoint,
program.builtins,
fn.offset,
fn.builtins,
program.hints
);
}
Expand Down Expand Up @@ -143,7 +149,7 @@ export class CairoRunner {
}

getOutput() {
const builtins = this.program.builtins;
const builtins = (this.program as CairoZeroProgram).builtins;
const outputIdx = builtins.findIndex((name) => name === 'output');
return outputIdx >= 0 ? this.vm.memory.segments[outputIdx + 2] : [];
}
Expand Down
2 changes: 1 addition & 1 deletion src/scripts/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export const run = (

if (cairo) {
const program = parseCairoProgram(file);
runner = CairoRunner.fromCairoProgram(program);
runner = CairoRunner.fromCairoProgram(program, cairo);
} else {
const program = parseCairoZeroProgram(file);
runner = CairoRunner.fromCairoZeroProgram(program);
Expand Down
5 changes: 3 additions & 2 deletions src/vm/program.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,9 @@ describe('program', () => {
const program = parseCairoProgram(programContent);
expect(program.bytecode).toEqual(bytecode);
expect(program.hints).toEqual(hints);
expect(program.entrypoint).toEqual(programJson.entrypoint);
expect(program.builtins).toEqual(programJson.builtins);
expect(program.entry_points_by_function).toEqual(
programJson.entry_points_by_function
);
});
});
});
22 changes: 20 additions & 2 deletions src/vm/program.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ export type Identifier = z.infer<typeof identifier>;
const programBase = z.object({
prime: z.string(),
compiler_version: z.string(),
builtins: z.array(z.string()),
});

const cairoZeroProgram = programBase.extend({
Expand All @@ -58,12 +57,31 @@ const cairoZeroProgram = programBase.extend({
reference_manager: referenceManager,
});

const cairoArg = z.object({
generic_id: z.string(),
size: z.number(),
debug_name: z.string(),
});

const cairoOutputArg = cairoArg.extend({
panic_inner_type: cairoArg.optional(),
});

const entrypoint = z.object({
offset: z.number(),
builtins: z.array(z.string()),
input_args: z.array(cairoArg),
return_arg: z.array(cairoOutputArg),
});

export type Entrypoint = z.infer<typeof entrypoint>;

const cairoProgram = programBase.extend({
bytecode: z
.array(z.string())
.transform((value) => value.map((v) => new Felt(BigInt(v)))),
hints,
entrypoint: z.number(),
entry_points_by_function: z.record(z.string(), entrypoint),
});

export type CairoZeroProgram = z.infer<typeof cairoZeroProgram>;
Expand Down

0 comments on commit 4c2c395

Please sign in to comment.