[Tarantool-patches] [PATCH luajit 2/4] dbg: avoid hardcoded enums
Sergey Kaplun
skaplun at tarantool.org
Fri Nov 20 17:13:11 MSK 2026
Hi, Mikhail!
Thanks for the update!
LGTM, with minor nits below.
On 13.11.26, Mikhail Elhimov wrote:
> Besides reducing lines of code this way the extension becomes compatible
> with various versions of LuaJIT because different versions might use
> different sets of enum members (a newer version might introduce
> additional BC/IR/etc.)
>
> Prior to this patch, the string was used as a debugger-agnostic way to
> specify type, but this way might not work in the case of an enum because
> debugging information might be optimized out if no variable of such an
> enum type is declared and its members are used only as predefined
> constants. The type of enum is needed to be able to get human-readable
> value (member name instead of number). The alternative way to get enum
> type is to get it from the value, i.e. somehow obtain the value that is
> of enum type and then use this type to convert the numbers to the member
> names.
>
> Creating the value of the given enum member is quite different in GDB
> and LLDB, so separate method `create_enum_value` was introduced in
> Debugger API. Also new method `cast_typeof` was introduced to perform
> value-based cast, i.e. it casts to the same type as a reference value.
>
> Also this patch fixes the mapping of FPMATHOP to a human-readable form,
> because prior to this patch, IRFPMS contained the incorrect entry
> 'exp2'. IRFPMS is a human-readable form of enum IRFPMathOp, but there is
> no 'exp2' enum member actually (see IRFPMDEF). It looks like it was
> added by mistake initially (corresponding tests to check mapping of
> FPMATHOP were added).
>
> Resolves tarantool/tarantool#13094
> Resolves tarantool/tarantool#13159
> ---
> src/luajit_dbg.py | 675 +++++-------------
> .../debug-extension-tests.py | 62 +-
> 2 files changed, 249 insertions(+), 488 deletions(-)
>
> diff --git a/src/luajit_dbg.py b/src/luajit_dbg.py
> index 77abf8c6..ceba4c58 100644
> --- a/src/luajit_dbg.py
> +++ b/src/luajit_dbg.py
> @@ -106,6 +106,8 @@ class Debugger(object):
> def initialize_extension(self, commands):
> if self.configure():
> for name, command in commands.items():
> + if hasattr(command, 'pre_register'):
> + command.pre_register()
Please add the comment that this is needed for generated documentation
for function's help.
> self.register_command(command, name)
> self.write('{} command initialized\n'.format(name))
> self.write('LuaJIT debug extension is successfully loaded\n')
> @@ -119,6 +121,13 @@ class Debugger(object):
<snipped>
> @@ -829,6 +916,55 @@ def strx64(val):
> return re.sub('L?$', '', hex(int(tou64(val))))
>
>
> +class EnumBasedList(object):
> + def __init__(self, max_enum_member, enum_name, map_func=None,
> + *map_func_args):
> + self.__max_enum_member = max_enum_member
> + self.__enum_name = enum_name
> + self.__map_func = map_func
> + self.__map_func_args = map_func_args
> + # Lazy initialization (see __get_items) as the required
> + # information might be unavailable at this moment.
> + self.__items = None
Nit: Please sort these items alphabetically.
Nit: Please sort functions below alphabetically.
> +
> + def __iter__(self):
> + return iter(self.__get_items())
> +
> + def __getitem__(self, key):
> + return self.__get_items()[key]
> +
> + def __len__(self):
> + return len(self.__get_items())
> +
> + def __get_items(self):
> + if self.__items is None:
> + try:
> + max_enum_value = dbg.create_enum_value(
> + self.__max_enum_member, self.__enum_name
> + )
> + except Exception as e:
> + msg = 'failed to resolve {} enum members using {}'.format(
> + self.__enum_name or 'anonymous',
> + self.__max_enum_member,
> + )
> + raise dbg.error('{}: {}'.format(msg, e))
> + items = []
> + for i in range(max_enum_value):
> + item = str(dbg.cast_typeof(max_enum_value, i))
> + if self.__map_func:
> + item = self.__map_func(item, *self.__map_func_args)
> + items.append(item)
> + self.__items = items
> + return self.__items
> +
> + def index(self, item):
> + return self.__get_items().index(item)
Why do not use __index__ method for this?
Since `IRTYPES['num']` is more readable than `IRTYPES.index('num')`, IMO.
Since we assume that idx == value for these lists.
Also, it is worth mentinoning that these generated lists skips some enums
that are defined equal to others like there is no: IRT_PGC, IRT_PTR, and
also skips all enums that are greater than border __MAX value (IRT_MARK,
etc) (I suppose there is no need in this functionality right now, just
worth mentioning).
<snipped>
> --
> 2.55.0
>
--
Best regards,
Sergey Kaplun
More information about the Tarantool-patches
mailing list