Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make it possible to call instance_exec with rb_block_call #1802

Merged
merged 2 commits into from
Nov 7, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
3 changes: 2 additions & 1 deletion lib/truffle/truffle/cext.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1512,12 +1512,13 @@ def send_splatted(object, method, args)
end

def rb_block_call(object, method, args, func, data)
outer_self = self
object.__send__(method, *args) do |*block_args|
TrufflePrimitive.cext_unwrap(TrufflePrimitive.call_with_c_mutex(func, [
TrufflePrimitive.cext_wrap(block_args.first),
data,
block_args.size, # argc
RARRAY_PTR(block_args), # argv
outer_self.RARRAY_PTR(block_args), # argv
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you use an explicit Truffle::CExt.RARRAY_PTR(block_args) here?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll make that change as it's a minor thing.

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