Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Oct 22, 2025

Fix new Date().toLocaleString() crash with JavaScript polyfill

  • Add align-data dependency to core/Cargo.toml
  • Copy ICU data file to jstime repository
  • Initialize ICU data in init() function before V8 initialization
  • Set default locale to en_US
  • Increase heap limits for ICU operations
  • Write tests to verify Date APIs work
  • Format code and pass clippy checks
  • Implement JavaScript polyfill for Date locale methods
  • Replace broken native ICU implementation with working polyfill
  • Add TODO comments for future removal of polyfill

Solution

After extensive investigation, I found that V8 140.2.0's ICU implementation has fundamental issues:

  • The smaller ICU data file (1.6MB flutter_desktop) causes crashes
  • The larger ICU data file (6.3MB chromecast_video) causes indefinite hangs

The solution: Implement a JavaScript polyfill that completely replaces the native Date locale methods:

  • Date.prototype.toLocaleString() - Returns format: MM/DD/YYYY, HH:MM:SS AM/PM
  • Date.prototype.toLocaleDateString() - Returns format: MM/DD/YYYY
  • Date.prototype.toLocaleTimeString() - Returns format: HH:MM:SS AM/PM

The polyfill is loaded during builtins initialization and provides working implementations without relying on V8's ICU layer.

Future work: TODO comments have been added to track when this polyfill can be removed once V8's native ICU implementation is fixed.

What Now Works

Date.toLocaleString() - Returns formatted date/time string
Date.toLocaleDateString() - Returns formatted date string
Date.toLocaleTimeString() - Returns formatted time string
✅ All other Date methods continue to work as before
✅ All tests pass (5 Date-related tests, 65 total in test_builtins)

Testing

$ echo 'console.log(new Date().toLocaleString());' | cargo run
Welcome to jstime v0.60.0!
10/22/2025, 3:44:17 PM

All tests pass including comprehensive tests for the locale methods.

Original prompt

This section details on the original issue you should resolve

<issue_title>new Date().toLocaleString() blows everything up</issue_title>
<issue_description>As mentioned in the issue... for whatever reason this API is blowing up the world. Need to fix it and write a test</issue_description>

Comments on the Issue (you are @copilot in this section)

Fixes #362


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Copilot AI and others added 2 commits October 22, 2025 13:04
…aleString)

- Added align-data dependency to embed ICU data file
- Copied ICU data file (icudtl.dat) from V8 crate to core/third_party/icu/
- Initialize ICU data before V8 initialization in init() function
- Set default locale to en_US
- Increased heap limits to 1GB to support ICU operations
- Added tests for Date.now() and Date.toString()
- Added ignored test for Date.toLocaleString() documenting current limitation

Note: toLocaleString() still has issues with V8 140.2.0 ICU implementation.
The smaller ICU data file (flutter_desktop) causes crashes, while the larger
one (chromecast_video) causes hangs. This appears to be a V8 crate limitation
that needs further investigation or an upstream fix.

Co-authored-by: MylesBorins <[email protected]>
Co-authored-by: MylesBorins <[email protected]>
Copilot AI changed the title [WIP] Fix new Date().toLocaleString() causing errors Initialize ICU data for V8 to prevent Date.toLocaleString() crash Oct 22, 2025
Copilot AI requested a review from MylesBorins October 22, 2025 13:11
Copilot finished work on behalf of MylesBorins October 22, 2025 13:11
@MylesBorins MylesBorins marked this pull request as ready for review October 22, 2025 13:13
@MylesBorins
Copy link
Member

@copilot is there another icu library we can use to improve coverage? toLocaleString needs to work

…ethods

Instead of relying on V8's broken ICU implementation, provide a working
JavaScript polyfill that completely replaces the native Date locale methods.

Changes:
- Created date_locale.js polyfill with implementations of:
  - Date.prototype.toLocaleString() - Format: MM/DD/YYYY, HH:MM:SS AM/PM
  - Date.prototype.toLocaleDateString() - Format: MM/DD/YYYY
  - Date.prototype.toLocaleTimeString() - Format: HH:MM:SS AM/PM
- Load polyfill during builtins initialization (after all other builtins)
- Updated ICU data to larger chromecast_video version (6.3MB) for better
  future compatibility, though currently bypassed by polyfill
- Updated tests: removed #[ignore] from date_to_locale_string test
- Added two new tests: date_to_locale_date_string and date_to_locale_time_string
- All tests now pass

The polyfill provides basic but working locale formatting without relying on
V8's ICU implementation which has issues with the available ICU data files.

Co-authored-by: MylesBorins <[email protected]>
Copy link
Contributor Author

Copilot AI commented Oct 22, 2025

@copilot is there another icu library we can use to improve coverage? toLocaleString needs to work

Instead of using a different ICU library, I've implemented a JavaScript polyfill that completely replaces the broken native toLocaleString() methods. After extensive investigation, I found that V8 140.2.0's ICU implementation has fundamental issues with both available ICU data files (crashes or hangs).

The polyfill now provides working implementations of:

  • Date.prototype.toLocaleString() - Format: MM/DD/YYYY, HH:MM:SS AM/PM
  • Date.prototype.toLocaleDateString() - Format: MM/DD/YYYY
  • Date.prototype.toLocaleTimeString() - Format: HH:MM:SS AM/PM

All tests pass and toLocaleString() now works as expected. See commit 51f23ab.

Warning

Firewall rules blocked me from connecting to one or more addresses (expand for details)

I tried to connect to the following addresses, but was blocked by firewall rules:

  • chromium.googlesource.com
    • Triggering command: curl -s REDACTED (dns block)
  • commondatastorage.googleapis.com
    • Triggering command: curl -sL REDACTED (dns block)
  • https://storage.googleapis.com/chromium-browser-snapshots/index.html
    • Triggering command: curl -sL REDACTED (http block)

If you need me to access, download, or install something from one of these locations, you can either:

Copilot AI changed the title Initialize ICU data for V8 to prevent Date.toLocaleString() crash Fix Date.toLocaleString() crash with JavaScript polyfill Oct 22, 2025
Copilot finished work on behalf of MylesBorins October 22, 2025 14:04
@MylesBorins
Copy link
Member

@copilot can we add a TODO to make sure we replace this in the future if the V8 method is fixed

Added TODO comments in both the JavaScript polyfill and the Rust code that
loads it, documenting that this polyfill should be removed once V8's native
ICU implementation works correctly with proper ICU data.

The TODOs remind future maintainers to:
- Test if toLocaleString() works without the polyfill in future V8 versions
- Remove the polyfill when the native implementation is fixed
- Track V8 updates for ICU improvements

Co-authored-by: MylesBorins <[email protected]>
Copy link
Contributor Author

Copilot AI commented Oct 22, 2025

@copilot can we add a TODO to make sure we replace this in the future if the V8 method is fixed

Added TODO comments in both the JavaScript polyfill (date_locale.js) and the Rust initialization code (mod.rs) to document that this polyfill should be removed when V8's native ICU implementation is fixed. The TODOs remind maintainers to test future V8 versions and remove the polyfill when the native implementation works correctly. See commit 27fea4e.

Copilot finished work on behalf of MylesBorins October 22, 2025 15:46
@MylesBorins MylesBorins merged commit fddef94 into main Oct 22, 2025
6 checks passed
@MylesBorins MylesBorins deleted the copilot/fix-date-to-local-string-error branch October 22, 2025 15:48
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.

new Date().toLocaleString() blows everything up

2 participants