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
15 changes: 11 additions & 4 deletions internal/vm/marshal_codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
15 changes: 15 additions & 0 deletions internal/vm/time.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
})
Expand Down
17 changes: 17 additions & 0 deletions internal/vm/time_conformance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
}
2 changes: 1 addition & 1 deletion scripts/conformance/rubyspec/FLOOR
Original file line number Diff line number Diff line change
@@ -1 +1 @@
16285
16290