Skip to content

Commit

Permalink
Polyfill: Make Date.prototype.toTemporalInstant not a constructor
Browse files Browse the repository at this point in the history
A test for this was recently added to test262. Per the spec text,
Date.prototype.toTemporalInstant should not be contstructible. However,
the previous implementation did allow it to be called as a constructor.
  • Loading branch information
ptomato authored and Ms2ger committed Nov 11, 2024
1 parent 897604e commit b17d1a8
Showing 1 changed file with 14 additions and 3 deletions.
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;

0 comments on commit b17d1a8

Please sign in to comment.