Skip to content
This repository has been archived by the owner on Feb 26, 2024. It is now read-only.

Type opcodes table as const in @truffle/code-utils #5557

Merged
merged 2 commits into from
Sep 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions packages/code-utils/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import parseOpcode from "./opcodes";
import type { Instruction, OpcodeTable, DisassemblyOptions } from "./types";
export type { Instruction, OpcodeTable, DisassemblyOptions };
import { parseOpcode, OpcodeTable } from "./opcodes";
export type { OpcodeTable };
import type { Instruction, DisassemblyOptions } from "./types";
export type { Instruction, DisassemblyOptions };
import * as cbor from "cbor";

/**
Expand Down
13 changes: 7 additions & 6 deletions packages/code-utils/src/opcodes.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import type { OpcodeTable } from "./types";

const codes: OpcodeTable = {
const codes = {
0x00: "STOP",
0x01: "ADD",
0x02: "MUL",
Expand Down Expand Up @@ -162,8 +160,11 @@ const codes: OpcodeTable = {
0xf5: "CREATE2",
0xfa: "STATICCALL",
0xfd: "REVERT",
//(we can omit 0xfe INVALID)
0xfe: "INVALID",
0xff: "SELFDESTRUCT"
};
} as const;

export type OpcodeTable = typeof codes;

export = (op: number) => (codes[op] ? codes[op] : "INVALID");
export const parseOpcode = (op: number) =>
op in codes ? codes[op as keyof OpcodeTable] : "INVALID";
4 changes: 0 additions & 4 deletions packages/code-utils/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,6 @@ export interface Instruction {
pushData?: string;
}

export interface OpcodeTable {
[hex: number]: string;
}

export interface DisassemblyOptions {
maxInstructionCount?: number;
attemptStripMetadata?: boolean;
Expand Down
4 changes: 2 additions & 2 deletions packages/code-utils/test/opcodes.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import opcodes from "../src/opcodes";
import { parseOpcode } from "../src/opcodes";
import assert from "assert";
import { describe, it } from "mocha";

describe("opcode parsing method", function () {
it(`returns "INVALID" when passed an invalid opcode`, function () {
const returnValue = opcodes(0xbad);
const returnValue = parseOpcode(0xbad);
assert.strictEqual("INVALID", returnValue);
});
});