class.h: move ROM method table types from internal.h

The ROM table types (mrb_mt_entry, mrb_mt_tbl) and macros
(MRB_MT_ENTRY, MRB_MT_ROM_TAB, etc.) are used by 34 files
across core and gems -- they are part of the public method
registration API, not internal implementation details.
Move them to class.h where the rest of the method table API
lives, eliminating the #ifdef MRUBY_CLASS_H guard that was
needed in internal.h.

Co-authored-by: Claude <noreply@anthropic.com>
This commit is contained in:
Yukihiro "Matz" Matsumoto
2026-02-20 17:11:49 +09:00
parent ea938c531f
commit 10f36a0cbb
2 changed files with 48 additions and 48 deletions
+48
View File
@@ -100,6 +100,54 @@ void mrb_mc_clear_by_class(mrb_state *mrb, struct RClass* c);
typedef int (mrb_mt_foreach_func)(mrb_state*,mrb_sym,mrb_method_t,void*);
MRB_API void mrb_mt_foreach(mrb_state*, struct RClass*, mrb_mt_foreach_func*, void*);
/* ROM method table types for static method registration */
union mrb_mt_ptr {
const struct RProc *proc;
mrb_func_t func;
};
/* entry combining function pointer, symbol key, and flags */
typedef struct mrb_mt_entry {
union mrb_mt_ptr val;
mrb_sym key; /* pure symbol ID (no flags packed) */
uint32_t flags; /* method flags + aspec */
} mrb_mt_entry;
typedef struct mrb_mt_tbl {
int size;
int alloc; /* bit 30: MRB_MT_READONLY_BIT, bit 29: MRB_MT_FROZEN_BIT */
mrb_mt_entry *ptr;
struct mrb_mt_tbl *next;
} mrb_mt_tbl;
#define MRB_MT_READONLY_BIT (1 << 30)
#define MRB_MT_FROZEN_BIT (1 << 29)
#define MRB_MT_FUNC (1 << 24) /* MRB_METHOD_FUNC_FL */
#define MRB_MT_PUBLIC 0
#define MRB_MT_PRIVATE (1 << 25) /* MRB_METHOD_PRIVATE_FL */
/* ROM table entry: 3rd param is MRB_ARGS_*() optionally OR'd with MRB_MT_PRIVATE. */
#define MRB_MT_ENTRY(fn, sym, flags) \
{ { .func = (fn) }, (sym), (flags) | MRB_MT_FUNC }
#define MRB_MT_ASPEC(flags) ((mrb_aspec)((flags) & 0xffffff))
/* ROM table initializer from const entries array (auto-computes size).
Casts away const because mrb_mt_tbl.ptr is shared with mutable layers;
the MRB_MT_READONLY_BIT prevents writes. */
#define MRB_MT_ROM_TAB(entries) { \
(int)(sizeof(entries)/sizeof(entries[0])), \
(int)(sizeof(entries)/sizeof(entries[0])) | MRB_MT_READONLY_BIT, \
(mrb_mt_entry*)(entries), NULL }
/* "removed" tombstone: MRB_MT_FUNC flag set with NULL function pointer.
This combination never occurs naturally (C functions are never NULL).
Unlike undef (proc=NULL without MRB_MT_FUNC), a removed marker makes
mt_get() return 0 ("not found"), blocking ROM chain walk while
allowing superclass lookup. */
#define MRB_MT_REMOVED_P(e) (((e).flags&MRB_MT_FUNC) && (e).val.func==NULL)
void mrb_mt_init_rom(struct RClass *c, mrb_mt_tbl *rom);
MRB_END_DECL
#endif /* MRUBY_CLASS_H */
-48
View File
@@ -27,54 +27,6 @@ mrb_value mrb_mod_const_missing(mrb_state *mrb, mrb_value mod);
mrb_value mrb_const_missing(mrb_state *mrb, mrb_value mod, mrb_sym sym);
size_t mrb_class_mt_memsize(mrb_state*, struct RClass*);
mrb_value mrb_obj_extend(mrb_state*, mrb_value obj);
/* ROM method table types for static method registration */
union mrb_mt_ptr {
const struct RProc *proc;
mrb_func_t func;
};
/* entry combining function pointer, symbol key, and flags */
typedef struct mrb_mt_entry {
union mrb_mt_ptr val;
mrb_sym key; /* pure symbol ID (no flags packed) */
uint32_t flags; /* method flags + aspec */
} mrb_mt_entry;
typedef struct mrb_mt_tbl {
int size;
int alloc; /* bit 30: MRB_MT_READONLY_BIT, bit 29: MRB_MT_FROZEN_BIT */
mrb_mt_entry *ptr;
struct mrb_mt_tbl *next;
} mrb_mt_tbl;
#define MRB_MT_READONLY_BIT (1 << 30)
#define MRB_MT_FROZEN_BIT (1 << 29)
#define MRB_MT_FUNC (1 << 24) /* MRB_METHOD_FUNC_FL */
#define MRB_MT_PUBLIC 0
#define MRB_MT_PRIVATE (1 << 25) /* MRB_METHOD_PRIVATE_FL */
/* ROM table entry: 3rd param is MRB_ARGS_*() optionally OR'd with MRB_MT_PRIVATE. */
#define MRB_MT_ENTRY(fn, sym, flags) \
{ { .func = (fn) }, (sym), (flags) | MRB_MT_FUNC }
#define MRB_MT_ASPEC(flags) ((mrb_aspec)((flags) & 0xffffff))
/* ROM table initializer from const entries array (auto-computes size).
Casts away const because mrb_mt_tbl.ptr is shared with mutable layers;
the MRB_MT_READONLY_BIT prevents writes. */
#define MRB_MT_ROM_TAB(entries) { \
(int)(sizeof(entries)/sizeof(entries[0])), \
(int)(sizeof(entries)/sizeof(entries[0])) | MRB_MT_READONLY_BIT, \
(mrb_mt_entry*)(entries), NULL }
/* "removed" tombstone: MRB_MT_FUNC flag set with NULL function pointer.
This combination never occurs naturally (C functions are never NULL).
Unlike undef (proc=NULL without MRB_MT_FUNC), a removed marker makes
mt_get() return 0 ("not found"), blocking ROM chain walk while
allowing superclass lookup. */
#define MRB_MT_REMOVED_P(e) (((e).flags&MRB_MT_FUNC) && (e).val.func==NULL)
void mrb_mt_init_rom(struct RClass *c, mrb_mt_tbl *rom);
#endif
mrb_value mrb_obj_equal_m(mrb_state *mrb, mrb_value);