From c56bd6a6435ddd69408d3d7b96ecdde6b0f31923 Mon Sep 17 00:00:00 2001 From: tannevaled Date: Wed, 26 Aug 2026 16:46:00 +0200 Subject: [PATCH] Marshal: BINARY result, and the 'C' (builtin subclass) and 'e' (extended) codes Bring Marshal to MRI byte fidelity on three fronts: - Marshal.dump now returns an ASCII-8BIT (BINARY) string, as MRI does. - Instances of a user subclass of a built-in value type (Array/Hash/String/ Range/...) dump and load as the 'C' container: the class name followed by the wrapped built-in value, with any instance variables carried by an outer 'I' wrapper. The dump- and load-side link ids stay aligned so shared references round-trip. - A singleton-extended object dumps and loads as the 'e' container, one per extending module (last-extended first, matching MRI's ancestry order); extending with an anonymous module raises TypeError, as MRI does. Verified byte-for-byte against ruby 4.0.6. Whole-package coverage stays at 100%; the ruby/spec floor rises 15600 -> 15680 (language+core now 15735 passing, +75). Co-Authored-By: Claude Opus 4.8 --- internal/vm/marshal.go | 5 ++- internal/vm/marshal_codec.go | 60 ++++++++++++++++++++++++++++++ internal/vm/marshal_test.go | 17 +++++++++ scripts/conformance/rubyspec/FLOOR | 2 +- 4 files changed, 81 insertions(+), 3 deletions(-) diff --git a/internal/vm/marshal.go b/internal/vm/marshal.go index 99bcb5bb..3d47fb1c 100644 --- a/internal/vm/marshal.go +++ b/internal/vm/marshal.go @@ -30,11 +30,12 @@ func (vm *VM) registerMarshal() { raise("ArgumentError", "wrong number of arguments (given 0, expected 1..3)") } data := vm.marshalDump(args[0]) + // Marshal.dump always returns an ASCII-8BIT (BINARY) string. if io := marshalDumpIO(vm, args); io != nil { - vm.send(io, "write", []object.Value{object.NewStringBytes(data)}, nil) + vm.send(io, "write", []object.Value{object.NewStringBytesEnc(data, "ASCII-8BIT")}, nil) return io } - return object.NewStringBytes(data) + return object.NewStringBytesEnc(data, "ASCII-8BIT") }) // Marshal.load(source, proc=nil, freeze:) / Marshal.restore: deserialize from diff --git a/internal/vm/marshal_codec.go b/internal/vm/marshal_codec.go index 2d1a40ec..c976d73a 100644 --- a/internal/vm/marshal_codec.go +++ b/internal/vm/marshal_codec.go @@ -297,6 +297,18 @@ func (d *mDumper) writeObject(o *RObject) { if d.link(o) { return } + // An object singleton-extended by one or more modules is prefixed with an 'e' + // container per module (last-extended first, matching MRI's ancestry order). + if o.singleton != nil { + inc := o.singleton.includes + for i := len(inc) - 1; i >= 0; i-- { + if inc[i].name == "" { + raise("TypeError", "can't dump anonymous class %s", inc[i].ToS()) + } + d.buf = append(d.buf, 'e') + d.writeSymbol(inc[i].name) + } + } if d.vm.respondsTo(o, "marshal_dump") { val := d.vm.send(o, "marshal_dump", nil, nil) d.emitUserMarshal(o.class.name, val) @@ -323,6 +335,27 @@ func (d *mDumper) writeObject(o *RObject) { } return } + // An instance of a user subclass of a built-in value type (Array/Hash/ + // String/Range/...) dumps as 'C' — the class name followed by the wrapped + // built-in value. Any instance variables are carried by an outer 'I' wrapper, + // as MRI does for objects whose payload type has no inline ivar slot. + if o.builtin != nil { + names := o.liveIvarNames() + if len(names) > 0 { + d.buf = append(d.buf, 'I') + } + d.buf = append(d.buf, 'C') + d.writeSymbol(o.class.name) + d.writeValue(o.builtin) + if len(names) > 0 { + d.writeLong(len(names)) + for _, name := range names { + d.writeSymbol(name) + d.writeValue(o.ivars[name]) + } + } + return + } if o.class.name == "" { raise("TypeError", "can't dump anonymous class %s", o.class.ToS()) } @@ -523,6 +556,10 @@ func (r *mReader) readObject() (object.Value, bool) { return r.freezeValue(r.readHash(tag == '}')), true case 'o': return r.freezeValue(r.readObj()), true + case 'e': + return r.freezeValue(r.readExtended()), true + case 'C': + return r.freezeValue(r.readSubclass()), true case 'S': return r.freezeValue(r.readStruct()), true case 'c': @@ -657,6 +694,29 @@ func (r *mReader) readObj() object.Value { return o } +// readExtended reads the 'e' container: a module name followed by the object +// that was singleton-extended with it. The base is re-extended on load. 'e' +// carries no link id of its own — the wrapped object is what gets registered. +func (r *mReader) readExtended() object.Value { + modName := r.readSymbol() + mod := r.vm.marshalClass(modName) + base, _ := r.readObject() + r.vm.send(base, "extend", []object.Value{mod}, nil) + return base +} + +// readSubclass reads the 'C' container: a user subclass of a built-in value +// type, whose wrapped built-in value follows the class name. The outer object +// is registered before the payload so the link ids match the dump side. +func (r *mReader) readSubclass() object.Value { + className := r.readSymbol() + cls := r.vm.marshalClass(className) + o := &RObject{class: cls, ivars: map[string]object.Value{}} + r.register(o) + o.builtin = r.readValue() + return o +} + func (r *mReader) readRange() object.Value { rng := &object.Range{} r.register(rng) diff --git a/internal/vm/marshal_test.go b/internal/vm/marshal_test.go index b7fb59bb..673073ae 100644 --- a/internal/vm/marshal_test.go +++ b/internal/vm/marshal_test.go @@ -137,6 +137,23 @@ func TestMarshal(t *testing.T) { {`p Marshal.dump(5, nil).bytes`, "[4, 8, 105, 10]\n"}, {`p Marshal.dump(5, Object.new).bytes`, "[4, 8, 105, 10]\n"}, + // --- Marshal.dump always returns an ASCII-8BIT (BINARY) string ----- + {`p Marshal.dump(42).encoding.name`, "\"ASCII-8BIT\"\n"}, + {`p Marshal.dump("hi").encoding.name`, "\"ASCII-8BIT\"\n"}, + + // --- 'C': instances of a user subclass of a built-in value type ---- + {`class UA < Array; end; p Marshal.dump(UA.new([1, 2])).bytes`, "[4, 8, 67, 58, 7, 85, 65, 91, 7, 105, 6, 105, 7]\n"}, + {`class UH < Hash; end; p Marshal.dump(UH.new).bytes`, "[4, 8, 67, 58, 7, 85, 72, 123, 0]\n"}, + {`class UA2 < Array; end; a = UA2.new([1]); a.instance_variable_set(:@x, 7); p Marshal.dump(a).bytes`, "[4, 8, 73, 67, 58, 8, 85, 65, 50, 91, 6, 105, 6, 6, 58, 7, 64, 120, 105, 12]\n"}, + {`class UA3 < Array; end; c = Marshal.load(Marshal.dump(UA3.new([4, 5]))); p [c.class.name, c.to_a]`, "[\"UA3\", [4, 5]]\n"}, + {`class UA4 < Array; end; a = UA4.new([1]); a.instance_variable_set(:@y, 9); c = Marshal.load(Marshal.dump(a)); p c.instance_variable_get(:@y)`, "9\n"}, + {`class UA5 < Array; end; o = UA5.new; c = Marshal.load(Marshal.dump([o, o])); p c[0].equal?(c[1])`, "true\n"}, + + // --- 'e': a singleton-extended object ------------------------------ + {`module ME; end; o = Object.new; o.extend(ME); p Marshal.dump(o).bytes`, "[4, 8, 101, 58, 7, 77, 69, 111, 58, 11, 79, 98, 106, 101, 99, 116, 0]\n"}, + {`module ME2; def tag; :ok; end; end; o = Object.new; o.extend(ME2); c = Marshal.load(Marshal.dump(o)); p c.tag`, ":ok\n"}, + {`o = Object.new; o.extend(Module.new); begin; Marshal.dump(o); rescue TypeError => e; p e.message.start_with?("can't dump anonymous"); end`, "true\n"}, + // --- version constants + restore alias ----------------------------- {`p Marshal::MAJOR_VERSION`, "4\n"}, {`p Marshal::MINOR_VERSION`, "8\n"}, diff --git a/scripts/conformance/rubyspec/FLOOR b/scripts/conformance/rubyspec/FLOOR index 8dc09e21..2a6673cc 100644 --- a/scripts/conformance/rubyspec/FLOOR +++ b/scripts/conformance/rubyspec/FLOOR @@ -1 +1 @@ -15600 +15680