Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add opcode enumerating helpers using macros #346

Merged
merged 2 commits into from
Jun 6, 2023

Conversation

xxuejie
Copy link
Collaborator

@xxuejie xxuejie commented Jun 5, 2023

Previously, we have to manually maintain the opcodes so the list of opcode definitions is in sync with the array of
INSTRUCTION_OPCODE_NAMES, which is a tedious step. In addition, it is also quite easier to forget one opcode or two in the implementations somewhere. This change leverages some purposely designed macros to grant us a way to enumerate all the macros, providing accessing code for each of them individually. This way we can define opcodes in one place and make sure every enumeration contains all the opcodes correctly.

Note that using macros does have the drawbacks of making code less clearer to read. We are also providing a one-line command to generate the underlying Rust code. What's more one can also refer to Rust docs to see all the defined opcodes

Previously, we have to manually maintain the opcodes so the list of
opcode definitions is in sync with the array of
`INSTRUCTION_OPCODE_NAMES`, which is a tedious step. In addition, it
is also quite easier to forget one opcode or two in the
implementations somewhere. This change leverages some purposely
designed macros to grant us a way to enumerate all the macros,
providing accessing code for each of them individually. This way we
can define opcodes in one place and make sure every enumeration
contains all the opcodes correctly.

Note that using macros does have the drawbacks of making code less
clearer to read. We are also providing a one-line command to generate
the underlying Rust code. What's more one can also refer to Rust docs
to see all the defined opcodes
@xxuejie xxuejie requested a review from mohanson June 5, 2023 07:31
@xxuejie xxuejie merged commit e445627 into nervosnetwork:develop Jun 6, 2023
11 checks passed
@xxuejie xxuejie deleted the opcode-helpers-using-macros branch June 6, 2023 01:23
mohanson pushed a commit to libraries/ckb-vm that referenced this pull request Jul 21, 2023
* feat: Add opcode enumerating helpers using macros

Previously, we have to manually maintain the opcodes so the list of
opcode definitions is in sync with the array of
`INSTRUCTION_OPCODE_NAMES`, which is a tedious step. In addition, it
is also quite easier to forget one opcode or two in the
implementations somewhere. This change leverages some purposely
designed macros to grant us a way to enumerate all the macros,
providing accessing code for each of them individually. This way we
can define opcodes in one place and make sure every enumeration
contains all the opcodes correctly.

Note that using macros does have the drawbacks of making code less
clearer to read. We are also providing a one-line command to generate
the underlying Rust code. What's more one can also refer to Rust docs
to see all the defined opcodes

* ci: Format
mohanson pushed a commit that referenced this pull request Jul 21, 2023
* feat: Add opcode enumerating helpers using macros

Previously, we have to manually maintain the opcodes so the list of
opcode definitions is in sync with the array of
`INSTRUCTION_OPCODE_NAMES`, which is a tedious step. In addition, it
is also quite easier to forget one opcode or two in the
implementations somewhere. This change leverages some purposely
designed macros to grant us a way to enumerate all the macros,
providing accessing code for each of them individually. This way we
can define opcodes in one place and make sure every enumeration
contains all the opcodes correctly.

Note that using macros does have the drawbacks of making code less
clearer to read. We are also providing a one-line command to generate
the underlying Rust code. What's more one can also refer to Rust docs
to see all the defined opcodes

* ci: Format
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants