From 0a9b73da05ef07360de3c945f9a7ecf57c3ef0d9 Mon Sep 17 00:00:00 2001 From: fcarreiro Date: Wed, 7 Feb 2024 09:57:40 +0000 Subject: [PATCH] chore(avm): add SET serialize comments and simplify signature --- .../simulator/src/avm/opcodes/instruction.ts | 17 ++++++++++------- .../simulator/src/avm/opcodes/memory.ts | 7 +++---- 2 files changed, 13 insertions(+), 11 deletions(-) diff --git a/yarn-project/simulator/src/avm/opcodes/instruction.ts b/yarn-project/simulator/src/avm/opcodes/instruction.ts index 3f4cf27f7de7..f968ad88d2b9 100644 --- a/yarn-project/simulator/src/avm/opcodes/instruction.ts +++ b/yarn-project/simulator/src/avm/opcodes/instruction.ts @@ -1,4 +1,4 @@ -import { assert } from 'console'; +import { strict as assert } from 'assert'; import type { AvmContext } from '../avm_context.js'; import { BufferCursor } from '../serialization/buffer_cursor.js'; @@ -39,7 +39,7 @@ export abstract class Instruction { * @returns The serialized instruction. */ public serialize(this: any): Buffer { - assert(this instanceof Instruction); + assert(!!this.constructor.wireFormat, 'wireFormat must be defined on the class'); return serialize(this.constructor.wireFormat, this); } @@ -50,12 +50,15 @@ export abstract class Instruction { * @param buf Buffer to read from. * @returns Constructed instance of Class. */ - public static deserialize; wireFormat: OperandType[] }>( - this: T, - buf: BufferCursor | Buffer, - ): InstanceType { + public static deserialize(this: InstructionConstructor, buf: BufferCursor | Buffer): Instruction { + assert(!!this.wireFormat, 'wireFormat must be defined on the instruction class'); const res = deserialize(buf, this.wireFormat); - const args = res.slice(1) as ConstructorParameters; // Remove opcode. + const args = res.slice(1); // Remove opcode. return new this(...args); } } + +type InstructionConstructor = { + new (...args: any[]): Instruction; + wireFormat?: OperandType[]; +}; diff --git a/yarn-project/simulator/src/avm/opcodes/memory.ts b/yarn-project/simulator/src/avm/opcodes/memory.ts index 06e7b6153321..6f181e52f96f 100644 --- a/yarn-project/simulator/src/avm/opcodes/memory.ts +++ b/yarn-project/simulator/src/avm/opcodes/memory.ts @@ -43,6 +43,7 @@ export class Set extends Instruction { super(); } + /** We need to use a custom serialize function because of the variable length of the value. */ public serialize(): Buffer { const format: OperandType[] = [ ...Set.wireFormatBeforeConst, @@ -52,10 +53,8 @@ export class Set extends Instruction { return serialize(format, this); } - public static deserialize }>( - this: T, - buf: BufferCursor | Buffer, - ): InstanceType { + /** We need to use a custom deserialize function because of the variable length of the value. */ + public static deserialize(this: typeof Set, buf: BufferCursor | Buffer): Set { if (buf instanceof Buffer) { buf = new BufferCursor(buf); }