diff --git a/internal/vm/array_edges.go b/internal/vm/array_edges.go index 8c19e9fd..f011d120 100644 --- a/internal/vm/array_edges.go +++ b/internal/vm/array_edges.go @@ -21,7 +21,8 @@ func (vm *VM) registerArrayEdges() { // does. vm.cArray.define("bsearch", func(vm *VM, self object.Value, _ []object.Value, blk *Proc) object.Value { if blk == nil { - return enumFor(self, "bsearch") + // MRI's bsearch Enumerator reports an unknown (nil) size. + return enumForSized(self, "bsearch", func(*VM) object.Value { return object.NilV }) } elems := self.(*object.Array).Elems if i, found := vm.arrayBsearchIndex(elems, blk); found { @@ -31,7 +32,8 @@ func (vm *VM) registerArrayEdges() { }) vm.cArray.define("bsearch_index", func(vm *VM, self object.Value, _ []object.Value, blk *Proc) object.Value { if blk == nil { - return enumFor(self, "bsearch_index") + // MRI's bsearch_index Enumerator reports an unknown (nil) size. + return enumForSized(self, "bsearch_index", func(*VM) object.Value { return object.NilV }) } if i, found := vm.arrayBsearchIndex(self.(*object.Array).Elems, blk); found { return object.IntValue(int64(i)) diff --git a/internal/vm/array_residuals_wave12_test.go b/internal/vm/array_residuals_wave12_test.go new file mode 100644 index 00000000..d02f7123 --- /dev/null +++ b/internal/vm/array_residuals_wave12_test.go @@ -0,0 +1,279 @@ +// Copyright (c) the go-embedded-ruby/ruby authors +// +// SPDX-License-Identifier: BSD-3-Clause + +package vm_test + +import ( + "strings" + "testing" +) + +// TestArrayAliasesAndArity covers the wave-12 residual fixes that align Array +// method identity and argument checking with MRI 4.0.6: #length is the same +// method record as #size, #clear/#map reject stray arguments, and Array.allocate +// yields a fully-formed empty Array while rejecting arguments. +func TestArrayAliasesAndArity(t *testing.T) { + runCases(t, []struct{ src, want string }{ + // #length is a true alias of #size (identical UnboundMethod record). + {`p(Array.instance_method(:length) == Array.instance_method(:size))`, "true\n"}, + {`p([1,2,3].length)`, "3\n"}, + // Array.allocate is a real, mutable, empty Array. + {`a = Array.allocate; p a.instance_of?(Array); p a.size; a << 1; p a`, + "true\n0\n[1]\n"}, + // A subclass allocates an (empty) instance of the subclass. + {`class SubA < Array; end; a = SubA.allocate; p a.class; p a.size`, + "SubA\n0\n"}, + }) +} + +// TestArrayArityErrors covers the ArgumentError paths added in wave 12: #clear +// and #map take no positional arguments, and Array.allocate takes none either. +func TestArrayArityErrors(t *testing.T) { + for _, src := range []string{ + `[1].clear(true)`, + `[1,2,3].map(:foo)`, + `Array.allocate(1)`, + } { + if err := runErr(t, src); err == nil || !strings.Contains(err.Error(), "ArgumentError") { + t.Errorf("src=%q expected ArgumentError, got %v", src, err) + } + } +} + +// TestArrayArefArithmeticSequence covers Array#[] indexed by an +// Enumerator::ArithmeticSequence: positive and negative steps, endless and +// beginless ranges, exclusive endpoints, negative indices, an empty result, and +// the begin-past-end nil result. Every expectation matches MRI 4.0.6. +func TestArrayArefArithmeticSequence(t *testing.T) { + runCases(t, []struct{ src, want string }{ + {`p([0,1,2,3,4,5][(0..).step(2)])`, "[0, 2, 4]\n"}, + {`p([0,1,2,3,4,5][(2..).step(-1)])`, "[2, 1, 0]\n"}, + {`p([0,1,2,3,4,5][(...0).step(-1)])`, "[5, 4, 3, 2, 1]\n"}, + {`p([0,1,2,3,4,5][(..0).step(-1)])`, "[5, 4, 3, 2, 1, 0]\n"}, + {`p([0,1,2,3,4,5][(1...3).step(2)])`, "[1]\n"}, + {`p([0,1,2,3,4,5][(-4..4).step(2)])`, "[2, 4]\n"}, + {`p([0,1,2,3,4,5][(1..3).step(-1)])`, "[]\n"}, // positive range, negative step + {`p([0,1,2,3,4,5][(6..).step(1)])`, "[]\n"}, // begin == length + {`p([0,1,2,3,4,5][(7..).step(1)])`, "nil\n"}, // begin past the end (step > 0) + {`p([0,1,2,3,4,5][(0..8).step(-1)])`, "nil\n"}, // begin past the end (step < 0) + {`p([0,1,2,3,4,5][(9..).step(-1)])`, "[5, 4, 3, 2, 1, 0]\n"}, + {`p([0,1,2,3,4,5][(-3..).step(-2)])`, "[3, 1]\n"}, + // A begin still out of the array after negative-index resolution is nil. + {`p([0,1,2,3,4,5][(-9..).step(1)])`, "nil\n"}, + {`p([0,1,2,3,4,5][(..-9).step(-1)])`, "nil\n"}, + }) +} + +// TestArrayArefArithSeqErrors covers the RangeError guard for a strided slice +// whose declared span reaches past the array (|step| > 1) and the ArgumentError +// for a step that truncates to zero (a fractional step). Verified against MRI. +func TestArrayArefArithSeqErrors(t *testing.T) { + cases := []struct{ src, want string }{ + {`[0,1,2,3,4,5][(0..6).step(2)]`, "RangeError"}, + {`[0,1,2,3,4,5][(2..8).step(2)]`, "RangeError"}, + {`[0,1,2,3,4,5][(8..2).step(-2)]`, "RangeError"}, + {`[0,1,2,3,4,5][(7..).step(2)]`, "RangeError"}, + {`[0,1,2,3,4,5][(-9..).step(2)]`, "RangeError"}, // begin below 0 after resolution + {`[0,1,2,3,4,5][(0..5).step(0.5)]`, "slice step cannot be zero"}, + } + for _, c := range cases { + if err := runErr(t, c.src); err == nil || !strings.Contains(err.Error(), c.want) { + t.Errorf("src=%q expected %q, got %v", c.src, c.want, err) + } + } +} + +// TestArrayArefRangeAndIndexEdges covers the remaining Array#[] residuals: a user +// Range subclass slices like a plain Range, a Range with a length argument is a +// TypeError, and an index or length beyond the Fixnum range raises RangeError +// (while an ordinary Float index truncates). Verified against MRI 4.0.6. +func TestArrayArefRangeAndIndexEdges(t *testing.T) { + runCases(t, []struct{ src, want string }{ + {`class MyR < Range; end; p([1,2,3,4][MyR.new(1,2)])`, "[2, 3]\n"}, + {`class MyR2 < Range; end; p([1,2,3,4][MyR2.new(-3,-1,true)])`, "[2, 3]\n"}, + {`p([10,20,30][1.9])`, "20\n"}, // ordinary Float index truncates + }) + errCases := []struct{ src, want string }{ + {`[1,2,3][1..2, 1]`, "TypeError"}, + {`[1,2,3,4,5,6][2.0**63]`, "RangeError"}, + {`[1,2,3,4,5,6][1, 8e19]`, "RangeError"}, + {`[1,2,3,4,5,6][1, -8e19]`, "RangeError"}, + } + for _, c := range errCases { + if err := runErr(t, c.src); err == nil || !strings.Contains(err.Error(), c.want) { + t.Errorf("src=%q expected %q, got %v", c.src, c.want, err) + } + } +} + +// TestArraySumKahan covers Array#sum's exact-then-Kahan folding: an exact integer +// sum, the Kahan-compensated float sum that reduce(:+) cannot match, a Float init +// seeding the Kahan accumulator, a non-numeric run through #+, and the NaN/Infinity +// rules of kahanAdd. Verified against MRI 4.0.6. +func TestArraySumKahan(t *testing.T) { + runCases(t, []struct{ src, want string }{ + {`p([1,2,3].sum)`, "6\n"}, + {`p([1,2,3].sum(10))`, "16\n"}, + {`p([1,2,3].sum { |i| i * 10 })`, "60\n"}, + {`p([].sum(0.0))`, "0.0\n"}, + // Kahan compensation: reduce(:+) drifts to 50.00000000000001, sum is exact. + {`floats = [2.7800000000000002, 5.0, 2.5, 4.44, 3.89, 3.89, 4.44, 7.78, 5.0, 2.7800000000000002, 5.0, 2.5]; p(floats.sum)`, + "50.0\n"}, + {`p([1, 2.5, 3].sum)`, "6.5\n"}, // exact -> float switch mid-array + {`p(["a","b","c"].sum(""))`, "\"abc\"\n"}, // non-numeric fold through #+ + // NaN / Infinity handling (kahanAdd branches). + {`p([1.0, Float::INFINITY].sum)`, "Infinity\n"}, + {`p([Float::INFINITY, 1.0].sum)`, "Infinity\n"}, + {`p([1.0, -Float::INFINITY].sum)`, "-Infinity\n"}, + {`p([1.0, Float::NAN].sum.nan?)`, "true\n"}, + {`p([Float::NAN, 1.0].sum.nan?)`, "true\n"}, + {`p([Float::INFINITY, -Float::INFINITY].sum.nan?)`, "true\n"}, + {`p([Float::INFINITY, Float::INFINITY].sum)`, "Infinity\n"}, + // Live-index loop folds a block-appended tail (integers stay exact). + {`a=[1,2]; i=0; r=a.sum{|e| a<<3 if i<1; i+=1; e}; p r`, "6\n"}, + }) + // A Float appearing after a non-numeric accumulator, and a non-numeric element + // after a float run, both raise TypeError (and exercise the fall-through paths). + for _, src := range []string{`[1.0].sum("")`, `[1.0, "x"].sum`} { + if err := runErr(t, src); err == nil || !strings.Contains(err.Error(), "TypeError") { + t.Errorf("src=%q expected TypeError, got %v", src, err) + } + } +} + +// TestArrayIndexAndFindIndex covers Array#index becoming a full #find_index (value, +// block and no-arg Enumerator forms) and a true alias of #find_index (shared method +// record), matching MRI 4.0.6. +func TestArrayIndexAndFindIndex(t *testing.T) { + runCases(t, []struct{ src, want string }{ + {`p([1,2,3,2].index(2))`, "1\n"}, + {`p([1,2,3,2].index { |x| x > 1 })`, "1\n"}, + {`p([1,2,3].index(9))`, "nil\n"}, + {`p([1,2,3].index.class)`, "Enumerator\n"}, + {`p([1,2,3].find_index { |x| x == 3 })`, "2\n"}, + {`p(Array.instance_method(:index) == Array.instance_method(:find_index))`, "true\n"}, + }) + if err := runErr(t, `[1,2,3].index(1, 2)`); err == nil || !strings.Contains(err.Error(), "ArgumentError") { + t.Errorf("index with 2 args: expected ArgumentError, got %v", err) + } +} + +// TestArrayPermutationLazyEnumerator covers Array#permutation returning a lazy +// Enumerator that re-reads self when iterated (so a later mutation is seen) and +// reports the descending-factorial #size. Verified against MRI 4.0.6. +func TestArrayPermutationLazyEnumerator(t *testing.T) { + runCases(t, []struct{ src, want string }{ + {`n=[1,2]; e=n.permutation; n<<3; p e.to_a.sort`, + "[[1, 2, 3], [1, 3, 2], [2, 1, 3], [2, 3, 1], [3, 1, 2], [3, 2, 1]]\n"}, + {`p([1,2,3].permutation.size)`, "6\n"}, + {`p([1,2,3].permutation(2).size)`, "6\n"}, + {`p([1,2,3].permutation(0).size)`, "1\n"}, + {`p([1,2,3].permutation(5).size)`, "0\n"}, // k > n + {`p([].permutation.size)`, "1\n"}, + {`p([1,2,3].permutation(2).to_a.length)`, "6\n"}, + {`p([1,2,3].permutation(5).to_a)`, "[]\n"}, // k > n yields nothing + }) +} + +// TestPackUnicode covers Array#pack('U') across every codepoint width of MRI's +// extended UTF-8 (1..6 bytes, including values above U+10FFFF that Go's own +// encoder would clamp), the count/'*' modifiers, and #to_int coercion of the +// argument. Every expectation was checked against MRI 4.0.6. +func TestPackUnicode(t *testing.T) { + runCases(t, []struct{ src, want string }{ + {`p([0x41].pack("U").bytes)`, "[65]\n"}, + {`p([0x85].pack("U").bytes)`, "[194, 133]\n"}, + {`p([0x3042].pack("U").bytes)`, "[227, 129, 130]\n"}, + {`p([0x110000].pack("U").bytes)`, "[244, 144, 128, 128]\n"}, + {`p([0x200000].pack("U").bytes)`, "[248, 136, 128, 128, 128]\n"}, + {`p([0x7FFFFFFF].pack("U").bytes)`, "[253, 191, 191, 191, 191, 191]\n"}, + {`p([65,66].pack("U*").bytes)`, "[65, 66]\n"}, + {`p([65,66,67].pack("U2").bytes)`, "[65, 66]\n"}, + {`p([65].pack("U").encoding.to_s)`, "\"UTF-8\"\n"}, + // #to_int coerces the argument. + {`o=Object.new; def o.to_int; 66; end; p([o].pack("U").bytes)`, "[66]\n"}, + }) + // Out-of-range and bad-coercion errors. + errCases := []struct{ src, want string }{ + {`[-1].pack("U")`, "RangeError"}, + {`[2**32].pack("U")`, "RangeError"}, // fits int64, exceeds 0x7FFFFFFF + {`[2**64].pack("U")`, "RangeError"}, // Bignum + {`o=Object.new; def o.to_int; "5"; end; [o].pack("U")`, "TypeError"}, + {`[Object.new].pack("U")`, "no implicit conversion of Object into Integer"}, + } + for _, c := range errCases { + if err := runErr(t, c.src); err == nil || !strings.Contains(err.Error(), c.want) { + t.Errorf("src=%q expected %q, got %v", c.src, c.want, err) + } + } +} + +// TestPackStringNil covers a/A/Z packing a nil argument as an empty string +// (padded with spaces for A, NULs for a/Z), matching MRI 4.0.6. +func TestPackStringNil(t *testing.T) { + runCases(t, []struct{ src, want string }{ + {`p([nil].pack("A3"))`, "\" \"\n"}, + {`p([nil].pack("a3"))`, "\"\\x00\\x00\\x00\"\n"}, + {`p([nil].pack("Z3"))`, "\"\\x00\\x00\\x00\"\n"}, + {`p([nil].pack("a*"))`, "\"\"\n"}, + }) +} + +// TestPackBuffer covers Array#pack's :buffer option: appending to the buffer's +// content, absolute repositioning with @, the buffer being the returned object, +// encoding preservation, and the TypeError / FrozenError guards. Verified against +// MRI 4.0.6. +func TestPackBuffer(t *testing.T) { + runCases(t, []struct{ src, want string }{ + {`b=+"123"; r=[65,66,67].pack("ccc", buffer: b); p r; p r.equal?(b)`, "\"123ABC\"\ntrue\n"}, + {`p([65,66,67].pack("@3ccc", buffer: +"123456"))`, "\"123ABC\"\n"}, + {`p([65,66,67].pack("@6ccc", buffer: +"123"))`, "\"123\\u0000\\u0000\\u0000ABC\"\n"}, + {`p([65,66,67].pack("@3ccc", buffer: +"1234567890"))`, "\"123ABC\"\n"}, + {`buf=''.encode(Encoding::ISO_8859_1); [65].pack("c", buffer: buf); p buf.encoding.to_s`, + "\"ISO-8859-1\"\n"}, + // A trailing keyword hash without :buffer is ignored (no buffer redirection). + {`p([65].pack("C", other: 1))`, "\"A\"\n"}, + }) + errCases := []struct{ src, want string }{ + {`[65].pack("ccc", buffer: [])`, "buffer must be String, not Array"}, + {`[65].pack("c", buffer: "x".freeze)`, "FrozenError"}, + } + for _, c := range errCases { + if err := runErr(t, c.src); err == nil || !strings.Contains(err.Error(), c.want) { + t.Errorf("src=%q expected %q, got %v", c.src, c.want, err) + } + } +} + +// TestPackPointer covers the P/p (pointer) directives: pack emits a native-width +// word and unpack recovers the registered string (P by leading count, p whole); +// nil packs a null pointer that unpacks to nil, and reads it as integer 0. +// Verified against MRI 4.0.6. +func TestPackPointer(t *testing.T) { + runCases(t, []struct{ src, want string }{ + {`p(["hello"].pack("P").size == [0].pack("J").size)`, "true\n"}, + {`p(["hello"].pack("P").unpack("P5"))`, "[\"hello\"]\n"}, + {`p(["hello"].pack("p").unpack("p"))`, "[\"hello\"]\n"}, + {`p(["hello"].pack("P").unpack("P3"))`, "[\"hel\"]\n"}, // P count truncation + {`p([nil].pack("P").unpack("J"))`, "[0]\n"}, + {`p([nil].pack("P").unpack("P5"))`, "[nil]\n"}, // null pointer unpacks to nil + {`p("".unpack("P"))`, "[]\n"}, // too few bytes -> no element + {`p(["hi"].pack("P").unpack("P10"))`, "[\"hi\"]\n"}, // count clamped to length + }) +} + +// TestArrayEnumeratorUnknownSize covers the wave-12 fix that gives the block-less +// #bsearch / #bsearch_index / #rindex Enumerators an unknown (nil) size, as MRI +// does, while the ordinary #map Enumerator keeps its known size. +func TestArrayEnumeratorUnknownSize(t *testing.T) { + runCases(t, []struct{ src, want string }{ + {`p([1,2,3].bsearch.size)`, "nil\n"}, + {`p([1,2,3].bsearch_index.size)`, "nil\n"}, + {`p([1,2,3].rindex.size)`, "nil\n"}, + {`p([1,2,3].map.size)`, "3\n"}, + // The unsized Enumerators still drive their search when iterated. + {`p([1,2,3,4].bsearch.each { |x| x >= 3 })`, "3\n"}, + {`p([1,2,3,4].bsearch_index.each { |x| x >= 3 })`, "2\n"}, + }) +} diff --git a/internal/vm/builtins.go b/internal/vm/builtins.go index 513f5de6..1ea4eb48 100644 --- a/internal/vm/builtins.go +++ b/internal/vm/builtins.go @@ -2514,12 +2514,12 @@ func (vm *VM) bootstrap() { }) // Array. - vm.cArray.define("length", func(_ *VM, self object.Value, _ []object.Value, _ *Proc) object.Value { - return object.IntValue(int64(len(self.(*object.Array).Elems))) - }) vm.cArray.define("size", func(_ *VM, self object.Value, _ []object.Value, _ *Proc) object.Value { return object.IntValue(int64(len(self.(*object.Array).Elems))) }) + // #length is a true alias of #size (shares the exact method record, so + // Array.instance_method(:length) == Array.instance_method(:size), as in MRI). + aliasBuiltin(vm.cArray, "length", "size") vm.cArray.define("empty?", func(_ *VM, self object.Value, _ []object.Value, _ *Proc) object.Value { return object.Bool(len(self.(*object.Array).Elems) == 0) }) @@ -2585,6 +2585,21 @@ func (vm *VM) bootstrap() { arrayInit(vm, arr, args, blk) return arr }} + // Array.allocate returns a fully-formed, empty Array (not the generic + // Class#allocate RObject, which would make Array#size/#<< fail). It takes no + // arguments; a subclass allocation wraps the empty array without running + // #initialize, matching MRI. + vm.cArray.smethods["allocate"] = &Method{name: "allocate", owner: vm.cArray, + native: func(vm *VM, self object.Value, args []object.Value, _ *Proc) object.Value { + if len(args) != 0 { + raise("ArgumentError", "wrong number of arguments (given %d, expected 0)", len(args)) + } + arr := object.NewArray() + if recv, ok := self.(*RClass); ok && recv != vm.cArray { + return &RObject{class: recv, ivars: map[string]object.Value{}, builtin: arr} + } + return arr + }} // Array[a, b, c] (and any subclass's inherited []) builds an array from its // arguments. On a subclass it wraps the elements in a subclass instance without // calling #initialize, matching MRI's Array.[]. @@ -2942,7 +2957,10 @@ func (vm *VM) bootstrap() { } return a }) - vm.cArray.define("clear", func(_ *VM, self object.Value, _ []object.Value, _ *Proc) object.Value { + vm.cArray.define("clear", func(_ *VM, self object.Value, args []object.Value, _ *Proc) object.Value { + if len(args) != 0 { + raise("ArgumentError", "wrong number of arguments (given %d, expected 0)", len(args)) + } a := self.(*object.Array) vm.checkArrayFrozen(a) a.Elems = nil @@ -2996,6 +3014,13 @@ func (vm *VM) bootstrap() { // fast path may hand them the live operand-stack region (defineNR). arrayAref := func(vm *VM, self object.Value, args []object.Value, _ *Proc) object.Value { a := self.(*object.Array) + // a[(m..n).step(k)] — an Enumerator::ArithmeticSequence selects a strided + // slice (MRI 2.6+). + if len(args) == 1 { + if e, ok := args[0].(*Enumerator); ok && e.isArithSeq { + return vm.arrayArefArithSeq(a, e) + } + } start, length, isSpan, ok := vm.arrayArefSpan(a, args) if !ok { return object.NilV @@ -3215,7 +3240,10 @@ func (vm *VM) bootstrap() { } return a }) - vm.cArray.define("map", func(vm *VM, self object.Value, _ []object.Value, blk *Proc) object.Value { + vm.cArray.define("map", func(vm *VM, self object.Value, args []object.Value, blk *Proc) object.Value { + if len(args) != 0 { + raise("ArgumentError", "wrong number of arguments (given %d, expected 0)", len(args)) + } if blk == nil { return enumFor(self, "map") } @@ -3412,20 +3440,11 @@ func (vm *VM) bootstrap() { return self }) vm.cArray.define("sum", func(vm *VM, self object.Value, args []object.Value, blk *Proc) object.Value { - acc := object.Value(object.IntValue(0)) + init := object.Value(object.IntValue(0)) if len(args) > 0 { - acc = args[0] + init = args[0] } - a := self.(*object.Array) - // Live-index loop so a block that grows the array folds the new tail too. - for i := 0; i < len(a.Elems); i++ { - e := a.Elems[i] - if blk != nil { // sum { |x| ... } maps each element before adding - e = vm.callBlock(blk, []object.Value{e}) - } - acc = vm.binaryOp(bytecode.OpAdd, acc, e) - } - return acc + return vm.arraySum(self.(*object.Array), init, blk) }) vm.cArray.define("to_h", func(vm *VM, self object.Value, args []object.Value, blk *Proc) object.Value { if len(args) != 0 { @@ -3624,6 +3643,18 @@ func (vm *VM) bootstrap() { return self }) vm.cArray.define("permutation", func(vm *VM, self object.Value, args []object.Value, blk *Proc) object.Value { + // No block: a lazy Enumerator that re-reads self when iterated (MRI sees a + // later mutation of the array), with an explicit descending-factorial #size. + if blk == nil { + return enumForSized(self, "permutation", func(vm *VM) object.Value { + n := len(self.(*object.Array).Elems) + kk := n + if len(args) > 0 { + kk = int(vm.toIntCoerce(args[0])) + } + return object.NormInt(permutationCountBig(n, kk)) + }, args...) + } elems := self.(*object.Array).Elems k := len(elems) if len(args) > 0 { @@ -3653,9 +3684,6 @@ func (vm *VM) bootstrap() { } gen(0) } - if blk == nil { - return enumFor(object.NewArrayFromSlice(perms), "each") - } for _, pr := range perms { vm.callBlock(blk, []object.Value{pr}) } @@ -3808,14 +3836,34 @@ func (vm *VM) bootstrap() { } return vm.arrayJoin(a, sep, map[*object.Array]bool{}) }) - vm.cArray.define("index", func(vm *VM, self object.Value, args []object.Value, _ *Proc) object.Value { - for i, e := range self.(*object.Array).Elems { - if vm.vmValueEqual(e, args[0]) { + // index(obj) → first index whose element == obj; index { |e| … } → first index + // whose block is truthy (an argument, if given, wins over the block); no + // argument and no block → an Enumerator. #find_index is a true alias (shared + // record, so Array.instance_method(:index) == Array.instance_method(:find_index), + // as in MRI — both are the same C function there). The live-index loop lets a + // block that grows the array scan the appended tail too. + vm.cArray.define("index", func(vm *VM, self object.Value, args []object.Value, blk *Proc) object.Value { + if len(args) > 1 { + raise("ArgumentError", "wrong number of arguments (given %d, expected 0..1)", len(args)) + } + a := self.(*object.Array) + if len(args) == 0 && blk == nil { + return enumFor(self, "index") + } + for i := 0; i < len(a.Elems); i++ { + var match bool + if len(args) > 0 { + match = vm.vmValueEqual(a.Elems[i], args[0]) + } else { + match = vm.callBlock(blk, []object.Value{a.Elems[i]}).Truthy() + } + if match { return object.IntValue(int64(i)) } } return object.NilV }) + aliasBuiltin(vm.cArray, "find_index", "index") // rindex searches backward from the end: rindex(obj) → the last index whose // element == obj; rindex { |e| … } → the last index whose block is truthy (an // argument, if given, wins over the block); no argument and no block → a sized @@ -3823,7 +3871,8 @@ func (vm *VM) bootstrap() { vm.cArray.define("rindex", func(vm *VM, self object.Value, args []object.Value, blk *Proc) object.Value { a := self.(*object.Array) if len(args) == 0 && blk == nil { - return enumForSized(self, "rindex", func(*VM) object.Value { return object.IntValue(int64(len(a.Elems))) }) + // MRI's rindex Enumerator reports an unknown (nil) size. + return enumForSized(self, "rindex", func(*VM) object.Value { return object.NilV }) } for i := len(a.Elems) - 1; i >= 0; i-- { // A block may shrink the array; realign to the new end and re-check size @@ -6114,14 +6163,20 @@ func (vm *VM) fisherYates(s []object.Value, rng object.Value) { } func (vm *VM) arrayArefSpan(a *object.Array, args []object.Value) (start, length int, isSpan, ok bool) { - if rng, isR := args[0].(*object.Range); isR { + // A Range (or a user subclass of Range, e.g. ArraySpecs::MyRange) selects a + // span. Passing a Range together with a length is a TypeError (MRI tries to + // coerce the Range to the integer index of an a[i, len] call and fails). + if rng, isR := asRangeValue(args[0]); isR { + if len(args) == 2 { + raise("TypeError", "no implicit conversion of Range into Integer") + } // A non-nil, non-Integer endpoint converts via #to_int (e.g. a[obj..obj]). s, l, r := sliceRange(len(a.Elems), vm.coerceRangeBounds(rng)) return s, l, true, r } if len(args) == 2 { // a[start, len] — start and length convert via #to_int. - s := normIndex(vm.repeatLong(args[0]), len(a.Elems)) - l := int(vm.repeatLong(args[1])) + s := normIndex(vm.arrayArefLong(args[0]), len(a.Elems)) + l := int(vm.arrayArefLong(args[1])) if s < 0 || s > len(a.Elems) || l < 0 { return 0, 0, true, false } @@ -6130,12 +6185,120 @@ func (vm *VM) arrayArefSpan(a *object.Array, args []object.Value) (start, length } return s, l, true, true } - if i, isok := arrayIndex(a, vm.repeatLong(args[0])); isok { + if i, isok := arrayIndex(a, vm.arrayArefLong(args[0])); isok { return i, 1, false, true } return 0, 0, false, false } +// arrayArefLong coerces an Array#[] index or length argument to an int64 with +// MRI's out-of-Fixnum-range semantics: a Bignum, or a Float whose magnitude +// reaches 2**63, raises RangeError rather than silently overflowing the int64 +// conversion (a[2.0**63] and a[1, 8e19] both raise). Any smaller Float truncates +// toward zero and every other value follows the usual #to_int coercion. +func (vm *VM) arrayArefLong(v object.Value) int64 { + if f, ok := v.(object.Float); ok { + d := float64(f) + if !math.IsNaN(d) && !math.IsInf(d, 0) && + (d >= 9223372036854775808.0 || d < -9223372036854775808.0) { + raise("RangeError", "float %g out of range of integer", d) + } + } + return vm.repeatLong(v) +} + +// arrayArefArithSeq implements Array#[] indexed by an Enumerator::ArithmeticSequence +// (e.g. a[(0..).step(2)] or a[(5..).step(-2)]). It selects the array elements at +// the sequence's begin, begin+step, … positions bounded by its range, resolving +// negative and endless/beginless endpoints against the array length. A begin +// position past the end yields nil; when |step| > 1 and the sequence's declared +// span extends past the array, MRI raises RangeError. Verified against MRI 4.0.6. +func (vm *VM) arrayArefArithSeq(a *object.Array, e *Enumerator) object.Value { + n := len(a.Elems) + step := int(vm.repeatLong(e.asStep)) + if step == 0 { + // A fractional step (e.g. .step(0.5)) truncates to 0; MRI rejects it here. + raise("ArgumentError", "slice step cannot be zero") + } + // RangeError guard for |step| > 1: replicate MRI's strict beg/len computation + // (the endpoints swap for a negative step) and reject a span whose begin or + // length reaches beyond the array. + if step > 1 || step < -1 { + sb, se, sExcl := e.asBegin, e.asEnd, e.asExcl + if step < 0 { + sb, se = se, sb + } + beg := int64(0) + if !object.IsNil(sb) { + if beg = vm.repeatLong(sb); beg < 0 { + beg += int64(n) + } + } + end := int64(-1) + if object.IsNil(se) { + sExcl = false + } else if end = vm.repeatLong(se); end < 0 { + end += int64(n) + } + if !sExcl { + end++ + } + clen := end - beg + if clen < 0 { + clen = 0 + } + if beg < 0 || beg > int64(n) || clen > int64(n) { + raise("RangeError", "Enumerator::ArithmeticSequence has too large index") + } + } + // Orient the range so [loV, hiV] runs low→high: for a negative step the + // range's begin/end swap and its (end-attached) exclusivity moves to the low + // bound; the walk then runs downward from the top of the span. + var loV, hiV object.Value + var exclLow, exclHigh bool + if step < 0 { + loV, hiV, exclLow = e.asEnd, e.asBegin, e.asExcl + } else { + loV, hiV, exclHigh = e.asBegin, e.asEnd, e.asExcl + } + lo := 0 + if !object.IsNil(loV) { + // A low bound (the walk's conceptual begin) still out of the array after + // resolving a negative index — below 0 or past the end — is nil, exactly + // like a[k..] with k < -length or k > length. + if lo = normIndex(vm.repeatLong(loV), n); lo < 0 || lo > n { + return object.NilV + } + } + if exclLow { + lo++ + } + hi := n + if !object.IsNil(hiV) { + if hi = normIndex(vm.repeatLong(hiV), n); !exclHigh { + hi++ + } + if hi > n { + hi = n + } + } + out := []object.Value{} + if step > 0 { + for i := lo; i < hi; i += step { + if i >= 0 && i < n { + out = append(out, a.Elems[i]) + } + } + } else { + for i := hi - 1; i >= lo; i += step { + if i >= 0 && i < n { + out = append(out, a.Elems[i]) + } + } + } + return object.NewArrayFromSlice(out) +} + // Kernel#puts/print/p write through the current $stdout (an IOObj), so a host // or program that reassigns $stdout — e.g. to a StringIO — captures the output, // as in MRI. The puts array-flattening/newline logic lives in io.go (ioPuts). @@ -7722,6 +7885,82 @@ const maxFillSize = 1 << 40 // end (or start) is past the current length, and returns self. The block form // stores block(i) at each index, so a block raising mid-iteration leaves the // already-filled elements in place (the array is never truncated). +// arraySum implements Array#sum with MRI's exact-then-Kahan folding: Integer, +// Bignum and Rational elements accumulate exactly through Ruby #+, the first +// Float switches to Kahan–Babuška–Neumaier compensated summation (so a run of +// floats sums without drift — [x, y, …].sum is exact where reduce(:+) is not), +// and a non-numeric element (or accumulator) falls back to plain #+. A block, if +// given, maps each element before it is added; the live-index loop folds a tail +// the block appends. Verified against MRI 4.0.6. +func (vm *VM) arraySum(a *object.Array, init object.Value, blk *Proc) object.Value { + const ( + sumExact = iota + sumFloat + sumGeneric + ) + v := init + phase := sumExact + var f, c float64 + if fv, ok := init.(object.Float); ok { + phase, f = sumFloat, float64(fv) + } + for i := 0; i < len(a.Elems); i++ { + e := a.Elems[i] + if blk != nil { // sum { |x| ... } maps each element before adding + e = vm.callBlock(blk, []object.Value{e}) + } + if phase == sumFloat { + if x, ok := toFloat(e); ok { + f, c = kahanAdd(f, c, x) + continue + } + // A non-numeric element ends the Kahan run: materialise the sum so far + // and keep folding the rest through Ruby #+. + v, phase = object.Float(f+c), sumGeneric + } else if phase == sumExact { + if x, ok := e.(object.Float); ok { + if fv, okv := toFloat(v); okv { + f, c = kahanAdd(fv, 0, float64(x)) + phase = sumFloat + continue + } + } + } + v = vm.binaryOp(bytecode.OpAdd, v, e) + } + if phase == sumFloat { + return object.Float(f + c) + } + return v +} + +// kahanAdd performs one Kahan–Babuška–Neumaier compensated-summation step, +// adding x to the running sum f with running compensation c and returning the +// updated pair. NaN and infinities follow MRI's Array#sum rules: a NaN in either +// operand makes the sum NaN, +∞ added to −∞ is NaN, and any other infinity wins. +func kahanAdd(f, c, x float64) (float64, float64) { + switch { + case math.IsNaN(f): + return f, c + case math.IsNaN(x): + return x, c + case math.IsInf(x, 0): + if math.IsInf(f, 0) && math.Signbit(x) != math.Signbit(f) { + return math.NaN(), c + } + return x, c + case math.IsInf(f, 0): + return f, c + } + t := f + x + if math.Abs(f) >= math.Abs(x) { + c += (f - t) + x + } else { + c += (x - t) + f + } + return t, c +} + func arrayFill(vm *VM, self object.Value, args []object.Value, blk *Proc) object.Value { a := self.(*object.Array) vm.checkArrayFrozen(a) @@ -8120,6 +8359,21 @@ func binomialBig(n, k int) *big.Int { return res } +// permutationCountBig returns the number of k-permutations of n items, +// n!/(n-k)! (the descending factorial n·(n-1)·…·(n-k+1)), as a big.Int — the +// #size of Array#permutation. It is 1 when k is 0 (including the empty receiver's +// P(0, 0)) and 0 once k exceeds n or is negative. +func permutationCountBig(n, k int) *big.Int { + if k < 0 || k > n { + return big.NewInt(0) + } + res := big.NewInt(1) + for i := 0; i < k; i++ { + res.Mul(res, big.NewInt(int64(n-i))) + } + return res +} + // isJoinImmediate reports whether a value is a simple immediate whose #to_s // rendering can be taken directly during Array#join (no #to_str/#to_ary path). func isJoinImmediate(v object.Value) bool { diff --git a/internal/vm/pack.go b/internal/vm/pack.go index d102532d..bf3576f4 100644 --- a/internal/vm/pack.go +++ b/internal/vm/pack.go @@ -4,11 +4,42 @@ import ( binpkg "encoding/binary" "math" "math/big" + "sync" "unicode/utf8" "github.com/go-embedded-ruby/ruby/internal/object" ) +// ptrSpec is the native-width encoding of a machine pointer, shared by the P/p +// directives (an 8-byte native-endian word on the LP64 platforms rbgo targets). +var ptrSpec = intSpec{8, binpkg.NativeEndian, false} + +// packPtrTable backs the P/p (pointer) directives. A real machine pointer cannot +// round-trip through pure Go, so pack registers the referenced string under a +// synthetic, monotonically increasing id and emits that id as a native-width +// word; unpack looks the string back up. Ids are never reused (so a value packed +// and later unpacked resolves), and a null pointer is the reserved id 0. +var packPtrTable = struct { + mu sync.Mutex + m map[uint64]*object.String + next uint64 +}{m: map[uint64]*object.String{}} + +func registerPackPtr(s *object.String) uint64 { + packPtrTable.mu.Lock() + defer packPtrTable.mu.Unlock() + packPtrTable.next++ + packPtrTable.m[packPtrTable.next] = s + return packPtrTable.next +} + +func lookupPackPtr(id uint64) (*object.String, bool) { + packPtrTable.mu.Lock() + defer packPtrTable.mu.Unlock() + s, ok := packPtrTable.m[id] + return s, ok +} + // registerPackUnpack installs Array#pack and String#unpack/#unpack1, supporting // the common format directives with MRI-compatible semantics: // @@ -31,10 +62,23 @@ import ( // long, i.e. 64-bit on LP64). Each directive takes an optional count N, or '*' // for "all remaining"; spaces in the format are ignored. func (vm *VM) registerPackUnpack() { - vm.cArray.define("pack", func(_ *VM, self object.Value, args []object.Value, _ *Proc) object.Value { + vm.cArray.define("pack", func(vm *VM, self object.Value, args []object.Value, _ *Proc) object.Value { elems := self.(*object.Array).Elems - fmtStr := vm.packFormat(args) - out, enc := vm.packBytes(elems, fmtStr) + // The buffer: keyword (a trailing kwargs Hash) directs the packed bytes into + // an existing String, which is mutated and returned. Packing begins at the + // buffer's current end (an @ directive then repositions absolutely), so the + // result keeps the buffer's leading content and encoding. + buf := vm.packBuffer(args) + var initial []byte + if buf != nil { + vm.checkFrozen(buf) + initial = buf.Bytes() + } + out, enc := vm.packBytes(elems, vm.packFormat(args), initial) + if buf != nil { + buf.SetBytes(out) + return buf + } return object.NewStringBytesEnc(out, enc) }) vm.cString.define("unpack", func(_ *VM, self object.Value, args []object.Value, _ *Proc) object.Value { @@ -70,6 +114,26 @@ func (vm *VM) packFormat(args []object.Value) string { return "" } +// packBuffer returns the String named by Array#pack's buffer: keyword (a trailing +// kwargs Hash), or nil when the option is absent. A non-String buffer is a +// TypeError. packFormat still reads the format from args[0]; the trailing Hash it +// simply ignores. +func (vm *VM) packBuffer(args []object.Value) *object.String { + h := trailingKwHash(args) + if h == nil { + return nil + } + v, ok := h.Get(object.Symbol("buffer")) + if !ok { + return nil + } + s, isStr := v.(*object.String) + if !isStr { + raise("TypeError", "buffer must be String, not %s", classNameOf(v)) + } + return s +} + // packDir is one parsed directive: its letter, a count, whether the count was // the '*' wildcard ("all remaining"), and the byte-order / native-size // modifiers that followed the letter. @@ -176,7 +240,7 @@ func isPackCode(c byte) bool { case 'C', 'c', 'S', 's', 'L', 'l', 'Q', 'q', 'I', 'i', 'J', 'j', 'n', 'N', 'v', 'V', 'a', 'A', 'Z', 'H', 'h', 'U', 'f', 'F', 'd', 'D', 'e', 'E', 'g', 'G', - 'x', 'X', '@', 'b', 'B', 'm', 'M', 'u', 'w': + 'x', 'X', '@', 'b', 'B', 'm', 'M', 'u', 'w', 'P', 'p': return true } return false @@ -376,10 +440,62 @@ func (vm *VM) packIntArg(v object.Value) uint64 { return 0 } +// packUnicodeArg coerces a 'U' (Unicode codepoint) pack argument to a codepoint +// in the range MRI's UTF-8 packer accepts: an Integer is used directly, any other +// object is converted through #to_int (which must return an Integer, else +// TypeError). A negative value or one above 0x7FFFFFFF is out of range (RangeError). +func (vm *VM) packUnicodeArg(v object.Value) uint32 { + switch n := v.(type) { + case object.Integer: + if n < 0 || int64(n) > 0x7FFFFFFF { + raise("RangeError", "pack(U): value out of range") + } + return uint32(n) + case *object.Bignum: + // A Bignum is always outside int64, hence far above 0x7FFFFFFF (or below 0). + raise("RangeError", "pack(U): value out of range") + } + if vm.respondsToDynamic(v, "to_int") { + r := vm.send(v, "to_int", nil, nil) + switch r.(type) { + case object.Integer, *object.Bignum: + return vm.packUnicodeArg(r) + } + raise("TypeError", "can't convert %s to Integer (%s#to_int gives %s)", + classNameOf(v), classNameOf(v), classNameOf(r)) + } + raise("TypeError", "no implicit conversion of %s into Integer", classNameOf(v)) + return 0 +} + +// appendPackU appends the MRI extended-UTF-8 encoding of a codepoint (up to +// 0x7FFFFFFF, six bytes) to out. Unlike Go's utf8.AppendRune it does not clamp at +// U+10FFFF, so [0x110000].pack("U") yields the raw four-byte form MRI produces. +func appendPackU(out []byte, uv uint32) []byte { + switch { + case uv <= 0x7f: + return append(out, byte(uv)) + case uv <= 0x7ff: + return append(out, byte(0xc0|(uv>>6)), byte(0x80|(uv&0x3f))) + case uv <= 0xffff: + return append(out, byte(0xe0|(uv>>12)), byte(0x80|((uv>>6)&0x3f)), byte(0x80|(uv&0x3f))) + case uv <= 0x1fffff: + return append(out, byte(0xf0|(uv>>18)), byte(0x80|((uv>>12)&0x3f)), + byte(0x80|((uv>>6)&0x3f)), byte(0x80|(uv&0x3f))) + case uv <= 0x3ffffff: + return append(out, byte(0xf8|(uv>>24)), byte(0x80|((uv>>18)&0x3f)), + byte(0x80|((uv>>12)&0x3f)), byte(0x80|((uv>>6)&0x3f)), byte(0x80|(uv&0x3f))) + default: // uv <= 0x7fffffff + return append(out, byte(0xfc|(uv>>30)), byte(0x80|((uv>>24)&0x3f)), + byte(0x80|((uv>>18)&0x3f)), byte(0x80|((uv>>12)&0x3f)), + byte(0x80|((uv>>6)&0x3f)), byte(0x80|(uv&0x3f))) + } +} + // packBytes serialises elems according to fmtStr. -func (vm *VM) packBytes(elems []object.Value, fmtStr string) ([]byte, string) { +func (vm *VM) packBytes(elems []object.Value, fmtStr string, initial []byte) ([]byte, string) { dirs := parseFormat(fmtStr, "pack") - out := []byte{} + out := append([]byte{}, initial...) idx := 0 next := func() object.Value { if idx >= len(elems) { @@ -420,7 +536,7 @@ func (vm *VM) packBytes(elems []object.Value, fmtStr string) ([]byte, string) { count = len(elems) - idx } for k := 0; k < count; k++ { - out = utf8.AppendRune(out, rune(toInt(next()))) + out = appendPackU(out, vm.packUnicodeArg(next())) } case d.code == 'x': n := d.count @@ -452,7 +568,14 @@ func (vm *VM) packBytes(elems []object.Value, fmtStr string) ([]byte, string) { } } case d.code == 'a' || d.code == 'A' || d.code == 'Z': - out = packString(out, d, vm.packStrArg(next())) + // A nil argument packs as an empty string (padded with spaces for A, + // NULs for a/Z), matching MRI — it is not coerced via #to_str. + arg := next() + var b []byte + if !object.IsNil(arg) { + b = vm.packStrArg(arg) + } + out = packString(out, d, b) case d.code == 'H' || d.code == 'h': out = packHex(out, d, vm.packStrArg(next())) case d.code == 'B' || d.code == 'b': @@ -475,6 +598,17 @@ func (vm *VM) packBytes(elems []object.Value, fmtStr string) ([]byte, string) { } out = packBER(out, z) } + case d.code == 'P' || d.code == 'p': + // Pointer: emit a native-width word standing in for a pointer to the + // argument string (nil is a null pointer). The referenced string is + // registered so a later unpack("P"/"p") can recover it. + arg := next() + var id uint64 + if !object.IsNil(arg) { + b := vm.packStrArg(arg) + id = registerPackPtr(object.NewStringBytesEnc(append([]byte(nil), b...), "ASCII-8BIT")) + } + out = putUint(out, id, ptrSpec) } } return out, packEncoding(dirs) @@ -1087,6 +1221,31 @@ func unpackElems(data []byte, fmtStr string) []object.Value { z, pos = berDecodeAt(data, pos) out = append(out, object.NormInt(z)) } + case d.code == 'P' || d.code == 'p': + // Pointer: read a native-width word and recover the string pack + // registered for it. An unknown or null pointer unpacks to nil; 'P' + // takes the leading count bytes, 'p' the whole registered string. + // Too few bytes for a whole pointer yields no element at all (MRI). + w := ptrSpec.width + if pos+w > len(data) { + continue + } + id := getUint(data[pos:pos+w], ptrSpec) + pos += w + s, ok := lookupPackPtr(id) + switch { + case !ok: + out = append(out, object.NilV) + case d.code == 'p': + out = append(out, object.NewStringBytesEnc(append([]byte(nil), s.Bytes()...), "ASCII-8BIT")) + default: // 'P' takes the leading count bytes + b := s.Bytes() + n := d.count + if n > len(b) { + n = len(b) + } + out = append(out, object.NewStringBytesEnc(append([]byte(nil), b[:n]...), "ASCII-8BIT")) + } } } return out