diff --git a/internal/vm/arith.go b/internal/vm/arith.go index 19c37c2f..cbb3c313 100644 --- a/internal/vm/arith.go +++ b/internal/vm/arith.go @@ -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 `+`, diff --git a/internal/vm/format_bind.go b/internal/vm/format_bind.go index ec3c891e..0f47a2a6 100644 --- a/internal/vm/format_bind.go +++ b/internal/vm/format_bind.go @@ -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} hash (MRI's behaviour // where "%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}/% +// 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} } diff --git a/internal/vm/format_to_ary_internal_test.go b/internal/vm/format_to_ary_internal_test.go new file mode 100644 index 00000000..2c4c2e74 --- /dev/null +++ b/internal/vm/format_to_ary_internal_test.go @@ -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}/% 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 "%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") + } + } +}