Skip to content

Commit

Permalink
Add rb_io_close_p function
Browse files Browse the repository at this point in the history
  • Loading branch information
andrykonchin committed Nov 20, 2024
1 parent 49a4475 commit b5abbdc
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ Compatibility:
* Add `rb_gc_mark_locations()` (#3704, @andrykonchin).
* Implement `rb_str_format()` (#3716, @andrykonchin).
* Add `IO#{pread, pwrite}` methods (#3718, @andrykonchin).
* Add `rb_io_closed_p()` (#3681, @andrykonchin).

Performance:

Expand Down
2 changes: 1 addition & 1 deletion lib/cext/include/truffleruby/truffleruby-abi-version.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@
// $RUBY_VERSION must be the same as TruffleRuby.LANGUAGE_VERSION.
// $ABI_NUMBER starts at 1 and is incremented for every ABI-incompatible change.

#define TRUFFLERUBY_ABI_VERSION "3.3.5.4"
#define TRUFFLERUBY_ABI_VERSION "3.3.5.5"

#endif
4 changes: 4 additions & 0 deletions lib/truffle/truffle/cext.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2367,6 +2367,10 @@ def rb_io_path(io)
io.instance_variable_get(:@path)
end

def rb_io_closed_p(io)
io.closed?
end

def rb_tr_io_pointer(io)
Primitive.object_hidden_var_get(io, RB_IO_STRUCT)
end
Expand Down
5 changes: 5 additions & 0 deletions spec/ruby/optional/capi/ext/io_spec.c
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,10 @@ static VALUE io_spec_rb_io_mode(VALUE self, VALUE io) {
static VALUE io_spec_rb_io_path(VALUE self, VALUE io) {
return rb_io_path(io);
}

static VALUE io_spec_rb_io_closed_p(VALUE self, VALUE io) {
return rb_io_closed_p(io);
}
#endif

void Init_io_spec(void) {
Expand Down Expand Up @@ -409,6 +413,7 @@ void Init_io_spec(void) {
#if defined(RUBY_VERSION_IS_3_3) || defined(TRUFFLERUBY)
rb_define_method(cls, "rb_io_mode", io_spec_rb_io_mode, 1);
rb_define_method(cls, "rb_io_path", io_spec_rb_io_path, 1);
rb_define_method(cls, "rb_io_closed_p", io_spec_rb_io_closed_p, 1);
#endif
}

Expand Down
14 changes: 14 additions & 0 deletions spec/ruby/optional/capi/io_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -516,6 +516,20 @@
@o.rb_io_path(@rw_io).should == @name
end
end

describe "rb_io_closed_p" do
it "returns true when io is not closed" do
@o.rb_io_closed_p(@r_io).should == false
@r_io.closed?.should == false
end

it "returns false when io is closed" do
@r_io.close

@o.rb_io_closed_p(@r_io).should == true
@r_io.closed?.should == true
end
end
end

ruby_version_is "3.4" do
Expand Down
4 changes: 4 additions & 0 deletions src/main/c/cext/io.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ VALUE rb_io_path(VALUE io) {
return RUBY_CEXT_INVOKE("rb_io_path", io);
}

VALUE rb_io_closed_p(VALUE io) {
return RUBY_CEXT_INVOKE("rb_io_closed_p", io);
}

static RFile_and_rb_io_t* get_RFile_and_rb_io_t(VALUE io) {
RFile_and_rb_io_t* pointer = RUBY_CEXT_INVOKE_NO_WRAP("rb_tr_io_pointer", io);
if (!polyglot_is_null(pointer)) {
Expand Down

0 comments on commit b5abbdc

Please sign in to comment.