Skip to content

Commit 7f2fb62

Browse files
committed
Fix and specs for return value of rb_protect when an error occurs.
1 parent bd034d6 commit 7f2fb62

File tree

3 files changed

+15
-1
lines changed

3 files changed

+15
-1
lines changed

lib/truffle/truffle/cext.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1084,7 +1084,11 @@ def rb_proc_call(prc, args)
10841084
# throw it and allow normal error handling to continue.
10851085

10861086
def rb_protect_with_block(function, arg)
1087-
res = nil
1087+
# We wrap nil here to avoid wrapping any result returned, as the
1088+
# function called will do that. In general we try not to touch the
1089+
# values passed in or out of protected functions as C extensions
1090+
# may accept or return arbitrary pointers rather than ruby VALUEs.
1091+
res = rb_tr_wrap(nil)
10881092
pos = 0
10891093
e = capture_exception do
10901094
res = Truffle::Interop.execute_without_conversion(function, arg)

spec/ruby/optional/capi/ext/kernel_spec.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,7 @@ static VALUE kernel_spec_rb_protect_yield(VALUE self, VALUE obj, VALUE ary) {
168168
int status = 0;
169169
VALUE res = rb_protect(rb_yield, obj, &status);
170170
rb_ary_store(ary, 0, INT2NUM(23));
171+
rb_ary_store(ary, 1, res);
171172
if (status) {
172173
rb_jump_tag(status);
173174
}

spec/ruby/optional/capi/kernel_spec.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,15 @@
281281
end.should raise_error(NameError)
282282
proof[0].should == 23
283283
end
284+
285+
it "will return nil if an error was raised" do
286+
proof = [] # Hold proof of work performed after the yield.
287+
lambda do
288+
@s.rb_protect_yield(7, proof) { |x| raise NameError}
289+
end.should raise_error(NameError)
290+
proof[0].should == 23
291+
proof[1].should == nil
292+
end
284293
end
285294

286295
describe "rb_rescue" do

0 commit comments

Comments
 (0)