Collator.h (4118B)
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 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 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(JS::GCContext* gcx, JSObject* obj); 57 }; 58 59 /** 60 * Returns an array with the collation type identifiers per Unicode 61 * Technical Standard 35, Unicode Locale Data Markup Language, for the 62 * collations supported for the given locale. "standard" and "search" are 63 * excluded. 64 * 65 * Usage: collations = intl_availableCollations(locale) 66 */ 67 [[nodiscard]] extern bool intl_availableCollations(JSContext* cx, unsigned argc, 68 JS::Value* vp); 69 70 /** 71 * Compares x and y (which must be String values), and returns a number less 72 * than 0 if x < y, 0 if x = y, or a number greater than 0 if x > y according 73 * to the sort order for the locale and collation options of the given 74 * Collator. 75 * 76 * Spec: ECMAScript Internationalization API Specification, 10.3.2. 77 * 78 * Usage: result = intl_CompareStrings(collator, x, y) 79 */ 80 [[nodiscard]] extern bool intl_CompareStrings(JSContext* cx, unsigned argc, 81 JS::Value* vp); 82 83 /** 84 * Returns true if the given locale sorts upper-case before lower-case 85 * characters. 86 * 87 * Usage: result = intl_isUpperCaseFirst(locale) 88 */ 89 [[nodiscard]] extern bool intl_isUpperCaseFirst(JSContext* cx, unsigned argc, 90 JS::Value* vp); 91 92 /** 93 * Returns true if the given locale ignores punctuation by default. 94 * 95 * Usage: result = intl_isIgnorePunctuation(locale) 96 */ 97 [[nodiscard]] extern bool intl_isIgnorePunctuation(JSContext* cx, unsigned argc, 98 JS::Value* vp); 99 100 namespace intl { 101 102 /** 103 * Returns a new instance of the standard built-in Collator constructor. 104 */ 105 [[nodiscard]] extern CollatorObject* CreateCollator( 106 JSContext* cx, JS::Handle<JS::Value> locales, 107 JS::Handle<JS::Value> options); 108 109 /** 110 * Returns a possibly cached instance of the standard built-in Collator 111 * constructor. 112 */ 113 [[nodiscard]] extern CollatorObject* GetOrCreateCollator( 114 JSContext* cx, JS::Handle<JS::Value> locales, 115 JS::Handle<JS::Value> options); 116 117 [[nodiscard]] extern bool CompareStrings(JSContext* cx, 118 JS::Handle<CollatorObject*> collator, 119 JS::Handle<JSString*> str1, 120 JS::Handle<JSString*> str2, 121 JS::MutableHandle<JS::Value> result); 122 123 } // namespace intl 124 125 } // namespace js 126 127 #endif /* builtin_intl_Collator_h */