Skip to content

Commit

Permalink
[ruby/io-nonblock] Remove usage of IO internals.
Browse files Browse the repository at this point in the history
  • Loading branch information
ioquatix authored and matzbot committed May 28, 2023
1 parent 881757c commit 26dd9c5
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 23 deletions.
27 changes: 27 additions & 0 deletions Gemfile.lock
@@ -0,0 +1,27 @@
PATH
remote: .
specs:
io-nonblock (0.2.0)

GEM
remote: https://rubygems.org/
specs:
power_assert (2.0.3)
rake (13.0.6)
rake-compiler (1.2.1)
rake
test-unit (3.5.7)
power_assert

PLATFORMS
arm64-darwin-22

DEPENDENCIES
bundler
io-nonblock!
rake
rake-compiler
test-unit

BUNDLED WITH
2.4.7
2 changes: 2 additions & 0 deletions ext/io/nonblock/extconf.rb
Expand Up @@ -7,3 +7,5 @@
(have_macro("F_GETFL", hdr) or have_macro("F_SETFL", hdr))
create_makefile(target)
end

have_function("rb_io_descriptor")
54 changes: 31 additions & 23 deletions ext/io/nonblock/nonblock.c
Expand Up @@ -17,6 +17,16 @@
#endif
#include <fcntl.h>

#ifndef HAVE_RB_IO_DESCRIPTOR
static int
rb_io_descriptor(VALUE io)
{
rb_io_t *fptr;
GetOpenFile(io, fptr);
return fptr->fd;
}
#endif

#ifdef F_GETFL
static int
get_fcntl_flags(int fd)
Expand All @@ -39,10 +49,8 @@ get_fcntl_flags(int fd)
static VALUE
rb_io_nonblock_p(VALUE io)
{
rb_io_t *fptr;
GetOpenFile(io, fptr);
if (get_fcntl_flags(fptr->fd) & O_NONBLOCK)
return Qtrue;
if (get_fcntl_flags(rb_io_descriptor(io)) & O_NONBLOCK)
return Qtrue;
return Qfalse;
}
#else
Expand Down Expand Up @@ -122,15 +130,13 @@ io_nonblock_set(int fd, int f, int nb)
*
*/
static VALUE
rb_io_nonblock_set(VALUE io, VALUE nb)
rb_io_nonblock_set(VALUE self, VALUE value)
{
rb_io_t *fptr;
GetOpenFile(io, fptr);
if (RTEST(nb))
rb_io_set_nonblock(fptr);
else
io_nonblock_set(fptr->fd, get_fcntl_flags(fptr->fd), RTEST(nb));
return io;
int descriptor = rb_io_descriptor(self);

io_nonblock_set(rb_io_descriptor(descriptor), get_fcntl_flags(descriptor), RTEST(value));

return self;
}

static VALUE
Expand All @@ -152,23 +158,25 @@ io_nonblock_restore(VALUE arg)
* The original mode is restored after the block is executed.
*/
static VALUE
rb_io_nonblock_block(int argc, VALUE *argv, VALUE io)
rb_io_nonblock_block(int argc, VALUE *argv, VALUE self)
{
int nb = 1;
rb_io_t *fptr;
int f, restore[2];

GetOpenFile(io, fptr);
int descriptor = rb_io_descriptor(self);

if (argc > 0) {
VALUE v;
rb_scan_args(argc, argv, "01", &v);
nb = RTEST(v);
VALUE v;
rb_scan_args(argc, argv, "01", &v);
nb = RTEST(v);
}
f = get_fcntl_flags(fptr->fd);
restore[0] = fptr->fd;
restore[1] = f;
if (!io_nonblock_set(fptr->fd, f, nb))
return rb_yield(io);

int current_flags = get_fcntl_flags(descriptor);
int restore[2] = {descriptor, current_flags};

if (!io_nonblock_set(descriptor, current_flags, nb))
return rb_yield(io);

return rb_ensure(rb_yield, io, io_nonblock_restore, (VALUE)restore);
}
#else
Expand Down

0 comments on commit 26dd9c5

Please sign in to comment.