From: nobu.nakada@... Date: 2003-03-09T03:16:12+09:00 Subject: [ruby-dev:19772] Re: IO#initialize is not called from IO.pipe and IO.popen なかだです。 At Sun, 9 Mar 2003 00:40:59 +0900, Tanaka Akira wrote: > というように、IO.pipe や IO.popen は IO#initialize を呼びません。 Rubyレベルで置き換えられるということは、初期化に必要なパラメー タもRubyレベルに露出するということです。 IO.popenは現在djgpp, human68k, VMSでpopen(3)を使っているために FILE*をどう渡すか考えないといけません。というかFILE*のラッパー がIOなわけですが。 とりあえずIO.pipeのほうだけ。IO#initializeでmodeをFixnumで指定 できるように拡張してます。 Index: io.c =================================================================== RCS file: //sharui/cvs/ruby/src/ruby/io.c,v retrieving revision 1.189 diff -u -2 -p -r1.189 io.c --- io.c 4 Mar 2003 07:04:08 -0000 1.189 +++ io.c 8 Mar 2003 17:51:11 -0000 @@ -2828,6 +2828,11 @@ rb_io_initialize(argc, argv, io) fd = NUM2INT(fnum); if (argc == 2) { - SafeStringValue(mode); - flags = rb_io_mode_modenum(RSTRING(mode)->ptr); + if (FIXNUM_P(mode)) { + flags = FIX2LONG(mode); + } + else { + SafeStringValue(mode); + flags = rb_io_mode_modenum(RSTRING(mode)->ptr); + } } else { @@ -3508,10 +3513,18 @@ rb_f_syscall(argc, argv) } +static VALUE io_new_instance _((VALUE)); +static VALUE +io_new_instance(args) + VALUE args; +{ + return rb_class_new_instance(2, (VALUE*)args, rb_cIO); +} + static VALUE rb_io_s_pipe() { #ifndef __human68k__ - int pipes[2]; - VALUE r, w; + int pipes[2], state; + VALUE r, w, args[2]; #ifdef _WIN32 @@ -3522,6 +3535,21 @@ rb_io_s_pipe() rb_sys_fail(0); - r = prep_stdio(rb_fdopen(pipes[0], "r"), FMODE_READABLE, rb_cIO); - w = prep_stdio(rb_fdopen(pipes[1], "w"), FMODE_WRITABLE|FMODE_SYNC, rb_cIO); + args[0] = INT2NUM(pipes[0]); + args[1] = INT2FIX(O_RDONLY); + r = rb_protect(io_new_instance, (VALUE)args, &state); + if (state) { + close(pipes[0]); + close(pipes[1]); + rb_jump_tag(state); + } + args[0] = INT2NUM(pipes[1]); + args[1] = INT2FIX(O_WRONLY); + w = rb_protect(io_new_instance, (VALUE)args, &state); + if (state) { + close(pipes[1]); + if (!NIL_P(r)) rb_io_close(r); + rb_jump_tag(state); + } + rb_io_synchronized(RFILE(w)->fptr); return rb_assoc_new(r, w); -- --- 僕の前にBugはない。 --- 僕の後ろにBugはできる。 中田 伸悦