diff --git a/internal/vm/argf.go b/internal/vm/argf.go new file mode 100644 index 00000000..0c440412 --- /dev/null +++ b/internal/vm/argf.go @@ -0,0 +1,275 @@ +// Copyright (c) the go-embedded-ruby/ruby authors +// +// SPDX-License-Identifier: BSD-3-Clause + +package vm + +import "github.com/go-embedded-ruby/ruby/internal/object" + +// argfObj backs the ARGF stream ($< / ARGF): a virtual concatenation of the +// files named in ARGV (falling back to $stdin when ARGV is empty), read as one +// continuous stream. It consumes ARGV — each filename is shifted off as it is +// opened — and tracks a cumulative line number ($.). +type argfObj struct { + vm *VM + cur *IOObj // the stream currently being read, nil before the first read + started bool // whether any stream has been selected yet + lineno int // cumulative line count across all files ($.) + curName string // the current file's name ("-" for $stdin) + files []string // pending filenames for an ARGF.class.new(*files) instance + fromARGV bool // the singleton ARGF: draw filenames from the live ARGV +} + +// nextName shifts and returns the next input filename (from the live ARGV for the +// singleton, else from the instance's own list), or "", false when none remain. +func (a *argfObj) nextName() (string, bool) { + if a.fromARGV { + if argv, ok := a.vm.consts["ARGV"].(*object.Array); ok && len(argv.Elems) > 0 { + name := "" + if s, ok := argv.Elems[0].(*object.String); ok { + name = s.Str() + } + argv.Elems = argv.Elems[1:] + return name, true + } + return "", false + } + if len(a.files) > 0 { + name := a.files[0] + a.files = a.files[1:] + return name, true + } + return "", false +} + +func (a *argfObj) ToS() string { return "ARGF" } +func (a *argfObj) Inspect() string { return "ARGF" } +func (a *argfObj) Truthy() bool { return true } + +// ensure returns the stream that still has bytes to read, advancing to the next +// ARGV file (or $stdin, once, when ARGV is empty) as each is exhausted. It +// returns nil when every input has been consumed. +func (a *argfObj) ensure() *IOObj { + for { + if a.cur != nil { + a.cur.pipeRefresh() + if a.cur.pos < len(a.cur.buf) { + return a.cur + } + } + if name, ok := a.nextName(); ok { + a.cur = openFileIO(a.vm.consts["File"].(*RClass), name, "r") + a.curName = name + a.started = true + continue + } + if !a.started { + a.cur, _ = a.vm.globals["$stdin"].(*IOObj) + a.curName = "-" + a.started = true + if a.cur != nil { + continue + } + } + return nil + } +} + +// bumpLine advances ARGF's line counter and mirrors it into $. as MRI does. +func (a *argfObj) bumpLine() { + a.lineno++ + a.vm.globals["$."] = object.IntValue(int64(a.lineno)) +} + +// registerARGF installs the ARGF constant (and the $< / $stdin-style globals) +// with the core reading protocol. It runs after registerIO so File and $stdin +// exist. +func (vm *VM) registerARGF() { + cls := newClass("ARGF.class", vm.cObject) + vm.cARGF = cls + a := &argfObj{vm: vm, fromARGV: true} + vm.consts["ARGF"] = a + vm.globals["$<"] = a + + // ARGF.class.new(*filenames): a fresh ARGF reading exactly the given files + // (used by the spec harness); no arguments reads $stdin. + cls.smethods["new"] = &Method{name: "new", owner: cls, native: func(vm *VM, _ object.Value, args []object.Value, _ *Proc) object.Value { + files := make([]string, 0, len(args)) + for _, v := range args { + if s, ok := v.(*object.String); ok { + files = append(files, s.Str()) + } else { + files = append(files, vm.send(v, "to_s", nil, nil).(*object.String).Str()) + } + } + return &argfObj{vm: vm, files: files} + }} + + d := func(name string, fn NativeFn) { cls.define(name, fn) } + self := func(v object.Value) *argfObj { return v.(*argfObj) } + + // read(length = nil): read length bytes (or all remaining when nil) across the + // files. With a length, returns nil once every input is exhausted; with none, + // returns "" at end of input. + d("read", func(vm *VM, v object.Value, args []object.Value, _ *Proc) object.Value { + a := self(v) + length, hasLen := -1, false + if len(args) > 0 && !object.IsNil(args[0]) { + length, hasLen = int(intArg(args[0])), true + } + var out []byte + for { + o := a.ensure() + if o == nil { + break + } + avail := o.buf[o.pos:] + if !hasLen { + out = append(out, avail...) + o.pos = len(o.buf) + continue + } + take := length - len(out) + if take > len(avail) { + take = len(avail) + } + out = append(out, avail[:take]...) + o.pos += take + if len(out) >= length { + break + } + } + if hasLen && len(out) == 0 { + return object.NilV + } + return object.NewString(string(out)) + }) + + // gets(sep = $/, ...): the next line across the files, advancing $. ; nil at + // end of input. + d("gets", func(vm *VM, v object.Value, args []object.Value, _ *Proc) object.Value { + a := self(v) + o := a.ensure() + if o == nil { + return object.NilV + } + // ensure guarantees o has bytes, so ioGets yields a line here. + line := ioGets(o, args) + a.bumpLine() + return line + }) + + // readline: like gets but raises EOFError at end of input. + d("readline", func(vm *VM, v object.Value, args []object.Value, _ *Proc) object.Value { + line := vm.send(v, "gets", args, nil) + if object.IsNil(line) { + raise("EOFError", "end of file reached") + } + return line + }) + + // each_line / each { |line| … }: yield every line; returns self (or an + // Enumerator with no block). + eachLine := func(vm *VM, v object.Value, args []object.Value, blk *Proc) object.Value { + if blk == nil { + return enumFor(v, "each_line", args...) + } + for { + line := vm.send(v, "gets", args, nil) + if object.IsNil(line) { + return v + } + vm.callBlock(blk, []object.Value{line}) + } + } + d("each_line", eachLine) + d("each", eachLine) + + // readlines / to_a: every remaining line as an Array. + readlines := func(vm *VM, v object.Value, args []object.Value, _ *Proc) object.Value { + out := object.NewArray() + for { + line := vm.send(v, "gets", args, nil) + if object.IsNil(line) { + return out + } + out.Elems = append(out.Elems, line) + } + } + d("readlines", readlines) + d("to_a", readlines) + + // eof? / eof: true when the current stream is exhausted and no file remains. + eof := func(vm *VM, v object.Value, _ []object.Value, _ *Proc) object.Value { + return object.Bool(self(v).ensure() == nil) + } + d("eof?", eof) + d("eof", eof) + + // lineno / lineno=: the cumulative line number ($.). + d("lineno", func(vm *VM, v object.Value, _ []object.Value, _ *Proc) object.Value { + return object.IntValue(int64(self(v).lineno)) + }) + d("lineno=", func(vm *VM, v object.Value, args []object.Value, _ *Proc) object.Value { + self(v).lineno = int(intArg(args[0])) + return args[0] + }) + + // filename / path: the name of the file currently being read ("-" for stdin), + // selecting the first input if reading has not started. + filename := func(vm *VM, v object.Value, _ []object.Value, _ *Proc) object.Value { + a := self(v) + if !a.started { + a.ensure() // selects the first input, setting curName + } + return object.NewString(a.curName) + } + d("filename", filename) + d("path", filename) + + // to_io / file: the underlying IO of the current file. + d("to_io", func(vm *VM, v object.Value, _ []object.Value, _ *Proc) object.Value { + a := self(v) + if o := a.ensure(); o != nil { + return o + } + return object.NilV + }) + + // #inspect / #to_s ("ARGF") come from the argfObj ToS/Inspect via Object's + // default display path. + + // argv: the (mutating) ARGV array ARGF draws its inputs from. + d("argv", func(vm *VM, _ object.Value, _ []object.Value, _ *Proc) object.Value { + return vm.consts["ARGV"] + }) + // close closes the current file and drops any remaining inputs; skip abandons + // the current file so the next read starts on the following one. binmode is a + // no-op (rbgo reads bytes already). Each returns ARGF. + d("close", func(vm *VM, v object.Value, _ []object.Value, _ *Proc) object.Value { + a := self(v) + a.cur, a.files, a.started = nil, nil, true + return v + }) + d("skip", func(vm *VM, v object.Value, _ []object.Value, _ *Proc) object.Value { + self(v).cur = nil + return v + }) + d("binmode", func(vm *VM, v object.Value, _ []object.Value, _ *Proc) object.Value { return v }) + + // getc / readchar: the next character across the files. + d("getc", func(vm *VM, v object.Value, _ []object.Value, _ *Proc) object.Value { + o := self(v).ensure() + if o == nil { + return object.NilV + } + return vm.send(o, "getc", nil, nil) + }) + d("readchar", func(vm *VM, v object.Value, _ []object.Value, _ *Proc) object.Value { + c := vm.send(v, "getc", nil, nil) + if object.IsNil(c) { + raise("EOFError", "end of file reached") + } + return c + }) +} diff --git a/internal/vm/argf_test.go b/internal/vm/argf_test.go new file mode 100644 index 00000000..f12b2cb6 --- /dev/null +++ b/internal/vm/argf_test.go @@ -0,0 +1,70 @@ +// Copyright (c) the go-embedded-ruby/ruby authors +// +// SPDX-License-Identifier: BSD-3-Clause + +package vm_test + +import "testing" + +// TestARGF covers the ARGF reading protocol over a fresh ARGF.class.new(*files) +// instance (files created under Dir.mktmpdir for portability) and the singleton +// ARGF drawing from ARGV. Asserted against MRI Ruby 4.0.6. +func TestARGF(t *testing.T) { + // setup builds two files "l1\nl2\n" and "l3\n" and binds them into `code`. + with := func(code string) string { + return `require "tmpdir" +Dir.mktmpdir do |d| + f1 = File.join(d, "a"); f2 = File.join(d, "b") + File.write(f1, "l1\nl2\n"); File.write(f2, "l3\n") + ` + code + ` +end` + } + cases := []struct{ src, want string }{ + // read: whole stream, a length, then nil past EOF. + {with(`a = ARGF.class.new(f1, f2); p a.read`), "\"l1\\nl2\\nl3\\n\"\n"}, + {with(`a = ARGF.class.new(f1, f2); p [a.read(3), a.read(3)]`), "[\"l1\\n\", \"l2\\n\"]\n"}, + {with(`a = ARGF.class.new(f1); a.read; p a.read(5)`), "nil\n"}, + {with(`a = ARGF.class.new(f1); a.read; p a.read`), "\"\"\n"}, + // gets / lineno / readline / readlines / each_line. + {with(`a = ARGF.class.new(f1, f2); ls = []; while l = a.gets; ls << l; end; p [ls, a.lineno]`), "[[\"l1\\n\", \"l2\\n\", \"l3\\n\"], 3]\n"}, + {with(`a = ARGF.class.new(f1, f2); p a.readlines`), "[\"l1\\n\", \"l2\\n\", \"l3\\n\"]\n"}, + {with(`a = ARGF.class.new(f1); p a.to_a`), "[\"l1\\n\", \"l2\\n\"]\n"}, + {with(`a = ARGF.class.new(f1); out = []; a.each_line { |l| out << l.chomp }; p out`), "[\"l1\", \"l2\"]\n"}, + {with(`a = ARGF.class.new(f1); p a.each_line.class`), "Enumerator\n"}, + {with(`a = ARGF.class.new(f2); p [a.readline, (a.readline rescue :eof)]`), "[\"l3\\n\", :eof]\n"}, + // getc / readchar. + {with(`a = ARGF.class.new(f2); p [a.getc, a.getc]`), "[\"l\", \"3\"]\n"}, + {with(`a = ARGF.class.new(f2); a.read; p (a.readchar rescue :eof)`), ":eof\n"}, + // eof?, filename/path, to_io, lineno=, skip, close, binmode, inspect. + {with(`a = ARGF.class.new(f1); a.read; p a.eof?`), "true\n"}, + {with(`a = ARGF.class.new(f1, f2); a.gets; p [a.filename == f1, a.path == f1]`), "[true, true]\n"}, + {with(`a = ARGF.class.new(f1); p a.to_io.is_a?(IO)`), "true\n"}, + {with(`a = ARGF.class.new(f1); a.lineno = 10; p a.lineno`), "10\n"}, + {with(`a = ARGF.class.new(f1, f2); a.gets; a.skip; p a.gets`), "\"l3\\n\"\n"}, + {with(`a = ARGF.class.new(f1); a.close; p a.eof?`), "true\n"}, + {with(`a = ARGF.class.new(f1); p a.binmode.equal?(a)`), "true\n"}, + {with(`a = ARGF.class.new(f1); p [a.inspect, a.to_s]`), "[\"ARGF\", \"ARGF\"]\n"}, + // The singleton ARGF draws from ARGV, and #argv returns it. + {with(`ARGV.replace([f1, f2]); p [ARGF.read, ARGF.argv]`), "[\"l1\\nl2\\nl3\\n\", []]\n"}, + {`p ARGF.class.new.class.name`, "\"ARGF.class\"\n"}, + // Native display / truthiness (ToS via interpolation, Inspect via Array, Truthy). + {with(`p "x#{ARGF.class.new(f1)}"`), "\"xARGF\"\n"}, + {with(`p [ARGF.class.new(f1)]`), "[ARGF]\n"}, + {`p(ARGF ? :y : :n)`, ":y\n"}, + // A non-String filename is coerced with #to_s. + {with(`class P; def initialize(p); @p = p; end; def to_s; @p; end; end; p ARGF.class.new(P.new(f1)).read`), "\"l1\\nl2\\n\"\n"}, + // The singleton falling back to (empty, in-test) $stdin when ARGV is empty. + {`ARGV.replace([]); p ARGF.gets`, "nil\n"}, + // read(len) spanning a file boundary; readchar returning a char; to_io and + // filename at their edges. + {with(`a = ARGF.class.new(f2, f1); p a.read(5)`), "\"l3\\nl1\"\n"}, + {with(`p ARGF.class.new(f2).readchar`), "\"l\"\n"}, + {with(`a = ARGF.class.new(f1); a.read; p a.to_io`), "nil\n"}, + {with(`p ARGF.class.new(f1).filename == f1`), "true\n"}, + } + for _, c := range cases { + if got := eval(t, c.src); got != c.want { + t.Errorf("src=%q\n got=%q\nwant=%q", c.src, got, c.want) + } + } +} diff --git a/internal/vm/builtins.go b/internal/vm/builtins.go index d4f0dced..a888f64e 100644 --- a/internal/vm/builtins.go +++ b/internal/vm/builtins.go @@ -507,6 +507,7 @@ func (vm *VM) bootstrap() { vm.registerFile() // needs the exception hierarchy (Errno::ENOENT < StandardError) vm.registerIO() // IO/StringIO + $stdout/$stderr/$stdin (needs IOError/EOFError) + vm.registerARGF() // ARGF / $< — the concatenated ARGV input stream (needs File + $stdin) vm.registerIOBuffer() // IO::Buffer (in-memory byte buffer); after registerIO so IO exists vm.registerDir() // Dir (reuses the Errno module set up by registerFile) vm.registerTmpdir() // Dir.tmpdir / Dir.mktmpdir (layers onto Dir; require "tmpdir") diff --git a/internal/vm/object_model.go b/internal/vm/object_model.go index 69f99f63..e457d592 100644 --- a/internal/vm/object_model.go +++ b/internal/vm/object_model.go @@ -1460,6 +1460,8 @@ func (vm *VM) classOf(v object.Value) *RClass { return vm.consts["ConditionVariable"].(*RClass) case *IOObj: return x.cls + case *argfObj: + return vm.cARGF case *RelineHistory: return x.cls case *tcpSocket: diff --git a/internal/vm/vm.go b/internal/vm/vm.go index eca62a4c..7567c636 100644 --- a/internal/vm/vm.go +++ b/internal/vm/vm.go @@ -386,6 +386,7 @@ type VM struct { cEnumeratorChain *RClass cEnumeratorProduct *RClass cIOBuffer *RClass + cARGF *RClass cYielder *RClass cWeakMap *RClass cWeakKeyMap *RClass diff --git a/scripts/conformance/rubyspec/FLOOR b/scripts/conformance/rubyspec/FLOOR index 8d5d2793..accc2d5c 100644 --- a/scripts/conformance/rubyspec/FLOOR +++ b/scripts/conformance/rubyspec/FLOOR @@ -1 +1 @@ -16095 +16150 diff --git a/scripts/conformance/rubyspec/spec_helper.rb b/scripts/conformance/rubyspec/spec_helper.rb index 7e54cddd..93426777 100644 --- a/scripts/conformance/rubyspec/spec_helper.rb +++ b/scripts/conformance/rubyspec/spec_helper.rb @@ -192,6 +192,16 @@ def new_io(name, mode = "w:utf-8") def new_fd(name, mode = "w:utf-8") File.open(name, mode).fileno end +# mspec's ARGF helper: bind @argf to a fresh ARGF reading the given files for the +# duration of the block. +def argf(argv) + @argf = ARGF.class.new(*argv) + begin + yield + ensure + @argf = nil + end +end def infinity_value; 1.0/0.0; end def nan_value; 0.0/0.0; end def bignum_value(plus = 0); (2**64) + plus; end