23 Commits

Author SHA1 Message Date
Yukihiro "Matz" Matsumoto 16151a0daa proc.c: mark Proc#dup / Proc#clone copies as orphan blocks
A copied Proc now always carries `MRB_PROC_ORPHAN`, so calling a
`dup`'d block that contains `break` or `return` raises
`LocalJumpError` even while the original yielding method is still on
the stack.

This is stricter than CRuby — which only marks the copy orphan once
the original yielding method returns — but matches mruby's
memory-first design: tracking the original via a back pointer in
RProc would also enlarge the GC mark set. dearblue's option (1) in
the linked issue, accepted for the simpler RProc layout.

Document the divergence in `doc/limitations.md` and add a regression
test in `test/t/proc.rb`.

close #6345

Co-authored-by: Claude <noreply@anthropic.com>
2026-05-11 15:06:24 +09:00
dearblue d94ec9786e Fixes identity for proc object
Previously, the identity of the proc object was verified solely based on the identity of irep.
This patch makes the behavior consistent with CRuby.

The reason I noticed this issue was that when adding multiple proc objects with the same irep to a set object, only one was added.

```ruby
p Set.new(Array.new(3) { -> {} }).size
# => 3 (Ruby 4.0)
# => 1 (mruby without this patch)
```

If the block scope is the same, there is only one in CRuby as well.
However, in CRuby, the result of `Proc#to_s` is not affected by the block scope, so it has been changed to be based on the object's address.
The reason no test for `Proc#to_s` was added is that I couldn't determine whether it should be based on `Proc#hash` or the object's address.

```ruby
b = []
t = 3
while t > 0
  b << -> {}
  t -= 1
end

p Set.new(b).size
# => 1 (Ruby 4.0 and mruby)

p b[0].to_s == b[1].to_s
# => false (Ruby 4.0)
# => true (mruby without this patch)
```
2026-02-07 16:47:39 +09:00
KOBAYASHI Shuji 6f395a58d2 Add assert_same and assert_not_same 2019-01-05 20:41:09 +09:00
Christopher Aue 35deaae776 Tested LocalJumpErrors caused by break in a proc 2017-08-30 12:25:55 +02:00
ksss a68b568911 Should call initialize method if defined 2016-12-22 10:40:13 +09:00
ksss 3cba13c249 Proc shouldn't have initialize method
Fix #3356
2016-12-21 15:17:05 +09:00
Clayton Smith 1ff4b3f800 Fix segfault in mrb_proc_copy. 2016-12-02 09:36:26 -05:00
Francois Chagnon 1ec5994377 Fix calling .arity on Proc with undefined initialize
Reported by @bouk
2016-11-24 10:23:31 -05:00
cremno f0eaf9eaf5 fix arity of lambdas with optional arguments
From the CRuby 2.2.2 Proc#arity documentation:

If the block has optional arguments, returns -n-1, where n is the number
of mandatory arguments, with the exception for blocks that are not
lambdas and have only a finite number of optional arguments; in this
latter case, returns n.
2015-06-22 13:34:24 +02:00
take_cheeze ec5b055694 Move direct superclass checking to test/t/superclass.rb. 2014-06-15 15:02:21 +09:00
Jun Hiroe 6a2eced74e Fix assert_equal argument order in test/t/proc.rb 2014-05-25 00:34:40 +09:00
Yukihiro "Matz" Matsumoto b4657182da convert &arg using to_proc; fix #2242 2014-05-13 09:13:33 +09:00
ksss 7dbfe19fc6 OP_ENTER fix segmentation fault 2014-05-10 09:43:32 +09:00
take_cheeze 9a5ef1a127 Remove use of mruby-proc-ext method. 2014-05-10 00:07:03 +09:00
ksss 06a67df950 OP_ENTER clean block object in register 2014-05-09 08:04:36 +09:00
Keita Obo c4e5a8b167 Fix self value in a block is changed with return value for Fixnum, nil, instance variable.
Fix #1504
2013-09-18 00:57:23 +09:00
Keita Obo 0bce3a36d2 Fixed self value in a block is changed with return value
fix #1504
2013-09-18 00:20:13 +09:00
Jun Hiroe 20c6810ec7 I fix order of actual and expect test value in proc.rb. 2013-08-02 22:36:38 +09:00
Daniel Bovensiepen 9166a1352b Improve Proc Tests 2013-06-14 22:18:16 +08:00
h2so5 b1ffb3b3b8 Add Proc#arity 2013-04-16 16:03:36 +09:00
Daniel Bovensiepen 0c2d74020a Add more superclass tests 2012-06-03 23:31:53 +08:00
Daniel Bovensiepen 31e9705022 Add Test cases for Literals, Enumeration, Exceptions and clean line endings 2012-05-29 01:20:46 +08:00
Daniel Bovensiepen 150b235fb6 Add Tests for all Exception classes, for false, true, Proc, Module, nil and Object 2012-05-25 14:01:14 +08:00