mirror of
https://github.com/mruby/mruby
synced 2026-06-08 16:11:16 +00:00
class.c: merge conditional methods into ROM tables
Move conditional mrb_define_method_id() calls into ROM entry arrays using #ifdef guards. With linear search, sizeof in MRB_MT_ROM_TAB() adjusts automatically after preprocessing. Cross-class ROM tables (methods a gem defines on a class it does not own) are reverted to mrb_define_method_id(). Multiple gems should not add ROM table layers to the same class; each layer costs a 16-byte mrb_mt_tbl struct in RAM and deepens the lookup chain. Use mrb_define_method_id() for cross-class methods. Co-authored-by: Claude <noreply@anthropic.com>
This commit is contained in:
+4
-6
@@ -694,6 +694,10 @@ static const mrb_mt_entry kernel_rom_entries[] = {
|
||||
MRB_MT_ENTRY(mrb_ensure_int_type, MRB_SYM(__to_int), MRB_MT_NOARG),
|
||||
MRB_MT_ENTRY(mrb_false, MRB_SYM_Q(respond_to_missing), MRB_MT_PRIVATE),
|
||||
MRB_MT_ENTRY(mrb_obj_method_recursive_p, MRB_SYM_Q(__method_recursive), 0),
|
||||
#ifndef HAVE_MRUBY_IO_GEM
|
||||
MRB_MT_ENTRY(mrb_p_m, MRB_SYM(p), MRB_MT_PRIVATE),
|
||||
MRB_MT_ENTRY(mrb_print_m, MRB_SYM(print), MRB_MT_PRIVATE),
|
||||
#endif
|
||||
};
|
||||
static mrb_mt_tbl kernel_rom_mt = MRB_MT_ROM_TAB(kernel_rom_entries);
|
||||
|
||||
@@ -711,11 +715,5 @@ mrb_init_kernel(mrb_state *mrb)
|
||||
|
||||
mrb_mt_init_rom(krn, &kernel_rom_mt);
|
||||
|
||||
/* conditional methods not in ROM table */
|
||||
#ifndef HAVE_MRUBY_IO_GEM
|
||||
mrb_define_private_method_id(mrb, krn, MRB_SYM(p), mrb_p_m, MRB_ARGS_ANY()); /* 15.3.1.3.34 */
|
||||
mrb_define_private_method_id(mrb, krn, MRB_SYM(print), mrb_print_m, MRB_ARGS_ANY()); /* 15.3.1.3.35 */
|
||||
#endif
|
||||
|
||||
mrb_include_module(mrb, mrb->object_class, mrb->kernel_module);
|
||||
}
|
||||
|
||||
+7
-7
@@ -2303,6 +2303,9 @@ static const mrb_mt_entry numeric_rom_entries[] = {
|
||||
MRB_MT_ENTRY(num_finite_p, MRB_SYM_Q(finite), MRB_MT_NOARG),
|
||||
MRB_MT_ENTRY(num_infinite_p, MRB_SYM_Q(infinite), MRB_MT_NOARG),
|
||||
MRB_MT_ENTRY(num_eql, MRB_SYM_Q(eql), 0),
|
||||
#ifndef MRB_NO_FLOAT
|
||||
MRB_MT_ENTRY(num_fdiv, MRB_SYM(fdiv), 0),
|
||||
#endif
|
||||
};
|
||||
static mrb_mt_tbl numeric_rom_mt = MRB_MT_ROM_TAB(numeric_rom_entries);
|
||||
|
||||
@@ -2338,6 +2341,10 @@ static const mrb_mt_entry integer_rom_entries[] = {
|
||||
MRB_MT_ENTRY(int_to_s, MRB_SYM(inspect), 0),
|
||||
MRB_MT_ENTRY(int_divmod, MRB_SYM(divmod), 0),
|
||||
MRB_MT_ENTRY(coerce_step_counter, MRB_SYM(__coerce_step_counter), 0),
|
||||
#ifndef MRB_NO_FLOAT
|
||||
MRB_MT_ENTRY(int_fdiv, MRB_SYM(fdiv), 0),
|
||||
MRB_MT_ENTRY(int_to_f, MRB_SYM(to_f), MRB_MT_NOARG),
|
||||
#endif
|
||||
};
|
||||
static mrb_mt_tbl integer_rom_mt = MRB_MT_ROM_TAB(integer_rom_entries);
|
||||
|
||||
@@ -2387,9 +2394,6 @@ mrb_init_numeric(mrb_state *mrb)
|
||||
/* Numeric Class */
|
||||
numeric = mrb_define_class_id(mrb, MRB_SYM(Numeric), mrb->object_class); /* 15.2.7 */
|
||||
mrb_mt_init_rom(numeric, &numeric_rom_mt);
|
||||
#ifndef MRB_NO_FLOAT
|
||||
mrb_define_method_id(mrb, numeric, MRB_SYM(fdiv), num_fdiv, MRB_ARGS_REQ(1));
|
||||
#endif
|
||||
|
||||
/* Integer Class */
|
||||
mrb->integer_class = integer = mrb_define_class_id(mrb, MRB_SYM(Integer), numeric); /* 15.2.8 */
|
||||
@@ -2397,10 +2401,6 @@ mrb_init_numeric(mrb_state *mrb)
|
||||
MRB_UNDEF_ALLOCATOR(integer);
|
||||
mrb_undef_class_method_id(mrb, integer, MRB_SYM(new));
|
||||
mrb_mt_init_rom(integer, &integer_rom_mt);
|
||||
#ifndef MRB_NO_FLOAT
|
||||
mrb_define_method_id(mrb, integer, MRB_SYM(fdiv), int_fdiv, MRB_ARGS_REQ(1));
|
||||
mrb_define_method_id(mrb, integer, MRB_SYM(to_f), int_to_f, MRB_ARGS_NONE()); /* 15.2.8.3.23 */
|
||||
#endif
|
||||
|
||||
/* Fixnum Class for compatibility */
|
||||
mrb_define_const_id(mrb, mrb->object_class, MRB_SYM(Fixnum), mrb_obj_value(integer));
|
||||
|
||||
+3
-4
@@ -3555,6 +3555,9 @@ static const mrb_mt_entry string_rom_entries[] = {
|
||||
MRB_MT_ENTRY(mrb_str_byteslice, MRB_SYM(byteslice), 0),
|
||||
MRB_MT_ENTRY(mrb_str_bytesplice, MRB_SYM(bytesplice), 0),
|
||||
MRB_MT_ENTRY(sub_replace, MRB_SYM(__sub_replace), 0),
|
||||
#ifndef MRB_NO_FLOAT
|
||||
MRB_MT_ENTRY(mrb_str_to_f, MRB_SYM(to_f), MRB_MT_NOARG),
|
||||
#endif
|
||||
};
|
||||
static mrb_mt_tbl string_rom_mt = MRB_MT_ROM_TAB(string_rom_entries);
|
||||
|
||||
@@ -3571,9 +3574,5 @@ mrb_init_string(mrb_state *mrb)
|
||||
|
||||
mrb_mt_init_rom(s, &string_rom_mt);
|
||||
|
||||
/* conditional methods not in ROM table */
|
||||
#ifndef MRB_NO_FLOAT
|
||||
mrb_define_method_id(mrb, s, MRB_SYM(to_f), mrb_str_to_f, MRB_ARGS_NONE()); /* 15.2.10.5.38 */
|
||||
#endif
|
||||
mrb_define_method_id(mrb, mrb->kernel_module, MRB_SYM(__ENCODING__), mrb_encoding, MRB_ARGS_NONE());
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user