28 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 31fea1709f gc.c: replace gcnext gray linked list with fixed-size gray stack
remove per-object gcnext pointer from MRB_OBJECT_HEADER, saving one
word (8 bytes on 64-bit) per object slot. the gray list for tri-color
marking is replaced by a fixed-size stack (MRB_GRAY_STACK_SIZE=1024)
in mrb_gc. when the stack overflows, a linear heap rescan recovers
gray objects.

object slot size: 48 -> 40 bytes (16.7% reduction on 64-bit).
benchmarks show up to 12% RSS reduction on object-heavy workloads
with neutral performance impact.

Co-authored-by: Claude <noreply@anthropic.com>
2026-02-12 13:38:21 +09:00
Yukihiro "Matz" Matsumoto 32a27216bb test: add parentheses to method calls on assignment RHS
Preparation for future grammar simplification that may
require parentheses for method calls with arguments on
the right-hand side of assignments.

Co-authored-by: Claude <noreply@anthropic.com>
2026-01-27 14:58:14 +09:00
Yukihiro "Matz" Matsumoto d0892f1ba9 mruby-catch: add comprehensive call-seq documentation and helper function comments
Added complete call-seq documentation for catch/throw functionality across
both Ruby and C implementations:

- Class documentation: explains exception raised for unmatched throws
- initialize: constructor with tag and value parameters, creates error
  message with proper tag inspection and stores thrown values for debugging

- throw: transfers control to matching catch block with optional return value,
  raises UncaughtThrowError if no matching catch found, supports both
  single tag and tag+value forms with comprehensive usage examples

- find_catcher: searches call stack for matching catch block by comparing
  tags using mrb_obj_eq, returns call stack index or 0 if not found

- catch_syms: pre-defined symbols (Object, new, call) used by catch bytecode
  implementation for efficient symbol lookup

- catch_iseq: bytecode instruction sequence implementing catch method logic,
  handles default tag creation (Object.new) and block parameter passing

- catch_irep: instruction representation containing bytecode metadata
  for catch method execution

- catch_proc: procedure object used to identify catch blocks in call stack
  during throw operations, marked with proper GC and scope flags

- mrb_mruby_catch_gem_init: defines catch and throw as private methods
  in Kernel module, initializes symbols and sets up bytecode procedure

- mrb_mruby_catch_gem_final: cleanup function (currently no-op as
  implementation uses static data structures)

Co-authored-by: Atlassian Rovo Dev
2025-08-14 10:52:47 +09:00
Yukihiro "Matz" Matsumoto 6bcee69c9c mruby-catch: add README.md
The document is written by Google Jules.
2025-06-08 17:00:07 +09:00
Yukihiro "Matz" Matsumoto 4f5cad6444 mruby-catch: make #catch and #throw private 2025-03-07 17:17:43 +09:00
Yukihiro "Matz" Matsumoto 21003746df object.h: separate frozen flag to the independent bit-field
So that other flag operation do not break frozen flag.
2025-01-07 07:44:10 +09:00
Yukihiro "Matz" Matsumoto 3ececd4f1f mruby-catch: fixed typo 2024-11-25 16:51:46 +09:00
Yukihiro "Matz" Matsumoto 1c317cf79e mruby-catch (throw_m): small refactoring 2024-11-25 12:28:50 +09:00
dearblue 42513d61fd Need to place static proc objects into 8-byte alignments
Static proc objects defined as methods may be placed in 4-byte alignments in 32-bit environments.
This may be misinterpreted as an immediate value depending on the address.

Since C11 and C++11 have additional language features for byte alignment, corresponding compilers use them to define the `mrb_alignas()` macro.
For earlier compilers, they use their own extensions to define the `mrb_alignas()` macro.

GCC supports `__attribute__((aligned(alignment)))` since at least version 2.95.3 (1999).
https://gcc.gnu.org/onlinedocs/gcc-2.95.3/gcc_4.html#IDX305
According to GPT-4, support was added in version 2.7 (1995).

It is not known which version of Visual C++ added support for `__declspec(align(n))`.
According to GPT-4, at least Visual C++ 6.0 (1998) seems to support it.
Also, the documentation of past Intel C/C++ compilers that support `__declspec(align(n))` makes reference to support with Visual C++ 4.2 (1996).
https://www.intel.com/content/dam/www/public/ijkk/jp/ja/documents/developer/ccomp40j.pdf
2024-09-01 20:28:34 +09:00
John Bampton de8a6fe787 Fix spelling 2024-07-13 00:21:53 +10:00
Yukihiro "Matz" Matsumoto e4c2423f6e mruby-catch: use presym for initialization 2024-06-14 00:23:11 +09:00
dearblue 73c8330cbd RBreak remembers the CI location
It is now possible to specify return destination directly.
This allows callinfo to distinguish between calls to the same proc object.

At the same time, the `Kernel#catch` method is adjusted.
By removing the previously required double lambda object, the REnv object is no longer created as well.
2023-12-07 21:33:14 +09:00
Yukihiro "Matz" Matsumoto 9a5a9b814f mruby-catch/catch.c: remove mrb_ prefix from a static function 2023-04-25 23:27:30 +09:00
Yukihiro "Matz" Matsumoto 6ca1b8e4e7 mruby-catch/catch.c: update disasm comment for new output format 2023-02-06 11:21:52 +09:00
Yukihiro "Matz" Matsumoto 1e122f7887 mruby-catch/catch.c: add disassembled comment for catch irep 2023-02-05 22:53:11 +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
dearblue 94c5c8f44c Introduce MRB_GC_RED
Replaces the magic number `7` except in `src/gc.c`.
2021-04-24 10:31:21 +09:00
dearblue caeacea86e Making a proc object static for a method with static irep
The following methods will be made static.

  - `Class#new`
  - `Proc#call`
  - `Kernel#catch`

Previously, static const RProc could not be registered as a method, but this has been changed to allow it.
2021-04-04 15:44:19 +09:00
Yukihiro "Matz" Matsumoto 5ba37db981 Merge pull request #5336 from shuujii/fix-build-mruby-catch-gem-without-presym
Fix build `mruby-catch` gem without presym
2021-02-12 21:11:59 +09:00
KOBAYASHI Shuji 8ba7be0999 Fix build mruby-catch gem without presym 2021-02-12 19:19:32 +09:00
KOBAYASHI Shuji 955464ca3c Add UncaughtThrowError#{tag,value} for Ruby compatibility 2021-02-12 19:09:18 +09:00
Yukihiro "Matz" Matsumoto 0db6e3eb6f Rename mruby-catch.c to catch.c; ref #5328 2021-02-12 10:09:19 +09:00
Yukihiro "Matz" Matsumoto c7809ca025 Refactor mruby-catch; ref #5328
- Move `#catch` definition to `mruby-catch.c` to avoid tweaking
- Remove `#__preserve_catch_method`
- Implement whole `#throw` method in C
2021-02-10 20:09:00 +09:00
dearblue 232e07ad61 Reimplement mruby-catch; ref #5321
When there is a corresponding tag, the `RBreak` object is used to make a global jump.

Like CRuby, it can't be caught by `rescue`.
It is also the same as CRuby that it can be canceled in the middle by `ensure`.

 ### How to find the corresponding tag with `throw`

The called `catch` method remains in the call stack, and the tag also remains in the stack at that time.
So it is possible to find the called location by searching the two.

Note that no method can be given to the `proc` object specified in `RBreak`.
Therefore, inside the `catch` method, the argument block is called in a seemingly meaningless closure.

Also, as a countermeasure against `alias` etc., the `proc` object, which is the body of the `catch` method, is saved when mrbgem is initialized.
2021-02-06 21:41:08 +09:00
KOBAYASHI Shuji 1ea4c51245 Make the argument of Kernel#catch optional as Ruby does
Also implement the following changes.

* Add tests
* Use `Object#equal?` to compare tags for Ruby compatibility
* Use `attr_reader`
2021-02-06 13:28:47 +09:00
KOBAYASHI Shuji 80965fe2d7 Fix message of ThrowCatchJump in mruby-catch gem
### Example

```ruby
begin
  throw 1
rescue Exception => e
  puts e.message
end
```

#### Before this patch:

```console
$ bin/mruby example.rb
uncaught throw :1
```
#### After this patch (same as Ruby):

```console
$ bin/mruby example.rb
uncaught throw 1
```
2021-02-05 23:14:58 +09:00
Yukihiro "Matz" Matsumoto 7eaaee5405 Add a new gem: mruby-catch.
Implements `catch`/`throw` non-local jump inherited from Lisp.

`catch([tag]) {|tag| block }  -> obj`

Example:
```
  catch(:foo) { 123 }               # => 123
  catch(:foo) { throw(:foo, 456) }  # => 456
  catch(:foo) { throw(:foo) }       # => nil
```
2020-10-12 18:20:07 +09:00