Commit e8d0bd9c

e8d0bd9c0ad2Merged

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

Description

Add TY_IO type, File class methods, and IO instance method dispatch

  • Add TY_IO type for sp_File * handles with SP_BUILTIN_IO boxing
  • Pin File.open/new block param to TY_IO in analyze (enables f.write, f.puts, etc.)
  • Pin IO each_line block param to TY_STRING in analyze
  • Fix File.open block form: use emit_stmt for void/nil last expressions (prevents g_pre ordering bugs when f.print is the last body stmt)
  • Add TY_IO/TY_FIBER to emit_boxed for poly-context boxing
  • Add TY_IO each_line dispatch: emits C while(sp_File_gets) loop
  • Add File.readlines class method (sp_file_readlines / sp_file_readlines_chomp)
  • Add sp_File_readlines instance method
  • Add File.join class method
  • Add sp_file_join, sp_file_readlines, sp_file_readlines_chomp to sp_runtime.h

Fixes: file_read_count, file_readlines, i1006 (already fixed)

Co-Authored-By: Claude Sonnet 4.6 [email protected]

Details

Commit
e8d0bd9c0ad2
Parent
7acdafdd6af6

lib/sp_runtime.hmodifiedView file

@@ -2043,6 +2043,57 @@
20432043 return s;
20442044 }
20452045 static inline const char *sp_File_path(sp_File *f) { return f && f->path ? f->path : sp_str_empty; }
2046+static const char *sp_file_join(const char **parts, int n) {
2047+ size_t total = 0;
2048+ for (int i = 0; i < n; i++) {
2049+ if (parts[i]) total += strlen(parts[i]);
2050+ if (i < n - 1) total++;
2051+ }
2052+ char *r = sp_str_alloc((mrb_int)total);
2053+ size_t off = 0;
2054+ for (int i = 0; i < n; i++) {
2055+ if (parts[i]) { size_t l = strlen(parts[i]); memcpy(r + off, parts[i], l); off += l; }
2056+ if (i < n - 1) r[off++] = '/';
2057+ }
2058+ r[off] = 0;
2059+ return r;
2060+}
2061+static inline sp_StrArray *sp_File_readlines(sp_File *f) {
2062+ sp_StrArray *a = sp_StrArray_new();
2063+ const char *line;
2064+ while ((line = sp_File_gets(f)) != NULL) sp_StrArray_push(a, line);
2065+ return a;
2066+}
2067+static sp_StrArray *sp_file_readlines(const char *path) {
2068+ sp_StrArray *a = sp_StrArray_new();
2069+ FILE *_fp = fopen(path ? path : "", "r");
2070+ if (!_fp) return a;
2071+ char _buf[4096];
2072+ while (fgets(_buf, (int)sizeof(_buf), _fp)) {
2073+ size_t _l = strlen(_buf);
2074+ char *_r = sp_str_alloc_raw(_l + 1);
2075+ memcpy(_r, _buf, _l + 1);
2076+ sp_StrArray_push(a, _r);
2077+ }
2078+ fclose(_fp);
2079+ return a;
2080+}
2081+static sp_StrArray *sp_file_readlines_chomp(const char *path) {
2082+ sp_StrArray *a = sp_StrArray_new();
2083+ FILE *_fp = fopen(path ? path : "", "r");
2084+ if (!_fp) return a;
2085+ char _buf[4096];
2086+ while (fgets(_buf, (int)sizeof(_buf), _fp)) {
2087+ size_t _l = strlen(_buf);
2088+ if (_l > 0 && _buf[_l-1] == '\n') { _buf[--_l] = '\0'; }
2089+ if (_l > 0 && _buf[_l-1] == '\r') { _buf[--_l] = '\0'; }
2090+ char *_r = sp_str_alloc_raw(_l + 1);
2091+ memcpy(_r, _buf, _l + 1);
2092+ sp_StrArray_push(a, _r);
2093+ }
2094+ fclose(_fp);
2095+ return a;
2096+}
20462097
20472098 /* Array#inspect for each typed array: `[elem1, elem2, ...]` with each
20482099 element rendered via its own primitive inspect. Matches CRuby's
@@ -2677,6 +2728,7 @@
26772728 #define SP_BUILTIN_POLY_POLY_HASH (-20)
26782729 #define SP_BUILTIN_OBJECT (-21) /* Object.new identity sentinel */
26792730 #define SP_BUILTIN_FIBER (-22) /* sp_Fiber * boxed into poly slot */
2731+#define SP_BUILTIN_IO (-23) /* sp_File * (File/IO handle) boxed into poly slot */
26802732 /* sp_RbVal is defined in sp_gc.h (the mark helpers dispatch on its tag). */
26812733 static sp_RbVal sp_box_int(mrb_int v) { sp_RbVal r; r.tag = SP_TAG_INT; r.cls_id = 0; r.v.i = v; return r; }
26822734 static sp_RbVal sp_box_str(const char *v) { sp_RbVal r; r.tag = SP_TAG_STR; r.cls_id = 0; r.v.s = v; return r; }