SegmenterConstructor.cpp 4.4 KB

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