mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-05-04 10:18:51 +00:00
Some of these are allocated upon initialization of the intrinsics, and some lazily, but in neither case the getters actually return a nullptr. This saves us a whole bunch of pointer dereferences (as NonnullGCPtr has an `operator T&()`), and also has the interesting side effect of forcing us to explicitly use the FunctionObject& overload of call(), as passing a NonnullGCPtr is ambigous - it could implicitly be turned into a Value _or_ a FunctionObject& (so we have to dereference manually).
73 lines
2.5 KiB
C++
73 lines
2.5 KiB
C++
/*
|
|
* Copyright (c) 2021-2023, Linus Groh <linusg@serenityos.org>
|
|
*
|
|
* 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
|
|
CalendarConstructor::CalendarConstructor(Realm& realm)
|
|
: NativeFunction(realm.vm().names.Calendar.as_string(), realm.intrinsics().function_prototype())
|
|
{
|
|
}
|
|
|
|
ThrowCompletionOr<void> CalendarConstructor::initialize(Realm& realm)
|
|
{
|
|
MUST_OR_THROW_OOM(NativeFunction::initialize(realm));
|
|
|
|
auto& vm = this->vm();
|
|
|
|
// 12.3.1 Temporal.Calendar.prototype, https://tc39.es/proposal-temporal/#sec-temporal.calendar.prototype
|
|
define_direct_property(vm.names.prototype, realm.intrinsics().temporal_calendar_prototype(), 0);
|
|
|
|
u8 attr = Attribute::Writable | Attribute::Configurable;
|
|
define_native_function(realm, vm.names.from, from, 1, attr);
|
|
|
|
define_direct_property(vm.names.length, Value(1), Attribute::Configurable);
|
|
|
|
return {};
|
|
}
|
|
|
|
// 12.2.1 Temporal.Calendar ( id ), https://tc39.es/proposal-temporal/#sec-temporal.calendar
|
|
ThrowCompletionOr<Value> CalendarConstructor::call()
|
|
{
|
|
auto& vm = this->vm();
|
|
|
|
// 1. If NewTarget is undefined, then
|
|
// a. Throw a TypeError exception.
|
|
return vm.throw_completion<TypeError>(ErrorType::ConstructorWithoutNew, "Temporal.Calendar");
|
|
}
|
|
|
|
// 12.2.1 Temporal.Calendar ( id ), https://tc39.es/proposal-temporal/#sec-temporal.calendar
|
|
ThrowCompletionOr<NonnullGCPtr<Object>> CalendarConstructor::construct(FunctionObject& new_target)
|
|
{
|
|
auto& vm = this->vm();
|
|
|
|
// 2. Set id to ? ToString(id).
|
|
auto identifier = TRY(vm.argument(0).to_string(vm));
|
|
|
|
// 3. If IsBuiltinCalendar(id) is false, then
|
|
if (!is_builtin_calendar(identifier)) {
|
|
// a. Throw a RangeError exception.
|
|
return vm.throw_completion<RangeError>(ErrorType::TemporalInvalidCalendarIdentifier, identifier);
|
|
}
|
|
|
|
// 4. Return ? CreateTemporalCalendar(id, NewTarget).
|
|
return *TRY(create_temporal_calendar(vm, identifier, &new_target));
|
|
}
|
|
|
|
// 12.3.2 Temporal.Calendar.from ( calendarLike ), https://tc39.es/proposal-temporal/#sec-temporal.calendar.from
|
|
JS_DEFINE_NATIVE_FUNCTION(CalendarConstructor::from)
|
|
{
|
|
auto calendar_like = vm.argument(0);
|
|
|
|
// 1. Return ? ToTemporalCalendar(calendarLike).
|
|
return TRY(to_temporal_calendar(vm, calendar_like));
|
|
}
|
|
|
|
}
|