Commit Graph

17560 Commits

Author SHA1 Message Date
Yukihiro "Matz" Matsumoto 47253a0fe4 mruby-compiler: implement variable-sized nodes for containers and arguments
Add variable-sized node support for containers (array, hash, words, symbols)
and arguments (splat, to_ary, svalue, block_arg) to optimize memory usage
for statement blocks and argument processing.

Co-authored-by: Claude <noreply@anthropic.com>
2025-10-03 19:46:05 +09:00
Yukihiro "Matz" Matsumoto da8072e00a mruby-compiler: implement variable-sized nodes for function calls and special forms
Implements variable-sized nodes for function calls and special forms
(NODE_FCALL, NODE_ZSUPER, NODE_LAMBDA) with optimized memory allocation.
These nodes now use compact variable-sized structures instead of fixed-size
headers, reducing AST memory usage.

Co-authored-by: Claude <noreply@anthropic.com>
2025-10-03 19:46:05 +09:00
Yukihiro "Matz" Matsumoto d01be7af0f mruby-compiler: implement variable-sized nodes for operators and expressions
Implements variable-sized nodes for operators and expressions
(NODE_NEGATE, NODE_COLON2, NODE_COLON3) with optimized memory
allocation.  These nodes now use compact variable-sized structures
instead of fixed-size headers, reducing AST memory usage.

Co-authored-by: Claude <noreply@anthropic.com>
2025-10-03 19:46:05 +09:00
Yukihiro "Matz" Matsumoto ada87dffdb mruby-compiler: implement variable-sized nodes for references and variables
Implements variable-sized nodes for references and variables (NODE_NTH_REF,
NODE_BACK_REF, NODE_DVAR, NODE_NVAR, NODE_MATCH) with optimized memory
allocation. These nodes now use compact variable-sized structures instead
of fixed-size headers, reducing AST memory usage.

Co-authored-by: Claude <noreply@anthropic.com>
2025-10-03 19:46:04 +09:00
Yukihiro "Matz" Matsumoto 761dd43b7d mruby-compiler: implement variable-sized nodes for control flows
added variable-sized nodes for control flow and string/regex variants:
- control flow: break, next, redo, retry, while_mod, until_mod
- string/regex: xstr, dxstr, dregx, heredoc, dsym
- proper integration with existing codegen patterns
- maintains backward compatibility with traditional nodes
- tested with control flow and string interpolation

Co-authored-by: Claude <noreply@anthropic.com>
2025-10-03 19:46:04 +09:00
Yukihiro "Matz" Matsumoto 84c94cf18b mruby-compiler: implement variable-sized AST nodes optimization
added variable-sized node structures for memory optimization:
- simple nodes: singleton values (self, nil, true, false) and constants
- advanced nodes: complex structures (rescue, ensure, block)
- size class allocation system (TINY, SMALL, MEDIUM, LARGE, XLARGE)
- NODE_VARIABLE wrapper for flexible memory layout
- removed NODE_ARG from variable-sized implementation per analysis
- fixed memory corruption issues in gen_block_var with stack allocation
- cleaned up consecutive blank lines and unused code

Co-authored-by: Claude <noreply@anthropic.com>
2025-10-03 19:46:04 +09:00
Yukihiro "Matz" Matsumoto 7836af0d52 mruby-compiler: add variable-sized simple node support
add variable-sized node structures for simple nodes (self, nil, true,
false, const) with conditional usage based on var_nodes_enabled.
singleton nodes use only 8-byte header for maximum memory efficiency.
includes proper forward declarations, casting macros, creation functions,
and codegen support maintaining compatibility with existing functions.

Co-authored-by: Claude <noreply@anthropic.com>
2025-10-03 19:46:04 +09:00
Yukihiro "Matz" Matsumoto c40c28c2ab mruby-compiler: add variable-sized literal node support
add variable-sized node structures for literal nodes (dstr, regx,
dot2/dot3 ranges, float) with conditional usage based on var_nodes_enabled.
includes casting macros, value access macros, creation functions,
and codegen support that maintains compatibility with existing
traditional codegen functions.

Co-authored-by: Claude <noreply@anthropic.com>
2025-10-03 19:46:04 +09:00
Yukihiro "Matz" Matsumoto f01cc52835 mruby-compiler: implement variable-sized AST nodes for expression operations
Add support for variable-sized AST nodes for logical and control expression
operations including AND, OR, RETURN, YIELD, and SUPER.

Changes:
- Add variable-sized node structures for expression nodes in node.h
- Add casting and value access macros for expression nodes
- Modify existing expression functions to conditionally use variable-sized versions
- Implement variable-sized node creation functions (new_and_var, new_or_var, etc.)
- Add codegen support for variable-sized expression nodes
- All expression types (AND, OR, RETURN, YIELD, SUPER) now support variable-sized allocation

Co-authored-by: Claude <noreply@anthropic.com>
2025-10-03 19:46:04 +09:00
Yukihiro "Matz" Matsumoto e3d52cec12 mruby-compiler: implement variable-sized AST nodes for assignment operations
Add support for variable-sized AST nodes for assignment operations including
simple assignment, multiple assignment, and operator assignment.

Changes:
- Add variable-sized node structures for assignment nodes in node.h
- Add casting and value access macros for assignment nodes
- Modify existing assignment functions to conditionally use variable-sized versions
- Implement variable-sized node creation functions (new_asgn_var, new_masgn_var, new_op_asgn_var)
- Add codegen support for variable-sized assignment nodes
- All assignment types (simple, multiple, operator) now support variable-sized allocation

Co-authored-by: Claude <noreply@anthropic.com>
2025-10-03 19:46:03 +09:00
Yukihiro "Matz" Matsumoto 21037456df mruby-compiler: implement variable-sized AST nodes for control flow constructs
Add variable-sized node structures for all control flow statements:
- IF/ELSIF/ELSE statements with optimized condition handling
- WHILE and UNTIL loops with proper jump generation
- FOR loops with iterator support
- CASE/WHEN statements with multiple condition matching

Key changes:
- Added variable-sized node structures (mrb_ast_if_node, mrb_ast_while_node,
  mrb_ast_until_node, mrb_ast_case_node, mrb_ast_for_node) to node.h
- Implemented parser functions with size class allocation in parse.y
- Added comprehensive codegen support with proper jump handling and
  stack management in codegen.c
- All control flow nodes now use NODE_VARIABLE wrapper for consistency
- Variable-sized nodes enabled by default for improved memory efficiency

This provides memory-efficient storage for control flow constructs while
maintaining full compatibility with existing functionality.

Co-authored-by: Claude <noreply@anthropic.com>
2025-10-03 19:46:03 +09:00
Yukihiro "Matz" Matsumoto b534621957 mruby-compiler: implement variable-sized ast nodes for control flow
This completes the implementation of variable-sized AST nodes for control flow
structures (if, while, for, case), further reducing memory usage. Changes were
verified with AddressSanitizer.

Co-authored-by: Gemini <gemini@google.com>
2025-10-03 19:46:03 +09:00
Yukihiro "Matz" Matsumoto 392f4fabc0 mruby-compiler: use variable-sized ast nodes for call, array, and hash
Introduces variable-sized AST nodes for method calls (NODE_CALL),
arrays (NODE_ARRAY), and hashes (NODE_HASH). This change improves
memory efficiency by storing elements directly within the AST node,
avoiding an extra layer of pointer indirection for their data.

This is achieved by adding new data structures and functions in both
the parser and the code generator to handle these new node types.
Variable-sized nodes are now enabled by default.

Co-authored-by: Claude <noreply@anthropic.com>
2025-10-03 19:46:03 +09:00
Yukihiro "Matz" Matsumoto ac02635ba5 mruby-compiler: add infrastructure for variable-sized ast nodes
This commit introduces the core infrastructure for variable-sized AST
nodes, designed to improve memory efficiency. The previous fixed-size
nodes are replaced by nodes that can store data inline, such as
strings and integers, reducing pointer indirection and memory overhead.

Key changes include:
- A generic variable-sized node header (`mrb_ast_var_header`).
- A size-class-based memory allocation system for these nodes.
- Implementation of variable-sized nodes for core types: symbols,
  strings, integers, and variables (lvar, gvar, ivar, cvar).
- Integration into the parser and code generator, controlled by a
  feature flag.
- Centralized and improved type-casting macros for AST nodes.

Co-authored-by: Claude <noreply@anthropic.com>
2025-10-03 19:46:03 +09:00
Yukihiro "Matz" Matsumoto 8c36e227e7 mruby-compiler: implement head-only location optimization for ast nodes
This implements a memory optimization for AST nodes that stores location
information (lineno, filename_index) only in head nodes rather than in
every node, reducing memory usage for structure nodes.

Key changes:
- Split node types: mrb_ast_node (structure nodes without location),
  mrb_ast_head_node (with location info). Sizes are platform-dependent:
  8/12 bytes on 32-bit, 16/24 bytes on 64-bit platforms
- Separate allocation: cons() creates structure nodes, cons_head()
  creates head nodes with location information
- Node recycling: all nodes are recycled when freed, but only smaller
  structure nodes are reused from the free list to maintain type safety
- Updated macro: added headn() for consistent head node casting
- Removed NODE_LINENO macro: eliminated redundant location copying
  since head-only optimization already provides adequate location info
- Fixed codegen to properly access location fields via head node casts

This optimization reduces AST memory usage while preserving all
debugging and location information functionality.

Co-authored-by: Claude <noreply@anthropic.com>
2025-10-03 19:46:03 +09:00
Yukihiro "Matz" Matsumoto e41d18a49c mruby-compiler: add helper functions for common load operation patterns
Add helper functions to reduce code duplication in codegen load operations:

- gen_load_op1/gen_load_op2: for simple literal load operations following
  the pattern "if (!val) return; genop_X(...); push();"
- gen_load_nil: for conditional nil loading with "if (!val) return;" check
- gen_load_lit: for literal loading with push

Refactor 8 literal loading functions (codegen_self, codegen_nil, codegen_true,
codegen_false, codegen_sym, codegen_float, codegen_back_ref, codegen_nth_ref)
and multiple inline nil loading patterns throughout codegen.c.

Each refactored function reduced from 5-8 lines to 2-4 lines while maintaining
identical bytecode generation behavior. All 1730 tests pass.

Co-authored-by: Claude <noreply@anthropic.com>
2025-10-03 19:46:02 +09:00
Yukihiro "Matz" Matsumoto 1e178b0860 codegen.c: complete monolithic function refactoring
Extract final complex cases (NODE_OP_ASGN, NODE_MASGN), unify while/until
loop handling, apply early return pattern to reduce indentation, and achieve
complete switch statement consistency.

The original 5000+ line monolithic function is now organized into 60+ focused
functions while preserving all functionality and performance.

Co-Authored-By: Claude <noreply@anthropic.com>
2025-10-03 19:46:02 +09:00
Yukihiro "Matz" Matsumoto c54d5abdaf mruby-dir: unify int and mrb_int types for consistency
Change int variables to mrb_int in mrb_dir_getwd and mrb_dir_chroot
to maintain consistent use of mruby's integer type internally.
Keep explicit casts only at system interface boundaries where
different types are required by system calls.

Eliminates VC warning C4267 while following the same type
unification approach used in pack.c.

Co-authored-by: Claude <noreply@anthropic.com>
2025-10-03 19:46:02 +09:00
Yukihiro "Matz" Matsumoto b6a67b7721 mruby-pack: unify mrb_int and int types in template parsing
Change count variables from int to mrb_int in mrb_pack_pack and
read_tmpl functions to eliminate mixed type usage and resolve
VC warning C4244 about conversion from mrb_int to int.

Co-authored-by: Claude <noreply@anthropic.com>
2025-10-03 19:46:02 +09:00
Yukihiro "Matz" Matsumoto 24b06a2d69 Merge pull request #6634 from mruby/dependabot/github_actions/super-linter/super-linter-8.2.0 2025-10-03 09:11:18 +09:00
dependabot[bot] adccd54fad build(deps): bump super-linter/super-linter from 8.1.0 to 8.2.0
Bumps [super-linter/super-linter](https://github.com/super-linter/super-linter) from 8.1.0 to 8.2.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.1.0...v8.2.0)

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

Signed-off-by: dependabot[bot] <support@github.com>
2025-10-01 14:02:06 +00:00
Yukihiro "Matz" Matsumoto 4fa9420332 Merge pull request #6633 from oliverchang/fix-ossfuzz-428404023 2025-10-01 18:51:19 +09:00
Oliver Chang b9fe516d23 Fix a heap-buffer-overflow in in str strip! methods.
This issue was originally discovered by OSS-Fuzz:

https://issues.oss-fuzz.com/issues/428404023

The root cause was that str_strip_bang modified the string content and
length in-place but failed to null-terminate the string at its new
length.

When this modified, non-null-terminated string was duplicated, the
buffer may be resized, dropping the old null terminator (via str_uminus
-> mrb_str_dup -> str_replace -> str_share). When this is later passed
to mrb_raisef using the %!s format specifier, mrb_vformat called strlen
on the underlying non-null terminated buffer pointer.

The fix adds explicit null-termination in str_strip_bang,
str_lstrip_bang, and str_rstrip_bang after the string length is updated.
2025-10-01 05:34:10 +00:00
Yukihiro "Matz" Matsumoto 531823546b Merge pull request #6629 from jbampton/update-pre-commit 2025-09-23 07:53:44 +01:00
Yukihiro "Matz" Matsumoto 233de28d43 Merge pull request #6628 from dearblue/File.absolute_path 2025-09-23 07:52:14 +01:00
John Bampton 4658c8a703 pre-commit update
https://pre-commit.com/#pre-commit-autoupdate

https://nodejs.org/en/download
2025-09-20 02:06:06 +10:00
dearblue 5838de682b Revert File.absolute_path logic
The error was introduced by commit 7b9d1da3fc.
2025-09-19 22:51:09 +09:00
dearblue 445fe9ba32 Add a test for the File.absolute_path method 2025-09-19 22:43:12 +09:00
Yukihiro "Matz" Matsumoto d074aaadce Merge pull request #6624 from dearblue/build.current 2025-09-18 01:23:55 +09:00
dearblue 80d0f4e243 Fixed wrong MRuby::Build.current at the top level of mrbgem.rake
Until now, GEMS added via `gem.add_dependency` retained the last `MRuby::Build.current` from the build configuration file, which was accessible from the top level of `mrbgem.rake`.
2025-09-17 22:27:36 +09:00
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