Skip to content

io: IO::Buffer transfer/locked/bitwise-bang + IO#advise (core/io +59) - #600

Merged
tannevaled merged 2 commits into
mainfrom
wave21/io
Sep 8, 2026
Merged

tannevaled merged 2 commits into
mainfrom
wave21/io

Conversation

@tannevaled

Copy link
Copy Markdown
Contributor

Wave-21 IO conformance against MRI Ruby 4.0.x. Two self-contained clusters in the IO layer, each verified byte-for-byte against ruby 4.0.5 and backed by whitebox tests at 100% coverage.

IO::Buffer (internal/vm/io_buffer.go)

References: MRI io_buffer.c (io_buffer_transfer, io_buffer_locked, io_buffer_and/or/xor/not!, io_buffer_for, io_buffer_initialize/resize, io_buffer_valid_p) and ruby/spec core/io/buffer/{and,or,xor,not,transfer,locked,valid,resize,for,null,initialize}_spec.rb.

  • Added exceptions IO::Buffer::LockedError / InvalidatedError / AllocationError (RuntimeError subclasses).
  • In-place bitwise #and!/#or!/#xor!/#not! (mutate the receiver, return self; AccessError on a read-only buffer). The binary &/|/^ argument-type error now reads wrong argument type X (expected IO::Buffer) (nil/true/false lower-cased), matching MRI.
  • #transfer: moves memory and kind (external/read-only/slice) to a fresh buffer and nullifies the source; refused while locked.
  • #locked / #locked?: locks the buffer for the block; #resize/#transfer raise LockedError inside, re-entry raises Buffer already locked!; the lock does not propagate to or from slices.
  • IO::Buffer.for over a mutable String (with a block) now aliases the String's bytes and is writable; a frozen source or the no-block form stays a read-only copy.
  • .new/#resize size validation: non-Integer -> TypeError "not an Integer", negative -> ArgumentError "Size can't be negative!"; #resize(0) frees the buffer, and a freed external buffer may be re-grown.
  • #valid? now reports slice invalidation (freed/transferred/resized-out source), while a slice of a String-backed buffer stays valid after the buffer is freed; operations on an invalidated slice raise InvalidatedError "Buffer has been invalidated!".

IO#advise (internal/vm/io.go)

References: MRI io.c rb_io_advise + advice_arg_check, ruby/spec core/io/advise_spec.rb. rbgo has no posix_fadvise (as MRI on a platform without it), so #advise validates its arguments and returns nil: non-Symbol -> TypeError; unrecognized -> NotImplementedError "Unsupported advice: :sym"; closed stream -> IOError; offset/len coerced as off_t (TypeError/RangeError); wrong arity -> ArgumentError. advise_spec goes 0 -> 13.

Measured delta (core/io ruby/spec)

  • pass 1090 -> 1149 (+59), fail 180 -> 161 (-19), error 159 -> 119 (-40).

Gates

  • go build ./... and GOWORK=off go test ./... green (pre-existing 127.0.0.1:1 network tests excluded).
  • 100% coverage on POSIX for io_buffer.go and io.go (go tool cover -func shows nothing below 100 introduced).
  • gofmt -l internal/vm/ empty; wasip1 build clean.
  • Edits confined to the wave's exclusive files (io.go, io_buffer.go) plus new _test.go; FLOOR untouched.

🤖 Generated with Claude Code

tannevaled and others added 2 commits September 8, 2026 20:26
…lidation (core/io +46)

Advance IO::Buffer conformance against MRI 4.0.x. References read: MRI
io_buffer.c (io_buffer_transfer, io_buffer_locked, io_buffer_and/or/xor/not!,
io_buffer_for, io_buffer_initialize/resize, io_buffer_valid_p) and ruby/spec
core/io/buffer/{and,or,xor,not,transfer,locked,valid,resize,for,null,
initialize}_spec.rb. Every value below verified byte-for-byte against
ruby 4.0.5.

New / fixed:
- Exceptions IO::Buffer::LockedError / InvalidatedError / AllocationError
  (all RuntimeError subclasses).
- In-place bitwise operators #and!/#or!/#xor!/#not! (mutate the receiver,
  return self; AccessError on a read-only buffer). The binary &/|/^ argument
  type error now reads "wrong argument type X (expected IO::Buffer)"
  (nil/true/false lower-cased), matching MRI.
- #transfer: moves memory and kind (external/read-only/slice) to a fresh
  buffer and nullifies the source; refused while locked with LockedError.
- #locked / #locked?: locks the buffer for the block, #resize and #transfer
  raise LockedError inside, re-entry raises "Buffer already locked!"; the lock
  does not propagate to or from slices.
- IO::Buffer.for over a *mutable* String (with a block) now aliases the
  String's bytes and is writable (writes propagate); a frozen source or the
  no-block form stays a read-only copy.
- .new / #resize size validation: non-Integer -> TypeError "not an Integer",
  negative -> ArgumentError "Size can't be negative!"; #resize(0) frees the
  buffer, and a freed external buffer may be re-grown into an internal one.
- #valid? now reports slice invalidation: a slice invalidates when its source
  is freed, transferred, or resized out from under it, but a slice of a
  String-backed buffer stays valid after the buffer is freed; operations on an
  invalidated slice raise InvalidatedError "Buffer has been invalidated!".

core/io: pass 1090->1136, fail 180->168, error 159->125. Full whitebox
coverage (100% on io_buffer.go); go build/test ./..., wasip1 build, and
gofmt all clean.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Reference: MRI io.c rb_io_advise + advice_arg_check, and ruby/spec
core/io/advise_spec.rb. rbgo has no posix_fadvise (as MRI on a platform
without it), so #advise validates its arguments and returns nil. Checks, in
MRI's order and verified byte-for-byte against ruby 4.0.5 on darwin:
- a non-Symbol advice raises TypeError "advice must be a Symbol";
- an unrecognized advice raises NotImplementedError "Unsupported advice: :sym"
  (recognized: :normal :sequential :random :willneed :dontneed :noreuse);
- a closed stream raises IOError "closed stream";
- offset and len are coerced as off_t (reusing ioOfftArg): a non-Integer
  raises TypeError, a too-large Bignum RangeError;
- wrong arity (0 or >3 args) raises ArgumentError.

advise_spec: 13 examples now all pass (was 6 error + 7 fail). Whitebox test
covers every branch; io.go stays at 100% coverage, gofmt clean, wasip1 builds.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
@tannevaled
tannevaled merged commit 415efad into main Sep 8, 2026
7 checks passed
@tannevaled
tannevaled deleted the wave21/io branch September 8, 2026 20:33
tannevaled added a commit that referenced this pull request Sep 8, 2026
…602)

Wave-21 merged three bibliography-first conformance PRs:
- #600 core/io: IO::Buffer transfer/locked/and!/or!/xor!/not!/for/resize/valid?,
  IO#advise validated no-op — +59.
- #601 core/file+dir: File.lutime, File.utime nil (UTIME_NOW), Dir.chroot — +10.
- #599 core/kernel: Kernel#loop StopIteration rescue + infinite Enumerator,
  Kernel#fail alias of raise — +7.

Measured total on merged main: 19993 passing. Local measure at TIMEOUT=25;
CI runs TIMEOUT=60 so its total is >= this floor.

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