CollatorCompareFunction.cpp 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. /*
  2. * Copyright (c) 2022, Idan Horowitz <idan.horowitz@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/Utf8View.h>
  7. #include <LibJS/Runtime/GlobalObject.h>
  8. #include <LibJS/Runtime/Intl/Collator.h>
  9. #include <LibJS/Runtime/Intl/CollatorCompareFunction.h>
  10. namespace JS::Intl {
  11. JS_DEFINE_ALLOCATOR(CollatorCompareFunction);
  12. NonnullGCPtr<CollatorCompareFunction> CollatorCompareFunction::create(Realm& realm, Collator& collator)
  13. {
  14. return realm.create<CollatorCompareFunction>(realm, collator);
  15. }
  16. CollatorCompareFunction::CollatorCompareFunction(Realm& realm, Collator& collator)
  17. : NativeFunction(realm.intrinsics().function_prototype())
  18. , m_collator(collator)
  19. {
  20. }
  21. void CollatorCompareFunction::initialize(Realm&)
  22. {
  23. auto& vm = this->vm();
  24. define_direct_property(vm.names.length, Value(2), Attribute::Configurable);
  25. define_direct_property(vm.names.name, PrimitiveString::create(vm, String {}), Attribute::Configurable);
  26. }
  27. void CollatorCompareFunction::visit_edges(Visitor& visitor)
  28. {
  29. Base::visit_edges(visitor);
  30. visitor.visit(m_collator);
  31. }
  32. // 10.3.3.1 Collator Compare Functions, https://tc39.es/ecma402/#sec-collator-compare-functions
  33. ThrowCompletionOr<Value> CollatorCompareFunction::call()
  34. {
  35. auto& vm = this->vm();
  36. // 1. Let collator be F.[[Collator]].
  37. // 2. Assert: Type(collator) is Object and collator has an [[InitializedCollator]] internal slot.
  38. // 3. If x is not provided, let x be undefined.
  39. // 4. If y is not provided, let y be undefined.
  40. // 5. Let X be ? ToString(x).
  41. auto x = TRY(vm.argument(0).to_string(vm));
  42. // 6. Let Y be ? ToString(y).
  43. auto y = TRY(vm.argument(1).to_string(vm));
  44. // 7. Return CompareStrings(collator, X, Y).
  45. return compare_strings(m_collator, x, y);
  46. }
  47. // 10.3.3.2 CompareStrings ( collator, x, y ), https://tc39.es/ecma402/#sec-collator-comparestrings
  48. int compare_strings(Collator const& collator, StringView x, StringView y)
  49. {
  50. auto result = collator.collator().compare(x, y);
  51. // The result is intended to correspond with a sort order of String values according to the effective locale and
  52. // collation options of collator, and will be negative when x is ordered before y, positive when x is ordered after
  53. // y, and zero in all other cases (representing no relative ordering between x and y).
  54. switch (result) {
  55. case Unicode::Collator::Order::Before:
  56. return -1;
  57. case Unicode::Collator::Order::Equal:
  58. return 0;
  59. case Unicode::Collator::Order::After:
  60. return 1;
  61. }
  62. VERIFY_NOT_REACHED();
  63. }
  64. }