Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion internal/vm/actionview_render_open.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
2 changes: 1 addition & 1 deletion internal/vm/aot_level2.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion internal/vm/binding_eval_open.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 12 additions & 2 deletions internal/vm/builtins.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion internal/vm/defined.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
4 changes: 2 additions & 2 deletions internal/vm/eval.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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)
})
}
2 changes: 1 addition & 1 deletion internal/vm/irb_bind.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
23 changes: 18 additions & 5 deletions internal/vm/object_model.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down
55 changes: 55 additions & 0 deletions internal/vm/proc_block_arg_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
}
4 changes: 2 additions & 2 deletions internal/vm/require.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion internal/vm/sinatra_erb_open.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
22 changes: 15 additions & 7 deletions internal/vm/vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion scripts/conformance/rubyspec/FLOOR
Original file line number Diff line number Diff line change
@@ -1 +1 @@
17270
17300