Commit 7372c37f

7372c37f9b9fMerged

Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 3 files · ~3 modified

Description

Implement IO.pipe returning real IO handles

IO.pipe was unimplemented: the call fell through to the 0 fallback, so r, w = IO.pipe left r/w as Integer 0 and r.class printed "Integer".

Add an "io" type (an sp_File * under the hood, distinct from "file" only at the type level so .class reports IO, not File) and lower IO.pipe to a tuple:io,io: pipe(2) the fds, fdopen the read end "r" and write end "w" into GC-managed sp_File handles via the new sp_io_fdopen. r, w = IO.pipe destructures the homogeneous pair into two io-typed locals; the existing sp_File_* ops (read/write/gets/close) work on them.

Supporting pieces:

  • Register IO and File as builtin classes (appended last, so the hardcoded primitive cls_ids in sp_class_for_poly stay put). This also fixes a latent gap: File.open(...).class previously raised "undefined method 'class' for file".
  • .class on an io/file recv -> IO/File (analyze infer + codegen emit).
  • type_is_pointer("io") so io values are GC-rooted and the tuple scan marks both pipe ends.
  • sp_File_read falls back to a chunked read-to-EOF loop on a non-seekable stream (pipe/socket), where the fseek size probe fails -- so reading a pipe end actually works.

r, w = IO.pipe; r.class/w.class -> IO (matches CRuby); a write to w + read from r round-trips ("hi"). File.open(...).class -> File. Gate green (test/bench/optcarrot 59662/self-host fixpoint).

Fixes #1282.

Co-Authored-By: Claude Opus 4.8 [email protected]

Details

Commit
7372c37f9b9f
Parent
15a3305780d0

lib/sp_runtime.hmodifiedView file

@@ -1824,6 +1824,16 @@
18241824 f->mode = mode;
18251825 return f;
18261826 }
1827+/* IO.pipe end: wrap a raw pipe fd in a GC-managed sp_File so the
1828+ sp_File_* I/O ops work on it. Same finalizer/scan as sp_File_open. */
1829+static sp_File *sp_io_fdopen(int fd, const char *mode) {
1830+ sp_File *f = (sp_File *)sp_gc_alloc(sizeof(sp_File), sp_File_fin, sp_File_scan);
1831+ f->fp = fdopen(fd, mode ? mode : "r");
1832+ if (!f->fp) { sp_raise_cls("IOError", "fdopen failed"); return NULL; }
1833+ f->path = NULL;
1834+ f->mode = mode;
1835+ return f;
1836+}
18271837 static inline mrb_int sp_File_write(sp_File *f, const char *s) {
18281838 if (!f || !f->fp || !s) return 0;
18291839 size_t n = strlen(s);
@@ -1862,15 +1872,35 @@
18621872 static inline const char *sp_File_read(sp_File *f) {
18631873 if (!f || !f->fp) return sp_str_empty;
18641874 long pos = ftell(f->fp);
1865- if (pos < 0) pos = 0;
1866- if (fseek(f->fp, 0, SEEK_END) != 0) return sp_str_empty;
1867- long end = ftell(f->fp);
1868- fseek(f->fp, pos, SEEK_SET);
1869- long n = (end > pos) ? (end - pos) : 0;
1870- if (n <= 0) return sp_str_empty;
1871- char *r = sp_str_alloc((size_t)n);
1872- size_t got = fread(r, 1, (size_t)n, f->fp);
1873- r[got] = 0;
1875+ if (pos >= 0 && fseek(f->fp, 0, SEEK_END) == 0) {
1876+ long end = ftell(f->fp);
1877+ fseek(f->fp, pos, SEEK_SET);
1878+ long n = (end > pos) ? (end - pos) : 0;
1879+ if (n <= 0) return sp_str_empty;
1880+ char *r = sp_str_alloc((size_t)n);
1881+ size_t got = fread(r, 1, (size_t)n, f->fp);
1882+ r[got] = 0;
1883+ return r;
1884+ }
1885+ /* Non-seekable stream (IO.pipe end, socket): read to EOF in chunks. */
1886+ size_t cap = 256, len = 0;
1887+ char *buf = (char *)malloc(cap);
1888+ if (!buf) return sp_str_empty;
1889+ for (;;) {
1890+ if (len + 4096 + 1 > cap) {
1891+ cap = (len + 4096 + 1) * 2;
1892+ char *nb = (char *)realloc(buf, cap);
1893+ if (!nb) { free(buf); return sp_str_empty; }
1894+ buf = nb;
1895+ }
1896+ size_t got = fread(buf + len, 1, 4096, f->fp);
1897+ len += got;
1898+ if (got < 4096) break;
1899+ }
1900+ char *r = sp_str_alloc(len);
1901+ memcpy(r, buf, len);
1902+ r[len] = 0;
1903+ free(buf);
18741904 return r;
18751905 }
18761906 static inline mrb_bool sp_File_eof_p(sp_File *f) {