Source code

Revision control

Other Tools

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 "mozilla/Attributes.h"
11
12
#include "builtin/SelfHostingDefines.h"
13
#include "js/Class.h"
14
#include "js/RootingAPI.h"
15
#include "vm/NativeObject.h"
16
17
struct UFormattedNumber;
18
struct UNumberFormatter;
19
struct UPluralRules;
20
21
namespace js {
22
23
class PluralRulesObject : public NativeObject {
24
public:
25
static const JSClass class_;
26
static const JSClass& protoClass_;
27
28
static constexpr uint32_t INTERNALS_SLOT = 0;
29
static constexpr uint32_t UPLURAL_RULES_SLOT = 1;
30
static constexpr uint32_t UNUMBER_FORMATTER_SLOT = 2;
31
static constexpr uint32_t UFORMATTED_NUMBER_SLOT = 3;
32
static constexpr uint32_t SLOT_COUNT = 4;
33
34
static_assert(INTERNALS_SLOT == INTL_INTERNALS_OBJECT_SLOT,
35
"INTERNALS_SLOT must match self-hosting define for internals "
36
"object slot");
37
38
// Estimated memory use for UNumberFormatter and UFormattedNumber.
39
static constexpr size_t UNumberFormatterEstimatedMemoryUse = 750;
40
41
// Estimated memory use for UPluralRules.
42
static constexpr size_t UPluralRulesEstimatedMemoryUse = 2976;
43
44
UPluralRules* getPluralRules() const {
45
const auto& slot = getFixedSlot(UPLURAL_RULES_SLOT);
46
if (slot.isUndefined()) {
47
return nullptr;
48
}
49
return static_cast<UPluralRules*>(slot.toPrivate());
50
}
51
52
void setPluralRules(UPluralRules* pluralRules) {
53
setFixedSlot(UPLURAL_RULES_SLOT, PrivateValue(pluralRules));
54
}
55
56
UNumberFormatter* getNumberFormatter() const {
57
const auto& slot = getFixedSlot(UNUMBER_FORMATTER_SLOT);
58
if (slot.isUndefined()) {
59
return nullptr;
60
}
61
return static_cast<UNumberFormatter*>(slot.toPrivate());
62
}
63
64
void setNumberFormatter(UNumberFormatter* formatter) {
65
setFixedSlot(UNUMBER_FORMATTER_SLOT, PrivateValue(formatter));
66
}
67
68
UFormattedNumber* getFormattedNumber() const {
69
const auto& slot = getFixedSlot(UFORMATTED_NUMBER_SLOT);
70
if (slot.isUndefined()) {
71
return nullptr;
72
}
73
return static_cast<UFormattedNumber*>(slot.toPrivate());
74
}
75
76
void setFormattedNumber(UFormattedNumber* formatted) {
77
setFixedSlot(UFORMATTED_NUMBER_SLOT, PrivateValue(formatted));
78
}
79
80
private:
81
static const JSClassOps classOps_;
82
static const ClassSpec classSpec_;
83
84
static void finalize(JSFreeOp* fop, JSObject* obj);
85
};
86
87
/**
88
* Returns a plural rule for the number x according to the effective
89
* locale and the formatting options of the given PluralRules.
90
*
91
* A plural rule is a grammatical category that expresses count distinctions
92
* (such as "one", "two", "few" etc.).
93
*
94
* Usage: rule = intl_SelectPluralRule(pluralRules, x)
95
*/
96
extern MOZ_MUST_USE bool intl_SelectPluralRule(JSContext* cx, unsigned argc,
97
JS::Value* vp);
98
99
/**
100
* Returns an array of plural rules categories for a given pluralRules object.
101
*
102
* Usage: categories = intl_GetPluralCategories(pluralRules)
103
*
104
* Example:
105
*
106
* pluralRules = new Intl.PluralRules('pl', {type: 'cardinal'});
107
* intl_getPluralCategories(pluralRules); // ['one', 'few', 'many', 'other']
108
*/
109
extern MOZ_MUST_USE bool intl_GetPluralCategories(JSContext* cx, unsigned argc,
110
JS::Value* vp);
111
112
} // namespace js
113
114
#endif /* builtin_intl_PluralRules_h */