SegmenterConstructor.cpp 4.3 KB

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