TimeZoneConstructor.cpp 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. /*
  2. * Copyright (c) 2021-2023, 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. JS_DEFINE_ALLOCATOR(TimeZoneConstructor);
  12. // 11.2 The Temporal.TimeZone Constructor, https://tc39.es/proposal-temporal/#sec-temporal-timezone-constructor
  13. TimeZoneConstructor::TimeZoneConstructor(Realm& realm)
  14. : NativeFunction(realm.vm().names.TimeZone.as_string(), realm.intrinsics().function_prototype())
  15. {
  16. }
  17. void TimeZoneConstructor::initialize(Realm& realm)
  18. {
  19. Base::initialize(realm);
  20. auto& vm = this->vm();
  21. // 11.3.1 Temporal.TimeZone.prototype, https://tc39.es/proposal-temporal/#sec-temporal.timezone.prototype
  22. define_direct_property(vm.names.prototype, realm.intrinsics().temporal_time_zone_prototype(), 0);
  23. u8 attr = Attribute::Writable | Attribute::Configurable;
  24. define_native_function(realm, vm.names.from, from, 1, attr);
  25. define_direct_property(vm.names.length, Value(1), Attribute::Configurable);
  26. }
  27. // 11.2.1 Temporal.TimeZone ( identifier ), https://tc39.es/proposal-temporal/#sec-temporal.timezone
  28. ThrowCompletionOr<Value> TimeZoneConstructor::call()
  29. {
  30. auto& vm = this->vm();
  31. // 1. If NewTarget is undefined, then
  32. // a. Throw a TypeError exception.
  33. return vm.throw_completion<TypeError>(ErrorType::ConstructorWithoutNew, "Temporal.TimeZone");
  34. }
  35. // 11.2.1 Temporal.TimeZone ( identifier ), https://tc39.es/proposal-temporal/#sec-temporal.timezone
  36. ThrowCompletionOr<NonnullGCPtr<Object>> TimeZoneConstructor::construct(FunctionObject& new_target)
  37. {
  38. auto& vm = this->vm();
  39. // 2. Set identifier to ? ToString(identifier).
  40. auto identifier = TRY(vm.argument(0).to_string(vm));
  41. // 3. If IsTimeZoneOffsetString(identifier) is false, then
  42. if (!is_time_zone_offset_string(identifier)) {
  43. // a. If IsAvailableTimeZoneName(identifier) is false, then
  44. if (!is_available_time_zone_name(identifier)) {
  45. // i. Throw a RangeError exception.
  46. return vm.throw_completion<RangeError>(ErrorType::TemporalInvalidTimeZoneName, identifier);
  47. }
  48. // b. Set identifier to ! CanonicalizeTimeZoneName(identifier).
  49. identifier = MUST_OR_THROW_OOM(canonicalize_time_zone_name(vm, identifier));
  50. }
  51. // 4. Return ? CreateTemporalTimeZone(identifier, NewTarget).
  52. return *TRY(create_temporal_time_zone(vm, 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(vm, item));
  60. }
  61. }