Bug 1174386 - Make workers inherit the default Intl locale from the main thread, rather than using a bogus fallback value. r=jwalden

--HG--
extra : rebase_source : bb58c75416d4942affef3617ec427b2745261cf3
This commit is contained in:
Arthur Edelstein 2016-07-11 18:57:25 -07:00
parent 9075afbca0
commit 214a38a3e8
7 changed files with 59 additions and 4 deletions

View File

@ -987,6 +987,7 @@ class WorkerThreadPrimaryRunnable final : public Runnable
WorkerPrivate* mWorkerPrivate;
RefPtr<WorkerThread> mThread;
JSRuntime* mParentRuntime;
JS::UniqueChars mDefaultLocale;
class FinishedRunnable final : public Runnable
{
@ -1011,8 +1012,10 @@ class WorkerThreadPrimaryRunnable final : public Runnable
public:
WorkerThreadPrimaryRunnable(WorkerPrivate* aWorkerPrivate,
WorkerThread* aThread,
JSRuntime* aParentRuntime)
: mWorkerPrivate(aWorkerPrivate), mThread(aThread), mParentRuntime(aParentRuntime)
JSRuntime* aParentRuntime,
JS::UniqueChars aDefaultLocale)
: mWorkerPrivate(aWorkerPrivate), mThread(aThread)
, mParentRuntime(aParentRuntime), mDefaultLocale(Move(aDefaultLocale))
{
MOZ_ASSERT(aWorkerPrivate);
MOZ_ASSERT(aThread);
@ -1626,9 +1629,15 @@ RuntimeService::ScheduleWorker(WorkerPrivate* aWorkerPrivate)
}
JSRuntime* rt = CycleCollectedJSRuntime::Get()->Runtime();
JSRuntime* parentRuntime = JS_GetParentRuntime(rt);
JS::UniqueChars defaultLocale = parentRuntime ? JS_GetDefaultLocale(parentRuntime) : nullptr;
if (!parentRuntime) {
NS_WARNING("Could not obtain parent runtime's locale!");
}
nsCOMPtr<nsIRunnable> runnable =
new WorkerThreadPrimaryRunnable(aWorkerPrivate, thread,
JS_GetParentRuntime(rt));
new WorkerThreadPrimaryRunnable(aWorkerPrivate, thread, parentRuntime,
Move(defaultLocale));
if (NS_FAILED(thread->DispatchPrimaryRunnable(friendKey, runnable.forget()))) {
UnregisterWorker(aWorkerPrivate);
return false;
@ -2540,6 +2549,14 @@ WorkerThreadPrimaryRunnable::Run()
JSRuntime* rt = runtime.Runtime();
if (mDefaultLocale) {
if (!JS_SetDefaultLocale(rt, mDefaultLocale.get())) {
NS_WARNING("Could not set worker locale!");
}
mDefaultLocale = nullptr;
}
JSContext* cx = InitJSContextForWorker(mWorkerPrivate, rt);
if (!cx) {
// XXX need to fire an error at parent.

View File

@ -0,0 +1,5 @@
self.onmessage = function (data) {
let myLocale = Intl.NumberFormat().resolvedOptions().locale;
self.postMessage(myLocale);
};

View File

@ -0,0 +1 @@
content locale ./

View File

@ -0,0 +1,12 @@
function run_test() {
do_load_manifest("data/chrome.manifest");
do_test_pending();
let mainThreadLocale = Intl.NumberFormat().resolvedOptions().locale;
let testWorker = new Worker("chrome://locale/content/bug1174386_worker.js");
testWorker.onmessage = function (e) {
let workerLocale = e.data;
equal(mainThreadLocale, workerLocale, "Worker should inherit Intl locale from main thread.");
do_test_finished();
};
testWorker.postMessage("go!");
}

View File

@ -2,6 +2,9 @@
head =
tail =
skip-if = toolkit == 'gonk'
support-files =
data/bug1174386_worker.js
data/chrome.manifest
[test_bug22310.js]
skip-if = toolkit != "windows" && toolkit != "cocoa"
@ -14,6 +17,7 @@ skip-if = toolkit == "windows" || toolkit == "cocoa"
skip-if = toolkit != "cocoa"
[test_bug1086527.js]
[test_bug1174386.js]
[test_pluralForm.js]
[test_pluralForm_english.js]
[test_pluralForm_makeGetter.js]

View File

@ -5819,6 +5819,16 @@ JS_SetDefaultLocale(JSContext* cx, const char* locale)
return cx->setDefaultLocale(locale);
}
JS_PUBLIC_API(UniqueChars)
JS_GetDefaultLocale(JSRuntime* rt)
{
AssertHeapIsIdle(rt);
if (const char* locale = rt->getDefaultLocale())
return UniqueChars(JS_strdup(rt, locale));
return nullptr;
}
JS_PUBLIC_API(void)
JS_ResetDefaultLocale(JSContext* cx)
{

View File

@ -5038,6 +5038,12 @@ JS_ParseJSONWithReviver(JSContext* cx, JS::HandleString str, JS::HandleValue rev
extern JS_PUBLIC_API(bool)
JS_SetDefaultLocale(JSContext* cx, const char* locale);
/**
* Look up the default locale for the ECMAScript Internationalization API.
*/
extern JS_PUBLIC_API(JS::UniqueChars)
JS_GetDefaultLocale(JSRuntime* rt);
/**
* Reset the default locale to OS defaults.
*/