1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-26 05:47:34 +00:00

LibJS: Convert Temporal.TimeZone functions to ThrowCompletionOr

This commit is contained in:
Linus Groh 2021-10-21 20:18:24 +01:00
parent 15f52c5f8e
commit 7085458a23
4 changed files with 33 additions and 33 deletions

View file

@ -26,7 +26,7 @@ void TimeZoneConstructor::initialize(GlobalObject& global_object)
define_direct_property(vm.names.prototype, global_object.temporal_time_zone_prototype(), 0);
u8 attr = Attribute::Writable | Attribute::Configurable;
define_old_native_function(vm.names.from, from, 1, attr);
define_native_function(vm.names.from, from, 1, attr);
define_direct_property(vm.names.length, Value(1), Attribute::Configurable);
}
@ -77,12 +77,12 @@ ThrowCompletionOr<Object*> TimeZoneConstructor::construct(FunctionObject& new_ta
}
// 11.3.2 Temporal.TimeZone.from ( item ), https://tc39.es/proposal-temporal/#sec-temporal.timezone.from
JS_DEFINE_OLD_NATIVE_FUNCTION(TimeZoneConstructor::from)
JS_DEFINE_NATIVE_FUNCTION(TimeZoneConstructor::from)
{
auto item = vm.argument(0);
// 1. Return ? ToTemporalTimeZone(item).
return TRY_OR_DISCARD(to_temporal_time_zone(global_object, item));
return TRY(to_temporal_time_zone(global_object, item));
}
}

View file

@ -24,7 +24,7 @@ public:
private:
virtual bool has_constructor() const override { return true; }
JS_DECLARE_OLD_NATIVE_FUNCTION(from);
JS_DECLARE_NATIVE_FUNCTION(from);
};
}

View file

@ -27,36 +27,36 @@ void TimeZonePrototype::initialize(GlobalObject& global_object)
auto& vm = this->vm();
u8 attr = Attribute::Writable | Attribute::Configurable;
define_old_native_accessor(vm.names.id, id_getter, {}, Attribute::Configurable);
define_old_native_function(vm.names.getOffsetNanosecondsFor, get_offset_nanoseconds_for, 1, attr);
define_old_native_function(vm.names.getOffsetStringFor, get_offset_string_for, 1, attr);
define_old_native_function(vm.names.getPlainDateTimeFor, get_plain_date_time_for, 1, attr);
define_old_native_function(vm.names.toString, to_string, 0, attr);
define_old_native_function(vm.names.toJSON, to_json, 0, attr);
define_native_accessor(vm.names.id, id_getter, {}, Attribute::Configurable);
define_native_function(vm.names.getOffsetNanosecondsFor, get_offset_nanoseconds_for, 1, attr);
define_native_function(vm.names.getOffsetStringFor, get_offset_string_for, 1, attr);
define_native_function(vm.names.getPlainDateTimeFor, get_plain_date_time_for, 1, attr);
define_native_function(vm.names.toString, to_string, 0, attr);
define_native_function(vm.names.toJSON, to_json, 0, attr);
// 11.4.2 Temporal.TimeZone.prototype[ @@toStringTag ], https://tc39.es/proposal-temporal/#sec-temporal.timezone.prototype-@@tostringtag
define_direct_property(*vm.well_known_symbol_to_string_tag(), js_string(vm, "Temporal.TimeZone"), Attribute::Configurable);
}
// 11.4.3 get Temporal.TimeZone.prototype.id, https://tc39.es/proposal-temporal/#sec-get-temporal.timezone.prototype.id
JS_DEFINE_OLD_NATIVE_FUNCTION(TimeZonePrototype::id_getter)
JS_DEFINE_NATIVE_FUNCTION(TimeZonePrototype::id_getter)
{
// 1. Let timeZone be the this value.
auto time_zone = vm.this_value(global_object);
// 2. Return ? ToString(timeZone).
return js_string(vm, TRY_OR_DISCARD(time_zone.to_string(global_object)));
return js_string(vm, TRY(time_zone.to_string(global_object)));
}
// 11.4.4 Temporal.TimeZone.prototype.getOffsetNanosecondsFor ( instant ), https://tc39.es/proposal-temporal/#sec-temporal.timezone.prototype.getoffsetnanosecondsfor
JS_DEFINE_OLD_NATIVE_FUNCTION(TimeZonePrototype::get_offset_nanoseconds_for)
JS_DEFINE_NATIVE_FUNCTION(TimeZonePrototype::get_offset_nanoseconds_for)
{
// 1. Let timeZone be the this value.
// 2. Perform ? RequireInternalSlot(timeZone, [[InitializedTemporalTimeZone]]).
auto* time_zone = TRY_OR_DISCARD(typed_this_object(global_object));
auto* time_zone = TRY(typed_this_object(global_object));
// 3. Set instant to ? ToTemporalInstant(instant).
auto* instant = TRY_OR_DISCARD(to_temporal_instant(global_object, vm.argument(0)));
auto* instant = TRY(to_temporal_instant(global_object, vm.argument(0)));
// 4. If timeZone.[[OffsetNanoseconds]] is not undefined, return 𝔽(timeZone.[[OffsetNanoseconds]]).
if (time_zone->offset_nanoseconds().has_value())
@ -67,55 +67,55 @@ JS_DEFINE_OLD_NATIVE_FUNCTION(TimeZonePrototype::get_offset_nanoseconds_for)
}
// 11.4.5 Temporal.TimeZone.prototype.getOffsetStringFor ( instant ), https://tc39.es/proposal-temporal/#sec-temporal.timezone.prototype.getoffsetstringfor
JS_DEFINE_OLD_NATIVE_FUNCTION(TimeZonePrototype::get_offset_string_for)
JS_DEFINE_NATIVE_FUNCTION(TimeZonePrototype::get_offset_string_for)
{
// 1. Let timeZone be the this value.
// 2. Perform ? RequireInternalSlot(timeZone, [[InitializedTemporalTimeZone]]).
auto* time_zone = TRY_OR_DISCARD(typed_this_object(global_object));
auto* time_zone = TRY(typed_this_object(global_object));
// 3. Set instant to ? ToTemporalInstant(instant).
auto* instant = TRY_OR_DISCARD(to_temporal_instant(global_object, vm.argument(0)));
auto* instant = TRY(to_temporal_instant(global_object, vm.argument(0)));
// 4. Return ? BuiltinTimeZoneGetOffsetStringFor(timeZone, instant).
auto offset_string = TRY_OR_DISCARD(builtin_time_zone_get_offset_string_for(global_object, time_zone, *instant));
auto offset_string = TRY(builtin_time_zone_get_offset_string_for(global_object, time_zone, *instant));
return js_string(vm, move(offset_string));
}
// 11.4.6 Temporal.TimeZone.prototype.getPlainDateTimeFor ( instant [ , calendarLike ] ), https://tc39.es/proposal-temporal/#sec-temporal.timezone.prototype.getplaindatetimefor
JS_DEFINE_OLD_NATIVE_FUNCTION(TimeZonePrototype::get_plain_date_time_for)
JS_DEFINE_NATIVE_FUNCTION(TimeZonePrototype::get_plain_date_time_for)
{
// 1. Let timeZone be the this value.
auto time_zone = vm.this_value(global_object);
// 2. Set instant to ? ToTemporalInstant(instant).
auto* instant = TRY_OR_DISCARD(to_temporal_instant(global_object, vm.argument(0)));
auto* instant = TRY(to_temporal_instant(global_object, vm.argument(0)));
// 3. Let calendar be ? ToTemporalCalendarWithISODefault(calendarLike).
auto* calendar = TRY_OR_DISCARD(to_temporal_calendar_with_iso_default(global_object, vm.argument(1)));
auto* calendar = TRY(to_temporal_calendar_with_iso_default(global_object, vm.argument(1)));
// 4. Return ? BuiltinTimeZoneGetPlainDateTimeFor(timeZone, instant, calendar).
return TRY_OR_DISCARD(builtin_time_zone_get_plain_date_time_for(global_object, time_zone, *instant, *calendar));
return TRY(builtin_time_zone_get_plain_date_time_for(global_object, time_zone, *instant, *calendar));
}
// 11.4.11 Temporal.TimeZone.prototype.toString ( ), https://tc39.es/proposal-temporal/#sec-temporal.timezone.prototype.tostring
JS_DEFINE_OLD_NATIVE_FUNCTION(TimeZonePrototype::to_string)
JS_DEFINE_NATIVE_FUNCTION(TimeZonePrototype::to_string)
{
// 1. Let timeZone be the this value.
// 2. Perform ? RequireInternalSlot(timeZone, [[InitializedTemporalTimeZone]]).
auto* time_zone = TRY_OR_DISCARD(typed_this_object(global_object));
auto* time_zone = TRY(typed_this_object(global_object));
// 3. Return timeZone.[[Identifier]].
return js_string(vm, time_zone->identifier());
}
// 11.4.12 Temporal.TimeZone.prototype.toJSON ( ), https://tc39.es/proposal-temporal/#sec-temporal.timezone.prototype.tojson
JS_DEFINE_OLD_NATIVE_FUNCTION(TimeZonePrototype::to_json)
JS_DEFINE_NATIVE_FUNCTION(TimeZonePrototype::to_json)
{
// 1. Let timeZone be the this value.
auto time_zone = vm.this_value(global_object);
// 2. Return ? ToString(timeZone).
return js_string(vm, TRY_OR_DISCARD(time_zone.to_string(global_object)));
return js_string(vm, TRY(time_zone.to_string(global_object)));
}
}

View file

@ -20,12 +20,12 @@ public:
virtual ~TimeZonePrototype() override = default;
private:
JS_DECLARE_OLD_NATIVE_FUNCTION(id_getter);
JS_DECLARE_OLD_NATIVE_FUNCTION(get_offset_nanoseconds_for);
JS_DECLARE_OLD_NATIVE_FUNCTION(get_offset_string_for);
JS_DECLARE_OLD_NATIVE_FUNCTION(get_plain_date_time_for);
JS_DECLARE_OLD_NATIVE_FUNCTION(to_string);
JS_DECLARE_OLD_NATIVE_FUNCTION(to_json);
JS_DECLARE_NATIVE_FUNCTION(id_getter);
JS_DECLARE_NATIVE_FUNCTION(get_offset_nanoseconds_for);
JS_DECLARE_NATIVE_FUNCTION(get_offset_string_for);
JS_DECLARE_NATIVE_FUNCTION(get_plain_date_time_for);
JS_DECLARE_NATIVE_FUNCTION(to_string);
JS_DECLARE_NATIVE_FUNCTION(to_json);
};
}