Update `IO#popen` to use keyword arguments instead of `Hash` args.
MODIFY
mrbgems/mruby-io/src/io.c
mrbgems/mruby-io/src/io.c
@@ -327,19 +327,16 @@ mrb_io_alloc(mrb_state *mrb)
#endif
static int
-option_to_fd(mrb_state *mrb, mrb_value hash, const char *key)
+option_to_fd(mrb_state *mrb, mrb_value v)
{
- mrb_value opt;
+ if (mrb_undef_p(v)) return -1;
+ if (mrb_nil_p(v)) return -1;
- if (!mrb_hash_p(hash)) return -1;
- opt = mrb_hash_fetch(mrb, hash, mrb_symbol_value(mrb_intern_static(mrb, key, strlen(key))), mrb_nil_value());
- if (mrb_nil_p(opt)) return -1;
-
- switch (mrb_type(opt)) {
+ switch (mrb_type(v)) {
case MRB_TT_DATA: /* IO */
- return mrb_io_fileno(mrb, opt);
+ return mrb_io_fileno(mrb, v);
case MRB_TT_INTEGER:
- return (int)mrb_integer(opt);
+ return (int)mrb_integer(v);
default:
mrb_raise(mrb, E_ARGUMENT_ERROR, "wrong exec redirect action");
break;
@@ -353,8 +350,14 @@ mrb_io_s_popen(mrb_state *mrb, mrb_value klass)
{
mrb_value cmd, io;
mrb_value mode = mrb_str_new_cstr(mrb, "r");
- mrb_value opt = mrb_hash_new(mrb);
-
+ mrb_value kv[3];
+ mrb_sym knames[3] = {MRB_SYM(in), MRB_SYM(out), MRB_SYM(err)};
+ const mrb_kwargs kw = {
+ 3, 0,
+ knames,
+ kv,
+ NULL,
+ };
struct mrb_io *fptr;
const char *pname;
int pid = 0, flags;
@@ -366,23 +369,22 @@ mrb_io_s_popen(mrb_state *mrb, mrb_value klass)
HANDLE ofd[2];
int doexec;
- int opt_in, opt_out, opt_err;
ifd[0] = INVALID_HANDLE_VALUE;
ifd[1] = INVALID_HANDLE_VALUE;
ofd[0] = INVALID_HANDLE_VALUE;
ofd[1] = INVALID_HANDLE_VALUE;
- mrb_get_args(mrb, "S|oH", &cmd, &mode, &opt);
+ mrb_get_args(mrb, "S|o:", &cmd, &mode, &kw, &kv);
io = mrb_obj_value(mrb_data_object_alloc(mrb, mrb_class_ptr(klass), NULL, &mrb_io_type));
pname = RSTRING_CSTR(mrb, cmd);
flags = mrb_io_mode_to_flags(mrb, mode);
doexec = (strcmp("-", pname) != 0);
- opt_in = option_to_fd(mrb, opt, "in");
- opt_out = option_to_fd(mrb, opt, "out");
- opt_err = option_to_fd(mrb, opt, "err");
+ option_to_fd(mrb, kv[0]);
+ option_to_fd(mrb, kv[1]);
+ option_to_fd(mrb, kv[2]);
saAttr.nLength = sizeof(SECURITY_ATTRIBUTES);
saAttr.bInheritHandle = TRUE;
@@ -458,8 +460,14 @@ mrb_io_s_popen(mrb_state *mrb, mrb_value klass)
{
mrb_value cmd, io, result;
mrb_value mode = mrb_str_new_cstr(mrb, "r");
- mrb_value opt = mrb_hash_new(mrb);
-
+ mrb_value kv[3];
+ mrb_sym knames[3] = {MRB_SYM(in), MRB_SYM(out), MRB_SYM(err)};
+ const mrb_kwargs kw = {
+ 3, 0,
+ knames,
+ kv,
+ NULL,
+ };
struct mrb_io *fptr;
const char *pname;
int pid, flags, fd, write_fd = -1;
@@ -469,16 +477,16 @@ mrb_io_s_popen(mrb_state *mrb, mrb_value klass)
int saved_errno;
int opt_in, opt_out, opt_err;
- mrb_get_args(mrb, "S|oH", &cmd, &mode, &opt);
+ mrb_get_args(mrb, "S|o:", &cmd, &mode, &kw);
io = mrb_obj_value(mrb_data_object_alloc(mrb, mrb_class_ptr(klass), NULL, &mrb_io_type));
pname = RSTRING_CSTR(mrb, cmd);
flags = mrb_io_mode_to_flags(mrb, mode);
doexec = (strcmp("-", pname) != 0);
- opt_in = option_to_fd(mrb, opt, "in");
- opt_out = option_to_fd(mrb, opt, "out");
- opt_err = option_to_fd(mrb, opt, "err");
+ opt_in = option_to_fd(mrb, kv[0]);
+ opt_out = option_to_fd(mrb, kv[1]);
+ opt_err = option_to_fd(mrb, kv[2]);
if (OPEN_READABLE_P(flags)) {
if (pipe(pr) == -1) {

Update `IO#popen` to use keyword arguments instead of `Hash` args.