Skip to content

Commit

Permalink
refactor: move hintProcessor attribute to the VM
Browse files Browse the repository at this point in the history
  • Loading branch information
zmalatrax committed Jun 25, 2024
1 parent 1e56084 commit 0c90902
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 18 deletions.
24 changes: 13 additions & 11 deletions src/hints/hint.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { HintData, HintProcessor } from './hint';
import { HintReference, ValueType } from './hintReference';
import { Register } from 'vm/instruction';
import { IdsManager } from './idsManager';
import { VirtualMachine } from 'vm/virtualMachine';

const AP_TRACKING_DATA_DEFAULT: ApTrackingData = {
group: 0,
Expand All @@ -16,7 +17,6 @@ const AP_TRACKING_DATA_DEFAULT: ApTrackingData = {
describe('Hints', () => {
describe('compile', () => {
test('should correctly compile the given hint', () => {
const hintProcessor = new HintProcessor();
const hint: Hint = {
code: 'ids.a = ids.b',
flow_tracking_data: {
Expand Down Expand Up @@ -79,12 +79,11 @@ describe('Hints', () => {
code: 'ids.a = ids.b',
};

const data = hintProcessor.compile(hint, refManager);
const data = HintProcessor.compile(hint, refManager);
expect(data).toEqual(expectedData);
});

test('should throw when compiling a hint with a missing reference', () => {
const hintProcessor = new HintProcessor();
const hint: Hint = {
code: 'ids.a = ids.b',
flow_tracking_data: {
Expand All @@ -98,23 +97,26 @@ describe('Hints', () => {
};
const refManager: ReferenceManager = { references: [] };

expect(() => hintProcessor.compile(hint, refManager)).toThrow(
expect(() => HintProcessor.compile(hint, refManager)).toThrow(
new UnreachableReference(0, 0)
);
});
});

describe('execute', () => {
test('should throw UnknownHint when executing a non-existing hint', () => {
const vm = new VirtualMachine();
expect(() =>
new HintProcessor().execute({
accessible_scopes: [],
flow_tracking_data: {
ap_tracking: AP_TRACKING_DATA_DEFAULT,
reference_ids: {},
new HintProcessor().execute(
{
ids: new IdsManager(
new Map<string, HintReference>(),
AP_TRACKING_DATA_DEFAULT
),
code: 'no-code',
},
code: 'no-code',
})
vm
)
).toThrow(new UnknownHint('no-code'));
});
});
Expand Down
4 changes: 1 addition & 3 deletions src/runners/cairoRunner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ export class CairoRunner {
programBase: Relocatable;
executionBase: Relocatable;
finalPc: Relocatable;
hintProcessor: HintProcessor;

static readonly defaultRunOptions: RunOptions = {
relocate: false,
Expand All @@ -43,7 +42,6 @@ export class CairoRunner {
const mainOffset = mainId !== undefined ? mainId.pc ?? 0 : 0;

const constants = extractConstants(program);
this.hintProcessor = new HintProcessor();
const compiledHints = new CompiledHintData(
Object.entries(program.hints).map(([offset, hints]) => [
Number(offset),
Expand Down Expand Up @@ -76,7 +74,7 @@ export class CairoRunner {
*/
run(config: RunOptions = CairoRunner.defaultRunOptions): void {
while (!this.vm.pc.eq(this.finalPc)) {
this.vm.step(this.hintProcessor);
this.vm.step();
}
const { relocate, offset } = config;
if (relocate) this.vm.relocate(offset);
Expand Down
10 changes: 6 additions & 4 deletions src/vm/virtualMachine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,13 @@ export type RelocatedMemory = {

export class VirtualMachine {
private currentStep: bigint;
constants: ProgramConstants;
hints: CompiledHintData;
memory: Memory;
pc: Relocatable;
ap: Relocatable;
fp: Relocatable;
constants: ProgramConstants;
hints: CompiledHintData;
hintProcessor: HintProcessor;
trace: TraceEntry[];
relocatedMemory: RelocatedMemory[];
relocatedTrace: RelocatedTraceEntry[];
Expand All @@ -73,17 +74,18 @@ export class VirtualMachine {

this.hints = hints;
this.constants = constants;
this.hintProcessor = new HintProcessor();
}

/**
* Execute one step:
* - Decode the instruction at PC
* - Run the instruction
*/
step(hintProcessor: HintProcessor): void {
step(): void {
const hints = this.hints.get(this.pc.offset);
if (hints) {
hints.map((hint: HintData) => hintProcessor.execute(hint, this));
hints.map((hint: HintData) => this.hintProcessor.execute(hint, this));
}

const maybeEncodedInstruction = this.memory.get(this.pc);
Expand Down

0 comments on commit 0c90902

Please sign in to comment.