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
57 changes: 52 additions & 5 deletions internal/vm/io.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,13 +148,60 @@ func (vm *VM) registerIO() {
vm.consts["STDOUT"], vm.consts["STDERR"], vm.consts["STDIN"] = stdout, stderr, stdin
vm.globals["$stdout"], vm.globals["$stderr"], vm.globals["$stdin"] = stdout, stderr, stdin

// Kernel#warn writes each message (newline-terminated) to the current $stderr.
// Kernel#warn builds one message — each argument on its own line, a newline
// appended only when it does not already end with one — and routes it through
// Warning.warn(message, category:) so a program that overrides Warning.warn (or
// reads the category) sees it, as in MRI. A String category is converted to a
// Symbol (anything else is a TypeError); a category whose warnings are disabled
// (Warning[category] is false) is dropped, and an unknown category raises. The
// uplevel: keyword is validated (a negative or non-Integer value raises) but not
// acted on, as rbgo has no caller line to prepend. With no message it does
// nothing.
vm.cObject.define("warn", func(vm *VM, _ object.Value, args []object.Value, _ *Proc) object.Value {
o := vm.curStderr()
for _, a := range args {
vm.ioPutsValue(o, a)
var category object.Value = object.NilV
pos := args
if kw := trailingKwHash(args); kw != nil {
pos = args[:len(args)-1]
if v, ok := kw.Get(object.SymVal("category")); ok {
category = v
}
if v, ok := kw.Get(object.SymVal("uplevel")); ok && !object.IsNil(v) {
n, isInt := v.(object.Integer)
if !isInt {
raise("TypeError", "no implicit conversion of %s into Integer", vm.classOf(v).name)
}
if int64(n) < 0 {
raise("ArgumentError", "negative level (%d)", int64(n))
}
}
}
return object.NilV
var b strings.Builder
for _, a := range pos {
s := vm.displayStr(a)
b.WriteString(s)
if !strings.HasSuffix(s, "\n") {
b.WriteByte('\n')
}
}
if b.Len() == 0 {
return object.NilV
}
if !object.IsNil(category) {
switch c := category.(type) {
case object.Symbol:
case *object.String:
category = object.Symbol(c.Str())
default:
raise("TypeError", "no implicit conversion of %s into Symbol", vm.classOf(category).name)
}
// Warning[category] filters the message (and raises for an unknown one).
if !vm.send(vm.consts["Warning"], "[]", []object.Value{category}, nil).Truthy() {
return object.NilV
}
}
kw := object.NewHash()
kw.Set(object.SymVal("category"), category)
return vm.send(vm.consts["Warning"], "warn", []object.Value{object.NewString(b.String()), kw}, nil)
})

// File streams: File.open returns a buffered, file-backed IO carrying the
Expand Down
43 changes: 43 additions & 0 deletions internal/vm/kernel_warn_internal_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// Copyright (c) the go-embedded-ruby/ruby authors
//
// SPDX-License-Identifier: BSD-3-Clause

package vm

import "testing"

// TestKernelWarn covers Kernel#warn routing through Warning.warn: it builds one
// message (each argument newline-terminated, no doubled newline), passes a
// nil-or-converted category keyword, drops a message whose category is disabled
// and raises for an unknown one, validates the uplevel keyword, and does nothing
// with no message. Verified against ruby 4.0.6.
func TestKernelWarn(t *testing.T) {
// Capture what Warning.warn receives by overriding it.
const cap = `$c = nil; Warning.singleton_class.send(:define_method, :warn) { |m, category: nil| $c = [m, category] }; `
cases := []struct{ src, want string }{
// Each argument on its own line; an existing newline is not doubled.
{cap + `warn("a", "b\n", "c"); p $c`, `["a\nb\nc\n", nil]`},
{cap + `warn("hello"); p $c`, `["hello\n", nil]`},
// A String category converts to a Symbol; nil stays nil.
{cap + `Warning[:deprecated] = true; warn("x", category: "deprecated"); p $c`, `["x\n", :deprecated]`},
// A disabled category drops the message (Warning.warn is not called).
{cap + `Warning[:deprecated] = false; warn("x", category: :deprecated); p $c`, `nil`},
// An enabled category passes through.
{cap + `Warning[:experimental] = true; warn("x", category: :experimental); p $c`, `["x\n", :experimental]`},
// No message does nothing.
{cap + `warn; p $c`, `nil`},
// An unknown category raises ArgumentError.
{`begin; warn("x", category: :bogus); rescue ArgumentError => e; p e.message; end`, `"unknown category: bogus"`},
// A non-Symbol/String category is a TypeError.
{`begin; warn("x", category: 5); rescue TypeError => e; p e.message; end`, `"no implicit conversion of Integer into Symbol"`},
// uplevel is validated: negative raises ArgumentError, non-Integer TypeError.
{`begin; warn("x", uplevel: -1); rescue ArgumentError => e; p e.message; end`, `"negative level (-1)"`},
{`begin; warn("x", uplevel: "a"); rescue TypeError => e; p e.message; end`, `"no implicit conversion of String into Integer"`},
{cap + `warn("x", uplevel: 0); p $c`, `["x\n", nil]`},
}
for _, c := range cases {
if got := eval(t, c.src); got != c.want+"\n" {
t.Errorf("src=%q got=%q want=%q", c.src, got, c.want+"\n")
}
}
}