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
84 changes: 66 additions & 18 deletions internal/vm/format_bind.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package vm

import (
"errors"
"math/big"
"strconv"
"strings"
Expand Down Expand Up @@ -209,26 +210,43 @@ func parseFormatInteger(s, inspect string) (*big.Int, error) {
// "0777"->511, "0b1101_0000"->208, but "123__456"/"_1"/"08" raise ArgumentError.
v, ok := intFromString(s, 0)
if !ok {
return nil, &format.Error{
Class: "ArgumentError",
Message: "invalid value for Integer(): " + inspect,
}
// Return only the message: the format engine promotes a non-nil parse
// error to an ArgumentError by wrapping err.Error(), so a *format.Error
// here (whose Error() is "Class: Message") would double the class prefix.
return nil, errors.New("invalid value for Integer(): " + inspect)
}
z, _ := object.BigOf(v)
return z, nil
}

// parseFormatFloat parses a String operand for a float conversion as MRI's
// Float() does for sprintf: surrounding whitespace is trimmed. A malformed
// value yields a non-nil error whose message matches MRI's
// `invalid value for Float(): <inspect>`.
// parseFormatFloat parses a String operand for a float conversion exactly as
// MRI's Kernel#Float does (the "%e/%f/%g behaves as if calling Kernel#Float"
// rule): it honours a "0x…" hexadecimal-float literal ("0xA" -> 10.0), applies
// MRI's strict digit-separator underscore placement, and treats an out-of-range
// magnitude as ±Inf / 0.0 (overflow/underflow) rather than an error — routing
// through the same normalizeFloatLiteral primitive as Kernel#Float so the two
// stay byte-for-byte identical. A genuinely malformed value yields a non-nil
// error whose message matches MRI's `invalid value for Float(): <inspect>` (the
// library promotes it to an ArgumentError).
func parseFormatFloat(s, inspect string) (float64, error) {
f, err := strconv.ParseFloat(strings.TrimSpace(s), 64)
// Return only the message: the format engine promotes a non-nil parse error
// to an ArgumentError by wrapping err.Error(), so a *format.Error here (whose
// Error() is "Class: Message") would double the class prefix.
badFloat := func() error {
return errors.New("invalid value for Float(): " + inspect)
}
norm, ok := normalizeFloatLiteral(s)
if !ok {
return 0, badFloat()
}
f, err := strconv.ParseFloat(norm, 64)
if err != nil {
return 0, &format.Error{
Class: "ArgumentError",
Message: "invalid value for Float(): " + inspect,
// An out-of-range literal is not malformed: MRI yields ±Infinity (overflow)
// or 0.0 (underflow), which is exactly what ParseFloat returns with ErrRange.
if ne, ok := err.(*strconv.NumError); ok && ne.Err == strconv.ErrRange {
return f, nil
}
return 0, badFloat()
}
return f, nil
}
Expand Down Expand Up @@ -277,7 +295,7 @@ func (vm *VM) formatString(fmtStr string, args []object.Value) string {
}
out, err := format.Format(fmtStr, vals, vm.formatNamedArgs(args))
if err != nil {
raiseFormatError(err)
vm.raiseFormatError(err, args)
}
return out
}
Expand All @@ -286,12 +304,42 @@ func (vm *VM) formatString(fmtStr string, args []object.Value) string {
// exception: a *format.Error carries the MRI exception class and message
// (ArgumentError / KeyError / TypeError) verbatim; any other error (which the
// library never produces, but is handled defensively) surfaces as an
// ArgumentError. It never returns.
func raiseFormatError(err error) {
if fe, ok := err.(*format.Error); ok {
raise(fe.Class, "%s", fe.Message)
// ArgumentError. Every KeyError the engine raises comes from an unmatched
// %<name>/%{name} reference, which is possible only when the sole operand is a
// hash; that KeyError is re-raised with MRI's #key (the missing key as a Symbol)
// and #receiver (that hash) set, so `rescue KeyError => e; e.key; e.receiver`
// behave as MRI does. args is the positional operand list. It never returns.
func (vm *VM) raiseFormatError(err error, args []object.Value) {
fe, ok := err.(*format.Error)
if !ok {
raise("ArgumentError", "%s", err.Error())
}
raise("ArgumentError", "%s", err.Error())
if fe.Class == "KeyError" {
var receiver object.Value
if len(args) == 1 {
if h, isHash := args[0].(*object.Hash); isHash {
receiver = h
}
}
vm.raiseWithIvars("KeyError", fe.Message, map[string]object.Value{
"@key": object.Symbol(namedKeyFromMessage(fe.Message)),
"@receiver": receiver,
})
}
raise(fe.Class, "%s", fe.Message)
}

// namedKeyFromMessage extracts the referenced key from the format engine's
// unmatched-reference KeyError message, which is `key{NAME} not found` for a
// %{NAME} reference and `key<NAME> not found` for a %<NAME> one; either bracket
// style yields the same MRI #key. The NAME token never itself contains a
// reference bracket (such a byte would terminate the reference), so trimming the
// fixed prefix/suffix and the surrounding brackets recovers it exactly.
func namedKeyFromMessage(msg string) string {
inner := strings.TrimPrefix(strings.TrimSuffix(msg, " not found"), "key")
return strings.TrimFunc(inner, func(r rune) bool {
return r == '{' || r == '}' || r == '<' || r == '>'
})
}

// formatArgs unpacks the right-hand operand of String#%: an Array spreads into
Expand Down
2 changes: 1 addition & 1 deletion internal/vm/format_bind_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,5 +128,5 @@ func TestRaiseFormatErrorFallback(t *testing.T) {
t.Fatalf("want ArgumentError boom, got %v", re)
}
}()
raiseFormatError(errors.New("boom"))
New(io.Discard).raiseFormatError(errors.New("boom"), nil)
}
127 changes: 127 additions & 0 deletions internal/vm/sprintf_conformance_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
// Copyright (c) the go-embedded-ruby/ruby authors
//
// SPDX-License-Identifier: BSD-3-Clause

package vm_test

import (
"strings"
"testing"
)

// TestSprintfFloatStringHex covers the format binding's parseFormatFloat: a
// String operand for a float conversion is parsed exactly as MRI's Kernel#Float
// (the "%f/%e/%g behaves as if calling Kernel#Float" rule), so a hexadecimal
// float literal is accepted and an out-of-range magnitude yields ±Inf / 0.0
// rather than an error. Every expectation is byte-for-byte against MRI 4.0.
func TestSprintfFloatStringHex(t *testing.T) {
tests := []struct {
name, src, want string
}{
// Hexadecimal float strings (the previously-failing case): "0xA" == 10.0.
{"hex_f", `p("%f" % "0xA")`, "\"10.000000\"\n"},
{"hex_e", `p("%e" % "0xA")`, "\"1.000000e+01\"\n"},
{"hex_E", `p("%E" % "0xA")`, "\"1.000000E+01\"\n"},
{"hex_g", `p("%g" % "0xA")`, "\"10\"\n"},
{"hex_G", `p("%G" % "0xA")`, "\"10\"\n"},
{"hex_scaled", `p("%f" % "0x1.8p3")`, "\"12.000000\"\n"},
{"hex_signed", `p("%f" % "-0xA")`, "\"-10.000000\"\n"},
// Overflow -> +Infinity, underflow -> 0.0 (strconv's ErrRange path).
{"overflow", `p("%f" % "1e400")`, "\"Inf\"\n"},
{"underflow", `p("%f" % "1e-400")`, "\"0.000000\"\n"},
// Decimal strings and underscores keep working (regression guard).
{"decimal", `p("%f" % "1.5")`, "\"1.500000\"\n"},
{"integerish", `p("%f" % "10")`, "\"10.000000\"\n"},
{"underscore", `p("%f" % "1_000.5")`, "\"1000.500000\"\n"},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
if got := eval(t, tc.src); got != tc.want {
t.Errorf("src=%q\n got=%q\nwant=%q", tc.src, got, tc.want)
}
})
}
}

// TestSprintfFloatStringErrors covers parseFormatFloat's error path: a malformed
// String operand raises ArgumentError with MRI's exact, un-doubled message
// "invalid value for Float(): <inspect>" (the engine wraps err.Error(), so the
// binding must return only the message, not a class-prefixed *format.Error).
func TestSprintfFloatStringErrors(t *testing.T) {
tests := []struct{ name, src, wantMsg string }{
{"garbage", `"%f" % "xyz"`, `invalid value for Float(): "xyz"`},
{"binary_prefix", `"%f" % "0b1"`, `invalid value for Float(): "0b1"`},
{"octal_prefix", `"%f" % "0o17"`, `invalid value for Float(): "0o17"`},
{"bad_hex_underscore", `"%f" % "0x1_p0"`, `invalid value for Float(): "0x1_p0"`},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
err := runErr(t, tc.src)
if err == nil {
t.Fatalf("src=%q: expected ArgumentError, got nil", tc.src)
}
if !strings.Contains(err.Error(), "ArgumentError") {
t.Errorf("src=%q: want ArgumentError, got %v", tc.src, err)
}
// The message must not carry a doubled "ArgumentError: " prefix.
if !strings.Contains(err.Error(), tc.wantMsg) || strings.Contains(err.Error(), "ArgumentError: ArgumentError") {
t.Errorf("src=%q: want message %q un-doubled, got %v", tc.src, tc.wantMsg, err)
}
})
}
}

// TestSprintfIntegerStringErrorMessage covers parseFormatInteger's error path:
// like the float path, a malformed integer String operand raises ArgumentError
// with the un-doubled "invalid value for Integer(): <inspect>" message.
func TestSprintfIntegerStringErrorMessage(t *testing.T) {
got := eval(t, `begin; "%d" % "abc"; rescue => e; p e.message; end`)
want := "\"invalid value for Integer(): \\\"abc\\\"\"\n"
if got != want {
t.Errorf("got=%q want=%q", got, want)
}
}

// TestSprintfIntegerStringOK guards the parseFormatInteger success path: a valid
// radix-prefixed String operand still formats (so the error-path change did not
// disturb the accepted case).
func TestSprintfIntegerStringOK(t *testing.T) {
if got := eval(t, `p("%d" % "0x1f")`); got != "\"31\"\n" {
t.Errorf("got=%q want %q", got, "\"31\"\n")
}
}

// TestSprintfNamedKeyError covers raiseFormatError's KeyError path: an unmatched
// %{name} / %<name> reference raises a KeyError whose #key is the missing key as
// a Symbol and whose #receiver is the very hash operand (identity), matching
// MRI. Both bracket styles are exercised (namedKeyFromMessage handles each).
func TestSprintfNamedKeyError(t *testing.T) {
// %{name}: message, class, and #key.
got := eval(t, `begin; "%{foo}" % {a: 5}; rescue KeyError => e; p [e.message, e.key]; end`)
if want := "[\"key{foo} not found\", :foo]\n"; got != want {
t.Errorf("braced: got=%q want=%q", got, want)
}
// %<name>: #key.to_s and #receiver identity with the passed hash.
got = eval(t, `h = {a: 5}
begin
"%<foo>s" % h
rescue KeyError => e
p [e.key.to_s, e.receiver.equal?(h)]
end`)
if want := "[\"foo\", true]\n"; got != want {
t.Errorf("angle: got=%q want=%q", got, want)
}
}

// TestSprintfNonKeyErrorUnaffected guards raiseFormatError's non-KeyError branch:
// a ClassName-driven TypeError from an unconvertible operand and an ArgumentError
// from a bad literal still surface with their own class (the KeyError special-
// casing does not intercept them).
func TestSprintfNonKeyErrorUnaffected(t *testing.T) {
if err := runErr(t, `"%d" % :sym`); err == nil || !strings.Contains(err.Error(), "TypeError") {
t.Errorf("want TypeError, got %v", err)
}
if err := runErr(t, `"%d" % "abc"`); err == nil || !strings.Contains(err.Error(), "ArgumentError") {
t.Errorf("want ArgumentError, got %v", err)
}
}