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
5 changes: 3 additions & 2 deletions internal/vm/marshal.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
60 changes: 60 additions & 0 deletions internal/vm/marshal_codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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())
}
Expand Down Expand Up @@ -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':
Expand Down Expand Up @@ -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)
Expand Down
17 changes: 17 additions & 0 deletions internal/vm/marshal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"},
Expand Down
2 changes: 1 addition & 1 deletion scripts/conformance/rubyspec/FLOOR
Original file line number Diff line number Diff line change
@@ -1 +1 @@
15600
15680