Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Polyfill: Date.prototype.toTemporalInstant should not be a constructor #3040

Merged
merged 2 commits into from
Nov 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 14 additions & 3 deletions polyfill/lib/legacydate.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,18 @@ import { Instant } from './instant.mjs';

import bigInt from 'big-integer';

export function toTemporalInstant() {
const epochNanoseconds = bigInt(ES.Call(DatePrototypeValueOf, this, [])).multiply(1e6);
return new Instant(ES.BigIntIfAvailable(epochNanoseconds));
// By default, a plain function can be called as a constructor. A method such as
// Date.prototype.toTemporalInstant should not be able to. We could check
// new.target in the body of toTemporalInstant, but that is not sufficient for
// preventing construction when passing it as the newTarget parameter of
// Reflect.construct. So we create it as a method of an otherwise unused class,
// and monkeypatch it onto Date.prototype.

class LegacyDateImpl {
toTemporalInstant() {
const epochNanoseconds = bigInt(ES.Call(DatePrototypeValueOf, this, [])).multiply(1e6);
return new Instant(ES.BigIntIfAvailable(epochNanoseconds));
}
}

export const toTemporalInstant = LegacyDateImpl.prototype.toTemporalInstant;
3 changes: 3 additions & 0 deletions polyfill/test/expected-failures.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,6 @@ intl402/DateTimeFormat/proto-from-ctor-realm.js

# https://github.com/tc39/ecma402/issues/402
intl402/DateTimeFormat/prototype/resolvedOptions/hourCycle-default.js

# Fails until CLDR 46 (released 2024-10-24) makes its way into a Node.js release
staging/Intl402/Temporal/old/non-iso-calendars.js
2 changes: 1 addition & 1 deletion polyfill/test262
Submodule test262 updated 1082 files
Loading