From 8f50da00101c04d43a85094e59e7434ed0da24c4 Mon Sep 17 00:00:00 2001 From: tannevaled Date: Fri, 28 Aug 2026 21:32:24 +0200 Subject: [PATCH] Add IO.copy_stream IO.copy_stream(src, dst, copy_length = nil, src_offset = nil) copies bytes from src to dst, each of which may be a path String (read/written via binread / binwrite) or an IO (read/written through the IO protocol), and returns the number of bytes copied. A copy_length limits the amount; src_offset is honoured for a path but rejected for a non-fd IO such as StringIO, matching MRI; and fewer than two arguments raises ArgumentError. Verified against ruby 4.0.6. Whole-package coverage stays at 100%; the ruby/spec floor rises 16080 -> 16095 (core/io/copy_stream 0 -> 25). Co-Authored-By: Claude Opus 4.8 --- internal/vm/io_class.go | 48 +++++++++++++++++++++++++++ internal/vm/io_class_internal_test.go | 29 ++++++++++++++++ scripts/conformance/rubyspec/FLOOR | 2 +- 3 files changed, 78 insertions(+), 1 deletion(-) diff --git a/internal/vm/io_class.go b/internal/vm/io_class.go index 19776b1c..5107830a 100644 --- a/internal/vm/io_class.go +++ b/internal/vm/io_class.go @@ -33,6 +33,54 @@ func (vm *VM) registerIOClassMethods(cIO, cFile *RClass) { def("binwrite", func(vm *VM, _ object.Value, args []object.Value, _ *Proc) object.Value { return vm.ioWriteFile(args) }) + // IO.copy_stream(src, dst, copy_length = nil, src_offset = nil): copy bytes from + // src to dst, each of which may be a path String or an IO. Returns the number + // of bytes copied. src_offset is honoured for a path but rejected for a + // (non-fd) IO such as StringIO, matching MRI. + def("copy_stream", func(vm *VM, _ object.Value, args []object.Value, _ *Proc) object.Value { + if len(args) < 2 { + raise("ArgumentError", "wrong number of arguments (given %d, expected 2..4)", len(args)) + } + length, hasLen := -1, false + if len(args) >= 3 && !object.IsNil(args[2]) { + length, hasLen = int(intArg(args[2])), true + } + srcOffset, hasOff := -1, false + if len(args) >= 4 && !object.IsNil(args[3]) { + srcOffset, hasOff = int(intArg(args[3])), true + } + var data *object.String + if p, ok := args[0].(*object.String); ok { + ra := []object.Value{p} + if hasLen { + ra = append(ra, object.IntValue(int64(length))) + if hasOff { + ra = append(ra, object.IntValue(int64(srcOffset))) + } + } + data = vm.ioReadFile(ra, true).(*object.String) + } else { + if hasOff { + raise("ArgumentError", "cannot specify src_offset for non-IO") + } + var ra []object.Value + if hasLen { + ra = append(ra, object.IntValue(int64(length))) + } + r := vm.send(args[0], "read", ra, nil) + if s, ok := r.(*object.String); ok { + data = s + } else { + data = object.NewString("") + } + } + if p, ok := args[1].(*object.String); ok { + vm.ioWriteFile([]object.Value{p, data}) + } else { + vm.send(args[1], "write", []object.Value{data}, nil) + } + return object.IntValue(int64(len(data.Bytes()))) + }) def("foreach", func(vm *VM, _ object.Value, args []object.Value, blk *Proc) object.Value { pos, _ := splitIOOpts(args) if len(pos) == 0 { diff --git a/internal/vm/io_class_internal_test.go b/internal/vm/io_class_internal_test.go index 9c6a387a..25175cd1 100644 --- a/internal/vm/io_class_internal_test.go +++ b/internal/vm/io_class_internal_test.go @@ -199,3 +199,32 @@ func TestIOClassHelpers(t *testing.T) { t.Error("modeBase wrong") } } + +// TestIOCopyStream covers IO.copy_stream across path and StringIO sources and +// destinations, the copy_length and src_offset arguments, and the +// src_offset-for-non-IO ArgumentError. Paths use Dir.mktmpdir so the test is +// cross-platform. Asserted against MRI Ruby 4.0.6. +func TestIOCopyStream(t *testing.T) { + pre := `require "tmpdir"; require "stringio"; ` + cases := []struct{ src, want string }{ + // StringIO → StringIO, with and without a copy length. + {pre + `a = StringIO.new("abcdef"); b = StringIO.new; n = IO.copy_stream(a, b); p [n, b.string]`, "[6, \"abcdef\"]\n"}, + {pre + `a = StringIO.new("abcdef"); b = StringIO.new; n = IO.copy_stream(a, b, 3); p [n, b.string]`, "[3, \"abc\"]\n"}, + // path → path, path → StringIO, StringIO → path, and length+offset on a path. + {pre + `Dir.mktmpdir { |d| s = File.join(d, "s"); t = File.join(d, "t"); File.write(s, "hello"); n = IO.copy_stream(s, t); p [n, File.read(t)] }`, "[5, \"hello\"]\n"}, + {pre + `Dir.mktmpdir { |d| s = File.join(d, "s"); File.write(s, "world"); b = StringIO.new; n = IO.copy_stream(s, b); p [n, b.string] }`, "[5, \"world\"]\n"}, + {pre + `Dir.mktmpdir { |d| t = File.join(d, "t"); a = StringIO.new("data"); n = IO.copy_stream(a, t); p [n, File.read(t)] }`, "[4, \"data\"]\n"}, + {pre + `Dir.mktmpdir { |d| s = File.join(d, "s"); t = File.join(d, "t"); File.write(s, "abcdef"); n = IO.copy_stream(s, t, 3, 2); p [n, File.read(t)] }`, "[3, \"cde\"]\n"}, + // A length read past EOF copies nothing. + {pre + `a = StringIO.new(""); b = StringIO.new; n = IO.copy_stream(a, b, 5); p [n, b.string]`, "[0, \"\"]\n"}, + // src_offset is rejected for a non-IO (StringIO) source. + {pre + `a = StringIO.new("abcdef"); b = StringIO.new; begin; IO.copy_stream(a, b, 3, 2); rescue ArgumentError => e; p e.message; end`, "\"cannot specify src_offset for non-IO\"\n"}, + // Fewer than two arguments is an ArgumentError. + {pre + `begin; IO.copy_stream(StringIO.new("x")); rescue ArgumentError => e; p e.class; end`, "ArgumentError\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) + } + } +} diff --git a/scripts/conformance/rubyspec/FLOOR b/scripts/conformance/rubyspec/FLOOR index 83dece49..8d5d2793 100644 --- a/scripts/conformance/rubyspec/FLOOR +++ b/scripts/conformance/rubyspec/FLOOR @@ -1 +1 @@ -16080 +16095