From eaa5834a4830cc6e9c5b6650e54742fbf141bd29 Mon Sep 17 00:00:00 2001 From: tannevaled Date: Sun, 30 Aug 2026 10:37:45 +0200 Subject: [PATCH] Add the private Time#_dump / Time._load Marshal hooks Time#_dump packs a Time into MRI's 8-byte marshal form (extracted from the Marshal codec's writeTime into a shared timeDumpBytes) and Time._load rebuilds a Time from those bytes. Both are private (as MRI has them): _dump an instance method absent from respond_to?, _load a private class method. Marshal round-trips are unchanged. Verified against ruby 4.0.6. Whole-package coverage stays at 100%; the ruby/spec floor rises to 16290 (core/time/_dump 0 -> 5, _load 0 -> 2, marshal/dump +1). Co-Authored-By: Claude Opus 4.8 --- internal/vm/marshal_codec.go | 15 +++++++++++---- internal/vm/time.go | 15 +++++++++++++++ internal/vm/time_conformance_test.go | 17 +++++++++++++++++ scripts/conformance/rubyspec/FLOOR | 2 +- 4 files changed, 44 insertions(+), 5 deletions(-) diff --git a/internal/vm/marshal_codec.go b/internal/vm/marshal_codec.go index c976d73a..812d07ea 100644 --- a/internal/vm/marshal_codec.go +++ b/internal/vm/marshal_codec.go @@ -259,24 +259,31 @@ func (d *mDumper) writeClassRef(c *RClass) { // writeTime emits Time's user-defined dump: the 8-byte packed form (MRI's // time_mdump), I-wrapped with the :zone / :offset ivars. -func (d *mDumper) writeTime(t *Time) { +// timeDumpBytes packs a Time into MRI's 8-byte marshal/_dump form (two +// little-endian uint32s: the date/UTC-flag word and the time/usec word). +func timeDumpBytes(t *Time) []byte { tt := t.t.UTC() year, mon, mday := tt.Date() hour, min, sec := tt.Clock() usec := tt.Nanosecond() / 1000 - utc := t.t.Location() == stdtime.UTC p := uint32(0x80000000) | uint32((year-1900)<<14) | uint32((int(mon)-1)<<10) | uint32(mday<<5) | uint32(hour) - if utc { + if t.t.Location() == stdtime.UTC { p |= 0x40000000 } s := uint32(min<<26) | uint32(sec<<20) | uint32(usec) var payload [8]byte encbinary.LittleEndian.PutUint32(payload[0:4], p) encbinary.LittleEndian.PutUint32(payload[4:8], s) + return payload[:] +} + +func (d *mDumper) writeTime(t *Time) { + utc := t.t.Location() == stdtime.UTC + payload := timeDumpBytes(t) d.buf = append(d.buf, 'I', 'u') d.writeSymbol("Time") - d.writeBytes(string(payload[:])) + d.writeBytes(string(payload)) if utc { d.writeLong(1) d.writeSymbol("zone") diff --git a/internal/vm/time.go b/internal/vm/time.go index 2f3b0543..c26cca3a 100644 --- a/internal/vm/time.go +++ b/internal/vm/time.go @@ -175,6 +175,21 @@ func (vm *VM) registerTime() { d := func(name string, fn NativeFn) { vm.cTime.define(name, fn) } self := func(v object.Value) *Time { return v.(*Time) } + // Time#_dump / Time._load are the private Marshal hooks: _dump packs the 8-byte + // form, _load rebuilds a Time from it. + d("_dump", func(_ *VM, v object.Value, _ []object.Value, _ *Proc) object.Value { + return object.NewStringBytesEnc(timeDumpBytes(self(v)), "ASCII-8BIT") + }) + vm.setInstanceVisibility(vm.cTime, "_dump", visPrivate) + vm.cTime.smethods["_load"] = &Method{name: "_load", owner: vm.cTime, native: func(_ *VM, _ object.Value, args []object.Value, _ *Proc) object.Value { + s, ok := args[0].(*object.String) + if !ok { + raise("TypeError", "no implicit conversion of %s into String", vm.classOf(args[0]).name) + } + return marshalLoadTime([]byte(s.Str())) + }} + vm.setClassMethodVisibility(vm.cTime, "_load", visPrivate) + d("to_i", func(_ *VM, v object.Value, _ []object.Value, _ *Proc) object.Value { return object.IntValue(self(v).t.Unix()) }) diff --git a/internal/vm/time_conformance_test.go b/internal/vm/time_conformance_test.go index 1deca175..938b9211 100644 --- a/internal/vm/time_conformance_test.go +++ b/internal/vm/time_conformance_test.go @@ -291,3 +291,20 @@ func TestTimeCalendarCoercion(t *testing.T) { } } } + +// TestTimeDumpLoad covers the private Marshal hooks Time#_dump / Time._load: the +// 8-byte ASCII-8BIT packed form, its round-trip, the privacy, and the +// non-String _load TypeError. Asserted against MRI Ruby 4.0.6. +func TestTimeDumpLoad(t *testing.T) { + for _, c := range []struct{ src, want string }{ + {`d = Time.utc(2020, 1, 2, 3, 4, 5).send(:_dump); p [d.class, d.encoding.to_s, d.bytesize]`, "[String, \"ASCII-8BIT\", 8]\n"}, + {`t = Time.utc(2021, 7, 8, 9, 10, 11); p Time.send(:_load, t.send(:_dump)) == t`, "true\n"}, + {`p [Time.private_instance_methods(false).include?(:_dump), Time.respond_to?(:_dump)]`, "[true, false]\n"}, + {`p Time.private_methods(false).include?(:_load)`, "true\n"}, + {`begin; Time.send(:_load, 42); rescue TypeError => e; p e.class; end`, "TypeError\n"}, + } { + if got := eval(t, c.src); got != c.want { + t.Errorf("src=%q got=%q want=%q", c.src, got, c.want) + } + } +} diff --git a/scripts/conformance/rubyspec/FLOOR b/scripts/conformance/rubyspec/FLOOR index 540bbc28..c2c6c1f0 100644 --- a/scripts/conformance/rubyspec/FLOOR +++ b/scripts/conformance/rubyspec/FLOOR @@ -1 +1 @@ -16285 +16290