Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions internal/vm/argf.go
Original file line number Diff line number Diff line change
Expand Up @@ -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++
Expand Down Expand Up @@ -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)
Expand Down
10 changes: 10 additions & 0 deletions internal/vm/argf_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion scripts/conformance/rubyspec/FLOOR
Original file line number Diff line number Diff line change
@@ -1 +1 @@
16240
16250