Commit c232d789 authored by Frank Tang's avatar Frank Tang Committed by V8 LUCI CQ

[Temporal] Add Duration.prototype.(abs|negated)

Also add AO: CreateNegatedTemporalDuration

Spec Text:
https://tc39.es/proposal-temporal/#sec-temporal.duration.prototype.abs
https://tc39.es/proposal-temporal/#sec-temporal.duration.prototype.negated
https://tc39.es/proposal-temporal/#sec-temporal-createnegatedtemporalduration

Bug: v8:11544
Change-Id: Ie522a7446f40c946c30f2e90c5f6c7fbc96c41eb
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/3380101Reviewed-by: 's avatarAdam Klein <adamk@chromium.org>
Commit-Queue: Frank Tang <ftang@chromium.org>
Cr-Commit-Position: refs/heads/main@{#79633}
parent 89ed081c
......@@ -208,10 +208,6 @@ TO_BE_IMPLEMENTED(TemporalDurationFrom)
TO_BE_IMPLEMENTED(TemporalDurationCompare)
/* Temporal #sec-temporal.duration.prototype.with */
TO_BE_IMPLEMENTED(TemporalDurationPrototypeWith)
/* Temporal #sec-temporal.duration.prototype.negated */
TO_BE_IMPLEMENTED(TemporalDurationPrototypeNegated)
/* Temporal #sec-temporal.duration.prototype.abs */
TO_BE_IMPLEMENTED(TemporalDurationPrototypeAbs)
/* Temporal #sec-temporal.duration.prototype.add */
TO_BE_IMPLEMENTED(TemporalDurationPrototypeAdd)
/* Temporal #sec-temporal.duration.prototype.subtract */
......@@ -785,6 +781,8 @@ TEMPORAL_GET(Duration, Microseconds, microseconds)
TEMPORAL_GET(Duration, Nanoseconds, nanoseconds)
TEMPORAL_PROTOTYPE_METHOD0(Duration, Sign, sign)
TEMPORAL_PROTOTYPE_METHOD0(Duration, Blank, blank)
TEMPORAL_PROTOTYPE_METHOD0(Duration, Negated, negated)
TEMPORAL_PROTOTYPE_METHOD0(Duration, Abs, abs)
TEMPORAL_VALUE_OF(Duration)
// Instant
......
......@@ -4701,6 +4701,66 @@ MaybeHandle<Oddball> JSTemporalDuration::Blank(
: isolate->factory()->false_value();
}
namespace {
// #sec-temporal-createnegatedtemporalduration
MaybeHandle<JSTemporalDuration> CreateNegatedTemporalDuration(
Isolate* isolate, Handle<JSTemporalDuration> duration) {
TEMPORAL_ENTER_FUNC();
// 1. Assert: Type(duration) is Object.
// 2. Assert: duration has an [[InitializedTemporalDuration]] internal slot.
// 3. Return ! CreateTemporalDuration(−duration.[[Years]],
// −duration.[[Months]], −duration.[[Weeks]], −duration.[[Days]],
// −duration.[[Hours]], −duration.[[Minutes]], −duration.[[Seconds]],
// −duration.[[Milliseconds]], −duration.[[Microseconds]],
// −duration.[[Nanoseconds]]).
return CreateTemporalDuration(
isolate, -NumberToInt64(duration->years()),
-NumberToInt64(duration->months()), -NumberToInt64(duration->weeks()),
-NumberToInt64(duration->days()), -NumberToInt64(duration->hours()),
-NumberToInt64(duration->minutes()), -NumberToInt64(duration->seconds()),
-NumberToInt64(duration->milliseconds()),
-NumberToInt64(duration->microseconds()),
-NumberToInt64(duration->nanoseconds()));
}
} // namespace
// #sec-temporal.duration.prototype.negated
MaybeHandle<JSTemporalDuration> JSTemporalDuration::Negated(
Isolate* isolate, Handle<JSTemporalDuration> duration) {
// Let duration be the this value.
// 2. Perform ? RequireInternalSlot(duration,
// [[InitializedTemporalDuration]]).
// 3. Return ! CreateNegatedTemporalDuration(duration).
return CreateNegatedTemporalDuration(isolate, duration);
}
// #sec-temporal.duration.prototype.abs
MaybeHandle<JSTemporalDuration> JSTemporalDuration::Abs(
Isolate* isolate, Handle<JSTemporalDuration> duration) {
// 1. Let duration be the this value.
// 2. Perform ? RequireInternalSlot(duration,
// [[InitializedTemporalDuration]]).
// 3. Return ? CreateTemporalDuration(abs(duration.[[Years]]),
// abs(duration.[[Months]]), abs(duration.[[Weeks]]), abs(duration.[[Days]]),
// abs(duration.[[Hours]]), abs(duration.[[Minutes]]),
// abs(duration.[[Seconds]]), abs(duration.[[Milliseconds]]),
// abs(duration.[[Microseconds]]), abs(duration.[[Nanoseconds]])).
return CreateTemporalDuration(
isolate, std::abs(NumberToInt64(duration->years())),
std::abs(NumberToInt64(duration->months())),
std::abs(NumberToInt64(duration->weeks())),
std::abs(NumberToInt64(duration->days())),
std::abs(NumberToInt64(duration->hours())),
std::abs(NumberToInt64(duration->minutes())),
std::abs(NumberToInt64(duration->seconds())),
std::abs(NumberToInt64(duration->milliseconds())),
std::abs(NumberToInt64(duration->microseconds())),
std::abs(NumberToInt64(duration->nanoseconds())));
}
// #sec-temporal.calendar
MaybeHandle<JSTemporalCalendar> JSTemporalCalendar::Constructor(
Isolate* isolate, Handle<JSFunction> target, Handle<HeapObject> new_target,
......
......@@ -86,6 +86,14 @@ class JSTemporalDuration
V8_WARN_UNUSED_RESULT static MaybeHandle<Oddball> Blank(
Isolate* isolate, Handle<JSTemporalDuration> duration);
// #sec-temporal.duration.prototype.negated
V8_WARN_UNUSED_RESULT static MaybeHandle<JSTemporalDuration> Negated(
Isolate* isolate, Handle<JSTemporalDuration> duration);
// #sec-temporal.duration.prototype.abs
V8_WARN_UNUSED_RESULT static MaybeHandle<JSTemporalDuration> Abs(
Isolate* isolate, Handle<JSTemporalDuration> duration);
DECL_PRINTER(JSTemporalDuration)
TQ_OBJECT_CONSTRUCTORS(JSTemporalDuration)
......
......@@ -63,10 +63,8 @@
'temporal/calendar-week-of-year': [FAIL],
'temporal/calendar-year': [FAIL],
'temporal/calendar-year-month-from-fields': [FAIL],
'temporal/duration-abs': [FAIL],
'temporal/duration-add': [FAIL],
'temporal/duration-from': [FAIL],
'temporal/duration-negated': [FAIL],
'temporal/duration-to-json': [FAIL],
'temporal/duration-with': [FAIL],
'temporal/instant-add': [FAIL],
......
......@@ -6,7 +6,7 @@
d8.file.execute('test/mjsunit/temporal/temporal-helpers.js');
let d1 = new Temporal.Duration();
assertDuration(d1.negated(), -0, -0, -0, -0, -0, -0, -0, -0, -0, -0, 0, true);
assertDuration(d1.negated(), 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, true);
let d2 = new Temporal.Duration(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
assertDuration(d2.negated(), -1, -2, -3, -4, -5, -6, -7, -8, -9, -10, -1, false);
......
......@@ -312,7 +312,6 @@
'built-ins/Temporal/PlainDate/prototype/since/smallestunit-wrong-type': [FAIL],
'built-ins/Temporal/Duration/prototype/add/argument-string-negative-fractional-units': [FAIL],
'built-ins/Temporal/Duration/prototype/add/balance-negative-result': [FAIL],
'built-ins/Temporal/Duration/prototype/negated/subclassing-ignored': [FAIL],
'built-ins/Temporal/TimeZone/from/argument-valid': [SKIP],
......@@ -687,8 +686,6 @@
'built-ins/Temporal/Duration/from/order-of-operations': [FAIL],
'built-ins/Temporal/Duration/from/string-with-skipped-units': [FAIL],
'built-ins/Temporal/Duration/from/subclassing-ignored': [FAIL],
'built-ins/Temporal/Duration/prototype/abs/branding': [FAIL],
'built-ins/Temporal/Duration/prototype/abs/subclassing-ignored': [FAIL],
'built-ins/Temporal/Duration/prototype/add/branding': [FAIL],
'built-ins/Temporal/Duration/prototype/add/calendar-dateadd-called-with-plaindate-instance': [FAIL],
'built-ins/Temporal/Duration/prototype/add/infinity-throws-rangeerror': [FAIL],
......@@ -708,7 +705,6 @@
'built-ins/Temporal/Duration/prototype/add/relativeto-zoneddatetime-timezone-getoffsetnanosecondsfor-out-of-range': [FAIL],
'built-ins/Temporal/Duration/prototype/add/relativeto-zoneddatetime-timezone-getoffsetnanosecondsfor-wrong-type': [FAIL],
'built-ins/Temporal/Duration/prototype/add/subclassing-ignored': [FAIL],
'built-ins/Temporal/Duration/prototype/negated/branding': [FAIL],
'built-ins/Temporal/Duration/prototype/round/branding': [FAIL],
'built-ins/Temporal/Duration/prototype/round/calendar-dateadd-called-with-plaindate-instance': [FAIL],
'built-ins/Temporal/Duration/prototype/round/dateuntil-field': [FAIL],
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment