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