41 Commits

Author SHA1 Message Date
Yukihiro "Matz" Matsumoto 8956c5abb5 mruby.h: include mruby/presym.h for all source files
Since presym is now mandatory, mruby.h includes presym.h so that
MRB_SYM() macros are available everywhere without explicit include.
Remove redundant #include <mruby/presym.h> from all source files.

Co-authored-by: Claude <noreply@anthropic.com>
2026-03-09 16:50:58 +09:00
Yukihiro "Matz" Matsumoto 71cb3c2e3a class.c: allocate ROM table wrappers per mrb_state
ROM method tables used static mrb_mt_tbl variables shared
across the process. The next pointer in each wrapper was
mutated by mrb_mt_init_rom(), causing cross-state
contamination when multiple mrb_state instances existed.

Allocate mrb_mt_tbl wrappers per-state via mrb_malloc().
The const mrb_mt_entry[] arrays remain static and shared.
Wrappers are tracked in mrb->rom_mt and freed at mrb_close().

Remove MRB_MT_ROM_TAB macro; add MRB_MT_INIT_ROM macro that
auto-computes size and calls the new mrb_mt_init_rom().

Co-authored-by: Claude <noreply@anthropic.com>
2026-02-20 22:24:31 +09:00
Yukihiro "Matz" Matsumoto 483c155a41 class.c: store aspec in ROM method table entries
Restore MRB_ARGS_* argument specs and ISO section comments to all
709 ROM method table entries. The aspec is encoded in bits 4-27 of
the flags field; MRB_MT_NOARG is now auto-derived from aspec==0.

Add MRB_MT_ENTRY_PRIVATE() macro for private methods (53 entries)
and MRB_MT_ASPEC() accessor for extracting aspec from flags.

Co-authored-by: Claude <noreply@anthropic.com>
2026-02-20 11:44:28 +09:00
Yukihiro "Matz" Matsumoto 8adba34bd9 class.c: auto-set MRB_MT_FUNC in MRB_MT_ENTRY macro
Since ROM table entries are always C functions, have the
MRB_MT_ENTRY() macro set MRB_MT_FUNC automatically. This
simplifies entry definitions across all 32 source files.

Co-authored-by: Claude <noreply@anthropic.com>
2026-02-20 10:37:06 +09:00
Yukihiro "Matz" Matsumoto 0fab703028 class.c: use linear search for method tables; make ROM entries const
Replace binary search with linear scan in mt_get(), mt_put(),
mt_del(), mt_chain_has(), and mrb_mt_foreach(). The method cache
makes repeated lookups O(1), so linear scan on cache misses is
acceptable.

This removes the sorting requirement, allowing ROM entry arrays
to be declared const. On embedded systems, const static data
resides in flash/ROM instead of RAM, saving ~8.4KB for ~700
method entries on 32-bit MCUs.

Co-authored-by: Claude <noreply@anthropic.com>
2026-02-20 08:26:05 +09:00
Yukihiro "Matz" Matsumoto bde2202100 class.c: refactor ROM method tables to array-of-structs layout
Replace the parallel-arrays (struct-of-arrays) ROM method table
layout with an array-of-structs layout where each mrb_mt_entry
bundles its function pointer and symbol key together.

New MRB_MT_ENTRY() and MRB_MT_ROM_TAB() macros simplify ROM table
definitions from a 3-part pattern (SIZE define + anonymous struct +
mrb_mt_tbl) to a 2-part pattern (entries array + mrb_mt_tbl).

Internal mt_* functions in class.c are simplified: single memmove/
memcpy operations replace paired key+value operations.

Co-authored-by: Claude <noreply@anthropic.com>
2026-02-19 23:59:30 +09:00
Yukihiro "Matz" Matsumoto 0ed26f8352 class.c: rename mt_/MT_ to mrb_mt_/MRB_MT_ for non-static identifiers
Follow mruby's naming convention: non-static types, macros, and
functions use the mrb_/MRB_ prefix. Renamed:
- union mt_ptr -> union mrb_mt_ptr
- mt_tbl -> mrb_mt_tbl
- MT_KEY(), MT_FUNC, MT_NOARG, MT_PUBLIC, MT_PRIVATE -> MRB_MT_*
- MT_KEY_SHIFT, MT_READONLY_BIT, MT_REMOVED_P -> MRB_MT_*
- mt_init_rom() -> mrb_mt_init_rom()
File-local static functions and macros in class.c are unchanged.

Co-authored-by: Claude <noreply@anthropic.com>
2026-02-19 15:22:55 +09:00
Yukihiro "Matz" Matsumoto 9fbc11c6d0 mrbgems: remove MRB_NO_PRESYM guards from core extension gems
Co-authored-by: Claude <noreply@anthropic.com>
2026-02-19 12:17:34 +09:00
Yukihiro "Matz" Matsumoto 51d0bfa1fc mruby-class-ext: ROM method tables for Module/Class extensions (11 methods)
Co-authored-by: Claude <noreply@anthropic.com>
2026-02-18 16:41:18 +09:00
Yukihiro "Matz" Matsumoto 4c1b2a59bd mruby-class-ext: fix crash in module comparison with invalid types
Co-authored-by: Claude <noreply@anthropic.com>
2025-10-14 08:22:41 +09:00
Yukihiro "Matz" Matsumoto 2c1a7ad933 mruby-class-ext: add comprehensive documentation to all functions
Add detailed comments to all functions in class.c including:

- Function purpose and behavior descriptions
- Parameter documentation with types and meanings
- Return value explanations with all possible outcomes
- Error conditions and exception documentation
- Helper function and structure documentation

This improves code maintainability and follows Ruby documentation
conventions with proper call-seq formatting.

Co-authored-by: Atlassian Rovo Dev
2025-07-05 18:32:46 +09:00
Yukihiro "Matz" Matsumoto 815b34c509 mruby-class-ext: migrate comparison methods to c
The following Module methods were migrated from Ruby to C:

   - `<`
   - `<=`
   - `>`
   - `>=`
   - `<=>`

Co-authored-by: Gemini <gemini@google.com>
2025-07-05 18:32:46 +09:00
Yukihiro "Matz" Matsumoto 5cc5a8a9c3 mruby-class-ext: add README.md
The document is written by Google Jules.
2025-06-07 11:48:40 +09:00
Yukihiro "Matz" Matsumoto 8276143f03 object.h: remove MRB_SET_FROZEN_FLAG/MRB_UNSET_FROZEN_FLAG macros
Simple `o->frozen = 1/0` now works.
2025-01-07 13:56:30 +09:00
dearblue a196d8a5f3 Fixed also argument forwarding in class_exec
Pointed out by @matz.
https://github.com/mruby/mruby/pull/6391#issuecomment-2433662139
2024-10-24 22:05:59 +09:00
Yukihiro "Matz" Matsumoto 574f9f0d39 mruby-class_ext: use presym for initialization 2024-06-14 00:37:05 +09:00
Yukihiro "Matz" Matsumoto 10bc15c23e mruby-class-ext: adjust variable declarations 2024-05-25 16:41:24 +09:00
Yukihiro "Matz" Matsumoto 87b358a342 Including header files in include/* by <> 2024-03-26 13:59:59 +09:00
Yukihiro "Matz" Matsumoto 2810491587 mruby-class-ext/class.c: remove mrb_ prefix from static functions 2023-04-19 00:12:28 +09:00
Yukihiro "Matz" Matsumoto 990d41f9f7 mruby-class-ext: new method attached_object introduced in Ruby3.2 2022-12-09 17:30:17 +09:00
Yukihiro "Matz" Matsumoto 0a0ab73e8d mruby-class-ext: #subclasses should be a method of Class, not Module. 2022-09-27 23:03:23 +09:00
Yukihiro "Matz" Matsumoto f0af96c130 mruby-class-ext: add Class#subclasses method. 2022-02-01 22:37:49 +09:00
Yukihiro "Matz" Matsumoto 7df31d9451 mruby.h: remove acc from callinfo; add cci instead.
`acc` was used as an index of the receiver (if positive), or a flag for
methods implemented in C. We replace `regs[ci->acc]` by `ci[1].stack[0]`.
And renamed `acc` (originally meant accumulator position) to `cci`
(means callinfo for C implemented method).
2021-09-10 12:39:51 +09:00
dearblue 58e9442737 Unified target_class and env of mrb_callinfo
If there is `env`, `env->c` means `target_class`.
2021-01-10 13:23:28 +09:00
Yukihiro "Matz" Matsumoto d3a02a297f Fix instance_exec and class_exec to avoid crash on indirect calls.
Thank you @shuujii to additional report on #4973
2020-04-22 16:48:33 +09:00
KOBAYASHI Shuji a367373fe3 Revert "Implement Ruby2.7's frozen strings from Symbol#to_s"
This feature was reverted from Ruby 2.7.
2019-11-16 19:47:31 +09:00
Yukihiro "Matz" Matsumoto 7624e23426 Revert part of #4758 to reduce number of tests.
More tests, more time.
2019-10-09 10:29:04 +09:00
KOBAYASHI Shuji fb103c1319 Implement Ruby2.7's frozen strings from Module#name 2019-10-08 17:34:13 +09:00
KOBAYASHI Shuji feaf80d899 Use type predicate macros instead of mrb_type if possible
For efficiency with `MRB_WORD_BOXING` (implement type predicate macros for
all `enum mrb_vtype`).
2019-09-26 22:23:27 +09:00
dearblue 099a4f3969 Entrust "no block given" error to mrb_get_args()
Some error messages will be changed.
2019-09-16 20:29:58 +09:00
KOBAYASHI Shuji faec8331b7 Remove unneeded mrb_str_dup() in Module#name
`mrb_class_path()` always returns a new string or `nil`.
2019-06-17 21:46:30 +09:00
Yukihiro "Matz" Matsumoto e6bad6766a Add new methods Module#{>,>=,<=>}; ref #4174 2018-12-04 08:41:09 +09:00
Yukihiro "Matz" Matsumoto d5d9cc8b62 Remove unnecessary check in Module#<; ref #4174 2018-12-04 08:40:23 +09:00
Yukihiro "Matz" Matsumoto 6ef4a5fd6b Replace RDoc <i></i> to Markdown back quotes; ref #4174 2018-12-04 08:39:44 +09:00
Rob e300ac8e3a Adds Module#< and Module#<= 2018-12-02 20:56:47 -05:00
Yukihiro "Matz" Matsumoto e471d37ca5 Separate meta-programming features to mruby-metaprog gem.
We assume meta-programming is less used in embedded environments.
We have moved following methods:

 * Kernel module
   global_variables, local_variables, singleton_class,
   instance_variables, instance_variables_defined?, instance_variable_get,
   instance_variable_set, methods, private_methods, public_methods,
   protected_methods, singleton_methods, define_singleton_methods

 * Module class
   class_variables, class_variables_defined?, class_variable_get,
   class_variable_set, remove_class_variable, included_modules,
   instance_methods, remove_method, method_removed, constants

 * Module class methods
   constants, nesting

Note:
Following meta-programming methods are kept in the core:

 * Module class
   alias_method, undef_method, ancestors, const_defined?, const_get,
   const_set, remove_const, method_defined?, define_method

 * Toplevel object
   define_method

`mruby-metaprog` gem is linked by default (specified in default.gembox).
When it is removed, it will save 40KB (stripped:8KB) on x86-64
environment last time I measured.
2018-08-30 22:30:36 +09:00
Yukihiro "Matz" Matsumoto 8d7e95282c Add #module_exec and #class_exec in mruby-class-ext gem. 2017-12-23 10:01:30 +09:00
Yukihiro "Matz" Matsumoto ca2cfc6fe9 Merge pull request #3746 from christopheraue/mod_singleton_class_p
Implemented Module#singleton_class?
2017-07-19 09:08:11 +09:00
Christopher Aue 468cc34c93 Fixed Module#to_s and #name for #const_set modules 2017-07-18 22:55:37 +02:00
Christopher Aue dd52b88101 implemented Module#singleton_class? 2017-07-18 20:47:05 +02:00
Yukihiro "Matz" Matsumoto 01dddaf385 rename mruby-module-ext to mruby-class-ext; ref #2470 2016-11-13 16:17:44 +09:00