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
14 changes: 14 additions & 0 deletions internal/bytecode/bytecode.go
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,20 @@ type ISeq struct {
// was defined rather than line 0.
FirstLine int

// Ruby2Keywords is MRI's ISEQ_BODY(iseq)->param.flags.ruby2_keywords, set by
// Module#ruby2_keywords / Proc#ruby2_keywords (vm_method.c
// rb_mod_ruby2_keywords:3114, proc.c proc_ruby2_keywords:4133 at tag
// ruby_4_0). It can only be set on an ISeq that has a *rest and neither
// keyword parameters nor post positionals, which is what makes it
// unambiguous: a trailing keyword Hash bound into that *rest is FLAGGED
// (RHASH_PASS_AS_KEYWORDS) so a later `f(*rest)` re-splats it as keywords.
//
// It lives on the ISeq, not the Method, because MRI marks the instruction
// sequence: an alias created either before or after the call shares it, which
// is the "applies across aliasing" contract, and a Proc duplicated with #dup
// or #clone shares it too.
Ruby2Keywords bool

// Caches backs the per-call-site inline method caches, one slot per
// instruction (only OpSend slots are ever used). It is opaque to this package
// — the vm package allocates it and gives it meaning — so the field is typed
Expand Down
34 changes: 25 additions & 9 deletions internal/vm/modulefunc.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,19 @@ func (vm *VM) registerModuleExtras() {
native: func(vm *VM, _ object.Value, args []object.Value, blk *Proc) object.Value {
return vm.send(vm.cObject, "define_method", args, blk)
}}
// Top-level `ruby2_keywords :foo` marks a method on Object, main's default
// definee. MRI registers it exactly like top_public/top_private — as a
// PRIVATE singleton method of main (vm_method.c:3560 at tag ruby_4_0),
// whose body is rb_mod_ruby2_keywords applied to rb_top_main_class, i.e.
// Object. It has to be a singleton method and not merely Module's private
// instance method, because main is not a Module: without this entry
// `main.private_methods(false)` does not list it and a top-level
// `ruby2_keywords :foo` finds no receiver that answers.
sc.methods["ruby2_keywords"] = &Method{name: "ruby2_keywords", owner: sc, vis: visPrivate,
native: func(vm *VM, _ object.Value, args []object.Value, _ *Proc) object.Value {
return vm.send(vm.cObject, "ruby2_keywords", args, nil)
}}

// Bare `include M` at the top level mixes M into Object, so its constants
// and methods become globally visible. MRI defines this as a PRIVATE
// singleton method on main that forwards to Module#include on Object —
Expand Down Expand Up @@ -173,15 +186,11 @@ func (vm *VM) registerModuleExtras() {
// warning shows at $VERBOSE == false and is silenced only by nil. It returns
// nil whatever happened.
//
// The flag itself is not carried yet: MRI writes
// ISEQ_BODY(...)->param.flags.ruby2_keywords, which rbgo's bytecode.ISeq has
// no field for, and honouring it belongs to the splat binding. So a markable
// method is accepted silently and behaves as it did before — the same
// partial contract Proc#ruby2_keywords already ships in this VM. What IS
// decided here is every observable that does not depend on the flag: the
// method exists (a program guarded by `respond_to?(:ruby2_keywords, true)`
// no longer dies on NoMethodError), the arity/TypeError/NameError contract,
// and the four warnings.
// A markable name sets bytecode.ISeq.Ruby2Keywords, MRI's
// ISEQ_BODY(...)->param.flags.ruby2_keywords. Honouring it is the splat
// binding's job, in wave_c_ruby2_keywords.go: the callee side flags the
// trailing keyword Hash bound into the *rest, and the call site re-splats a
// flagged Hash as keywords.
vm.cModule.define("ruby2_keywords", func(vm *VM, self object.Value, args []object.Value, _ *Proc) object.Value {
mod := self.(*RClass)
if len(args) == 0 {
Expand Down Expand Up @@ -218,6 +227,13 @@ func (vm *VM) registerModuleExtras() {
// no post-splat positional either, which is also why 4.0 names post
// arguments in the text where 3.4 did not.
skip(name, "method accepts keywords or post arguments or method does not accept argument splat")
default:
// ISEQ_BODY(me->def->body.iseq.iseqptr)->param.flags.ruby2_keywords = 1
// (vm_method.c rb_mod_ruby2_keywords:3114 at tag ruby_4_0). The write
// lands on the ISeq, which an alias shares whichever order the two
// were written in — the spec's "applies to the underlying method and
// applies across aliasing".
markRuby2KeywordsISeq(methodISeq(own))
}
}
return object.NilV
Expand Down
11 changes: 9 additions & 2 deletions internal/vm/proc.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,15 +69,22 @@ func (vm *VM) registerProcMethods() {
// *rest as a flagged hash. Only a proc that accepts an argument splat and
// neither keywords, a keyword-splat nor post-splat positionals is markable;
// any other shape prints a warning and is left unchanged. Either way it returns
// self. (Full flag propagation needs Hash keyword-flagging, so this installs the
// MRI-visible warning + self-return contract.)
// self.
//
// The flag is written on the ISeq, which is MRI's
// ISEQ_BODY(proc->block.as.captured.code.iseq)->param.flags.ruby2_keywords = 1
// (proc.c proc_ruby2_keywords:4133 at tag ruby_4_0). Putting it there rather
// than on the Proc is what makes it survive #dup and #clone, which share the
// ISeq — core/proc/ruby2_keywords_spec.rb's "applies across duplication".
vm.cProc.define("ruby2_keywords", func(vm *VM, self object.Value, _ []object.Value, _ *Proc) object.Value {
p := self.(*Proc)
if !procRuby2KeywordsMarkable(p.iseq) {
vm.send(vm.main, "warn", []object.Value{
object.NewString("Skipping set of ruby2_keywords flag for proc (proc accepts keywords or proc does not accept argument splat)"),
}, nil)
return self
}
markRuby2KeywordsISeq(p.iseq)
return self
})

Expand Down
11 changes: 11 additions & 0 deletions internal/vm/vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -1350,6 +1350,13 @@ func iseqPostCount(is *bytecode.ISeq) int {
// FlagSendNoKW.
func applyKWSplat(elems []object.Value, flags int) ([]object.Value, bool) {
if flags&bytecode.FlagSendKWSplat == 0 {
// A ruby2_keywords-flagged Hash arriving last in a splatted argument list
// turns this call into a keyword call, whatever the compiler concluded
// about the written syntax (vm_caller_setup_arg_splat sets
// VM_CALL_KW_SPLAT). See wave_c_ruby2_keywords.go.
if out, isKW := ruby2KeywordsResplat(elems); isKW {
return out, false
}
return elems, flags&bytecode.FlagSendNoKW != 0
}
if n := len(elems); n > 0 {
Expand Down Expand Up @@ -1560,6 +1567,10 @@ func (vm *VM) exec(iseq *bytecode.ISeq, self object.Value, args []object.Value,
rest = append(rest, args[si:avail]...)
}
env.slots[si] = object.NewArrayFromSlice(rest)
// ruby2_keywords: a trailing keyword Hash bound into this *rest is
// replaced by a FLAGGED copy, so a later `f(*rest)` re-splats it as
// keywords. See wave_c_ruby2_keywords.go.
ruby2KeywordsBindRest(iseq, env.slots[si], noKW)
for j := 0; j < npost; j++ {
if srcIdx := len(args) - npost + j; srcIdx >= 0 && srcIdx < len(args) {
env.slots[si+1+j] = args[srcIdx]
Expand Down
125 changes: 125 additions & 0 deletions internal/vm/wave_c_ruby2_keywords.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
package vm

import (
"github.com/go-embedded-ruby/ruby/internal/bytecode"
"github.com/go-embedded-ruby/ruby/internal/object"
)

// ruby2_keywords is a delegation device, not a calling convention: it exists so
// that `def m(*args); target(*args); end` can forward keywords it never declared.
// Two halves make it work, and either one alone is invisible.
//
// 1. MARKING. When a method (or proc) whose ISeq carries
// param.flags.ruby2_keywords is called WITH keywords, MRI copies the trailing
// keyword Hash and sets RHASH_PASS_AS_KEYWORDS on the copy before binding it
// into the *rest (setup_parameters_complex, vm_args.c at tag ruby_4_0 — the
// `ISEQ_BODY(iseq)->param.flags.ruby2_keywords && kw_flag` arm, which calls
// rb_hash_dup + RHASH_SET_PASS_AS_KEYWORDS). The ORIGINAL is left alone, which
// is what core/module/ruby2_keywords_spec.rb states as "makes a copy of the
// hash and only marks the copy as keyword hash".
//
// 2. RE-SPLAT. When an argument Array is splatted at a call site and its last
// element is a marked Hash, that call is treated as carrying a keyword splat
// (vm_caller_setup_arg_splat sets VM_CALL_KW_SPLAT). The hash handed to the
// callee is a fresh UNMARKED copy, so the flag does not travel further than
// one delegation hop unless the next callee is itself ruby2_keywords.
//
// Marking without re-splatting passes Hash.ruby2_keywords_hash? and still fails
// every delegation, so both halves are tested together in
// wave_c_ruby2_keywords_test.go.
//
// The one shape that must NOT be touched is a marked Hash passed as an ordinary
// positional argument — `obj.single(marked)` — which keeps its identity and its
// flag. That is why the re-splat lives in applyKWSplat, which only the array
// dispatch opcodes reach, and not in the generic send path.

// ruby2KeywordsMark returns the Hash to bind into a ruby2_keywords *rest in
// place of h: a copy of h carrying the pass-as-keywords flag. MRI's
// rb_hash_dup + RHASH_SET_PASS_AS_KEYWORDS.
func ruby2KeywordsMark(h *object.Hash) *object.Hash {
out := copyHashPreservingShape(h)
out.Ruby2Keywords = true
return out
}

// ruby2KeywordsUnmark returns a copy of h with the flag cleared, which is what a
// re-splatted keyword hash is delivered as.
func ruby2KeywordsUnmark(h *object.Hash) *object.Hash {
out := copyHashPreservingShape(h)
out.Ruby2Keywords = false
return out
}

// copyHashPreservingShape duplicates a Hash the way MRI's rb_hash_dup does:
// entries in insertion order, plus the compare_by_identity flag, the default
// value and the default proc. The frozen bit is deliberately NOT copied — a dup
// of a frozen Hash is mutable.
func copyHashPreservingShape(h *object.Hash) *object.Hash {
out := newHashLike(h) // preserves compare_by_identity
out.Default = h.Default
out.DefaultProc = h.DefaultProc
for _, k := range h.Keys {
v, _ := h.Get(k)
out.Set(k, v)
}
return out
}

// ruby2KeywordsBindRest applies half 1 to a *rest that has just been filled.
// It is a no-op unless the callee's ISeq is marked, the call site passed its
// last argument AS keywords (noKW == false — see bytecode.FlagSendNoKW), and
// that last argument is a Hash.
//
// rest is the Array already stored in the splat slot; the replacement is written
// back in place, so the caller needs no return value.
func ruby2KeywordsBindRest(iseq *bytecode.ISeq, rest object.Value, noKW bool) {
if !iseq.Ruby2Keywords || noKW {
return
}
arr, ok := rest.(*object.Array)
if !ok || len(arr.Elems) == 0 {
return
}
last := len(arr.Elems) - 1
h, ok := arr.Elems[last].(*object.Hash)
if !ok {
return
}
arr.Elems[last] = ruby2KeywordsMark(h)
}

// ruby2KeywordsResplat applies half 2. It reports whether the last element of a
// splatted argument list is a marked Hash and, when it is, returns the list with
// that element replaced by an unmarked copy. The second result is the verdict to
// hand the callee: false means "these ARE keywords", matching the sense of
// bytecode.FlagSendNoKW.
//
// The element is replaced rather than left in place because the callee must not
// be able to observe the caller's flagged hash: core/module/ruby2_keywords_spec.rb
// requires `after_usage.should_not.equal?(marked)` and
// `Hash.ruby2_keywords_hash?(after_usage).should == false` for every delegation
// shape, while `marked` itself stays flagged.
func ruby2KeywordsResplat(elems []object.Value) ([]object.Value, bool) {
n := len(elems)
if n == 0 {
return elems, false
}
h, ok := elems[n-1].(*object.Hash)
if !ok || !h.Ruby2Keywords {
return elems, false
}
out := make([]object.Value, n)
copy(out, elems)
out[n-1] = ruby2KeywordsUnmark(h)
return out, true
}

// markRuby2KeywordsISeq sets the flag on a method's or proc's ISeq. It is the
// write MRI performs as ISEQ_BODY(...)->param.flags.ruby2_keywords = 1; the
// caller has already checked that the shape can carry it
// (procRuby2KeywordsMarkable).
func markRuby2KeywordsISeq(is *bytecode.ISeq) {
if is != nil {
is.Ruby2Keywords = true
}
}
Loading
Loading