зеркало из https://github.com/mozilla/gecko-dev.git
Bug 1726141 : Add GetOrCreate helper functions. r=anba
Differential Revision: https://phabricator.services.mozilla.com/D126705
This commit is contained in:
Родитель
7e53f41285
Коммит
ae1dc1dcf2
|
@ -351,6 +351,24 @@ static mozilla::intl::Collator* NewIntlCollator(
|
|||
return coll.release();
|
||||
}
|
||||
|
||||
static mozilla::intl::Collator* GetOrCreateCollator(
|
||||
JSContext* cx, Handle<CollatorObject*> collator) {
|
||||
// Obtain a cached mozilla::intl::Collator object.
|
||||
mozilla::intl::Collator* coll = collator->getCollator();
|
||||
if (coll) {
|
||||
return coll;
|
||||
}
|
||||
|
||||
coll = NewIntlCollator(cx, collator);
|
||||
if (!coll) {
|
||||
return nullptr;
|
||||
}
|
||||
collator->setCollator(coll);
|
||||
|
||||
intl::AddICUCellMemory(collator, CollatorObject::EstimatedMemoryUse);
|
||||
return coll;
|
||||
}
|
||||
|
||||
static bool intl_CompareStrings(JSContext* cx, mozilla::intl::Collator* coll,
|
||||
HandleString str1, HandleString str2,
|
||||
MutableHandleValue result) {
|
||||
|
@ -389,16 +407,9 @@ bool js::intl_CompareStrings(JSContext* cx, unsigned argc, Value* vp) {
|
|||
Rooted<CollatorObject*> collator(cx,
|
||||
&args[0].toObject().as<CollatorObject>());
|
||||
|
||||
// Obtain a cached mozilla::intl::Collator object.
|
||||
mozilla::intl::Collator* coll = collator->getCollator();
|
||||
mozilla::intl::Collator* coll = GetOrCreateCollator(cx, collator);
|
||||
if (!coll) {
|
||||
coll = NewIntlCollator(cx, collator);
|
||||
if (!coll) {
|
||||
return false;
|
||||
}
|
||||
collator->setCollator(coll);
|
||||
|
||||
intl::AddICUCellMemory(collator, CollatorObject::EstimatedMemoryUse);
|
||||
return false;
|
||||
}
|
||||
|
||||
// Use the UCollator to actually compare the strings.
|
||||
|
|
|
@ -985,6 +985,25 @@ static mozilla::intl::DateTimeFormat* NewDateTimeFormat(
|
|||
return df.release();
|
||||
}
|
||||
|
||||
static mozilla::intl::DateTimeFormat* GetOrCreateDateTimeFormat(
|
||||
JSContext* cx, Handle<DateTimeFormatObject*> dateTimeFormat) {
|
||||
// Obtain a cached mozilla::intl::DateTimeFormat object.
|
||||
mozilla::intl::DateTimeFormat* df = dateTimeFormat->getDateFormat();
|
||||
if (df) {
|
||||
return df;
|
||||
}
|
||||
|
||||
df = NewDateTimeFormat(cx, dateTimeFormat);
|
||||
if (!df) {
|
||||
return nullptr;
|
||||
}
|
||||
dateTimeFormat->setDateFormat(df);
|
||||
|
||||
intl::AddICUCellMemory(dateTimeFormat,
|
||||
DateTimeFormatObject::UDateFormatEstimatedMemoryUse);
|
||||
return df;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
static bool SetResolvedProperty(JSContext* cx, HandleObject resolved,
|
||||
HandlePropertyName name,
|
||||
|
@ -1016,17 +1035,10 @@ bool js::intl_resolveDateTimeFormatComponents(JSContext* cx, unsigned argc,
|
|||
|
||||
bool includeDateTimeFields = args[2].toBoolean();
|
||||
|
||||
// Obtain a cached mozilla::intl::DateTimeFormat object.
|
||||
mozilla::intl::DateTimeFormat* df = dateTimeFormat->getDateFormat();
|
||||
mozilla::intl::DateTimeFormat* df =
|
||||
GetOrCreateDateTimeFormat(cx, dateTimeFormat);
|
||||
if (!df) {
|
||||
df = NewDateTimeFormat(cx, dateTimeFormat);
|
||||
if (!df) {
|
||||
return false;
|
||||
}
|
||||
dateTimeFormat->setDateFormat(df);
|
||||
|
||||
intl::AddICUCellMemory(dateTimeFormat,
|
||||
DateTimeFormatObject::UDateFormatEstimatedMemoryUse);
|
||||
return false;
|
||||
}
|
||||
|
||||
auto result = df->ResolveComponents();
|
||||
|
@ -1295,17 +1307,10 @@ bool js::intl_FormatDateTime(JSContext* cx, unsigned argc, Value* vp) {
|
|||
return false;
|
||||
}
|
||||
|
||||
// Obtain a cached DateTimeFormat object.
|
||||
mozilla::intl::DateTimeFormat* df = dateTimeFormat->getDateFormat();
|
||||
mozilla::intl::DateTimeFormat* df =
|
||||
GetOrCreateDateTimeFormat(cx, dateTimeFormat);
|
||||
if (!df) {
|
||||
df = NewDateTimeFormat(cx, dateTimeFormat);
|
||||
if (!df) {
|
||||
return false;
|
||||
}
|
||||
dateTimeFormat->setDateFormat(df);
|
||||
|
||||
intl::AddICUCellMemory(dateTimeFormat,
|
||||
DateTimeFormatObject::UDateFormatEstimatedMemoryUse);
|
||||
return false;
|
||||
}
|
||||
|
||||
// Use the DateTimeFormat to actually format the time stamp.
|
||||
|
@ -1373,6 +1378,28 @@ static mozilla::intl::DateIntervalFormat* NewDateIntervalFormat(
|
|||
return dif.unwrap().release();
|
||||
}
|
||||
|
||||
static mozilla::intl::DateIntervalFormat* GetOrCreateDateIntervalFormat(
|
||||
JSContext* cx, Handle<DateTimeFormatObject*> dateTimeFormat,
|
||||
mozilla::intl::DateTimeFormat& mozDtf) {
|
||||
// Obtain a cached DateIntervalFormat object.
|
||||
mozilla::intl::DateIntervalFormat* dif =
|
||||
dateTimeFormat->getDateIntervalFormat();
|
||||
if (dif) {
|
||||
return dif;
|
||||
}
|
||||
|
||||
dif = NewDateIntervalFormat(cx, dateTimeFormat, mozDtf);
|
||||
if (!dif) {
|
||||
return nullptr;
|
||||
}
|
||||
dateTimeFormat->setDateIntervalFormat(dif);
|
||||
|
||||
intl::AddICUCellMemory(
|
||||
dateTimeFormat,
|
||||
DateTimeFormatObject::UDateIntervalFormatEstimatedMemoryUse);
|
||||
return dif;
|
||||
}
|
||||
|
||||
/**
|
||||
* PartitionDateTimeRangePattern ( dateTimeFormat, x, y )
|
||||
*/
|
||||
|
@ -1545,32 +1572,16 @@ bool js::intl_FormatDateTimeRange(JSContext* cx, unsigned argc, Value* vp) {
|
|||
MOZ_ASSERT(x.toDouble() <= y.toDouble(),
|
||||
"start date mustn't be after the end date");
|
||||
|
||||
// Obtain a cached mozilla::intl::DateTimeFormat object.
|
||||
mozilla::intl::DateTimeFormat* df = dateTimeFormat->getDateFormat();
|
||||
mozilla::intl::DateTimeFormat* df =
|
||||
GetOrCreateDateTimeFormat(cx, dateTimeFormat);
|
||||
if (!df) {
|
||||
df = NewDateTimeFormat(cx, dateTimeFormat);
|
||||
if (!df) {
|
||||
return false;
|
||||
}
|
||||
dateTimeFormat->setDateFormat(df);
|
||||
|
||||
intl::AddICUCellMemory(dateTimeFormat,
|
||||
DateTimeFormatObject::UDateFormatEstimatedMemoryUse);
|
||||
return false;
|
||||
}
|
||||
|
||||
// Obtain a cached DateIntervalFormat object.
|
||||
mozilla::intl::DateIntervalFormat* dif =
|
||||
dateTimeFormat->getDateIntervalFormat();
|
||||
GetOrCreateDateIntervalFormat(cx, dateTimeFormat, *df);
|
||||
if (!dif) {
|
||||
dif = NewDateIntervalFormat(cx, dateTimeFormat, *df);
|
||||
if (!dif) {
|
||||
return false;
|
||||
}
|
||||
dateTimeFormat->setDateIntervalFormat(dif);
|
||||
|
||||
intl::AddICUCellMemory(
|
||||
dateTimeFormat,
|
||||
DateTimeFormatObject::UDateIntervalFormatEstimatedMemoryUse);
|
||||
return false;
|
||||
}
|
||||
|
||||
// Use the DateIntervalFormat to actually format the time range.
|
||||
|
|
|
@ -210,6 +210,24 @@ static mozilla::intl::ListFormat* NewListFormat(
|
|||
return nullptr;
|
||||
}
|
||||
|
||||
static mozilla::intl::ListFormat* GetOrCreateListFormat(
|
||||
JSContext* cx, Handle<ListFormatObject*> listFormat) {
|
||||
// Obtain a cached mozilla::intl::ListFormat object.
|
||||
mozilla::intl::ListFormat* lf = listFormat->getListFormatSlot();
|
||||
if (lf) {
|
||||
return lf;
|
||||
}
|
||||
|
||||
lf = NewListFormat(cx, listFormat);
|
||||
if (!lf) {
|
||||
return nullptr;
|
||||
}
|
||||
listFormat->setListFormatSlot(lf);
|
||||
|
||||
intl::AddICUCellMemory(listFormat, ListFormatObject::EstimatedMemoryUse);
|
||||
return lf;
|
||||
}
|
||||
|
||||
/**
|
||||
* FormatList ( listFormat, list )
|
||||
*/
|
||||
|
@ -300,16 +318,9 @@ bool js::intl_FormatList(JSContext* cx, unsigned argc, Value* vp) {
|
|||
|
||||
bool formatToParts = args[2].toBoolean();
|
||||
|
||||
// Obtain a cached mozilla::intl::ListFormat object.
|
||||
mozilla::intl::ListFormat* lf = listFormat->getListFormatSlot();
|
||||
mozilla::intl::ListFormat* lf = GetOrCreateListFormat(cx, listFormat);
|
||||
if (!lf) {
|
||||
lf = NewListFormat(cx, listFormat);
|
||||
if (!lf) {
|
||||
return false;
|
||||
}
|
||||
listFormat->setListFormatSlot(lf);
|
||||
|
||||
intl::AddICUCellMemory(listFormat, ListFormatObject::EstimatedMemoryUse);
|
||||
return false;
|
||||
}
|
||||
|
||||
// Collect all strings and their lengths.
|
||||
|
|
|
@ -778,6 +778,44 @@ static Formatter* NewNumberFormat(JSContext* cx,
|
|||
return nullptr;
|
||||
}
|
||||
|
||||
static mozilla::intl::NumberFormat* GetOrCreateNumberFormat(
|
||||
JSContext* cx, Handle<NumberFormatObject*> numberFormat) {
|
||||
// Obtain a cached mozilla::intl::NumberFormat object.
|
||||
mozilla::intl::NumberFormat* nf = numberFormat->getNumberFormatter();
|
||||
if (nf) {
|
||||
return nf;
|
||||
}
|
||||
|
||||
nf = NewNumberFormat<mozilla::intl::NumberFormat>(cx, numberFormat);
|
||||
if (!nf) {
|
||||
return nullptr;
|
||||
}
|
||||
numberFormat->setNumberFormatter(nf);
|
||||
|
||||
intl::AddICUCellMemory(numberFormat, NumberFormatObject::EstimatedMemoryUse);
|
||||
return nf;
|
||||
}
|
||||
|
||||
static mozilla::intl::NumberRangeFormat* GetOrCreateNumberRangeFormat(
|
||||
JSContext* cx, Handle<NumberFormatObject*> numberFormat) {
|
||||
// Obtain a cached mozilla::intl::NumberRangeFormat object.
|
||||
mozilla::intl::NumberRangeFormat* nrf =
|
||||
numberFormat->getNumberRangeFormatter();
|
||||
if (nrf) {
|
||||
return nrf;
|
||||
}
|
||||
|
||||
nrf = NewNumberFormat<mozilla::intl::NumberRangeFormat>(cx, numberFormat);
|
||||
if (!nrf) {
|
||||
return nullptr;
|
||||
}
|
||||
numberFormat->setNumberRangeFormatter(nrf);
|
||||
|
||||
intl::AddICUCellMemory(numberFormat,
|
||||
NumberFormatObject::EstimatedRangeFormatterMemoryUse);
|
||||
return nrf;
|
||||
}
|
||||
|
||||
static FieldType GetFieldTypeForNumberPartType(
|
||||
mozilla::intl::NumberPartType type) {
|
||||
switch (type) {
|
||||
|
@ -1126,17 +1164,9 @@ bool js::intl_FormatNumber(JSContext* cx, unsigned argc, Value* vp) {
|
|||
}
|
||||
#endif
|
||||
|
||||
// Obtain a cached mozilla::intl::NumberFormat object.
|
||||
mozilla::intl::NumberFormat* nf = numberFormat->getNumberFormatter();
|
||||
mozilla::intl::NumberFormat* nf = GetOrCreateNumberFormat(cx, numberFormat);
|
||||
if (!nf) {
|
||||
nf = NewNumberFormat<mozilla::intl::NumberFormat>(cx, numberFormat);
|
||||
if (!nf) {
|
||||
return false;
|
||||
}
|
||||
numberFormat->setNumberFormatter(nf);
|
||||
|
||||
intl::AddICUCellMemory(numberFormat,
|
||||
NumberFormatObject::EstimatedMemoryUse);
|
||||
return false;
|
||||
}
|
||||
|
||||
// Actually format the number
|
||||
|
@ -1466,18 +1496,10 @@ bool js::intl_FormatNumberRange(JSContext* cx, unsigned argc, Value* vp) {
|
|||
return false;
|
||||
}
|
||||
|
||||
// Obtain a cached mozilla::intl::NumberFormat object.
|
||||
using NumberRangeFormat = mozilla::intl::NumberRangeFormat;
|
||||
NumberRangeFormat* nf = numberFormat->getNumberRangeFormatter();
|
||||
NumberRangeFormat* nf = GetOrCreateNumberRangeFormat(cx, numberFormat);
|
||||
if (!nf) {
|
||||
nf = NewNumberFormat<NumberRangeFormat>(cx, numberFormat);
|
||||
if (!nf) {
|
||||
return false;
|
||||
}
|
||||
numberFormat->setNumberRangeFormatter(nf);
|
||||
|
||||
intl::AddICUCellMemory(
|
||||
numberFormat, NumberFormatObject::EstimatedRangeFormatterMemoryUse);
|
||||
return false;
|
||||
}
|
||||
|
||||
auto valueRepresentableAsDouble = [](const Value& val, double* num) {
|
||||
|
|
|
@ -291,6 +291,25 @@ static mozilla::intl::PluralRules* NewPluralRules(
|
|||
return result.unwrap().release();
|
||||
}
|
||||
|
||||
static mozilla::intl::PluralRules* GetOrCreatePluralRules(
|
||||
JSContext* cx, Handle<PluralRulesObject*> pluralRules) {
|
||||
// Obtain a cached PluralRules object.
|
||||
mozilla::intl::PluralRules* pr = pluralRules->getPluralRules();
|
||||
if (pr) {
|
||||
return pr;
|
||||
}
|
||||
|
||||
pr = NewPluralRules(cx, pluralRules);
|
||||
if (!pr) {
|
||||
return nullptr;
|
||||
}
|
||||
pluralRules->setPluralRules(pr);
|
||||
|
||||
intl::AddICUCellMemory(pluralRules,
|
||||
PluralRulesObject::UPluralRulesEstimatedMemoryUse);
|
||||
return pr;
|
||||
}
|
||||
|
||||
bool js::intl_SelectPluralRule(JSContext* cx, unsigned argc, Value* vp) {
|
||||
CallArgs args = CallArgsFromVp(argc, vp);
|
||||
MOZ_ASSERT(args.length() == 2);
|
||||
|
@ -300,18 +319,10 @@ bool js::intl_SelectPluralRule(JSContext* cx, unsigned argc, Value* vp) {
|
|||
|
||||
double x = args[1].toNumber();
|
||||
|
||||
// Obtain a cached PluralRules object.
|
||||
using PluralRules = mozilla::intl::PluralRules;
|
||||
PluralRules* pr = pluralRules->getPluralRules();
|
||||
PluralRules* pr = GetOrCreatePluralRules(cx, pluralRules);
|
||||
if (!pr) {
|
||||
pr = NewPluralRules(cx, pluralRules);
|
||||
if (!pr) {
|
||||
return false;
|
||||
}
|
||||
pluralRules->setPluralRules(pr);
|
||||
|
||||
intl::AddICUCellMemory(pluralRules,
|
||||
PluralRulesObject::UPluralRulesEstimatedMemoryUse);
|
||||
return false;
|
||||
}
|
||||
|
||||
auto keywordResult = pr->Select(x);
|
||||
|
@ -345,18 +356,10 @@ bool js::intl_SelectPluralRuleRange(JSContext* cx, unsigned argc, Value* vp) {
|
|||
return false;
|
||||
}
|
||||
|
||||
// Obtain a cached PluralRules object.
|
||||
using PluralRules = mozilla::intl::PluralRules;
|
||||
PluralRules* pr = pluralRules->getPluralRules();
|
||||
PluralRules* pr = GetOrCreatePluralRules(cx, pluralRules);
|
||||
if (!pr) {
|
||||
pr = NewPluralRules(cx, pluralRules);
|
||||
if (!pr) {
|
||||
return false;
|
||||
}
|
||||
pluralRules->setPluralRules(pr);
|
||||
|
||||
intl::AddICUCellMemory(pluralRules,
|
||||
PluralRulesObject::UPluralRulesEstimatedMemoryUse);
|
||||
return false;
|
||||
}
|
||||
|
||||
auto keywordResult = pr->SelectRange(x, y);
|
||||
|
@ -382,18 +385,10 @@ bool js::intl_GetPluralCategories(JSContext* cx, unsigned argc, Value* vp) {
|
|||
Rooted<PluralRulesObject*> pluralRules(
|
||||
cx, &args[0].toObject().as<PluralRulesObject>());
|
||||
|
||||
// Obtain a cached PluralRules object.
|
||||
using PluralRules = mozilla::intl::PluralRules;
|
||||
PluralRules* pr = pluralRules->getPluralRules();
|
||||
PluralRules* pr = GetOrCreatePluralRules(cx, pluralRules);
|
||||
if (!pr) {
|
||||
pr = NewPluralRules(cx, pluralRules);
|
||||
if (!pr) {
|
||||
return false;
|
||||
}
|
||||
pluralRules->setPluralRules(pr);
|
||||
|
||||
intl::AddICUCellMemory(pluralRules,
|
||||
PluralRulesObject::UPluralRulesEstimatedMemoryUse);
|
||||
return false;
|
||||
}
|
||||
|
||||
auto categoriesResult = pr->Categories();
|
||||
|
|
|
@ -267,6 +267,26 @@ static mozilla::intl::RelativeTimeFormat* NewRelativeTimeFormatter(
|
|||
return nullptr;
|
||||
}
|
||||
|
||||
static mozilla::intl::RelativeTimeFormat* GetOrCreateRelativeTimeFormat(
|
||||
JSContext* cx, Handle<RelativeTimeFormatObject*> relativeTimeFormat) {
|
||||
// Obtain a cached RelativeDateTimeFormatter object.
|
||||
mozilla::intl::RelativeTimeFormat* rtf =
|
||||
relativeTimeFormat->getRelativeTimeFormatter();
|
||||
if (rtf) {
|
||||
return rtf;
|
||||
}
|
||||
|
||||
rtf = NewRelativeTimeFormatter(cx, relativeTimeFormat);
|
||||
if (!rtf) {
|
||||
return nullptr;
|
||||
}
|
||||
relativeTimeFormat->setRelativeTimeFormatter(rtf);
|
||||
|
||||
intl::AddICUCellMemory(relativeTimeFormat,
|
||||
RelativeTimeFormatObject::EstimatedMemoryUse);
|
||||
return rtf;
|
||||
}
|
||||
|
||||
bool js::intl_FormatRelativeTime(JSContext* cx, unsigned argc, Value* vp) {
|
||||
CallArgs args = CallArgsFromVp(argc, vp);
|
||||
MOZ_ASSERT(args.length() == 4);
|
||||
|
@ -289,18 +309,10 @@ bool js::intl_FormatRelativeTime(JSContext* cx, unsigned argc, Value* vp) {
|
|||
return false;
|
||||
}
|
||||
|
||||
// Obtain a cached URelativeDateTimeFormatter object.
|
||||
mozilla::intl::RelativeTimeFormat* rtf =
|
||||
relativeTimeFormat->getRelativeTimeFormatter();
|
||||
GetOrCreateRelativeTimeFormat(cx, relativeTimeFormat);
|
||||
if (!rtf) {
|
||||
rtf = NewRelativeTimeFormatter(cx, relativeTimeFormat);
|
||||
if (!rtf) {
|
||||
return false;
|
||||
}
|
||||
relativeTimeFormat->setRelativeTimeFormatter(rtf);
|
||||
|
||||
intl::AddICUCellMemory(relativeTimeFormat,
|
||||
RelativeTimeFormatObject::EstimatedMemoryUse);
|
||||
return false;
|
||||
}
|
||||
|
||||
intl::FieldType jsUnitType;
|
||||
|
|
Загрузка…
Ссылка в новой задаче