70 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 4a097525df proc.h: unify method flag layout; eliminate aspec shifting
Move MRB_METHOD_FUNC_FL to bit 24 and visibility flags to
bits 25-26 so that MRB_ARGS_*() values (bits 0-23) can be
stored directly without shifting. This makes MRB_MT_PRIVATE
and MRB_METHOD_PRIVATE_FL the same value, eliminating the
dual-constant confusion and simplifying the MRB_MT_ENTRY()
macro to a single OR operation.

Co-authored-by: Claude <noreply@anthropic.com>
2026-02-20 13:49:17 +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 52c71f5b99 mrbgems: remove MRB_NO_PRESYM guards from additional gems
Co-authored-by: Claude <noreply@anthropic.com>
2026-02-19 12:17:43 +09:00
Yukihiro "Matz" Matsumoto 08e800e43e mruby-metaprog: ROM method tables for metaprogramming extensions
Co-authored-by: Claude <noreply@anthropic.com>
2026-02-19 09:39:26 +09:00
Yukihiro "Matz" Matsumoto 76188b46ef khash: remove unused mrb parameter from KHASH_FOREACH macro
Remove the unused mrb_state parameter from KHASH_FOREACH macro to clean
up the API. The parameter was never used in the macro implementation and
only cluttered the call sites.

Changes:
- Update KHASH_FOREACH macro signature: (name, mrb, kh, k) -> (name, kh, k)
- Update documentation and usage examples in khash.h
- Update KSET_FOREACH wrapper macro in mruby-set
- Update 2 direct call sites in mruby-metaprog
- All mruby-set call sites automatically updated via wrapper macro

This is a breaking change but follows the recent API cleanup where we
already modified KHASH_FOREACH signature. The macro now has a cleaner
interface without the unused parameter.

Co-authored-by: Claude <noreply@anthropic.com>
2025-08-14 10:53:03 +09:00
Yukihiro "Matz" Matsumoto 2371b52ab4 khash: optimize structure size by 50% with single data pointer
BREAKING CHANGE: khash field access macros now require type name parameter

Replace individual pointer fields (keys, vals, ed_flags) with single data
pointer and address calculation functions. This reduces khash structure
size from 32 to 16 bytes (50% reduction) while maintaining performance
through pointer caching in hot paths.

Structure changes:
- Single void *data field replaces keys/vals/ed_flags pointers
- Address calculation functions compute array locations on demand
- Hot path functions cache calculated pointers for performance

API changes (BREAKING):
- kh_key(h, x)      -> kh_key(typename, h, x)
- kh_val(h, x)      -> kh_val(typename, h, x)
- kh_exist(h, x)    -> kh_exist(typename, h, x)
- kh_value(h, x)    -> kh_value(typename, h, x)
- KHASH_FOREACH()   -> KHASH_FOREACH(typename, ...)

Migration required:
- mruby-metaprog: 4 call sites updated (familiar macro names, just add type parameter)
- mruby-array-ext: no changes needed (uses function-style API)
- External users: add type name as first parameter to field access macros

Benefits:
- 50% memory reduction per hash table (32 -> 16 bytes)
- 464 bytes total memory savings in mrbtest execution
- Better cache locality with smaller structures
- Optimized hot path performance with pointer caching
- Consistent with mruby memory-first design priority

Co-authored-by: Claude <noreply@anthropic.com>
2025-08-14 10:53:02 +09:00
Yukihiro "Matz" Matsumoto 07b803e28a docs: replace xml-style markup with markdown in comments
Replace XML-style markup tags in comments with markdown equivalents:
- <code>...</code> to `...` (inline code)
- <tt>...</tt> to `...` (teletype/monospace)
- <i>...</i> to *...* (italics/emphasis)
- +...+ to `...` (parameter/variable references)

Updated 80+ files across core source, headers, mrbgems, and libraries
to use consistent markdown formatting in documentation comments.
Handled edge cases including special characters like <=> operators.

Co-authored-by: Atlassian Rovo Dev
2025-08-14 10:52:49 +09:00
Yukihiro "Matz" Matsumoto e30ec3da42 mruby-metaprog: use KHASH_FOREACH macro 2025-06-23 17:07:28 +09:00
Yukihiro "Matz" Matsumoto a6ef3a45d1 vm.c (mrb_f_public_send): define #public_send method
The method itself is available when mruby-metaprog is loaded.
2025-04-25 17:39:12 +09:00
Yukihiro "Matz" Matsumoto 95f9de4877 mruby-metaprog (mrb_mod_s_constants): Module.constants implemented
The method returns the list of all constants accessible from the point
of call.
2025-04-14 22:09:35 +09:00
Yukihiro "Matz" Matsumoto cda551ce25 mruby-metaprog (mrb_mod_s_constants): call Module#constants if possible
When the optional argument is given or the receiver is not `Module`.
2025-04-14 22:09:34 +09:00
Yukihiro "Matz" Matsumoto 76d3739a89 mruby-metaprog (mrb_obj_methods): should not retrieve obj->c directly
In some cases, `obj` may not be an object structure, but an immediate.
2025-03-10 11:40:48 +09:00
Yukihiro "Matz" Matsumoto d7a4275dd1 mruby-metaprog: make #global_variables, #local_variables private 2025-03-07 17:17:44 +09:00
Yukihiro "Matz" Matsumoto 457aaa173b mruby-metaprog (mrb_mod_remove_method): stop calling method_removed()
The hook method should be called from mrb_remove_method().
2025-03-07 17:17:37 +09:00
Yukihiro "Matz" Matsumoto 9e3e7b2cfe mruby-metaprog: add 3 new methods to retrieve instance method names
- public_instance_methods
- private_instance_methods
- protected_instance_methods
2025-03-07 17:17:34 +09:00
Yukihiro "Matz" Matsumoto 9229da10e4 mruby-metaprog: implement method list from a class (or an instance) 2025-03-07 17:17:34 +09:00
Yukihiro "Matz" Matsumoto a282f8bf2a mruby-metaprog: remove unnecessary enum 2025-03-07 17:17:34 +09:00
dearblue e476d9a344 Need to restore the GC arena after some function calls
When calling `mrb_equal()` or `mrb_funcall()` family functions, the GC arena should be restored if the loop is repeated by a non-immediate return value.

In my opinion, restoring the GC arena is unnecessary when a non-immediate (true) value causes the function to return (e.g. the `mrb_ary_index_m()` function).

The patch does not take into account the case of recursive calls and may be incomplete.
2024-09-03 21:29:06 +09:00
Yukihiro "Matz" Matsumoto d1e49f7121 mruby-metaprog: use presym for initialization 2024-06-14 01:43:57 +09:00
Yukihiro "Matz" Matsumoto 87b358a342 Including header files in include/* by <> 2024-03-26 13:59:59 +09:00
Yukihiro "Matz" Matsumoto 81e8617d02 mruby-metaprog: explicitly include khash.h which was included from proc.h 2024-03-26 13:59:59 +09:00
Yukihiro "Matz" Matsumoto 9ee7d955e3 mruby-metaprog: narrow the scope of a local variable 2024-01-04 11:18:14 +09:00
Yukihiro "Matz" Matsumoto d6d434eb3f Inline loop variables everywhere 2023-07-13 08:05:00 +09:00
Yukihiro "Matz" Matsumoto 89f7bb1056 use more lightweight mrb_funcall_argv instead of mrb_funcall_id 2023-06-12 14:22:04 +09:00
Yukihiro "Matz" Matsumoto 8b39a7825d Reduce unnecessary mrb_obj_ptr() to mrb_check_frozen() 2023-05-31 08:29:12 +09:00
Yukihiro "Matz" Matsumoto 0d07af104b internal.h: move function prototypes to <mruby/internal.h> 2023-03-14 11:06:44 +09:00
Yukihiro "Matz" Matsumoto bf642becc2 internal.h: aggregate prototypes to internal.h.
- mrb_f_send()
- mrb_ci_nregs()
2022-11-02 14:24:42 +09:00
Yukihiro "Matz" Matsumoto b99c389ec3 internal.h: aggregate internal functions.
Internal functions can only be called from within the library.
Functions listed in `mruby/internal.h` can be called from:

* core (src/*.c)
* gems (mrbgems/**/*.c)

But not from the application linked with `libmruby`.
2022-04-02 18:25:13 +09:00
Yukihiro "Matz" Matsumoto 982a4769a1 metaprog.c: add undefined_instance_methods method. 2022-03-21 11:15:23 +09:00
dearblue 927615e1f0 Added other methods for Binding
- Added to `mruby-binding-core`
  - `Binding#local_variable_defined?`
  - `Binding#local_variable_get`
  - `Binding#local_variable_set`
  - `Binding#local_variables`
  - `Binding#receiver`
  - `Binding#source_location`
  - `Binding#inspect`
- Added to `mruby-proc-binding`
  - `Proc#binding`

The reason for separating `Proc#binding` is that core-mrbgems has a method that returns a closure object to minimize possible problems with being able to manipulate internal variables.
By separating it as different mrbgem, each user can judge this problem and incorporate it arbitrarily.
2021-02-22 23:32:43 +09:00
dearblue 792f6ac670 Adjustment of the current HEAD and bindings, and separation
Make changes to make `Binding` work.
At the same time, it separates `Binding#eval`, which depends on `mruby-eval`, from `mruby-binding-core`.
2021-02-22 23:32:18 +09:00
Yukihiro "Matz" Matsumoto 0d7f5b8216 metaprog.c: fix a methods() bug with false argument; fix #5351 2021-02-20 14:56:55 +09:00
Yukihiro "Matz" Matsumoto 8ed15c078b metaprog.c: remove unused argument from a private function. 2021-02-20 13:20:14 +09:00
Yukihiro "Matz" Matsumoto 17ecf14511 Revert "Minimize the changes in #5277"
This reverts commit dc51d89ac2.
2021-01-26 10:57:07 +09:00
Yukihiro "Matz" Matsumoto dc51d89ac2 Minimize the changes in #5277
Instead of including `mruby/presym.h` everywhere, we provided the
fallback `mruby/presym.inc` under `include/mruby` directory, and specify
`-I<build-dir>/include` before `-I<top-dir>/include` in `presym.rake`.
So even when someone drops `-I<build-dir>/include` in compiler options,
it just compiles without failure.
2021-01-22 18:38:53 +09:00
KOBAYASHI Shuji 90b53f4c29 Avoid including presym.inc in existing header files
Addressed an issue where existing programs linking `libmruby.a` could only
be built by adding `<build-dir>/include` to compiler's include path.
2021-01-11 09:21:07 +09:00
dearblue b2bfcabae2 Improved Object#define_singleton_method
Integrate the implementation with `Module#define_method`.

- Introduce the internal function `mrb_mod_define_method_m()` (no static)
- The `Object#define_singleton_method` method can now accept a second argument
2020-11-21 22:31:13 +09:00
dearblue 368f8a5da6 Don't use private structs with mrb_mt_foreach()
The `MRB_API` function `mrb_mt_foreach()` previously used the private structure `struct mt_elem`.
Therefore, use `mrb_method_t` instead.
2020-11-14 11:30:34 +09:00
Yukihiro "Matz" Matsumoto ac51a58683 ISO C does not allow extra ';' outside of a function; #5107 2020-11-02 23:04:12 +09:00
dearblue f0a64329b1 Prohibit array changes by "a"/"*" specifier of mrb_get_args()
The "a"/"*" specifier of the `mrb_get_args()` function will now return `const mrb_value *`.
This is because it is difficult for the caller to check if it is an array object and write-barrier if necessary.
And it requires calling `mrb_ary_modify()` on the unmodified array object, which is also difficult (this is similar to #5087).
2020-10-22 22:55:35 +09:00
Yukihiro "Matz" Matsumoto 397b005715 Replace the implementation of method tables in classes/modules.
They are basically the copy of instance variable tables. On my Linux
box, memory consumption of `mrbtest` measured by `valgrind` is:

- old: 17,683,830 bytes
- new: 14,283,749 bytes
2020-10-12 18:20:06 +09:00
Yukihiro "Matz" Matsumoto 3d8a38bea4 You don't need to keep index in local variables info in irep. 2020-10-12 16:21:22 +09:00
Yukihiro "Matz" Matsumoto 52507b1083 Generate C struct from irep instead of binary dump. 2020-10-12 16:21:10 +09:00
Yukihiro "Matz" Matsumoto 5a3e014e49 Constify irep members.
- `pool`
- `syms`
- `reps`
2020-10-12 16:21:03 +09:00