Commit Graph

94 Commits

Author SHA1 Message Date
Asmod4n d8911416c1 add alias support to mruby-method and mruby-prox-ext 2026-06-04 17:36:41 +02:00
Yukihiro "Matz" Matsumoto db29955b7a mruby-internal: switch internal mrb_funcall_id callers to argv1/argv2
Replace the 12 in-tree mrb_funcall_id call sites (all argc=1 or
argc=2) with the typed inline helpers added in the previous commit.
After inlining each call site allocates exactly the slots it needs
on its own frame instead of going through mrb_funcall_id's fixed
16-element argv buffer.

ref #5804

Co-authored-by: Claude <noreply@anthropic.com>
2026-05-10 01:32:33 +09:00
Chris Hasiński 55f0228bf8 Store compressed aspec on cfunc RProc for correct arity/parameters
Compress the 24-bit aspec into 13 free flag bits on RProc (bits 0-6
and 14-19) when wrapping cfunc methods. Field widths: req/opt 3 bits
(max 7), post/key 2 bits (max 3), rest/kdict/block 1 bit each. Values
exceeding the compressed range are clamped and rest is forced to 1.

This enables Proc#arity and Proc#parameters to return correct results
for cfunc-backed Procs (e.g. from Method#to_proc) with zero memory
overhead -- no struct change needed.

Closes #6764
2026-04-13 13:11:27 +02:00
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 b460554d33 vm.c: generalize pre-dispatch argument count check for C methods
Replace check_method_noarg() with check_argument_count() that validates
min <= argc <= max using the full aspec stored in mrb_method_t.flags.
This catches ArgumentError earlier at dispatch time, before entering
the C function.

The old check only handled the special case of aspec==0 (NOARG).
The new check extracts REQ, OPT, REST, POST, KEY, and KDICT from
the aspec and validates accordingly. Keyword hash is counted as
a positional arg only when the method doesn't accept keywords.

Remove MRB_METHOD_NOARG_P macro from proc.h (subsumed by aspec check).
Fix 15 incorrect aspec declarations across the codebase that were
exposed by the stricter enforcement.

Co-authored-by: Claude <noreply@anthropic.com>
2026-02-20 14:25:48 +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 20b9002214 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>
2026-02-20 11:05:03 +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 067f123193 mruby-method: ROM method tables for Method/UnboundMethod classes
Co-authored-by: Claude <noreply@anthropic.com>
2026-02-19 09:39:28 +09:00
Yukihiro "Matz" Matsumoto 4bef968452 mruby-method: standardize block parameter spacing
changed block spacing from { | to {| for consistency.

Co-authored-by: Claude <noreply@anthropic.com>
2025-12-11 18:09:10 +09:00
Yukihiro "Matz" Matsumoto 4ab07c74d6 mruby-method: add comprehensive call-seq documentation for Method extensions
Added complete call-seq documentation for all Method extension methods in
mrblib/method.rb (3 methods):

## Method Extension Methods:

- to_proc: converts Method object to Proc for functional programming
  patterns, enables use with &: syntax for concise method references
  and supports full argument passing including blocks and keyword arguments

- << (left composition): method composition operator that calls other_proc
  first then this method, enables right-to-left function composition with
  mathematical notation f(g(x)) for building complex transformations

- >> (right composition): method composition operator that calls this method
  first then other_proc, enables left-to-right function composition with
  pipeline notation for intuitive data flow transformations

Co-authored-by: Atlassian Rovo Dev
2025-08-14 10:52:46 +09:00
Yukihiro "Matz" Matsumoto eca051e022 mruby-method: add comprehensive documentation for all public methods
Added call-seq documentation for 16 key functions addressing critical 0%
documentation coverage. Includes method signatures, clear descriptions,
practical examples, and expected output for Method, UnboundMethod, Kernel,
and Module method introspection functionality.

Co-authored-by: Atlassian Rovo Dev
2025-07-09 13:49:59 +09:00
Yukihiro "Matz" Matsumoto 18f65c6652 mruby-method: update README.md
Updated document is written by Google Jules.
2025-06-11 18:35:59 +09:00
Yukihiro "Matz" Matsumoto 41e91a2ca8 vm.c (mrb_exec_irep): remove recently added separate_module argument
The argument was added in #6512
2025-05-08 07:27:23 +09:00
dearblue 3fd5e1c250 Fixed visibility at method definition
There was a problem with visibility state from proc that straddles a fiber or is independent.

Therefore, it has been changed to give priority to env objects, if any.
Also, added "separate module" flag to block traversal to a higher level env object.

Note that the "separate module" flag is now set when calling blocks with the `mrb_yield_with_class()` function.

fixed https://github.com/mruby/mruby/issues/6494
2025-04-11 21:36:26 +09:00
dearblue a981f5aed7 Add more const qualifier for RProc 2025-01-20 22:25:50 +09:00
Yukihiro "Matz" Matsumoto 8faf78f1b8 mruby-method: singleton_method() should search in superclasses
[ruby-bugs:20620](https://bugs.ruby-lang.org/issues/20620)
2024-10-04 21:37:49 +09:00
Pete Kinnecom 509590c18d Method missing kwargs test failure:
Fail: Method#call with undefined method -- only kwargs (mrbgems: mruby-method)
 - Assertion[6]
    NoMethodError exception expected, not
    Class: <TypeError>
    Message: <Array cannot be converted to Hash>
 - Assertion[9]
    Expected: {:kwarg1=>:val1, :kwarg2=>:val2}
      Actual: [:foo]
2024-07-19 20:37:40 +00:00
Yukihiro "Matz" Matsumoto d5fd3338a4 mruby-method: use presym for initialization 2024-06-14 01:46:22 +09:00
Yukihiro "Matz" Matsumoto ae6e5b7b5e mruby-method: adjust local variable declaration place 2024-05-13 23:37:21 +09:00
Yukihiro "Matz" Matsumoto 87b358a342 Including header files in include/* by <> 2024-03-26 13:59:59 +09:00
dearblue 8ecfacefca Prohibit Class#allocate in a different way
The method introduced by #5979 causes a fault by swapping classes.

```console
% bin/mruby -e 'Method = Proc; p Object.method(:inspect)'
zsh: segmentation fault (core dumped)  bin/mruby -e 'Method = Proc; p Object.method(:inspect)'
```

After applying this patch, a `TypeError` exception will be raised.

```console
% bin/mruby -e 'Method = Proc; p Object.method(:inspect)'
trace (most recent call last):
        [1] -e:1
-e:1:in method: allocation failure of Proc (TypeError)
```

However, if the `mrb_vtype` is the same object, the same care must still be taken as before.

```console
% bin/mruby -e 'Method = Binding; p method(:puts).eval("12345")'
trace (most recent call last):
        [1] -e:1
-e:1:in eval: wrong argument type nil (expected Proc) (TypeError)
```
2023-12-22 21:59:34 +09:00
Yukihiro "Matz" Matsumoto bef7eea468 mruby-method: small refactoring mostly regarding declarations 2023-12-06 22:20:38 +09:00
Yukihiro "Matz" Matsumoto 7f1ae1c344 mruby-method (method_to_s): avoid goto across declarations for C++ 2023-12-03 22:39:27 +09:00
Yukihiro "Matz" Matsumoto 9eae7df26e mruby-method (method_to_s): _proc may be nil; add check 2023-12-03 22:38:31 +09:00
Yukihiro "Matz" Matsumoto 02f189cebc mruby-method (method_to_s): add origin name in the string representation 2023-12-03 22:38:30 +09:00
Yukihiro "Matz" Matsumoto f5bc82f267 mruby-method (method_to_s): avoid origin class like Object(Object) 2023-12-03 22:38:30 +09:00
Yukihiro "Matz" Matsumoto b52870b4c6 mruby-method: add const modifier to struct RProc* 2023-12-03 22:38:29 +09:00
Yukihiro "Matz" Matsumoto 95e09c0c95 mruby-method: implement Kernel#singleton method in C
- Also fixes error message inconsistency
- Avoid unnecessary method object allocation if possible
2023-08-22 10:53:52 +09:00
Yukihiro "Matz" Matsumoto c32f7915fb reformat else clause indentation style 2023-05-20 00:21:01 +09:00
dearblue d3128ce58a Allow Class#allocate to be prohibited
Calling `Class#allocate` with `UnboundMethod#bind_call` usually succeeds without problems.
If this behavior does not make us happy, we can now prohibit it with `MRB_SET_INSTANCE_TT(klass, MRB_TT_UNDEF)`.

At the same time, it applies to the `Binding`, `Complex`, `Data`, `Float`, `Integer`, `Method`, `Rational` and `UnboundMethod` classes.
2023-04-09 21:14:07 +09:00
Yukihiro "Matz" Matsumoto eaaa474630 mruby-method/method.c: fix proc comparison for uncovered case 2023-04-01 10:37:16 +09:00
Yukihiro "Matz" Matsumoto 1f5a4d5386 mruby-method/method.c (method_eql): simplify the logic 2023-03-27 19:21:40 +09:00
Yukihiro "Matz" Matsumoto 553f9ba802 mruby-method/method.c (method_eql): use mrb_proc_eql 2023-03-27 19:15:03 +09:00
Yukihiro "Matz" Matsumoto 0f63551def mruby-method/method.c (method_p): stricter method type check 2023-03-27 11:32:15 +09:00
Yukihiro "Matz" Matsumoto 5617de0071 mruby-method/method.c (method_eql): do not check _klass for equality. 2022-11-03 23:38:27 +09:00
John Bampton 696226a60e docs: standardize Markdown lists
Previously for lists we were using both `*` and `-` to start the list items.

This pr changes all lists to use `-`.
2022-10-26 13:09:41 +10:00
Yukihiro "Matz" Matsumoto e7e22ec6a8 mruby-method/method.c (method_to_s): add location information.
Test updated too.
2022-10-04 11:05:14 +09:00
Yukihiro "Matz" Matsumoto 3c16423c0a mruby-method/method.c (method_to_s): making it compatible with CRuby. 2022-09-29 11:00:49 +09:00
Yukihiro "Matz" Matsumoto 5e1c16653f mruby-method/method.c (method_super_method): fixed module related bug.
When the method is defined in module, super_method started method
searching from wrong point. We needed to find ICLASS corresponding to the
module, then start searching super_method from the next ancestor.
2022-09-27 23:03:38 +09:00
Yukihiro "Matz" Matsumoto 4107cc95a2 internal.c: move some functions to <mruby/internal.h>.
Ref #5776
2022-08-15 18:13:59 +09:00
Yukihiro "Matz" Matsumoto 37dc1be74b internal.c: move mrb_method_missing prototype to <mruby/internal.h>.
Ref #5776
2022-08-15 17:25:08 +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
dearblue 58a9621df7 Fix args_unshift() in mrbgems/mruby-method
Both keyword arguments and block arguments were being destroyed when there were no arguments.
The cause of this is #5585. I' m sorry.
2021-11-28 14:10:37 +09:00
dearblue 668b12e756 Check more MRB_ARGS_NONE()
The `__id__` method implemented in the C function has `MRB_ARGS_NONE()` specified, but it is also effective in the following cases.

```ruby
p nil.__id__ opts: 1 rescue p :a
p nil.method(:__id__).call 1 rescue p :b
p nil.method(:__id__).call opts: 1 rescue p :c
p nil.method(:__id__).to_proc.call 1 rescue p :d
p nil.method(:__id__).to_proc.call opts: 1 rescue p :e
p nil.method(:__id__).unbind.bind_call nil, 1 rescue p :f
p nil.method(:__id__).unbind.bind_call nil, opts: 1 rescue p :g
p nil.__send__ :__id__, 1 rescue p :h
p nil.__send__ :__id__, opts: 1 rescue p :i
```

After applying this patch, all items will output symbols in the same way as CRuby.

For this purpose, add `MRB_PROC_NOARG` to `struct RProc::flags`.
2021-11-26 12:18:41 +09:00