CalendarConstructor.cpp 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. /*
  2. * Copyright (c) 2021, Linus Groh <linusg@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibJS/Runtime/GlobalObject.h>
  7. #include <LibJS/Runtime/Temporal/Calendar.h>
  8. #include <LibJS/Runtime/Temporal/CalendarConstructor.h>
  9. namespace JS::Temporal {
  10. // 12.2 The Temporal.Calendar Constructor, https://tc39.es/proposal-temporal/#sec-temporal-calendar-constructor
  11. CalendarConstructor::CalendarConstructor(GlobalObject& global_object)
  12. : NativeFunction(vm().names.Calendar.as_string(), *global_object.function_prototype())
  13. {
  14. }
  15. void CalendarConstructor::initialize(GlobalObject& global_object)
  16. {
  17. NativeFunction::initialize(global_object);
  18. auto& vm = this->vm();
  19. // 12.3.1 Temporal.Calendar.prototype, https://tc39.es/proposal-temporal/#sec-temporal-calendar-prototype
  20. define_direct_property(vm.names.prototype, global_object.temporal_calendar_prototype(), 0);
  21. define_direct_property(vm.names.length, Value(1), Attribute::Configurable);
  22. }
  23. // 12.2.1 Temporal.Calendar ( id ), https://tc39.es/proposal-temporal/#sec-temporal.calendar
  24. Value CalendarConstructor::call()
  25. {
  26. auto& vm = this->vm();
  27. // 1. If NewTarget is undefined, then
  28. // a. Throw a TypeError exception.
  29. vm.throw_exception<TypeError>(global_object(), ErrorType::ConstructorWithoutNew, "Temporal.Calendar");
  30. return {};
  31. }
  32. // 12.2.1 Temporal.Calendar ( id ), https://tc39.es/proposal-temporal/#sec-temporal.calendar
  33. Value CalendarConstructor::construct(FunctionObject& new_target)
  34. {
  35. auto& vm = this->vm();
  36. auto& global_object = this->global_object();
  37. // 2. Set id to ? ToString(id).
  38. auto identifier = vm.argument(0).to_string(global_object);
  39. if (vm.exception())
  40. return {};
  41. // 3. If ! IsBuiltinCalendar(id) is false, then
  42. if (!is_builtin_calendar(identifier)) {
  43. // a. Throw a RangeError exception.
  44. vm.throw_exception<RangeError>(global_object, ErrorType::TemporalInvalidCalendarIdentifier, identifier);
  45. return {};
  46. }
  47. // 4. Return ? CreateTemporalCalendar(id, NewTarget).
  48. return create_temporal_calendar(global_object, identifier, &new_target);
  49. }
  50. }