SegmenterConstructor.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /*
  2. * Copyright (c) 2022, Idan Horowitz <idan.horowitz@serenityos.org>
  3. * Copyright (c) 2023, Tim Flynn <trflynn89@serenityos.org>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include <LibJS/Runtime/AbstractOperations.h>
  8. #include <LibJS/Runtime/Array.h>
  9. #include <LibJS/Runtime/GlobalObject.h>
  10. #include <LibJS/Runtime/Intl/AbstractOperations.h>
  11. #include <LibJS/Runtime/Intl/Segmenter.h>
  12. #include <LibJS/Runtime/Intl/SegmenterConstructor.h>
  13. #include <LibJS/Runtime/Temporal/AbstractOperations.h>
  14. namespace JS::Intl {
  15. // 18.1 The Intl.Segmenter Constructor, https://tc39.es/ecma402/#sec-intl-segmenter-constructor
  16. SegmenterConstructor::SegmenterConstructor(Realm& realm)
  17. : NativeFunction(realm.vm().names.Segmenter.as_string(), realm.intrinsics().function_prototype())
  18. {
  19. }
  20. void SegmenterConstructor::initialize(Realm& realm)
  21. {
  22. Base::initialize(realm);
  23. auto& vm = this->vm();
  24. // 18.2.1 Intl.Segmenter.prototype, https://tc39.es/ecma402/#sec-intl.segmenter.prototype
  25. define_direct_property(vm.names.prototype, realm.intrinsics().intl_segmenter_prototype(), 0);
  26. define_direct_property(vm.names.length, Value(0), Attribute::Configurable);
  27. u8 attr = Attribute::Writable | Attribute::Configurable;
  28. define_native_function(realm, vm.names.supportedLocalesOf, supported_locales_of, 1, attr);
  29. }
  30. // 18.1.1 Intl.Segmenter ( [ locales [ , options ] ] ), https://tc39.es/ecma402/#sec-intl.segmenter
  31. ThrowCompletionOr<Value> SegmenterConstructor::call()
  32. {
  33. // 1. If NewTarget is undefined, throw a TypeError exception.
  34. return vm().throw_completion<TypeError>(ErrorType::ConstructorWithoutNew, "Intl.Segmenter");
  35. }
  36. // 18.1.1 Intl.Segmenter ( [ locales [ , options ] ] ), https://tc39.es/ecma402/#sec-intl.segmenter
  37. ThrowCompletionOr<NonnullGCPtr<Object>> SegmenterConstructor::construct(FunctionObject& new_target)
  38. {
  39. auto& vm = this->vm();
  40. auto locales = vm.argument(0);
  41. auto options_value = vm.argument(1);
  42. // 2. Let internalSlotsList be « [[InitializedSegmenter]], [[Locale]], [[SegmenterGranularity]] ».
  43. // 3. Let segmenter be ? OrdinaryCreateFromConstructor(NewTarget, "%Segmenter.prototype%", internalSlotsList).
  44. auto segmenter = TRY(ordinary_create_from_constructor<Segmenter>(vm, new_target, &Intrinsics::intl_segmenter_prototype));
  45. // 4. Let requestedLocales be ? CanonicalizeLocaleList(locales).
  46. auto requested_locales = TRY(canonicalize_locale_list(vm, locales));
  47. // 5. Set options to ? GetOptionsObject(options).
  48. auto* options = TRY(Temporal::get_options_object(vm, options_value));
  49. // 6. Let opt be a new Record.
  50. LocaleOptions opt {};
  51. // 7. Let matcher be ? GetOption(options, "localeMatcher", string, « "lookup", "best fit" », "best fit").
  52. auto matcher = TRY(get_option(vm, *options, vm.names.localeMatcher, OptionType::String, { "lookup"sv, "best fit"sv }, "best fit"sv));
  53. // 8. Set opt.[[localeMatcher]] to matcher.
  54. opt.locale_matcher = matcher;
  55. // 9. Let localeData be %Segmenter%.[[LocaleData]].
  56. // 10. Let r be ResolveLocale(%Segmenter%.[[AvailableLocales]], requestedLocales, opt, %Segmenter%.[[RelevantExtensionKeys]], localeData).
  57. auto result = TRY(resolve_locale(vm, requested_locales, opt, {}));
  58. // 11. Set segmenter.[[Locale]] to r.[[locale]].
  59. segmenter->set_locale(move(result.locale));
  60. // 12. Let granularity be ? GetOption(options, "granularity", string, « "grapheme", "word", "sentence" », "grapheme").
  61. auto granularity = TRY(get_option(vm, *options, vm.names.granularity, OptionType::String, { "grapheme"sv, "word"sv, "sentence"sv }, "grapheme"sv));
  62. // 13. Set segmenter.[[SegmenterGranularity]] to granularity.
  63. segmenter->set_segmenter_granularity(granularity.as_string().utf8_string_view());
  64. // 14. Return segmenter.
  65. return segmenter;
  66. }
  67. // 18.2.2 Intl.Segmenter.supportedLocalesOf ( locales [ , options ] ), https://tc39.es/ecma402/#sec-intl.segmenter.supportedlocalesof
  68. JS_DEFINE_NATIVE_FUNCTION(SegmenterConstructor::supported_locales_of)
  69. {
  70. auto locales = vm.argument(0);
  71. auto options = vm.argument(1);
  72. // 1. Let availableLocales be %Segmenter%.[[AvailableLocales]].
  73. // 2. Let requestedLocales be ? CanonicalizeLocaleList(locales).
  74. auto requested_locales = TRY(canonicalize_locale_list(vm, locales));
  75. // 3. Return ? SupportedLocales(availableLocales, requestedLocales, options).
  76. return TRY(supported_locales(vm, requested_locales, options));
  77. }
  78. }