JSC: Intl API should ignore encoding when parsing BCP 47 language tag from ISO 15897 locale string (passed via LANG)
https://bugs.webkit.org/show_bug.cgi?id=167991

Patch by Andy VanWagoner <andy@vanwagoner.family> on 2018-07-26
Reviewed by Michael Catanzaro.

Source/JavaScriptCore:

Improved the conversion of ICU locales to BCP47 tags, using their preferred method.
Checked locale.isEmpty() before returning it from defaultLocale, so there should be
no more cases where you might have an invalid locale come back from resolveLocale.

* runtime/IntlObject.cpp:
(JSC::convertICULocaleToBCP47LanguageTag):
(JSC::defaultLocale):
(JSC::lookupMatcher):
* runtime/IntlObject.h:
* runtime/JSGlobalObject.cpp:
(JSC::JSGlobalObject::intlCollatorAvailableLocales):
(JSC::JSGlobalObject::intlDateTimeFormatAvailableLocales):
(JSC::JSGlobalObject::intlNumberFormatAvailableLocales):
(JSC::JSGlobalObject::intlPluralRulesAvailableLocales):

LayoutTests:

Replaced expecting throwing a runtime error to avoid a crash, with testing for good default locale fallback behavior.

* js/intl-default-locale-expected.txt: Added.
* js/intl-default-locale.html: Added.
* js/intl-invalid-locale-crash-expected.txt: Removed.
* js/intl-invalid-locale-crash.html: Removed.
* platform/win/TestExpectations:

git-svn-id: http://svn.webkit.org/repository/webkit/trunk@234260 268f45cc-cd09-0410-ab3c-d52691b4dbfc
diff --git a/LayoutTests/js/intl-default-locale.html b/LayoutTests/js/intl-default-locale.html
new file mode 100644
index 0000000..b105958
--- /dev/null
+++ b/LayoutTests/js/intl-default-locale.html
@@ -0,0 +1,21 @@
+<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML//EN">
+<html>
+<head>
+<meta charset="utf-8">
+<script src="../resources/js-test-pre.js"></script>
+</head>
+<body>
+<script>
+if (window.internals) {
+    // Any language name with less than two characters is considered invalid, so we use "a" here.
+    // "i-klingon" is grandfathered, and is canonicalized "tlh".
+    // It should not be part of any available locale sets, so we know it came from here.
+    window.internals.setUserPreferredLanguages([ "a", "*", "en_US.utf8", "i-klingon", "en-US" ]);
+}
+shouldBe("new Intl.DateTimeFormat().resolvedOptions().locale", "'tlh'");
+shouldBe("new Intl.NumberFormat().resolvedOptions().locale", "'tlh'");
+shouldBe("new Intl.Collator().resolvedOptions().locale", "'tlh'");
+</script>
+<script src="../resources/js-test-post.js"></script>
+</body>
+</html>