Commit Graph

17530 Commits

Author SHA1 Message Date
Yukihiro "Matz" Matsumoto 437b47dca7 Merge pull request #6621 from dearblue/build/c++exc 2025-09-13 23:13:12 +09:00
dearblue db1578c123 Separating the build setup portion from the GEMS setup block
The issue resolved by the preceding patch was solely the C++ exception task within the mruby core.
This patch aims to resolve a similar sequencing issue that also exists in GEMS.

In practice, `mruby-compiler` is sometimes loaded via dependencies rather than being explicitly specified in the build configuration file.
In such cases, when `mruby-compiler/mrbgem.rake` is loaded, it is not yet determined whether C++ exceptions will be used. Consequently, even if it later becomes clear that `core/codegen-cxx.cxx` and `core/y.tab-cxx.cxx` are required, the system could not handle this.

To resolve this issue, we introduce the `MRuby::Gem::Specification#build_settings` method as a mechanism for lazily evaluating build setup.
However, for backward compatibility, the commands are cloned twice in `gem.setup` and `gem.setup_build`.
This is because many existing GEMS configure commands directly within the setup block.

ref. https://github.com/mruby/mruby/issues/6615
2025-09-11 22:53:06 +09:00
dearblue 004fe0b142 Set up all GEMS before mruby core tasks definition
Until now, GEMs dependent on GEMs described in the build configuration file were loaded and set up after mruby core tasks were defined.
This caused an issue where, if C++ exceptions were enabled later by a dependent GEM, the necessary tasks for mruby core were not defined.

fixed https://github.com/mruby/mruby/issues/6615
2025-09-11 22:53:03 +09:00
Yukihiro "Matz" Matsumoto 797caf4fc5 Merge pull request #6619 from mruby/dependabot/github_actions/actions/labeler-6 2025-09-05 23:43:12 +09:00
dependabot[bot] 7d0b5de384 build(deps): bump actions/labeler from 5 to 6
Bumps [actions/labeler](https://github.com/actions/labeler) from 5 to 6.
- [Release notes](https://github.com/actions/labeler/releases)
- [Commits](https://github.com/actions/labeler/compare/v5...v6)

---
updated-dependencies:
- dependency-name: actions/labeler
  dependency-version: '6'
  dependency-type: direct:production
  update-type: version-update:semver-major
...

Signed-off-by: dependabot[bot] <support@github.com>
2025-09-05 14:02:16 +00:00
Yukihiro "Matz" Matsumoto c6f340f981 Merge pull request #6618 from dearblue/bin/mruby 2025-09-01 16:14:38 +09:00
dearblue aac7751ed4 Stop generating unnecessary C++ files in mruby-bin-mruby
This is probably a remnant from when `MRB_TRY()` was used in the past.
2025-08-31 23:15:23 +09:00
Yukihiro "Matz" Matsumoto f830513978 Merge pull request #6617 from zenspider/zenspider__misc_fixes 2025-08-28 00:01:37 +09:00
Ryan Davis 2b3e5b1fd4 Fix warning about tautological comparison in pack.c
Changing from signed char to unsigned char to make comparison valid
rather than removing comparison.
2025-08-26 13:46:41 +01:00
Ryan Davis fc624020e6 Rakefile: make the whole thing parallel unless SERIAL=1
Goes from 36s to 16s on my system (from clean):
```
$ 2>&1 time -p rake -m       | rg real
real 14.72
$ 2>&1 time -p rake          | rg real
real 14.72
$ 2>&1 time -p rake SERIAL=1 | rg real
real 37.49
```
2025-08-26 13:33:33 +01:00
Ryan Davis 6b6aa830fd Fix some typos in doc/guides/mrbgems.md 2025-08-26 13:33:06 +01:00
Ryan Davis 4c69f42048 .gitignore: build -> /build
Allows lib/mruby/build/* to be seen
2025-08-26 13:31:46 +01:00
Yukihiro "Matz" Matsumoto 14d1ad0038 mruby-bigint: remove broken MSVC _umul128 optimization path
The MSVC _umul128 code path was designed for 64-bit limbs but mruby's
bigint implementation uses 32-bit limbs even on 64-bit builds. This
fundamental mismatch caused incorrect bigint calculations on VC 64-bit
builds, producing results like "100000000000000000000" -> "1661992960".

Removed the MSVC optimization to fall back to the portable double-limb
arithmetic which correctly handles 32-bit limbs.

Co-authored-by: Claude <noreply@anthropic.com>
2025-08-23 11:02:14 +09:00
Yukihiro "Matz" Matsumoto 589f256ffa mruby-bigint: fix carry calculation in MSVC 64-bit multiplication
The MSVC-specific _umul128 code path had incorrect carry propagation
when adding three values (rp[i] + lo + carry). The original code:

  carry = hi + (sum < lo);

only detected overflow between sum and lo, missing overflow in the
first addition rp[i] + lo. This caused incorrect bigint calculations
on VC 64-bit builds.

Fixed by splitting three-way addition into two two-way additions
with proper overflow detection for each step:

  temp = rp_val + lo;
  sum = temp + carry;
  carry = hi + (temp < rp_val) + (sum < temp);

Co-authored-by: Claude <noreply@anthropic.com>
2025-08-23 10:49:50 +09:00
Yukihiro "Matz" Matsumoto e7925141fb mruby-bigint: replace non-ascii characters with ascii equivalents
Replaced mathematical symbols in comments with ASCII equivalents:
- multiplication sign to *
- Greek mu to mu
- approximately equal to ~
- subscript 2 to 2
- less than or equal to <=

This complies with the coding standard to use English and ASCII
characters in all code comments and documentation.

Co-authored-by: Claude <noreply@anthropic.com>
2025-08-23 10:49:50 +09:00
Yukihiro "Matz" Matsumoto d50169b80e Merge pull request #6612 from mruby/dependabot/github_actions/super-linter/super-linter-8.1.0 2025-08-23 10:45:23 +09:00
Yukihiro "Matz" Matsumoto 109c77b9e5 Merge pull request #6614 from hasumikin/fix/unitialized-variables 2025-08-23 09:48:41 +09:00
Yukihiro "Matz" Matsumoto 9a7211bb25 numeric.h: fix integer multiplication overflow check
The previous implementation of mrb_int_mul_overflow performed
the multiplication before checking for overflow. This is undefined
behavior for signed integers and can lead to incorrect results on
some compilers (e.g., MSVC).

The implementation has been changed to perform the overflow checks
before the multiplication.

Co-authored-by: Gemini <gemini@google.com>
2025-08-23 09:43:02 +09:00
Yukihiro "Matz" Matsumoto 164a7302b1 mruby-math: add Math.expm1 and Math.log1p
Adds Math.expm1 and Math.log1p, which provide more accurate
calculations for exp(x) - 1 and log(1 + x) respectively,
especially for small values of x.

Co-authored-by: Gemini <gemini@google.com>
2025-08-23 06:49:56 +09:00
Yukihiro "Matz" Matsumoto 2ffe25636b vm.c: fix conversion warning in send_method visibility error path
Cast RARRAY_LEN result to int in send_method when handling visibility
errors to resolve C4244 warning about potential data loss from
mrb_ssize to int conversion. The cast is safe since n represents
argument count which should fit in int range.

Co-authored-by: Claude <noreply@anthropic.com>
2025-08-22 23:46:55 +09:00
Yukihiro "Matz" Matsumoto 2580d69782 mruby-bigint: fix signed/unsigned comparison warning
Cast base parameter to uint64_t in mpz_get_str power-of-2 path to
resolve C4018 warning about signed/unsigned mismatch. The comparison
now properly compares two unsigned values: ((uint64_t)1 << shift)
with (uint64_t)base.

Co-authored-by: Claude <noreply@anthropic.com>
2025-08-22 22:28:07 +09:00
Yukihiro "Matz" Matsumoto cc71d93714 mruby-array-ext: fix conversion warning in ary_init_temp_set
Cast mrb_int capacity to khint_t when calling kh_init_data to resolve
C4244 warning about potential data loss in conversion from signed to
unsigned type. The khash API expects khint_t (uint32_t) parameters.

Co-authored-by: Claude <noreply@anthropic.com>
2025-08-22 21:34:57 +09:00
Yukihiro "Matz" Matsumoto 5f9808587b mruby-random: simplify unsigned arithmetic in rand_i function
Remove intermediate bound variable and cast max directly to uint32_t
where needed for unsigned operations. This eliminates C4146 warning
about unary minus on unsigned type while maintaining the same
mathematical behavior.

Co-authored-by: Claude <noreply@anthropic.com>
2025-08-22 21:28:26 +09:00
Yukihiro "Matz" Matsumoto 0ce99e442d mruby-dir: fix C4244 warning in mrb_dir_getwd function
use mrb_int for size variable and cast to size_t only when calling getcwd.
this maintains consistency with mruby type system while avoiding
conversion warnings on windows vc compiler.

Co-authored-by: Claude <noreply@anthropic.com>
2025-08-22 18:20:47 +09:00
HASUMI Hitoshi 4035e42a39 Fix uninitialized variable in io_gets causing segmentation fault
This patch fixs a critical segmentation fault in `io_gets` function caused by an uninitialized limit variable.

This bug may specifically heppen when:
- MicroRuby with task scheduler, which I'm implementing, enabled

## Root Cause Analysis

When `io_gets` is called without arguments (argc=0), the local variable `limit` remains uninitialized on the stack.
I guess that this uninitialized memory often contains leftover heap addresses from previous stack frames.

### The problematic flow:

1. `mrb_get_args(mrb, "|o?i?", &rs, &rs_given, &limit, &limit_given)` with 0 arguments
2. `limit_given = FALSE` but limit contains garbage heap address
3. Looks like later processing truncates this address, creating invalid pointer 0xffff0000
4. This value gets pushed onto VM stack during string operations
5. Garbage collector attempts to mark 0xffff0000 as valid object pointer
6. SIGSEGV in mrb_gc_mark() at gc.c:748

    ```
    Program received signal SIGSEGV, Segmentation fault.
    0x00005c8bebffa95c in mrb_gc_mark (mrb=0x5c8bec2836c8 <heap_pool+728>, obj=0xffff0000)
        at .../gc.c:748
    748       if (!is_white(obj)) return;
    #1  mark_context_stack (mrb=0x5c8bec2836c8 <heap_pool+728>, c=0x5c8bec2b4a50 <heap_pool+202336>)
        at .../gc.c:555
    555       mrb_gc_mark(mrb, mrb_basic_ptr(v));
    ```

## Solution

I couldn't figure out the exact mechanism of the issue. Anyway, initializing the limit variable to zero could prevent invalid garbage stack memory:

```c
mrb_int limit = 0;  // Explicit initialization
```

## Files Changed

- mrbgems/picoruby-mruby/lib/mruby/mrbgems/mruby-io/src/io.c
2025-08-22 17:37:59 +09:00
Yukihiro "Matz" Matsumoto 5ed11a61fe mruby-pack: unify int/mrb_int types for string length parameters
changed all unpack function signatures from int srclen to mrb_int srclen
to maintain consistency with pack functions that use mrb_int sidx.
eliminates potential overflow when strings exceed INT_MAX and avoids
unnecessary casting from RSTRING_LEN() return value.

Co-authored-by: Claude <noreply@anthropic.com>
2025-08-22 09:35:46 +09:00
Yukihiro "Matz" Matsumoto 19db03ea67 mruby-bigint: fix vs 2022 compiler warnings
Fix C4334 and C4244 warnings that caused test failures on Windows VS 2022:
- Use uint64_t for shift operation to avoid undefined behavior
- Add explicit mp_limb casts for type conversions

Co-authored-by: Claude <noreply@anthropic.com>
2025-08-22 06:10:07 +09:00
dependabot[bot] d342de94b6 build(deps): bump super-linter/super-linter from 8.0.0 to 8.1.0
Bumps [super-linter/super-linter](https://github.com/super-linter/super-linter) from 8.0.0 to 8.1.0.
- [Release notes](https://github.com/super-linter/super-linter/releases)
- [Changelog](https://github.com/super-linter/super-linter/blob/main/CHANGELOG.md)
- [Commits](https://github.com/super-linter/super-linter/compare/v8.0.0...v8.1.0)

---
updated-dependencies:
- dependency-name: super-linter/super-linter
  dependency-version: 8.1.0
  dependency-type: direct:production
  update-type: version-update:semver-minor
...

Signed-off-by: dependabot[bot] <support@github.com>
2025-08-21 14:42:08 +00:00
Yukihiro "Matz" Matsumoto 4656cd4847 mrbgems: add newline before else keyword 2025-08-21 21:49:45 +09:00
Yukihiro "Matz" Matsumoto 8fd02f28a3 bigint.c: fix uninitialized embedded array in bint_new
When creating a bigint with embedded storage, the array wasn't being
initialized when x->p was NULL but x->sz > 0. This could leave garbage
memory in the embedded array, which VS 2022 might interpret differently
than VS 2019, causing test failures.

This fix ensures the embedded array is always properly initialized with
zeros when x->p is NULL, preventing potential undefined behavior.

Co-authored-by: Claude <noreply@anthropic.com>
2025-08-21 21:48:21 +09:00
Yukihiro "Matz" Matsumoto fb85e8b269 bigint.c: fix size update when carry occurs in mpz_mul_int
This fixes Windows VC build issues where MRB_NO_MPZ64BIT is automatically
enabled, switching to 16-bit limbs. When multiplication results in a carry,
the size must be updated to include the additional limb.

Co-authored-by: Claude <noreply@anthropic.com>
2025-08-21 13:48:46 +09:00
Yukihiro "Matz" Matsumoto 7f44320dfd mruby-bigint: fix windows vc build issues with 16-bit limbs
Fixes carry propagation in multiplication and integer conversion
overflow detection when MRB_NO_MPZ64BIT is enabled on windows
with MRB_INT32. resolves test failures for large number operations.

Co-authored-by: Claude <noreply@anthropic.com>
2025-08-21 13:02:39 +09:00
Yukihiro "Matz" Matsumoto fbb10cf73d mruby-io: fix incorrect pointer access in io.c
In the Windows-specific code path for IO.popen, the variable 'p'
is a struct, not a pointer. The code was using 'p->klass' to
access a member, which is incorrect and causes a build failure
on Windows. This has been corrected to use the 'klass' argument
directly.

Co-authored-by: Gemini <gemini@google.com>
2025-08-21 10:17:12 +09:00
Yukihiro "Matz" Matsumoto c5d8c7210d symbol.c: fix pointer tagging for windows
SYMTBL_LITERAL_FLAG was defined as 1UL, which can be smaller
than uintptr_t on some platforms (e.g., Windows 64-bit). This
caused symtbl_get_ptr() to return a corrupted pointer.

Changed the flag to be explicitly cast to uintptr_t to ensure
correct behavior on all platforms.

Co-authored-by: Gemini <gemini@google.com>
2025-08-21 10:04:18 +09:00
Yukihiro "Matz" Matsumoto a8dbe48311 symbol.c: remove unused function sym_lit_p
The function sym_lit_p was not used anywhere in the codebase.

Co-authored-by: Gemini <gemini@google.com>
2025-08-21 07:23:14 +09:00
Yukihiro "Matz" Matsumoto 240fbe41f9 symbol.c: fix label at end of compound statement in sym_intern_common
Add a null statement after the 'heap_allocation' label to silence
warnings from C++ compilers.

Co-authored-by: Gemini <gemini@google.com>
2025-08-21 07:23:14 +09:00
Yukihiro "Matz" Matsumoto 2cbb99c16d mruby-compiler: make mrb_ast_node an opaque struct in compile.h
Move the definition of struct mrb_ast_node to a private header to
hide implementation details from the public API.

Co-authored-by: Gemini <gemini@google.com>
2025-08-21 07:23:14 +09:00
Yukihiro "Matz" Matsumoto ae7e125388 mruby-compiler: encapsulate string and heredoc types
Move STR_FUNC_* macros, enum mrb_string_type, and struct
mrb_parser_heredoc_info from include/mruby/compile.h to
mrbgems/mruby-compiler/core/node.h.

These types are internal to the mruby compiler gem and are used by
both parse.y and codegen.c. Moving them to node.h encapsulates them
within the compiler gem, cleaning up the public mruby/compile.h header.

Co-authored-by: Gemini <gemini@google.com>
2025-08-21 07:23:13 +09:00
Yukihiro "Matz" Matsumoto fafb52d72a Merge pull request #6610 from pusewicz/remove-magic-numbers 2025-08-20 18:22:45 +09:00
Piotr Usewicz f5e7ea63bf Extract golden ration prime into constant
This removes the magic number that also gets repeated in the code.
Adding a constant adds extra context without having to add comments.
2025-08-19 11:11:26 +02:00
Yukihiro "Matz" Matsumoto ac3c160c3a khash.h: refactor rebuild to handle linear tables
This change allows for handling small tables as linear-search arrays,
improving performance for hashes with few elements.

Co-authored-by: Gemini <gemini@google.com>
2025-08-19 10:06:20 +09:00
Yukihiro "Matz" Matsumoto d42326ce80 khash.h: make khash rebuild GC-safe
The hash rebuild process was not GC-safe. When rebuilding the hash
table, the old data was orphaned before the new table was fully
populated, which could lead to a segmentation fault if a GC cycle
was triggered during the process.

This patch refactors the rebuild function to follow a safer pattern:
- A new temporary hash table is allocated on the stack.
- Elements from the original table are copied to the new one.
- The original table's data is swapped with the new table's data
  only after the new table is complete.

This ensures the original data is always reachable by the GC during
the rebuild.

Co-authored-by: Gemini <gemini@google.com>
2025-08-19 10:06:19 +09:00
Yukihiro "Matz" Matsumoto bb2b7667c3 mruby-pack: replace designated initializers with switch statements
Replace designated initializer lookup tables with switch statement
functions for C++ compatibility. This approach is cleaner and works
perfectly in both C and C++ modes.

- char_to_bit array -> char_to_bit() function
- char_class array -> char_class() function
- format_table array -> get_format_info() function

Co-authored-by: Claude <noreply@anthropic.com>
2025-08-19 10:06:19 +09:00
Yukihiro "Matz" Matsumoto b97f7cb73f mruby-sprintf: replace designated initializers with switch statement
Replace designated initializer lookup table with a simple switch statement
for C++ compatibility. The switch approach is cleaner and works perfectly
in both C and C++ modes.

Co-authored-by: Claude <noreply@anthropic.com>
2025-08-19 10:06:19 +09:00
Yukihiro "Matz" Matsumoto 120c58108d mruby-bigint: fix c++ compatibility issues
- Add explicit cast for mrb_malloc return value
- Remove restrict keyword from function parameters
- Move variable declarations to avoid goto/initialization conflicts
- Fix signed/unsigned comparison warning in mpz_get_str

Co-authored-by: Claude <noreply@anthropic.com>
2025-08-19 10:06:19 +09:00
Yukihiro "Matz" Matsumoto 079dd28765 fixup! mruby-io: add filetest call-seq documentation to file test methods 2025-08-19 10:06:18 +09:00
Yukihiro "Matz" Matsumoto 7e6cdc0285 mruby-io: add filetest call-seq documentation to file test methods
Co-authored-by: Claude <noreply@anthropic.com>
2025-08-19 10:06:18 +09:00
Yukihiro "Matz" Matsumoto 9e8fe00114 mruby-io: migrate File.join to C
Implements File.join in C for better performance, replacing the Ruby
implementation with direct C string manipulation and array processing.
Uses mruby's built-in recursion detection (MRB_RECURSIVE_UNARY_P) for
cleaner and more reliable recursive array handling.

Co-authored-by: Claude <noreply@anthropic.com>
2025-08-19 10:06:18 +09:00
Yukihiro "Matz" Matsumoto 6ee3f00849 mruby-io: migrate File.path to C
Implements File.path in C for better performance, replacing the Ruby
implementation that used kind_of? check with direct C type validation.

Co-authored-by: Claude <noreply@anthropic.com>
2025-08-19 10:06:17 +09:00
Yukihiro "Matz" Matsumoto 786f0aa015 mruby-io: migrate File.extname to C
Implement C version of File.extname for better performance:
- Direct C string processing instead of Ruby basename + rindex
- Efficient path parsing with single pass through string
- Proper handling of edge cases (dotfiles, trailing slashes, etc.)
- Maintains full compatibility with Ruby implementation

Performance improvement:
- Eliminates Ruby method call overhead for basename/rindex
- Direct C string operations vs Ruby string methods
- Faster path processing for file extension extraction

Co-authored-by: Claude <noreply@anthropic.com>
2025-08-19 10:06:17 +09:00