Ticket #13019: 0001-Bug-5926-Allow-customization-of-content-JS-locale.patch

File 0001-Bug-5926-Allow-customization-of-content-JS-locale.patch, 5.1 KB (added by arthuredelstein, 3 years ago)
  • js/src/jsapi.cpp

    From b3149dc9796e830574efe1625245ecda3bee647a Mon Sep 17 00:00:00 2001
    From: Arthur Edelstein <arthuredelstein@gmail.com>
    Date: Wed, 1 Oct 2014 21:54:57 -0700
    Subject: [PATCH] Bug #5926: Allow customization of content JS locale.
    
    ---
     js/src/jsapi.cpp            |  7 +++++
     js/src/jsapi.h              |  9 ++++++
     xpcom/build/nsXPComInit.cpp | 69 +++++++++++++++++++++++++++++++++++++++++++++
     3 files changed, 85 insertions(+)
    
    diff --git a/js/src/jsapi.cpp b/js/src/jsapi.cpp
    index 7b1c886..cd5f9c4 100644
    a b JS_SetDefaultLocale(JSRuntime *rt, const char *locale) 
    58795879    return rt->setDefaultLocale(locale);
    58805880}
    58815881
     5882JS_PUBLIC_API(const char*)
     5883JS_GetDefaultLocale(JSRuntime *rt)
     5884{
     5885    AssertHeapIsIdle(rt);
     5886    return rt->getDefaultLocale();
     5887}
     5888
    58825889JS_PUBLIC_API(void)
    58835890JS_ResetDefaultLocale(JSRuntime *rt)
    58845891{
  • js/src/jsapi.h

    diff --git a/js/src/jsapi.h b/js/src/jsapi.h
    index 5ae1f86..98543e5 100644
    a b extern JS_PUBLIC_API(bool) 
    43254325JS_SetDefaultLocale(JSRuntime *rt, const char *locale);
    43264326
    43274327/*
     4328 * Returns the default locale for the ECMAScript Internationalization API
     4329 * (Intl.Collator, Intl.NumberFormat, Intl.DateTimeFormat).
     4330 * Note that the Internationalization API encourages clients to
     4331 * specify their own locales.
     4332 */
     4333extern JS_PUBLIC_API(const char*)
     4334JS_GetDefaultLocale(JSRuntime *rt);
     4335
     4336/*
    43284337 * Reset the default locale to OS defaults.
    43294338 */
    43304339extern JS_PUBLIC_API(void)
  • xpcom/build/nsXPComInit.cpp

    diff --git a/xpcom/build/nsXPComInit.cpp b/xpcom/build/nsXPComInit.cpp
    index aef309f..979e10b 100644
    a b  
    88
    99#include "mozilla/Atomics.h"
    1010#include "mozilla/Poison.h"
     11#include "mozilla/Preferences.h"
    1112#include "mozilla/XPCOM.h"
    1213#include "nsXULAppAPI.h"
    1314
     
    4142#include "nsSupportsPrimitives.h"
    4243#include "nsConsoleService.h"
    4344
     45#include "nsIJSRuntimeService.h"
     46
    4447#include "nsComponentManager.h"
    4548#include "nsCategoryManagerUtils.h"
    4649#include "nsIServiceManager.h"
    NS_IMPL_ISUPPORTS(NesteggReporter, nsIMemoryReporter) 
    443446/* static */ template<> Atomic<size_t> CountingAllocatorBase<NesteggReporter>::sAmount(0);
    444447#endif /* MOZ_WEBM */
    445448
     449// Anonymous namespace for customizing the default locale that JavaScript
     450// uses, according to the value of the "javascript.default_locale" pref.
     451// The current default locale can be detected in JavaScript by calling
     452// `Intl.DateTimeFormat().resolvedOptions().locale`
     453namespace {
     454
     455#define DEFAULT_LOCALE_PREF "javascript.default_locale"
     456
     457static char* sSystemLocale;
     458static char* sJSLocale;
     459
     460// Returns a pointer to the current JS Runtime.
     461static
     462JSRuntime* GetRuntime() {
     463  nsresult rv;
     464  nsCOMPtr<nsIJSRuntimeService> rts = do_GetService("@mozilla.org/js/xpc/RuntimeService;1", &rv);
     465  if (NS_FAILED(rv)) return NULL;
     466  JSRuntime* rt;
     467  rts->GetRuntime(&rt);
     468  return rt;
     469}
     470
     471// Takes the "javascript.default_locale" pref value and applies it. If the locale
     472// is empty, we fall back to the default system and JS locales.
     473static
     474void DefaultLocaleChangedCallback(const char* /* pref */, void* /* closure */) {
     475  // Get a pointer to the default JS Runtime.
     476  JSRuntime* rt = GetRuntime();
     477  if (rt) {
     478    // Read the pref, which may contain a custom default locale to be used in JavaScript.
     479    nsAutoCString prefLocale;
     480    mozilla::Preferences::GetCString(DEFAULT_LOCALE_PREF, &prefLocale);
     481    // Set the application-wide C-locale. Needed for Date.toLocaleFormat().
     482    setlocale(LC_ALL, prefLocale.IsEmpty() ? sSystemLocale : prefLocale.get());
     483    // Now override the JavaScript Runtime Locale that is used by the Intl API
     484    // as well as Date.toLocaleString, Number.toLocaleString, and String.localeCompare.
     485    JS_SetDefaultLocale(rt, prefLocale.IsEmpty() ? sJSLocale : prefLocale.get());
     486  }
     487}
     488
     489static
     490void StartWatchingDefaultLocalePref() {
     491  // Get the default system locale. To be used if pref is not available.
     492  sSystemLocale = strdup(setlocale(LC_ALL,NULL));
     493  // Store the default JavaScript locale.
     494  JSRuntime* rt = GetRuntime();
     495  if (rt) {
     496    sJSLocale = strdup(JS_GetDefaultLocale(rt));
     497  }
     498  // Now keep the locale updated with the current pref value.
     499  mozilla::Preferences::RegisterCallbackAndCall(DefaultLocaleChangedCallback, DEFAULT_LOCALE_PREF);
     500}
     501
     502static
     503void StopWatchingDefaultLocalePref() {
     504  mozilla::Preferences::UnregisterCallback(DefaultLocaleChangedCallback, DEFAULT_LOCALE_PREF);
     505  if (sSystemLocale) free(sSystemLocale);
     506  if (sJSLocale) JS_free(nullptr, sJSLocale);
     507}
     508
     509} // anonymous namespace for locale pref
     510
    446511EXPORT_XPCOM_API(nsresult)
    447512NS_InitXPCOM2(nsIServiceManager* *result,
    448513              nsIFile* binDirectory,
    NS_InitXPCOM2(nsIServiceManager* *result, 
    701766    mozilla::eventtracer::Init();
    702767#endif
    703768
     769    // Start watching the javascript.default_locale pref.
     770    StartWatchingDefaultLocalePref();
    704771    return NS_OK;
    705772}
    706773
    ShutdownXPCOM(nsIServiceManager* servMgr) 
    9661033        sExitManager = nullptr;
    9671034    }
    9681035
     1036    StopWatchingDefaultLocalePref();
     1037
    9691038    Omnijar::CleanUp();
    9701039
    9711040    HangMonitor::Shutdown();