Skip to content

Conversation

@benjaminwood
Copy link
Contributor

Warning

C is not my area of expertise. Vibe code content warning 😅 - please review as such! Thank you!

This fixes a bug where separate Ruby objects with identical content were incorrectly sharing the same JavaScript object reference during serialization.

Problem:

  • Ruby hashes like [{"value" => 42}, {"value" => 42}, {"value" => 42}] were becoming the same JavaScript object reference
  • Mutating one object affected all identical objects
  • This happened because serialization used Ruby's content-based hash equality instead of object identity for reference tracking

Solution:

  • Modified serialize1() in mini_racer_extension.c to use rb_obj_id(v) as the hash key instead of the object itself (v)
  • This ensures only truly identical Ruby objects (same memory address) share JavaScript references, preserving circular reference handling
  • Separate Ruby objects now correctly become separate JavaScript objects

Changes:

  • ext/mini_racer_extension/mini_racer_extension.c: Use object identity for refs
  • test/test_object_identity_fix.rb: Comprehensive regression tests

Tests:

  • New regression tests verify both separate and shared object scenarios
  • Validates fix for nested objects and circular references

This fixes a bug where separate Ruby objects with identical content were
incorrectly sharing the same JavaScript object reference during serialization.

Problem:
- Ruby hashes like [{"value" => 42}, {"value" => 42}, {"value" => 42}]
  were becoming the same JavaScript object reference
- Mutating one object affected all identical objects
- This happened because serialization used Ruby's content-based hash equality
  instead of object identity for reference tracking

Solution:
- Modified serialize1() in mini_racer_extension.c to use rb_obj_id(v) as the
  hash key instead of the object itself (v)
- This ensures only truly identical Ruby objects (same memory address) share
  JavaScript references, preserving circular reference handling
- Separate Ruby objects now correctly become separate JavaScript objects

Changes:
- ext/mini_racer_extension/mini_racer_extension.c: Use object identity for refs
- test/test_object_identity_fix.rb: Comprehensive regression tests

Tests:
- All existing tests pass (97 runs, 171 assertions, 0 failures)
- New regression tests verify both separate and shared object scenarios
- Validates fix for nested objects and circular references
@SamSaffron
Copy link
Collaborator

Thanks so much, will leave @bnoordhuis to have a review, love the tests.

@benjaminwood
Copy link
Contributor Author

love the tests

When vibe coding OSS, the least you can do is write some tests 😄

(Hey, that rhymed!)

@bnoordhuis
Copy link
Collaborator

Expectation management: I'm on vacation and won't be back for another ~1.5 week.

Copy link
Collaborator

@bnoordhuis bnoordhuis left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks.

@SamSaffron SamSaffron merged commit 715a9b4 into rubyjs:main Aug 17, 2025
28 of 32 checks passed
@ekzobrain
Copy link

Hi. Could you, please, make a new release with this fix?

@madwire
Copy link

madwire commented Oct 2, 2025

I second that, we are running into this issue at the moment

@SamSaffron
Copy link
Collaborator

sure will push a new release today

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants