Repository navigation
fix(codemode): honor locale and options in Date toLocale methods - #64938
Conversation
There was a problem hiding this comment.
This fixes the bug as described. On the base, with TZ=America/New_York, all of the description's examples print 1/15/2024, 1:05:09 PM while getHours() returns 8. With this PR they match Bun (10:05:09 PM for Tokyo, Jan 15, 15.1.2024, 13:05:09, 8:05:09 AM with no arguments). Getters, inherited options, hour12 given an object, hourCycle, fractionalSecondDigits and holes in the locale array all behave correctly. stdlib.test.ts passes (154 tests), and the toLocale tests also pass under TZ=Asia/Tokyo. tsc --noEmit is clean. bun run typecheck fails here only because this Bun version (1.4.2) has no bun check.
Public API: unchanged. Only the behaviour of Date methods inside Code Mode programs changes.
One edge case is worth fixing: locales isn't read the way Intl reads it (see the inline comment).
There was a problem hiding this comment.
The new commits fix both points from my last review. toLocaleString(null) now throws a Code Mode TypeError instead of the host's message, and non-array objects as locales are read as array-like lists: {} gives the default output and { length: 2, 1: "de-DE" } formats in German. I checked this with probe scripts through CodeMode.execute. stdlib.test.ts passes (154 tests) and tsc --noEmit is clean.
Public API: unchanged.
Nothing new to raise.
|
Security follow-up: locale lists over 100 entries (arrays and array-likes, checked before any element is read) and tags over 256 characters now throw a catchable |
There was a problem hiding this comment.
The new commit's locale caps work as described. I checked them through CodeMode.execute: 100 locales format; 101, in an array or an array-like, throw the RangeError; { length: 2 ** 32 } throws without reading any element; and a tag over 256 characters, including one built by toString, is rejected. Long timeZone/calendar/numberingSystem values fail quickly in the host, so limiting tags alone looks enough. stdlib.test.ts passes (155 tests) and tsc --noEmit is clean.
Public API: unchanged.
Nothing new to raise.
|
Removed the size caps from this PR at the maintainer's request. |
There was a problem hiding this comment.
Dropping the tag-length cap reopens a way to hang a run that timeoutMs can't stop: formatting with one long locale tag takes time that grows with the square of its length (see inline). new Date(0).toLocaleString("de-x-" + "abcdefgh-".repeat(200000) + "a") ran for 15.8 s under limits: { timeoutMs: 1000 } and then returned normally. A 3.6M-character tag takes 82 s in Bun, and Code Mode strings can be up to 16M characters. The new array-like cap at 10M is fine: { length: 2 ** 32 } throws Invalid array length and { length: 1e7 } finishes in about 0.7 s. stdlib.test.ts passes (154 tests) and tsc --noEmit is clean.
Public API: unchanged.
| ], | ||
| [...tags, ...entries.map((entry) => entry[1])], | ||
| (primitives) => | ||
| new Date(time)[name]( |
There was a problem hiding this comment.
With the tag-length check gone, a program-built tag reaches Bun's Intl without any limit, and Bun's tag parsing takes time that grows roughly with the square of the length. In Bun I measured 0.9M chars → 2.6 s, 1.8M → 15.9 s, 3.6M → 82 s, and Code Mode allows strings up to 16M characters. This runs as one host call that timeoutMs can't interrupt: a 1.8M-char tag ran 15.8 s under timeoutMs: 1000 and returned normally. Could the length check on string tags (the old MAX_LOCALE_LENGTH check, or any small bound) come back, even without the locale-count cap?
Summary
Date.prototype.toLocaleString,toLocaleDateStringandtoLocaleTimeStringignored their arguments and always printeden-USin UTC, sotimeZone,weekday,dateStyle,hour12, ... and the locale were silently dropped, and under a non-UTCTZthe output disagreed withgetHours()/toDateString(). They now delegate to the host'sIntl.DateTimeFormatwith the program's locale and options.1/15/2024, 10:05:09 PM1/15/2024, 1:05:09 PM1/15/2024, 10:05:09 PMJan 151/15/2024Jan 1515.1.2024, 13:05:091/15/2024, 1:05:09 PM15.1.2024, 13:05:091/15/2024, 8:05:09 AM1/15/2024, 1:05:09 PM1/15/2024, 8:05:09 AMNo-argument default. Bun formats with
en-USregardless ofLANG/LC_ALL, in the local timezone. The old pin to UTC was meant to keep output host-independent, butgetHours,toDateString,toTimeStringandgetTimezoneOffsetalready use the host's local time, so the pin only madetoLocale*contradict them. This keeps the locale pinned toen-US(Bun's default, and deterministic) and drops the UTC pin, so no-argument output matches Bun and the other local-time methods.Option objects are read in VM space: program getters run once (through
withReads) and object values are converted withToString/ToNumberbefore reaching the host;hour12usesToBoolean. Non-array objects as locales are read as array-like, as Intl does. Invalid input throws the host'sRangeError/TypeError(nulllocales or options throw a Code ModeTypeErrorso the host's message doesn't leak its own source). No globalIntlis added.Number.prototype.toLocaleStringis untouched.Testing
stdlib.test.tscases: locale/options (Tokyo, weekday, month/day, de-DE, en-GB, locale arrays, dateStyle/timeStyle, timeZoneName, hour12, invalid Date), no-argument output matching the host andgetHours, getter-once and object conversion, and Intl errors. All use explicit time zones or compare against the host, so they pass underTZ=UTC,Asia/TokyoandAmerica/New_York.bun testinpackages/codemode: 1789 pass, 0 fail.bun typecheck(codemode),bun run check(root).Visual