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
1 change: 1 addition & 0 deletions internal/vm/argf.go
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,7 @@ func (vm *VM) registerARGF() {
d("tell", delegate("tell"))
d("pos=", delegate("pos="))
d("seek", delegate("seek"))
d("readpartial", delegate("readpartial"))
d("fileno", delegate("fileno"))
d("to_i", delegate("fileno"))
d("set_encoding", delegate("set_encoding"))
Expand Down
1 change: 1 addition & 0 deletions internal/vm/argf_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ end`
{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(f1); p a.readpartial(3)`), "\"l1\\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"},
Expand Down
38 changes: 38 additions & 0 deletions internal/vm/io.go
Original file line number Diff line number Diff line change
Expand Up @@ -709,6 +709,44 @@ func defStringIORead(cls *RClass) {
o.pos = len(o.buf)
return result(data, false)
})
// readpartial(maxlen, out = nil): read up to maxlen bytes, blocking only until
// some are available. On the in-memory buffer that is the leading maxlen bytes;
// a zero maxlen yields ""; reading at end of input raises EOFError.
cls.define("readpartial", func(_ *VM, self object.Value, args []object.Value, _ *Proc) object.Value {
o := self.(*IOObj)
ioCheckReadable(o)
o.pipeRefresh()
n := int(toInt(args[0]))
if n < 0 {
raise("ArgumentError", "negative length %d given", n)
}
var buf *object.String
if len(args) > 1 {
if b, ok := args[1].(*object.String); ok {
buf = b
}
}
fill := func(data []byte) object.Value {
if buf != nil {
if buf.Frozen {
raise("FrozenError", "can't modify frozen String: %s", buf.Inspect())
}
buf.SetBytes(append([]byte(nil), data...))
return buf
}
return object.NewStringBytes(append([]byte(nil), data...))
}
if n == 0 {
return fill(nil)
}
if o.pos >= len(o.buf) {
raise("EOFError", "end of file reached")
}
end := min(o.pos+n, len(o.buf))
data := o.buf[o.pos:end]
o.pos = end
return fill(data)
})
cls.define("getc", func(_ *VM, self object.Value, _ []object.Value, _ *Proc) object.Value {
o := self.(*IOObj)
ioCheckReadable(o)
Expand Down
21 changes: 21 additions & 0 deletions internal/vm/io_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,24 @@ func TestIOFileno(t *testing.T) {
}
}
}

// TestIOReadpartial covers StringIO#readpartial: reading up to a length, the
// output-buffer form, the zero-length "" case, EOFError at end of input, a
// negative length ArgumentError and IOError on a closed stream. Asserted against
// MRI Ruby 4.0.6.
func TestIOReadpartial(t *testing.T) {
cases := []struct{ src, want string }{
{`require "stringio"; s = StringIO.new("hello world"); p [s.readpartial(5), s.readpartial(100)]`, "[\"hello\", \" world\"]\n"},
{`require "stringio"; s = StringIO.new("abc"); buf = "x"; r = s.readpartial(2, buf); p [r, buf.equal?(r)]`, "[\"ab\", true]\n"},
{`require "stringio"; p StringIO.new("abc").readpartial(0)`, "\"\"\n"},
{`require "stringio"; s = StringIO.new("hi"); s.readpartial(2); begin; s.readpartial(5); rescue EOFError => e; p e.class; end`, "EOFError\n"},
{`require "stringio"; begin; StringIO.new("x").readpartial(-1); rescue ArgumentError => e; p e.class; end`, "ArgumentError\n"},
{`require "stringio"; s = StringIO.new("x"); s.close; begin; s.readpartial(5); rescue IOError => e; p e.class; end`, "IOError\n"},
{`require "stringio"; s = StringIO.new("hi"); begin; s.readpartial(2, "z".freeze); rescue FrozenError => e; p e.class; end`, "FrozenError\n"},
}
for _, c := range cases {
if got := eval(t, c.src); got != c.want {
t.Errorf("src=%q got=%q want=%q", c.src, got, c.want)
}
}
}
2 changes: 1 addition & 1 deletion scripts/conformance/rubyspec/FLOOR
Original file line number Diff line number Diff line change
@@ -1 +1 @@
16282
16285