Skip to content

feat(isDate, isRegExp): support cross-realm#1568

Closed
haejunejung wants to merge 4 commits intotoss:mainfrom
haejunejung:feat/cross-realm
Closed

feat(isDate, isRegExp): support cross-realm#1568
haejunejung wants to merge 4 commits intotoss:mainfrom
haejunejung:feat/cross-realm

Conversation

@haejunejung
Copy link
Contributor

Support cross-realm

Problem

instanceof fails when checking objects created in different JavaScript realms (e.g., iframes, vm contexts). Each realm has its own set of built-in constructors, so value instanceof RegExp returns false for a RegExp created in another realm.

const vm = require('node:vm');
const context = vm.createContext({});
const regex = vm.runInContext('/test/', context);

regex instanceof RegExp;  // false
Object.prototype.toString.call(regex) === '[object RegExp]';  // true

date instanceof Date; // false
Object.prototype.toString.call(date) === '[object Date]'; // true

Solution

Replace instanceof with Object.prototype.toString.call(), which checks the object's internal [[Class]] slot and works reliably across realms.

Changes

  • Changed type checking to use Object.prototype.toString.call() instead of instanceof
  • Added cross-realm tests using node:vm

Reference

Copilot AI review requested due to automatic review settings January 2, 2026 13:47
@vercel
Copy link

vercel bot commented Jan 2, 2026

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Review Updated (UTC)
es-toolkit Ready Ready Preview, Comment Jan 2, 2026 1:58pm

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR adds cross-realm support to isDate and isRegExp predicates by replacing instanceof checks with Object.prototype.toString.call(), which works reliably across different JavaScript realms (e.g., iframes, VM contexts).

Key Changes:

  • Replaced instanceof checks with Object.prototype.toString.call() for realm-independent type checking
  • Added isObject guard for performance optimization before the toString check
  • Added cross-realm tests using node:vm module

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated no comments.

File Description
src/predicate/isRegExp.ts Updated implementation to use Object.prototype.toString.call() instead of instanceof RegExp
src/predicate/isRegExp.spec.ts Added cross-realm test using runInNewContext
src/predicate/isDate.ts Updated implementation to use Object.prototype.toString.call() instead of instanceof Date
src/predicate/isDate.spec.ts Added cross-realm test using runInNewContext

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@codecov-commenter
Copy link

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 99.97%. Comparing base (6d278b4) to head (7bc7002).

Additional details and impacted files

Impacted file tree graph

@@           Coverage Diff           @@
##             main    #1568   +/-   ##
=======================================
  Coverage   99.97%   99.97%           
=======================================
  Files         495      495           
  Lines        4648     4648           
  Branches     1340     1342    +2     
=======================================
  Hits         4647     4647           
  Misses          1        1           
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@raon0211
Copy link
Collaborator

Hey, thanks for the PR! We're not sure cross-realm support is a common enough use case for us to prioritize. es-toolkit aims to keep implementations simple and cover ~85% of real-world scenarios—edge cases aren't really our focus (more on that in our contributing guide).

For checking RegExp or Date values, we think instanceof is the more straightforward approach, so we'll stick with what we have.

Appreciate the contribution though—hope to see more from you!

@raon0211 raon0211 closed this Jan 15, 2026
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.

3 participants