Skip to content

io: StringIO/IO reader & writer conformance to MRI 4.0.6 - #536

Merged
tannevaled merged 7 commits into
mainfrom
feat/agent-io
Aug 31, 2026
Merged

tannevaled merged 7 commits into
mainfrom
feat/agent-io

Conversation

@tannevaled

Copy link
Copy Markdown
Contributor

Advances MRI-Ruby-4.0.6 conformance for the buffer-backed IO/StringIO reader and
writer paths. All work is in internal/vm/io.go (plus small call-site edits in
io_class.go, io_descriptors.go, argf.go, irb.go) and a new
io_residuals_test.go.

What changed

  • StringIO.new / File.open modes. StringIO.new honours its mode (r/r+/w/w+/a/a+,
    integer flags, mode:): read-only rejects writes, write-only rejects reads,
    w/w+ truncate, a/a+ append every write at end-of-buffer, a frozen backing
    string opens read-only. File.open enforces the same access half.
  • gets / readline / readlines / each_line / IO.readlines / IO.foreach. One
    separator/limit parser: a non-String separator coerces via #to_str, a
    non-Integer limit via #to_int (each exactly once), the default separator
    follows $/, and a lone #to_int-only argument is the byte limit. Paragraph
    mode ("") honours the limit and chomp:, and keeps the whole newline run for
    StringIO but only two for a real IO/File — matching MRI's divergence.
  • read. read(length) coerces via #to_int, returns an ASCII-8BIT String, and
    accepts a #to_str output buffer; read with no length stays UTF-8; nil-vs-""
    at EOF preserved.
  • write / << / print / puts. write/<< stringify via #to_s; print writes
    $_ with no args and honours $, / $\; puts expands a #to_ary object and now
    emits through the stream's #write (so an overridden #write captures output).
  • Enumerators. each_line/each_char/each_byte with no block return an
    Enumerator (size nil) instead of crashing; #each is a true alias of
    #each_line on both IO and StringIO.

Conformance deltas (rubyspec, pass count)

StringIO: gets 24→30, readline 24→30, readlines 14→17, print 7→11, puts 15→19,
read 15→18, each_char/byte/line and append all green. Core IO: readlines 30→46,
puts 3→14, each_line 19→28, each_char 3→8, gets 26→32, print 2→5.

Verification

go test ./internal/vm/ and go test ./... green; GOOS=wasip1 GOARCH=wasm go build ./...
OK; gofmt clean on changed files; merged coverage total 100.0%.

Out of scope (reported, not fixed): encoding transcoding on read/gets/write,
|command subprocess pipes, C off_t RangeError, newline: CRLF translation.

🤖 Generated with Claude Code

tannevaled and others added 7 commits August 31, 2026 17:29
StringIO.new now honours its mode argument (r/r+/w/w+/a/a+, integer flags,
and mode:) — read-only rejects writes, write-only rejects reads, "w"/"w+"
truncate, "a"/"a+" append every write at end-of-buffer, and a frozen backing
string opens read-only. File.open enforces the same access half.

gets/readline/readlines/each_line coerce a non-String separator via #to_str
and a non-Integer limit via #to_int exactly once per call, default the
separator to $/, and treat a lone #to_int-only argument as the byte limit.
Paragraph mode ("") honours the limit and keeps the whole newline run for
StringIO but only two for a real IO/File, matching MRI's divergence.

read(length) coerces via #to_int, returns an ASCII-8BIT String, and accepts a
#to_str-convertible output buffer. write/<< stringify via #to_s. print writes
$_ with no args and honours $, / $\. puts expands a #to_ary object. #each is
a true alias of #each_line (shared method entry).

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
…ia #write

each_line/each_char/each_byte with no block now return an Enumerator (whose
#size is nil) instead of crashing, and defer the readable check to iteration so
the enumerator is buildable on a closed stream. #each is a real alias of
#each_line on IO as well.

Paragraph mode honours chomp: (stripping the terminating newline run but not a
lone EOF "\n"). IO.readlines/foreach route their separator/limit/chomp/$/ through
the same coercion path as the instance methods, so a #to_str separator, a
#to_int (or Integer) limit, and the $/ default all work.

Kernel#puts and IO#puts emit through the stream's #write method, so a stream
that overrides #write (as the puts specs do) captures the output.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
…ches

Fold the single-shot ioGets (ARGF#gets, IRB reader) onto the shared
resolveGetsArgs/ioGetsResolved path and delete the now-duplicate parseGetsArgs
and the dead checkGetsLimit, so there is one separator/limit/$/-aware parser.

Extend io_residuals_test.go to exercise the new branches: StringIO/File modes,
read coercions and binary encoding, gets/readlines/each_line separator+limit+$/,
paragraph mode (StringIO whole-run vs File two-newline, chomp, leading blanks,
EOF), the no-block Enumerator forms, puts via #write with array flattening and
recursion, print's $_/$,/$\, IO.readlines/foreach, and File read/write gating.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Add a StringIO.gets("") case over a buffer of only blank lines, which the
leading-blank skip consumes to end-of-input, exercising ioGetsParagraph's
end-of-buffer nil return.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Add a File.open("/dev/null", "a") case: a non-regular file opens with an empty
buffer and its write reports the byte count, exercising openFileIO's append-mode
notRegular branch.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
The character-device subcases open "/dev/null", which does not exist on
Windows (its null device is NUL), failing test (windows-latest). Guard the
two device cases behind runtime.GOOS != "windows"; the rest of the mode
gating still runs on every platform.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
@tannevaled
tannevaled merged commit 02dd9b0 into main Aug 31, 2026
7 checks passed
@tannevaled
tannevaled deleted the feat/agent-io branch August 31, 2026 22:25
tannevaled added a commit that referenced this pull request Sep 1, 2026
Locks in #535 (String methods: index/casecmp/chomp/gsub/scan/tr/... ~+110),
#536 (IO/StringIO reader+writer), #538 (Array#[]= auto-grow + mutators),
#539 (Range + Comparable), #540 (Struct + Data), #541 (Time). Measured
17983 passing on the pinned corpus, up from 17654.

Co-authored-by: Claude Opus 4.8 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant