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
30 changes: 30 additions & 0 deletions internal/vm/io.go
Original file line number Diff line number Diff line change
Expand Up @@ -816,6 +816,36 @@ func defIOWrite(cls *RClass) {
ioClosedRealIO(self.(*IOObj)) // as flush: real IO raises on close, StringIO does not
return object.IntValue(0)
})
// IO#advise(advice, offset = 0, len = 0) (rb_io_advise + advice_arg_check in
// io.c). rbgo has no posix_fadvise, so like MRI on a platform without it the
// call is a validated no-op returning nil. Argument checks, in MRI's order:
// a non-Symbol advice raises TypeError; an unrecognized one NotImplementedError
// "Unsupported advice: :sym"; a closed stream IOError; then offset and len are
// coerced as off_t (a non-Integer raises TypeError, a too-large Bignum
// RangeError).
cls.define("advise", func(vm *VM, self object.Value, args []object.Value, _ *Proc) object.Value {
if len(args) < 1 || len(args) > 3 {
raise("ArgumentError", "wrong number of arguments (given %d, expected 1..3)", len(args))
}
sym, ok := args[0].(object.Symbol)
if !ok {
raise("TypeError", "advice must be a Symbol")
}
switch string(sym) {
case "normal", "sequential", "random", "willneed", "dontneed", "noreuse":
// recognized advice types (io.c io_advise_sym_to_const)
default:
raise("NotImplementedError", "Unsupported advice: %s", args[0].Inspect())
}
ioClosedRealIO(self.(*IOObj))
if len(args) > 1 && !object.IsNil(args[1]) {
vm.ioOfftArg(args[1]) // offset: coerced for its TypeError/RangeError checks
}
if len(args) > 2 && !object.IsNil(args[2]) {
vm.ioOfftArg(args[2]) // len: likewise
}
return object.NilV
})
// These sync/sync= entries serve a real IO; StringIO overrides both in
// defStringIOExtra (its #sync is always true), so the closed-stream guard here
// only ever fires for a real IO/File, which MRI makes raise IOError.
Expand Down
61 changes: 61 additions & 0 deletions internal/vm/io_advise_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
// Copyright (c) the go-embedded-ruby/ruby authors
//
// SPDX-License-Identifier: BSD-3-Clause

package vm_test

import (
"fmt"
"path/filepath"
"testing"
)

// TestIOAdvise covers IO#advise (rb_io_advise): the six recognized advice types
// as validated no-ops returning nil, the optional offset/len (present, nil, and
// coercion errors), and the error branches — a non-Symbol advice (TypeError), an
// unrecognized one (NotImplementedError "Unsupported advice: :sym"), wrong arity
// (ArgumentError), a non-Integer or too-large offset/len (TypeError / RangeError),
// and a closed stream (IOError). Every line verified byte-for-byte against ruby
// 4.0.5 on darwin. File-backed via a per-test temp dir so no machine file is
// touched; the asserted classes/messages are platform-independent.
func TestIOAdvise(t *testing.T) {
dir := t.TempDir()
path := filepath.ToSlash(filepath.Join(dir, "advise.txt"))
src := fmt.Sprintf(`
f = File.open(%q, "w+")
p f.advise(:normal)
p f.advise(:sequential)
p f.advise(:random)
p f.advise(:willneed)
p f.advise(:dontneed)
p f.advise(:noreuse)
p f.advise(:normal, 0)
p f.advise(:normal, nil)
p f.advise(:normal, 0, 0)
p f.advise(:normal, 0, nil)
begin; f.advise("normal"); rescue Exception => e; p e.class; end
begin; f.advise(:foo); rescue Exception => e; p [e.class, e.message]; end
begin; f.advise; rescue Exception => e; p e.class; end
begin; f.advise(:normal, 0, 0, 0); rescue Exception => e; p e.class; end
begin; f.advise(:normal, "wat"); rescue Exception => e; p e.class; end
begin; f.advise(:normal, 0, "wat"); rescue Exception => e; p e.class; end
begin; f.advise(:normal, 10 ** 32); rescue Exception => e; p e.class; end
begin; f.advise(:normal, 0, 10 ** 32); rescue Exception => e; p e.class; end
f.close
begin; f.advise(:normal); rescue Exception => e; p [e.class, e.message]; end
`, path)
want := "nil\nnil\nnil\nnil\nnil\nnil\n" +
"nil\nnil\nnil\nnil\n" +
"TypeError\n" +
"[NotImplementedError, \"Unsupported advice: :foo\"]\n" +
"ArgumentError\n" +
"ArgumentError\n" +
"TypeError\n" +
"TypeError\n" +
"RangeError\n" +
"RangeError\n" +
"[IOError, \"closed stream\"]\n"
if got := eval(t, src); got != want {
t.Errorf("src=%q\n got=%q\nwant=%q", src, got, want)
}
}
Loading