Skip to content

Commit

Permalink
refactor: wrap cli action in a global try catch clause
Browse files Browse the repository at this point in the history
  • Loading branch information
zmalatrax committed Jun 12, 2024
1 parent 673c17e commit 65f51df
Showing 1 changed file with 60 additions and 72 deletions.
132 changes: 60 additions & 72 deletions src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,89 +55,77 @@ program
.option('--print-relocated-memory', 'print the relocated memory')
.option('--print-output', 'print the output segment')
.action(async (path, options) => {
const {
relocate,
offset,
exportMemory,
exportTrace,
printOutput,
printMemory,
printRelocatedMemory,
printTrace,
} = options;
try {
const {
relocate,
offset,
exportMemory,
exportTrace,
printOutput,
printMemory,
printRelocatedMemory,
printTrace,
} = options;

if (
(!relocate && !!offset) ||
(!relocate && exportMemory) ||
(!relocate && printRelocatedMemory)
) {
consola.error(
"option '--no-relocate' cannot be used with options '--offset <OFFSET>', '--export-memory <MEMORY_FILENAME>' or '--print-relocated-memory'"
);
process.exit(1);
}
if (
(!relocate && !!offset) ||
(!relocate && exportMemory) ||
(!relocate && printRelocatedMemory)
) {
consola.log(
"option '--no-relocate' cannot be used with options '--offset <OFFSET>', '--export-memory <MEMORY_FILENAME>' or '--print-relocated-memory'"
);
process.exit(1);
}

consola.info(`Cairo VM TS ${VERSION_CLI} - Execution Mode`);
consola.info(`Cairo VM TS ${VERSION_CLI} - Execution Mode`);

const program = parseProgram(fs.readFileSync(String(path), 'utf-8'));
const runner = new CairoRunner(program);
const config: RunOptions = { relocate: relocate, offset: offset };
try {
const program = parseProgram(fs.readFileSync(String(path), 'utf-8'));
const runner = new CairoRunner(program);
const config: RunOptions = { relocate: relocate, offset: offset };
runner.run(config);
} catch (err) {
consola.fail('Execution failed!');
throw err;
}
consola.success('Execution finished!');
consola.success('Execution finished!');

if (exportMemory) {
consola.info('Exporting memory...');
try {
if (exportMemory) {
consola.info('Exporting memory...');
runner.exportMemory(exportMemory, offset);
} catch (err) {
consola.fail(
`Failed to export memory to ${exportMemory} with offset ${offset}`
);
consola.error(err);
process.exit(1);
consola.success(`Memory exported to ${exportMemory}`);
}
consola.success(`Memory exported to ${exportMemory}`);
}
if (exportTrace) {
consola.info('Exporting trace...');
try {
if (exportTrace) {
consola.info('Exporting trace...');
runner.exportTrace(exportTrace);
} catch (err) {
consola.fail(`Failed to export trace to ${exportTrace}`);
throw err;
consola.success(`Trace exported to ${exportTrace}`);
}
consola.success(`Trace exported to ${exportTrace}`);
}

if (printMemory) consola.log(runner.vm.memory.toString());
if (printRelocatedMemory) consola.log(runner.vm.relocatedMemoryToString());
if (printTrace)
consola.log(
'\nTRACE:',
runner.vm.trace
.map((entry: TraceEntry, index: number) =>
[
`\nSTEP: ${index}`,
`pc: ${entry.pc.toString()}`,
`ap: ${entry.ap.toString()}`,
`fp: ${entry.fp.toString()}\n`,
].join('\n')
)
.join('\n')
);
if (printOutput) {
const output = runner.getOutput();
if (output.length) {
consola.log('Program output: ');
output.forEach((value) => consola.log(value.toString()));
} else {
consola.log('Output segment is empty');
if (printMemory) consola.log(runner.vm.memory.toString());
if (printRelocatedMemory)
consola.log(runner.vm.relocatedMemoryToString());
if (printTrace)
consola.log(
'\nTRACE:',
runner.vm.trace
.map((entry: TraceEntry, index: number) =>
[
`\nSTEP: ${index}`,
`pc: ${entry.pc.toString()}`,
`ap: ${entry.ap.toString()}`,
`fp: ${entry.fp.toString()}\n`,
].join('\n')
)
.join('\n')
);
if (printOutput) {
const output = runner.getOutput();
if (output.length) {
consola.log('Program output: ');
output.forEach((value) => consola.log(value.toString()));
} else {
consola.log('Output segment is empty');
}
}
} catch (err) {
consola.fail(`Execution failed`);
throw err;
}
});

Expand Down

0 comments on commit 65f51df

Please sign in to comment.