Commit ab5c2555
ab5c2555097bMerged
Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 13 files · +6 added · ~7 modified
Description
runtime: implement ARGF (read ARGV files or stdin)
ARGF was an undefined constant, so it resolved to Integer and every
method on it emitted the int-fallback 0 (ARGF.class printed Integer).
Add a real ARGF: the constant is a TY_ARGF singleton (a marker pointer to
a global state), and a runtime that reads the files named in ARGV in
sequence -- or stdin when ARGV is empty (a - filename also means stdin).
Supported methods: read, gets / readline, readlines / to_a, each_line /
each (block), filename / path, eof? / eof, to_s, and class (its singleton
class name "ARGF.class", matching CRuby). The dispatch mirrors the
existing TY_IO handling; the type plumbing follows the TY_RANDOM template
(ctype, default, scalar-ret, class-name).
Tests: reading a file passed in ARGV (filename + each_line), and the no-args class/filename shape (stdin "-").
Fixes #1157
Details
- Commit
- ab5c2555097b
- Parent
- 9d9a70880703
lib/sp_runtime.hmodifiedView file
return sp_argv_array_cache;}/* ARGF: a pseudo-IO that reads the files named in ARGV in sequence, or stdin when ARGV is empty (a `-` filename also means stdin). The state is a single global; the ARGF constant is a marker pointer to it. */typedef struct { mrb_int idx; FILE *cur; int started; const char *fname; } sp_Argf;static sp_Argf sp_argf_obj = {0, NULL, 0, NULL};/* Ensure a current readable stream, or return 0 at total end of input. */static int sp_argf_ensure(void) { if (sp_argf_obj.cur) return 1; if (sp_argv.len == 0) { if (sp_argf_obj.started) return 0; sp_argf_obj.started = 1; sp_argf_obj.cur = stdin; sp_argf_obj.fname = "-"; return 1; } while (sp_argf_obj.idx < sp_argv.len) { const char *fn = sp_argv.data[sp_argf_obj.idx++]; sp_argf_obj.started = 1; if (fn && fn[0] == '-' && fn[1] == 0) { sp_argf_obj.cur = stdin; sp_argf_obj.fname = "-"; return 1; } FILE *f = fn ? fopen(fn, "r") : NULL; if (f) { sp_argf_obj.cur = f; sp_argf_obj.fname = fn; return 1; } } return 0;}static const char *sp_argf_gets(void) { for (;;) { if (!sp_argf_ensure()) return NULL; char buf[8192]; if (fgets(buf, sizeof buf, sp_argf_obj.cur)) { size_t l = strlen(buf); char *r = sp_str_alloc_raw(l + 1); memcpy(r, buf, l + 1); return r; } if (sp_argf_obj.cur && sp_argf_obj.cur != stdin) fclose(sp_argf_obj.cur); sp_argf_obj.cur = NULL; /* EOF on this stream; advance on next ensure */ }}static const char *sp_argf_read(void) { sp_String *s = sp_String_new(""); SP_GC_ROOT(s); const char *line; while ((line = sp_argf_gets())) sp_String_append(s, line); return s->data;}static sp_StrArray *sp_argf_readlines(void) { sp_StrArray *a = sp_StrArray_new(); SP_GC_ROOT(a); const char *line; while ((line = sp_argf_gets())) sp_StrArray_push(a, line); return a;}static const char *sp_argf_filename(void) { if (sp_argf_obj.fname) return sp_argf_obj.fname; return sp_argv.len > 0 ? sp_argv.data[0] : "-";}static mrb_bool sp_argf_eof(void) { return !sp_argf_ensure(); }/* Mark active in-flight exception messages. Most raises pass string literals (rodata, marker byte ≠ 0xfe → no-op for sp_mark_string), but raises that build messages dynamically via sp_sprintf (e.g.src/analyze_infer.cmodifiedView file
Loading diff…
src/codegen.cmodifiedView file
Loading diff…
src/codegen_call.cmodifiedView file
Loading diff…
src/codegen_expr.cmodifiedView file
Loading diff…
src/codegen_util.cmodifiedView file
Loading diff…
src/types.hmodifiedView file
Loading diff…
test/argf_class_no_args.rbaddedView file
Loading diff…
test/argf_class_no_args.rb.expectedaddedView file
Loading diff…
test/argf_input.txtaddedView file
Loading diff…
test/argf_reads_args.rbaddedView file
Loading diff…
test/argf_reads_args.rb.argsaddedView file
Loading diff…
test/argf_reads_args.rb.expectedaddedView file
Loading diff…