PluralRules.h (3343B)
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_PluralRules_h 8 #define builtin_intl_PluralRules_h 9 10 #include "builtin/SelfHostingDefines.h" 11 #include "js/Class.h" 12 #include "vm/NativeObject.h" 13 14 namespace mozilla::intl { 15 class PluralRules; 16 } 17 18 namespace js { 19 20 class PluralRulesObject : public NativeObject { 21 public: 22 static const JSClass class_; 23 static const JSClass& protoClass_; 24 25 static constexpr uint32_t INTERNALS_SLOT = 0; 26 static constexpr uint32_t PLURAL_RULES_SLOT = 1; 27 static constexpr uint32_t SLOT_COUNT = 2; 28 29 static_assert(INTERNALS_SLOT == INTL_INTERNALS_OBJECT_SLOT, 30 "INTERNALS_SLOT must match self-hosting define for internals " 31 "object slot"); 32 33 // Estimated memory use for UPluralRules (see IcuMemoryUsage). 34 // Includes usage for UNumberFormat and UNumberRangeFormatter since our 35 // PluralRules implementations contains a NumberFormat and a NumberRangeFormat 36 // object. 37 static constexpr size_t UPluralRulesEstimatedMemoryUse = 5736; 38 39 mozilla::intl::PluralRules* getPluralRules() const { 40 const auto& slot = getFixedSlot(PLURAL_RULES_SLOT); 41 if (slot.isUndefined()) { 42 return nullptr; 43 } 44 return static_cast<mozilla::intl::PluralRules*>(slot.toPrivate()); 45 } 46 47 void setPluralRules(mozilla::intl::PluralRules* pluralRules) { 48 setFixedSlot(PLURAL_RULES_SLOT, PrivateValue(pluralRules)); 49 } 50 51 private: 52 static const JSClassOps classOps_; 53 static const ClassSpec classSpec_; 54 55 static void finalize(JS::GCContext* gcx, JSObject* obj); 56 }; 57 58 /** 59 * Returns a plural rule for the number x according to the effective 60 * locale and the formatting options of the given PluralRules. 61 * 62 * A plural rule is a grammatical category that expresses count distinctions 63 * (such as "one", "two", "few" etc.). 64 * 65 * Usage: rule = intl_SelectPluralRule(pluralRules, x) 66 */ 67 [[nodiscard]] extern bool intl_SelectPluralRule(JSContext* cx, unsigned argc, 68 JS::Value* vp); 69 70 /** 71 * Returns a plural rule for the number range «x - y» according to the effective 72 * locale and the formatting options of the given PluralRules. 73 * 74 * A plural rule is a grammatical category that expresses count distinctions 75 * (such as "one", "two", "few" etc.). 76 * 77 * Usage: rule = intl_SelectPluralRuleRange(pluralRules, x, y) 78 */ 79 [[nodiscard]] extern bool intl_SelectPluralRuleRange(JSContext* cx, 80 unsigned argc, 81 JS::Value* vp); 82 83 /** 84 * Returns an array of plural rules categories for a given pluralRules object. 85 * 86 * Usage: categories = intl_GetPluralCategories(pluralRules) 87 * 88 * Example: 89 * 90 * pluralRules = new Intl.PluralRules('pl', {type: 'cardinal'}); 91 * intl_getPluralCategories(pluralRules); // ['one', 'few', 'many', 'other'] 92 */ 93 [[nodiscard]] extern bool intl_GetPluralCategories(JSContext* cx, unsigned argc, 94 JS::Value* vp); 95 96 } // namespace js 97 98 #endif /* builtin_intl_PluralRules_h */