1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2  * vim: set ts=8 sts=2 et sw=2 tw=80:
3  * This Source Code Form is subject to the terms of the Mozilla Public
4  * License, v. 2.0. If a copy of the MPL was not distributed with this
5  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 
7 #ifndef builtin_intl_Collator_h
8 #define builtin_intl_Collator_h
9 
10 #include <stdint.h>
11 
12 #include "builtin/SelfHostingDefines.h"
13 #include "js/Class.h"
14 #include "vm/NativeObject.h"
15 
16 namespace mozilla::intl {
17 class Collator;
18 }
19 
20 namespace js {
21 
22 /******************** Collator ********************/
23 
24 class CollatorObject : public NativeObject {
25  public:
26   static const JSClass class_;
27   static const JSClass& protoClass_;
28 
29   static constexpr uint32_t INTERNALS_SLOT = 0;
30   static constexpr uint32_t INTL_COLLATOR_SLOT = 1;
31   static constexpr uint32_t SLOT_COUNT = 2;
32 
33   static_assert(INTERNALS_SLOT == INTL_INTERNALS_OBJECT_SLOT,
34                 "INTERNALS_SLOT must match self-hosting define for internals "
35                 "object slot");
36 
37   // Estimated memory use for UCollator (see IcuMemoryUsage).
38   static constexpr size_t EstimatedMemoryUse = 1128;
39 
getCollator()40   mozilla::intl::Collator* getCollator() const {
41     const auto& slot = getFixedSlot(INTL_COLLATOR_SLOT);
42     if (slot.isUndefined()) {
43       return nullptr;
44     }
45     return static_cast<mozilla::intl::Collator*>(slot.toPrivate());
46   }
47 
setCollator(mozilla::intl::Collator * collator)48   void setCollator(mozilla::intl::Collator* collator) {
49     setFixedSlot(INTL_COLLATOR_SLOT, PrivateValue(collator));
50   }
51 
52  private:
53   static const JSClassOps classOps_;
54   static const ClassSpec classSpec_;
55 
56   static void finalize(JSFreeOp* fop, JSObject* obj);
57 };
58 
59 /**
60  * Returns a new instance of the standard built-in Collator constructor.
61  * Self-hosted code cannot cache this constructor (as it does for others in
62  * Utilities.js) because it is initialized after self-hosted code is compiled.
63  *
64  * Usage: collator = intl_Collator(locales, options)
65  */
66 [[nodiscard]] extern bool intl_Collator(JSContext* cx, unsigned argc,
67                                         JS::Value* vp);
68 
69 /**
70  * Returns an array with the collation type identifiers per Unicode
71  * Technical Standard 35, Unicode Locale Data Markup Language, for the
72  * collations supported for the given locale. "standard" and "search" are
73  * excluded.
74  *
75  * Usage: collations = intl_availableCollations(locale)
76  */
77 [[nodiscard]] extern bool intl_availableCollations(JSContext* cx, unsigned argc,
78                                                    JS::Value* vp);
79 
80 /**
81  * Compares x and y (which must be String values), and returns a number less
82  * than 0 if x < y, 0 if x = y, or a number greater than 0 if x > y according
83  * to the sort order for the locale and collation options of the given
84  * Collator.
85  *
86  * Spec: ECMAScript Internationalization API Specification, 10.3.2.
87  *
88  * Usage: result = intl_CompareStrings(collator, x, y)
89  */
90 [[nodiscard]] extern bool intl_CompareStrings(JSContext* cx, unsigned argc,
91                                               JS::Value* vp);
92 
93 /**
94  * Returns true if the given locale sorts upper-case before lower-case
95  * characters.
96  *
97  * Usage: result = intl_isUpperCaseFirst(locale)
98  */
99 [[nodiscard]] extern bool intl_isUpperCaseFirst(JSContext* cx, unsigned argc,
100                                                 JS::Value* vp);
101 
102 }  // namespace js
103 
104 #endif /* builtin_intl_Collator_h */
105