Commit 8d18c5bd

8d18c5bdc9b9Merged

Yukihiro "Matz" Matsumoto <[email protected]> · 8 months ago · 5 files · +1 added · ~4 modified

Description

Implement non-local return from blocks (block return via longjmp)

When return appears inside a block passed to a yield function, it now correctly returns from the enclosing method using longjmp.

Mechanism:

  • block_has_return() detects PM_RETURN_NODE in block bodies
  • Block env gets _blk_ret_val/_blk_ret_depth fields
  • Call site wraps yield with setjmp; block return does longjmp(3)
  • Distinguishes from exceptions (1) and throw (2)

throw benchmark: Spinel 0.03s vs CRuby 0.29s (10x faster) 70/70 tests pass.

Co-Authored-By: Claude Opus 4.6 (1M context) [email protected]

Details

Commit
8d18c5bdc9b9
Parent
d559e8076432

examples/bm_throw.rbaddedView file

1+def foo
2+ yield
3+end
4+
5+def bar
6+ foo { return 1 }
7+end
8+
9+total = 0
10+i = 0
11+while i < 200000
12+ total = total + bar
13+ total = total + bar
14+ total = total + bar
15+ total = total + bar
16+ total = total + bar
17+ total = total + bar
18+ total = total + bar
19+ total = total + bar
20+ total = total + bar
21+ total = total + bar
22+ i = i + 1
23+end
24+puts total
25+puts "done"