159 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 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 3a7e0e9d82 mruby-rational: ROM method tables for Rational/Integer/NilClass/Kernel
Co-authored-by: Claude <noreply@anthropic.com>
2026-02-19 09:39:33 +09:00
Yukihiro "Matz" Matsumoto 78fe8a0476 mruby-rational: fix infinite recursion with bigint comparison
rational_eq_b was using wrong struct fields (p1->numerator/denominator
which access i.num/i.den) for bigint-backed rationals that use b.num/b.den.
Also added missing MRB_TT_BIGINT case to prevent fallthrough to default
case which caused ping-pong recursion between Rational#== and Integer#==.

Co-authored-by: Claude <noreply@anthropic.com>
2026-01-12 19:23:15 +09:00
Yukihiro "Matz" Matsumoto bbcadd6bf9 mruby-rational: fix left shift overflow in rational_new_f
Shifting 1 left by MRB_INT_BIT-1 (e.g., 63 on 64-bit) bits into the sign
bit is undefined behavior. Change the overflow check from >= MRB_INT_BIT
to >= MRB_INT_BIT-1 to prevent this.

Co-authored-by: Claude <noreply@anthropic.com>
2026-01-05 17:47:57 +09:00
Yukihiro "Matz" Matsumoto a9825e92df mruby-rational: fix crash in rational_new_f with negative exponent
rational_new_b() expects both arguments to be bigints, but rational_new_f()
was passing an integer value for the numerator when the exponent was negative.
This caused a segfault in mrb_bint_reduce() which called RBIGINT() on the
integer value.

Test case: 5r**-92 (from oss-fuzz)

Co-authored-by: Claude <noreply@anthropic.com>
2026-01-03 13:13:02 +09:00
Yukihiro "Matz" Matsumoto 5eca2fae1e rational.c: fix undefined behavior from large shift exponents
In rational_new_f(), the code performed ((mrb_int)1)<<exp without
checking if exp >= MRB_INT_BIT. Shifting by a value >= bit width
is undefined behavior in C.

Also fixed the negative exponent case which incorrectly used
deno >>= exp (right-shift by negative is UB). The correct logic
is deno <<= -exp to multiply denominator by 2^(-exp).

Both cases now check for overflow before shifting and fall back
to bigint operations when necessary.

Discovered via ClusterFuzz with input "92r**11".

Co-authored-by: Claude <noreply@anthropic.com>
2025-12-30 14:28:44 +09:00
Yukihiro "Matz" Matsumoto ce570c28bb mruby-rational: inline int_lshift into rational_new_f
Remove the static int_lshift function and directly call mrb_bint_lshift
at the only call site. This simplifies the code and avoids static
function name collision with src/numeric.c for future amalgamation
support.

Co-authored-by: Claude <noreply@anthropic.com>
2025-12-23 08:50:30 +09:00
Yukihiro "Matz" Matsumoto 1cf225dfbe mruby-rational: add comprehensive call-seq documentation for Rational methods
Added complete call-seq documentation for all Rational methods in
mrblib/rational.rb (4 methods):

## Rational Class Methods:

- inspect: returns string representation for debugging with parentheses
  format, showing the rational value in "(numerator/denominator)" form

- to_s: returns string representation in "numerator/denominator" format
  for display and conversion purposes

- <=>: spaceship operator for comparison with other numeric types,
  returns -1/0/+1 for less/equal/greater comparisons, enables Comparable
  module functionality with proper nil handling for incomparable values

## Numeric Extension Methods:

- to_r: converts any numeric value to rational representation with
  denominator of 1, part of the standard numeric conversion protocol

Co-authored-by: Atlassian Rovo Dev
2025-08-14 10:52:45 +09:00
Yukihiro "Matz" Matsumoto 2edfd96cbe mruby-rational: add README.md
The document is written by Google Jules.
2025-06-17 16:22:19 +09:00
Yukihiro "Matz" Matsumoto 86ef7a912d mruby-rational (int_lshift): check before actual left bit shift 2025-05-21 13:25:08 +09:00
Yukihiro "Matz" Matsumoto f4b01ed19e mruby-rational (int_lshift): exponential may be bigger than int bits 2025-05-21 10:40:02 +09:00
Yukihiro "Matz" Matsumoto d908daa8b9 mruby-rational (rational_hash): hash value is uint32_t 2025-03-25 08:10:49 +09:00
Yukihiro "Matz" Matsumoto e5e8dd11c0 mruby-rational: make Rational() private 2025-03-07 17:17:45 +09:00
dearblue 12e90896a5 Moved tests for Integer#quo
The `Rational` class is never defined during core testing.
2025-01-26 21:11:21 +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
Yukihiro "Matz" Matsumoto 3885e14049 mruby-rational: explicitly add return for error cases too 2024-10-31 14:06:10 +09:00
Yukihiro "Matz" Matsumoto 7b5608e78a mruby-rational: refactor float to rational conversion
- the function `float_decode_internal` was removed
- simplified `rational_new_f()`
- do not need to call `ldexp`
- allocate less bigint objects
2024-10-15 21:10:11 +09:00
Yukihiro "Matz" Matsumoto 51b11a6919 mruby-rational: add comments
We used `mrb_bint_mul(mrb, n, ONE)` to normalize `n`. The code confused
ChatGPT that could not read the intention.
2024-10-15 21:10:10 +09:00
Yukihiro "Matz" Matsumoto 763c9cba03 mruby-rational (int_lshift): add cast to stop overflow warning 2024-10-11 08:44:12 +09:00
Yukihiro "Matz" Matsumoto 453e9eabf0 mruby-rational (rat_add_b): might have called rational_new_b with int
`rational_new_b` should be called with bigint objects.
2024-10-10 07:55:51 +09:00
Yukihiro "Matz" Matsumoto afefb5261b mruby-rational (rat_sub_b): avoid normalization of big integers 2024-09-15 03:04:32 +09:00
Yukihiro "Matz" Matsumoto 50225acec4 mruby-rational (rat_mul_b): should not normalize multiplication results 2024-09-15 02:50:57 +09:00
Yukihiro "Matz" Matsumoto 4a71d4604e mruby-rational: add typecast to check if a value is fit in mrb_int 2024-08-27 22:48:40 +09:00
Yukihiro "Matz" Matsumoto 4ea6d74d83 mruby-rational: introduce the constants ONE and ZERO 2024-08-21 18:38:51 +09:00
Yukihiro "Matz" Matsumoto 39b9a1a06a mruby-rational (rational_new): fix compilation condition 2024-08-17 16:29:51 +09:00
Yukihiro "Matz" Matsumoto 44908e5cba mruby-rational: support bigint numerators & denominators 2024-08-16 15:12:03 +09:00
Yukihiro "Matz" Matsumoto fcab7ef377 numeric.c (int_div): fixed a bug regarding bigint / non-integer
Non-integer means Complex or Rational.
2024-07-15 21:35:42 +09:00
Yukihiro "Matz" Matsumoto 3b9aec90dd mruby-rational: define <=> method in Ruby, not in C 2024-06-17 08:02:42 +09:00
Yukihiro "Matz" Matsumoto 36eff3ec59 mruby-rational/test: remove UserDefinedNumeric tests
Since they checks the behavior different from CRuby.
2024-06-17 07:57:54 +09:00
Yukihiro "Matz" Matsumoto b44dc1c0b9 mruby-rational: use presym for initialization 2024-06-14 06:17:20 +09:00
Yukihiro "Matz" Matsumoto 7c2be58e2a mruby-rational (rational_pow): fix wrong power calculation
`a/b ** -n` can be calculated by `b/a ** n` for correctness.
2024-05-25 16:39:32 +09:00
Yukihiro "Matz" Matsumoto d148bf8c0b mruby-rational: adjust local variable declarations with initialization 2024-05-25 16:39:32 +09:00
Yukihiro "Matz" Matsumoto 6e09555749 Merge pull request #6150 from dearblue/itself
Introduce `mrb_obj_itself()`
2024-02-01 16:15:42 +09:00
dearblue 3111990089 Introduce mrb_obj_itself()
Some method definitions were changed to use this function.
2024-01-20 22:07:29 +09:00
dearblue 35375257f6 Remove unnecessary Rational._new method 2024-01-20 21:52:49 +09:00
Yukihiro "Matz" Matsumoto 2cf6f7f5c7 mruby-rational: move misplaced #ifndef MRB_NO_FLOAT 2024-01-08 07:19:30 +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 89f7bb1056 use more lightweight mrb_funcall_argv instead of mrb_funcall_id 2023-06-12 14:22:04 +09:00
Yukihiro "Matz" Matsumoto 795044f82c internal.h: add mrb_check_num_exact prototype to the header 2023-05-30 08:54:05 +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 9c5dc42e59 small cosmetic changes.
I prefer `i++` style unless absolutely necessary.
This commit is an addition to 41e4148.
2022-11-19 17:11:56 +09:00
fn ⌃ ⌥ f3ec5f744f rational.c: implement nil.to_r and Float#to_r 2022-10-24 17:31:52 -07:00