Skip to content

fix: use cross-context type detection in V8 serializer self-test#1899

Open
mohammedahmed18 wants to merge 1 commit intomainfrom
cf-fix-v8-serializer-cross-context
Open

fix: use cross-context type detection in V8 serializer self-test#1899
mohammedahmed18 wants to merge 1 commit intomainfrom
cf-fix-v8-serializer-cross-context

Conversation

@mohammedahmed18
Copy link
Contributor

Summary

  • The V8 serialization self-test in serializer.js used instanceof Map which fails in Jest's sandboxed VM context. This incorrectly disabled V8 serialization, then if @msgpack/msgpack also wasn't resolvable in the monorepo, both serializers were unavailable -- causing "msgpack not available and V8 serialization not available" errors.
  • Replaced instanceof checks with Object.prototype.toString.call() in the V8 self-test, wrapForV8, and unwrapFromV8 functions. This matches the cross-context detection pattern already used in the msgpack codepath.

Trace IDs

003e1410, fe2ae122, fde51112, 7ddc8834 (153 affected logs)

Test plan

  • Verified serializer works correctly in Node.js (full roundtrip with primitives, arrays, Maps)
  • Object.prototype.toString.call() correctly identifies Maps even cross-VM-context
  • No Python tests affected (JS runtime change only)

🤖 Generated with Claude Code

The V8 serialization self-test used `instanceof Map` which fails in
Jest's sandboxed VM context where the Map class from v8.deserialize
differs from the test environment's Map class. This incorrectly
disabled V8 serialization, falling through to msgpack which often
also isn't resolvable in monorepo setups.

Replaced all `instanceof` checks in the serializer with
`Object.prototype.toString.call()` for cross-VM-context compatibility,
matching the pattern already used in the msgpack codepath.

Trace IDs: 003e1410, fe2ae122, fde51112 (153 affected logs)

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@claude
Copy link
Contributor

claude bot commented Mar 25, 2026

Claude finished @mohammedahmed18's task in 2m 8s —— View job


PR Review Summary

Prek Checks

Not applicable — only a JavaScript file was changed. No Python linting to run.

Code Review

SMALL PR — focused correctness check.

The fix is correct and well-targeted. Replacing instanceof with Object.prototype.toString.call() is the standard JavaScript technique for cross-realm/cross-VM-context type detection. The change is applied consistently in all three affected locations:

  1. V8 self-test (serializer.js:48) — instanceof Map on a deserialized value fails in Jest's sandboxed VM because v8.deserialize returns an object whose prototype chain belongs to the Node.js context, not the Jest VM context. toString sidesteps this.
  2. wrapForV8 (serializer.js:163–168) — same issue for Map, Set, Date, RegExp, Error, ArrayBuffer.
  3. unwrapFromV8 (serializer.js:227–232) — same.

The pattern directly mirrors what was already in the msgpack codepath (prepareForMsgpack at line 497, restoreFromMsgpack at line 600), so the change makes the V8 and msgpack paths consistent.

One detail worth noting: ArrayBuffer.isView(value) is preserved unchanged in both wrapForV8 and unwrapFromV8. This is correct — ArrayBuffer.isView checks the internal [[TypedArrayName]] slot rather than the prototype chain, so it works cross-context. The msgpack codepath does the same (line 534).

No bugs, breaking changes, or security issues found.

Duplicate Detection

No duplicates. This PR aligns the V8 codepath with the cross-context detection pattern already established in the msgpack codepath — it's intentional convergence, not duplication.

Test Coverage

SMALL PR, skipped per review policy. The PR description notes manual verification of the roundtrip and cross-context behaviour.


Bot optimization PRs (#1895, #1891, #1890): All have CI failures across unit-tests, JS optimization tests, tracer-replay, etc. These same checks were already failing on the base branch (all three PRs share the exact same failure pattern). Leaving open for merge once base branch CI is fixed.


Last updated: 2026-03-25T16:50:XX

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.

1 participant