TimeZoneConstructor.cpp 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. /*
  2. * Copyright (c) 2021-2022, 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/TimeZone.h>
  8. #include <LibJS/Runtime/Temporal/TimeZoneConstructor.h>
  9. namespace JS::Temporal {
  10. // 11.2 The Temporal.TimeZone Constructor, https://tc39.es/proposal-temporal/#sec-temporal-timezone-constructor
  11. TimeZoneConstructor::TimeZoneConstructor(GlobalObject& global_object)
  12. : NativeFunction(vm().names.TimeZone.as_string(), *global_object.function_prototype())
  13. {
  14. }
  15. void TimeZoneConstructor::initialize(GlobalObject& global_object)
  16. {
  17. NativeFunction::initialize(global_object);
  18. auto& vm = this->vm();
  19. // 11.3.1 Temporal.TimeZone.prototype, https://tc39.es/proposal-temporal/#sec-temporal.timezone.prototype
  20. define_direct_property(vm.names.prototype, global_object.temporal_time_zone_prototype(), 0);
  21. u8 attr = Attribute::Writable | Attribute::Configurable;
  22. define_native_function(vm.names.from, from, 1, attr);
  23. define_direct_property(vm.names.length, Value(1), Attribute::Configurable);
  24. }
  25. // 11.2.1 Temporal.TimeZone ( identifier ), https://tc39.es/proposal-temporal/#sec-temporal.timezone
  26. ThrowCompletionOr<Value> TimeZoneConstructor::call()
  27. {
  28. auto& vm = this->vm();
  29. // 1. If NewTarget is undefined, then
  30. // a. Throw a TypeError exception.
  31. return vm.throw_completion<TypeError>(global_object(), ErrorType::ConstructorWithoutNew, "Temporal.TimeZone");
  32. }
  33. // 11.2.1 Temporal.TimeZone ( identifier ), https://tc39.es/proposal-temporal/#sec-temporal.timezone
  34. ThrowCompletionOr<Object*> TimeZoneConstructor::construct(FunctionObject& new_target)
  35. {
  36. auto& vm = this->vm();
  37. auto& global_object = this->global_object();
  38. // 2. Set identifier to ? ToString(identifier).
  39. auto identifier = TRY(vm.argument(0).to_string(global_object));
  40. // 3. Let parseResult be ParseText(StringToCodePoints(identifier), TimeZoneNumericUTCOffset).
  41. // 4. If parseResult is a List of errors, then
  42. if (!is_valid_time_zone_numeric_utc_offset_syntax(identifier)) {
  43. // a. If IsValidTimeZoneName(identifier) is false, then
  44. if (!is_valid_time_zone_name(identifier)) {
  45. // i. Throw a RangeError exception.
  46. return vm.throw_completion<RangeError>(global_object, ErrorType::TemporalInvalidTimeZoneName, identifier);
  47. }
  48. // b. Set identifier to ! CanonicalizeTimeZoneName(identifier).
  49. identifier = canonicalize_time_zone_name(identifier);
  50. }
  51. // 5. Return ? CreateTemporalTimeZone(identifier, NewTarget).
  52. return TRY(create_temporal_time_zone(global_object, identifier, &new_target));
  53. }
  54. // 11.3.2 Temporal.TimeZone.from ( item ), https://tc39.es/proposal-temporal/#sec-temporal.timezone.from
  55. JS_DEFINE_NATIVE_FUNCTION(TimeZoneConstructor::from)
  56. {
  57. auto item = vm.argument(0);
  58. // 1. Return ? ToTemporalTimeZone(item).
  59. return TRY(to_temporal_time_zone(global_object, item));
  60. }
  61. }