diff options
author | Jeremy Evans <code@jeremyevans.net> | 2023-10-25 12:49:28 -0700 |
---|---|---|
committer | Jeremy Evans <code@jeremyevans.net> | 2023-12-07 09:30:36 -0800 |
commit | 3a88de3ca73052809f5c0bfb4ef8cd435b29ae5f (patch) | |
tree | d2b80103106391194d69322940bb43a8ca00e177 /vm_insnhelper.c | |
parent | 0c3593b6573b4186c980fb4ea7635bf95261c749 (diff) |
Support eval "return" at toplevel
Since Ruby 2.4, `return` is supported at toplevel. This makes `eval "return"`
also supported at toplevel.
This mostly uses the same tests as direct `return` at toplevel, with a couple
differences:
`END {return if false}` is a SyntaxError, but `END {eval "return" if false}`
is not an error since the eval is never executed. `END {return}` is a
SyntaxError, but `END {eval "return"}` is a LocalJumpError.
The following is a SyntaxError:
```ruby
class X
nil&defined?0--begin e=no_method_error(); return; 0;end
end
```
However, the following is not, because the eval is never executed:
```ruby
class X
nil&defined?0--begin e=no_method_error(); eval "return"; 0;end
end
```
Fixes [Bug #19779]
Diffstat (limited to 'vm_insnhelper.c')
-rw-r--r-- | vm_insnhelper.c | 11 |
1 files changed, 10 insertions, 1 deletions
diff --git a/vm_insnhelper.c b/vm_insnhelper.c index 8e1ac27c51..50ca9902fd 100644 --- a/vm_insnhelper.c +++ b/vm_insnhelper.c @@ -1809,7 +1809,16 @@ vm_throw_start(const rb_execution_context_t *ec, rb_control_frame_t *const reg_c } } break; - case ISEQ_TYPE_EVAL: + case ISEQ_TYPE_EVAL: { + const rb_iseq_t *is = escape_cfp->iseq; + enum rb_iseq_type t = ISEQ_BODY(is)->type; + while (t == ISEQ_TYPE_RESCUE || t == ISEQ_TYPE_ENSURE || t == ISEQ_TYPE_EVAL) { + if (!(is = ISEQ_BODY(is)->parent_iseq)) break; + t = ISEQ_BODY(is)->type; + } + toplevel = t == ISEQ_TYPE_TOP || t == ISEQ_TYPE_MAIN; + break; + } case ISEQ_TYPE_CLASS: toplevel = 0; break; |