Bug 1840374 - Part 27: Add CopyOptions. r=spidermonkey-reviewers,sfink

From <https://github.com/tc39/proposal-temporal/pull/2562>.

Will be replaced with `SnapshotOwnProperties` in a later patch.

Depends on D182046

Differential Revision: https://phabricator.services.mozilla.com/D182047
This commit is contained in:
André Bargull committed 2023-07-17 20:10:25 +00:00
1 parent c749ac3802
commit 14579630fd
8 files changed
+90 -109

No files matched your search

+5 -11
View File
@@ -1033,7 +1033,7 @@ static bool DifferenceTemporalInstant(JSContext* cx,
return false;
}
// Steps 3-5.
// Steps 3-4.
DifferenceSettings settings;
if (args.hasDefined(1)) {
Rooted<JSObject*> options(
@@ -1043,25 +1043,19 @@ static bool DifferenceTemporalInstant(JSContext* cx,
}
// Step 3.
Rooted<PlainObject*> resolvedOptions(cx,
NewPlainObjectWithProto(cx, nullptr));
Rooted<PlainObject*> resolvedOptions(cx, CopyOptions(cx, options));
if (!resolvedOptions) {
return false;
}
// Step 4.
if (!CopyDataProperties(cx, resolvedOptions, options)) {
return false;
}
// Step 5.
if (!GetDifferenceSettings(
cx, operation, resolvedOptions, TemporalUnitGroup::Time,
TemporalUnit::Nanosecond, TemporalUnit::Second, &settings)) {
return false;
}
} else {
// Steps 3-5.
// Steps 3-4.
settings = {
TemporalUnit::Nanosecond,
TemporalUnit::Second,
@@ -1070,7 +1064,7 @@ static bool DifferenceTemporalInstant(JSContext* cx,
};
}
// Step 6.
// Step 5.
Duration difference;
if (!DifferenceInstant(cx, instant, other, settings.roundingIncrement,
settings.smallestUnit, settings.largestUnit,
@@ -1078,7 +1072,7 @@ static bool DifferenceTemporalInstant(JSContext* cx,
return false;
}
// Step 7.
// Step 6.
if (operation == TemporalDifference::Since) {
difference = difference.negate();
}
+9 -15
View File
@@ -1187,7 +1187,7 @@ static bool DifferenceTemporalPlainDate(JSContext* cx,
return false;
}
// Steps 4-8.
// Steps 4-7.
DifferenceSettings settings;
Duration duration;
if (args.hasDefined(1)) {
@@ -1198,25 +1198,19 @@ static bool DifferenceTemporalPlainDate(JSContext* cx,
}
// Step 4.
Rooted<PlainObject*> resolvedOptions(cx,
NewPlainObjectWithProto(cx, nullptr));
Rooted<PlainObject*> resolvedOptions(cx, CopyOptions(cx, options));
if (!resolvedOptions) {
return false;
}
// Step 5.
if (!CopyDataProperties(cx, resolvedOptions, options)) {
return false;
}
// Step 6.
if (!GetDifferenceSettings(cx, operation, resolvedOptions,
TemporalUnitGroup::Date, TemporalUnit::Day,
TemporalUnit::Day, &settings)) {
return false;
}
// Step 7.
// Step 6.
Rooted<Value> largestUnitValue(
cx, StringValue(TemporalUnitToString(cx, settings.largestUnit)));
if (!DefineDataProperty(cx, resolvedOptions, cx->names().largestUnit,
@@ -1224,7 +1218,7 @@ static bool DifferenceTemporalPlainDate(JSContext* cx,
return false;
}
// Step 8.
// Step 7.
Duration result;
if (!CalendarDateUntil(cx, calendar, temporalDate, other, resolvedOptions,
&result)) {
@@ -1232,7 +1226,7 @@ static bool DifferenceTemporalPlainDate(JSContext* cx,
}
duration = result.date();
} else {
// Steps 4-7.
// Steps 4-6.
settings = {
TemporalUnit::Day,
TemporalUnit::Day,
@@ -1240,7 +1234,7 @@ static bool DifferenceTemporalPlainDate(JSContext* cx,
Increment{1},
};
// Step 8.
// Step 7.
Duration result;
if (!CalendarDateUntil(cx, calendar, temporalDate, other,
settings.largestUnit, &result)) {
@@ -1249,10 +1243,10 @@ static bool DifferenceTemporalPlainDate(JSContext* cx,
duration = result.date();
}
// Step 9.
// Step 8.
if (settings.smallestUnit != TemporalUnit::Day ||
settings.roundingIncrement != Increment{1}) {
// Step 9.a.
// Step 8.a.
if (!temporal::RoundDuration(cx, duration.date(),
settings.roundingIncrement,
settings.smallestUnit, settings.roundingMode,
@@ -1261,7 +1255,7 @@ static bool DifferenceTemporalPlainDate(JSContext* cx,
}
}
// Step 10.
// Step 9.
if (operation == TemporalDifference::Since) {
duration = duration.negate();
}
+14 -25
View File
@@ -879,17 +879,12 @@ static bool DifferenceISODateTime(JSContext* cx, const PlainDateTime& one,
// https://github.com/tc39/proposal-temporal/issues/2525
// Step 11.
Rooted<PlainObject*> untilOptions(cx, NewPlainObjectWithProto(cx, nullptr));
Rooted<PlainObject*> untilOptions(cx, CopyOptions(cx, maybeOptions));
if (!untilOptions) {
return false;
}
// Step 12.
if (!CopyDataProperties(cx, untilOptions, maybeOptions)) {
return false;
}
// Step 13.
Rooted<Value> largestUnitValue(
cx, StringValue(TemporalUnitToString(cx, dateLargestUnit)));
if (!DefineDataProperty(cx, untilOptions, cx->names().largestUnit,
@@ -897,20 +892,20 @@ static bool DifferenceISODateTime(JSContext* cx, const PlainDateTime& one,
return false;
}
// Step 14.
// Step 13.
if (!CalendarDateUntil(cx, calendar, date1, date2, untilOptions,
&dateDifference)) {
return false;
}
} else {
// Steps 11-14.
// Steps 11-13.
if (!CalendarDateUntil(cx, calendar, date1, date2, dateLargestUnit,
&dateDifference)) {
return false;
}
}
// Step 15.
// Step 14.
TimeDuration balanceResult;
if (!BalanceDuration(cx,
{
@@ -929,7 +924,7 @@ static bool DifferenceISODateTime(JSContext* cx, const PlainDateTime& one,
return false;
}
// Step 16.
// Step 15.
*result = {dateDifference.years, dateDifference.months,
dateDifference.weeks, balanceResult.days,
balanceResult.hours, balanceResult.minutes,
@@ -1016,7 +1011,7 @@ static bool DifferenceTemporalPlainDateTime(JSContext* cx,
return false;
}
// Steps 4-7.
// Steps 4-6.
DifferenceSettings settings;
Duration diff;
if (args.hasDefined(1)) {
@@ -1027,32 +1022,26 @@ static bool DifferenceTemporalPlainDateTime(JSContext* cx,
}
// Step 4.
Rooted<PlainObject*> resolvedOptions(cx,
NewPlainObjectWithProto(cx, nullptr));
Rooted<PlainObject*> resolvedOptions(cx, CopyOptions(cx, options));
if (!resolvedOptions) {
return false;
}
// Step 5.
if (!CopyDataProperties(cx, resolvedOptions, options)) {
return false;
}
// Step 6.
if (!GetDifferenceSettings(
cx, operation, resolvedOptions, TemporalUnitGroup::DateTime,
TemporalUnit::Nanosecond, TemporalUnit::Day, &settings)) {
return false;
}
// Step 7.
// Step 6.
if (!::DifferenceISODateTime(cx, ToPlainDateTime(dateTime), other, calendar,
settings.largestUnit, resolvedOptions,
&diff)) {
return false;
}
} else {
// Steps 4-6.
// Steps 4-5.
settings = {
TemporalUnit::Nanosecond,
TemporalUnit::Day,
@@ -1060,21 +1049,21 @@ static bool DifferenceTemporalPlainDateTime(JSContext* cx,
Increment{1},
};
// Step 7.
// Step 6.
if (!::DifferenceISODateTime(cx, ToPlainDateTime(dateTime), other, calendar,
settings.largestUnit, nullptr, &diff)) {
return false;
}
}
// Step 8.
// Step 7.
Rooted<PlainDateObject*> relativeTo(
cx, CreateTemporalDate(cx, ToPlainDate(dateTime), calendar));
if (!relativeTo) {
return false;
}
// Step 9.
// Step 8.
Duration roundResult;
if (!temporal::RoundDuration(cx, diff, settings.roundingIncrement,
settings.smallestUnit, settings.roundingMode,
@@ -1082,13 +1071,13 @@ static bool DifferenceTemporalPlainDateTime(JSContext* cx,
return false;
}
// Step 10.
// Step 9.
TimeDuration result;
if (!BalanceDuration(cx, roundResult, settings.largestUnit, &result)) {
return false;
}
// Step 11.
// Step 10.
Duration duration = {
roundResult.years, roundResult.months, roundResult.weeks,
result.days, result.hours, result.minutes,
+7 -13
View File
@@ -1718,7 +1718,7 @@ static bool DifferenceTemporalPlainTime(JSContext* cx,
return false;
}
// Steps 3-5.
// Steps 3-4.
DifferenceSettings settings;
if (args.hasDefined(1)) {
Rooted<JSObject*> options(
@@ -1728,25 +1728,19 @@ static bool DifferenceTemporalPlainTime(JSContext* cx,
}
// Step 3.
Rooted<PlainObject*> resolvedOptions(cx,
NewPlainObjectWithProto(cx, nullptr));
Rooted<PlainObject*> resolvedOptions(cx, CopyOptions(cx, options));
if (!resolvedOptions) {
return false;
}
// Step 4.
if (!CopyDataProperties(cx, resolvedOptions, options)) {
return false;
}
// Step 5.
if (!GetDifferenceSettings(
cx, operation, resolvedOptions, TemporalUnitGroup::Time,
TemporalUnit::Nanosecond, TemporalUnit::Hour, &settings)) {
return false;
}
} else {
// Steps 3-5.
// Steps 3-4.
settings = {
TemporalUnit::Nanosecond,
TemporalUnit::Hour,
@@ -1755,10 +1749,10 @@ static bool DifferenceTemporalPlainTime(JSContext* cx,
};
}
// Step 6.
// Step 5.
auto diff = DifferenceTime(temporalTime, other);
// Step 7.
// Step 6.
Duration roundedDuration;
if (!RoundDuration(cx, diff.toDuration().time(), settings.roundingIncrement,
settings.smallestUnit, settings.roundingMode,
@@ -1766,11 +1760,11 @@ static bool DifferenceTemporalPlainTime(JSContext* cx,
return false;
}
// Step 8.
// Step 7.
auto balancedDuration =
BalanceTimeDuration(roundedDuration, settings.largestUnit);
// Step 9.
// Step 8.
if (operation == TemporalDifference::Since) {
balancedDuration = balancedDuration.negate();
}
+17 -27
View File
@@ -427,17 +427,12 @@ static bool DifferenceTemporalPlainYearMonth(JSContext* cx,
}
// Step 5.
resolvedOptions = NewPlainObjectWithProto(cx, nullptr);
resolvedOptions = CopyOptions(cx, options);
if (!resolvedOptions) {
return false;
}
// Step 6.
if (!CopyDataProperties(cx, resolvedOptions, options)) {
return false;
}
// Step 7.
if (!GetDifferenceSettings(cx, operation, resolvedOptions,
TemporalUnitGroup::Date, TemporalUnit::Month,
TemporalUnit::Month, TemporalUnit::Year,
@@ -445,7 +440,7 @@ static bool DifferenceTemporalPlainYearMonth(JSContext* cx,
return false;
}
// Step 8.
// Step 7.
Rooted<Value> largestUnitValue(
cx, StringValue(TemporalUnitToString(cx, settings.largestUnit)));
if (!DefineDataProperty(cx, resolvedOptions, cx->names().largestUnit,
@@ -453,7 +448,7 @@ static bool DifferenceTemporalPlainYearMonth(JSContext* cx,
return false;
}
} else {
// Steps 5-7.
// Steps 5-6.
settings = {
TemporalUnit::Month,
TemporalUnit::Year,
@@ -461,10 +456,10 @@ static bool DifferenceTemporalPlainYearMonth(JSContext* cx,
Increment{1},
};
// Step 8. (Not applicable in our implementation.)
// Step 7. (Not applicable in our implementation.)
}
// Step 9.
// Step 8.
JS::RootedVector<PropertyKey> fieldNames(cx);
if (!CalendarFields(cx, calendar,
{CalendarField::MonthCode, CalendarField::Year},
@@ -472,47 +467,47 @@ static bool DifferenceTemporalPlainYearMonth(JSContext* cx,
return false;
}
// Step 10.
// Step 9.
Rooted<PlainObject*> thisFields(
cx, PrepareTemporalFields(cx, yearMonth, fieldNames));
if (!thisFields) {
return false;
}
// Step 11.
// Step 10.
Value one = Int32Value(1);
auto handleOne = Handle<Value>::fromMarkedLocation(&one);
if (!DefineDataProperty(cx, thisFields, cx->names().day, handleOne)) {
return false;
}
// Step 12.
// Step 11.
Rooted<Wrapped<PlainDateObject*>> thisDate(
cx, CalendarDateFromFields(cx, calendar, thisFields));
if (!thisDate) {
return false;
}
// Step 13.
// Step 12.
Rooted<PlainObject*> otherFields(
cx, PrepareTemporalFields(cx, other, fieldNames));
if (!otherFields) {
return false;
}
// Step 14.
// Step 13.
if (!DefineDataProperty(cx, otherFields, cx->names().day, handleOne)) {
return false;
}
// Step 15.
// Step 14.
Rooted<Wrapped<PlainDateObject*>> otherDate(
cx, CalendarDateFromFields(cx, calendar, otherFields));
if (!otherDate) {
return false;
}
// Step 16.
// Step 15.
Duration result;
if (resolvedOptions) {
if (!CalendarDateUntil(cx, calendar, thisDate, otherDate, resolvedOptions,
@@ -529,7 +524,7 @@ static bool DifferenceTemporalPlainYearMonth(JSContext* cx,
// We only care about years and months here, all other fields are set to zero.
Duration duration = {result.years, result.months};
// Step 17.
// Step 16.
if (settings.smallestUnit != TemporalUnit::Month ||
settings.roundingIncrement != Increment{1}) {
Duration rounded;
@@ -542,7 +537,7 @@ static bool DifferenceTemporalPlainYearMonth(JSContext* cx,
duration = {rounded.years, rounded.months};
}
// Step 18.
// Step 17.
if (operation == TemporalDifference::Since) {
duration = duration.negate();
}
@@ -653,31 +648,26 @@ static bool AddDurationToOrSubtractDurationFromPlainYearMonth(
}
// Step 14.
Rooted<PlainObject*> optionsCopy(cx, NewPlainObjectWithProto(cx, nullptr));
Rooted<PlainObject*> optionsCopy(cx, CopyOptions(cx, options));
if (!optionsCopy) {
return false;
}
// Step 15.
if (!CopyDataProperties(cx, optionsCopy, options)) {
return false;
}
// Step 16.
Rooted<Wrapped<PlainDateObject*>> addedDate(
cx, CalendarDateAdd(cx, calendar, date, durationToAdd, options));
if (!addedDate) {
return false;
}
// Step 17.
// Step 16.
Rooted<PlainObject*> addedDateFields(
cx, PrepareTemporalFields(cx, addedDate, fieldNames));
if (!addedDateFields) {
return false;
}
// Step 18.
// Step 17.
auto obj =
CalendarYearMonthFromFields(cx, calendar, addedDateFields, optionsCopy);
if (!obj) {
+20
View File
@@ -1675,6 +1675,26 @@ bool js::temporal::GetMethodForCall(JSContext* cx, Handle<JSObject*> object,
return true;
}
/**
* CopyOptions ( ... )
*/
PlainObject* js::temporal::CopyOptions(JSContext* cx,
JS::Handle<JSObject*> options) {
// Step 1.
Rooted<PlainObject*> optionsCopy(cx, NewPlainObjectWithProto(cx, nullptr));
if (!optionsCopy) {
return nullptr;
}
// Step 2.
if (!CopyDataProperties(cx, optionsCopy, options)) {
return nullptr;
}
// Step 3.
return optionsCopy;
}
/**
* CopyDataProperties ( target, source, excludedKeys [ , excludedValues ] )
*
+5
View File
@@ -354,6 +354,11 @@ bool GetMethodForCall(JSContext* cx, JS::Handle<JSObject*> object,
JS::Handle<PropertyName*> name,
JS::MutableHandle<JS::Value> result);
/**
* CopyOptions ( ... )
*/
PlainObject* CopyOptions(JSContext* cx, JS::Handle<JSObject*> options);
/**
* CopyDataProperties ( target, source, excludedKeys [ , excludedValues ] )
*/
+13 -18
View File
@@ -1336,7 +1336,7 @@ static bool DifferenceTemporalZonedDateTime(JSContext* cx,
return false;
}
// Steps 4-7.
// Steps 4-6.
Rooted<PlainObject*> resolvedOptions(cx);
DifferenceSettings settings;
if (args.hasDefined(1)) {
@@ -1347,24 +1347,19 @@ static bool DifferenceTemporalZonedDateTime(JSContext* cx,
}
// Step 4.
resolvedOptions = NewPlainObjectWithProto(cx, nullptr);
resolvedOptions = CopyOptions(cx, options);
if (!resolvedOptions) {
return false;
}
// Step 5.
if (!CopyDataProperties(cx, resolvedOptions, options)) {
return false;
}
// Step 6.
if (!GetDifferenceSettings(
cx, operation, resolvedOptions, TemporalUnitGroup::DateTime,
TemporalUnit::Nanosecond, TemporalUnit::Hour, &settings)) {
return false;
}
// Step 7.
// Step 6.
Rooted<Value> largestUnitValue(
cx, StringValue(TemporalUnitToString(cx, settings.largestUnit)));
if (!DefineDataProperty(cx, resolvedOptions, cx->names().largestUnit,
@@ -1372,7 +1367,7 @@ static bool DifferenceTemporalZonedDateTime(JSContext* cx,
return false;
}
} else {
// Steps 4-6.
// Steps 4-5.
settings = {
TemporalUnit::Nanosecond,
TemporalUnit::Hour,
@@ -1380,14 +1375,14 @@ static bool DifferenceTemporalZonedDateTime(JSContext* cx,
Increment{1},
};
// Step 7. (Not applicable in our implementation.)
// Step 6. (Not applicable in our implementation.)
}
// Step 8.
// Step 7.
if (settings.largestUnit > TemporalUnit::Day) {
MOZ_ASSERT(settings.smallestUnit >= settings.largestUnit);
// Step 8.a.
// Step 7.a.
Duration difference;
if (!DifferenceInstant(cx, epochInstant, otherInstant,
settings.roundingIncrement, settings.smallestUnit,
@@ -1396,7 +1391,7 @@ static bool DifferenceTemporalZonedDateTime(JSContext* cx,
return false;
}
// Step 8.b.
// Step 7.b.
if (operation == TemporalDifference::Since) {
difference = difference.negate();
}
@@ -1413,12 +1408,12 @@ static bool DifferenceTemporalZonedDateTime(JSContext* cx,
// FIXME: spec issue - move this step next to the calendar validation?
// https://github.com/tc39/proposal-temporal/issues/2533
// Step 9.
// Step 8.
if (!TimeZoneEqualsOrThrow(cx, timeZone, otherTimeZone)) {
return false;
}
// Step 10.
// Step 9.
Duration difference;
if (resolvedOptions) {
if (!::DifferenceZonedDateTime(cx, epochInstant, otherInstant, timeZone,
@@ -1434,7 +1429,7 @@ static bool DifferenceTemporalZonedDateTime(JSContext* cx,
}
}
// Step 11.
// Step 10.
Duration roundResult;
if (!RoundDuration(cx, difference, settings.roundingIncrement,
settings.smallestUnit, settings.roundingMode,
@@ -1443,7 +1438,7 @@ static bool DifferenceTemporalZonedDateTime(JSContext* cx,
return false;
}
// Step 12.
// Step 11.
Duration result;
if (!AdjustRoundedDurationDays(cx, roundResult, settings.roundingIncrement,
settings.smallestUnit, settings.roundingMode,
@@ -1451,7 +1446,7 @@ static bool DifferenceTemporalZonedDateTime(JSContext* cx,
return false;
}
// Step 13.
// Step 12.
if (operation == TemporalDifference::Since) {
result = result.negate();
}