100 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 7e83c0e974 mruby-complex: ROM method tables for Complex/NilClass/Kernel
Co-authored-by: Claude <noreply@anthropic.com>
2026-02-19 09:39:34 +09:00
Yukihiro "Matz" Matsumoto 676ab238a2 mruby-complex: add parentheses to Math method calls
added parentheses to Math.hypot and Math.atan2 calls where the return
value is used.

Co-authored-by: Claude <noreply@anthropic.com>
2025-12-11 18:09:08 +09:00
Yukihiro "Matz" Matsumoto 4656cd4847 mrbgems: add newline before else keyword 2025-08-21 21:49:45 +09:00
Yukihiro "Matz" Matsumoto ee4d349f2e mruby-complex: implement Complex#** in C
Implement the Complex#** method in C. This method calculates complex
exponentiation using `exp(w * log(z))` for complex exponents and
`(abs(z)**n) * Complex.polar(1, n * arg(z))` for real exponents.
2025-08-16 12:03:12 +09:00
Yukihiro "Matz" Matsumoto 8b2975424f mruby-complex: improve Complex#div numerical stability and performance
Optimize the performance of `Complex#div` by using a hybrid approach.
For common cases, a direct calculation is used. For extreme values,
it falls back to the `frexp`/`ldexp` based calculation for numerical
stability.

Co-authored-by: Gemini <gemini@google.com>
2025-08-16 11:40:02 +09:00
Yukihiro "Matz" Matsumoto c750a47de9 mruby-complex: refactor arithmetic operations
Refactor the C implementation of arithmetic operations (+, -, *)
to reduce code duplication. A new static helper function `complex_op`
is introduced to handle the common logic of the operations.

Co-authored-by: Gemini <gemini@google.com>
2025-08-16 09:03:52 +09:00
Yukihiro "Matz" Matsumoto ef86757fd6 mruby-complex: fix division by zero
Fix a division by zero error when dividing a complex number by
`Complex(0, 0)`.

Co-authored-by: Gemini <gemini@google.com>
2025-08-16 09:03:52 +09:00
Yukihiro "Matz" Matsumoto c3610fdab2 mruby-complex: add test for division by zero
Add a test case to ensure that dividing a complex number by
`Complex(0, 0)` raises a `ZeroDivisionError`.

Co-authored-by: Gemini <gemini@google.com>
2025-08-16 09:03:52 +09:00
Yukihiro "Matz" Matsumoto 01e008167b mruby-complex: add comprehensive call-seq documentation for Complex methods
Added complete call-seq documentation for all Complex methods in
mrblib/complex.rb (18 methods):

## Complex Class Methods:

- polar: creates complex number from polar coordinates (magnitude, angle)
  with trigonometric conversion using Math.cos and Math.sin

## Complex Instance Methods:

- inspect, to_s: string representation methods for debugging and display
  with proper formatting of real and imaginary parts

- +@, -@: unary plus and minus operators for identity and negation

- <=>: spaceship operator for comparison with other numeric types,
  enables Comparable module functionality with proper nil handling

- abs/magnitude: absolute value (magnitude) calculation using hypot
- abs2: square of absolute value for performance-critical calculations
- arg/angle/phase: argument (angle) calculation using atan2

- conjugate/conj: complex conjugate operation (negates imaginary part)
- fdiv: floating-point division ensuring float results
- polar: returns [magnitude, angle] array representation
- real?: always returns false for complex numbers
- rectangular/rect: returns [real, imaginary] array representation

- to_c: returns self (identity conversion)
- to_r: converts to rational when imaginary part is zero, raises RangeError otherwise

## Numeric Extension Methods:

- i: creates pure imaginary number (0+num*i) for convenient complex creation
- to_c: converts any numeric to complex with zero imaginary part

Co-authored-by: Atlassian Rovo Dev
2025-08-14 10:52:45 +09:00
Yukihiro "Matz" Matsumoto ef1cdbc0c7 mruby-complex: add README.md
The document is written by Google Jules.
2025-06-17 16:19:44 +09:00
Yukihiro "Matz" Matsumoto 2a991fb399 mruby-complex: make Complex() private 2025-03-07 17:17:45 +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 75de002058 mruby-complex: provide Complex#<=> in Ruby 2024-06-17 08:14:27 +09:00
Yukihiro "Matz" Matsumoto 0e045a52f6 mruby-complex: use presym for initialization 2024-06-14 06:15:34 +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 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 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
fn ⌃ ⌥ c65200cb5a complex.rb: implement Numeric#to_c 2022-10-24 17:31:52 -07:00
fn ⌃ ⌥ 7801741790 complex.c: implement nil.to_c 2022-10-24 17:31:52 -07:00
Yukihiro "Matz" Matsumoto 25f9d2f65a complex.c (complex_hash): generate hash code from complex numbers. 2022-08-06 06:43:42 +09:00
Yukihiro "Matz" Matsumoto 89b0c7d7ab complex.c (mrb_complex_to_i): return bigint if possible. 2022-07-30 08:44:15 +09:00
Yukihiro "Matz" Matsumoto 6da34fa735 numeric.c (int_div): add checks for division by zero. 2022-07-29 17:48:02 +09:00
Yukihiro "Matz" Matsumoto bae11ef689 complex.c (mrb_complex_copy): allow copying of complex numbers. 2022-07-11 11:25:31 +09:00
dearblue 23611332d9 Introduced mrb_static_assert_object_size()
Asserts the size of the object structure is less than or equal to 6 words.
2022-05-28 11:08:27 +09:00
Yukihiro "Matz" Matsumoto 4f869f3367 numeric.c: enhance float division to support Complex. 2022-03-18 14:23:17 +09:00
Yukihiro "Matz" Matsumoto fe03e01078 numeric.c: enhance integer division to support Rational and Complex.
No longer need to override division methods in mrbgems.
2022-03-18 12:27:46 +09:00
Yukihiro "Matz" Matsumoto 13d014ab5e complex.c: rename static functions to remove abbreviations.
- `flo_`
- `cpx_`
2022-03-15 19:14:45 +09:00
Yukihiro "Matz" Matsumoto 4beebc73a7 complex.c: avoid mrb_funcall; call functions directly. 2022-03-12 13:31:38 +09:00
Yukihiro "Matz" Matsumoto 10001ff67f rational.c, complex.c: expose arithmetic operation functions.
* mrb_{rational,complex}_{add,sub,mul,div}
2022-03-12 08:35:07 +09:00
Yukihiro "Matz" Matsumoto b118ba9fb3 rational.c: avoid indirect calls using mrb_funcall(). 2022-02-25 17:53:55 +09:00
Yukihiro "Matz" Matsumoto c088af87b8 object.c: Call functions directly from mrb_ensure_int_type(); #5622 2022-01-05 19:00:14 +09:00
dearblue ac22a63ae3 Call functions directly from mrb_ensure_float_type()
ref. commit 7f40b645d2

Currently, the build configurations `MRB_USE_COMPLEX` and `MRB_USE_RATIONAL` are not listed in the documentation.
In other words, they are hidden settings.
They are defined in `mrbgems/mruby-{complex,rational}/mrbgem.rake`.
So this patch assumes that it is safe to refer to these functions in core-gems directly from core functions.

However, applications that link with `libmruby_core.a` will have compatibility issues.
In fact, `mrbgems/mruby-bin-mrbc` links with `libmruby_core.a`, so I had to prepare a dummy function.
2021-12-31 19:12:11 +09:00
dearblue 5a57602860 Organize the include of header files
- `#include <math.h>` is done in `mruby.h`.
  Eliminate the need to worry about the `MRB_NO_FLOAT` macro.

- Include mruby header files before standard header files.
  If the standard header file is already placed before `mruby.h`, the standard header file added in the future tends to be placed before `mruby.h`.

This change should some reduce the chances of macros that must be defined becoming undefined in C++ or including problematic header files in a particular mruby build configuration.
2021-08-21 15:42:57 +09:00
dearblue cc95e346fd Added MRB_OBJ_ALLOC() macro that does not require a cast
The `MRB_OBJ_ALLOC()` macro function returns a pointer of the type corresponding to the constant literal defined in `enum mrb_vtype`.
2021-06-20 11:08:28 +09:00
John Bampton dab5502e8a Run pre-commit with GitHub Actions
Running pre-commit with GitHub Actions now gives us more tests and coverage

Remove duplicate GitHub Actions for merge conflicts and trailing whitespace

Remove duplicate checks for markdownlint and yamllint from the GitHub Super-Linter

Add new custom pre-commit hook running with a shell script to sort alphabetically and uniquify codespell.txt

Add new pre-commit hook to check spelling with codespell

https://github.com/codespell-project/codespell

Fix spelling
2021-06-16 08:34:54 +10:00
Yukihiro "Matz" Matsumoto 5eebbd7df2 Global renaming regarding integer and float.
Consistent number conversion function names:
* `mrb_value` to immediate (C) value
  * `mrb_int()` -> `mrb_as_int()`
  * `mrb_to_flo()` -> `mrb_as_float()`
* `mrb_value` to `mrb_value` (converted)
  * `mrb_to_int()'
  * `mrb_Integer()` - removed
  * `mrb_Float()` -> `mrb_to_float`

Consistent function name (avoid `_flo` suffix):
* `mrb_div_flo()` -> `mrb_div_float`
2021-05-17 15:07:05 +09:00
KOBAYASHI Shuji c6911c54e5 Remove unused #include in complex.c and rational.c 2021-04-20 16:42:40 +09:00
John Bampton 9d32d440eb feat(CI): add the GitHub Super Linter
The GitHub Super Linter is a more robust and better supported
tool than the current GitHub Actions we are using.

Running these checks:

ERROR_ON_MISSING_EXEC_BIT: true
VALIDATE_BASH: true
VALIDATE_BASH_EXEC: true
VALIDATE_EDITORCONFIG: true
VALIDATE_MARKDOWN: true
VALIDATE_SHELL_SHFMT: true
VALIDATE_YAML: true

https://github.com/marketplace/actions/super-linter
https://github.com/github/super-linter

Added the GitHub Super Linter badge to the README.

Also updated the pre-commit framework and added
more documentation on pre-commit.

Added one more pre-commit check: check-executables-have-shebangs

Added one extra check for merge conflicts to our
GitHub Actions.

EditorConfig and Markdown linting.

Minor grammar and spelling fixes.

Update linter.yml
2021-04-16 16:37:52 +09:00
Yukihiro "Matz" Matsumoto a5244b02c5 numeric.c: function renaming.
- `mrb_num_div_int(mrb,x,y)` -> `mrb_div_int(mrb,x,y)`
- `mrb_num_div_flo(mrb,x,y)` -> `mrb_div_flo(x,y)`

They are internal function not supposed to be used outside of the core.
2021-03-28 08:35:29 +09:00
Yukihiro "Matz" Matsumoto c2f929ad0f Move default Integer#/ from rational.c to complex.c. 2021-03-27 12:38:16 +09:00