Skip to content

Commit

Permalink
[GR-17457] Refactor rb_obj_call_init to pass block argument to `ini…
Browse files Browse the repository at this point in the history
…tialize`.

PullRequest: truffleruby/3376
  • Loading branch information
aardvark179 committed Jun 10, 2022
2 parents 07df635 + 28cac9a commit a4e867a
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ Bug fixes:
* Fix `String#unpack("Z")` to not advance after the null byte, like CRuby (#2659, @aardvark179).
* Fix `Float#round` to avoid losing precision during the rounding process (@aardvark179).
* Fix `String#insert` to not call a subclassed string method (@bjfish).
* Fix `rb_obj_call_init` to pass any block argument to the `initialize` method (#2675, @aardvark179).

Compatibility:

Expand Down
2 changes: 1 addition & 1 deletion lib/cext/ABI_check.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
3
4
4 changes: 2 additions & 2 deletions lib/truffle/truffle/cext.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1217,8 +1217,8 @@ def rb_equal(a, b)
Primitive.object_same_or_equal(a, b)
end

def rb_obj_call_init(obj, args)
obj.__send__ :initialize, *args
def rb_obj_call_init(obj, args, block)
obj.__send__ :initialize, *args, &block
end

def rb_obj_instance_eval(obj, args, block)
Expand Down
10 changes: 10 additions & 0 deletions spec/ruby/optional/capi/object_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ def to_array
class ObjectTest
def initialize
@foo = 7
yield if block_given?
end

def foo
Expand Down Expand Up @@ -88,6 +89,15 @@ def six(a, b, *c, &d); end
o.initialized.should be_true
o.arguments.should == [:one, :two]
end

it "passes the block to #initialize" do
v = nil
o = @o.rb_obj_alloc(ObjectTest)
@o.rb_obj_call_init(o, 0, []) do
v = :foo
end
v.should == :foo
end
end

describe "rb_is_instance_of" do
Expand Down
2 changes: 1 addition & 1 deletion src/main/c/cext/object.c
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ VALUE rb_equal(VALUE a, VALUE b) {
}

void rb_obj_call_init(VALUE object, int argc, const VALUE *argv) {
RUBY_CEXT_INVOKE_NO_WRAP("rb_obj_call_init", object, rb_ary_new4(argc, argv));
RUBY_CEXT_INVOKE_NO_WRAP("rb_obj_call_init", object, rb_ary_new4(argc, argv), rb_block_proc());
}

// taint status
Expand Down

0 comments on commit a4e867a

Please sign in to comment.