Skip to content

Commit

Permalink
[GR-19220] Make it possible to call instance_exec with rb_block_call (#…
Browse files Browse the repository at this point in the history
…1802).

PullRequest: truffleruby/1124
  • Loading branch information
eregon committed Nov 7, 2019
2 parents 56d267f + 8ed42b7 commit 3d2def7
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ Bug fixes:
* Default `close_others` in `Process.exec` to false like Ruby 2.6 (#1798, @XrXr).
* Don't clone methods when setting method to the same visibility (#1794, @XrXr).
* BigDecimal() deal with large rationals precisely (#1797, @XrXr).
* Make it possible to call `instance_exec` with `rb_block_call` (#1802, @XrXr).

Compatibility:

Expand Down
2 changes: 1 addition & 1 deletion lib/truffle/truffle/cext.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1517,7 +1517,7 @@ def rb_block_call(object, method, args, func, data)
TrufflePrimitive.cext_wrap(block_args.first),
data,
block_args.size, # argc
RARRAY_PTR(block_args), # argv
Truffle::CExt.RARRAY_PTR(block_args), # argv
nil, # blockarg
]))
end
Expand Down
9 changes: 9 additions & 0 deletions spec/ruby/optional/capi/ext/kernel_spec.c
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,14 @@ VALUE kernel_spec_rb_block_call_multi_arg(VALUE self, VALUE ary) {
return rb_block_call(ary, rb_intern("inject"), 1, method_args, block_call_inject_multi_arg, Qnil);
}

static VALUE return_extra_data(RB_BLOCK_CALL_FUNC_ARGLIST(yield_value, extra_data)) {
return extra_data;
}

VALUE rb_block_call_extra_data(VALUE self, VALUE object) {
return rb_block_call(object, rb_intern("instance_exec"), 0, NULL, return_extra_data, object);
}

VALUE kernel_spec_rb_block_call_no_func(VALUE self, VALUE ary) {
return rb_block_call(ary, rb_intern("map"), 0, NULL, NULL, Qnil);
}
Expand Down Expand Up @@ -304,6 +312,7 @@ void Init_kernel_spec(void) {
rb_define_method(cls, "rb_block_call", kernel_spec_rb_block_call, 1);
rb_define_method(cls, "rb_block_call_multi_arg", kernel_spec_rb_block_call_multi_arg, 1);
rb_define_method(cls, "rb_block_call_no_func", kernel_spec_rb_block_call_no_func, 1);
rb_define_method(cls, "rb_block_call_extra_data", rb_block_call_extra_data, 1);
rb_define_method(cls, "rb_block_proc", kernel_spec_rb_block_proc, 0);
rb_define_method(cls, "rb_block_lambda", kernel_spec_rb_block_lambda, 0);
rb_define_method(cls, "rb_frame_this_func_test", kernel_spec_rb_frame_this_func, 0);
Expand Down
5 changes: 5 additions & 0 deletions spec/ruby/optional/capi/kernel_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,11 @@
i + 1
end.should == [2, 4, 6]
end

it "can pass extra data to the function" do
ary = [3]
@s.rb_block_call_extra_data(ary).should equal(ary)
end
end

describe "rb_frame_this_func" do
Expand Down

0 comments on commit 3d2def7

Please sign in to comment.