mirror of
https://github.com/mozilla/gecko-dev.git
synced 2024-11-24 21:31:04 +00:00
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:
parent
9075afbca0
commit
214a38a3e8
@ -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.
|
||||
|
5
intl/locale/tests/unit/data/bug1174386_worker.js
Normal file
5
intl/locale/tests/unit/data/bug1174386_worker.js
Normal file
@ -0,0 +1,5 @@
|
||||
self.onmessage = function (data) {
|
||||
let myLocale = Intl.NumberFormat().resolvedOptions().locale;
|
||||
self.postMessage(myLocale);
|
||||
};
|
||||
|
1
intl/locale/tests/unit/data/chrome.manifest
Normal file
1
intl/locale/tests/unit/data/chrome.manifest
Normal file
@ -0,0 +1 @@
|
||||
content locale ./
|
12
intl/locale/tests/unit/test_bug1174386.js
Normal file
12
intl/locale/tests/unit/test_bug1174386.js
Normal 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!");
|
||||
}
|
@ -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]
|
||||
|
@ -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)
|
||||
{
|
||||
|
@ -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.
|
||||
*/
|
||||
|
Loading…
Reference in New Issue
Block a user