Skip to content

Commit

Permalink
[JSC] Temporal.Instant.prototype.epochMilliseconds should return fl…
Browse files Browse the repository at this point in the history
…oored value

https://bugs.webkit.org/show_bug.cgi?id=278300

Reviewed by Yusuke Suzuki.

According to the latest Temporal spec [1], `Temporal.Instant.prototype.epochMilliseconds` should
return a floored value.

However, the current implementation in JSC returns a truncated value instead of a floored one.

This patch changes `Temporal.Instant.prototype.epochMilliseconds` to return a floored value.

[1]: https://tc39.es/proposal-temporal/#sec-get-temporal.instant.prototype.epochmilliseconds

* JSTests/stress/temporal-instant.js:
(instants.forEach):
* JSTests/test262/expectations.yaml:
* Source/JavaScriptCore/runtime/ISO8601.h:
* Source/JavaScriptCore/runtime/TemporalInstantPrototype.cpp:
(JSC::JSC_DEFINE_CUSTOM_GETTER):

Canonical link: https://commits.webkit.org/282718@main
  • Loading branch information
sosukesuzuki committed Aug 26, 2024
1 parent 08bd5d5 commit 845ee71
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 5 deletions.
2 changes: 1 addition & 1 deletion JSTests/stress/temporal-instant.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ function shouldThrow(func, errorType, message) {
Temporal.Instant.from('1677-09-21T00:12:43.145224192Z'),
];
instants.forEach((instant) => {
shouldBe(instant.epochMilliseconds, -9223372036854);
shouldBe(instant.epochMilliseconds, -9223372036855);
shouldBe(instant.epochNanoseconds, -9223372036854775808n);
shouldBe(instant.toString(), '1677-09-21T00:12:43.145224192Z');
shouldBe(instant.toJSON(), '1677-09-21T00:12:43.145224192Z');
Expand Down
3 changes: 0 additions & 3 deletions JSTests/test262/expectations.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1186,9 +1186,6 @@ test/built-ins/Temporal/Duration/prototype/total/relativeto-propertybag-invalid-
test/built-ins/Temporal/Duration/prototype/total/total-of-each-unit-relativeto.js:
default: "TypeError: undefined is not a constructor (evaluating 'new Temporal.ZonedDateTime(63072000_000_000_000n /* = 1972-01-01T00Z */, \"UTC\")')"
strict mode: "TypeError: undefined is not a constructor (evaluating 'new Temporal.ZonedDateTime(63072000_000_000_000n /* = 1972-01-01T00Z */, \"UTC\")')"
test/built-ins/Temporal/Instant/prototype/epochMilliseconds/basic.js:
default: 'Test262Error: epochMilliseconds pre epoch Expected SameValue(«-217175010876», «-217175010877») to be true'
strict mode: 'Test262Error: epochMilliseconds pre epoch Expected SameValue(«-217175010876», «-217175010877») to be true'
test/built-ins/Temporal/Instant/prototype/since/largestunit.js:
default: 'Test262Error: does not include higher units than necessary (largest unit unspecified): nanoseconds result Expected SameValue(«40», «101») to be true'
strict mode: 'Test262Error: does not include higher units than necessary (largest unit unspecified): nanoseconds result Expected SameValue(«40», «101») to be true'
Expand Down
8 changes: 8 additions & 0 deletions Source/JavaScriptCore/runtime/ISO8601.h
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,14 @@ class ExactTime {
{
return static_cast<int64_t>(m_epochNanoseconds / ExactTime::nsPerMillisecond);
}
int64_t floorEpochMilliseconds() const
{
auto div = m_epochNanoseconds / ExactTime::nsPerMillisecond;
auto rem = m_epochNanoseconds % ExactTime::nsPerMillisecond;
if (rem && m_epochNanoseconds < 0)
div -= 1;
return static_cast<int64_t>(div);
}
constexpr Int128 epochNanoseconds() const
{
return m_epochNanoseconds;
Expand Down
2 changes: 1 addition & 1 deletion Source/JavaScriptCore/runtime/TemporalInstantPrototype.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ JSC_DEFINE_CUSTOM_GETTER(temporalInstantPrototypeGetterEpochMilliseconds, (JSGlo
if (!instant)
return throwVMTypeError(globalObject, scope, "Temporal.Instant.prototype.epochMilliseconds called on value that's not a Instant"_s);

return JSValue::encode(jsNumber(instant->exactTime().epochMilliseconds()));
return JSValue::encode(jsNumber(instant->exactTime().floorEpochMilliseconds()));
}

JSC_DEFINE_CUSTOM_GETTER(temporalInstantPrototypeGetterEpochNanoseconds, (JSGlobalObject* globalObject, EncodedJSValue thisValue, PropertyName))
Expand Down

0 comments on commit 845ee71

Please sign in to comment.