Skip to content

Commit

Permalink
Add Float objects to object links in Marshal.dump
Browse files Browse the repository at this point in the history
  • Loading branch information
herwinw committed Feb 1, 2025
1 parent 2d6c8a7 commit fa65bbd
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 4 deletions.
6 changes: 2 additions & 4 deletions spec/core/marshal/dump_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -313,9 +313,7 @@ def _dump(level)
end

it "uses object links for objects repeatedly dumped" do
NATFIXME 'it uses object links for objects repeatedly dumped', exception: SpecFailedException do
Marshal.dump([0.0, 0.0]).should == "\x04\b[\af\x060@\x06" # @\x06 is a link to the float value
end
Marshal.dump([0.0, 0.0]).should == "\x04\b[\af\x060@\x06" # @\x06 is a link to the float value
end
end

Expand Down Expand Up @@ -851,7 +849,7 @@ class << obj

it "dumps a BasicObject subclass if it defines respond_to?" do
obj = MarshalSpec::BasicObjectSubWithRespondToFalse.new
NATFIXME 'it dumps a BasicObject subclass if it defines respond_to?', exception: NoMethodError, message: /undefined method [`']nil\?' for an instance of MarshalSpec::BasicObjectSubWithRespondToFalse/ do
NATFIXME 'it dumps a BasicObject subclass if it defines respond_to?', exception: NoMethodError, message: /undefined method [`']is_a\?' for an instance of MarshalSpec::BasicObjectSubWithRespondToFalse/ do
Marshal.dump(obj).should == "\x04\bo:2MarshalSpec::BasicObjectSubWithRespondToFalse\x00"
end
end
Expand Down
5 changes: 5 additions & 0 deletions src/marshal.rb
Original file line number Diff line number Diff line change
Expand Up @@ -336,10 +336,15 @@ def write(value)
if value.respond_to?(:object_id) && @object_lookup.key?(value.object_id)
write_object_link(@object_lookup.fetch(value.object_id))
return @output
elsif value.is_a?(Float) && @object_lookup.key?(value)
write_object_link(@object_lookup.fetch(value))
return @output
end

if !value.nil? && !value.is_a?(TrueClass) && !value.is_a?(FalseClass) && !value.is_a?(Integer) && !value.is_a?(Float) && !value.is_a?(Symbol)
@object_lookup[value.object_id] = @object_lookup.size
elsif value.is_a?(Float)
@object_lookup[value] = @object_lookup.size
end

ivars = value.instance_variables.map { |ivar_name| [ivar_name, value.instance_variable_get(ivar_name)] }
Expand Down

0 comments on commit fa65bbd

Please sign in to comment.