diff --git a/internal/vm/argf.go b/internal/vm/argf.go index a730bb00..21c61eec 100644 --- a/internal/vm/argf.go +++ b/internal/vm/argf.go @@ -75,6 +75,16 @@ func (a *argfObj) ensure() *IOObj { } } +// curIO returns the current stream (selecting the first input if reading has not +// started), even when it is at EOF — unlike ensure, which skips past an exhausted +// file. It is nil only when there is no input at all. +func (a *argfObj) curIO() *IOObj { + if !a.started { + a.ensure() + } + return a.cur +} + // bumpLine advances ARGF's line counter and mirrors it into $. as MRI does. func (a *argfObj) bumpLine() { a.lineno++ @@ -311,6 +321,39 @@ func (vm *VM) registerARGF() { return vm.send(c, "ord", nil, nil) })) + // pos / tell / pos= / seek / fileno / to_i delegate to the current file's IO + // (which carries the byte cursor and descriptor). + delegate := func(meth string) NativeFn { + return func(vm *VM, v object.Value, args []object.Value, _ *Proc) object.Value { + o := self(v).curIO() + if o == nil { + raise("ArgumentError", "no stream") + } + return vm.send(o, meth, args, nil) + } + } + d("pos", delegate("pos")) + d("tell", delegate("tell")) + d("pos=", delegate("pos=")) + d("seek", delegate("seek")) + d("fileno", delegate("fileno")) + d("to_i", delegate("fileno")) + d("set_encoding", delegate("set_encoding")) + d("external_encoding", delegate("external_encoding")) + d("internal_encoding", delegate("internal_encoding")) + // rewind returns the current file to its start and resets the line counter. + d("rewind", func(vm *VM, v object.Value, _ []object.Value, _ *Proc) object.Value { + a := self(v) + o := a.curIO() + if o == nil { + raise("ArgumentError", "no stream to rewind") + } + o.pos = 0 + a.lineno = 0 + a.vm.globals["$."] = object.IntValue(0) + return object.IntValue(0) + }) + // file: the IO of the file currently being read. d("file", func(vm *VM, v object.Value, _ []object.Value, _ *Proc) object.Value { a := self(v) diff --git a/internal/vm/argf_test.go b/internal/vm/argf_test.go index a6a51628..13d024f0 100644 --- a/internal/vm/argf_test.go +++ b/internal/vm/argf_test.go @@ -76,6 +76,16 @@ end` {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"}, + // pos / tell / seek / rewind / fileno / encodings delegate to the current file. + {with(`a = ARGF.class.new(f1); a.gets; p [a.pos, a.tell]`), "[3, 3]\n"}, + {with(`a = ARGF.class.new(f1); a.gets; a.rewind; p [a.gets, a.lineno]`), "[\"l1\\n\", 1]\n"}, + {with(`a = ARGF.class.new(f2); a.seek(1); p a.read`), "\"3\\n\"\n"}, + {with(`a = ARGF.class.new(f2); a.pos = 2; p a.read`), "\"\\n\"\n"}, + {with(`a = ARGF.class.new(f1); p [a.fileno.is_a?(Integer), a.to_i.is_a?(Integer)]`), "[true, true]\n"}, + {with(`a = ARGF.class.new(f1); a.set_encoding("UTF-8", "ISO-8859-1"); p [a.external_encoding.to_s, a.internal_encoding.to_s]`), "[\"UTF-8\", \"ISO-8859-1\"]\n"}, + // After close there is no stream, so a delegating call raises. + {with(`a = ARGF.class.new(f1); a.close; p(a.pos rescue :err)`), ":err\n"}, + {with(`a = ARGF.class.new(f1); a.close; p(a.rewind rescue :err)`), ":err\n"}, } for _, c := range cases { if got := eval(t, c.src); got != c.want { diff --git a/scripts/conformance/rubyspec/FLOOR b/scripts/conformance/rubyspec/FLOOR index a7b654a4..b02867d9 100644 --- a/scripts/conformance/rubyspec/FLOOR +++ b/scripts/conformance/rubyspec/FLOOR @@ -1 +1 @@ -16240 +16250