Ignore:
Timestamp:
Feb 27, 2016, 4:36:01 PM (9 years ago)
Author:
[email protected]
Message:

Intl.Collator uses POSIX locale (detected by js/intl-collator.html on iOS Simulator)
https://bugs.webkit.org/show_bug.cgi?id=152448

Patch by Andy VanWagoner <[email protected]> on 2016-02-27
Reviewed by Darin Adler.

Source/JavaScriptCore:

Add defaultLanguage to the globalObjectMethodTable and use it for the
default locale in Intl object initializations. Fall back to ICU default
locale only if the defaultLanguage function is null, or returns an
empty string.

  • jsc.cpp:
  • runtime/IntlCollator.cpp:

(JSC::IntlCollator::initializeCollator):

  • runtime/IntlDateTimeFormat.cpp:

(JSC::IntlDateTimeFormat::initializeDateTimeFormat):

  • runtime/IntlNumberFormat.cpp:

(JSC::IntlNumberFormat::initializeNumberFormat):

  • runtime/IntlObject.cpp:

(JSC::defaultLocale):
(JSC::lookupMatcher):
(JSC::bestFitMatcher):
(JSC::resolveLocale):

  • runtime/IntlObject.h:
  • runtime/JSGlobalObject.cpp:
  • runtime/JSGlobalObject.h:
  • runtime/StringPrototype.cpp:

(JSC::toLocaleCase):

Source/WebCore:

Pass defaultLanguage from Language.h to the globalObjectMethodTable to
ensure Intl objects can be initialized with the correct default locale.

  • bindings/js/JSDOMWindowBase.cpp:
  • bindings/js/JSWorkerGlobalScopeBase.cpp:

LayoutTests:

Add tests for default locale in test runner to be en-US.

  • js/intl-collator-expected.txt:
  • js/intl-datetimeformat-expected.txt:
  • js/intl-numberformat-expected.txt:
  • js/script-tests/intl-collator.js:
  • js/script-tests/intl-datetimeformat.js:
  • js/script-tests/intl-numberformat.js:
Location:
trunk/Source/JavaScriptCore/runtime
Files:
8 edited

Legend:

Unmodified
Added
Removed
  • trunk/Source/JavaScriptCore/runtime/IntlCollator.cpp

    r196887 r197261  
    251251    // 18. Let r be ResolveLocale(%Collator%.[[availableLocales]], requestedLocales, opt, relevantExtensionKeys, localeData).
    252252    auto& availableLocales = state.callee()->globalObject()->intlCollatorAvailableLocales();
    253     auto result = resolveLocale(availableLocales, requestedLocales, opt, relevantExtensionKeys, WTF_ARRAY_LENGTH(relevantExtensionKeys), localeData);
     253    auto result = resolveLocale(state, availableLocales, requestedLocales, opt, relevantExtensionKeys, WTF_ARRAY_LENGTH(relevantExtensionKeys), localeData);
    254254
    255255    // 19. Set collator.[[locale]] to the value of r.[[locale]].
  • trunk/Source/JavaScriptCore/runtime/IntlDateTimeFormat.cpp

    r196887 r197261  
    450450    // 11. Let localeData be the value of %DateTimeFormat%.[[localeData]].
    451451    // 12. Let r be ResolveLocale( %DateTimeFormat%.[[availableLocales]], requestedLocales, opt, %DateTimeFormat%.[[relevantExtensionKeys]], localeData).
    452     const HashSet<String> availableLocales = exec.lexicalGlobalObject()->intlDateTimeFormatAvailableLocales();
    453     HashMap<String, String> resolved = resolveLocale(availableLocales, requestedLocales, localeOpt, relevantExtensionKeys, WTF_ARRAY_LENGTH(relevantExtensionKeys), localeData);
     452    const HashSet<String> availableLocales = exec.callee()->globalObject()->intlDateTimeFormatAvailableLocales();
     453    HashMap<String, String> resolved = resolveLocale(exec, availableLocales, requestedLocales, localeOpt, relevantExtensionKeys, WTF_ARRAY_LENGTH(relevantExtensionKeys), localeData);
    454454
    455455    // 13. Set dateTimeFormat.[[locale]] to the value of r.[[locale]].
  • trunk/Source/JavaScriptCore/runtime/IntlNumberFormat.cpp

    r196850 r197261  
    197197    // 12. Let r be ResolveLocale(%NumberFormat%.[[availableLocales]], requestedLocales, opt, %NumberFormat%.[[relevantExtensionKeys]], localeData).
    198198    auto& availableLocales = state.callee()->globalObject()->intlNumberFormatAvailableLocales();
    199     auto result = resolveLocale(availableLocales, requestedLocales, opt, relevantExtensionKeys, WTF_ARRAY_LENGTH(relevantExtensionKeys), localeData);
     199    auto result = resolveLocale(state, availableLocales, requestedLocales, opt, relevantExtensionKeys, WTF_ARRAY_LENGTH(relevantExtensionKeys), localeData);
    200200
    201201    // 13. Set numberFormat.[[locale]] to the value of r.[[locale]].
  • trunk/Source/JavaScriptCore/runtime/IntlObject.cpp

    r196887 r197261  
    113113{
    114114    return Structure::create(vm, globalObject, prototype, TypeInfo(ObjectType, StructureFlags), info());
    115 }
    116 
    117 String defaultLocale()
    118 {
    119     // 6.2.4 DefaultLocale ()
    120     String locale = uloc_getDefault();
    121     convertICULocaleToBCP47LanguageTag(locale);
    122     return locale;
    123115}
    124116
     
    651643}
    652644
     645String defaultLocale(ExecState& state)
     646{
     647    // 6.2.4 DefaultLocale ()
     648    if (auto defaultLanguage = state.callee()->globalObject()->globalObjectMethodTable()->defaultLanguage) {
     649        String locale = defaultLanguage();
     650        if (!locale.isEmpty())
     651            return canonicalizeLanguageTag(locale);
     652    }
     653    String locale = uloc_getDefault();
     654    convertICULocaleToBCP47LanguageTag(locale);
     655    return locale;
     656}
     657
    653658String removeUnicodeLocaleExtension(const String& locale)
    654659{
     
    673678}
    674679
    675 static MatcherResult lookupMatcher(const HashSet<String>& availableLocales, const Vector<String>& requestedLocales)
     680static MatcherResult lookupMatcher(ExecState& state, const HashSet<String>& availableLocales, const Vector<String>& requestedLocales)
    676681{
    677682    // 9.2.3 LookupMatcher (availableLocales, requestedLocales) (ECMA-402 2.0)
     
    710715        }
    711716    } else
    712         result.locale = defaultLocale();
     717        result.locale = defaultLocale(state);
    713718    return result;
    714719}
    715720
    716 static MatcherResult bestFitMatcher(const HashSet<String>& availableLocales, const Vector<String>& requestedLocales)
     721static MatcherResult bestFitMatcher(ExecState& state, const HashSet<String>& availableLocales, const Vector<String>& requestedLocales)
    717722{
    718723    // 9.2.4 BestFitMatcher (availableLocales, requestedLocales) (ECMA-402 2.0)
    719724    // FIXME: Implement something better than lookup.
    720     return lookupMatcher(availableLocales, requestedLocales);
    721 }
    722 
    723 HashMap<String, String> resolveLocale(const HashSet<String>& availableLocales, const Vector<String>& requestedLocales, const HashMap<String, String>& options, const char* const relevantExtensionKeys[], size_t relevantExtensionKeyCount, Vector<String> (*localeData)(const String&, size_t))
     725    return lookupMatcher(state, availableLocales, requestedLocales);
     726}
     727
     728HashMap<String, String> resolveLocale(ExecState& state, const HashSet<String>& availableLocales, const Vector<String>& requestedLocales, const HashMap<String, String>& options, const char* const relevantExtensionKeys[], size_t relevantExtensionKeyCount, Vector<String> (*localeData)(const String&, size_t))
    724729{
    725730    // 9.2.5 ResolveLocale (availableLocales, requestedLocales, options, relevantExtensionKeys, localeData) (ECMA-402 2.0)
     
    728733
    729734    // 2. If matcher is "lookup", then
    730     MatcherResult (*matcherOperation)(const HashSet<String>&, const Vector<String>&);
     735    MatcherResult (*matcherOperation)(ExecState&, const HashSet<String>&, const Vector<String>&);
    731736    if (matcher == "lookup") {
    732737        // a. Let MatcherOperation be the abstract operation LookupMatcher.
     
    738743
    739744    // 4. Let r be MatcherOperation(availableLocales, requestedLocales).
    740     MatcherResult matcherResult = matcherOperation(availableLocales, requestedLocales);
     745    MatcherResult matcherResult = matcherOperation(state, availableLocales, requestedLocales);
    741746
    742747    // 5. Let foundLocale be the value of r.[[locale]].
  • trunk/Source/JavaScriptCore/runtime/IntlObject.h

    r196434 r197261  
    5858};
    5959
    60 String defaultLocale();
     60String defaultLocale(ExecState&);
    6161void convertICULocaleToBCP47LanguageTag(String& locale);
    6262bool intlBooleanOption(ExecState&, JSValue options, PropertyName, bool& usesFallback);
     
    6464unsigned intlNumberOption(ExecState&, JSValue options, PropertyName, unsigned minimum, unsigned maximum, unsigned fallback);
    6565Vector<String> canonicalizeLocaleList(ExecState&, JSValue locales);
    66 HashMap<String, String> resolveLocale(const HashSet<String>& availableLocales, const Vector<String>& requestedLocales, const HashMap<String, String>& options, const char* const relevantExtensionKeys[], size_t relevantExtensionKeyCount, Vector<String> (*localeData)(const String&, size_t));
     66HashMap<String, String> resolveLocale(ExecState&, const HashSet<String>& availableLocales, const Vector<String>& requestedLocales, const HashMap<String, String>& options, const char* const relevantExtensionKeys[], size_t relevantExtensionKeyCount, Vector<String> (*localeData)(const String&, size_t));
    6767JSValue supportedLocales(ExecState&, const HashSet<String>& availableLocales, const Vector<String>& requestedLocales, JSValue options);
    6868String removeUnicodeLocaleExtension(const String& locale);
  • trunk/Source/JavaScriptCore/runtime/JSGlobalObject.cpp

    r196967 r197261  
    177177const ClassInfo JSGlobalObject::s_info = { "GlobalObject", &Base::s_info, &globalObjectTable, CREATE_METHOD_TABLE(JSGlobalObject) };
    178178
    179 const GlobalObjectMethodTable JSGlobalObject::s_globalObjectMethodTable = { &allowsAccessFrom, &supportsLegacyProfiling, &supportsRichSourceInfo, &shouldInterruptScript, &javaScriptRuntimeFlags, nullptr, &shouldInterruptScriptBeforeTimeout, nullptr, nullptr, nullptr, nullptr, nullptr };
     179const GlobalObjectMethodTable JSGlobalObject::s_globalObjectMethodTable = { &allowsAccessFrom, &supportsLegacyProfiling, &supportsRichSourceInfo, &shouldInterruptScript, &javaScriptRuntimeFlags, nullptr, &shouldInterruptScriptBeforeTimeout, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr };
    180180
    181181/* Source for JSGlobalObject.lut.h
  • trunk/Source/JavaScriptCore/runtime/JSGlobalObject.h

    r196966 r197261  
    174174    typedef JSValue (*ModuleLoaderEvaluatePtr)(JSGlobalObject*, ExecState*, JSValue, JSValue);
    175175    ModuleLoaderEvaluatePtr moduleLoaderEvaluate;
     176
     177    typedef String (*DefaultLanguageFunctionPtr)();
     178    DefaultLanguageFunctionPtr defaultLanguage;
    176179};
    177180
  • trunk/Source/JavaScriptCore/runtime/StringPrototype.cpp

    r196721 r197261  
    15011501    // 8. Else
    15021502    // a. Let requestedLocale be DefaultLocale().
    1503     String requestedLocale = len > 0 ? requestedLocales.first() : defaultLocale();
     1503    String requestedLocale = len > 0 ? requestedLocales.first() : defaultLocale(*state);
    15041504
    15051505    // 9. Let noExtensionsLocale be the String value that is requestedLocale with all Unicode locale extension sequences (6.2.1) removed.
Note: See TracChangeset for help on using the changeset viewer.