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
f->mode = mode; return f;}/* IO.pipe end: wrap a raw pipe fd in a GC-managed sp_File so the sp_File_* I/O ops work on it. Same finalizer/scan as sp_File_open. */static sp_File *sp_io_fdopen(int fd, const char *mode) { sp_File *f = (sp_File *)sp_gc_alloc(sizeof(sp_File), sp_File_fin, sp_File_scan); f->fp = fdopen(fd, mode ? mode : "r"); if (!f->fp) { sp_raise_cls("IOError", "fdopen failed"); return NULL; } f->path = NULL; f->mode = mode; return f;}static inline mrb_int sp_File_write(sp_File *f, const char *s) { if (!f || !f->fp || !s) return 0; size_t n = strlen(s);static inline const char *sp_File_read(sp_File *f) { if (!f || !f->fp) return sp_str_empty; long pos = ftell(f->fp); if (pos < 0) pos = 0; if (fseek(f->fp, 0, SEEK_END) != 0) return sp_str_empty; long end = ftell(f->fp); fseek(f->fp, pos, SEEK_SET); long n = (end > pos) ? (end - pos) : 0; if (n <= 0) return sp_str_empty; char *r = sp_str_alloc((size_t)n); size_t got = fread(r, 1, (size_t)n, f->fp); r[got] = 0; if (pos >= 0 && fseek(f->fp, 0, SEEK_END) == 0) { long end = ftell(f->fp); fseek(f->fp, pos, SEEK_SET); long n = (end > pos) ? (end - pos) : 0; if (n <= 0) return sp_str_empty; char *r = sp_str_alloc((size_t)n); size_t got = fread(r, 1, (size_t)n, f->fp); r[got] = 0; return r; } /* Non-seekable stream (IO.pipe end, socket): read to EOF in chunks. */ size_t cap = 256, len = 0; char *buf = (char *)malloc(cap); if (!buf) return sp_str_empty; for (;;) { if (len + 4096 + 1 > cap) { cap = (len + 4096 + 1) * 2; char *nb = (char *)realloc(buf, cap); if (!nb) { free(buf); return sp_str_empty; } buf = nb; } size_t got = fread(buf + len, 1, 4096, f->fp); len += got; if (got < 4096) break; } char *r = sp_str_alloc(len); memcpy(r, buf, len); r[len] = 0; free(buf); return r;}static inline mrb_bool sp_File_eof_p(sp_File *f) {spinel_analyze.rbmodifiedView file
Loading diff…
spinel_codegen.rbmodifiedView file
Loading diff…