Skip to content

Commit

Permalink
test: add test for error case
Browse files Browse the repository at this point in the history
  • Loading branch information
Maddiaa0 committed Jan 18, 2024
1 parent f9cc6ea commit 0aa72d1
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { AvmStateManager } from '../avm_state_manager.js';
import { Add, Mul, Sub } from './arithmetic.js';
import { And, Not, Or, Shl, Shr, Xor } from './bitwise.js';
import { Eq, Lt, Lte } from './comparators.js';
import { InternalCall, InternalReturn, Jump } from './control_flow.js';
import { InternalCall, InternalCallStackEmptyError, InternalReturn, Jump } from './control_flow.js';
import { CalldataCopy, Cast, Mov, Set } from './memory.js';

describe('Control Flow Opcodes', () => {
Expand Down Expand Up @@ -42,6 +42,11 @@ describe('Control Flow Opcodes', () => {
expect(machineState.pc).toBe(1);
});

it("Should error if Internal Return is called without a corresponding Internal Call", () => {
const returnInstruction = new InternalReturn();
expect(() => returnInstruction.execute(machineState, stateManager)).toThrow(InternalCallStackEmptyError);
});

it('Should increment PC on All other Instructions', () => {
const instructions = [
new Add(0, 1, 2),
Expand Down
3 changes: 0 additions & 3 deletions yarn-project/acir-simulator/src/avm/opcodes/control_flow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ export class Jump extends Instruction {
}

execute(machineState: AvmMachineState, _stateManager: AvmStateManager): void {
// TODO: this jump offset must be range constrained.
machineState.pc = this.jumpOffset;
}
}
Expand All @@ -42,7 +41,6 @@ export class InternalCall extends Instruction {
}

execute(machineState: AvmMachineState, _stateManager: AvmStateManager): void {
// TODO: this jump offset must be range constrained.
machineState.internalCallStack.push(machineState.pc + 1);
machineState.pc = this.jumpOffset;
}
Expand All @@ -60,7 +58,6 @@ export class InternalReturn extends Instruction {
execute(machineState: AvmMachineState, _stateManager: AvmStateManager): void {
const jumpOffset = machineState.internalCallStack.pop();
if (jumpOffset === undefined) {
// TODO: Add in an error if this pop is undefined
throw new InternalCallStackEmptyError();
}
machineState.pc = jumpOffset;
Expand Down

0 comments on commit 0aa72d1

Please sign in to comment.