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
2 changes: 1 addition & 1 deletion internal/vm/arith.go
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,7 @@ func (vm *VM) binaryOp(op bytecode.Op, a, b object.Value) object.Value {
// %f#to_f, %{name}#to_s), so route it through the VM-aware formatter rather
// than the VM-less stringOp path.
if as, ok := a.(*object.String); ok && op == bytecode.OpMod {
return object.NewString(vm.formatString(as.Str(), formatArgs(b)))
return object.NewString(vm.formatString(as.Str(), vm.vmFormatArgs(b)))
}
// A user object (RObject with no builtin backing) that defines an
// arithmetic operator dispatches to it, so `Pathname + str`, a Money `+`,
Expand Down
24 changes: 21 additions & 3 deletions internal/vm/format_bind.go
Original file line number Diff line number Diff line change
Expand Up @@ -299,9 +299,27 @@ func raiseFormatError(err error) {
// stays a one-element argument list, which formatString both treats as a
// positional operand and exposes as the %<name>/%{name} hash (MRI's behaviour
// where "%<a>d %s" % {a: 1} formats the hash for both forms).
func formatArgs(b object.Value) []object.Value {
if arr, ok := b.(*object.Array); ok {
return arr.Elems
// vmFormatArgs turns the right-hand side of String#% into the positional
// argument list. An Array is spread; a Hash is kept whole (for %{name}/%<name>
// references); any other object is converted with #to_ary when it responds —
// an Array result is spread, a nil result falls back to a single argument, and a
// non-Array result raises TypeError, as MRI does; otherwise it is a single
// argument.
func (vm *VM) vmFormatArgs(b object.Value) []object.Value {
switch b.(type) {
case *object.Array:
return b.(*object.Array).Elems
case *object.Hash:
return []object.Value{b}
}
if vm.respondsToDynamic(b, "to_ary") {
if r := vm.send(b, "to_ary", nil, nil); !object.IsNil(r) {
if arr, ok := r.(*object.Array); ok {
return arr.Elems
}
raise("TypeError", "can't convert %s to Array (%s#to_ary gives %s)",
vm.classOf(b).name, vm.classOf(b).name, vm.classOf(r).name)
}
}
return []object.Value{b}
}
36 changes: 36 additions & 0 deletions internal/vm/format_to_ary_internal_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// Copyright (c) the go-embedded-ruby/ruby authors
//
// SPDX-License-Identifier: BSD-3-Clause

package vm

import "testing"

// TestFormatToAry covers how String#% turns its right-hand side into the
// positional argument list: an Array is spread, a Hash is kept whole for
// %{name}/%<name> references, a single scalar is one argument, and any other
// object is converted with #to_ary (an Array spreads, a nil result falls back to
// a single argument, a non-Array result raises TypeError). Verified against
// ruby 4.0.6.
func TestFormatToAry(t *testing.T) {
cases := []struct{ src, want string }{
// Array spreads; a plain scalar is a single argument.
{`p "%d %d" % [1, 2]`, `"1 2"`},
{`p "%d" % 5`, `"5"`},
// A Hash stays whole (named references / %s of the hash).
{`p "%<x>d" % {x: 7}`, `"7"`},
{`p "%s" % {a: 1}`, `"{a: 1}"`},
// #to_ary returning an Array is spread.
{`class FA; def to_ary; [1, 2]; end; end; p "%d %d" % FA.new`, `"1 2"`},
// #to_ary returning nil falls back to a single argument.
{`class FB; def to_ary; nil; end; def to_s; "b"; end; end; p "%s" % FB.new`, `"b"`},
// #to_ary returning a non-Array raises TypeError.
{`class FC; def to_ary; 5; end; end
begin; "%s" % FC.new; rescue TypeError => e; p e.message; end`, `"can't convert FC to Array (FC#to_ary gives Integer)"`},
}
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")
}
}
}