diff --git a/internal/vm/actionview_render_open.go b/internal/vm/actionview_render_open.go index b1c7927b..3dc9a39a 100644 --- a/internal/vm/actionview_render_open.go +++ b/internal/vm/actionview_render_open.go @@ -61,7 +61,7 @@ func (vm *VM) actionViewInlineRender(b *ActionViewBase, tmpl string, locals map[ slots := make([]object.Value, len(vals)) copy(slots, vals) parent := &Env{slots: slots} - res := vm.exec(iseq, b, nil, b.cls, "", parent, nil, nil, nil) + res := vm.exec(iseq, b, nil, b.cls, "", parent, nil, nil, nil, nil) return avToS(res) } diff --git a/internal/vm/aot_level2.go b/internal/vm/aot_level2.go index 8614796a..9492aec3 100644 --- a/internal/vm/aot_level2.go +++ b/internal/vm/aot_level2.go @@ -33,7 +33,7 @@ func (vm *VM) runTop(iseq *bytecode.ISeq) object.Value { vm.mainArmed = false // fire once, for the user program only (not nested require/eval) return compiledMainFn(vm) } - return vm.exec(iseq, vm.main, nil, vm.cObject, "", nil, nil, nil, nil) + return vm.exec(iseq, vm.main, nil, vm.cObject, "", nil, nil, nil, nil, nil) } // aotBlockArgs shapes a native block's raw yield arguments to its np fixed diff --git a/internal/vm/binding_eval_open.go b/internal/vm/binding_eval_open.go index 8cd4dec7..8c6b58c9 100644 --- a/internal/vm/binding_eval_open.go +++ b/internal/vm/binding_eval_open.go @@ -48,7 +48,7 @@ func (vm *VM) bindingEval(b *Binding, srcV object.Value) object.Value { // through the caller's binding, reports the enclosing method), matching the // no-binding eval path. vm.pendingMethodCtx = vm.currentMethodCtxPtr() - return vm.exec(iseq, b.self, nil, b.definee, "", b.env, nil, nil, nil) + return vm.exec(iseq, b.self, nil, b.definee, "", b.env, nil, nil, nil, nil) } // bindingNewLocals returns the named top-scope locals a compiled eval body diff --git a/internal/vm/builtins.go b/internal/vm/builtins.go index 780d34a9..fbb5823b 100644 --- a/internal/vm/builtins.go +++ b/internal/vm/builtins.go @@ -122,8 +122,10 @@ func (vm *VM) bootstrap() { vm.registerBenchmark() // Benchmark module (require "benchmark"), backed by go-ruby-benchmark vm.registerScanf() // String#scanf / IO#scanf / Kernel#scanf (require "scanf"), backed by go-ruby-scanf - procCall := func(vm *VM, self object.Value, args []object.Value, _ *Proc) object.Value { - return vm.callBlock(self.(*Proc), args) + procCall := func(vm *VM, self object.Value, args []object.Value, blk *Proc) object.Value { + // The block passed to `.call` (blk) binds the proc's own `&b` parameter; + // a bare `yield` inside the proc still reaches its captured block. + return vm.callProcWithBlock(self.(*Proc), args, blk) } vm.cProc.smethods["new"] = &Method{name: "new", owner: vm.cProc, native: func(_ *VM, _ object.Value, _ []object.Value, blk *Proc) object.Value { @@ -8855,6 +8857,14 @@ func (vm *VM) numericStep(blk *Proc, loV, hiV, stepV object.Value, exclusive boo } return } + if math.IsInf(lo, 0) { + // A non-finite start makes every term lo+i*step the same non-finite value, + // so the progress loop below could never terminate. MRI's step count is + // (hi-lo)/step — NaN when hi is also infinite, negative when hi is finite — + // so it yields nothing here. (An infinite start with an infinite step is + // handled above.) + return + } for i := 0; ; i++ { v := lo + float64(i)*step if !stepInRange(v, hi, step, exclusive) { diff --git a/internal/vm/defined.go b/internal/vm/defined.go index 3149226a..a9b154a2 100644 --- a/internal/vm/defined.go +++ b/internal/vm/defined.go @@ -123,5 +123,5 @@ func (vm *VM) runDefinedGuard(child *bytecode.ISeq, self object.Value, definee * panic(r) // control-flow signals / internal bugs propagate } }() - return vm.exec(child, self, nil, definee, "", parentEnv, block, nil, nil) + return vm.exec(child, self, nil, definee, "", parentEnv, block, nil, block, nil) } diff --git a/internal/vm/eval.go b/internal/vm/eval.go index def2e2b7..562d2a31 100644 --- a/internal/vm/eval.go +++ b/internal/vm/eval.go @@ -22,7 +22,7 @@ func (vm *VM) instanceEvalString(self object.Value, src, file string) object.Val definee = vm.classOf(self) } vm.pendingMethodCtx = vm.currentMethodCtxPtr() - return vm.exec(iseq, self, nil, definee, "", nil, nil, nil, nil) + return vm.exec(iseq, self, nil, definee, "", nil, nil, nil, nil, nil) } // registerEval installs Kernel#eval — the embedded front-end's reason for being. @@ -57,6 +57,6 @@ func (vm *VM) registerEval() { // inherits the caller's method context (eval "__method__" inside a method // reports that method), so hand exec the caller's pair. vm.pendingMethodCtx = vm.currentMethodCtxPtr() - return vm.exec(iseq, self, nil, vm.classOf(self), "", nil, nil, nil, nil) + return vm.exec(iseq, self, nil, vm.classOf(self), "", nil, nil, nil, nil, nil) }) } diff --git a/internal/vm/irb_bind.go b/internal/vm/irb_bind.go index 9d2f8135..51e4bef3 100644 --- a/internal/vm/irb_bind.go +++ b/internal/vm/irb_bind.go @@ -69,7 +69,7 @@ func (vm *VM) irbEval(b *Binding, code string) (result object.Value, errClass, e result, errClass, errMsg, raised = nil, rerr.Class, rerr.Message, true } }() - result = vm.exec(iseq, b.self, nil, b.definee, "", b.env, nil, nil, nil) + result = vm.exec(iseq, b.self, nil, b.definee, "", b.env, nil, nil, nil, nil) return result, "", "", false } diff --git a/internal/vm/object_model.go b/internal/vm/object_model.go index 90af57f3..4e3c77b0 100644 --- a/internal/vm/object_model.go +++ b/internal/vm/object_model.go @@ -1842,7 +1842,7 @@ func (vm *VM) invokeBody(m *Method, self object.Value, args []object.Value, blk // method's original name (unchanged through an alias), __callee__ the name it // was called by (m.name — the alias when aliased). vm.pendingMethodCtx = &frameMethod{orig: methodOriginalName(m), callee: m.name} - return vm.exec(m.iseq, self, args, m.owner, m.name, nil, blk, nil, m.lexScope) + return vm.exec(m.iseq, self, args, m.owner, m.name, nil, blk, nil, blk, m.lexScope) } // callBlock invokes a captured block with args. Block arity is lenient: extra @@ -1928,7 +1928,20 @@ func (vm *VM) callBlockSelf(p *Proc, self object.Value, args []object.Value) obj copy(cp, args) return p.native(vm, cp) } - return vm.exec(p.iseq, self, vm.bindBlockArgs(p, args), vm.blockDefinee(p), "", p.env, p.block, p, nil) + return vm.exec(p.iseq, self, vm.bindBlockArgs(p, args), vm.blockDefinee(p), "", p.env, p.block, p, p.block, nil) +} + +// callProcWithBlock invokes a proc/lambda through Proc#call/[]/=== with the block +// the caller passed to that call. That block (blk) binds the proc's own `&b` +// block parameter, while a bare `yield` inside the proc still reaches the block +// captured where the proc was defined (p.block) — the two diverge, matching MRI. +func (vm *VM) callProcWithBlock(p *Proc, args []object.Value, blk *Proc) object.Value { + if p.native != nil { + cp := make([]object.Value, len(args)) + copy(cp, args) + return p.native(vm, cp) + } + return vm.exec(p.iseq, p.self, vm.bindBlockArgs(p, args), vm.blockDefinee(p), "", p.env, p.block, p, blk, nil) } // blockDefinee returns the definee a block body runs with: the block's captured @@ -1972,7 +1985,7 @@ func (vm *VM) callProcMethod(p *Proc, self object.Value, args []object.Value, bl // The define_method body IS the method: __method__ / __callee__ both report the // method name it was defined under (and a block nested in the body inherits it). anchored.methodCtx = frameMethod{orig: name, callee: name} - return vm.exec(p.iseq, self, vm.bindBlockArgs(p, args), vm.blockDefinee(p), "", p.env, body, &anchored, nil) + return vm.exec(p.iseq, self, vm.bindBlockArgs(p, args), vm.blockDefinee(p), "", p.env, body, &anchored, body, nil) } // classEval runs a block as class_eval/module_eval would: self and the method @@ -1981,7 +1994,7 @@ func (vm *VM) callProcMethod(p *Proc, self object.Value, args []object.Value, bl func (vm *VM) classEval(cls *RClass, p *Proc, args []object.Value) object.Value { // class_eval/module_eval always receive a literal (ISeq) block, never a // synthesized native Proc, so no native fast path is needed here. - return vm.exec(p.iseq, cls, vm.bindBlockArgs(p, args), cls, "", p.env, p.block, p, nil) + return vm.exec(p.iseq, cls, vm.bindBlockArgs(p, args), cls, "", p.env, p.block, p, p.block, nil) } // classEvalString runs Ruby source as class_eval/module_eval would for the @@ -1994,7 +2007,7 @@ func (vm *VM) classEvalString(cls *RClass, src string) object.Value { return raise("SyntaxError", "%s", cerr.Error()) } iseq.Name = "(eval)" - return vm.exec(iseq, cls, nil, cls, "", nil, nil, nil, nil) + return vm.exec(iseq, cls, nil, cls, "", nil, nil, nil, nil, nil) } // bindBlockArgs maps call args onto a block's parameters, with the auto-splat a diff --git a/internal/vm/proc_block_arg_test.go b/internal/vm/proc_block_arg_test.go new file mode 100644 index 00000000..2780b4a5 --- /dev/null +++ b/internal/vm/proc_block_arg_test.go @@ -0,0 +1,55 @@ +package vm_test + +import "testing" + +// TestProcCallBlockArg covers a proc/lambda's own `&b` block parameter binding to +// the block passed to Proc#call / [] / .() — previously discarded, so `&b` was +// nil — while a bare `yield` inside the proc still reaches the block captured +// where the proc was defined. Verified against MRI Ruby 4.0.6. +func TestProcCallBlockArg(t *testing.T) { + cases := []struct{ src, want string }{ + // The block passed to .call binds &b. + {`f = ->(&b){ b.call(5) }; p f.call { |x| x * 2 }`, "10\n"}, + {`g = ->(a, &b){ b.call(a) }; p g.call(3) { |x| x + 1 }`, "4\n"}, + {`pr = proc { |&b| b.call(2) }; p pr.call { |x| x * 3 }`, "6\n"}, + // A splat plus &b, and the block forwarded on to another method. + {`l = ->(*a, &b){ [a, b.call] }; p l.call(1, 2) { 99 }`, "[[1, 2], 99]\n"}, + {`st = ->(r, *a, &b){ r.step(*a, &b) } +res = []; st.call(1, 10, 3) { |x| res << x }; p res`, "[1, 4, 7, 10]\n"}, + // &b is nil when the proc is called without a block. + {`f = ->(&b){ b.nil? }; p f.call`, "true\n"}, + // A bare `yield` inside a proc still reaches the block captured where the + // proc was defined, NOT the block passed to .call (MRI semantics). + {`def outer; pr = proc { yield }; pr.call { :passed }; end +p(outer { :captured })`, ":captured\n"}, + // Proc#[] and .() thread the block too. + {`f = ->(&b){ b.call(4) }; p f[] { |x| x - 1 }`, "3\n"}, + // A method's &b (already working) is unaffected. + {`def m(&b); b.call(7); end; p m { |x| x * 10 }`, "70\n"}, + } + for _, c := range cases { + if got := eval(t, c.src); got != c.want { + t.Errorf("src=%q\n got=%q\nwant=%q", c.src, got, c.want) + } + } +} + +// TestNumericStepInfiniteStart covers Numeric#step with a non-finite start and a +// finite step: MRI's step count is (stop-start)/step — NaN or negative here — so +// it yields nothing rather than looping forever on the constant term. An infinite +// step still yields the start once. Verified against MRI Ruby 4.0.6. +func TestNumericStepInfiniteStart(t *testing.T) { + cases := []struct{ src, want string }{ + {`n = 0; Float::INFINITY.step(Float::INFINITY, 1) { n += 1 }; p n`, "0\n"}, + {`n = 0; Float::INFINITY.step(10, 1) { n += 1 }; p n`, "0\n"}, + {`n = 0; (-Float::INFINITY).step(-Float::INFINITY, -1) { n += 1 }; p n`, "0\n"}, + // A finite walk is unchanged. + {`a = []; 1.0.step(10.0, 3.0) { |x| a << x }; p a`, "[1.0, 4.0, 7.0, 10.0]\n"}, + {`a = []; 0.0.step(1.0, 0.1) { |x| a << x }; p a.size`, "11\n"}, + } + for _, c := range cases { + if got := eval(t, c.src); got != c.want { + t.Errorf("src=%q\n got=%q\nwant=%q", c.src, got, c.want) + } + } +} diff --git a/internal/vm/require.go b/internal/vm/require.go index 77574571..6b71b5be 100644 --- a/internal/vm/require.go +++ b/internal/vm/require.go @@ -184,7 +184,7 @@ func (vm *VM) doLoad(name string) object.Value { setISeqFile(iseq, abs) vm.requireDirs = append(vm.requireDirs, filepath.Dir(abs)) defer func() { vm.requireDirs = vm.requireDirs[:len(vm.requireDirs)-1] }() - vm.exec(iseq, vm.main, nil, vm.cObject, "", nil, nil, nil, nil) + vm.exec(iseq, vm.main, nil, vm.cObject, "", nil, nil, nil, nil, nil) return object.Bool(true) } return raise("LoadError", "cannot load such file -- %s", name) @@ -248,7 +248,7 @@ func (vm *VM) doRequire(name string, relative bool) object.Value { // Push the file's directory so a nested require_relative resolves against it. vm.requireDirs = append(vm.requireDirs, filepath.Dir(abs)) defer func() { vm.requireDirs = vm.requireDirs[:len(vm.requireDirs)-1] }() - vm.exec(iseq, vm.main, nil, vm.cObject, "", nil, nil, nil, nil) + vm.exec(iseq, vm.main, nil, vm.cObject, "", nil, nil, nil, nil, nil) return object.Bool(true) } return raise("LoadError", "cannot load such file -- %s", name) diff --git a/internal/vm/sinatra_erb_open.go b/internal/vm/sinatra_erb_open.go index 98b0e013..c553f246 100644 --- a/internal/vm/sinatra_erb_open.go +++ b/internal/vm/sinatra_erb_open.go @@ -42,5 +42,5 @@ func (vm *VM) sinatraErbEval(sc *SinatraCtx, src string, names []string, vals [] slots := make([]object.Value, len(vals)) copy(slots, vals) parent := &Env{slots: slots} - return vm.exec(iseq, sc, nil, vm.classOf(sc), "", parent, block, nil, nil) + return vm.exec(iseq, sc, nil, vm.classOf(sc), "", parent, block, nil, block, nil) } diff --git a/internal/vm/vm.go b/internal/vm/vm.go index a9dfced9..631bec08 100644 --- a/internal/vm/vm.go +++ b/internal/vm/vm.go @@ -1039,7 +1039,13 @@ type frameMethod struct { callee string } -func (vm *VM) exec(iseq *bytecode.ISeq, self object.Value, args []object.Value, definee *RClass, methodName string, parentEnv *Env, block, selfBlock *Proc, methodLexScope *RClass) (execResult object.Value) { +// blockArg is the block reified by a `&b` block parameter (iseq.BlockSlot). It is +// normally the same as block (a method's block backs both `&b` and a bare +// `yield`), but for Proc#call they diverge: the block passed to `.call` binds +// `&b`, while `yield` inside the proc still reaches the block captured where the +// proc was defined (block). Passing blockArg == block preserves the old +// behaviour everywhere it is not deliberately split. +func (vm *VM) exec(iseq *bytecode.ISeq, self object.Value, args []object.Value, definee *RClass, methodName string, parentEnv *Env, block, selfBlock, blockArg *Proc, methodLexScope *RClass) (execResult object.Value) { // Determine this frame's __method__/__callee__ pair before any nested call can // disturb pendingMethodCtx: a block inherits the pair captured on the block // Proc, an invokeBody/eval caller supplies one via pendingMethodCtx, and any @@ -1120,10 +1126,12 @@ func (vm *VM) exec(iseq *bytecode.ISeq, self object.Value, args []object.Value, env.slots[iseq.KwRestSlot] = rest } } - // &block reifies the method's block as a Proc (nil → no block given). + // &block reifies the block bound to `&b` as a Proc (nil → no block given). + // This is blockArg, which for a plain method equals block but for Proc#call is + // the block passed to `.call` rather than the proc's captured yield-block. if iseq.BlockSlot >= 0 { - if block != nil { - env.slots[iseq.BlockSlot] = block + if blockArg != nil { + env.slots[iseq.BlockSlot] = blockArg } else { env.slots[iseq.BlockSlot] = object.NilV } @@ -1682,7 +1690,7 @@ func (vm *VM) exec(iseq *bytecode.ISeq, self object.Value, args []object.Value, if sc.lexParent == nil && definee != nil && definee != vm.cObject { sc.lexParent = definee } - push(vm.exec(iseq.Children[in.A], sc, nil, sc, "", nil, nil, nil, nil)) + push(vm.exec(iseq.Children[in.A], sc, nil, sc, "", nil, nil, nil, nil, nil)) case bytecode.OpAlias: vm.aliasMethod(definee, iseq.Names[in.A], iseq.Names[in.B]) push(object.NilV) @@ -2261,7 +2269,7 @@ func (vm *VM) defineClassIn(parent *RClass, name string, body *bytecode.ISeq, su // module_function mode (MRI resets these on every (re)open). class.defaultVis, class.funcMode = visPublic, false adoptReopenLexParent(class, parent, scoped) - return vm.exec(body, class, nil, class, "", nil, nil, nil, nil) + return vm.exec(body, class, nil, class, "", nil, nil, nil, nil, nil) } // adoptReopenLexParent upgrades a class/module's lexParent when it is reopened @@ -2303,7 +2311,7 @@ func (vm *VM) defineModuleIn(parent *RClass, name string, body *bytecode.ISeq, s } mod.defaultVis, mod.funcMode = visPublic, false adoptReopenLexParent(mod, parent, scoped) - return vm.exec(body, mod, nil, mod, "", nil, nil, nil, nil) + return vm.exec(body, mod, nil, mod, "", nil, nil, nil, nil, nil) } // asModuleParent coerces a popped value to the class/module that a scoped diff --git a/scripts/conformance/rubyspec/FLOOR b/scripts/conformance/rubyspec/FLOOR index 6d617e28..7d75b0ab 100644 --- a/scripts/conformance/rubyspec/FLOOR +++ b/scripts/conformance/rubyspec/FLOOR @@ -1 +1 @@ -17270 +17300