class.c, string.c: ROM method table for String class

Move String's 46 method definitions from runtime
mrb_define_method_id() calls to a static ROM method table
sorted at init time. mrb_mt_init_rom() sorts the parallel
vals/keys arrays by presym ID and sets the readonly flag.

Expose mt_tbl and related types in internal.h so ROM tables
can be defined in individual source files.

When MRB_NO_PRESYM is defined, falls back to traditional
runtime method registration.

Co-authored-by: Claude <noreply@anthropic.com>
This commit is contained in:
Yukihiro "Matz" Matsumoto
2026-02-18 16:12:56 +09:00
parent 1e0f9b76c3
commit 96da40605f
3 changed files with 216 additions and 60 deletions
+23
View File
@@ -27,6 +27,29 @@ 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 mt_ptr {
const struct RProc *proc;
mrb_func_t func;
};
typedef struct mt_tbl {
int size;
int alloc; /* bit 30: MT_READONLY_BIT */
union mt_ptr *ptr;
struct mt_tbl *next;
} mt_tbl;
#define MT_READONLY_BIT (1 << 30)
#define MT_KEY_SHIFT 4
#define MT_KEY(sym, flags) ((sym)<<MT_KEY_SHIFT|(flags))
#define MT_FUNC 8 /* MRB_METHOD_FUNC_FL */
#define MT_NOARG 4 /* MRB_METHOD_NOARG_FL */
#define MT_PUBLIC 0 /* MRB_METHOD_PUBLIC_FL */
#define MT_PRIVATE 1 /* MRB_METHOD_PRIVATE_FL */
void mrb_mt_init_rom(struct RClass *c, mt_tbl *rom);
#endif
mrb_value mrb_obj_equal_m(mrb_state *mrb, mrb_value);