Commit c4bf8da0

c4bf8da0eb94Merged

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

Description

runtime,analyze,codegen: non-block File.open with sp_File handle

File.open(path, mode) without a block silently returned 0; the follow-up f.write / f.close then no-opped on an int receiver ("cannot resolve call to 'write' on int") and the file never reached disk. The block form already worked.

Add an sp_File wrapper (GC-managed FILE*, finalizer fclose()s any still-open fp) plus open/close/write/puts/print/flush/gets/read/eof?/ closed?/path helpers. Add a "file" type to analyze with return-type arms for each method; codegen dispatches on recv_type == "file". File.open at the constant-call site now returns sp_File* in expr context (the existing block-form path in compile_file_open_call_stmt is unchanged).

Fixes #1006

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

Details

Commit
c4bf8da0eb94
Parent
bde42a0d889b

lib/sp_runtime.hmodifiedView file

@@ -1768,6 +1768,82 @@
17681768 static inline const char*sp_String_cstr(sp_String*s){return s->data;}
17691769 static inline int64_t sp_String_length(sp_String*s){return s->len;}
17701770 static sp_String*sp_String_dup(sp_String*s){return sp_String_new(s->data);}
1771+
1772+/* `File.open(path, mode)` without a block returns an sp_File * — a
1773+ GC-managed wrapper around `FILE *fp`. The finalizer fclose()s any
1774+ still-open fp so a dropped file handle doesn't leak. `path` and
1775+ `mode` are kept live for `f.path` / `f.mode` introspection; they
1776+ come from the caller's already-live string slot so no extra mark
1777+ is needed. */
1778+typedef struct { FILE *fp; const char *path; const char *mode; } sp_File;
1779+static void sp_File_fin(void *p) { sp_File *f = (sp_File *)p; if (f->fp) { fclose(f->fp); f->fp = NULL; } }
1780+static void sp_File_scan(void *p) { sp_File *f = (sp_File *)p; if (f->path) sp_mark_string(f->path); if (f->mode) sp_mark_string(f->mode); }
1781+static sp_File *sp_File_open(const char *path, const char *mode) {
1782+ sp_File *f = (sp_File *)sp_gc_alloc(sizeof(sp_File), sp_File_fin, sp_File_scan);
1783+ f->fp = fopen(path ? path : "", mode ? mode : "r");
1784+ if (!f->fp) { sp_raise_cls("Errno::ENOENT", "No such file or directory"); return NULL; }
1785+ f->path = path;
1786+ f->mode = mode;
1787+ return f;
1788+}
1789+static inline mrb_int sp_File_write(sp_File *f, const char *s) {
1790+ if (!f || !f->fp || !s) return 0;
1791+ size_t n = strlen(s);
1792+ return (mrb_int)fwrite(s, 1, n, f->fp);
1793+}
1794+static inline mrb_int sp_File_close(sp_File *f) {
1795+ if (f && f->fp) { fclose(f->fp); f->fp = NULL; }
1796+ return 0;
1797+}
1798+static inline mrb_bool sp_File_closed_p(sp_File *f) {
1799+ return !f || !f->fp;
1800+}
1801+static inline void sp_File_puts(sp_File *f, const char *s) {
1802+ if (!f || !f->fp || !s) return;
1803+ size_t n = strlen(s);
1804+ fputs(s, f->fp);
1805+ if (n == 0 || s[n - 1] != '\n') fputc('\n', f->fp);
1806+}
1807+static inline void sp_File_print(sp_File *f, const char *s) {
1808+ if (!f || !f->fp || !s) return;
1809+ fputs(s, f->fp);
1810+}
1811+static inline mrb_int sp_File_flush(sp_File *f) {
1812+ if (f && f->fp) fflush(f->fp);
1813+ return 0;
1814+}
1815+static inline const char *sp_File_gets(sp_File *f) {
1816+ if (!f || !f->fp) return NULL;
1817+ char buf[65536];
1818+ if (!fgets(buf, (int)sizeof(buf), f->fp)) return NULL;
1819+ size_t n = strlen(buf);
1820+ char *r = sp_str_alloc(n);
1821+ memcpy(r, buf, n);
1822+ return r;
1823+}
1824+static inline const char *sp_File_read(sp_File *f) {
1825+ if (!f || !f->fp) return sp_str_empty;
1826+ long pos = ftell(f->fp);
1827+ if (pos < 0) pos = 0;
1828+ if (fseek(f->fp, 0, SEEK_END) != 0) return sp_str_empty;
1829+ long end = ftell(f->fp);
1830+ fseek(f->fp, pos, SEEK_SET);
1831+ long n = (end > pos) ? (end - pos) : 0;
1832+ if (n <= 0) return sp_str_empty;
1833+ char *r = sp_str_alloc((size_t)n);
1834+ size_t got = fread(r, 1, (size_t)n, f->fp);
1835+ r[got] = 0;
1836+ return r;
1837+}
1838+static inline mrb_bool sp_File_eof_p(sp_File *f) {
1839+ if (!f || !f->fp) return TRUE;
1840+ int c = fgetc(f->fp);
1841+ if (c == EOF) return TRUE;
1842+ ungetc(c, f->fp);
1843+ return FALSE;
1844+}
1845+static inline const char *sp_File_path(sp_File *f) { return f && f->path ? f->path : sp_str_empty; }
1846+
17711847 /* Array#inspect for each typed array: `[elem1, elem2, ...]` with each
17721848 element rendered via its own primitive inspect. Matches CRuby's
17731849 Array#inspect output byte-for-byte. Returns a GC-managed C string. */