Skip to content

Commit

Permalink
Parte 5
Browse files Browse the repository at this point in the history
  • Loading branch information
eugenioclrc committed Jun 23, 2023
1 parent 197f6ed commit dfeec4c
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 4 deletions.
16 changes: 16 additions & 0 deletions src/execution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,29 @@ export class ExecutionContext{
pc: number;
stopped: boolean;
returndata: bigint[] = [];
validjumpdest: Set<number> = new Set([]);

constructor(code: string = "", pc: number = 0, stack: Stack = new Stack(), memory: Memory = new Memory()) {
this.code = code;
this.stack = stack;
this.memory = memory;
this.pc = pc;
this.stopped = false;
this.initJumpDest(code);
}

initJumpDest(code: string) {
let i = 0;
while (i < code.length) {
const opcode = parseInt(code.slice(i, i + 2), 16);
if (opcode == 0x5b) {
this.validjumpdest.add(i / 2);
} else if(opcode == 0x60) {
// @audit recordar preparar todo para skipear push2 ... push32, preguntarle a adri
i += 2;
}
i += 2;
}
}

stop() {
Expand Down
17 changes: 16 additions & 1 deletion src/instructions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,24 @@ registerInstruction(0x58, "PC", ((ctx: ExecutionContext) => {
}));

registerInstruction(0x56, "JUMP", ((ctx: ExecutionContext) => {
ctx.pc = Number(ctx.stack.pop());
const jumpdest = Number(ctx.stack.pop());
if (!ctx.validjumpdest.has(jumpdest)) {
throw new Error("InvalidJumpDest");
}

ctx.pc = jumpdest;
}));

registerInstruction(0x5b, "JUMPDEST", ((ctx: ExecutionContext) => {
// esto es para marcar saltos validos
}));

registerInstruction(0x80, "DUP1", ((ctx: ExecutionContext) => {
ctx.stack.push(ctx.stack.peek(0));
}));


// registerInstruction(, "JUMPDEST", ((ctx: ExecutionContext) => {

/*
const INSTRUCTIONS = {
Expand Down
22 changes: 20 additions & 2 deletions src/main_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,28 @@ describe("ExecutionContextTest", () => {
});

it("should work JUMP", () => {
const context = run("60ff56");
expect(context.pc).to.be.equal(0xff);
expect(() => run("60ff56")).to.throw("InvalidJumpDest");
});

it("should work JUMP", () => {
const context = run("60055660015b");
expect(context.stack.length()).to.be.equal(0);
});
})

describe("DUPs", () => {
it("should work DUP1", () => {
const context = run("600280");
expect(context.stack.peek(0)).to.be.equal(0x02n);
expect(context.stack.peek(1)).to.be.equal(0x02n);
});
});

describe("DUPs", () => {
it("should work DUP1", () => {
const context = run("5b60025b605b");
expect(context.validjumpdest).to.be.deep.equal(new Set([0, 3]));
});
});

});
8 changes: 8 additions & 0 deletions src/stack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,14 @@ export class Stack {

}

peek(item: number) {
if (item < 0 || item >= this.stack.length) {
throw new Error('InvalidStackItem');
}

return this.stack[item];
}

length() {
return this.stack.length;
}
Expand Down
19 changes: 18 additions & 1 deletion src/stack_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,5 +65,22 @@ describe("StackUnitTest", () => {
expect(actual).to.be.equal(item);
});
});


describe("Peek", () => {
let stack: Stack;
let item = 1n;
beforeEach(() => {
stack = new Stack();
stack.push(item);
});

it("should peek the item in the stack", () => {
const actual = stack.peek(0);
expect(actual).to.be.equal(item);
});

it("should revert if the item is not in the stack", () => {
expect(() => stack.peek(1)).to.throw('InvalidStackItem');
});
});
});

0 comments on commit dfeec4c

Please sign in to comment.