TimeZoneConstructor.cpp 2.7 KB

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