diff --git a/internal/bytecode/bytecode.go b/internal/bytecode/bytecode.go index 6fbada4b..24ff840e 100644 --- a/internal/bytecode/bytecode.go +++ b/internal/bytecode/bytecode.go @@ -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 diff --git a/internal/vm/modulefunc.go b/internal/vm/modulefunc.go index f146e68d..373d452b 100644 --- a/internal/vm/modulefunc.go +++ b/internal/vm/modulefunc.go @@ -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 — @@ -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 { @@ -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 diff --git a/internal/vm/proc.go b/internal/vm/proc.go index 5658e697..10566c59 100644 --- a/internal/vm/proc.go +++ b/internal/vm/proc.go @@ -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 }) diff --git a/internal/vm/vm.go b/internal/vm/vm.go index 3a4d59c4..5a8f3309 100644 --- a/internal/vm/vm.go +++ b/internal/vm/vm.go @@ -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 { @@ -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] diff --git a/internal/vm/wave_c_ruby2_keywords.go b/internal/vm/wave_c_ruby2_keywords.go new file mode 100644 index 00000000..dcf9737f --- /dev/null +++ b/internal/vm/wave_c_ruby2_keywords.go @@ -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 + } +} diff --git a/internal/vm/wave_c_ruby2_keywords_test.go b/internal/vm/wave_c_ruby2_keywords_test.go new file mode 100644 index 00000000..07b5bffc --- /dev/null +++ b/internal/vm/wave_c_ruby2_keywords_test.go @@ -0,0 +1,368 @@ +package vm + +import ( + "strings" + "testing" + + "github.com/go-embedded-ruby/ruby/internal/bytecode" + "github.com/go-embedded-ruby/ruby/internal/object" +) + +// Every expectation below was taken from MRI 4.0.5 (ruby 4.0.5 (2026-05-20 +// revision 64336ffd0e) +PRISM [arm64-darwin25]) by running the same program. + +// TestRuby2KeywordsMarkAndPropagate is the pair of observables the feature is +// made of, run TOGETHER on purpose. Marking alone satisfies +// Hash.ruby2_keywords_hash? and still loses every keyword on delegation, so a +// test that only asked the predicate would pass over a half-built feature. +func TestRuby2KeywordsMarkAndPropagate(t *testing.T) { + t.Run("the predicate sees the mark", func(t *testing.T) { + got := runSrc(t, ` + c = Class.new { ruby2_keywords def m(*args) = args.last } + p Hash.ruby2_keywords_hash?(c.new.m(a: 1)) + `) + if got != "true" { + t.Fatalf("Hash.ruby2_keywords_hash?(m(a: 1)) = %s, want true", got) + } + }) + + t.Run("delegation re-splats it as keywords", func(t *testing.T) { + // Without the re-splat half the target reports [[{a: 1}], {}] — the hash + // arrives POSITIONAL — which is exactly the state this branch found. + got := runSrc(t, ` + def target(*a, **k) = [a, k] + c = Class.new { ruby2_keywords def m(*args) = target(*args) } + p c.new.m(a: 1) + `) + if want := "[[], {a: 1}]"; got != want { + t.Fatalf("delegated call = %s, want %s", got, want) + } + }) + + t.Run("the caller's hash is neither copied into nor marked", func(t *testing.T) { + // core/module/ruby2_keywords_spec.rb, "makes a copy of the hash and only + // marks the copy as keyword hash". + got := runSrc(t, ` + c = Class.new { ruby2_keywords def m(*args) = args.last } + h = {a: 1} + marked = c.new.m(**h) + p [Hash.ruby2_keywords_hash?(marked), Hash.ruby2_keywords_hash?(h), marked.equal?(h)] + `) + if want := "[true, false, false]"; got != want { + t.Fatalf("got %s, want %s", got, want) + } + }) + + t.Run("the callee receives an unmarked copy", func(t *testing.T) { + // The flag travels exactly one hop: `splat` is a plain *args method, so + // what reaches it must be a fresh hash with the flag cleared. + got := runSrc(t, ` + c = Class.new do + ruby2_keywords def m(*args) = args + def splat(*args) = args.last + end + o = c.new + args = o.m(a: 1) + marked = args.last + after = o.splat(*args) + p [after == {a: 1}, after.equal?(marked), + Hash.ruby2_keywords_hash?(after), Hash.ruby2_keywords_hash?(marked)] + `) + if want := "[true, false, false, true]"; got != want { + t.Fatalf("got %s, want %s", got, want) + } + }) + + t.Run("a marked hash passed positionally keeps its identity and its flag", func(t *testing.T) { + // The one shape the re-splat must NOT touch: no splat at the call site. + got := runSrc(t, ` + c = Class.new do + ruby2_keywords def m(*args) = args.last + def single(arg) = arg + end + o = c.new + marked = o.m(a: 1) + after = o.single(marked) + p [after.equal?(marked), Hash.ruby2_keywords_hash?(after)] + `) + if want := "[true, true]"; got != want { + t.Fatalf("got %s, want %s", got, want) + } + }) + + t.Run("the flag rides the ISeq, so aliasing sees it either way round", func(t *testing.T) { + // vm_method.c writes ISEQ_BODY(...)->param.flags.ruby2_keywords, which an + // alias shares; putting the flag on the Method would make the order of + // `alias_method` and `ruby2_keywords` matter, and it does not. + got := runSrc(t, ` + c = Class.new do + def foo(*a) = a.last + alias_method :bar, :foo + ruby2_keywords :foo + + def baz(*a) = a.last + ruby2_keywords :baz + alias_method :bob, :baz + end + o = c.new + p [:foo, :bar, :baz, :bob].map { |n| Hash.ruby2_keywords_hash?(o.send(n, 1, a: 2)) } + `) + if want := "[true, true, true, true]"; got != want { + t.Fatalf("got %s, want %s", got, want) + } + }) + + t.Run("a proc carries it across duplication", func(t *testing.T) { + got := runSrc(t, ` + pr = proc { |*args| args.last } + pr.ruby2_keywords + p [Hash.ruby2_keywords_hash?(pr.call(a: 1)), + Hash.ruby2_keywords_hash?(pr.dup.call(a: 1))] + `) + if want := "[true, true]"; got != want { + t.Fatalf("got %s, want %s", got, want) + } + }) + + t.Run("an unmarkable shape warns and stays unmarked", func(t *testing.T) { + // The negative control: without it, a test that only looked at marked + // methods could not tell the flag from an unconditional one. + got := runSrc(t, ` + c = Class.new { def m(*a, **k) = a.last } + p Hash.ruby2_keywords_hash?(c.new.m(a: 1) || {}) + `) + if got != "false" { + t.Fatalf("an (*a, **k) method must not mark: got %s", got) + } + }) + + t.Run("top-level ruby2_keywords marks a method on Object", func(t *testing.T) { + // MRI registers it as a private singleton method of main + // (vm_method.c:3560); before that entry existed this raised NoMethodError. + got := runSrc(t, ` + def tl(*args) = args.last + ruby2_keywords :tl + p Hash.ruby2_keywords_hash?(tl(a: 1)) + `) + if got != "true" { + t.Fatalf("top-level ruby2_keywords: got %s, want true", got) + } + }) +} + +// TestRuby2KeywordsFourCallShapes separates the four ways a Hash can reach a +// ruby2_keywords `*rest`, because they must NOT all answer alike. MRI 4.0.5: +// +// mid(k: 1) => marked (VM_CALL_KWARG at the call site) +// mid({k: 1}) => NOT marked (a positional Hash, no keyword flag) +// mid(**h) => marked (VM_CALL_KW_SPLAT) +// mid(*arr) => NOT marked (the splat is positional; MRI only re-flags when +// arr's last element is ALREADY a marked Hash) +// +// What decides is the CALL SITE's keyword flag, never the runtime type of the +// last argument: a rule keyed off "is the last element a Hash" would mark +// shapes 2 and 4 too. rbgo's stand-in for the flag pair is +// bytecode.FlagSendNoKW / FlagSendKWSplat, which is what ruby2KeywordsBindRest +// reads as noKW — shape 4 answers correctly only because of it. +// +// Shape 2 is rbgo's ONE divergence here, and it is not this feature's: the +// compiler cannot see braces, because go-ruby-parser v0.8.0 parses `f({k: 1})` +// and `f(k: 1)` into the same *ast.HashLit with no record of the braces and no +// position to re-read the source at (ast.Call carries only Args []Node). So the +// sub-test below pins the INVARIANT rather than the wrong value: a +// ruby2_keywords delegator must read a braced literal exactly as an ordinary +// `**kwargs` method does — this feature must not be MORE wrong than the rest of +// the VM. On origin/main `f({x: 1})` with `def f(*a, **k)` already answers +// [[], {x: 1}] where MRI answers [[{x: 1}], {}]. When the parser grows a +// `Braced` bit both sides move together and this test keeps passing. +func TestRuby2KeywordsFourCallShapes(t *testing.T) { + const prelude = ` + def target(*a, **kw) = [a, kw] + ruby2_keywords def mid(*a) = target(*a) + ruby2_keywords def keep(*a) = a + h = {k: 1} + arr = [{k: 1}] + ` + // Shapes 1, 3 and 4 are asserted at MRI 4.0.5's answers, verified by running + // the same program under `ruby 4.0.5 (2026-05-20 revision 64336ffd0e)`. + for _, tc := range []struct{ name, call, delegated, marked string }{ + {"keyword list is marked", "mid(k: 1)", "[[], {k: 1}]", "true"}, + {"double splat is marked", "mid(**h)", "[[], {k: 1}]", "true"}, + {"a splatted Array stays positional", "mid(*arr)", "[[{k: 1}], {}]", "false"}, + } { + t.Run(tc.name, func(t *testing.T) { + got := runSrc(t, prelude+"\np "+tc.call) + if got != tc.delegated { + t.Fatalf("%s delegated = %s, want %s (MRI 4.0.5)", tc.call, got, tc.delegated) + } + mark := strings.Replace(tc.call, "mid(", "keep(", 1) + got = runSrc(t, prelude+"\np Hash.ruby2_keywords_hash?("+mark+".last)") + if got != tc.marked { + t.Fatalf("%s marked = %s, want %s (MRI 4.0.5)", mark, got, tc.marked) + } + }) + } + + t.Run("a braced literal reads the same as it does without ruby2_keywords", func(t *testing.T) { + // The control is the SUBJECT's own sibling: an ordinary **kwargs method, + // which has nothing to do with ruby2_keywords and whose reading of + // `f({k: 1})` predates this change. The two must agree. If they ever + // disagree, this feature has invented a divergence of its own, which is + // the thing worth failing on — not the shared parser gap. + plain := runSrc(t, "def f(*a, **k) = [a, k]\np f({k: 1})") + viaR2K := runSrc(t, prelude+"\np mid({k: 1})") + if plain != viaR2K { + t.Fatalf("braced literal read differently through ruby2_keywords: plain=%s r2k=%s", plain, viaR2K) + } + // And it is still the compiler's verdict doing the deciding, not the + // argument's type: the same Hash reaching the same delegator through a + // SPLAT is left positional and unmarked (shape 4 above). + if plain == "[[{k: 1}], {}]" { + t.Skip("the parser grew a Braced bit — MRI parity reached; drop this control and assert MRI directly") + } + }) +} + +// TestRuby2KeywordsBindRest drives the callee half's arms directly, including +// the ones no Ruby program can reach through the compiler (a *rest slot holding +// something that is not an Array). +func TestRuby2KeywordsBindRest(t *testing.T) { + newHash := func() *object.Hash { + h := object.NewHash() + h.Set(object.SymVal("a"), object.IntValue(1)) + return h + } + marked := &bytecode.ISeq{Ruby2Keywords: true} + plain := &bytecode.ISeq{} + + t.Run("marks the trailing hash of a marked iseq", func(t *testing.T) { + h := newHash() + rest := object.NewArrayFromSlice([]object.Value{object.IntValue(9), h}) + ruby2KeywordsBindRest(marked, rest, false) + out, ok := rest.Elems[1].(*object.Hash) + if !ok || !out.Ruby2Keywords { + t.Fatalf("trailing hash not marked: %#v", rest.Elems[1]) + } + if out == h { + t.Fatal("the caller's hash was marked in place; MRI marks a COPY") + } + if h.Ruby2Keywords { + t.Fatal("the original hash was marked") + } + if v, _ := out.Get(object.SymVal("a")); v != object.IntValue(1) { + t.Fatalf("copy lost its entries: %v", v) + } + }) + + t.Run("leaves everything else alone", func(t *testing.T) { + for _, tc := range []struct { + name string + is *bytecode.ISeq + rest object.Value + noKW bool + }{ + {"unmarked iseq", plain, object.NewArrayFromSlice([]object.Value{newHash()}), false}, + {"positional call site", marked, object.NewArrayFromSlice([]object.Value{newHash()}), true}, + {"rest is not an Array", marked, object.NilV, false}, + {"empty rest", marked, object.NewArrayFromSlice(nil), false}, + {"last is not a Hash", marked, object.NewArrayFromSlice([]object.Value{object.IntValue(1)}), false}, + } { + t.Run(tc.name, func(t *testing.T) { + ruby2KeywordsBindRest(tc.is, tc.rest, tc.noKW) + arr, ok := tc.rest.(*object.Array) + if !ok || len(arr.Elems) == 0 { + return + } + if h, ok := arr.Elems[len(arr.Elems)-1].(*object.Hash); ok && h.Ruby2Keywords { + t.Fatalf("%s: hash was marked", tc.name) + } + }) + } + }) +} + +// TestRuby2KeywordsResplat drives the call-site half, and asserts the copy is a +// faithful rb_hash_dup: compare_by_identity, the default value and the default +// proc all survive it, because a delegated hash that silently lost its default +// would be a defect nothing in the spec files would catch. +func TestRuby2KeywordsResplat(t *testing.T) { + t.Run("a marked tail turns the call into a keyword call", func(t *testing.T) { + h := object.NewHash() + h.Set(object.SymVal("a"), object.IntValue(1)) + h.Ruby2Keywords = true + in := []object.Value{object.IntValue(7), h} + out, isKW := ruby2KeywordsResplat(in) + if !isKW { + t.Fatal("a marked trailing hash must make the call a keyword call") + } + if out[0] != object.IntValue(7) { + t.Fatalf("leading arguments disturbed: %v", out[0]) + } + got := out[1].(*object.Hash) + if got == h || got.Ruby2Keywords { + t.Fatalf("the callee must get an unmarked COPY, got same=%v marked=%v", got == h, got.Ruby2Keywords) + } + if !h.Ruby2Keywords { + t.Fatal("the caller's hash lost its mark") + } + if in[1] != object.Value(h) { + t.Fatal("the input slice was mutated") + } + }) + + t.Run("leaves everything else alone", func(t *testing.T) { + plain := object.NewHash() + for _, tc := range []struct { + name string + in []object.Value + }{ + {"no arguments", nil}, + {"tail is not a Hash", []object.Value{object.IntValue(1)}}, + {"tail is an unmarked Hash", []object.Value{plain}}, + } { + t.Run(tc.name, func(t *testing.T) { + out, isKW := ruby2KeywordsResplat(tc.in) + if isKW { + t.Fatalf("%s: reported a keyword call", tc.name) + } + if len(out) != len(tc.in) { + t.Fatalf("%s: list length changed", tc.name) + } + }) + } + }) + + t.Run("the copy is a faithful rb_hash_dup", func(t *testing.T) { + src := object.NewHash() + src.CompareByIdentity() + src.Default = object.IntValue(42) + src.Ruby2Keywords = true + out := ruby2KeywordsUnmark(src) + if !out.Identity { + t.Fatal("compare_by_identity lost") + } + if out.Default != object.IntValue(42) { + t.Fatalf("default lost: %v", out.Default) + } + if out.Ruby2Keywords { + t.Fatal("unmark did not clear the flag") + } + back := ruby2KeywordsMark(out) + if !back.Ruby2Keywords || !back.Identity { + t.Fatalf("mark: flag=%v identity=%v", back.Ruby2Keywords, back.Identity) + } + }) +} + +// TestMarkRuby2KeywordsISeqNil pins the nil guard: Module#ruby2_keywords reaches +// this with methodISeq(own), which is nil for a method whose body is not Ruby — +// a case the surrounding switch already warns about, so the write must simply do +// nothing rather than panic if the two ever drift apart. +func TestMarkRuby2KeywordsISeqNil(t *testing.T) { + markRuby2KeywordsISeq(nil) + is := &bytecode.ISeq{} + markRuby2KeywordsISeq(is) + if !is.Ruby2Keywords { + t.Fatal("flag not set") + } +}