[ruby/io-nonblock] Remove usage of IO internals.

(https://github.com/ruby/io-nonblock/pull/11)

https://github.com/ruby/io-nonblock/commit/caa2b94d19
This commit is contained in:
Samuel Williams 2023-05-28 17:40:33 +09:00 committed by git
parent 55393645a1
commit 14abb799c9
2 changed files with 40 additions and 25 deletions

View File

@ -2,6 +2,8 @@
require 'mkmf' require 'mkmf'
target = "io/nonblock" target = "io/nonblock"
have_func("rb_io_descriptor")
hdr = %w"fcntl.h" hdr = %w"fcntl.h"
if have_macro("O_NONBLOCK", hdr) and if have_macro("O_NONBLOCK", hdr) and
(have_macro("F_GETFL", hdr) or have_macro("F_SETFL", hdr)) (have_macro("F_GETFL", hdr) or have_macro("F_SETFL", hdr))

View File

@ -17,6 +17,16 @@
#endif #endif
#include <fcntl.h> #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 #ifdef F_GETFL
static int static int
get_fcntl_flags(int fd) get_fcntl_flags(int fd)
@ -39,10 +49,8 @@ get_fcntl_flags(int fd)
static VALUE static VALUE
rb_io_nonblock_p(VALUE io) rb_io_nonblock_p(VALUE io)
{ {
rb_io_t *fptr; if (get_fcntl_flags(rb_io_descriptor(io)) & O_NONBLOCK)
GetOpenFile(io, fptr); return Qtrue;
if (get_fcntl_flags(fptr->fd) & O_NONBLOCK)
return Qtrue;
return Qfalse; return Qfalse;
} }
#else #else
@ -122,15 +130,19 @@ io_nonblock_set(int fd, int f, int nb)
* *
*/ */
static VALUE static VALUE
rb_io_nonblock_set(VALUE io, VALUE nb) rb_io_nonblock_set(VALUE self, VALUE value)
{ {
rb_io_t *fptr; if (RTEST(value)) {
GetOpenFile(io, fptr); rb_io_t *fptr;
if (RTEST(nb)) GetOpenFile(self, fptr);
rb_io_set_nonblock(fptr); rb_io_set_nonblock(fptr);
else }
io_nonblock_set(fptr->fd, get_fcntl_flags(fptr->fd), RTEST(nb)); else {
return io; int descriptor = rb_io_descriptor(self);
io_nonblock_set(descriptor, get_fcntl_flags(descriptor), RTEST(value));
}
return self;
} }
static VALUE static VALUE
@ -152,24 +164,25 @@ io_nonblock_restore(VALUE arg)
* The original mode is restored after the block is executed. * The original mode is restored after the block is executed.
*/ */
static VALUE 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; int nb = 1;
rb_io_t *fptr;
int f, restore[2];
GetOpenFile(io, fptr); int descriptor = rb_io_descriptor(self);
if (argc > 0) { if (argc > 0) {
VALUE v; VALUE v;
rb_scan_args(argc, argv, "01", &v); rb_scan_args(argc, argv, "01", &v);
nb = RTEST(v); nb = RTEST(v);
} }
f = get_fcntl_flags(fptr->fd);
restore[0] = fptr->fd; int current_flags = get_fcntl_flags(descriptor);
restore[1] = f; int restore[2] = {descriptor, current_flags};
if (!io_nonblock_set(fptr->fd, f, nb))
return rb_yield(io); if (!io_nonblock_set(descriptor, current_flags, nb))
return rb_ensure(rb_yield, io, io_nonblock_restore, (VALUE)restore); return rb_yield(self);
return rb_ensure(rb_yield, self, io_nonblock_restore, (VALUE)restore);
} }
#else #else
#define rb_io_nonblock_set rb_f_notimplement #define rb_io_nonblock_set rb_f_notimplement