diff --git a/internal/vm/enumerator.go b/internal/vm/enumerator.go index 0d259241..fcc27e15 100644 --- a/internal/vm/enumerator.go +++ b/internal/vm/enumerator.go @@ -2,6 +2,7 @@ package vm import ( "math" + "math/big" "strings" "github.com/go-embedded-ruby/ruby/internal/object" @@ -37,6 +38,9 @@ type Enumerator struct { produceInit object.Value produceHas bool + // isProduct marks an Enumerator::Product (built by Enumerator.product): a plain + // generator Enumerator whose only distinction is the class it reports. + isProduct bool // isChain marks an Enumerator::Chain: #each iterates chainParts in turn, and // entered[i] records whether part i has been iterated (so #rewind can rewind, // in reverse, exactly the parts that were entered). @@ -138,6 +142,15 @@ func (vm *VM) registerEnumerator() { return newChain(append([]object.Value{}, args...)) }} + // Enumerator::Product — the subclass Enumerator.product returns. + vm.cEnumeratorProduct = newClass("Enumerator::Product", vm.cEnumerator) + vm.cEnumeratorProduct.consts = vm.cEnumerator.consts + vm.cEnumerator.consts["Product"] = vm.cEnumeratorProduct + vm.cEnumerator.smethods["product"] = &Method{name: "product", owner: vm.cEnumerator, + native: func(vm *VM, _ object.Value, args []object.Value, blk *Proc) object.Value { + return vm.enumProduct(args, blk) + }} + // Enumerator::Yielder — `y << v` / `y.yield(v)` feed the generator's values in. vm.cYielder = newClass("Enumerator::Yielder", vm.cObject) vm.cYielder.consts = vm.cEnumerator.consts // (scope is cosmetic; share the map) @@ -362,6 +375,89 @@ func (vm *VM) enumProduce(args []object.Value, blk *Proc) object.Value { return e } +// enumProduct implements Enumerator.product. It returns an Enumerator::Product +// over the Cartesian product of its enumerable arguments, each consumed through +// #each_entry lazily and afresh at every outer step — so an infinite source +// yields an infinite product and a one-shot source is drained once. Keyword +// arguments are rejected. With a block it iterates the product and returns nil. +func (vm *VM) enumProduct(args []object.Value, blk *Proc) object.Value { + if h := trailingKwHash(args); h != nil { + bad := make([]string, len(h.Keys)) + for i, k := range h.Keys { + bad[i] = k.Inspect() + } + raise("ArgumentError", "unknown keywords: %s", strings.Join(bad, ", ")) + } + sources := append([]object.Value{}, args...) + + // #size is the product of the sources' sizes: 1 for no sources, Float::INFINITY + // when any is infinite, and nil when any is unknown. + var size object.Value = object.IntValue(1) + for _, e := range sources { + if !vm.respondsToDynamic(e, "size") { + size = object.NilV + break + } + s := vm.send(e, "size", nil, nil) + if object.IsNil(s) { + size = object.NilV + break + } + size = productSize(size, s) + } + + gen := &Proc{native: func(vm *VM, gargs []object.Value) object.Value { + y := gargs[0] + var combine func(rest, prefix []object.Value) + combine = func(rest, prefix []object.Value) { + if len(rest) == 0 { + vm.send(y, "<<", []object.Value{object.NewArrayFromSlice(append([]object.Value{}, prefix...))}, nil) + return + } + first, tail := rest[0], rest[1:] + step := &Proc{native: func(vm *VM, a []object.Value) object.Value { + // each_entry yields one entry per element; a multi-value yield keeps + // only its first value, a bare yield contributes nil — as MRI does. + var x object.Value = object.NilV + if len(a) > 0 { + x = a[0] + } + combine(tail, append(append([]object.Value{}, prefix...), x)) + return object.NilV + }} + vm.send(first, "each_entry", nil, step) + } + combine(sources, nil) + return object.NilV + }} + + e := &Enumerator{isProduct: true, block: gen, sizeSpec: size, sizeSpecSet: true} + if blk == nil { + return e + } + vm.send(e, "each", nil, blk) + return object.NilV +} + +// productSize multiplies two finite-or-infinite #size values under +// Enumerator.product's rules: an infinite factor makes the product infinite; +// otherwise the two Integer sizes multiply. The caller stops at the first nil +// (unknown) size, so neither argument here is nil. +func productSize(a, b object.Value) object.Value { + if isInfFloat(a) || isInfFloat(b) { + return object.Float(math.Inf(1)) + } + an, _ := object.BigOf(a) + bn, _ := object.BigOf(b) + return object.NormInt(new(big.Int).Mul(an, bn)) +} + +// isInfFloat reports whether v is a positive-infinite Float. +func isInfFloat(v object.Value) bool { + f, ok := v.(object.Float) + return ok && math.IsInf(float64(f), 1) +} + // enumResolveSize resolves a stored #size specification: a callable (Proc or any // object responding to #call) is invoked, anything else (an Integer or nil) is // returned verbatim. diff --git a/internal/vm/enumerator_product_internal_test.go b/internal/vm/enumerator_product_internal_test.go new file mode 100644 index 00000000..38fbaa05 --- /dev/null +++ b/internal/vm/enumerator_product_internal_test.go @@ -0,0 +1,58 @@ +// Copyright (c) the go-embedded-ruby/ruby authors +// +// SPDX-License-Identifier: BSD-3-Clause + +package vm + +import "testing" + +// TestEnumeratorProduct covers Enumerator.product: the Cartesian product of its +// enumerable arguments (any arity, including none), an Enumerator::Product +// instance, laziness over an infinite source, a #size that is the product of the +// sources' sizes (Float::INFINITY when any is infinite), the block form returning +// nil, keyword-argument rejection, use of #each_entry (drained once), and the +// NoMethodError when an argument lacks #each_entry. Verified against ruby 4.0.6. +func TestEnumeratorProduct(t *testing.T) { + const inf = `e = Enumerator.new { |y| i = 0; loop { y << (i += 1) } }; ` + const oneShot = `a = [1, 2, 3]; i = 0; en = Enumerator.new { |y| while i < a.size; y << a[i]; i += 1; end }; ` + const obj = `o = Object.new; def o.each_entry; yield 1; yield 2; end; ` + cases := []struct{ src, want string }{ + // Cartesian product of any arity. + {`p Enumerator.product(1..2, ["A", "B"]).to_a`, `[[1, "A"], [1, "B"], [2, "A"], [2, "B"]]`}, + {`p Enumerator.product(1..2).to_a`, `[[1], [2]]`}, + {`p Enumerator.product(2..3, ["A"], ["B"], ["C"]).to_a`, `[[2, "A", "B", "C"], [3, "A", "B", "C"]]`}, + // No arguments yields one empty array. + {`p Enumerator.product.to_a`, `[[]]`}, + // The result is an Enumerator::Product. + {`p Enumerator.product.class`, `Enumerator::Product`}, + {`p Enumerator.product(1..2).is_a?(Enumerator)`, `true`}, + // Laziness: an infinite source yields an infinite product. + {inf + `p Enumerator.product(e, ["A", "B"]).take(5)`, `[[1, "A"], [1, "B"], [2, "A"], [2, "B"], [3, "A"]]`}, + // #size is the product of the sizes; infinite when a source is infinite. + {`p Enumerator.product(1..2, ["A", "B"]).size`, `4`}, + {`p Enumerator.product.size`, `1`}, + {inf + `p Enumerator.product(e, ["A", "B"]).size`, `nil`}, + // #size on an infinite source is Float::INFINITY (computed, not iterated). + {`p Enumerator.product(1.., ["A", "B"]).size`, `Infinity`}, + // A block iterates the product and returns nil. + {`elems = []; Enumerator.product(1..2, ["X", "Y"]) { |c| elems << c }; p elems`, `[[1, "X"], [1, "Y"], [2, "X"], [2, "Y"]]`}, + {`r = Enumerator.product(1..2) {}; p r`, `nil`}, + // Keyword arguments are rejected. + {`begin; Enumerator.product(1..3, foo: 1, bar: 2); rescue ArgumentError => ex; p ex.message; end`, `"unknown keywords: :foo, :bar"`}, + // Sources are consumed through #each_entry, each drained only once. + {obj + `p Enumerator.product(o, ["A"]).to_a`, `[[1, "A"], [2, "A"]]`}, + {oneShot + `p Enumerator.product(["a", "b"], en).to_a`, `[["a", 1], ["a", 2], ["a", 3]]`}, + // A multi-value each_entry yield keeps its first value; a bare yield gives nil. + {`o = Object.new; def o.each_entry; yield 1, 2; yield 3; end; p Enumerator.product(o, ["A"]).to_a`, `[[1, "A"], [3, "A"]]`}, + {`o = Object.new; def o.each_entry; yield; yield 5; end; p Enumerator.product(o, ["A"]).to_a`, `[[nil, "A"], [5, "A"]]`}, + // An argument without #each_entry raises NoMethodError. + {`begin; Enumerator.product(Object.new).to_a; rescue NoMethodError => ex; p ex.class; end`, `NoMethodError`}, + // #each_entry is called lazily — building the enumerator does not raise. + {`p Enumerator.product(Object.new).is_a?(Enumerator)`, `true`}, + } + for _, c := range cases { + if got := eval(t, c.src); got != c.want+"\n" { + t.Errorf("src=%q got=%q want=%q", c.src, got, c.want+"\n") + } + } +} diff --git a/internal/vm/object_model.go b/internal/vm/object_model.go index d686d2b3..4500efcc 100644 --- a/internal/vm/object_model.go +++ b/internal/vm/object_model.go @@ -1425,6 +1425,9 @@ func (vm *VM) classOf(v object.Value) *RClass { if x.isChain { return vm.cEnumeratorChain } + if x.isProduct { + return vm.cEnumeratorProduct + } return vm.cEnumerator case *yielder: return vm.cYielder diff --git a/internal/vm/vm.go b/internal/vm/vm.go index 17476311..476fdb8f 100644 --- a/internal/vm/vm.go +++ b/internal/vm/vm.go @@ -384,6 +384,7 @@ type VM struct { cMethod *RClass cEnumerator *RClass cEnumeratorChain *RClass + cEnumeratorProduct *RClass cYielder *RClass cWeakMap *RClass cWeakKeyMap *RClass