CollatorConstructor.cpp 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. /*
  2. * Copyright (c) 2022, Tim Flynn <trflynn89@pm.me>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibJS/Runtime/AbstractOperations.h>
  7. #include <LibJS/Runtime/GlobalObject.h>
  8. #include <LibJS/Runtime/Intl/Collator.h>
  9. #include <LibJS/Runtime/Intl/CollatorConstructor.h>
  10. namespace JS::Intl {
  11. // 10.1 The Intl.Collator Constructor, https://tc39.es/ecma402/#sec-the-intl-collator-constructor
  12. CollatorConstructor::CollatorConstructor(GlobalObject& global_object)
  13. : NativeFunction(vm().names.Collator.as_string(), *global_object.function_prototype())
  14. {
  15. }
  16. void CollatorConstructor::initialize(GlobalObject& global_object)
  17. {
  18. NativeFunction::initialize(global_object);
  19. auto& vm = this->vm();
  20. // 10.2.1 Intl.Collator.prototype, https://tc39.es/ecma402/#sec-intl.collator.prototype
  21. define_direct_property(vm.names.prototype, global_object.intl_collator_prototype(), 0);
  22. define_direct_property(vm.names.length, Value(0), Attribute::Configurable);
  23. }
  24. // 10.1.2 Intl.Collator ( [ locales [ , options ] ] ), https://tc39.es/ecma402/#sec-intl.collator
  25. ThrowCompletionOr<Value> CollatorConstructor::call()
  26. {
  27. // 1. If NewTarget is undefined, let newTarget be the active function object, else let newTarget be NewTarget
  28. return TRY(construct(*this));
  29. }
  30. // 10.1.2 Intl.Collator ( [ locales [ , options ] ] ), https://tc39.es/ecma402/#sec-intl.collator
  31. ThrowCompletionOr<Object*> CollatorConstructor::construct(FunctionObject& new_target)
  32. {
  33. auto& global_object = this->global_object();
  34. // 2. Let internalSlotsList be « [[InitializedCollator]], [[Locale]], [[Usage]], [[Sensitivity]], [[IgnorePunctuation]], [[Collation]], [[BoundCompare]] ».
  35. // 3. If %Collator%.[[RelevantExtensionKeys]] contains "kn", then
  36. // a. Append [[Numeric]] as the last element of internalSlotsList.
  37. // 4. If %Collator%.[[RelevantExtensionKeys]] contains "kf", then
  38. // a. Append [[CaseFirst]] as the last element of internalSlotsList.
  39. // 5. Let collator be ? OrdinaryCreateFromConstructor(newTarget, "%Collator.prototype%", internalSlotsList).
  40. auto* collator = TRY(ordinary_create_from_constructor<Collator>(global_object, new_target, &GlobalObject::intl_collator_prototype));
  41. // 6. Return ? InitializeCollator(collator, locales, options).
  42. return collator;
  43. }
  44. }