Skip to content

Commit bb1e8a6

Browse files
TimothyGuMylesBorins
authored andcommitted
deps: cherry-pick 6989b3f6d7 from V8 upstream
Original commit message: Fix default Intl language tag handling With certain ICU data bundles (such as the Node.js "small-icu"), %GetDefaultICULocale() may return a more specific language tag (e.g. "en-US") than what's available (e.g. "en"). In those cases, consider the more specific language tag supported. This CL also resolves the following Node.js issue: #15223 Bug: v8:7024 Cq-Include-Trybots: luci.v8.try:v8_linux_noi18n_rel_ng Change-Id: Ifda0776b3418734d5caa8af4e50c17cda95add73 Reviewed-on: https://chromium-review.googlesource.com/668350 Commit-Queue: Daniel Ehrenberg <[email protected]> Reviewed-by: Daniel Ehrenberg <[email protected]> Cr-Commit-Position: refs/heads/master@{#52716} PR-URL: #20826 Fixes: #15223 Refs: v8/v8@6989b3f Reviewed-By: James M Snell <[email protected]> Reviewed-By: Anatoli Papirovski <[email protected]> Reviewed-By: Ben Noordhuis <[email protected]>
1 parent c6b4601 commit bb1e8a6

File tree

12 files changed

+119
-46
lines changed

12 files changed

+119
-46
lines changed

deps/v8/include/v8-version.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
#define V8_MAJOR_VERSION 6
1212
#define V8_MINOR_VERSION 2
1313
#define V8_BUILD_NUMBER 414
14-
#define V8_PATCH_LEVEL 64
14+
#define V8_PATCH_LEVEL 65
1515

1616
// Use 1 for candidates and 0 otherwise.
1717
// (Boolean macro values are not supported by all preprocessors.)

deps/v8/src/js/intl.js

Lines changed: 59 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -135,18 +135,11 @@ var AVAILABLE_LOCALES = {
135135
*/
136136
var DEFAULT_ICU_LOCALE = UNDEFINED;
137137

138-
function GetDefaultICULocaleJS(service) {
138+
function GetDefaultICULocaleJS() {
139139
if (IS_UNDEFINED(DEFAULT_ICU_LOCALE)) {
140140
DEFAULT_ICU_LOCALE = %GetDefaultICULocale();
141141
}
142-
// Check that this is a valid default for this service,
143-
// otherwise fall back to "und"
144-
// TODO(littledan,jshin): AvailableLocalesOf sometimes excludes locales
145-
// which don't require tailoring, but work fine with root data. Look into
146-
// exposing this fact in ICU or the way Chrome bundles data.
147-
return (IS_UNDEFINED(service) ||
148-
HAS_OWN_PROPERTY(getAvailableLocalesOf(service), DEFAULT_ICU_LOCALE))
149-
? DEFAULT_ICU_LOCALE : "und";
142+
return DEFAULT_ICU_LOCALE;
150143
}
151144

152145
/**
@@ -417,6 +410,48 @@ function resolveLocale(service, requestedLocales, options) {
417410
}
418411

419412

413+
/**
414+
* Look up the longest non-empty prefix of |locale| that is an element of
415+
* |availableLocales|. Returns undefined when the |locale| is completely
416+
* unsupported by |availableLocales|.
417+
*/
418+
function bestAvailableLocale(availableLocales, locale) {
419+
do {
420+
if (!IS_UNDEFINED(availableLocales[locale])) {
421+
return locale;
422+
}
423+
// Truncate locale if possible.
424+
var pos = %StringLastIndexOf(locale, '-');
425+
if (pos === -1) {
426+
break;
427+
}
428+
locale = %_Call(StringSubstring, locale, 0, pos);
429+
} while (true);
430+
431+
return UNDEFINED;
432+
}
433+
434+
435+
/**
436+
* Try to match any mutation of |requestedLocale| against |availableLocales|.
437+
*/
438+
function attemptSingleLookup(availableLocales, requestedLocale) {
439+
// Remove all extensions.
440+
var noExtensionsLocale = %RegExpInternalReplace(
441+
GetAnyExtensionRE(), requestedLocale, '');
442+
var availableLocale = bestAvailableLocale(
443+
availableLocales, requestedLocale);
444+
if (!IS_UNDEFINED(availableLocale)) {
445+
// Return the resolved locale and extension.
446+
var extensionMatch = %regexp_internal_match(
447+
GetUnicodeExtensionRE(), requestedLocale);
448+
var extension = IS_NULL(extensionMatch) ? '' : extensionMatch[0];
449+
return {locale: availableLocale, extension: extension};
450+
}
451+
return UNDEFINED;
452+
}
453+
454+
420455
/**
421456
* Returns best matched supported locale and extension info using basic
422457
* lookup algorithm.
@@ -429,31 +464,25 @@ function lookupMatcher(service, requestedLocales) {
429464
var availableLocales = getAvailableLocalesOf(service);
430465

431466
for (var i = 0; i < requestedLocales.length; ++i) {
432-
// Remove all extensions.
433-
var locale = %RegExpInternalReplace(
434-
GetAnyExtensionRE(), requestedLocales[i], '');
435-
do {
436-
if (!IS_UNDEFINED(availableLocales[locale])) {
437-
// Return the resolved locale and extension.
438-
var extensionMatch = %regexp_internal_match(
439-
GetUnicodeExtensionRE(), requestedLocales[i]);
440-
var extension = IS_NULL(extensionMatch) ? '' : extensionMatch[0];
441-
return {locale: locale, extension: extension, position: i};
442-
}
443-
// Truncate locale if possible.
444-
var pos = %StringLastIndexOf(locale, '-');
445-
if (pos === -1) {
446-
break;
447-
}
448-
locale = %_Call(StringSubstring, locale, 0, pos);
449-
} while (true);
467+
var result = attemptSingleLookup(availableLocales, requestedLocales[i]);
468+
if (!IS_UNDEFINED(result)) {
469+
return result;
470+
}
471+
}
472+
473+
var defLocale = GetDefaultICULocaleJS();
474+
475+
// While ECMA-402 returns defLocale directly, we have to check if it is
476+
// supported, as such support is not guaranteed.
477+
var result = attemptSingleLookup(availableLocales, defLocale);
478+
if (!IS_UNDEFINED(result)) {
479+
return result;
450480
}
451481

452482
// Didn't find a match, return default.
453483
return {
454-
locale: GetDefaultICULocaleJS(service),
455-
extension: '',
456-
position: -1
484+
locale: 'und',
485+
extension: ''
457486
};
458487
}
459488

deps/v8/test/intl/assert.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,16 @@ function assertFalse(value, user_message = '') {
132132
}
133133

134134

135+
/**
136+
* Throws if value is null.
137+
*/
138+
function assertNotNull(value, user_message = '') {
139+
if (value === null) {
140+
fail("not null", value, user_message);
141+
}
142+
}
143+
144+
135145
/**
136146
* Runs code() and asserts that it throws the specified exception.
137147
*/
@@ -189,3 +199,34 @@ function assertInstanceof(obj, type) {
189199
(actualTypeName ? ' but of < ' + actualTypeName + '>' : ''));
190200
}
191201
}
202+
203+
204+
/**
205+
* Split a BCP 47 language tag into locale and extension.
206+
*/
207+
function splitLanguageTag(tag) {
208+
var extRe = /(-[0-9A-Za-z](-[0-9A-Za-z]{2,8})+)+$/;
209+
var match = %regexp_internal_match(extRe, tag);
210+
if (match) {
211+
return { locale: tag.slice(0, match.index), extension: match[0] };
212+
}
213+
214+
return { locale: tag, extension: '' };
215+
}
216+
217+
218+
/**
219+
* Throw if |parent| is not a more general language tag of |child|, nor |child|
220+
* itself, per BCP 47 rules.
221+
*/
222+
function assertLanguageTag(child, parent) {
223+
var childSplit = splitLanguageTag(child);
224+
var parentSplit = splitLanguageTag(parent);
225+
226+
// Do not compare extensions at this moment, as %GetDefaultICULocale()
227+
// doesn't always output something we support.
228+
if (childSplit.locale !== parentSplit.locale &&
229+
!childSplit.locale.startsWith(parentSplit.locale + '-')) {
230+
fail(child, parent, 'language tag comparison');
231+
}
232+
}

deps/v8/test/intl/break-iterator/default-locale.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ assertFalse(options.locale === 'und');
3737
assertFalse(options.locale === '');
3838
assertFalse(options.locale === undefined);
3939

40-
// Then check for equality.
41-
assertEquals(options.locale, %GetDefaultICULocale());
40+
// Then check for legitimacy.
41+
assertLanguageTag(%GetDefaultICULocale(), options.locale);
4242

4343
var iteratorNone = new Intl.v8BreakIterator();
4444
assertEquals(options.locale, iteratorNone.resolvedOptions().locale);

deps/v8/test/intl/break-iterator/wellformed-unsupported-locale.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,4 @@
2929

3030
var iterator = Intl.v8BreakIterator(['xx']);
3131

32-
assertEquals(iterator.resolvedOptions().locale, %GetDefaultICULocale());
32+
assertLanguageTag(%GetDefaultICULocale(), iterator.resolvedOptions().locale);

deps/v8/test/intl/collator/default-locale.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ assertFalse(options.locale === 'und');
3737
assertFalse(options.locale === '');
3838
assertFalse(options.locale === undefined);
3939

40-
// Then check for equality.
41-
assertEquals(options.locale, %GetDefaultICULocale());
40+
// Then check for legitimacy.
41+
assertLanguageTag(%GetDefaultICULocale(), options.locale);
4242

4343
var collatorNone = new Intl.Collator();
4444
assertEquals(options.locale, collatorNone.resolvedOptions().locale);
@@ -48,5 +48,8 @@ var collatorBraket = new Intl.Collator({});
4848
assertEquals(options.locale, collatorBraket.resolvedOptions().locale);
4949

5050
var collatorWithOptions = new Intl.Collator(undefined, {usage: 'search'});
51-
assertEquals(%GetDefaultICULocale() + '-u-co-search',
52-
collatorWithOptions.resolvedOptions().locale);
51+
assertLanguageTag(%GetDefaultICULocale(),
52+
collatorWithOptions.resolvedOptions().locale);
53+
assertNotNull(
54+
%regexp_internal_match(/-u(-[a-zA-Z]+-[a-zA-Z]+)*-co-search/,
55+
collatorWithOptions.resolvedOptions().locale));

deps/v8/test/intl/collator/wellformed-unsupported-locale.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,4 @@
2929

3030
var collator = Intl.Collator(['xx']);
3131

32-
assertEquals(collator.resolvedOptions().locale, %GetDefaultICULocale());
32+
assertLanguageTag(%GetDefaultICULocale(), collator.resolvedOptions().locale);

deps/v8/test/intl/date-format/default-locale.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ assertFalse(options.locale === 'und');
3737
assertFalse(options.locale === '');
3838
assertFalse(options.locale === undefined);
3939

40-
// Then check for equality.
41-
assertEquals(options.locale, %GetDefaultICULocale());
40+
// Then check for legitimacy.
41+
assertLanguageTag(%GetDefaultICULocale(), options.locale);
4242

4343
var dtfNone = new Intl.DateTimeFormat();
4444
assertEquals(options.locale, dtfNone.resolvedOptions().locale);

deps/v8/test/intl/date-format/wellformed-unsupported-locale.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,4 @@
2929

3030
var dtf = Intl.DateTimeFormat(['xx']);
3131

32-
assertEquals(dtf.resolvedOptions().locale, %GetDefaultICULocale());
32+
assertLanguageTag(%GetDefaultICULocale(), dtf.resolvedOptions().locale);

deps/v8/test/intl/number-format/default-locale.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ assertFalse(options.locale === 'und');
3737
assertFalse(options.locale === '');
3838
assertFalse(options.locale === undefined);
3939

40-
// Then check for equality.
41-
assertEquals(options.locale, %GetDefaultICULocale());
40+
// Then check for legitimacy.
41+
assertLanguageTag(%GetDefaultICULocale(), options.locale);
4242

4343
var nfNone = new Intl.NumberFormat();
4444
assertEquals(options.locale, nfNone.resolvedOptions().locale);

0 commit comments

Comments
 (0)