Bug 1398396 - Make locale functions take a runtime instead of a context. r=janv,jorendorff

Most of these functions already solely operate on the runtime even though they
take a context. That leads to confusion at the API user level since it looks
like they're stored on the context. JS_GetDefaultLocale still takes a context
because it actually does use the passed-in context.

MozReview-Commit-ID: 4d0LQIBExvg

--HG--
extra : rebase_source : 79370897a9dca839a6760411ea95d97c5e92cb06
This commit is contained in:
Blake Kaplan 2017-09-11 15:25:40 -07:00
parent 5774ce7105
commit 9b6798ea6f
4 changed files with 26 additions and 21 deletions

View File

@ -9,6 +9,8 @@
BEGIN_TEST(testDateToLocaleString)
{
JSRuntime* rt = JS_GetRuntime(cx);
// This test should only attempt to run if we have Intl support: necessary
// to properly assume that changes to the default locale will predictably
// affect the behavior of the locale-sensitive Date methods tested here.
@ -22,7 +24,7 @@ BEGIN_TEST(testDateToLocaleString)
// Date.prototype.toLocale{,Date,Time}String behavior.
// Start with German.
CHECK(JS_SetDefaultLocale(cx, "de"));
CHECK(JS_SetDefaultLocale(rt, "de"));
// The (constrained) Date object we'll use to test behavior.
EXEC("var d = new Date(Date.UTC(2015, 9 - 1, 17));");
@ -30,25 +32,25 @@ BEGIN_TEST(testDateToLocaleString)
// Test that toLocaleString behavior changes with default locale changes.
EXEC("var deAll = d.toLocaleString();");
CHECK(JS_SetDefaultLocale(cx, "en"));
CHECK(JS_SetDefaultLocale(rt, "en"));
EXEC("if (d.toLocaleString() === deAll) \n"
" throw 'toLocaleString results should have changed with system locale change';");
// Test that toLocaleDateString behavior changes with default locale changes.
EXEC("var enDate = d.toLocaleDateString();");
CHECK(JS_SetDefaultLocale(cx, "de"));
CHECK(JS_SetDefaultLocale(rt, "de"));
EXEC("if (d.toLocaleDateString() === enDate) \n"
" throw 'toLocaleDateString results should have changed with system locale change';");
// Test that toLocaleTimeString behavior changes with default locale changes.
EXEC("var deTime = d.toLocaleTimeString();");
CHECK(JS_SetDefaultLocale(cx, "en"));
CHECK(JS_SetDefaultLocale(rt, "en"));
EXEC("if (d.toLocaleTimeString() === deTime) \n"
" throw 'toLocaleTimeString results should have changed with system locale change';");
JS_ResetDefaultLocale(cx);
JS_ResetDefaultLocale(rt);
return true;
}
END_TEST(testDateToLocaleString)

View File

@ -9,6 +9,8 @@
BEGIN_TEST(testIntlAvailableLocales)
{
JSRuntime* rt = JS_GetRuntime(cx);
// This test should only attempt to run if we have Intl support.
JS::Rooted<JS::Value> haveIntl(cx);
EVAL("typeof Intl !== 'undefined'", &haveIntl);
@ -17,7 +19,7 @@ BEGIN_TEST(testIntlAvailableLocales)
// Assumption: our Intl support always includes "de" (German) support,
// and our Intl support *does not* natively support de-ZA-ghijk. :-)
CHECK(JS_SetDefaultLocale(cx, "de-ZA-abcde-x-private"));
CHECK(JS_SetDefaultLocale(rt, "de-ZA-abcde-x-private"));
EXEC("if (Intl.Collator().resolvedOptions().locale !== 'de-ZA-abcde-x-private') \n"
" throw 'unexpected default locale';");
@ -41,18 +43,18 @@ BEGIN_TEST(testIntlAvailableLocales)
EXEC("if (Intl.Collator('de').resolvedOptions().locale !== 'de') \n"
" throw 'bad locale when using most-truncated default';");
CHECK(JS_SetDefaultLocale(cx, "en-US-u-co-phonebk"));
CHECK(JS_SetDefaultLocale(rt, "en-US-u-co-phonebk"));
EXEC("if (Intl.Collator().resolvedOptions().locale !== 'en-US') \n"
" throw 'unexpected default locale where proposed default included a Unicode extension';");
CHECK(JS_SetDefaultLocale(cx, "this is not a language tag at all, yo"));
CHECK(JS_SetDefaultLocale(rt, "this is not a language tag at all, yo"));
EXEC("if (Intl.Collator().resolvedOptions().locale !== 'en-GB') \n"
" throw 'unexpected last-ditch locale';");
EXEC("if (Intl.Collator('en-GB').resolvedOptions().locale !== 'en-GB') \n"
" throw 'unexpected used locale when specified, with last-ditch locale as default';");
JS_ResetDefaultLocale(cx);
JS_ResetDefaultLocale(rt);
return true;
}
END_TEST(testIntlAvailableLocales)

View File

@ -6725,10 +6725,10 @@ JS_GetRegExpSource(JSContext* cx, HandleObject obj)
/************************************************************************/
JS_PUBLIC_API(bool)
JS_SetDefaultLocale(JSContext* cx, const char* locale)
JS_SetDefaultLocale(JSRuntime* rt, const char* locale)
{
AssertHeapIsIdle();
return cx->runtime()->setDefaultLocale(locale);
return rt->setDefaultLocale(locale);
}
JS_PUBLIC_API(UniqueChars)
@ -6742,24 +6742,24 @@ JS_GetDefaultLocale(JSContext* cx)
}
JS_PUBLIC_API(void)
JS_ResetDefaultLocale(JSContext* cx)
JS_ResetDefaultLocale(JSRuntime* rt)
{
AssertHeapIsIdle();
cx->runtime()->resetDefaultLocale();
rt->resetDefaultLocale();
}
JS_PUBLIC_API(void)
JS_SetLocaleCallbacks(JSContext* cx, const JSLocaleCallbacks* callbacks)
JS_SetLocaleCallbacks(JSRuntime* rt, const JSLocaleCallbacks* callbacks)
{
AssertHeapIsIdle();
cx->runtime()->localeCallbacks = callbacks;
rt->localeCallbacks = callbacks;
}
JS_PUBLIC_API(const JSLocaleCallbacks*)
JS_GetLocaleCallbacks(JSContext* cx)
JS_GetLocaleCallbacks(JSRuntime* rt)
{
/* This function can be called by a finalizer. */
return cx->runtime()->localeCallbacks;
return rt->localeCallbacks;
}
/************************************************************************/

View File

@ -5520,10 +5520,11 @@ JS_ParseJSONWithReviver(JSContext* cx, JS::HandleString str, JS::HandleValue rev
* The locale string remains owned by the caller.
*/
extern JS_PUBLIC_API(bool)
JS_SetDefaultLocale(JSContext* cx, const char* locale);
JS_SetDefaultLocale(JSRuntime* rt, const char* locale);
/**
* Look up the default locale for the ECMAScript Internationalization API.
* NB: The locale information is retrieved from cx's runtime.
*/
extern JS_PUBLIC_API(JS::UniqueChars)
JS_GetDefaultLocale(JSContext* cx);
@ -5532,7 +5533,7 @@ JS_GetDefaultLocale(JSContext* cx);
* Reset the default locale to OS defaults.
*/
extern JS_PUBLIC_API(void)
JS_ResetDefaultLocale(JSContext* cx);
JS_ResetDefaultLocale(JSRuntime* rt);
/**
* Locale specific string conversion and error message callbacks.
@ -5549,14 +5550,14 @@ struct JSLocaleCallbacks {
* JSContext. Passing nullptr restores the default behaviour.
*/
extern JS_PUBLIC_API(void)
JS_SetLocaleCallbacks(JSContext* cx, const JSLocaleCallbacks* callbacks);
JS_SetLocaleCallbacks(JSRuntime* rt, const JSLocaleCallbacks* callbacks);
/**
* Return the address of the current locale callbacks struct, which may
* be nullptr.
*/
extern JS_PUBLIC_API(const JSLocaleCallbacks*)
JS_GetLocaleCallbacks(JSContext* cx);
JS_GetLocaleCallbacks(JSRuntime* rt);
/************************************************************************/