Compare commits
1
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
baca5bb914 |
No files matched your search
@@ -732,6 +732,7 @@ function initializeIntlObject(obj, type, lazyData) {
|
||||
|
||||
assert((type === "Collator" && IsCollator(obj)) ||
|
||||
(type === "DateTimeFormat" && IsDateTimeFormat(obj)) ||
|
||||
(type === "ListFormat" && IsListFormat(obj) !== null) ||
|
||||
(type === "NumberFormat" && IsNumberFormat(obj)) ||
|
||||
(type === "PluralRules" && IsPluralRules(obj)) ||
|
||||
(type === "RelativeTimeFormat" && IsRelativeTimeFormat(obj)),
|
||||
@@ -815,6 +816,7 @@ function getIntlObjectInternals(obj) {
|
||||
assert(hasOwn("type", internals), "missing type");
|
||||
assert((internals.type === "Collator" && IsCollator(obj)) ||
|
||||
(internals.type === "DateTimeFormat" && IsDateTimeFormat(obj)) ||
|
||||
(internals.type === "ListFormat" && IsListFormat(obj)) ||
|
||||
(internals.type === "NumberFormat" && IsNumberFormat(obj)) ||
|
||||
(internals.type === "PluralRules" && IsPluralRules(obj)) ||
|
||||
(internals.type === "RelativeTimeFormat" && IsRelativeTimeFormat(obj)),
|
||||
@@ -850,6 +852,9 @@ function getInternals(obj) {
|
||||
case "DateTimeFormat":
|
||||
internalProps = resolveDateTimeFormatInternals(internals.lazyData);
|
||||
break;
|
||||
case "ListFormat":
|
||||
internalProps = resolveListFormatInternals(internals.lazyData);
|
||||
break;
|
||||
case "PluralRules":
|
||||
internalProps = resolvePluralRulesInternals(internals.lazyData);
|
||||
break;
|
||||
|
||||
@@ -22,6 +22,7 @@
|
||||
#include "builtin/intl/CommonFunctions.h"
|
||||
#include "builtin/intl/DateTimeFormat.h"
|
||||
#include "builtin/intl/LanguageTag.h"
|
||||
#include "builtin/intl/ListFormat.h"
|
||||
#include "builtin/intl/ICUHeader.h"
|
||||
#include "builtin/intl/Locale.h"
|
||||
#include "builtin/intl/NumberFormat.h"
|
||||
@@ -523,6 +524,8 @@ js::intl_BestAvailableLocale(JSContext* cx, unsigned argc, Value* vp)
|
||||
kind = SupportedLocaleKind::Collator;
|
||||
} else if (StringEqualsAscii(typeStr, "DateTimeFormat")) {
|
||||
kind = SupportedLocaleKind::DateTimeFormat;
|
||||
} else if (StringEqualsAscii(typeStr, "ListFormat")) {
|
||||
kind = SupportedLocaleKind::ListFormat;
|
||||
} else if (StringEqualsAscii(typeStr, "NumberFormat")) {
|
||||
kind = SupportedLocaleKind::NumberFormat;
|
||||
} else if (StringEqualsAscii(typeStr, "PluralRules")) {
|
||||
@@ -649,12 +652,12 @@ js::intl_supportedLocaleOrFallback(JSContext* cx, unsigned argc, Value* vp)
|
||||
// That implies we must ignore any candidate which isn't supported by all Intl
|
||||
// service constructors.
|
||||
//
|
||||
// Note: We don't test the supported locales of either Intl.PluralRules or
|
||||
// Intl.RelativeTimeFormat, because ICU doesn't provide the necessary API to
|
||||
// return actual set of supported locales for these constructors. Instead it
|
||||
// returns the complete set of available locales for ULocale, which is a
|
||||
// superset of the locales supported by Collator, NumberFormat, and
|
||||
// DateTimeFormat.
|
||||
// Note: We don't test the supported locales of either Intl.ListFormat,
|
||||
// Intl.PluralRules, Intl.RelativeTimeFormat, because ICU doesn't provide the
|
||||
// necessary API to return actual set of supported locales for these
|
||||
// constructors. Instead it returns the complete set of available locales for
|
||||
// ULocale, which is a superset of the locales supported by Collator,
|
||||
// NumberFormat, and DateTimeFormat.
|
||||
bool isSupported = true;
|
||||
for (auto kind : {SupportedLocaleKind::Collator, SupportedLocaleKind::DateTimeFormat,
|
||||
SupportedLocaleKind::NumberFormat}) {
|
||||
|
||||
@@ -0,0 +1,501 @@
|
||||
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
|
||||
* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||
|
||||
#include "builtin/intl/ListFormat.h"
|
||||
|
||||
#include "mozilla/ArrayUtils.h"
|
||||
#include "mozilla/Assertions.h"
|
||||
#include "mozilla/Casting.h"
|
||||
#include "mozilla/PodOperations.h"
|
||||
#include "mozilla/Unused.h"
|
||||
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#include "builtin/intl/CommonFunctions.h"
|
||||
#include "builtin/intl/ScopedICUObject.h"
|
||||
#include "js/Utility.h"
|
||||
#include "js/Vector.h"
|
||||
#include "unicode/uformattedvalue.h"
|
||||
#include "unicode/ulistformatter.h"
|
||||
#include "unicode/utypes.h"
|
||||
|
||||
#include "jscntxt.h"
|
||||
#include "jsobjinlines.h"
|
||||
|
||||
#include "vm/SelfHosting.h"
|
||||
#include "vm/NativeObject-inl.h"
|
||||
|
||||
using namespace js;
|
||||
|
||||
using mozilla::AssertedCast;
|
||||
|
||||
using js::intl::CallICU;
|
||||
using js::intl::IcuLocale;
|
||||
|
||||
const ClassOps ListFormatObject::classOps_ = {nullptr, /* addProperty */
|
||||
nullptr, /* delProperty */
|
||||
nullptr, /* getProperty */
|
||||
nullptr, /* setProperty */
|
||||
nullptr, /* enumerate */
|
||||
nullptr, /* resolve */
|
||||
nullptr, /* mayResolve */
|
||||
ListFormatObject::finalize};
|
||||
const Class ListFormatObject::class_ = {
|
||||
js_Object_str,
|
||||
JSCLASS_HAS_RESERVED_SLOTS(ListFormatObject::SLOT_COUNT) |
|
||||
JSCLASS_HAS_CACHED_PROTO(JSProto_ListFormat) |
|
||||
JSCLASS_FOREGROUND_FINALIZE,
|
||||
&ListFormatObject::classOps_
|
||||
};
|
||||
|
||||
const Class& ListFormatObject::protoClass_ = PlainObject::class_;
|
||||
|
||||
#if JS_HAS_TOSOURCE
|
||||
static bool
|
||||
listFormat_toSource(JSContext* cx, unsigned argc, Value* vp) {
|
||||
CallArgs args = CallArgsFromVp(argc, vp);
|
||||
args.rval().setString(cx->names().ListFormat);
|
||||
return true;
|
||||
}
|
||||
#endif
|
||||
|
||||
static const JSFunctionSpec listFormat_static_methods[] = {
|
||||
JS_SELF_HOSTED_FN("supportedLocalesOf",
|
||||
"Intl_ListFormat_supportedLocalesOf", 1, 0),
|
||||
JS_FS_END};
|
||||
|
||||
static const JSFunctionSpec listFormat_methods[] = {
|
||||
JS_SELF_HOSTED_FN("resolvedOptions", "Intl_ListFormat_resolvedOptions", 0,
|
||||
0),
|
||||
JS_SELF_HOSTED_FN("format", "Intl_ListFormat_format", 1, 0),
|
||||
#ifndef U_HIDE_DRAFT_API
|
||||
JS_SELF_HOSTED_FN("formatToParts", "Intl_ListFormat_formatToParts", 1, 0),
|
||||
#endif
|
||||
#if JS_HAS_TOSOURCE
|
||||
JS_FN(js_toSource_str, listFormat_toSource, 0, 0),
|
||||
#endif
|
||||
JS_FS_END
|
||||
};
|
||||
|
||||
|
||||
static const JSPropertySpec listFormat_properties[] = {
|
||||
JS_STRING_SYM_PS(toStringTag, "Intl.ListFormat", JSPROP_READONLY),
|
||||
JS_PS_END};
|
||||
|
||||
static JSObject*
|
||||
CreateListFormatPrototype(JSContext* cx, JSProtoKey key) {
|
||||
return GlobalObject::createBlankPrototype<PlainObject>(cx, cx->global());
|
||||
}
|
||||
|
||||
static bool ListFormat(JSContext* cx, unsigned argc, Value* vp);
|
||||
|
||||
const ClassSpec ListFormatObject::classSpec_ = {
|
||||
GenericCreateConstructor<ListFormat, 0, gc::AllocKind::FUNCTION>,
|
||||
CreateListFormatPrototype,
|
||||
listFormat_static_methods,
|
||||
nullptr,
|
||||
listFormat_methods,
|
||||
listFormat_properties
|
||||
// nullptr,
|
||||
// ClassSpec::DontDefineConstructor
|
||||
};
|
||||
|
||||
/**
|
||||
* Intl.ListFormat([ locales [, options]])
|
||||
*/
|
||||
static bool ListFormat(JSContext* cx, unsigned argc, Value* vp) {
|
||||
CallArgs args = CallArgsFromVp(argc, vp);
|
||||
|
||||
// Step 1.
|
||||
if (!ThrowIfNotConstructing(cx, args, "Intl.ListFormat")) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Step 2 (Inlined 9.1.14, OrdinaryCreateFromConstructor).
|
||||
RootedObject proto(cx);
|
||||
if (!GetPrototypeFromCallableConstructor(cx, args, &proto)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
Rooted<ListFormatObject*> listFormat(
|
||||
cx, NewObjectWithClassProto<ListFormatObject>(cx, proto));
|
||||
if (!listFormat) {
|
||||
return false;
|
||||
}
|
||||
|
||||
HandleValue locales = args.get(0);
|
||||
HandleValue options = args.get(1);
|
||||
|
||||
// Step 3.
|
||||
if (!intl::InitializeObject(cx, listFormat, cx->names().InitializeListFormat,
|
||||
locales, options)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
args.rval().setObject(*listFormat);
|
||||
return true;
|
||||
}
|
||||
|
||||
void js::ListFormatObject::finalize(FreeOp* fop, JSObject* obj) {
|
||||
MOZ_ASSERT(fop->onMainThread());
|
||||
|
||||
if (UListFormatter* lf = obj->as<ListFormatObject>().getListFormatter()) {
|
||||
ulistfmt_close(lf);
|
||||
}
|
||||
}
|
||||
|
||||
bool js::AddListFormatConstructor(JSContext* cx, JS::Handle<JSObject*> intl) {
|
||||
JSObject* ctor = GlobalObject::createConstructor(cx, ListFormat, cx->names().ListFormat, 0);
|
||||
if (!ctor) {
|
||||
return false;
|
||||
}
|
||||
|
||||
RootedValue ctorValue(cx, ObjectValue(*ctor));
|
||||
return DefineProperty(cx, intl, cx->names().ListFormat, ctorValue, 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a new UListFormatter with the locale and list formatting options
|
||||
* of the given ListFormat.
|
||||
*/
|
||||
static UListFormatter* NewUListFormatter(JSContext* cx,
|
||||
Handle<ListFormatObject*> listFormat) {
|
||||
RootedObject internals(cx, intl::GetInternalsObject(cx, listFormat));
|
||||
if (!internals) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
RootedValue value(cx);
|
||||
|
||||
if (!GetProperty(cx, internals, internals, cx->names().locale, &value)) {
|
||||
return nullptr;
|
||||
}
|
||||
JSAutoByteString bytes;
|
||||
char* locale = bytes.encodeLatin1(cx, value.toString());
|
||||
if (!locale) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
enum class ListFormatType { Conjunction, Disjunction, Unit };
|
||||
|
||||
ListFormatType type;
|
||||
if (!GetProperty(cx, internals, internals, cx->names().type, &value)) {
|
||||
return nullptr;
|
||||
}
|
||||
{
|
||||
JSLinearString* strType = value.toString()->ensureLinear(cx);
|
||||
if (!strType) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
if (StringEqualsAscii(strType, "conjunction")) {
|
||||
type = ListFormatType::Conjunction;
|
||||
} else if (StringEqualsAscii(strType, "disjunction")) {
|
||||
type = ListFormatType::Disjunction;
|
||||
} else {
|
||||
MOZ_ASSERT(StringEqualsAscii(strType, "unit"));
|
||||
type = ListFormatType::Unit;
|
||||
}
|
||||
}
|
||||
|
||||
enum class ListFormatStyle { Long, Short, Narrow };
|
||||
|
||||
ListFormatStyle style;
|
||||
if (!GetProperty(cx, internals, internals, cx->names().style, &value)) {
|
||||
return nullptr;
|
||||
}
|
||||
{
|
||||
JSLinearString* strStyle = value.toString()->ensureLinear(cx);
|
||||
if (!strStyle) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
if (StringEqualsAscii(strStyle, "long")) {
|
||||
style = ListFormatStyle::Long;
|
||||
} else if (StringEqualsAscii(strStyle, "short")) {
|
||||
style = ListFormatStyle::Short;
|
||||
} else {
|
||||
MOZ_ASSERT(StringEqualsAscii(strStyle, "narrow"));
|
||||
style = ListFormatStyle::Narrow;
|
||||
}
|
||||
}
|
||||
|
||||
// We're currently only supporting "conjunctive-long" list formatters due to
|
||||
// missing ICU APIs: https://unicode-org.atlassian.net/browse/ICU-12863
|
||||
MOZ_ASSERT(type == ListFormatType::Conjunction);
|
||||
MOZ_ASSERT(style == ListFormatStyle::Long);
|
||||
|
||||
mozilla::Unused << type;
|
||||
mozilla::Unused << style;
|
||||
|
||||
UErrorCode status = U_ZERO_ERROR;
|
||||
UListFormatter* lf = ulistfmt_open(IcuLocale(locale), &status);
|
||||
if (U_FAILURE(status)) {
|
||||
intl::ReportInternalError(cx);
|
||||
return nullptr;
|
||||
}
|
||||
return lf;
|
||||
}
|
||||
|
||||
static constexpr size_t DEFAULT_LIST_LENGTH = 8;
|
||||
|
||||
using ListFormatStringVector = Vector<UniqueTwoByteChars, DEFAULT_LIST_LENGTH>;
|
||||
using ListFormatStringLengthVector = Vector<int32_t, DEFAULT_LIST_LENGTH>;
|
||||
|
||||
static_assert(sizeof(UniqueTwoByteChars) == sizeof(char16_t*),
|
||||
"UniqueTwoByteChars are stored efficiently and are held in "
|
||||
"continuous memory");
|
||||
|
||||
/**
|
||||
* FormatList ( listFormat, list )
|
||||
*/
|
||||
static bool FormatList(JSContext* cx, UListFormatter* lf,
|
||||
const ListFormatStringVector& strings,
|
||||
const ListFormatStringLengthVector& stringLengths,
|
||||
MutableHandleValue result) {
|
||||
MOZ_ASSERT(strings.length() == stringLengths.length());
|
||||
MOZ_ASSERT(strings.length() <= INT32_MAX);
|
||||
|
||||
JSString* str = intl::CallICU(cx, [lf, &strings, &stringLengths](
|
||||
UChar* chars, int32_t size,
|
||||
UErrorCode* status) {
|
||||
return ulistfmt_format(
|
||||
lf, reinterpret_cast<char16_t* const*>(strings.begin()),
|
||||
stringLengths.begin(), int32_t(strings.length()), chars, size, status);
|
||||
});
|
||||
if (!str) {
|
||||
return false;
|
||||
}
|
||||
|
||||
result.setString(str);
|
||||
return true;
|
||||
}
|
||||
|
||||
#ifndef U_HIDE_DRAFT_API
|
||||
static JSString* FormattedValueToString(JSContext* cx,
|
||||
const UFormattedValue* formattedValue) {
|
||||
UErrorCode status = U_ZERO_ERROR;
|
||||
int32_t strLength;
|
||||
const char16_t* str = ufmtval_getString(formattedValue, &strLength, &status);
|
||||
if (U_FAILURE(status)) {
|
||||
intl::ReportInternalError(cx);
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
return NewStringCopyN<CanGC>(cx, str, AssertedCast<uint32_t>(strLength));
|
||||
}
|
||||
|
||||
/**
|
||||
* FormatListToParts ( listFormat, list )
|
||||
*/
|
||||
static bool FormatListToParts(JSContext* cx, UListFormatter* lf,
|
||||
const ListFormatStringVector& strings,
|
||||
const ListFormatStringLengthVector& stringLengths,
|
||||
MutableHandleValue result) {
|
||||
MOZ_ASSERT(strings.length() == stringLengths.length());
|
||||
MOZ_ASSERT(strings.length() <= INT32_MAX);
|
||||
|
||||
UErrorCode status = U_ZERO_ERROR;
|
||||
UFormattedList* formatted = ulistfmt_openResult(&status);
|
||||
if (U_FAILURE(status)) {
|
||||
intl::ReportInternalError(cx);
|
||||
return false;
|
||||
}
|
||||
ScopedICUObject<UFormattedList, ulistfmt_closeResult> toClose(formatted);
|
||||
|
||||
ulistfmt_formatStringsToResult(
|
||||
lf, reinterpret_cast<char16_t* const*>(strings.begin()),
|
||||
stringLengths.begin(), int32_t(strings.length()), formatted, &status);
|
||||
if (U_FAILURE(status)) {
|
||||
intl::ReportInternalError(cx);
|
||||
return false;
|
||||
}
|
||||
|
||||
const UFormattedValue* formattedValue =
|
||||
ulistfmt_resultAsValue(formatted, &status);
|
||||
if (U_FAILURE(status)) {
|
||||
intl::ReportInternalError(cx);
|
||||
return false;
|
||||
}
|
||||
|
||||
RootedString overallResult(cx, FormattedValueToString(cx, formattedValue));
|
||||
if (!overallResult) {
|
||||
return false;
|
||||
}
|
||||
|
||||
RootedArrayObject partsArray(cx, NewDenseEmptyArray(cx));
|
||||
if (!partsArray) {
|
||||
return false;
|
||||
}
|
||||
|
||||
using FieldType = js::ImmutablePropertyNamePtr JSAtomState::*;
|
||||
|
||||
size_t lastEndIndex = 0;
|
||||
RootedObject singlePart(cx);
|
||||
RootedValue partType(cx);
|
||||
RootedValue val(cx);
|
||||
|
||||
auto AppendPart = [&](FieldType type, size_t beginIndex, size_t endIndex) {
|
||||
singlePart = NewBuiltinClassInstance<PlainObject>(cx);
|
||||
if (!singlePart) {
|
||||
return false;
|
||||
}
|
||||
|
||||
partType = StringValue(cx->names().*type);
|
||||
if (!DefineProperty(cx, singlePart, cx->names().type, partType)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
JSLinearString* partSubstr = NewDependentString(
|
||||
cx, overallResult, beginIndex, endIndex - beginIndex);
|
||||
if (!partSubstr) {
|
||||
return false;
|
||||
}
|
||||
|
||||
val = StringValue(partSubstr);
|
||||
if (!DefineProperty(cx, singlePart, cx->names().value, val)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!NewbornArrayPush(cx, partsArray, ObjectValue(*singlePart))) {
|
||||
return false;
|
||||
}
|
||||
|
||||
lastEndIndex = endIndex;
|
||||
return true;
|
||||
};
|
||||
|
||||
UConstrainedFieldPosition* fpos = ucfpos_open(&status);
|
||||
if (U_FAILURE(status)) {
|
||||
intl::ReportInternalError(cx);
|
||||
return false;
|
||||
}
|
||||
ScopedICUObject<UConstrainedFieldPosition, ucfpos_close> toCloseFpos(fpos);
|
||||
|
||||
// We're only interested in ULISTFMT_ELEMENT_FIELD fields.
|
||||
ucfpos_constrainField(fpos, UFIELD_CATEGORY_LIST, ULISTFMT_ELEMENT_FIELD,
|
||||
&status);
|
||||
if (U_FAILURE(status)) {
|
||||
intl::ReportInternalError(cx);
|
||||
return false;
|
||||
}
|
||||
|
||||
while (true) {
|
||||
bool hasMore = ufmtval_nextPosition(formattedValue, fpos, &status);
|
||||
if (U_FAILURE(status)) {
|
||||
intl::ReportInternalError(cx);
|
||||
return false;
|
||||
}
|
||||
if (!hasMore) {
|
||||
break;
|
||||
}
|
||||
|
||||
int32_t beginIndexInt, endIndexInt;
|
||||
ucfpos_getIndexes(fpos, &beginIndexInt, &endIndexInt, &status);
|
||||
if (U_FAILURE(status)) {
|
||||
intl::ReportInternalError(cx);
|
||||
return false;
|
||||
}
|
||||
|
||||
MOZ_ASSERT(beginIndexInt >= 0);
|
||||
MOZ_ASSERT(endIndexInt >= 0);
|
||||
MOZ_ASSERT(beginIndexInt <= endIndexInt,
|
||||
"field iterator returning invalid range");
|
||||
|
||||
size_t beginIndex = size_t(beginIndexInt);
|
||||
size_t endIndex = size_t(endIndexInt);
|
||||
|
||||
// Indices are guaranteed to be returned in order (from left to right).
|
||||
MOZ_ASSERT(lastEndIndex <= beginIndex,
|
||||
"field iteration didn't return fields in order start to "
|
||||
"finish as expected");
|
||||
|
||||
if (lastEndIndex < beginIndex) {
|
||||
if (!AppendPart(&JSAtomState::literal, lastEndIndex, beginIndex)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
if (!AppendPart(&JSAtomState::element, beginIndex, endIndex)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
// Append any final literal.
|
||||
if (lastEndIndex < overallResult->length()) {
|
||||
if (!AppendPart(&JSAtomState::literal, lastEndIndex,
|
||||
overallResult->length())) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
result.setObject(*partsArray);
|
||||
return true;
|
||||
}
|
||||
#endif // U_HIDE_DRAFT_API
|
||||
|
||||
bool js::intl_FormatList(JSContext* cx, unsigned argc, Value* vp) {
|
||||
CallArgs args = CallArgsFromVp(argc, vp);
|
||||
MOZ_ASSERT(args.length() == 3);
|
||||
|
||||
Rooted<ListFormatObject*> listFormat(
|
||||
cx, &args[0].toObject().as<ListFormatObject>());
|
||||
|
||||
bool formatToParts = args[2].toBoolean();
|
||||
|
||||
// Obtain a cached UListFormatter object.
|
||||
UListFormatter* lf = listFormat->getListFormatter();
|
||||
if (!lf) {
|
||||
lf = NewUListFormatter(cx, listFormat);
|
||||
if (!lf) {
|
||||
return false;
|
||||
}
|
||||
listFormat->setListFormatter(lf);
|
||||
}
|
||||
|
||||
// Collect all strings and their lengths.
|
||||
ListFormatStringVector strings(cx);
|
||||
ListFormatStringLengthVector stringLengths(cx);
|
||||
|
||||
RootedArrayObject list(cx, &args[1].toObject().as<ArrayObject>());
|
||||
RootedValue value(cx);
|
||||
for (uint32_t i = 0; i < list->length(); i++) {
|
||||
if (!GetElement(cx, list, list, i, &value)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
JSLinearString* linear = value.toString()->ensureLinear(cx);
|
||||
if (!linear) {
|
||||
return false;
|
||||
}
|
||||
|
||||
size_t linearLength = linear->length();
|
||||
if (!stringLengths.append(linearLength)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
UniqueTwoByteChars chars = cx->make_pod_array<char16_t>(linearLength);
|
||||
if (!chars) {
|
||||
return false;
|
||||
}
|
||||
CopyChars(chars.get(), *linear);
|
||||
|
||||
if (!strings.append(std::move(chars))) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
// Use the UListFormatter to actually format the strings.
|
||||
#ifndef U_HIDE_DRAFT_API
|
||||
if (formatToParts) {
|
||||
return FormatListToParts(cx, lf, strings, stringLengths, args.rval());
|
||||
}
|
||||
#else
|
||||
MOZ_ASSERT(!formatToParts);
|
||||
#endif
|
||||
|
||||
return FormatList(cx, lf, strings, stringLengths, args.rval());
|
||||
}
|
||||
@@ -0,0 +1,67 @@
|
||||
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
|
||||
* vim: set ts=8 sts=2 et sw=2 tw=80:
|
||||
* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||
|
||||
#ifndef builtin_intl_ListFormat_h
|
||||
#define builtin_intl_ListFormat_h
|
||||
|
||||
#include "mozilla/Attributes.h"
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#include "builtin/intl/CommonFunctions.h"
|
||||
#include "builtin/SelfHostingDefines.h"
|
||||
#include "js/Class.h"
|
||||
#include "js/RootingAPI.h"
|
||||
#include "vm/NativeObject.h"
|
||||
|
||||
struct UListFormatter;
|
||||
|
||||
namespace js {
|
||||
|
||||
class ListFormatObject : public NativeObject {
|
||||
public:
|
||||
static const Class class_;
|
||||
static const Class& protoClass_;
|
||||
|
||||
static constexpr uint32_t INTERNALS_SLOT = 0;
|
||||
static constexpr uint32_t ULIST_FORMATTER_SLOT = 1;
|
||||
static constexpr uint32_t SLOT_COUNT = 2;
|
||||
|
||||
static_assert(INTERNALS_SLOT == INTL_INTERNALS_OBJECT_SLOT,
|
||||
"INTERNALS_SLOT must match self-hosting define for internals "
|
||||
"object slot");
|
||||
|
||||
UListFormatter* getListFormatter() const {
|
||||
const auto& slot = getFixedSlot(ULIST_FORMATTER_SLOT);
|
||||
if (slot.isUndefined()) {
|
||||
return nullptr;
|
||||
}
|
||||
return static_cast<UListFormatter*>(slot.toPrivate());
|
||||
}
|
||||
|
||||
void setListFormatter(UListFormatter* formatter) {
|
||||
setFixedSlot(ULIST_FORMATTER_SLOT, PrivateValue(formatter));
|
||||
}
|
||||
|
||||
private:
|
||||
static const ClassOps classOps_;
|
||||
static const ClassSpec classSpec_;
|
||||
|
||||
static void finalize(FreeOp* fop, JSObject* obj);
|
||||
};
|
||||
|
||||
/**
|
||||
* Returns a string representing the array of string values |list| according to
|
||||
* the effective locale and the formatting options of the given ListFormat.
|
||||
*
|
||||
* Usage: formatted = intl_FormatList(listFormat, list, formatToParts)
|
||||
*/
|
||||
extern bool intl_FormatList(JSContext* cx, unsigned argc,
|
||||
Value* vp);
|
||||
|
||||
} // namespace js
|
||||
|
||||
#endif /* builtin_intl_ListFormat_h */
|
||||
@@ -0,0 +1,262 @@
|
||||
/* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||
|
||||
/**
|
||||
* ListFormat internal properties.
|
||||
*/
|
||||
var listFormatInternalProperties = {
|
||||
localeData: function() // eslint-disable-line object-shorthand
|
||||
{
|
||||
// ListFormat don't support any extension keys.
|
||||
return {};
|
||||
},
|
||||
relevantExtensionKeys: []
|
||||
};
|
||||
|
||||
/**
|
||||
* Intl.ListFormat ( [ locales [ , options ] ] )
|
||||
*
|
||||
* Compute an internal properties object from |lazyListFormatData|.
|
||||
*/
|
||||
function resolveListFormatInternals(lazyListFormatData) {
|
||||
assert(IsObject(lazyListFormatData), "lazy data not an object?");
|
||||
|
||||
var internalProps = std_Object_create(null);
|
||||
|
||||
var ListFormat = listFormatInternalProperties;
|
||||
|
||||
// Compute effective locale.
|
||||
|
||||
// Step 9.
|
||||
var localeData = ListFormat.localeData;
|
||||
|
||||
// Step 10.
|
||||
var r = ResolveLocale("ListFormat",
|
||||
lazyListFormatData.requestedLocales,
|
||||
lazyListFormatData.opt,
|
||||
ListFormat.relevantExtensionKeys,
|
||||
localeData);
|
||||
|
||||
// Step 11.
|
||||
internalProps.locale = r.locale;
|
||||
|
||||
// Step 13.
|
||||
internalProps.type = lazyListFormatData.type;
|
||||
|
||||
// Step 15.
|
||||
internalProps.style = lazyListFormatData.style;
|
||||
|
||||
// Steps 16-23 (not applicable in our implementation).
|
||||
|
||||
// The caller is responsible for associating |internalProps| with the right
|
||||
// object using |setInternalProperties|.
|
||||
return internalProps;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an object containing the ListFormat internal properties of |obj|.
|
||||
*/
|
||||
function getListFormatInternals(obj) {
|
||||
assert(IsObject(obj), "getListFormatInternals called with non-object");
|
||||
assert(GuardToListFormat(obj) !== null, "getListFormatInternals called with non-ListFormat");
|
||||
|
||||
var internals = getIntlObjectInternals(obj);
|
||||
assert(internals.type === "ListFormat", "bad type escaped getIntlObjectInternals");
|
||||
|
||||
// If internal properties have already been computed, use them.
|
||||
var internalProps = maybeInternalProperties(internals);
|
||||
if (internalProps)
|
||||
return internalProps;
|
||||
|
||||
// Otherwise it's time to fully create them.
|
||||
internalProps = resolveListFormatInternals(internals.lazyData);
|
||||
setInternalProperties(internals, internalProps);
|
||||
return internalProps;
|
||||
}
|
||||
|
||||
/**
|
||||
* Intl.ListFormat ( [ locales [ , options ] ] )
|
||||
*
|
||||
* Initializes an object as a ListFormat.
|
||||
*
|
||||
* This method is complicated a moderate bit by its implementing initialization
|
||||
* as a *lazy* concept. Everything that must happen now, does -- but we defer
|
||||
* all the work we can until the object is actually used as a ListFormat.
|
||||
* This later work occurs in |resolveListFormatInternals|; steps not noted
|
||||
* here occur there.
|
||||
*/
|
||||
function InitializeListFormat(listFormat, locales, options) {
|
||||
assert(IsObject(listFormat), "InitializeListFormat called with non-object");
|
||||
assert(GuardToListFormat(listFormat) !== null, "InitializeListFormat called with non-ListFormat");
|
||||
|
||||
// Lazy ListFormat data has the following structure:
|
||||
//
|
||||
// {
|
||||
// requestedLocales: List of locales,
|
||||
// type: "conjunction" / "disjunction" / "unit",
|
||||
// style: "long" / "short" / "narrow",
|
||||
//
|
||||
// opt: // opt object computed in InitializeListFormat
|
||||
// {
|
||||
// localeMatcher: "lookup" / "best fit",
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// Note that lazy data is only installed as a final step of initialization,
|
||||
// so every ListFormat lazy data object has *all* these properties, never a
|
||||
// subset of them.
|
||||
var lazyListFormatData = std_Object_create(null);
|
||||
|
||||
// Step 3.
|
||||
var requestedLocales = CanonicalizeLocaleList(locales);
|
||||
lazyListFormatData.requestedLocales = requestedLocales;
|
||||
|
||||
// Steps 4-5.
|
||||
if (options === undefined)
|
||||
options = std_Object_create(null);
|
||||
else
|
||||
options = ToObject(options);
|
||||
|
||||
// Step 6.
|
||||
var opt = new Record();
|
||||
lazyListFormatData.opt = opt;
|
||||
|
||||
// Steps 7-8.
|
||||
let matcher = GetOption(options, "localeMatcher", "string", ["lookup", "best fit"], "best fit");
|
||||
opt.localeMatcher = matcher;
|
||||
|
||||
// Compute formatting options.
|
||||
|
||||
// We're currently only supporting "conjunctive-long" list formatters due to
|
||||
// missing ICU APIs: https://unicode-org.atlassian.net/browse/ICU-12863
|
||||
|
||||
// Steps 12-13.
|
||||
var type = GetOption(options, "type", "string", ["conjunction"], "conjunction");
|
||||
lazyListFormatData.type = type;
|
||||
|
||||
// Steps 14-15.
|
||||
var style = GetOption(options, "style", "string", ["long"], "long");
|
||||
lazyListFormatData.style = style;
|
||||
|
||||
// We've done everything that must be done now: mark the lazy data as fully
|
||||
// computed and install it.
|
||||
initializeIntlObject(listFormat, "ListFormat", lazyListFormatData);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the subset of the given locale list for which this locale list has a
|
||||
* matching (possibly fallback) locale. Locales appear in the same order in the
|
||||
* returned list as in the input list.
|
||||
*/
|
||||
function Intl_ListFormat_supportedLocalesOf(locales /*, options*/) {
|
||||
var options = arguments.length > 1 ? arguments[1] : undefined;
|
||||
|
||||
// Step 1.
|
||||
var availableLocales = "ListFormat";
|
||||
|
||||
// Step 2.
|
||||
var requestedLocales = CanonicalizeLocaleList(locales);
|
||||
|
||||
// Step 3.
|
||||
return SupportedLocales(availableLocales, requestedLocales, options);
|
||||
}
|
||||
|
||||
/**
|
||||
* StringListFromIterable ( iterable )
|
||||
*/
|
||||
function StringListFromIterable(iterable, methodName) {
|
||||
// Step 1.
|
||||
if (iterable === undefined) {
|
||||
return [];
|
||||
}
|
||||
|
||||
// Step 3.
|
||||
var list = [];
|
||||
|
||||
// Steps 2, 4-5.
|
||||
for (var element of allowContentIter(iterable)) {
|
||||
// Step 5.b.ii.
|
||||
if (typeof element !== "string") {
|
||||
ThrowTypeError(JSMSG_NOT_EXPECTED_TYPE, methodName, "string", typeof element);
|
||||
}
|
||||
|
||||
// Step 5.b.iii.
|
||||
_DefineDataProperty(list, list.length, element);
|
||||
}
|
||||
|
||||
// Step 6.
|
||||
return list;
|
||||
}
|
||||
|
||||
/**
|
||||
* Intl.ListFormat.prototype.format ( list )
|
||||
*/
|
||||
function Intl_ListFormat_format(list) {
|
||||
// Step 1.
|
||||
var listFormat = this;
|
||||
|
||||
// Steps 2-3.
|
||||
if (!IsObject(listFormat) || (listFormat = GuardToListFormat(listFormat)) === null) {
|
||||
return callFunction(CallListFormatMethodIfWrapped, this, list,
|
||||
"Intl_ListFormat_format");
|
||||
}
|
||||
|
||||
// Ensure the ListFormat internals are resolved.
|
||||
getListFormatInternals(listFormat);
|
||||
|
||||
// Step 4.
|
||||
var stringList = StringListFromIterable(list, "format");
|
||||
|
||||
// Step 5.
|
||||
return intl_FormatList(listFormat, stringList, /* formatToParts = */ false);
|
||||
}
|
||||
|
||||
/**
|
||||
* Intl.ListFormat.prototype.formatToParts ( list )
|
||||
*/
|
||||
function Intl_ListFormat_formatToParts(list) {
|
||||
// Step 1.
|
||||
var listFormat = this;
|
||||
|
||||
// Steps 2-3.
|
||||
if (!IsObject(listFormat) || (listFormat = GuardToListFormat(listFormat)) === null) {
|
||||
return callFunction(CallListFormatMethodIfWrapped, this, list,
|
||||
"Intl_ListFormat_formatToParts");
|
||||
}
|
||||
|
||||
// Ensure the ListFormat internals are resolved.
|
||||
getListFormatInternals(listFormat);
|
||||
|
||||
// Step 4.
|
||||
var stringList = StringListFromIterable(list, "formatToParts");
|
||||
|
||||
// Step 5.
|
||||
return intl_FormatList(listFormat, stringList, /* formatToParts = */ true);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the resolved options for a ListFormat object.
|
||||
*/
|
||||
function Intl_ListFormat_resolvedOptions() {
|
||||
// Step 1.
|
||||
var listFormat = this;
|
||||
|
||||
// Steps 2-3.
|
||||
if (!IsObject(listFormat) || (listFormat = GuardToListFormat(listFormat)) === null) {
|
||||
return callFunction(CallListFormatMethodIfWrapped, this,
|
||||
"Intl_ListFormat_resolvedOptions");
|
||||
}
|
||||
|
||||
var internals = getListFormatInternals(listFormat);
|
||||
|
||||
// Steps 4-5.
|
||||
var result = {
|
||||
locale: internals.locale,
|
||||
type: internals.type,
|
||||
style: internals.style,
|
||||
};
|
||||
|
||||
// Step 6.
|
||||
return result;
|
||||
}
|
||||
File diff suppressed because it is too large.
Load diff
@@ -1,285 +1,286 @@
|
||||
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
|
||||
* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||
|
||||
#ifndef builtin_intl_SharedIntlData_h
|
||||
#define builtin_intl_SharedIntlData_h
|
||||
|
||||
#include "mozilla/MemoryReporting.h"
|
||||
|
||||
#include <stddef.h>
|
||||
|
||||
#include "jsalloc.h"
|
||||
#include "js/CharacterEncoding.h"
|
||||
#include "js/GCAPI.h"
|
||||
#include "js/GCHashTable.h"
|
||||
#include "js/RootingAPI.h"
|
||||
#include "js/Utility.h"
|
||||
#include "vm/String.h"
|
||||
|
||||
namespace js {
|
||||
|
||||
namespace intl {
|
||||
|
||||
/**
|
||||
* Stores Intl data which can be shared across compartments (but not contexts).
|
||||
*
|
||||
* Used for data which is expensive when computed repeatedly or is not
|
||||
* available through ICU.
|
||||
*/
|
||||
class SharedIntlData
|
||||
{
|
||||
struct LinearStringLookup
|
||||
{
|
||||
union {
|
||||
const JS::Latin1Char* latin1Chars;
|
||||
const char16_t* twoByteChars;
|
||||
};
|
||||
bool isLatin1;
|
||||
size_t length;
|
||||
JS::AutoCheckCannotGC nogc;
|
||||
HashNumber hash = 0;
|
||||
|
||||
explicit LinearStringLookup(JSLinearString* string)
|
||||
: isLatin1(string->hasLatin1Chars()), length(string->length())
|
||||
{
|
||||
if (isLatin1)
|
||||
latin1Chars = string->latin1Chars(nogc);
|
||||
else
|
||||
twoByteChars = string->twoByteChars(nogc);
|
||||
}
|
||||
|
||||
LinearStringLookup(const char* chars, size_t length)
|
||||
: isLatin1(true), length(length)
|
||||
{
|
||||
latin1Chars = reinterpret_cast<const JS::Latin1Char*>(chars);
|
||||
}
|
||||
};
|
||||
|
||||
private:
|
||||
/**
|
||||
* Information tracking the set of the supported time zone names, derived
|
||||
* from the IANA time zone database <https://www.iana.org/time-zones>.
|
||||
*
|
||||
* There are two kinds of IANA time zone names: Zone and Link (denoted as
|
||||
* such in database source files). Zone names are the canonical, preferred
|
||||
* name for a time zone, e.g. Asia/Kolkata. Link names simply refer to
|
||||
* target Zone names for their meaning, e.g. Asia/Calcutta targets
|
||||
* Asia/Kolkata. That a name is a Link doesn't *necessarily* reflect a
|
||||
* sense of deprecation: some Link names also exist partly for convenience,
|
||||
* e.g. UTC and GMT as Link names targeting the Zone name Etc/UTC.
|
||||
*
|
||||
* Two data sources determine the time zone names we support: those ICU
|
||||
* supports and IANA's zone information.
|
||||
*
|
||||
* Unfortunately the names ICU and IANA support, and their Link
|
||||
* relationships from name to target, aren't identical, so we can't simply
|
||||
* implicitly trust ICU's name handling. We must perform various
|
||||
* preprocessing of user-provided zone names and post-processing of
|
||||
* ICU-provided zone names to implement ECMA-402's IANA-consistent behavior.
|
||||
*
|
||||
* Also see <https://ssl.icu-project.org/trac/ticket/12044> and
|
||||
* <http://unicode.org/cldr/trac/ticket/9892>.
|
||||
*/
|
||||
|
||||
using TimeZoneName = JSAtom*;
|
||||
|
||||
struct TimeZoneHasher
|
||||
{
|
||||
struct Lookup : LinearStringLookup
|
||||
{
|
||||
explicit Lookup(JSFlatString* timeZone);
|
||||
};
|
||||
|
||||
static js::HashNumber hash(const Lookup& lookup) { return lookup.hash; }
|
||||
static bool match(TimeZoneName key, const Lookup& lookup);
|
||||
};
|
||||
|
||||
using TimeZoneSet = js::GCHashSet<TimeZoneName,
|
||||
TimeZoneHasher,
|
||||
js::SystemAllocPolicy>;
|
||||
|
||||
using TimeZoneMap = js::GCHashMap<TimeZoneName,
|
||||
TimeZoneName,
|
||||
TimeZoneHasher,
|
||||
js::SystemAllocPolicy>;
|
||||
|
||||
/**
|
||||
* As a threshold matter, available time zones are those time zones ICU
|
||||
* supports, via ucal_openTimeZones. But ICU supports additional non-IANA
|
||||
* time zones described in intl/icu/source/tools/tzcode/icuzones (listed in
|
||||
* IntlTimeZoneData.cpp's |legacyICUTimeZones|) for its own backwards
|
||||
* compatibility purposes. This set consists of ICU's supported time zones,
|
||||
* minus all backwards-compatibility time zones.
|
||||
*/
|
||||
TimeZoneSet availableTimeZones;
|
||||
|
||||
/**
|
||||
* IANA treats some time zone names as Zones, that ICU instead treats as
|
||||
* Links. For example, IANA considers "America/Indiana/Indianapolis" to be
|
||||
* a Zone and "America/Fort_Wayne" a Link that targets it, but ICU
|
||||
* considers the former a Link that targets "America/Indianapolis" (which
|
||||
* IANA treats as a Link).
|
||||
*
|
||||
* ECMA-402 requires that we respect IANA data, so if we're asked to
|
||||
* canonicalize a time zone name in this set, we must *not* return ICU's
|
||||
* canonicalization.
|
||||
*/
|
||||
TimeZoneSet ianaZonesTreatedAsLinksByICU;
|
||||
|
||||
/**
|
||||
* IANA treats some time zone names as Links to one target, that ICU
|
||||
* instead treats as either Zones, or Links to different targets. An
|
||||
* example of the former is "Asia/Calcutta, which IANA assigns the target
|
||||
* "Asia/Kolkata" but ICU considers its own Zone. An example of the latter
|
||||
* is "America/Virgin", which IANA assigns the target
|
||||
* "America/Port_of_Spain" but ICU assigns the target "America/St_Thomas".
|
||||
*
|
||||
* ECMA-402 requires that we respect IANA data, so if we're asked to
|
||||
* canonicalize a time zone name that's a key in this map, we *must* return
|
||||
* the corresponding value and *must not* return ICU's canonicalization.
|
||||
*/
|
||||
TimeZoneMap ianaLinksCanonicalizedDifferentlyByICU;
|
||||
|
||||
bool timeZoneDataInitialized = false;
|
||||
|
||||
/**
|
||||
* Precomputes the available time zone names, because it's too expensive to
|
||||
* call ucal_openTimeZones() repeatedly.
|
||||
*/
|
||||
bool ensureTimeZones(JSContext* cx);
|
||||
|
||||
public:
|
||||
/**
|
||||
* Returns the validated time zone name in |result|. If the input time zone
|
||||
* isn't a valid IANA time zone name, |result| remains unchanged.
|
||||
*/
|
||||
bool validateTimeZoneName(JSContext* cx, JS::HandleString timeZone,
|
||||
JS::MutableHandleString result);
|
||||
|
||||
/**
|
||||
* Returns the canonical time zone name in |result|. If no canonical name
|
||||
* was found, |result| remains unchanged.
|
||||
*
|
||||
* This method only handles time zones which are canonicalized differently
|
||||
* by ICU when compared to IANA.
|
||||
*/
|
||||
bool tryCanonicalizeTimeZoneConsistentWithIANA(JSContext* cx, JS::HandleString timeZone,
|
||||
JS::MutableHandleString result);
|
||||
private:
|
||||
using Locale = JSAtom*;
|
||||
|
||||
struct LocaleHasher
|
||||
{
|
||||
struct Lookup : LinearStringLookup
|
||||
{
|
||||
explicit Lookup(JSLinearString* locale);
|
||||
Lookup(const char* chars, size_t length);
|
||||
};
|
||||
|
||||
static js::HashNumber hash(const Lookup& lookup) { return lookup.hash; }
|
||||
static bool match(Locale key, const Lookup& lookup);
|
||||
};
|
||||
|
||||
using LocaleSet = GCHashSet<Locale, LocaleHasher, SystemAllocPolicy>;
|
||||
|
||||
// Set of supported locales for all Intl service constructors except Collator,
|
||||
// which uses its own set.
|
||||
//
|
||||
// UDateFormat:
|
||||
// udat_[count,get]Available() return the same results as their
|
||||
// uloc_[count,get]Available() counterparts.
|
||||
//
|
||||
// UNumberFormatter:
|
||||
// unum_[count,get]Available() return the same results as their
|
||||
// uloc_[count,get]Available() counterparts.
|
||||
//
|
||||
// UPluralRules and URelativeDateTimeFormatter:
|
||||
// We're going to use ULocale availableLocales as per ICU recommendation:
|
||||
// https://unicode-org.atlassian.net/browse/ICU-12756
|
||||
LocaleSet supportedLocales;
|
||||
|
||||
// ucol_[count,get]Available() return different results compared to
|
||||
// uloc_[count,get]Available(), we can't use |supportedLocales| here.
|
||||
LocaleSet collatorSupportedLocales;
|
||||
|
||||
bool supportedLocalesInitialized = false;
|
||||
|
||||
// CountAvailable and GetAvailable describe the signatures used for ICU API
|
||||
// to determine available locales for various functionality.
|
||||
using CountAvailable = int32_t (*)();
|
||||
using GetAvailable = const char* (*)(int32_t localeIndex);
|
||||
|
||||
static bool getAvailableLocales(JSContext* cx, LocaleSet& locales,
|
||||
CountAvailable countAvailable,
|
||||
GetAvailable getAvailable);
|
||||
|
||||
/**
|
||||
* Precomputes the available locales sets.
|
||||
*/
|
||||
bool ensureSupportedLocales(JSContext* cx);
|
||||
|
||||
public:
|
||||
enum class SupportedLocaleKind {
|
||||
Collator,
|
||||
DateTimeFormat,
|
||||
NumberFormat,
|
||||
PluralRules,
|
||||
RelativeTimeFormat
|
||||
};
|
||||
|
||||
/**
|
||||
* Sets |supported| to true if |locale| is supported by the requested Intl
|
||||
* service constructor. Otherwise sets |supported| to false.
|
||||
*/
|
||||
[[nodiscard]] bool isSupportedLocale(JSContext* cx, SupportedLocaleKind kind,
|
||||
JS::Handle<JSString*> locale,
|
||||
bool* supported);
|
||||
|
||||
private:
|
||||
/**
|
||||
* The case first parameter (BCP47 key "kf") allows to switch the order of
|
||||
* upper- and lower-case characters. ICU doesn't directly provide an API
|
||||
* to query the default case first value of a given locale, but instead
|
||||
* requires to instantiate a collator object and then query the case first
|
||||
* attribute (UCOL_CASE_FIRST).
|
||||
* To avoid instantiating an additional collator object whenever we need
|
||||
* to retrieve the default case first value of a specific locale, we
|
||||
* compute the default case first value for every supported locale only
|
||||
* once and then keep a list of all locales which don't use the default
|
||||
* case first setting.
|
||||
* There is almost no difference between lower-case first and when case
|
||||
* first is disabled (UCOL_LOWER_FIRST resp. UCOL_OFF), so we only need to
|
||||
* track locales which use upper-case first as their default setting.
|
||||
*/
|
||||
|
||||
LocaleSet upperCaseFirstLocales;
|
||||
|
||||
bool upperCaseFirstInitialized = false;
|
||||
|
||||
/**
|
||||
* Precomputes the available locales which use upper-case first sorting.
|
||||
*/
|
||||
bool ensureUpperCaseFirstLocales(JSContext* cx);
|
||||
|
||||
public:
|
||||
/**
|
||||
* Sets |isUpperFirst| to true if |locale| sorts upper-case characters
|
||||
* before lower-case characters.
|
||||
*/
|
||||
bool isUpperCaseFirst(JSContext* cx, JS::HandleString locale, bool* isUpperFirst);
|
||||
|
||||
public:
|
||||
void destroyInstance();
|
||||
|
||||
void trace(JSTracer* trc);
|
||||
|
||||
size_t sizeOfExcludingThis(mozilla::MallocSizeOf mallocSizeOf) const;
|
||||
};
|
||||
|
||||
} // namespace intl
|
||||
|
||||
} // namespace js
|
||||
|
||||
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
|
||||
* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||
|
||||
#ifndef builtin_intl_SharedIntlData_h
|
||||
#define builtin_intl_SharedIntlData_h
|
||||
|
||||
#include "mozilla/MemoryReporting.h"
|
||||
|
||||
#include <stddef.h>
|
||||
|
||||
#include "jsalloc.h"
|
||||
#include "js/CharacterEncoding.h"
|
||||
#include "js/GCAPI.h"
|
||||
#include "js/GCHashTable.h"
|
||||
#include "js/RootingAPI.h"
|
||||
#include "js/Utility.h"
|
||||
#include "vm/String.h"
|
||||
|
||||
namespace js {
|
||||
|
||||
namespace intl {
|
||||
|
||||
/**
|
||||
* Stores Intl data which can be shared across compartments (but not contexts).
|
||||
*
|
||||
* Used for data which is expensive when computed repeatedly or is not
|
||||
* available through ICU.
|
||||
*/
|
||||
class SharedIntlData
|
||||
{
|
||||
struct LinearStringLookup
|
||||
{
|
||||
union {
|
||||
const JS::Latin1Char* latin1Chars;
|
||||
const char16_t* twoByteChars;
|
||||
};
|
||||
bool isLatin1;
|
||||
size_t length;
|
||||
JS::AutoCheckCannotGC nogc;
|
||||
HashNumber hash = 0;
|
||||
|
||||
explicit LinearStringLookup(JSLinearString* string)
|
||||
: isLatin1(string->hasLatin1Chars()), length(string->length())
|
||||
{
|
||||
if (isLatin1)
|
||||
latin1Chars = string->latin1Chars(nogc);
|
||||
else
|
||||
twoByteChars = string->twoByteChars(nogc);
|
||||
}
|
||||
|
||||
LinearStringLookup(const char* chars, size_t length)
|
||||
: isLatin1(true), length(length)
|
||||
{
|
||||
latin1Chars = reinterpret_cast<const JS::Latin1Char*>(chars);
|
||||
}
|
||||
};
|
||||
|
||||
private:
|
||||
/**
|
||||
* Information tracking the set of the supported time zone names, derived
|
||||
* from the IANA time zone database <https://www.iana.org/time-zones>.
|
||||
*
|
||||
* There are two kinds of IANA time zone names: Zone and Link (denoted as
|
||||
* such in database source files). Zone names are the canonical, preferred
|
||||
* name for a time zone, e.g. Asia/Kolkata. Link names simply refer to
|
||||
* target Zone names for their meaning, e.g. Asia/Calcutta targets
|
||||
* Asia/Kolkata. That a name is a Link doesn't *necessarily* reflect a
|
||||
* sense of deprecation: some Link names also exist partly for convenience,
|
||||
* e.g. UTC and GMT as Link names targeting the Zone name Etc/UTC.
|
||||
*
|
||||
* Two data sources determine the time zone names we support: those ICU
|
||||
* supports and IANA's zone information.
|
||||
*
|
||||
* Unfortunately the names ICU and IANA support, and their Link
|
||||
* relationships from name to target, aren't identical, so we can't simply
|
||||
* implicitly trust ICU's name handling. We must perform various
|
||||
* preprocessing of user-provided zone names and post-processing of
|
||||
* ICU-provided zone names to implement ECMA-402's IANA-consistent behavior.
|
||||
*
|
||||
* Also see <https://ssl.icu-project.org/trac/ticket/12044> and
|
||||
* <http://unicode.org/cldr/trac/ticket/9892>.
|
||||
*/
|
||||
|
||||
using TimeZoneName = JSAtom*;
|
||||
|
||||
struct TimeZoneHasher
|
||||
{
|
||||
struct Lookup : LinearStringLookup
|
||||
{
|
||||
explicit Lookup(JSFlatString* timeZone);
|
||||
};
|
||||
|
||||
static js::HashNumber hash(const Lookup& lookup) { return lookup.hash; }
|
||||
static bool match(TimeZoneName key, const Lookup& lookup);
|
||||
};
|
||||
|
||||
using TimeZoneSet = js::GCHashSet<TimeZoneName,
|
||||
TimeZoneHasher,
|
||||
js::SystemAllocPolicy>;
|
||||
|
||||
using TimeZoneMap = js::GCHashMap<TimeZoneName,
|
||||
TimeZoneName,
|
||||
TimeZoneHasher,
|
||||
js::SystemAllocPolicy>;
|
||||
|
||||
/**
|
||||
* As a threshold matter, available time zones are those time zones ICU
|
||||
* supports, via ucal_openTimeZones. But ICU supports additional non-IANA
|
||||
* time zones described in intl/icu/source/tools/tzcode/icuzones (listed in
|
||||
* IntlTimeZoneData.cpp's |legacyICUTimeZones|) for its own backwards
|
||||
* compatibility purposes. This set consists of ICU's supported time zones,
|
||||
* minus all backwards-compatibility time zones.
|
||||
*/
|
||||
TimeZoneSet availableTimeZones;
|
||||
|
||||
/**
|
||||
* IANA treats some time zone names as Zones, that ICU instead treats as
|
||||
* Links. For example, IANA considers "America/Indiana/Indianapolis" to be
|
||||
* a Zone and "America/Fort_Wayne" a Link that targets it, but ICU
|
||||
* considers the former a Link that targets "America/Indianapolis" (which
|
||||
* IANA treats as a Link).
|
||||
*
|
||||
* ECMA-402 requires that we respect IANA data, so if we're asked to
|
||||
* canonicalize a time zone name in this set, we must *not* return ICU's
|
||||
* canonicalization.
|
||||
*/
|
||||
TimeZoneSet ianaZonesTreatedAsLinksByICU;
|
||||
|
||||
/**
|
||||
* IANA treats some time zone names as Links to one target, that ICU
|
||||
* instead treats as either Zones, or Links to different targets. An
|
||||
* example of the former is "Asia/Calcutta, which IANA assigns the target
|
||||
* "Asia/Kolkata" but ICU considers its own Zone. An example of the latter
|
||||
* is "America/Virgin", which IANA assigns the target
|
||||
* "America/Port_of_Spain" but ICU assigns the target "America/St_Thomas".
|
||||
*
|
||||
* ECMA-402 requires that we respect IANA data, so if we're asked to
|
||||
* canonicalize a time zone name that's a key in this map, we *must* return
|
||||
* the corresponding value and *must not* return ICU's canonicalization.
|
||||
*/
|
||||
TimeZoneMap ianaLinksCanonicalizedDifferentlyByICU;
|
||||
|
||||
bool timeZoneDataInitialized = false;
|
||||
|
||||
/**
|
||||
* Precomputes the available time zone names, because it's too expensive to
|
||||
* call ucal_openTimeZones() repeatedly.
|
||||
*/
|
||||
bool ensureTimeZones(JSContext* cx);
|
||||
|
||||
public:
|
||||
/**
|
||||
* Returns the validated time zone name in |result|. If the input time zone
|
||||
* isn't a valid IANA time zone name, |result| remains unchanged.
|
||||
*/
|
||||
bool validateTimeZoneName(JSContext* cx, JS::HandleString timeZone,
|
||||
JS::MutableHandleString result);
|
||||
|
||||
/**
|
||||
* Returns the canonical time zone name in |result|. If no canonical name
|
||||
* was found, |result| remains unchanged.
|
||||
*
|
||||
* This method only handles time zones which are canonicalized differently
|
||||
* by ICU when compared to IANA.
|
||||
*/
|
||||
bool tryCanonicalizeTimeZoneConsistentWithIANA(JSContext* cx, JS::HandleString timeZone,
|
||||
JS::MutableHandleString result);
|
||||
private:
|
||||
using Locale = JSAtom*;
|
||||
|
||||
struct LocaleHasher
|
||||
{
|
||||
struct Lookup : LinearStringLookup
|
||||
{
|
||||
explicit Lookup(JSLinearString* locale);
|
||||
Lookup(const char* chars, size_t length);
|
||||
};
|
||||
|
||||
static js::HashNumber hash(const Lookup& lookup) { return lookup.hash; }
|
||||
static bool match(Locale key, const Lookup& lookup);
|
||||
};
|
||||
|
||||
using LocaleSet = GCHashSet<Locale, LocaleHasher, SystemAllocPolicy>;
|
||||
|
||||
// Set of supported locales for all Intl service constructors except Collator,
|
||||
// which uses its own set.
|
||||
//
|
||||
// UDateFormat:
|
||||
// udat_[count,get]Available() return the same results as their
|
||||
// uloc_[count,get]Available() counterparts.
|
||||
//
|
||||
// UNumberFormatter:
|
||||
// unum_[count,get]Available() return the same results as their
|
||||
// uloc_[count,get]Available() counterparts.
|
||||
//
|
||||
// UListFormatter, UPluralRules and URelativeDateTimeFormatter:
|
||||
// We're going to use ULocale availableLocales as per ICU recommendation:
|
||||
// https://unicode-org.atlassian.net/browse/ICU-12756
|
||||
LocaleSet supportedLocales;
|
||||
|
||||
// ucol_[count,get]Available() return different results compared to
|
||||
// uloc_[count,get]Available(), we can't use |supportedLocales| here.
|
||||
LocaleSet collatorSupportedLocales;
|
||||
|
||||
bool supportedLocalesInitialized = false;
|
||||
|
||||
// CountAvailable and GetAvailable describe the signatures used for ICU API
|
||||
// to determine available locales for various functionality.
|
||||
using CountAvailable = int32_t (*)();
|
||||
using GetAvailable = const char* (*)(int32_t localeIndex);
|
||||
|
||||
static bool getAvailableLocales(JSContext* cx, LocaleSet& locales,
|
||||
CountAvailable countAvailable,
|
||||
GetAvailable getAvailable);
|
||||
|
||||
/**
|
||||
* Precomputes the available locales sets.
|
||||
*/
|
||||
bool ensureSupportedLocales(JSContext* cx);
|
||||
|
||||
public:
|
||||
enum class SupportedLocaleKind {
|
||||
Collator,
|
||||
DateTimeFormat,
|
||||
ListFormat,
|
||||
NumberFormat,
|
||||
PluralRules,
|
||||
RelativeTimeFormat
|
||||
};
|
||||
|
||||
/**
|
||||
* Sets |supported| to true if |locale| is supported by the requested Intl
|
||||
* service constructor. Otherwise sets |supported| to false.
|
||||
*/
|
||||
[[nodiscard]] bool isSupportedLocale(JSContext* cx, SupportedLocaleKind kind,
|
||||
JS::Handle<JSString*> locale,
|
||||
bool* supported);
|
||||
|
||||
private:
|
||||
/**
|
||||
* The case first parameter (BCP47 key "kf") allows to switch the order of
|
||||
* upper- and lower-case characters. ICU doesn't directly provide an API
|
||||
* to query the default case first value of a given locale, but instead
|
||||
* requires to instantiate a collator object and then query the case first
|
||||
* attribute (UCOL_CASE_FIRST).
|
||||
* To avoid instantiating an additional collator object whenever we need
|
||||
* to retrieve the default case first value of a specific locale, we
|
||||
* compute the default case first value for every supported locale only
|
||||
* once and then keep a list of all locales which don't use the default
|
||||
* case first setting.
|
||||
* There is almost no difference between lower-case first and when case
|
||||
* first is disabled (UCOL_LOWER_FIRST resp. UCOL_OFF), so we only need to
|
||||
* track locales which use upper-case first as their default setting.
|
||||
*/
|
||||
|
||||
LocaleSet upperCaseFirstLocales;
|
||||
|
||||
bool upperCaseFirstInitialized = false;
|
||||
|
||||
/**
|
||||
* Precomputes the available locales which use upper-case first sorting.
|
||||
*/
|
||||
bool ensureUpperCaseFirstLocales(JSContext* cx);
|
||||
|
||||
public:
|
||||
/**
|
||||
* Sets |isUpperFirst| to true if |locale| sorts upper-case characters
|
||||
* before lower-case characters.
|
||||
*/
|
||||
bool isUpperCaseFirst(JSContext* cx, JS::HandleString locale, bool* isUpperFirst);
|
||||
|
||||
public:
|
||||
void destroyInstance();
|
||||
|
||||
void trace(JSTracer* trc);
|
||||
|
||||
size_t sizeOfExcludingThis(mozilla::MallocSizeOf mallocSizeOf) const;
|
||||
};
|
||||
|
||||
} // namespace intl
|
||||
|
||||
} // namespace js
|
||||
|
||||
#endif /* builtin_intl_SharedIntlData_h */
|
||||
@@ -28,6 +28,7 @@
|
||||
\
|
||||
_(IntlIsCollator) \
|
||||
_(IntlIsDateTimeFormat) \
|
||||
_(IntlIsListFormat) \
|
||||
_(IntlIsNumberFormat) \
|
||||
_(IntlIsPluralRules) \
|
||||
_(IntlIsRelativeTimeFormat) \
|
||||
|
||||
@@ -14,6 +14,7 @@
|
||||
#include "builtin/TypedObject.h"
|
||||
#include "builtin/intl/Collator.h"
|
||||
#include "builtin/intl/DateTimeFormat.h"
|
||||
#include "builtin/intl/ListFormat.h"
|
||||
#include "builtin/intl/NumberFormat.h"
|
||||
#include "builtin/intl/PluralRules.h"
|
||||
#include "builtin/intl/RelativeTimeFormat.h"
|
||||
@@ -113,6 +114,8 @@ IonBuilder::inlineNativeCall(CallInfo& callInfo, JSFunction* target)
|
||||
return inlineHasClass(callInfo, &CollatorObject::class_);
|
||||
case InlinableNative::IntlIsDateTimeFormat:
|
||||
return inlineHasClass(callInfo, &DateTimeFormatObject::class_);
|
||||
case InlinableNative::IntlIsListFormat:
|
||||
return inlineHasClass(callInfo, &ListFormatObject::class_);
|
||||
case InlinableNative::IntlIsNumberFormat:
|
||||
return inlineHasClass(callInfo, &NumberFormatObject::class_);
|
||||
case InlinableNative::IntlIsPluralRules:
|
||||
|
||||
@@ -2877,6 +2877,11 @@ ToWindowIfWindowProxy(JSObject* obj);
|
||||
extern bool
|
||||
AddMozDateTimeFormatConstructor(JSContext* cx, JS::Handle<JSObject*> intl);
|
||||
|
||||
// Create and add the Intl.ListFormat constructor function to the provided
|
||||
// object.
|
||||
extern bool
|
||||
AddListFormatConstructor(JSContext* cx, JS::Handle<JSObject*> intl);
|
||||
|
||||
} /* namespace js */
|
||||
|
||||
class NativeProfiler
|
||||
|
||||
@@ -96,6 +96,7 @@
|
||||
real(Symbol, InitSymbolClass, OCLASP(Symbol)) \
|
||||
IF_SAB(real,imaginary)(SharedArrayBuffer, InitViaClassSpec, OCLASP(SharedArrayBuffer)) \
|
||||
IF_INTL(real,imaginary) (Intl, InitIntlClass, CLASP(Intl)) \
|
||||
IF_INTL(real,imaginary) (ListFormat, InitViaClassSpec, OCLASP(ListFormat)) \
|
||||
IF_BDATA(real,imaginary)(TypedObject, InitTypedObjectModuleObject, OCLASP(TypedObjectModule)) \
|
||||
real(Reflect, InitReflect, nullptr) \
|
||||
real(WeakSet, InitWeakSetClass, OCLASP(WeakSet)) \
|
||||
|
||||
@@ -126,6 +126,7 @@ main_deunified_sources = [
|
||||
'builtin/intl/IntlObject.cpp',
|
||||
'builtin/intl/LanguageTag.cpp',
|
||||
'builtin/intl/LanguageTagGenerated.cpp',
|
||||
'builtin/intl/ListFormat.cpp',
|
||||
'builtin/intl/Locale.cpp',
|
||||
'builtin/intl/NumberFormat.cpp',
|
||||
'builtin/intl/PluralRules.cpp',
|
||||
@@ -745,6 +746,7 @@ selfhosted.inputs = [
|
||||
'builtin/intl/CommonFunctions.js',
|
||||
'builtin/intl/DateTimeFormat.js',
|
||||
'builtin/intl/IntlObject.js',
|
||||
'builtin/intl/ListFormat.js',
|
||||
'builtin/intl/NumberFormat.js',
|
||||
'builtin/intl/PluralRules.js',
|
||||
'builtin/intl/RelativeTimeFormat.js',
|
||||
|
||||
@@ -935,6 +935,10 @@ AddIntlExtras(JSContext* cx, unsigned argc, Value* vp)
|
||||
if (!js::AddMozDateTimeFormatConstructor(cx, intl))
|
||||
return false;
|
||||
|
||||
if (!js::AddListFormatConstructor(cx, intl)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
args.rval().setUndefined();
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -118,6 +118,7 @@
|
||||
macro(dotStaticInitializers, dotStaticInitializers, ".staticInitializers") \
|
||||
macro(dotStaticFieldKeys, dotStaticFieldKeys, ".staticFieldKeys") \
|
||||
macro(each, each, "each") \
|
||||
macro(element, element, "element") \
|
||||
macro(elementType, elementType, "elementType") \
|
||||
macro(else, else_, "else") \
|
||||
macro(empty, empty, "") \
|
||||
@@ -204,6 +205,7 @@
|
||||
macro(Infinity, Infinity, "Infinity") \
|
||||
macro(InitializeCollator, InitializeCollator, "InitializeCollator") \
|
||||
macro(InitializeDateTimeFormat, InitializeDateTimeFormat, "InitializeDateTimeFormat") \
|
||||
macro(InitializeListFormat, InitializeListFormat, "InitializeListFormat") \
|
||||
macro(InitializeLocale, InitializeLocale, "InitializeLocale") \
|
||||
macro(InitializeNumberFormat, InitializeNumberFormat, "InitializeNumberFormat") \
|
||||
macro(InitializePluralRules, InitializePluralRules, "InitializePluralRules") \
|
||||
|
||||
@@ -29,6 +29,7 @@
|
||||
#include "builtin/WeakMapObject.h"
|
||||
#include "builtin/WeakRefObject.h"
|
||||
#include "builtin/WeakSetObject.h"
|
||||
#include "builtin/intl/ListFormat.h"
|
||||
#include "vm/Debugger.h"
|
||||
#include "vm/EnvironmentObject.h"
|
||||
#include "vm/HelperThreads.h"
|
||||
|
||||
@@ -26,6 +26,7 @@
|
||||
#include "builtin/intl/Collator.h"
|
||||
#include "builtin/intl/DateTimeFormat.h"
|
||||
#include "builtin/intl/IntlObject.h"
|
||||
#include "builtin/intl/ListFormat.h"
|
||||
#include "builtin/intl/Locale.h"
|
||||
#include "builtin/intl/NumberFormat.h"
|
||||
#include "builtin/intl/PluralRules.h"
|
||||
@@ -2556,6 +2557,7 @@ static const JSFunctionSpec intrinsic_functions[] = {
|
||||
JS_FN("intl_patternForStyle", intl_patternForStyle, 6, 0),
|
||||
JS_FN("intl_GetPluralCategories", intl_GetPluralCategories, 2, 0),
|
||||
JS_FN("intl_SelectPluralRule", intl_SelectPluralRule, 2,0),
|
||||
JS_FN("intl_FormatList", intl_FormatList, 3, 0),
|
||||
JS_FN("intl_toLocaleLowerCase", intl_toLocaleLowerCase, 2,0),
|
||||
JS_FN("intl_toLocaleUpperCase", intl_toLocaleUpperCase, 2,0),
|
||||
JS_FN("intl_ValidateAndCanonicalizeLanguageTag", intl_ValidateAndCanonicalizeLanguageTag, 2, 0),
|
||||
@@ -2569,6 +2571,9 @@ static const JSFunctionSpec intrinsic_functions[] = {
|
||||
JS_INLINABLE_FN("IsDateTimeFormat",
|
||||
intrinsic_IsInstanceOfBuiltin<DateTimeFormatObject>, 1,0,
|
||||
IntlIsDateTimeFormat),
|
||||
JS_INLINABLE_FN("IsListFormat",
|
||||
intrinsic_IsInstanceOfBuiltin<ListFormatObject>, 1, 0,
|
||||
IntlIsListFormat),
|
||||
JS_INLINABLE_FN("IsNumberFormat",
|
||||
intrinsic_IsInstanceOfBuiltin<NumberFormatObject>, 1,0,
|
||||
IntlIsNumberFormat),
|
||||
|
||||
Reference in new issue
Block a user