Skip to content

Commit

Permalink
ISeq#to_a respects use_block status
Browse files Browse the repository at this point in the history
```ruby
b = RubyVM::InstructionSequence.compile('def f = yield; def g = nil').to_a
pp b

 #=>
 ...
 {:use_block=>true},
 ...
```
  • Loading branch information
ko1 committed Apr 17, 2024
1 parent 0b630d6 commit f9f3018
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 0 deletions.
4 changes: 4 additions & 0 deletions compile.c
Expand Up @@ -11761,6 +11761,10 @@ rb_iseq_build_from_ary(rb_iseq_t *iseq, VALUE misc, VALUE locals, VALUE params,
ISEQ_BODY(iseq)->param.flags.ambiguous_param0 = TRUE;
}

if (Qtrue == rb_hash_aref(params, SYM(use_block))) {
ISEQ_BODY(iseq)->param.flags.use_block = TRUE;
}

if (int_param(&i, params, SYM(kwrest))) {
struct rb_iseq_param_keyword *keyword = (struct rb_iseq_param_keyword *)ISEQ_BODY(iseq)->param.keyword;
if (keyword == NULL) {
Expand Down
1 change: 1 addition & 0 deletions iseq.c
Expand Up @@ -3200,6 +3200,7 @@ iseq_data_to_ary(const rb_iseq_t *iseq)
}
if (iseq_body->param.flags.has_kwrest) rb_hash_aset(params, ID2SYM(rb_intern("kwrest")), INT2FIX(keyword->rest_start));
if (iseq_body->param.flags.ambiguous_param0) rb_hash_aset(params, ID2SYM(rb_intern("ambiguous_param0")), Qtrue);
if (iseq_body->param.flags.use_block) rb_hash_aset(params, ID2SYM(rb_intern("use_block")), Qtrue);
}

/* body */
Expand Down
12 changes: 12 additions & 0 deletions test/ruby/test_iseq.rb
Expand Up @@ -827,6 +827,18 @@ def test_compile_prism_with_file
end
end

def block_using_method
yield
end

def block_unused_method
end

def test_unused_param
assert_equal true, RubyVM::InstructionSequence.of(method(:block_using_method)).to_a.dig(11, :use_block)
assert_equal nil, RubyVM::InstructionSequence.of(method(:block_unused_method)).to_a.dig(11, :use_block)
end

def test_compile_prism_with_invalid_object_type
assert_raise(TypeError) do
RubyVM::InstructionSequence.compile_prism(Object.new)
Expand Down

0 comments on commit f9f3018

Please sign in to comment.