2021-07-14 21:01:12 +01:00
|
|
|
/*
|
2023-01-26 15:20:00 +00:00
|
|
|
* Copyright (c) 2021-2023, Linus Groh <linusg@serenityos.org>
|
2021-07-14 21:01:12 +01:00
|
|
|
*
|
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include <LibJS/Runtime/GlobalObject.h>
|
|
|
|
|
#include <LibJS/Runtime/Temporal/Calendar.h>
|
|
|
|
|
#include <LibJS/Runtime/Temporal/CalendarConstructor.h>
|
|
|
|
|
|
|
|
|
|
namespace JS::Temporal {
|
|
|
|
|
|
|
|
|
|
// 12.2 The Temporal.Calendar Constructor, https://tc39.es/proposal-temporal/#sec-temporal-calendar-constructor
|
2022-08-16 00:20:49 +01:00
|
|
|
CalendarConstructor::CalendarConstructor(Realm& realm)
|
2022-09-14 19:10:27 -04:00
|
|
|
: NativeFunction(realm.vm().names.Calendar.as_string(), *realm.intrinsics().function_prototype())
|
2021-07-14 21:01:12 +01:00
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-28 12:33:35 -05:00
|
|
|
ThrowCompletionOr<void> CalendarConstructor::initialize(Realm& realm)
|
2021-07-14 21:01:12 +01:00
|
|
|
{
|
2023-01-28 12:33:35 -05:00
|
|
|
MUST_OR_THROW_OOM(NativeFunction::initialize(realm));
|
2021-07-14 21:01:12 +01:00
|
|
|
|
|
|
|
|
auto& vm = this->vm();
|
|
|
|
|
|
2022-05-24 18:27:09 +01:00
|
|
|
// 12.3.1 Temporal.Calendar.prototype, https://tc39.es/proposal-temporal/#sec-temporal.calendar.prototype
|
2022-08-27 00:54:55 +01:00
|
|
|
define_direct_property(vm.names.prototype, realm.intrinsics().temporal_calendar_prototype(), 0);
|
2021-07-14 21:01:12 +01:00
|
|
|
|
2021-08-01 11:40:03 +01:00
|
|
|
u8 attr = Attribute::Writable | Attribute::Configurable;
|
2022-08-22 21:47:35 +01:00
|
|
|
define_native_function(realm, vm.names.from, from, 1, attr);
|
2021-08-01 11:40:03 +01:00
|
|
|
|
2021-07-14 21:01:12 +01:00
|
|
|
define_direct_property(vm.names.length, Value(1), Attribute::Configurable);
|
2023-01-28 12:33:35 -05:00
|
|
|
|
|
|
|
|
return {};
|
2021-07-14 21:01:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 12.2.1 Temporal.Calendar ( id ), https://tc39.es/proposal-temporal/#sec-temporal.calendar
|
2021-10-20 21:16:30 +01:00
|
|
|
ThrowCompletionOr<Value> CalendarConstructor::call()
|
2021-07-14 21:01:12 +01:00
|
|
|
{
|
|
|
|
|
auto& vm = this->vm();
|
|
|
|
|
|
|
|
|
|
// 1. If NewTarget is undefined, then
|
|
|
|
|
// a. Throw a TypeError exception.
|
2022-08-16 20:33:17 +01:00
|
|
|
return vm.throw_completion<TypeError>(ErrorType::ConstructorWithoutNew, "Temporal.Calendar");
|
2021-07-14 21:01:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 12.2.1 Temporal.Calendar ( id ), https://tc39.es/proposal-temporal/#sec-temporal.calendar
|
2022-12-14 19:18:10 +00:00
|
|
|
ThrowCompletionOr<NonnullGCPtr<Object>> CalendarConstructor::construct(FunctionObject& new_target)
|
2021-07-14 21:01:12 +01:00
|
|
|
{
|
|
|
|
|
auto& vm = this->vm();
|
|
|
|
|
|
|
|
|
|
// 2. Set id to ? ToString(id).
|
2023-01-26 15:20:00 +00:00
|
|
|
auto identifier = TRY(vm.argument(0).to_string(vm));
|
2021-07-14 21:01:12 +01:00
|
|
|
|
2022-04-29 18:42:56 +02:00
|
|
|
// 3. If IsBuiltinCalendar(id) is false, then
|
2021-07-14 21:01:12 +01:00
|
|
|
if (!is_builtin_calendar(identifier)) {
|
|
|
|
|
// a. Throw a RangeError exception.
|
2022-08-16 20:33:17 +01:00
|
|
|
return vm.throw_completion<RangeError>(ErrorType::TemporalInvalidCalendarIdentifier, identifier);
|
2021-07-14 21:01:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 4. Return ? CreateTemporalCalendar(id, NewTarget).
|
2022-12-14 19:18:10 +00:00
|
|
|
return *TRY(create_temporal_calendar(vm, identifier, &new_target));
|
2021-07-14 21:01:12 +01:00
|
|
|
}
|
|
|
|
|
|
2022-02-04 19:29:15 +00:00
|
|
|
// 12.3.2 Temporal.Calendar.from ( calendarLike ), https://tc39.es/proposal-temporal/#sec-temporal.calendar.from
|
2021-10-21 19:55:55 +01:00
|
|
|
JS_DEFINE_NATIVE_FUNCTION(CalendarConstructor::from)
|
2021-08-01 11:40:03 +01:00
|
|
|
{
|
2022-02-04 19:29:15 +00:00
|
|
|
auto calendar_like = vm.argument(0);
|
2021-08-01 11:40:03 +01:00
|
|
|
|
2022-02-04 19:29:15 +00:00
|
|
|
// 1. Return ? ToTemporalCalendar(calendarLike).
|
2022-08-20 08:52:42 +01:00
|
|
|
return TRY(to_temporal_calendar(vm, calendar_like));
|
2021-08-01 11:40:03 +01:00
|
|
|
}
|
|
|
|
|
|
2021-07-14 21:01:12 +01:00
|
|
|
}
|