88 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
John Bampton 8558ad40ec Adjust broken license links; clean up Markdown 2025-11-29 01:46:54 +10: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 84c308aedc mruby-metaprog: add README.md
The document is written by Google Jules.
2025-06-11 18:33:35 +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 3f93254b02 mruby-metaprog: add actual test for Kernel#protected_methods 2025-03-07 17:17:47 +09:00
Yukihiro "Matz" Matsumoto 16db15d6fd mruby-metaprog: add actual test for Kernel#private_methods 2025-03-07 17:17:47 +09:00
Yukihiro "Matz" Matsumoto 2db1ad9ce3 mruby-metaprog/test: avoid potential name crash by Foo class 2025-03-07 17:17:47 +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
dearblue f1c0752ab8 Shared empty iv_tbl of module
This will be a partial merge of #5317 with the following changes.

- Remove `iclass->iv_c` since `iclass->iv_c` is equivalent to `iclass->c`.
- `class_iv_ptr()` returns a single pointer instead of a double pointer.
2024-03-27 21:39:32 +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
John Bampton ea8964ef35 ruby: standardize whitespace 2022-10-31 16:25:56 +10:00
John Bampton b049a71582 Remove unneeded trailing comma from assert_equal statement 2022-10-26 13:58:53 +10: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
Yukihiro "Matz" Matsumoto 01a21085f6 add a few regressions test from #2313
The code was contributed from Carson McDonald (@carsonmcdonald)
2021-06-03 16:47:04 +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
KOBAYASHI Shuji 98d091436d Reduce memory usage of instance variable table
## Implementation Summary

* Only keys and only values of hash table are contiguous to eliminate
  structure padding.
* Change upper limit of `iv_tbl` size to `UINT16_MAX` (it seems to be
  acceptable in mruby because the total number of classes/modules
  immediately after starting Redmine is 20,000 or less).
* `iv_tbl*` point hash buckets directly.

## Benchmark Summary

Only the results of typical situations on 64-bit Word-boxing are present
here. For more detailed information, including consideration, see below
report (although most of the body is written in Japanese).

* https://shuujii.github.io/mruby-iv-benchmark

### Memory Usage

Lower value is better.

| iv_tbl Size |    Baseline    |       New      |   Factor   |
|------------:|---------------:|---------------:|-----------:|
|           4 |            88B |            52B |   0.59091x |
|          30 |           536B |           388B |   0.72388x |
|         100 |          2072B |          1540B |   0.74324x |
|         200 |          4120B |          3076B |   0.74660x |

Although not mentioned in the above report, the memory usage of `mrbtest`
(full-core gembox) is as follows in the result by Valgrind.

* Baseline: 108,086 allocs, 16,313,122 bytes allocated
* New:       94,273 allocs, 15,875,214 bytes allocated

### Performance

Higher value is better.

#### `mrb_obj_iv_set`

| iv_tbl Size |    Baseline    |       New      |   Factor   |
|------------:|---------------:|---------------:|-----------:|
|           4 |  88.63003M i/s |  92.60611M i/s |   1.04486x |
|          30 |  32.97066M i/s |  25.25095M i/s |   0.76586x |
|         100 |  16.33224M i/s |  22.74998M i/s |   1.39295x |
|         200 |   5.64484M i/s |   6.79949M i/s |   1.20455x |

#### `mrb_obj_iv_get`

| iv_tbl Size |    Baseline    |      New       |   Factor   |
|------------:|---------------:|---------------:|-----------:|
|           4 | 217.58391M i/s | 237.59912M i/s |   1.09199x |
|          30 | 139.56195M i/s | 160.49470M i/s |   1.14999x |
|         100 | 143.09716M i/s | 190.95047M i/s |   1.33441x |
|         200 |  89.75291M i/s | 134.78717M i/s |   1.50176x |

### Binary Size

Lower value is better.

|    File     |    Baseline    |      New       |   Factor   |
|:------------|---------------:|---------------:|-----------:|
| mruby       |       697,520B |       697,520B |   1.00000x |
| libmruby.a  |     1,046,570B |     1,046,682B |   0.99989x |

## Note

The address in `struct RObject::iv` may change after initialization because
`iv_tbl*` points directly to hash buckets. Therefore, the address cannot be
copied and shared when include/prepend. So, when sharing `iv_tbl`, refer to
it via the sharing source class. As a result, the following bug have also
been fixed.

* [An `iv_tbl` is not shared when a class includes or prepends an empty module](https://gist.github.com/shuujii/0ac23fa24b0c55b2c602b534d81e4a95)
2021-02-03 23:16:09 +09:00
Yukihiro "Matz" Matsumoto 17ecf14511 Revert "Minimize the changes in #5277"
This reverts commit dc51d89ac2.
2021-01-26 10:57:07 +09:00