From 30954788d580494cd8888679158abe2d7461adc2 Mon Sep 17 00:00:00 2001 From: tannevaled Date: Fri, 4 Sep 2026 12:08:31 +0200 Subject: [PATCH 01/11] Array: live-index iteration for each/map/select/sum/to_h/sort_by/take_while Array#each and the collect/filter family visited elements by ranging over a snapshot of the slice header, so elements a block appended during iteration were not yielded. Ruby's Array iteration is index-based (visit while i < length), so a growing array has its new tail seen. Switch each, map, select, sum, to_h, sort_by and take_while to a live-index loop. Co-Authored-By: Claude Opus 4.8 --- internal/vm/builtins.go | 59 ++++++++++++++++++++++++++++------------- 1 file changed, 41 insertions(+), 18 deletions(-) diff --git a/internal/vm/builtins.go b/internal/vm/builtins.go index 7c9046b0..8f513d60 100644 --- a/internal/vm/builtins.go +++ b/internal/vm/builtins.go @@ -3087,9 +3087,11 @@ func (vm *VM) bootstrap() { if blk.native != nil { // A synthesized native block (e.g. &:to_s, a Go-compiled AOT closure) // runs opaque Go code that could retain the args slice, so each yield - // gets its own fresh slice. - for _, e := range a.Elems { - vm.callBlock(blk, []object.Value{e}) + // gets its own fresh slice. Iterate by live index (not range, which + // snapshots the slice header) so elements the block appends are seen — + // Ruby's Array#each visits by an internal index while i < length. + for i := 0; i < len(a.Elems); i++ { + vm.callBlock(blk, []object.Value{a.Elems[i]}) } return a } @@ -3098,10 +3100,12 @@ func (vm *VM) bootstrap() { // synchronously at frame entry, before any block bytecode runs, and never // aliases the passed slice. So a single 1-element scratch slice, private to // this call (re-entrant each gets its own), can be reused across iterations - // without a capturing block observing the next iteration's overwrite. + // without a capturing block observing the next iteration's overwrite. The + // live-index loop lets the block grow the array and have the new tail + // yielded, matching Ruby. scratch := make([]object.Value, 1) - for _, e := range a.Elems { - scratch[0] = e + for i := 0; i < len(a.Elems); i++ { + scratch[0] = a.Elems[i] vm.callBlock(blk, scratch) } return a @@ -3111,9 +3115,11 @@ func (vm *VM) bootstrap() { return enumFor(self, "map") } a := self.(*object.Array) - out := make([]object.Value, len(a.Elems)) - for i, e := range a.Elems { - out[i] = vm.callBlock(blk, []object.Value{e}) + // Live-index loop: a block that grows the array has the new tail mapped + // too (Ruby's Array#map tolerates size increase during iteration). + out := make([]object.Value, 0, len(a.Elems)) + for i := 0; i < len(a.Elems); i++ { + out = append(out, vm.callBlock(blk, []object.Value{a.Elems[i]})) } return object.NewArrayFromSlice(out) }) @@ -3128,8 +3134,11 @@ func (vm *VM) bootstrap() { return enumFor(self, "select") } a := self.(*object.Array) + // Live-index loop: elements the block appends are also tested (Ruby's + // Array#select tolerates size increase during iteration). out := make([]object.Value, 0, len(a.Elems)) - for _, e := range a.Elems { + for i := 0; i < len(a.Elems); i++ { + e := a.Elems[i] if vm.callBlock(blk, []object.Value{e}).Truthy() { out = append(out, e) } @@ -3293,7 +3302,10 @@ func (vm *VM) bootstrap() { if len(args) > 0 { acc = args[0] } - for _, e := range self.(*object.Array).Elems { + 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}) } @@ -3303,7 +3315,10 @@ func (vm *VM) bootstrap() { }) vm.cArray.define("to_h", func(vm *VM, self object.Value, _ []object.Value, blk *Proc) object.Value { h := object.NewHash() - for i, e := range self.(*object.Array).Elems { + a := self.(*object.Array) + // Live-index loop so a block that grows the array processes the new tail. + for i := 0; i < len(a.Elems); i++ { + e := a.Elems[i] if blk != nil { // to_h { |x| [k, v] } maps each element to a pair e = vm.callBlock(blk, []object.Value{e}) } @@ -3606,7 +3621,10 @@ func (vm *VM) bootstrap() { return enumFor(self, "take_while") } var out []object.Value - for _, e := range self.(*object.Array).Elems { + a := self.(*object.Array) + // Live-index loop so elements the block appends are considered too. + for i := 0; i < len(a.Elems); i++ { + e := a.Elems[i] if !vm.callBlock(blk, []object.Value{e}).Truthy() { break } @@ -3840,19 +3858,24 @@ func (vm *VM) bootstrap() { return enumFor(self, "sort_by") } a := self.(*object.Array) - keys := make([]object.Value, len(a.Elems)) - for i, e := range a.Elems { - keys[i] = vm.callBlock(blk, []object.Value{e}) + // Live-index loop: a block that grows the array has the new tail keyed and + // sorted too. Snapshot each element beside its key so a later append cannot + // desync the two parallel slices. + var elems, keys []object.Value + for i := 0; i < len(a.Elems); i++ { + e := a.Elems[i] + elems = append(elems, e) + keys = append(keys, vm.callBlock(blk, []object.Value{e})) } // Sort an index permutation so each element stays paired with its key. - idx := make([]int, len(a.Elems)) + idx := make([]int, len(elems)) for i := range idx { idx[i] = i } sort.SliceStable(idx, func(i, j int) bool { return vm.spaceship(keys[idx[i]], keys[idx[j]]) < 0 }) out := make([]object.Value, len(idx)) for i, k := range idx { - out[i] = a.Elems[k] + out[i] = elems[k] } return object.NewArrayFromSlice(out) }) From fc3b6a2e13a006bcf24632f22ab7d4c0f5e2fd98 Mon Sep 17 00:00:00 2001 From: tannevaled Date: Fri, 4 Sep 2026 12:14:04 +0200 Subject: [PATCH 02/11] Array: in-place panic-safe compaction for keep_if/select!/reject!/delete_if; live map!/each_index MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit arrayKeepIf now compacts in place with a two-pointer walk under a deferred recover, so a block that raises partway leaves the array as MRI leaves it — the decided prefix, then the raising element and the untouched tail. delete_if routes through it for the same semantics. map! and each_index use a live-index loop so a growing array has its new tail visited. Co-Authored-By: Claude Opus 4.8 --- internal/vm/array_edges.go | 4 +++- internal/vm/builtins.go | 47 +++++++++++++++++++++++++++----------- 2 files changed, 37 insertions(+), 14 deletions(-) diff --git a/internal/vm/array_edges.go b/internal/vm/array_edges.go index c4457de0..8c51d7a2 100644 --- a/internal/vm/array_edges.go +++ b/internal/vm/array_edges.go @@ -106,7 +106,9 @@ func (vm *VM) registerArrayEdges() { n := len(a.Elems) return enumForSized(self, "each_index", func(*VM) object.Value { return object.IntValue(int64(n)) }) } - for i := range a.Elems { + // Live-index loop so indices for elements the block appends are yielded too + // (Ruby's Array#each_index tolerates size increase during iteration). + for i := 0; i < len(a.Elems); i++ { vm.callBlock(blk, []object.Value{object.IntValue(int64(i))}) } return self diff --git a/internal/vm/builtins.go b/internal/vm/builtins.go index 8f513d60..981a232a 100644 --- a/internal/vm/builtins.go +++ b/internal/vm/builtins.go @@ -2814,13 +2814,9 @@ func (vm *VM) bootstrap() { } a := self.(*object.Array) vm.checkArrayFrozen(a) - var out []object.Value - for _, e := range a.Elems { - if !vm.callBlock(blk, []object.Value{e}).Truthy() { - out = append(out, e) - } - } - a.Elems = out + // keep=false: drop elements the block finds truthy. Shares the in-place, + // panic-safe, live-growth compaction with keep_if/select!/reject!. + arrayKeepIf(vm, a, blk, false) return a }) vm.cArray.define("concat", func(_ *VM, self object.Value, args []object.Value, _ *Proc) object.Value { @@ -3203,7 +3199,9 @@ func (vm *VM) bootstrap() { } a := self.(*object.Array) vm.checkArrayFrozen(a) - for i := range a.Elems { + // Live-index loop so elements the block appends are also mapped in place + // (Ruby's Array#map! tolerates size increase during iteration). + for i := 0; i < len(a.Elems); i++ { a.Elems[i] = vm.callBlock(blk, []object.Value{a.Elems[i]}) } return self @@ -9024,17 +9022,40 @@ func arrayReduce(vm *VM, a *object.Array, args []object.Value, blk *Proc) object return acc } +// arrayKeepIf compacts a in place: it keeps every element whose block result's +// truthiness equals keep (keep=true for keep_if/select!, false for the removing +// reject!/delete_if). Compaction is in place with a two-pointer walk so that if +// the block raises partway, the array is left as MRI leaves it — the decided +// prefix followed by the raising element and the untouched tail. The live-index +// loop also lets a block that grows the array have the new tail considered. +// Returns nil when nothing was removed (Array#select!/#reject! signal "no +// change" that way), else a. func arrayKeepIf(vm *VM, a *object.Array, blk *Proc, keep bool) object.Value { - var out []object.Value - for _, e := range a.Elems { + w := 0 + i := 0 + defer func() { + if r := recover(); r != nil { + // The block raised at index i. a.Elems[:w] holds the kept prefix; the + // raising element a.Elems[i] and everything after it are retained. + if i < len(a.Elems) { + a.Elems = append(a.Elems[:w], a.Elems[i:]...) + } else { + a.Elems = a.Elems[:w] + } + panic(r) + } + }() + for i = 0; i < len(a.Elems); i++ { + e := a.Elems[i] if vm.callBlock(blk, []object.Value{e}).Truthy() == keep { - out = append(out, e) + a.Elems[w] = e + w++ } } - if len(out) == len(a.Elems) { + if w == len(a.Elems) { return object.NilV } - a.Elems = out + a.Elems = a.Elems[:w] return a } From 85bad45eee27ef09df177fb54ecdb389989f921b Mon Sep 17 00:00:00 2001 From: tannevaled Date: Fri, 4 Sep 2026 12:22:13 +0200 Subject: [PATCH 03/11] Array: element-dispatch #inspect/#to_s and #to_ary coercion for set ops MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Array#inspect now renders each element through its own Ruby-level #inspect (honouring user-defined and mocked #inspect), following MRI's rb_inspect → rb_obj_as_string: a non-String #inspect result passes through #to_s, then falls back to the # identity; a self-referential array renders as [...]. #to_s aliases it. Array#|, #&, #-, #union, #intersection, #difference and #intersect? now coerce each argument with #to_ary (Array subclasses used directly, never converted), matching MRI's rb_to_array_type, instead of rejecting every non-Array. Co-Authored-By: Claude Opus 4.8 --- internal/vm/array_edges.go | 18 ++------ internal/vm/builtins.go | 95 +++++++++++++++++++++++++++++++++++++- 2 files changed, 97 insertions(+), 16 deletions(-) diff --git a/internal/vm/array_edges.go b/internal/vm/array_edges.go index 8c51d7a2..8c19e9fd 100644 --- a/internal/vm/array_edges.go +++ b/internal/vm/array_edges.go @@ -43,7 +43,7 @@ func (vm *VM) registerArrayEdges() { // any argument array — duplicates in the receiver are preserved, exactly like a // chained `-`. vm.cArray.define("difference", func(vm *VM, self object.Value, args []object.Value, _ *Proc) object.Value { - others := arrArgs(args) + others := vm.toAryArgs(args) var out []object.Value for _, e := range self.(*object.Array).Elems { if !vm.inAnyArray(others, e) { @@ -55,7 +55,7 @@ func (vm *VM) registerArrayEdges() { // union concatenates the receiver and every argument, then removes eql? // duplicates keeping first-seen order — a multi-argument, de-duplicating `|`. vm.cArray.define("union", func(vm *VM, self object.Value, args []object.Value, _ *Proc) object.Value { - others := arrArgs(args) + others := vm.toAryArgs(args) var out []object.Value add := func(elems []object.Value) { for _, e := range elems { @@ -74,7 +74,7 @@ func (vm *VM) registerArrayEdges() { // of every argument array, de-duplicated in receiver order. With no arguments // it is simply the receiver de-duplicated (like #uniq under eql?). vm.cArray.define("intersection", func(vm *VM, self object.Value, args []object.Value, _ *Proc) object.Value { - others := arrArgs(args) + others := vm.toAryArgs(args) var out []object.Value for _, e := range self.(*object.Array).Elems { if vm.arrayIncludesEql(out, e) { @@ -89,7 +89,7 @@ func (vm *VM) registerArrayEdges() { // intersect? reports whether the receiver and the one argument array share any // element (compared with eql?), short-circuiting on the first match. vm.cArray.define("intersect?", func(vm *VM, self object.Value, args []object.Value, _ *Proc) object.Value { - other := arrArg(args[0]) + other := vm.toAryArg(args[0]) for _, e := range self.(*object.Array).Elems { if vm.arrayIncludesEql(other.Elems, e) { return object.True @@ -205,16 +205,6 @@ func arrayCycleSize(vm *VM, length int, args []object.Value, hasN bool) object.V return object.IntValue(int64(length) * count) } -// arrArgs coerces every argument to an Array (raising TypeError otherwise), -// backing the multi-argument set operations. -func arrArgs(args []object.Value) []*object.Array { - out := make([]*object.Array, len(args)) - for i, a := range args { - out[i] = arrArg(a) - } - return out -} - // inAnyArray reports whether v is eql? to an element of any of the arrays. func (vm *VM) inAnyArray(arrs []*object.Array, v object.Value) bool { for _, a := range arrs { diff --git a/internal/vm/builtins.go b/internal/vm/builtins.go index 981a232a..18c43dd5 100644 --- a/internal/vm/builtins.go +++ b/internal/vm/builtins.go @@ -2686,6 +2686,16 @@ func (vm *VM) bootstrap() { return object.NewArrayFromSlice(out) }) vm.cArray.define("fill", arrayFill) + // Array#inspect renders each element through its own Ruby-level #inspect (so a + // user-defined or mocked #inspect is honoured), following MRI's rb_inspect → + // rb_obj_as_string: a non-String #inspect result is passed through #to_s, and + // if that is still not a String the object's default # identity is + // used (never #to_str; a #to_s exception propagates). A self-referential array + // renders as "[...]". #to_s is the classic alias. + vm.cArray.define("inspect", func(vm *VM, self object.Value, _ []object.Value, _ *Proc) object.Value { + return vm.arrayInspect(self.(*object.Array), map[*object.Array]bool{}) + }) + vm.aliasMethod(vm.cArray, "to_s", "inspect") vm.cArray.define("push", func(_ *VM, self object.Value, args []object.Value, _ *Proc) object.Value { a := self.(*object.Array) vm.checkArrayFrozen(a) @@ -3173,7 +3183,7 @@ func (vm *VM) bootstrap() { // order, matching Ruby. vm.cArray.define("&", func(vm *VM, self object.Value, args []object.Value, _ *Proc) object.Value { a := self.(*object.Array) - b := arrArg(args[0]) + b := vm.toAryArg(args[0]) var out []object.Value for _, e := range a.Elems { if vm.arrayIncludesEql(b.Elems, e) && !vm.arrayIncludesEql(out, e) { @@ -3184,7 +3194,7 @@ func (vm *VM) bootstrap() { }) vm.cArray.define("|", func(vm *VM, self object.Value, args []object.Value, _ *Proc) object.Value { a := self.(*object.Array) - b := arrArg(args[0]) + b := vm.toAryArg(args[0]) var out []object.Value for _, e := range append(append([]object.Value{}, a.Elems...), b.Elems...) { if !vm.arrayIncludesEql(out, e) { @@ -7139,6 +7149,34 @@ func arrArg(v object.Value) *object.Array { return nil } +// toAryArg coerces a set-operation argument (Array#|, #&, #-, #union, +// #intersection, #difference, #intersect?) to an *object.Array. An Array — or an +// Array subclass — is used directly, without calling #to_ary (MRI does not +// convert subclasses); any other value is converted through #to_ary, whose result +// must be an Array. A value answering neither raises the same TypeError MRI's +// rb_to_array_type raises, naming the class. +func (vm *VM) toAryArg(v object.Value) *object.Array { + if a, ok := asArray(v); ok { + return a + } + if vm.respondsToDynamic(v, "to_ary") { + if a, ok := asArray(vm.send(v, "to_ary", nil, nil)); ok { + return a + } + } + raise("TypeError", "no implicit conversion of %s into Array", vm.classOf(v).name) + return nil +} + +// toAryArgs coerces each set-operation argument through toAryArg. +func (vm *VM) toAryArgs(args []object.Value) []*object.Array { + out := make([]*object.Array, len(args)) + for i, a := range args { + out[i] = vm.toAryArg(a) + } + return out +} + func strArg(v object.Value) string { if s, ok := v.(*object.String); ok { return s.Str() @@ -7618,6 +7656,59 @@ func (vm *VM) arrayJoin(a *object.Array, sep *object.String, path map[*object.Ar return object.NewStringBytesEnc(buf, resEnc) } +// arrayInspect renders an array as "[e0, e1, …]" for Array#inspect. The path set +// holds the arrays currently being inspected so a cycle at any depth renders as +// "[...]" rather than looping. The result string's encoding starts US-ASCII (so +// an empty array inspects as US-ASCII, matching MRI) and widens to UTF-8 once a +// non-ASCII byte appears. +func (vm *VM) arrayInspect(a *object.Array, path map[*object.Array]bool) *object.String { + if path[a] { + return object.NewStringBytesEnc([]byte("[...]"), "US-ASCII") + } + path[a] = true + defer delete(path, a) + buf := []byte{'['} + for i := range a.Elems { + if i > 0 { + buf = append(buf, ',', ' ') + } + buf = append(buf, vm.inspectElement(a.Elems[i], path)...) + } + buf = append(buf, ']') + enc := "US-ASCII" + for _, b := range buf { + if b >= 0x80 { + enc = "UTF-8" + break + } + } + return object.NewStringBytesEnc(buf, enc) +} + +// inspectElement returns the inspected bytes of one array element. A nested array +// recurses with the shared path so mutual/self recursion is detected; any other +// element is rendered by objAsString(element.inspect). +func (vm *VM) inspectElement(e object.Value, path map[*object.Array]bool) []byte { + if arr, ok := e.(*object.Array); ok { + return vm.arrayInspect(arr, path).Bytes() + } + return []byte(vm.objAsString(vm.send(e, "inspect", nil, nil))) +} + +// objAsString mirrors MRI's rb_obj_as_string: a String is returned as-is; any +// other value is passed through #to_s (whose exception propagates), and if that +// is still not a String the value's default # identity is used. #to_str +// is never consulted. +func (vm *VM) objAsString(v object.Value) string { + if s, ok := v.(*object.String); ok { + return s.Str() + } + if s, ok := vm.send(v, "to_s", nil, nil).(*object.String); ok { + return s.Str() + } + return vm.objectIdentityRepr(v) +} + // joinElement converts one array element to its String piece for Array#join: a // String is used as-is; an Array (or Array subclass) is joined recursively; // otherwise MRI's conversion order applies — #to_str, then #to_ary (whose result From 98d4cc2d67dde0ad82b3748d9ff0242b85bab4c1 Mon Sep 17 00:00:00 2001 From: tannevaled Date: Fri, 4 Sep 2026 12:25:10 +0200 Subject: [PATCH 04/11] Array: #initialize coerces via to_ary/to_int, arity 0..2, break-safe, private Array.new / Array#initialize now match MRI's rb_ary_initialize: a single non-Integer argument is copied as another array through #to_ary (Array subclasses directly); a size argument is coerced with #to_int; 3+ arguments raise ArgumentError; the array is built incrementally so a block break keeps the elements produced so far; and #initialize is marked private. Co-Authored-By: Claude Opus 4.8 --- internal/vm/builtins.go | 41 +++++++++++++++++++++++++++++------------ 1 file changed, 29 insertions(+), 12 deletions(-) diff --git a/internal/vm/builtins.go b/internal/vm/builtins.go index 18c43dd5..c897857e 100644 --- a/internal/vm/builtins.go +++ b/internal/vm/builtins.go @@ -2473,35 +2473,52 @@ func (vm *VM) bootstrap() { // Array.new / Array.new(other) / Array.new(n[, val]) / Array.new(n) { |i| } arr := self.(*object.Array) vm.checkArrayFrozen(arr) // re-initialising a frozen array raises (a fresh one is not frozen) - if len(args) == 1 { - if a, ok := args[0].(*object.Array); ok { - arr.Elems = append([]object.Value{}, a.Elems...) - return self - } + if len(args) > 2 { + raise("ArgumentError", "wrong number of arguments (given %d, expected 0..2)", len(args)) } if len(args) == 0 { arr.Elems = nil return self } - n := intArg(args[0]) + // A single non-Integer argument is treated as another array to copy: an Array + // (or subclass) directly, else through #to_ary (MRI's rb_check_array_type). + // Only when that yields nothing does the argument become a size via #to_int. + if len(args) == 1 && !isIntegerVal(args[0]) { + if src, ok := asArray(args[0]); ok { + arr.Elems = append([]object.Value{}, src.Elems...) + return self + } + if vm.respondsToDynamic(args[0], "to_ary") { + if src, ok := asArray(vm.send(args[0], "to_ary", nil, nil)); ok { + arr.Elems = append([]object.Value{}, src.Elems...) + return self + } + } + } + if _, isBig := args[0].(*object.Bignum); isBig { + raise("ArgumentError", "array size too big") + } + n := vm.toIntCoerce(args[0]) if n < 0 { raise("ArgumentError", "negative array size") } - out := make([]object.Value, n) - for i := range out { + // Build the array incrementally so that if the block calls break, the array + // is left holding the elements produced before the break, as MRI does. + arr.Elems = make([]object.Value, 0, n) + for i := int64(0); i < n; i++ { switch { case blk != nil: - out[i] = vm.callBlock(blk, []object.Value{object.IntValue(int64(i))}) + arr.Elems = append(arr.Elems, vm.callBlock(blk, []object.Value{object.IntValue(i)})) case len(args) >= 2: - out[i] = args[1] + arr.Elems = append(arr.Elems, args[1]) default: - out[i] = object.NilV + arr.Elems = append(arr.Elems, object.NilV) } } - arr.Elems = out return self } vm.cArray.define("initialize", arrayInit) + vm.setInstanceVisibility(vm.cArray, "initialize", visPrivate) // MRI keeps #initialize private vm.cArray.smethods["new"] = &Method{name: "new", owner: vm.cArray, native: func(vm *VM, self object.Value, args []object.Value, blk *Proc) object.Value { if recv := self.(*RClass); recv != vm.cArray { From 36df4b55b3b8ed5ed9af6ed309a9d9d71e4ad3cc Mon Sep 17 00:00:00 2001 From: tannevaled Date: Fri, 4 Sep 2026 12:28:02 +0200 Subject: [PATCH 05/11] Array: #to_h coerces pairs via to_ary and rejects arguments; robust #inspect elements Array#to_h now converts a non-Array pair through #to_ary (matching MRI) and raises ArgumentError when given a positional argument. Array#inspect renders an element that does not answer #inspect (a bare BasicObject subclass, which lacks even #respond_to?) through its native representation via a static method lookup, rather than raising NoMethodError. Co-Authored-By: Claude Opus 4.8 --- internal/vm/builtins.go | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/internal/vm/builtins.go b/internal/vm/builtins.go index c897857e..43c89695 100644 --- a/internal/vm/builtins.go +++ b/internal/vm/builtins.go @@ -3338,7 +3338,10 @@ func (vm *VM) bootstrap() { } return acc }) - vm.cArray.define("to_h", func(vm *VM, self object.Value, _ []object.Value, blk *Proc) object.Value { + vm.cArray.define("to_h", 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)) + } h := object.NewHash() a := self.(*object.Array) // Live-index loop so a block that grows the array processes the new tail. @@ -3347,7 +3350,12 @@ func (vm *VM) bootstrap() { if blk != nil { // to_h { |x| [k, v] } maps each element to a pair e = vm.callBlock(blk, []object.Value{e}) } - pair, ok := e.(*object.Array) + // Each element must be a [key, value] pair: an Array directly, or a value + // convertible with #to_ary (MRI's to_h coerces non-array contents). + pair, ok := asArray(e) + if !ok && vm.respondsToDynamic(e, "to_ary") { + pair, ok = asArray(vm.send(e, "to_ary", nil, nil)) + } if !ok { raise("TypeError", "wrong element type %s at %d (expected array)", vm.classOf(e).name, i) } @@ -7709,6 +7717,12 @@ func (vm *VM) inspectElement(e object.Value, path map[*object.Array]bool) []byte if arr, ok := e.(*object.Array); ok { return vm.arrayInspect(arr, path).Bytes() } + // An element that does not answer #inspect (e.g. a bare BasicObject subclass, + // which lacks even #respond_to?) uses its native rendering rather than raising + // NoMethodError. A static lookup avoids dispatching anything on such a receiver. + if !vm.respondsTo(e, "inspect") { + return []byte(e.Inspect()) + } return []byte(vm.objAsString(vm.send(e, "inspect", nil, nil))) } From d7dd90099829fb471c1e1b47b1d414ad4fac1d7c Mon Sep 17 00:00:00 2001 From: tannevaled Date: Fri, 4 Sep 2026 12:32:44 +0200 Subject: [PATCH 06/11] Array: to_int/to_ary argument coercion, arg-count and bounds fixes - first/last/pop/shift/drop/permutation coerce a count argument through #to_int (Float and #to_int-responders), instead of accepting only Integer. - concat/replace coerce arguments through #to_ary (Array subclasses directly), and concat snapshots each argument before appending so concat(self) matches MRI. - pop/shift raise ArgumentError on more than one argument; first raises RangeError for a Bignum count. - rindex rechecks the live length each step so a block that shrinks the array can no longer index out of range. Co-Authored-By: Claude Opus 4.8 --- internal/vm/builtins.go | 61 +++++++++++++++++++++++++++-------------- 1 file changed, 40 insertions(+), 21 deletions(-) diff --git a/internal/vm/builtins.go b/internal/vm/builtins.go index 43c89695..a8efee1f 100644 --- a/internal/vm/builtins.go +++ b/internal/vm/builtins.go @@ -2676,7 +2676,7 @@ func (vm *VM) bootstrap() { } return object.NormInt(new(big.Int).Sqrt(n)) }} - vm.cArray.define("first", func(_ *VM, self object.Value, args []object.Value, _ *Proc) object.Value { + vm.cArray.define("first", func(vm *VM, self object.Value, args []object.Value, _ *Proc) object.Value { a := self.(*object.Array) if len(args) == 0 { if len(a.Elems) == 0 { @@ -2684,12 +2684,15 @@ func (vm *VM) bootstrap() { } return a.Elems[0] } - n := clampCount(intArg(args[0]), len(a.Elems)) + if _, isBig := args[0].(*object.Bignum); isBig { + raise("RangeError", "bignum too big to convert into 'long'") + } + n := clampCount(vm.toIntCoerce(args[0]), len(a.Elems)) out := make([]object.Value, n) copy(out, a.Elems[:n]) return object.NewArrayFromSlice(out) }) - vm.cArray.define("last", func(_ *VM, self object.Value, args []object.Value, _ *Proc) object.Value { + vm.cArray.define("last", func(vm *VM, self object.Value, args []object.Value, _ *Proc) object.Value { a := self.(*object.Array) if len(args) == 0 { if len(a.Elems) == 0 { @@ -2697,7 +2700,7 @@ func (vm *VM) bootstrap() { } return a.Elems[len(a.Elems)-1] } - n := clampCount(intArg(args[0]), len(a.Elems)) + n := clampCount(vm.toIntCoerce(args[0]), len(a.Elems)) out := make([]object.Value, n) copy(out, a.Elems[len(a.Elems)-n:]) return object.NewArrayFromSlice(out) @@ -2725,11 +2728,14 @@ func (vm *VM) bootstrap() { a.Elems = append(a.Elems, args[0]) return a }) - vm.cArray.define("pop", func(_ *VM, self object.Value, args []object.Value, _ *Proc) object.Value { + vm.cArray.define("pop", func(vm *VM, self object.Value, args []object.Value, _ *Proc) object.Value { a := self.(*object.Array) vm.checkArrayFrozen(a) + if len(args) > 1 { + raise("ArgumentError", "wrong number of arguments (given %d, expected 0..1)", len(args)) + } if len(args) > 0 { // pop(n) removes and returns the last n as an array - n := int(intArg(args[0])) + n := int(vm.toIntCoerce(args[0])) if n < 0 { raise("ArgumentError", "negative array size") } @@ -2748,11 +2754,14 @@ func (vm *VM) bootstrap() { a.Elems = a.Elems[:len(a.Elems)-1] return v }) - vm.cArray.define("shift", func(_ *VM, self object.Value, args []object.Value, _ *Proc) object.Value { + vm.cArray.define("shift", func(vm *VM, self object.Value, args []object.Value, _ *Proc) object.Value { a := self.(*object.Array) vm.checkArrayFrozen(a) + if len(args) > 1 { + raise("ArgumentError", "wrong number of arguments (given %d, expected 0..1)", len(args)) + } if len(args) > 0 { // shift(n) removes and returns the first n as an array - n := int(intArg(args[0])) + n := int(vm.toIntCoerce(args[0])) if n < 0 { raise("ArgumentError", "negative array size") } @@ -2849,12 +2858,18 @@ func (vm *VM) bootstrap() { vm.cArray.define("concat", func(_ *VM, self object.Value, args []object.Value, _ *Proc) object.Value { a := self.(*object.Array) vm.checkArrayFrozen(a) - for _, arg := range args { - other, ok := arg.(*object.Array) - if !ok { - raise("TypeError", "no implicit conversion of %s into Array", classNameOf(arg)) - } - a.Elems = append(a.Elems, other.Elems...) + // Coerce every argument to an Array first (an Array subclass directly, else + // through #to_ary) so a #to_ary that inspects the receiver sees it unchanged, + // and a bad argument raises before any element is appended. + others := vm.toAryArgs(args) + // Snapshot each argument's current contents before appending, so concat(self) + // or a repeated self argument uses the pre-concat elements (MRI copies first). + snaps := make([][]object.Value, len(others)) + for i, other := range others { + snaps[i] = append([]object.Value(nil), other.Elems...) + } + for _, s := range snaps { + a.Elems = append(a.Elems, s...) } return a }) @@ -2867,10 +2882,8 @@ func (vm *VM) bootstrap() { vm.cArray.define("replace", func(_ *VM, self object.Value, args []object.Value, _ *Proc) object.Value { a := self.(*object.Array) vm.checkArrayFrozen(a) - other, ok := args[0].(*object.Array) - if !ok { - raise("TypeError", "no implicit conversion of %s into Array", classNameOf(args[0])) - } + // #replace coerces its argument to an Array (subclass directly, else #to_ary). + other := vm.toAryArg(args[0]) a.Elems = append([]object.Value(nil), other.Elems...) return a }) @@ -3532,7 +3545,7 @@ func (vm *VM) bootstrap() { elems := self.(*object.Array).Elems k := len(elems) if len(args) > 0 { - k = int(intArg(args[0])) + k = int(vm.toIntCoerce(args[0])) } var perms []object.Value if k >= 0 && k <= len(elems) { @@ -3731,6 +3744,12 @@ func (vm *VM) bootstrap() { return enumForSized(self, "rindex", func(*VM) object.Value { return object.IntValue(int64(len(a.Elems))) }) } for i := len(a.Elems) - 1; i >= 0; i-- { + // A block may shrink the array; realign to the new end and re-check size + // each step so we never index past it (MRI rechecks RARRAY_LEN too). + if i >= len(a.Elems) { + i = len(a.Elems) + continue + } var match bool if len(args) > 0 { match = vm.vmValueEqual(a.Elems[i], args[0]) @@ -3762,9 +3781,9 @@ func (vm *VM) bootstrap() { copy(out, a.Elems[:n]) return object.NewArrayFromSlice(out) }) - vm.cArray.define("drop", func(_ *VM, self object.Value, args []object.Value, _ *Proc) object.Value { + vm.cArray.define("drop", func(vm *VM, self object.Value, args []object.Value, _ *Proc) object.Value { a := self.(*object.Array) - n := int(intArg(args[0])) + n := int(vm.toIntCoerce(args[0])) if n < 0 { raise("ArgumentError", "attempt to drop negative size") } From f5d37d72de56ca214edf9900f0bbdee05b2764f8 Mon Sep 17 00:00:00 2001 From: tannevaled Date: Fri, 4 Sep 2026 12:37:43 +0200 Subject: [PATCH 07/11] Array: append/to_ary/collect/filter aliases, transpose+delete coercion/frozen - #append, #prepend, #to_s, #collect, #collect!, #filter, #filter! now share the method record of #push/#unshift/#inspect/#map/#map!/#select/#select! so their UnboundMethods are identical, as MRI's aliases are; #to_ary returns self. - #transpose coerces each row through #to_ary (subclasses directly). - #delete checks frozen only when an element is actually removed, so deleting an absent element from a frozen array returns nil instead of raising. Co-Authored-By: Claude Opus 4.8 --- internal/vm/builtins.go | 48 +++++++++++++++++++++++++++++++---------- 1 file changed, 37 insertions(+), 11 deletions(-) diff --git a/internal/vm/builtins.go b/internal/vm/builtins.go index a8efee1f..7982c5df 100644 --- a/internal/vm/builtins.go +++ b/internal/vm/builtins.go @@ -2715,13 +2715,17 @@ func (vm *VM) bootstrap() { vm.cArray.define("inspect", func(vm *VM, self object.Value, _ []object.Value, _ *Proc) object.Value { return vm.arrayInspect(self.(*object.Array), map[*object.Array]bool{}) }) - vm.aliasMethod(vm.cArray, "to_s", "inspect") + // #to_s is a true alias of #inspect: share the same method entry so + // Array.instance_method(:to_s) == Array.instance_method(:inspect), as in MRI. + vm.cArray.methods["to_s"] = vm.cArray.methods["inspect"] vm.cArray.define("push", func(_ *VM, self object.Value, args []object.Value, _ *Proc) object.Value { a := self.(*object.Array) vm.checkArrayFrozen(a) a.Elems = append(a.Elems, args...) return a }) + // #append is a true alias of #push (shared entry for UnboundMethod identity). + vm.cArray.methods["append"] = vm.cArray.methods["push"] vm.cArray.define("<<", func(_ *VM, self object.Value, args []object.Value, _ *Proc) object.Value { a := self.(*object.Array) vm.checkArrayFrozen(a) @@ -2786,7 +2790,12 @@ func (vm *VM) bootstrap() { return a } vm.cArray.define("unshift", unshift) - vm.cArray.define("prepend", unshift) + // #prepend is a true alias of #unshift: share the entry for identity. + vm.cArray.methods["prepend"] = vm.cArray.methods["unshift"] + // Array#to_ary returns self (the implicit Array-conversion protocol point). + vm.cArray.define("to_ary", func(_ *VM, self object.Value, _ []object.Value, _ *Proc) object.Value { + return self + }) // Array#insert(index, *objects): insert the objects before the element at // index (or, for a negative index, after the element index counts back to — // so -1 appends). Inserting past the end pads the gap with nil, as in MRI. @@ -2825,7 +2834,6 @@ func (vm *VM) bootstrap() { // Remove every element == the argument; return it, or (a block's result, // else nil) when nothing matched. a := self.(*object.Array) - vm.checkArrayFrozen(a) found := false var out []object.Value for _, e := range a.Elems { @@ -2835,8 +2843,12 @@ func (vm *VM) bootstrap() { out = append(out, e) } } - a.Elems = out if found { + // Only an actual removal is a modification: a frozen array raises here, + // but delete of an absent element on a frozen array is a no-op (returns + // nil / the block result) without raising, matching MRI. + vm.checkArrayFrozen(a) + a.Elems = out return args[0] } if blk != nil { @@ -3181,6 +3193,11 @@ func (vm *VM) bootstrap() { } return object.NewArrayFromSlice(out) }) + // #collect and #filter are true aliases of #map and #select. Defining them on + // Array with the shared record (rather than leaving Enumerable's alias to win) + // makes Array.instance_method(:collect)/(:filter) == (:map)/(:select), as MRI. + aliasBuiltin(vm.cArray, "collect", "map") + aliasBuiltin(vm.cArray, "filter", "select") // reduce/inject are native for the same reason (and #inject delegates here via // the prelude). The fold mirrors Enumerable#reduce exactly — the (init, sym), // (sym), (init) and bare-block forms, the "no block given" yield error, and the @@ -3246,8 +3263,9 @@ func (vm *VM) bootstrap() { } return self }) - // collect! is the classic alias of map! (as collect is of map). - vm.aliasMethod(vm.cArray, "collect!", "map!") + // collect! is the classic alias of map! (as collect is of map). Share the + // method record so Array.instance_method(:collect!) == (:map!), as in MRI. + aliasBuiltin(vm.cArray, "collect!", "map!") vm.cArray.define("reverse!", func(_ *VM, self object.Value, _ []object.Value, _ *Proc) object.Value { a := self.(*object.Array) vm.checkArrayFrozen(a) @@ -3271,7 +3289,8 @@ func (vm *VM) bootstrap() { return arrayKeepIf(vm, a, blk, true) } vm.cArray.define("select!", selectBang) - vm.cArray.define("filter!", selectBang) + // #filter! is a true alias of #select! (shared record for identity). + aliasBuiltin(vm.cArray, "filter!", "select!") vm.cArray.define("reject!", func(vm *VM, self object.Value, _ []object.Value, blk *Proc) object.Value { if blk == nil { return enumFor(self, "reject!") @@ -3426,12 +3445,19 @@ func (vm *VM) bootstrap() { if len(rows) == 0 { return object.NewArray() } + // Coerce each row to an Array (a subclass directly, else through #to_ary) so + // transpose accepts array-like rows without returning subclass instances. + mat := make([]*object.Array, len(rows)) var width int for i, r := range rows { - ra, ok := r.(*object.Array) + ra, ok := asArray(r) + if !ok && vm.respondsToDynamic(r, "to_ary") { + ra, ok = asArray(vm.send(r, "to_ary", nil, nil)) + } if !ok { raise("TypeError", "no implicit conversion of %s into Array", vm.classOf(r).name) } + mat[i] = ra if i == 0 { width = len(ra.Elems) } else if len(ra.Elems) != width { @@ -3440,9 +3466,9 @@ func (vm *VM) bootstrap() { } out := make([]object.Value, width) for j := 0; j < width; j++ { - col := make([]object.Value, len(rows)) - for i, r := range rows { - col[i] = r.(*object.Array).Elems[j] + col := make([]object.Value, len(mat)) + for i, ra := range mat { + col[i] = ra.Elems[j] } out[j] = object.NewArrayFromSlice(col) } From b819a6dab7437600ccc4bd24cd9cf8a050a4eb4e Mon Sep 17 00:00:00 2001 From: tannevaled Date: Fri, 4 Sep 2026 12:42:16 +0200 Subject: [PATCH 08/11] Array: #flatten coerces elements via to_ary (rb_check_array_type) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Array#flatten / #flatten! now flatten any element that converts to an Array through #to_ary — a real #to_ary, or one reached through #method_missing when respond_to_missing?(:to_ary, true) is true — following MRI's rb_check_array_type via rb_check_funcall. A #to_ary returning nil leaves the element in place; a non-nil non-Array result raises TypeError. Immediates and receivers without even #respond_to_missing? (a bare BasicObject) are left untouched rather than sent a message they cannot answer. The two flatten helpers are unified into one vm-method that also reports whether anything expanded (for flatten!). Co-Authored-By: Claude Opus 4.8 --- internal/vm/builtins.go | 98 +++++++++++++++++++++++++---------------- 1 file changed, 59 insertions(+), 39 deletions(-) diff --git a/internal/vm/builtins.go b/internal/vm/builtins.go index 7982c5df..d782c7fb 100644 --- a/internal/vm/builtins.go +++ b/internal/vm/builtins.go @@ -3333,21 +3333,21 @@ func (vm *VM) bootstrap() { } return object.NewArrayFromSlice(out) }) - vm.cArray.define("flatten", func(_ *VM, self object.Value, args []object.Value, _ *Proc) object.Value { + vm.cArray.define("flatten", func(vm *VM, self object.Value, args []object.Value, _ *Proc) object.Value { depth := -1 if len(args) > 0 && !object.IsNil(args[0]) { depth = int(vm.repeatLong(args[0])) // #to_int coercion (a nil depth is unbounded) } - return object.NewArrayFromSlice(flattenDepth(self.(*object.Array).Elems, depth)) + return object.NewArrayFromSlice(vm.flattenDepth(self.(*object.Array).Elems, depth)) }) - vm.cArray.define("flatten!", func(_ *VM, self object.Value, args []object.Value, _ *Proc) object.Value { + vm.cArray.define("flatten!", func(vm *VM, self object.Value, args []object.Value, _ *Proc) object.Value { depth := -1 if len(args) > 0 && !object.IsNil(args[0]) { depth = int(vm.repeatLong(args[0])) // #to_int coercion (a nil depth is unbounded) } a := self.(*object.Array) vm.checkArrayFrozen(a) - out, changed := flattenDepthChanged(a.Elems, depth) + out, changed := vm.flattenDepthIn(a.Elems, depth, map[*object.Array]bool{}) if !changed { return object.NilV } @@ -4756,7 +4756,7 @@ func (vm *VM) bootstrap() { // 1 (the pair wrappers are removed but Array values are left intact); a depth // >= 2 recurses that many further levels into nested Array values. A // non-Integer argument raises TypeError (via intArg). - vm.cHash.define("flatten", func(_ *VM, self object.Value, args []object.Value, _ *Proc) object.Value { + vm.cHash.define("flatten", func(vm *VM, self object.Value, args []object.Value, _ *Proc) object.Value { depth := 1 if len(args) > 0 { depth = int(intArg(args[0])) @@ -4767,7 +4767,7 @@ func (vm *VM) bootstrap() { v, _ := h.Get(k) pairs = append(pairs, hashPair(k, v)) } - return object.NewArrayFromSlice(flattenDepth(pairs, depth)) + return object.NewArrayFromSlice(vm.flattenDepth(pairs, depth)) }) // compact returns a copy without the nil-valued pairs, preserving the // receiver's default value and default proc. compact! removes them in place, @@ -7490,52 +7490,72 @@ func sign(n int) int { } } -// flattenDepth flattens nested arrays up to depth levels (-1 = fully). -func flattenDepth(elems []object.Value, depth int) []object.Value { - return flattenDepthIn(elems, depth, map[*object.Array]bool{}) -} - -// flattenDepthIn is flattenDepth remembering the arrays it is inside. A -// self-referential array has no flattening, and MRI says so rather than trying: -// without this it allocated 1.1 GB and did not finish. -func flattenDepthIn(elems []object.Value, depth int, path map[*object.Array]bool) []object.Value { - var out []object.Value - for _, e := range elems { - if sub, ok := e.(*object.Array); ok && depth != 0 { - if path[sub] { - raise("ArgumentError", "tried to flatten recursive array") - } - path[sub] = true - out = append(out, flattenDepthIn(sub.Elems, depth-1, path)...) - delete(path, sub) - } else { - out = append(out, e) - } +// checkArrayType is MRI's rb_check_array_type (via rb_check_funcall): an Array +// (or subclass) converts to itself; any other value is offered #to_ary when it +// defines one directly, or — for a #to_ary reachable only through +// #method_missing — when respond_to_missing?(:to_ary, true) is true. A #to_ary +// returning nil means "not an array" (no error); a non-nil non-Array result is a +// TypeError. Immediates never convert, and a receiver without even +// #respond_to_missing? (a bare BasicObject) is simply not converted rather than +// being sent a message it cannot answer. +func (vm *VM) checkArrayType(v object.Value) (*object.Array, bool) { + if a, ok := asArray(v); ok { + return a, true } - return out + if isJoinImmediate(v) { + return nil, false + } + switch { + case vm.respondsTo(v, "to_ary"): + // a real #to_ary method exists — call it below + case vm.respondsTo(v, "respond_to_missing?") && + vm.send(v, "respond_to_missing?", []object.Value{object.Symbol("to_ary"), object.True}, nil).Truthy(): + // #to_ary is served through #method_missing — call it below + default: + return nil, false + } + r := vm.send(v, "to_ary", nil, nil) + if object.IsNil(r) { + return nil, false + } + if a, ok := asArray(r); ok { + return a, true + } + raise("TypeError", "can't convert %s to Array (%s#to_ary gives %s)", + vm.classOf(v).name, vm.classOf(v).name, vm.classOf(r).name) + return nil, false } -// flattenDepthChanged flattens like flattenDepth but also reports whether any -// nested array was actually expanded. flatten!/flatten share semantics, but the -// bang form must return nil when nothing changed, which length alone cannot -// detect (e.g. [[1]] flattens to [1] with the same length). -func flattenDepthChanged(elems []object.Value, depth int) ([]object.Value, bool) { - return flattenDepthChangedIn(elems, depth, map[*object.Array]bool{}) +func (vm *VM) flattenDepth(elems []object.Value, depth int) []object.Value { + out, _ := vm.flattenDepthIn(elems, depth, map[*object.Array]bool{}) + return out } -// flattenDepthChangedIn carries the same cycle path flattenDepthIn does: the -// bang form must refuse a self-referential array for the same reason. -func flattenDepthChangedIn(elems []object.Value, depth int, path map[*object.Array]bool) ([]object.Value, bool) { +// flattenDepthIn flattens up to depth (unbounded when negative), remembering the +// arrays it is inside so a self-referential array raises rather than looping (MRI +// says so; without this it allocated 1.1 GB and did not finish). An element that +// is not an Array but converts through #to_ary (checkArrayType) is flattened too. +// It also reports whether anything was expanded, which flatten! needs to return +// nil when nothing changed (length alone cannot tell: [[1]] → [1] keeps length). +func (vm *VM) flattenDepthIn(elems []object.Value, depth int, path map[*object.Array]bool) ([]object.Value, bool) { var out []object.Value changed := false for _, e := range elems { - if sub, ok := e.(*object.Array); ok && depth != 0 { + var sub *object.Array + if depth != 0 { + if a, ok := e.(*object.Array); ok { + sub = a + } else if a, ok := vm.checkArrayType(e); ok { + sub = a + } + } + if sub != nil { if path[sub] { raise("ArgumentError", "tried to flatten recursive array") } changed = true path[sub] = true - inner, _ := flattenDepthChangedIn(sub.Elems, depth-1, path) + inner, _ := vm.flattenDepthIn(sub.Elems, depth-1, path) delete(path, sub) out = append(out, inner...) } else { From ac8d9ade8457487dd78d026c0514c9fbe09d1bda Mon Sep 17 00:00:00 2001 From: tannevaled Date: Fri, 4 Sep 2026 12:45:27 +0200 Subject: [PATCH 09/11] Array: #sort block comparator accepts Bignum/Comparable results; #product via checkArrayType Array#sort / #sort! with a block now turn the block's result into a sign the way MRI's rb_cmpint does: an Integer or Bignum contributes its own sign, and any other result is compared against 0 through #<=> (a non-Integer #<=> is "comparison of with 0 failed"). Previously only a small Integer result was accepted, so a Bignum or Comparable result raised. #product routes argument coercion through checkArrayType so a #to_ary reached through #method_missing is honoured. Co-Authored-By: Claude Opus 4.8 --- internal/vm/builtins.go | 42 +++++++++++++++++++++++++++++------------ 1 file changed, 30 insertions(+), 12 deletions(-) diff --git a/internal/vm/builtins.go b/internal/vm/builtins.go index d782c7fb..e0da4c44 100644 --- a/internal/vm/builtins.go +++ b/internal/vm/builtins.go @@ -3477,10 +3477,9 @@ func (vm *VM) bootstrap() { vm.cArray.define("product", func(vm *VM, self object.Value, args []object.Value, blk *Proc) object.Value { lists := [][]object.Value{self.(*object.Array).Elems} for _, a := range args { - la, ok := asArray(a) - if !ok && vm.respondsToDynamic(a, "to_ary") { - la, ok = asArray(vm.send(a, "to_ary", nil, nil)) - } + // checkArrayType coerces via #to_ary, including a #to_ary reached only + // through #method_missing (respond_to_missing?-aware), matching MRI. + la, ok := vm.checkArrayType(a) if !ok { raise("TypeError", "no implicit conversion of %s into Array", vm.classOf(a).name) } @@ -7416,17 +7415,36 @@ func (vm *VM) sortSlice(out []object.Value, blk *Proc) { return } sort.SliceStable(out, func(i, j int) bool { - r := vm.callBlock(blk, []object.Value{out[i], out[j]}) - c, ok := r.(object.Integer) - if !ok { - // MRI compares the block's result against 0, so a non-Integer fails as - // "comparison of with 0 failed". - raise("ArgumentError", "comparison of %s with 0 failed", vm.classOf(r).name) - } - return c < 0 + return vm.blockCmpToZero(vm.callBlock(blk, []object.Value{out[i], out[j]})) < 0 }) } +// blockCmpToZero turns a sort/comparator block's result into a three-way sign, +// like MRI's rb_cmpint: an Integer (or Bignum) contributes its own sign directly, +// and anything else is compared against 0 through #<=>. A #<=> that does not +// return an Integer is "comparison of with 0 failed". +func (vm *VM) blockCmpToZero(r object.Value) int { + switch v := r.(type) { + case object.Integer: + switch { + case v < 0: + return -1 + case v > 0: + return 1 + default: + return 0 + } + case *object.Bignum: + return bigVal(v).Sign() + } + res := vm.send(r, "<=>", []object.Value{object.IntValue(0)}, nil) + n, ok := res.(object.Integer) + if !ok { + raise("ArgumentError", "comparison of %s with 0 failed", vm.classOf(r).name) + } + return int(n) +} + // arrayByExtreme implements min_by/max_by: the element whose block key is // smallest (want=-1) or largest (want=1). nil for an empty array. func (vm *VM) arrayByExtreme(a *object.Array, blk *Proc, name string, want int) object.Value { From 0bfe62eb5cfeeb8e3856f8960e45280dfc6307ec Mon Sep 17 00:00:00 2001 From: tannevaled Date: Fri, 4 Sep 2026 12:56:35 +0200 Subject: [PATCH 10/11] test: cover Array residuals wave (live iteration, coercion, inspect, flatten, sort cmp) Adds internal/vm/array_residuals_wave10_test.go exercising every new branch: live-index iteration with a growing array; arrayKeepIf's in-place compaction, no-change nil, panic-safe partial retention and full-truncate; #inspect element dispatch with #to_s and identity fall-backs, recursion and BasicObject; set-op and constructor/#to_h/#concat/#replace/#transpose #to_ary and #to_int coercion; count-arg coercion and arity errors; the shared-record aliases' identity; #flatten #to_ary coercion (real, method_missing, nil, TypeError, recursion); and the sort block comparator over Integer/Bignum/Comparable/non-comparable results. Co-Authored-By: Claude Opus 4.8 --- internal/vm/array_residuals_wave10_test.go | 228 +++++++++++++++++++++ 1 file changed, 228 insertions(+) create mode 100644 internal/vm/array_residuals_wave10_test.go diff --git a/internal/vm/array_residuals_wave10_test.go b/internal/vm/array_residuals_wave10_test.go new file mode 100644 index 00000000..6133512d --- /dev/null +++ b/internal/vm/array_residuals_wave10_test.go @@ -0,0 +1,228 @@ +// Copyright (c) the go-embedded-ruby/ruby authors +// +// SPDX-License-Identifier: BSD-3-Clause + +package vm_test + +import ( + "regexp" + "testing" +) + +// TestArrayLiveIteration covers the index-based iteration of Array#each and the +// collect/filter family: an element the block appends during iteration is +// visited too (MRI's "tolerates increasing an array size during iteration"). +// Blocks avoid the != operator on purpose (an unrelated VM inline-cache defect +// mis-evaluates a block's first !=). Verified against MRI Ruby 4.0. +func TestArrayLiveIteration(t *testing.T) { + runCases(t, []struct{ src, want string }{ + // each visits appended tail. + {`a=[1,2,3]; s=[]; i=0; a.each{|e| s<1}; p r`, "[2, 3]\n"}, + // sum folds the appended tail. + {`a=[1,2]; i=0; r=a.sum{|e| a<<3 if i<1; i+=1; e}; p r`, "6\n"}, + // to_h processes the appended tail. + {`a=[[1,:a]]; i=0; h=a.to_h{|p| a<<[2,:b] if i<1; i+=1; p}; p h`, "{1 => :a, 2 => :b}\n"}, + // take_while considers the appended tail. + {`a=[1,2]; i=0; r=a.take_while{|e| a<<3 if i<1; i+=1; e<10}; p r`, "[1, 2, 3]\n"}, + // sort_by keys the appended tail. + {`a=[3,1]; i=0; r=a.sort_by{|e| a<<2 if i<1; i+=1; e}; p r`, "[1, 2, 3]\n"}, + // map! maps the appended tail in place. + {`a=[1,2]; i=0; a.map!{|e| a<<3 if i<1; i+=1; e*10}; p a`, "[10, 20, 30]\n"}, + // each_index yields the appended index. + {`a=[1,2]; s=[]; i=0; a.each_index{|k| s<0})`, "nil\n"}, // nothing removed → nil + {`p([1,2,3].reject!{|e| e>0})`, "[]\n"}, // all removed + // The block raises at 3: 1 kept, 2 dropped, then 3 and 4 retained. + {`a=[1,2,3,4]; begin; a.keep_if{|e| next false if e==2; raise "x" if e==3; true}; rescue; end; p a`, + "[1, 3, 4]\n"}, + {`a=[1,2,3,4]; begin; a.reject!{|e| next true if e==2; raise "x" if e==3; false}; rescue; end; p a`, + "[1, 3, 4]\n"}, + // The block clears the array (shrinking below i) then raises → empty. + {`b=[1,2,3]; begin; b.keep_if{|e| b.clear; raise "x"}; rescue; end; p b`, "[]\n"}, + // delete_if shares the same compaction. + {`a=[1,2,3,4]; a.delete_if{|e| e==2 || e==4}; p a`, "[1, 3]\n"}, + }) +} + +// TestArrayInspectDispatch covers Array#inspect rendering each element through +// its own Ruby #inspect, the #to_s and # identity fall-backs of MRI's +// rb_obj_as_string, the recursion sentinel, a BasicObject element (no #inspect), +// and the #to_s alias identity. Verified against MRI Ruby 4.0. +func TestArrayInspectDispatch(t *testing.T) { + runCases(t, []struct{ src, want string }{ + {`class C; def inspect; "CUSTOM"; end; end; puts [C.new].inspect`, "[CUSTOM]\n"}, + {`class D; def inspect; self; end; def to_s; "dee"; end; end; puts [D.new].inspect`, "[dee]\n"}, + {`a=[1]; a<. + got := eval(t, `class OS3; def inspect; self; end; def to_s; self; end; end; puts [OS3.new].inspect`) + if !regexp.MustCompile(`^\[#\]\n$`).MatchString(got) { + t.Errorf("identity fallback inspect = %q", got) + } + // A bare BasicObject element (no #inspect / #respond_to?) uses native rendering. + got = eval(t, `class BO < BasicObject; end; puts [BO.new].inspect`) + if !regexp.MustCompile(`^\[#e; puts e.message; end`, + "no implicit conversion of Integer into Array\n"}, + }) +} + +// TestArrayInitialize covers Array.new / Array#initialize: copying another array +// through #to_ary, a #to_int size, the 3+-argument and negative/Bignum size +// errors, break-safe incremental build, and #initialize being private. Verified +// against MRI Ruby 4.0. +func TestArrayInitialize(t *testing.T) { + runCases(t, []struct{ src, want string }{ + {`class F; def to_ary; [7,8]; end; end; p Array.new(F.new)`, "[7, 8]\n"}, + {`class G; def to_int; 3; end; end; p Array.new(G.new, 0)`, "[0, 0, 0]\n"}, + {`p Array.new(3){|i| i*2}`, "[0, 2, 4]\n"}, + {`p Array.new([1,2,3])`, "[1, 2, 3]\n"}, // Array argument copied + {`p Array.new`, "[]\n"}, + {`r=Array.new(5){|i| break "B" if i==3; i.to_s}; p r`, `"B"` + "\n"}, + {`begin; Array.new(1,2,3); rescue ArgumentError=>e; puts e.message; end`, + "wrong number of arguments (given 3, expected 0..2)\n"}, + {`begin; Array.new(-1); rescue ArgumentError=>e; puts e.message; end`, "negative array size\n"}, + {`begin; Array.new(10**40); rescue ArgumentError=>e; puts e.message; end`, "array size too big\n"}, + {`p(Array.private_instance_methods.include?(:initialize))`, "true\n"}, + }) +} + +// TestArrayToHCoercion covers Array#to_h coercing a non-Array pair through +// #to_ary and rejecting a positional argument. Verified against MRI Ruby 4.0. +func TestArrayToHCoercion(t *testing.T) { + runCases(t, []struct{ src, want string }{ + {`class H; def to_ary; [:k,:v]; end; end; p [H.new].to_h`, "{k: :v}\n"}, + {`begin; [[1,2]].to_h(:x); rescue ArgumentError=>e; puts e.message; end`, + "wrong number of arguments (given 1, expected 0)\n"}, + }) +} + +// TestArrayCountArgCoercion covers #first/#last/#pop/#shift/#drop/#permutation +// coercing a count through #to_int, the pop/shift over-argument error, and the +// first-with-Bignum RangeError. Verified against MRI Ruby 4.0. +func TestArrayCountArgCoercion(t *testing.T) { + runCases(t, []struct{ src, want string }{ + {`class N; def to_int; 2; end; end; p [1,2,3,4].first(N.new)`, "[1, 2]\n"}, + {`class N; def to_int; 2; end; end; p [1,2,3,4].last(N.new)`, "[3, 4]\n"}, + {`class N; def to_int; 2; end; end; a=[1,2,3,4]; p a.pop(N.new); p a`, "[3, 4]\n[1, 2]\n"}, + {`class N; def to_int; 2; end; end; a=[1,2,3,4]; p a.shift(N.new); p a`, "[1, 2]\n[3, 4]\n"}, + {`class N; def to_int; 2; end; end; p [1,2,3,4].drop(N.new)`, "[3, 4]\n"}, + {`p [1,2,3].permutation(2.0).to_a.length`, "6\n"}, // Float truncated via to_int + {`begin; [1,2].pop(1,2); rescue ArgumentError=>e; puts e.message; end`, + "wrong number of arguments (given 2, expected 0..1)\n"}, + {`begin; [1,2].shift(1,2); rescue ArgumentError=>e; puts e.message; end`, + "wrong number of arguments (given 2, expected 0..1)\n"}, + {`begin; [1,2,3].first(10**30); rescue RangeError; puts "RE"; end`, "RE\n"}, + }) +} + +// TestArrayConcatReplaceDelete covers #concat / #replace coercing through +// #to_ary, concat snapshotting each argument (so concat(self) doubles once), +// #transpose coercing rows, #delete only checking frozen on an actual removal, +// and #rindex re-checking the live length when a block shrinks the array. +// Verified against MRI Ruby 4.0. +func TestArrayConcatReplaceDelete(t *testing.T) { + runCases(t, []struct{ src, want string }{ + {`class TA; def to_ary; [3,4]; end; end; a=[1,2]; a.concat(TA.new); p a`, "[1, 2, 3, 4]\n"}, + {`a=[1,2]; a.concat(a,a); p a`, "[1, 2, 1, 2, 1, 2]\n"}, + {`class TA; def to_ary; [9,9]; end; end; a=[1]; a.replace(TA.new); p a`, "[9, 9]\n"}, + {`class R; def to_ary; [3,4]; end; end; p [[1,2],R.new].transpose`, "[[1, 3], [2, 4]]\n"}, + {`a=[1,2,3].freeze; p a.delete(9)`, "nil\n"}, // absent → no frozen error + {`begin; [1,2,3].freeze.delete(2); rescue FrozenError; puts "FE"; end`, "FE\n"}, + {`a=[4,5,6,7]; r=a.rindex{|e| a.clear; e==4}; p r`, "nil\n"}, // shrink mid-scan, no crash + }) +} + +// TestArrayAliases covers the shared-record aliases so their UnboundMethods are +// identical (as MRI's aliases are), plus their functionality. Verified against +// MRI Ruby 4.0. +func TestArrayAliases(t *testing.T) { + runCases(t, []struct{ src, want string }{ + {`a=[1]; a.append(2,3); p a`, "[1, 2, 3]\n"}, + {`a=[1]; a.prepend(2,3); p a`, "[2, 3, 1]\n"}, + {`p [1,2].to_ary.equal?([1,2].to_ary)`, "false\n"}, // returns self (fresh arrays differ) + {`x=[1,2]; p x.to_ary.equal?(x)`, "true\n"}, // to_ary returns self + {`p [1,2,3].collect{|e| e*2}`, "[2, 4, 6]\n"}, + {`p [1,2,3].filter{|e| e>1}`, "[2, 3]\n"}, + {`a=[1,2,3]; a.collect!{|e| e+1}; p a`, "[2, 3, 4]\n"}, + {`a=[1,2,3]; a.filter!{|e| e>1}; p a`, "[2, 3]\n"}, + {`p(Array.instance_method(:append) == Array.instance_method(:push))`, "true\n"}, + {`p(Array.instance_method(:prepend) == Array.instance_method(:unshift))`, "true\n"}, + {`p(Array.instance_method(:collect) == Array.instance_method(:map))`, "true\n"}, + {`p(Array.instance_method(:filter) == Array.instance_method(:select))`, "true\n"}, + {`p(Array.instance_method(:collect!) == Array.instance_method(:map!))`, "true\n"}, + {`p(Array.instance_method(:filter!) == Array.instance_method(:select!))`, "true\n"}, + }) +} + +// TestArrayFlattenCoercion covers Array#flatten coercing elements through +// #to_ary (a real one, one reached via #method_missing behind +// respond_to_missing?, a nil result kept, and a non-Array result raising +// TypeError), depth limiting, and the recursion guard. Verified against MRI +// Ruby 4.0. +func TestArrayFlattenCoercion(t *testing.T) { + runCases(t, []struct{ src, want string }{ + {`class I; def to_ary; [1,2]; end; end; p [I.new,3].flatten`, "[1, 2, 3]\n"}, + {`class MM + def respond_to_missing?(n,inc); n==:to_ary; end + def method_missing(n,*a); n==:to_ary ? [4,5] : super; end +end +p [MM.new,6].flatten`, "[4, 5, 6]\n"}, + {`class FN; def to_ary; nil; end; end; p [FN.new,1].flatten.size`, "2\n"}, + {`class FT; def to_ary; 5; end; end; begin; [FT.new].flatten; rescue TypeError; puts "TE"; end`, "TE\n"}, + {`p [1,[2,[3,[4]]]].flatten(1)`, "[1, 2, [3, [4]]]\n"}, + {`a=[1]; a<e; puts e.message; end`, + "tried to flatten recursive array\n"}, + }) +} + +// TestArraySortBlockCmp covers the sort block comparator turning its result into +// a sign like MRI's rb_cmpint: a small Integer, a Bignum, a Float compared via +// #<=> 0, and a non-comparable result raising. Verified against MRI Ruby 4.0. +func TestArraySortBlockCmp(t *testing.T) { + runCases(t, []struct{ src, want string }{ + {`p [3,1,2].sort{|a,b| a-b}`, "[1, 2, 3]\n"}, // small Integer sign + {`p [3,1,2].sort{|a,b| (a-b)*(10**20)}`, "[1, 2, 3]\n"}, // Bignum sign + {`p [3,1,2].sort{|a,b| (a-b).to_f}`, "[1, 2, 3]\n"}, // Float via <=> 0 + {`begin; [1,2].sort{|a,b| "x"}; rescue ArgumentError=>e; puts e.message; end`, + "comparison of String with 0 failed\n"}, + }) +} From fa5116def5e2e884cc21a12be123dfcb8dd0cac8 Mon Sep 17 00:00:00 2001 From: tannevaled Date: Fri, 4 Sep 2026 13:00:02 +0200 Subject: [PATCH 11/11] test: cover blockCmpToZero zero-result and arrayInspect UTF-8 widening branches Co-Authored-By: Claude Opus 4.8 --- internal/vm/array_residuals_wave10_test.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/internal/vm/array_residuals_wave10_test.go b/internal/vm/array_residuals_wave10_test.go index 6133512d..587a4b2f 100644 --- a/internal/vm/array_residuals_wave10_test.go +++ b/internal/vm/array_residuals_wave10_test.go @@ -69,6 +69,8 @@ func TestArrayInspectDispatch(t *testing.T) { {`class D; def inspect; self; end; def to_s; "dee"; end; end; puts [D.new].inspect`, "[dee]\n"}, {`a=[1]; a< 0 {`begin; [1,2].sort{|a,b| "x"}; rescue ArgumentError=>e; puts e.message; end`,