diff --git a/internal/vm/arith.go b/internal/vm/arith.go index 258a59d7..13dcb55e 100644 --- a/internal/vm/arith.go +++ b/internal/vm/arith.go @@ -271,6 +271,20 @@ func (vm *VM) binaryOp(op bytecode.Op, a, b object.Value) object.Value { return object.Bool(eq) } } + // A String compared against a non-String operand that answers #to_str + // defers to `other == self` (MRI's rb_str_equal only checks that #to_str is + // defined, never calling it), matching String#==. A String / String-subclass + // right operand was already reduced to *object.String above, so it takes the + // structural byte compare below. + if as, isStr := a.(*object.String); isStr { + if _, bStr := b.(*object.String); !bStr && vm.respondsToDynamic(b, "to_str") { + eq := vm.send(b, "==", []object.Value{as}, nil).Truthy() + if op == bytecode.OpNeq { + eq = !eq + } + return object.Bool(eq) + } + } // The structural compare is VM-aware so that an Array/Hash operand compares // its elements with each element's own Ruby #== (MRI's rb_equal per element), // not by Go identity. diff --git a/internal/vm/builtins.go b/internal/vm/builtins.go index 21c63747..513f5de6 100644 --- a/internal/vm/builtins.go +++ b/internal/vm/builtins.go @@ -1685,13 +1685,32 @@ func (vm *VM) bootstrap() { vm.cInteger.define("pow", powNumeric) vm.cFloat.define("**", powNumeric) vm.cFloat.define("pow", powNumeric) - vm.cString.define("<=>", func(_ *VM, self object.Value, args []object.Value, _ *Proc) object.Value { + vm.cString.define("<=>", func(vm *VM, self object.Value, args []object.Value, _ *Proc) object.Value { a := self.(*object.String) - b, ok := args[0].(*object.String) - if !ok { - return object.NilV + if b := stringOrSubclassBytes(args[0]); b != nil { + return object.IntValue(int64(strings.Compare(a.Str(), b.Str()))) + } + // Not a String: MRI's rb_check_string_type first tries #to_str and compares + // against its result; otherwise it inverts `other <=> self` (rb_invcmp). + if vm.respondsToDynamic(args[0], "to_str") { + if s, ok := vm.send(args[0], "to_str", nil, nil).(*object.String); ok { + return object.IntValue(int64(strings.Compare(a.Str(), s.Str()))) + } + } + return vm.spaceshipInvert(self, args[0]) + }) + vm.cString.define("==", func(vm *VM, self object.Value, args []object.Value, _ *Proc) object.Value { + a := self.(*object.String) + if b := stringOrSubclassBytes(args[0]); b != nil { + return object.Bool(a.Str() == b.Str()) } - return object.IntValue(int64(strings.Compare(a.Str(), b.Str()))) + // A non-String that answers #to_str: MRI's rb_str_equal defers to + // `other == self` (it only checks that #to_str is defined, never calling + // it). Anything else is unequal. + if vm.respondsToDynamic(args[0], "to_str") { + return object.Bool(vm.send(args[0], "==", []object.Value{self}, nil).Truthy()) + } + return object.False }) // String. Methods over the mutable byte-based String (length/chars/index are @@ -1809,8 +1828,8 @@ func (vm *VM) bootstrap() { vm.cString.define("succ!", succBang) // #next and #next! are true aliases of #succ / #succ! (they share the same // Method object, so instance_method(:next) == instance_method(:succ)). - vm.aliasMethod(vm.cString, "next", "succ") - vm.aliasMethod(vm.cString, "next!", "succ!") + aliasBuiltin(vm.cString, "next", "succ") + aliasBuiltin(vm.cString, "next!", "succ!") vm.cString.define("chr", func(_ *VM, self object.Value, _ []object.Value, _ *Proc) object.Value { return object.NewString(stringChr(strOf(self))) }) @@ -1837,10 +1856,13 @@ func (vm *VM) bootstrap() { }) vm.cString.define("upto", func(vm *VM, self object.Value, args []object.Value, blk *Proc) object.Value { if blk == nil { - return enumFor(self, "upto", args...) + // The returned Enumerator reports an unknown (nil) size, as MRI does. + return enumForSized(self, "upto", func(_ *VM) object.Value { return object.NilV }, args...) } excl := len(args) > 1 && truthyValue(args[1]) - stringUpto(strOf(self), strArg(args[0]), excl, func(cur string) { + // The end argument is coerced through #to_str (so a #to_str object works), + // raising TypeError otherwise — matching MRI, which rejects Integer/Symbol. + stringUpto(strOf(self), vm.affixString(args[0]).Str(), excl, func(cur string) { vm.callBlock(blk, []object.Value{object.NewString(cur)}) }) return self @@ -1890,6 +1912,9 @@ func (vm *VM) bootstrap() { return object.NewArrayFromSlice(out) }) vm.cString.define("getbyte", func(_ *VM, self object.Value, args []object.Value, _ *Proc) object.Value { + if len(args) != 1 { + raise("ArgumentError", "wrong number of arguments (given %d, expected 1)", len(args)) + } s := strOf(self) i := toInt(args[0]) if i < 0 { @@ -2012,12 +2037,22 @@ func (vm *VM) bootstrap() { s := strOf(self) for _, a := range args { // true if any prefix matches; a Regexp must match at offset 0 if re, ok := a.(*Regexp); ok { - if md := re.re.Match(s); md != nil && md.Begin(0) == 0 { + // A matching Regexp sets $~ (so $1.. and Regexp.last_match are live); + // a non-match clears it to nil, as MRI does. + if md := re.matcher().Match(s); md != nil && md.Begin(0) == 0 { + vm.lastMatch = &MatchData{md: md, subject: s, re: re} return object.True } + vm.lastMatch = object.NilV continue } - if strings.HasPrefix(s, vm.strPatternCompat(self, a)) { + // A non-Regexp prefix is coerced through #to_str (so a #to_str object + // works), raising TypeError when it is neither a String nor convertible; + // combinedEncName then raises Encoding::CompatibilityError for an + // incompatible pair, as MRI does. + pre, sobj := vm.strCoerceArg(a) + vm.combinedEncName(self.(*object.String), sobj) + if strings.HasPrefix(s, pre) { return object.True } } @@ -2140,10 +2175,12 @@ func (vm *VM) bootstrap() { vm.cString.define("gsub", func(vm *VM, self object.Value, args []object.Value, blk *Proc) object.Value { return vm.stringSub(strOf(self), args, blk, true) }) - vm.cString.define("to_i", func(_ *VM, self object.Value, args []object.Value, _ *Proc) object.Value { + vm.cString.define("to_i", func(vm *VM, self object.Value, args []object.Value, _ *Proc) object.Value { base := 10 if len(args) > 0 { - base = int(intArg(args[0])) + // The base is coerced through #to_int (MRI's rb_num2long), so a Float + // or a #to_int object works and a non-integer raises TypeError. + base = int(vm.integerArg(args[0]).Int64()) } return stringToInt(strOf(self), base) }) @@ -2159,14 +2196,12 @@ func (vm *VM) bootstrap() { vm.cString.define("to_s", func(_ *VM, self object.Value, _ []object.Value, _ *Proc) object.Value { return self }) - vm.cString.define("to_str", func(_ *VM, self object.Value, _ []object.Value, _ *Proc) object.Value { - return self - }) + aliasBuiltin(vm.cString, "to_str", "to_s") // MRI alias of String#to_s strToSym := func(_ *VM, self object.Value, _ []object.Value, _ *Proc) object.Value { return object.Symbol(strOf(self)) } vm.cString.define("to_sym", strToSym) - vm.cString.define("intern", strToSym) // MRI alias of String#to_sym + aliasBuiltin(vm.cString, "intern", "to_sym") // MRI alias of String#to_sym // scrub replaces each ill-formed byte sequence with a replacement (the encoding's // U+FFFD, or an explicit String / block result), returning a valid copy in the // receiver's encoding. scrub! does the same in place, returning self. @@ -6193,6 +6228,44 @@ func nativeP(vm *VM, _ object.Value, args []object.Value, _ *Proc) object.Value // Ruby (space, tab, newline, carriage return, form feed, vertical tab, NUL). const wsCutset = " \t\n\r\f\v\x00" +// stringOrSubclassBytes returns the underlying *object.String for a String or +// for an instance of a user subclass of String (whose bytes live in +// RObject.builtin), and nil for anything else. It performs no #to_str +// conversion, so callers use it to accept a String or String subclass by value +// while leaving the coercion of other objects to their own path. +func stringOrSubclassBytes(v object.Value) *object.String { + switch s := v.(type) { + case *object.String: + return s + case *RObject: + if b, ok := s.builtin.(*object.String); ok { + return b + } + } + return nil +} + +// spaceshipInvert implements MRI's rb_invcmp for String#<=>: when the operand is +// not a directly-comparable String it evaluates `other <=> self` and returns the +// negated Integer, or nil when that yields nil or a non-Integer. The (self, +// other) pair is guarded so a mutually-inverting operand +// (def other.<=>(x); x <=> self; end) returns nil rather than recurring forever. +func (vm *VM) spaceshipInvert(self, other object.Value) object.Value { + key := [2]object.Value{self, other} + if vm.invcmpPath[key] { + return object.NilV + } + if vm.invcmpPath == nil { + vm.invcmpPath = map[[2]object.Value]bool{} + } + vm.invcmpPath[key] = true + defer delete(vm.invcmpPath, key) + if r, ok := vm.send(other, "<=>", []object.Value{self}, nil).(object.Integer); ok { + return object.IntValue(-int64(r)) + } + return object.NilV +} + func reverseStr(s string) string { r := []rune(s) for i, j := 0, len(r)-1; i < j; i, j = i+1, j-1 { @@ -6301,7 +6374,13 @@ func stringToInt(s string, base int) object.Value { } } if base == 0 { - base = 10 + // With no base and no explicit 0x/0b/0o/0d prefix, a leading '0' selects + // octal (MRI: "017".to_i(0) == 15), everything else decimal. + if len(s) > 0 && s[0] == '0' { + base = 8 + } else { + base = 10 + } } var digits []byte prevDigit := false @@ -6431,36 +6510,62 @@ func accumInum(s string, i, base int, neg bool) object.Value { } // parseLeadingFloat mimics String#to_f: parse the longest leading float, 0.0 if -// none. +// none. A single underscore between two digits is permitted (and dropped) in the +// integer, fractional and exponent runs, matching MRI's rb_str_to_dbl. Only +// genuine ASCII whitespace (space, tab, newline, vtab, formfeed, CR) is skipped +// before the number — a NUL or other non-printable byte terminates it instead. func parseLeadingFloat(s string) float64 { - s = strings.TrimLeft(s, wsCutset) + s = strings.TrimLeft(s, " \t\n\v\f\r") + var b strings.Builder i := 0 - if i < len(s) && (s[i] == '+' || s[i] == '-') { - i++ + isDigit := func(c byte) bool { return c >= '0' && c <= '9' } + // scanDigits copies a run of digits into b, dropping a single underscore that + // sits between two digits; it reports whether it copied at least one digit. + scanDigits := func() bool { + any := false + for i < len(s) { + if isDigit(s[i]) { + b.WriteByte(s[i]) + i++ + any = true + continue + } + if s[i] == '_' && any && i+1 < len(s) && isDigit(s[i+1]) { + i++ // a single inter-digit underscore is dropped + continue + } + break + } + return any } - for i < len(s) && s[i] >= '0' && s[i] <= '9' { + if i < len(s) && (s[i] == '+' || s[i] == '-') { + b.WriteByte(s[i]) i++ } + scanDigits() if i < len(s) && s[i] == '.' { + b.WriteByte('.') i++ - for i < len(s) && s[i] >= '0' && s[i] <= '9' { - i++ - } + scanDigits() } if i < len(s) && (s[i] == 'e' || s[i] == 'E') { - j := i + 1 - if j < len(s) && (s[j] == '+' || s[j] == '-') { - j++ - } - k := j - for k < len(s) && s[k] >= '0' && s[k] <= '9' { - k++ + // The exponent is committed only when at least one digit follows the + // (optionally signed) marker; otherwise "5e" / "1.2e_x3" keep the mantissa. + pre := b.String() + save := i + b.WriteByte(s[i]) + i++ + if i < len(s) && (s[i] == '+' || s[i] == '-') { + b.WriteByte(s[i]) + i++ } - if k > j { - i = k + if !scanDigits() { + i = save + b.Reset() + b.WriteString(pre) } } - f, err := strconv.ParseFloat(s[:i], 64) + f, err := strconv.ParseFloat(b.String(), 64) if err != nil { return 0 } diff --git a/internal/vm/string_charset.go b/internal/vm/string_charset.go index 1db9becf..b8f28cc4 100644 --- a/internal/vm/string_charset.go +++ b/internal/vm/string_charset.go @@ -292,6 +292,23 @@ func leftPadZero(s string, width int) string { // it iterates numerically, preserving beg's width; otherwise it walks String#succ // results, stopping when the current value passes end. func stringUpto(beg, end string, excl bool, yield func(string)) { + // Both endpoints a single ASCII byte: iterate by codepoint so the run covers + // non-alphanumeric ASCII too ("9".upto("A") yields "9",":",…,"@","A"), matching + // MRI's single-character fast path rather than String#succ (which would jump + // "9" to "10" and stop on the length change). + if len(beg) == 1 && len(end) == 1 && beg[0] < 0x80 && end[0] < 0x80 { + for c := beg[0]; ; c++ { + if excl { + if c >= end[0] { + break + } + } else if c > end[0] { + break + } + yield(string(rune(c))) + } + return + } if allDigits(beg) && allDigits(end) { hi, _ := new(big.Int).SetString(end, 10) cur, _ := new(big.Int).SetString(beg, 10) diff --git a/internal/vm/string_method_wave11_test.go b/internal/vm/string_method_wave11_test.go new file mode 100644 index 00000000..2f653d9e --- /dev/null +++ b/internal/vm/string_method_wave11_test.go @@ -0,0 +1,106 @@ +package vm_test + +import ( + "strings" + "testing" +) + +// TestStringWave11 covers the non-encoding String conformance work of wave 11: +// the <=> / == coercion protocol, the built-in aliases, to_f underscores and +// terminals, to_i base handling, getbyte arity, upto codepoint iteration and +// coercion, and start_with? coercion / $~ setting. Each case is byte-exact +// against MRI ruby 4.x. +func TestStringWave11(t *testing.T) { + tests := []struct{ name, src, want string }{ + // --- built-in aliases share one Method record (== on UnboundMethod) --- + {"alias_next_succ", `p String.instance_method(:next) == String.instance_method(:succ)`, "true\n"}, + {"alias_next_bang", `p String.instance_method(:next!) == String.instance_method(:succ!)`, "true\n"}, + {"alias_intern", `p String.instance_method(:intern) == String.instance_method(:to_sym)`, "true\n"}, + {"alias_to_str", `p String.instance_method(:to_str) == String.instance_method(:to_s)`, "true\n"}, + + // --- String#<=> --- + {"cmp_plain_eq", `p("yep" <=> "yep")`, "0\n"}, + {"cmp_plain_lt", `p("a" <=> "b")`, "-1\n"}, + {"cmp_subclass", `class S < String; end; p("hello" <=> S.new("hello"))`, "0\n"}, + {"cmp_to_str", `o = Object.new; def o.to_str; "aaa"; end; p("abc" <=> o)`, "1\n"}, + {"cmp_invcmp_int", `o = Object.new; def o.<=>(x); -1; end; p("abc" <=> o)`, "1\n"}, + {"cmp_invcmp_nil", `p("a" <=> 5)`, "nil\n"}, + {"cmp_inverse_guard", `o = Object.new; def o.<=>(x); x <=> self; end; p("abc" <=> o)`, "nil\n"}, + + // --- String#== / operator fast path --- + {"eq_plain_true", `p("a" == "a")`, "true\n"}, + {"eq_plain_false", `p("a" == "b")`, "false\n"}, + {"eq_subclass", `class S2 < String; end; p("hello" == S2.new("hello"))`, "true\n"}, + {"eq_int_false", `p("hello" == 5)`, "false\n"}, + {"eq_sym_false", `p("hello" == :hello)`, "false\n"}, + {"eq_to_str_defer", `o = Object.new; def o.to_str; "x"; end; def o.==(x); true; end; p("hello" == o)`, "true\n"}, + {"eq_to_str_send", `o = Object.new; def o.to_str; "x"; end; def o.==(x); true; end; p("hello".send(:==, o))`, "true\n"}, + {"neq_to_str_defer", `o = Object.new; def o.to_str; "x"; end; def o.==(x); true; end; p("hello" != o)`, "false\n"}, + + // --- String#to_f: underscores, sign, exponent rollback, terminals --- + {"tof_underscore", `p("1_234_567.890_1".to_f == 1_234_567.890_1)`, "true\n"}, + {"tof_sign_exp_underscore", `p "-5_5e-5_0".to_f`, "-5.5e-49\n"}, + {"tof_nul_terminal", `p "\x001.2".to_f`, "0.0\n"}, + {"tof_e_rollback", `p "5e".to_f`, "5.0\n"}, + {"tof_dot_five_e", `p ".5e1".to_f`, "5.0\n"}, + {"tof_bad", `p "bad".to_f`, "0.0\n"}, + {"tof_double_us", `p "1__2".to_f`, "1.0\n"}, + {"tof_frac_us_terminal", `p "1.2e_x3".to_f`, "1.2\n"}, + {"tof_leading_us", `p "_9".to_f`, "0.0\n"}, + + // --- String#to_i: base 0 octal, base coercion --- + {"toi_base0_octal", `p "017".to_i(0)`, "15\n"}, + {"toi_base0_hex", `p "0x1f".to_i(0)`, "31\n"}, + {"toi_base0_decimal", `p "42".to_i(0)`, "42\n"}, + {"toi_base_float", `p "17".to_i(8.0)`, "15\n"}, + {"toi_base_to_int", `class B; def to_int; 8; end; end; p "17".to_i(B.new)`, "15\n"}, + + // --- String#getbyte arity --- + {"getbyte_ok", `p "abc".getbyte(1)`, "98\n"}, + + // --- String#upto: codepoint iteration + coercion --- + {"upto_ascii_span", `p "9".upto("A").to_a`, "[\"9\", \":\", \";\", \"<\", \"=\", \">\", \"?\", \"@\", \"A\"]\n"}, + {"upto_excl", `a = []; "a".upto("d", true) { |s| a << s }; p a`, "[\"a\", \"b\", \"c\"]\n"}, + {"upto_letters", `a = []; "a".upto("c") { |s| a << s }; p a`, "[\"a\", \"b\", \"c\"]\n"}, + {"upto_to_str", `o = Object.new; def o.to_str; "abd"; end; a = []; "abc".upto(o) { |s| a << s }; p a`, "[\"abc\", \"abd\"]\n"}, + {"upto_digits", `a = []; "8".upto("11") { |s| a << s }; p a`, "[\"8\", \"9\", \"10\", \"11\"]\n"}, + {"upto_digits_excl", `a = []; "8".upto("12", true) { |s| a << s }; p a`, "[\"8\", \"9\", \"10\", \"11\"]\n"}, + {"upto_multichar_excl", `a = []; "az".upto("bb", true) { |s| a << s }; p a`, "[\"az\", \"ba\"]\n"}, + {"upto_size_nil", `p "a".upto("b").size`, "nil\n"}, + + // --- String#start_with?: coercion + $~ --- + {"sw_plain", `p "hello".start_with?("he")`, "true\n"}, + {"sw_to_str", `o = Object.new; def o.to_str; "h"; end; p "hello".start_with?(o)`, "true\n"}, + {"sw_regexp_match", `r = "test-1337".start_with?(/test-(\d+)/); p r; p $1`, "true\n\"1337\"\n"}, + {"sw_regexp_nomatch", `r = "test-asdf".start_with?(/test-(\d+)/); p r; p $1`, "false\nnil\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) + } + }) + } +} + +// TestStringWave11Errors covers the TypeError / ArgumentError paths added in +// wave 11: getbyte arity, upto's non-convertible end, and start_with?'s +// non-convertible prefix. +func TestStringWave11Errors(t *testing.T) { + tests := []struct{ name, src, want string }{ + {"getbyte_zero_args", `"glark".getbyte`, "ArgumentError"}, + {"getbyte_two_args", `"food".getbyte(0, 0)`, "ArgumentError"}, + {"upto_int", `"abc".upto(123) { }`, "TypeError"}, + {"upto_symbol", `"a".upto(:c).to_a`, "TypeError"}, + {"start_with_int", `"hello".start_with?(1)`, "TypeError"}, + {"start_with_array", `"hello".start_with?(["h"])`, "TypeError"}, + } + for _, tc := range tests { + t.Run(tc.name, func(t *testing.T) { + err := runErr(t, tc.src) + if err == nil || !strings.Contains(err.Error(), tc.want) { + t.Errorf("src=%q: got err=%v, want containing %q", tc.src, err, tc.want) + } + }) + } +} diff --git a/internal/vm/vm.go b/internal/vm/vm.go index 95e426b8..2645917a 100644 --- a/internal/vm/vm.go +++ b/internal/vm/vm.go @@ -201,6 +201,12 @@ type VM struct { // recursion leaves the builtin and returns through method dispatch. cmpPath map[[2]*object.Array]bool + // invcmpPath guards String#<=> against an operand whose own #<=> inverts the + // comparison (def other.<=>(x); x <=> self; end): the (self, other) pair being + // inverted is recorded so re-entering it returns nil (MRI's rb_invcmp via + // rb_exec_recursive_paired) rather than recurring for ever. + invcmpPath map[[2]object.Value]bool + out io.Writer errOut io.Writer // $stderr/STDERR sink; defaults to out (no separate stream) main object.Value