Skip to content

Commit

Permalink
Bug 1840374 - Part 8: Implement changes for ToTemporalTimeZoneSlotVal…
Browse files Browse the repository at this point in the history
…ue. r=spidermonkey-reviewers,sfink

Implement the changes from <tc39/proposal-temporal#2485>.

The function name hasn't yet been updated to reflect the new name.

Depends on D182025

Differential Revision: https://phabricator.services.mozilla.com/D182026
  • Loading branch information
anba committed Jul 17, 2023
1 parent d535ce8 commit e57c9e2
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 57 deletions.
2 changes: 1 addition & 1 deletion js/public/friend/ErrorNumbers.msg
Original file line number Diff line number Diff line change
Expand Up @@ -824,7 +824,7 @@ MSG_DEF(JSMSG_TEMPORAL_INVALID_UNIT_RANGE, 0, JSEXN_RANGEERR, "smallest
MSG_DEF(JSMSG_TEMPORAL_INVALID_UNIT_OPTION, 2, JSEXN_RANGEERR, "{0} is not a valid {1} option in this context")
MSG_DEF(JSMSG_TEMPORAL_INVALID_NUMBER, 1, JSEXN_RANGEERR, "{0} must be larger than zero")
MSG_DEF(JSMSG_TEMPORAL_INVALID_INTEGER, 1, JSEXN_RANGEERR, "{0} must be an integer")
MSG_DEF(JSMSG_TEMPORAL_INVALID_OBJECT, 2, JSEXN_RANGEERR, "{0} mustn't be a {1} object")
MSG_DEF(JSMSG_TEMPORAL_INVALID_OBJECT, 2, JSEXN_TYPEERR, "{0} mustn't be a {1} object")
MSG_DEF(JSMSG_TEMPORAL_MISSING_OPTION, 1, JSEXN_RANGEERR, "undefined {0} option")
MSG_DEF(JSMSG_TEMPORAL_MISSING_PROPERTY, 1, JSEXN_TYPEERR, "{0} property is undefined")
MSG_DEF(JSMSG_TEMPORAL_UNEXPECTED_PROPERTY, 1, JSEXN_TYPEERR, "{0} property is not undefined")
Expand Down
105 changes: 51 additions & 54 deletions js/src/builtin/temporal/TimeZone.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -731,7 +731,7 @@ TimeZoneObject* js::temporal::CreateTemporalTimeZoneUTC(JSContext* cx) {
}

/**
* ToTemporalTimeZone ( temporalTimeZoneLike )
* ToTemporalTimeZoneSlotValue ( temporalTimeZoneLike )
*/
bool js::temporal::ToTemporalTimeZone(JSContext* cx, Handle<JSString*> string,
MutableHandle<TimeZoneValue> result) {
Expand All @@ -747,15 +747,15 @@ bool js::temporal::ToTemporalTimeZone(JSContext* cx, Handle<JSString*> string,

// Steps 4-5.
if (timeZoneName) {
// Step 4.a. (Implemented in ParseTemporalTimeZoneString)
// Steps 4.a-b. (Not applicable in our implementation.)

// Step 4.b.
// Steps 4.c-d.
timeZoneName = ValidateAndCanonicalizeTimeZoneName(cx, timeZoneName);
if (!timeZoneName) {
return false;
}

// Steps 4.c and 5.
// Steps 4.e and 5.
auto* obj = ::CreateTemporalTimeZone(cx, timeZoneName);
if (!obj) {
return false;
Expand All @@ -776,7 +776,39 @@ bool js::temporal::ToTemporalTimeZone(JSContext* cx, Handle<JSString*> string,
}

/**
* ToTemporalTimeZone ( temporalTimeZoneLike )
* ObjectImplementsTemporalTimeZoneProtocol ( object )
*/
static bool ObjectImplementsTemporalTimeZoneProtocol(JSContext* cx,
Handle<JSObject*> object,
bool* result) {
// Step 1. (Not applicable in our implementation.)
MOZ_ASSERT(!object->canUnwrapAs<TimeZoneObject>(),
"TimeZone objects handled in the caller");

// Step 2.
for (auto key : {
&JSAtomState::getOffsetNanosecondsFor,
&JSAtomState::getPossibleInstantsFor,
&JSAtomState::id,
}) {
// Step 2.a.
bool has;
if (!HasProperty(cx, object, cx->names().*key, &has)) {
return false;
}
if (!has) {
*result = false;
return true;
}
}

// Step 3.
*result = true;
return true;
}

/**
* ToTemporalTimeZoneSlotValue ( temporalTimeZoneLike )
*/
bool js::temporal::ToTemporalTimeZone(JSContext* cx,
Handle<Value> temporalTimeZoneLike,
Expand All @@ -786,13 +818,13 @@ bool js::temporal::ToTemporalTimeZone(JSContext* cx,
if (timeZoneLike.isObject()) {
Rooted<JSObject*> obj(cx, &timeZoneLike.toObject());

// Step 1.a.
// Step 1.b. (Partial)
if (obj->canUnwrapAs<TimeZoneObject>()) {
result.set(obj);
return true;
}

// Step 1.b.
// Step 1.a.
if (auto* zonedDateTime = obj->maybeUnwrapIf<ZonedDateTimeObject>()) {
Rooted<TimeZoneValue> timeZone(cx, zonedDateTime->timeZone());
if (!cx->compartment()->wrap(cx, &timeZone)) {
Expand All @@ -802,56 +834,22 @@ bool js::temporal::ToTemporalTimeZone(JSContext* cx,
return true;
}

// Step 1.c.
if (obj->canUnwrapAs<CalendarObject>()) {
JS_ReportErrorNumberUTF8(cx, GetErrorMessage, nullptr,
JSMSG_TEMPORAL_INVALID_OBJECT,
"Temporal.TimeZone", "Temporal.Calendar");
return false;
}

// Step 1.d.
bool hasTimeZone;
if (!HasProperty(cx, obj, cx->names().timeZone, &hasTimeZone)) {
// Step 1.b.
bool implementsTimeZoneProtocol;
if (!ObjectImplementsTemporalTimeZoneProtocol(
cx, obj, &implementsTimeZoneProtocol)) {
return false;
}
if (!hasTimeZone) {
result.set(obj);
return true;
}

// Step 1.e.
if (!GetProperty(cx, obj, obj, cx->names().timeZone, &timeZoneLike)) {
if (!implementsTimeZoneProtocol) {
JS_ReportErrorNumberUTF8(cx, GetErrorMessage, nullptr,
JSMSG_TEMPORAL_INVALID_OBJECT,
"Temporal.TimeZone", obj->getClass()->name);
return false;
}

// Step 1.f.
if (timeZoneLike.isObject()) {
obj = &timeZoneLike.toObject();

// FIXME: spec issue - does this check is actually useful? In which case
// will have a "timeZone" property be a CalendarObject?

// Step 1.f.i.
if (obj->canUnwrapAs<CalendarObject>()) {
JS_ReportErrorNumberUTF8(cx, GetErrorMessage, nullptr,
JSMSG_TEMPORAL_INVALID_OBJECT,
"Temporal.TimeZone", "Temporal.Calendar");
return false;
}

// FIXME: spec issue - does this check is actually useful? In which case
// will have a "timeZone" property have another "timeZone" property?

// Step 1.f.ii.
if (!HasProperty(cx, obj, cx->names().timeZone, &hasTimeZone)) {
return false;
}
if (!hasTimeZone) {
result.set(obj);
return true;
}
}
// Step 1.c.
result.set(obj);
return true;
}

// Step 2.
Expand All @@ -869,8 +867,7 @@ bool js::temporal::ToTemporalTimeZone(JSContext* cx,
// is then interpreted as "-10:00".
//
// FIXME: spec issue - ToString for negative Numbers/BigInt also accepted?
if (!timeZoneLike.isString() && !timeZoneLike.isObject() &&
!timeZoneLike.isNumeric()) {
if (!timeZoneLike.isString() && !timeZoneLike.isNumeric()) {
ReportValueError(cx, JSMSG_TEMPORAL_TIMEZONE_PARSE_BAD_TYPE,
JSDVG_IGNORE_STACK, timeZoneLike, nullptr);
return false;
Expand Down
4 changes: 2 additions & 2 deletions js/src/builtin/temporal/TimeZone.h
Original file line number Diff line number Diff line change
Expand Up @@ -112,14 +112,14 @@ TimeZoneObject* CreateTemporalTimeZone(JSContext* cx,
TimeZoneObject* CreateTemporalTimeZoneUTC(JSContext* cx);

/**
* ToTemporalTimeZone ( temporalTimeZoneLike )
* ToTemporalTimeZoneSlotValue ( temporalTimeZoneLike )
*/
bool ToTemporalTimeZone(JSContext* cx,
JS::Handle<JS::Value> temporalTimeZoneLike,
JS::MutableHandle<TimeZoneValue> result);

/**
* ToTemporalTimeZone ( temporalTimeZoneLike )
* ToTemporalTimeZoneSlotValue ( temporalTimeZoneLike )
*/
bool ToTemporalTimeZone(JSContext* cx, JS::Handle<JSString*> string,
JS::MutableHandle<TimeZoneValue> result);
Expand Down
1 change: 1 addition & 0 deletions js/src/vm/CommonPropertyNames.h
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,7 @@
MACRO_(hour12, hour12, "hour12") \
MACRO_(hourCycle, hourCycle, "hourCycle") \
MACRO_(hours, hours, "hours") \
MACRO2(id, id, "id") \
MACRO2(if, if_, "if") \
MACRO_(ignoreCase, ignoreCase, "ignoreCase") \
MACRO_(ignorePunctuation, ignorePunctuation, "ignorePunctuation") \
Expand Down

0 comments on commit e57c9e2

Please sign in to comment.