Skip to content

Commit 415efad

Browse files
tannevaledclaude
andauthored
io: IO::Buffer transfer/locked/bitwise-bang + IO#advise (core/io +59) (#600)
* io: IO::Buffer transfer/locked/bitwise-bang/for-writability + size validation (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> * io: IO#advise as a validated no-op (core/io advise_spec 0->13) 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> --------- Co-authored-by: Claude Opus 4.8 <noreply@anthropic.com>
1 parent 94c8a1e commit 415efad

4 files changed

Lines changed: 345 additions & 20 deletions

File tree

‎internal/vm/io.go‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -816,6 +816,36 @@ func defIOWrite(cls *RClass) {
816816
ioClosedRealIO(self.(*IOObj)) // as flush: real IO raises on close, StringIO does not
817817
return object.IntValue(0)
818818
})
819+
// IO#advise(advice, offset = 0, len = 0) (rb_io_advise + advice_arg_check in
820+
// io.c). rbgo has no posix_fadvise, so like MRI on a platform without it the
821+
// call is a validated no-op returning nil. Argument checks, in MRI's order:
822+
// a non-Symbol advice raises TypeError; an unrecognized one NotImplementedError
823+
// "Unsupported advice: :sym"; a closed stream IOError; then offset and len are
824+
// coerced as off_t (a non-Integer raises TypeError, a too-large Bignum
825+
// RangeError).
826+
cls.define("advise", func(vm *VM, self object.Value, args []object.Value, _ *Proc) object.Value {
827+
if len(args) < 1 || len(args) > 3 {
828+
raise("ArgumentError", "wrong number of arguments (given %d, expected 1..3)", len(args))
829+
}
830+
sym, ok := args[0].(object.Symbol)
831+
if !ok {
832+
raise("TypeError", "advice must be a Symbol")
833+
}
834+
switch string(sym) {
835+
case "normal", "sequential", "random", "willneed", "dontneed", "noreuse":
836+
// recognized advice types (io.c io_advise_sym_to_const)
837+
default:
838+
raise("NotImplementedError", "Unsupported advice: %s", args[0].Inspect())
839+
}
840+
ioClosedRealIO(self.(*IOObj))
841+
if len(args) > 1 && !object.IsNil(args[1]) {
842+
vm.ioOfftArg(args[1]) // offset: coerced for its TypeError/RangeError checks
843+
}
844+
if len(args) > 2 && !object.IsNil(args[2]) {
845+
vm.ioOfftArg(args[2]) // len: likewise
846+
}
847+
return object.NilV
848+
})
819849
// These sync/sync= entries serve a real IO; StringIO overrides both in
820850
// defStringIOExtra (its #sync is always true), so the closed-stream guard here
821851
// only ever fires for a real IO/File, which MRI makes raise IOError.

‎internal/vm/io_advise_test.go‎

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
// Copyright (c) the go-embedded-ruby/ruby authors
2+
//
3+
// SPDX-License-Identifier: BSD-3-Clause
4+
5+
package vm_test
6+
7+
import (
8+
"fmt"
9+
"path/filepath"
10+
"testing"
11+
)
12+
13+
// TestIOAdvise covers IO#advise (rb_io_advise): the six recognized advice types
14+
// as validated no-ops returning nil, the optional offset/len (present, nil, and
15+
// coercion errors), and the error branches — a non-Symbol advice (TypeError), an
16+
// unrecognized one (NotImplementedError "Unsupported advice: :sym"), wrong arity
17+
// (ArgumentError), a non-Integer or too-large offset/len (TypeError / RangeError),
18+
// and a closed stream (IOError). Every line verified byte-for-byte against ruby
19+
// 4.0.5 on darwin. File-backed via a per-test temp dir so no machine file is
20+
// touched; the asserted classes/messages are platform-independent.
21+
func TestIOAdvise(t *testing.T) {
22+
dir := t.TempDir()
23+
path := filepath.ToSlash(filepath.Join(dir, "advise.txt"))
24+
src := fmt.Sprintf(`
25+
f = File.open(%q, "w+")
26+
p f.advise(:normal)
27+
p f.advise(:sequential)
28+
p f.advise(:random)
29+
p f.advise(:willneed)
30+
p f.advise(:dontneed)
31+
p f.advise(:noreuse)
32+
p f.advise(:normal, 0)
33+
p f.advise(:normal, nil)
34+
p f.advise(:normal, 0, 0)
35+
p f.advise(:normal, 0, nil)
36+
begin; f.advise("normal"); rescue Exception => e; p e.class; end
37+
begin; f.advise(:foo); rescue Exception => e; p [e.class, e.message]; end
38+
begin; f.advise; rescue Exception => e; p e.class; end
39+
begin; f.advise(:normal, 0, 0, 0); rescue Exception => e; p e.class; end
40+
begin; f.advise(:normal, "wat"); rescue Exception => e; p e.class; end
41+
begin; f.advise(:normal, 0, "wat"); rescue Exception => e; p e.class; end
42+
begin; f.advise(:normal, 10 ** 32); rescue Exception => e; p e.class; end
43+
begin; f.advise(:normal, 0, 10 ** 32); rescue Exception => e; p e.class; end
44+
f.close
45+
begin; f.advise(:normal); rescue Exception => e; p [e.class, e.message]; end
46+
`, path)
47+
want := "nil\nnil\nnil\nnil\nnil\nnil\n" +
48+
"nil\nnil\nnil\nnil\n" +
49+
"TypeError\n" +
50+
"[NotImplementedError, \"Unsupported advice: :foo\"]\n" +
51+
"ArgumentError\n" +
52+
"ArgumentError\n" +
53+
"TypeError\n" +
54+
"TypeError\n" +
55+
"RangeError\n" +
56+
"RangeError\n" +
57+
"[IOError, \"closed stream\"]\n"
58+
if got := eval(t, src); got != want {
59+
t.Errorf("src=%q\n got=%q\nwant=%q", src, got, want)
60+
}
61+
}

0 commit comments

Comments
 (0)