RelativeTimeFormatConstructor.cpp 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. /*
  2. * Copyright (c) 2022, Tim Flynn <trflynn89@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibJS/Runtime/AbstractOperations.h>
  7. #include <LibJS/Runtime/Array.h>
  8. #include <LibJS/Runtime/GlobalObject.h>
  9. #include <LibJS/Runtime/Intl/AbstractOperations.h>
  10. #include <LibJS/Runtime/Intl/RelativeTimeFormat.h>
  11. #include <LibJS/Runtime/Intl/RelativeTimeFormatConstructor.h>
  12. namespace JS::Intl {
  13. // 17.2 The Intl.RelativeTimeFormat Constructor, https://tc39.es/ecma402/#sec-intl-relativetimeformat-constructor
  14. RelativeTimeFormatConstructor::RelativeTimeFormatConstructor(GlobalObject& global_object)
  15. : NativeFunction(vm().names.RelativeTimeFormat.as_string(), *global_object.function_prototype())
  16. {
  17. }
  18. void RelativeTimeFormatConstructor::initialize(GlobalObject& global_object)
  19. {
  20. NativeFunction::initialize(global_object);
  21. auto& vm = this->vm();
  22. // 17.3.1 Intl.RelativeTimeFormat.prototype, https://tc39.es/ecma402/#sec-Intl.RelativeTimeFormat.prototype
  23. define_direct_property(vm.names.prototype, global_object.intl_relative_time_format_prototype(), 0);
  24. define_direct_property(vm.names.length, Value(0), Attribute::Configurable);
  25. u8 attr = Attribute::Writable | Attribute::Configurable;
  26. define_native_function(vm.names.supportedLocalesOf, supported_locales_of, 1, attr);
  27. }
  28. // 17.2.1 Intl.RelativeTimeFormat ( [ locales [ , options ] ] ), https://tc39.es/ecma402/#sec-Intl.RelativeTimeFormat
  29. ThrowCompletionOr<Value> RelativeTimeFormatConstructor::call()
  30. {
  31. // 1. If NewTarget is undefined, throw a TypeError exception.
  32. return vm().throw_completion<TypeError>(global_object(), ErrorType::ConstructorWithoutNew, "Intl.RelativeTimeFormat");
  33. }
  34. // 17.2.1 Intl.RelativeTimeFormat ( [ locales [ , options ] ] ), https://tc39.es/ecma402/#sec-Intl.RelativeTimeFormat
  35. ThrowCompletionOr<Object*> RelativeTimeFormatConstructor::construct(FunctionObject& new_target)
  36. {
  37. auto& vm = this->vm();
  38. auto& global_object = this->global_object();
  39. auto locales = vm.argument(0);
  40. auto options = vm.argument(1);
  41. // 2. Let relativeTimeFormat be ? OrdinaryCreateFromConstructor(NewTarget, "%RelativeTimeFormat.prototype%", « [[InitializedRelativeTimeFormat]], [[Locale]], [[DataLocale]], [[Style]], [[Numeric]], [[NumberFormat]], [[NumberingSystem]], [[PluralRules]] »).
  42. auto* relative_time_format = TRY(ordinary_create_from_constructor<RelativeTimeFormat>(global_object, new_target, &GlobalObject::intl_relative_time_format_prototype));
  43. // 3. Return ? InitializeRelativeTimeFormat(relativeTimeFormat, locales, options).
  44. return TRY(initialize_relative_time_format(global_object, *relative_time_format, locales, options));
  45. }
  46. // 17.3.2 Intl.RelativeTimeFormat.supportedLocalesOf ( locales [ , options ] ), https://tc39.es/ecma402/#sec-Intl.RelativeTimeFormat.supportedLocalesOf
  47. JS_DEFINE_NATIVE_FUNCTION(RelativeTimeFormatConstructor::supported_locales_of)
  48. {
  49. auto locales = vm.argument(0);
  50. auto options = vm.argument(1);
  51. // 1. Let availableLocales be %RelativeTimeFormat%.[[AvailableLocales]].
  52. // 2. Let requestedLocales be ? CanonicalizeLocaleList(locales).
  53. auto requested_locales = TRY(canonicalize_locale_list(global_object, locales));
  54. // 3. Return ? SupportedLocales(availableLocales, requestedLocales, options).
  55. return TRY(supported_locales(global_object, requested_locales, options));
  56. }
  57. }