Bug 1484420 - Move locale-related functions into js/public/LocaleSensitive.h that isn't #include'd in jsapi.h. r=anba

--HG--
extra : rebase_source : 0c594e4a64373f0432194898ab18f94722c3c54a
This commit is contained in:
Jeff Walden 2018-08-20 17:11:32 -05:00
parent 0db0423880
commit 8acccdc0b8
12 changed files with 108 additions and 66 deletions

View File

@ -29,6 +29,7 @@
#include "mozilla/ipc/BackgroundChild.h"
#include "GeckoProfiler.h"
#include "jsfriendapi.h"
#include "js/LocaleSensitive.h"
#include "mozilla/AbstractThread.h"
#include "mozilla/ArrayUtils.h"
#include "mozilla/AsyncEventDispatcher.h"

View File

@ -6,6 +6,7 @@
#include "WorkerPrivate.h"
#include "js/LocaleSensitive.h"
#include "js/MemoryMetrics.h"
#include "MessageEventRunnable.h"
#include "mozilla/ScopeExit.h"

View File

@ -0,0 +1,99 @@
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
/*
* Functions and structures related to locale-sensitive behavior, including
* exposure of the default locale (used by operations like toLocaleString).
*/
#ifndef js_LocaleSensitive_h
#define js_LocaleSensitive_h
#include "jstypes.h" // JS_PUBLIC_API
#include "js/RootingAPI.h" // JS::Handle, JS::MutableHandle
#include "js/Utility.h" // JS::UniqueChars
struct JSContext;
struct JSRuntime;
class JSString;
namespace JS { union Value; }
/**
* Set the default locale for the ECMAScript Internationalization API
* (Intl.Collator, Intl.NumberFormat, Intl.DateTimeFormat, and others that will
* arise as time passes). (Note that the Internationalization API encourages
* clients to specify their own locales; this default locale is only used when
* no locale is specified, e.g. calling a toLocaleString function without
* passing a locale argument to it.)
*
* The locale string remains owned by the caller.
*/
extern JS_PUBLIC_API(bool)
JS_SetDefaultLocale(JSRuntime* rt, const char* locale);
/**
* Return a copy of the default locale for the ECMAScript Internationalization
* API (and for various ECMAScript functions that will invoke it). The locale
* is retrieved from the |JSRuntime| that corresponds to |cx|.
*
* XXX Bug 1483961 means it's difficult to interpret the meaning of a null
* return value for the time being, and we should fix this!
*/
extern JS_PUBLIC_API(JS::UniqueChars)
JS_GetDefaultLocale(JSContext* cx);
/** Reset the default locale to OS defaults. */
extern JS_PUBLIC_API(void)
JS_ResetDefaultLocale(JSRuntime* rt);
using JSLocaleToUpperCase =
bool (*)(JSContext* cx, JS::Handle<JSString*> src, JS::MutableHandle<JS::Value> rval);
using JSLocaleToLowerCase =
bool (*)(JSContext* cx, JS::Handle<JSString*> src, JS::MutableHandle<JS::Value> rval);
using JSLocaleCompare =
bool (*)(JSContext* cx, JS::Handle<JSString*> src1, JS::Handle<JSString*> src2,
JS::MutableHandle<JS::Value> rval);
using JSLocaleToUnicode =
bool (*)(JSContext* cx, const char* src, JS::MutableHandle<JS::Value> rval);
/**
* A suite of locale-specific string conversion and error message callbacks
* used to implement locale-sensitive behaviors (such as those performed by
* the various toLocaleString and toLocale{Date,Time}String functions).
*
* If SpiderMonkey is compiled --with-intl-api, then #if EXPOSE_INTL_API. In
* this case, SpiderMonkey itself will implement ECMA-402-compliant behavior by
* calling on ICU, and none of the fields in this struct will ever be used.
* (You'll still be able to call the get/set-callbacks functions; they just
* won't affect JavaScript semantics.)
*/
struct JSLocaleCallbacks
{
JSLocaleToUpperCase localeToUpperCase;
JSLocaleToLowerCase localeToLowerCase;
JSLocaleCompare localeCompare;
JSLocaleToUnicode localeToUnicode;
};
/**
* Set locale callbacks to be used in builds not compiled --with-intl-api.
* |callbacks| must persist as long as the |JSRuntime|. Pass |nullptr| to
* restore default behavior.
*/
extern JS_PUBLIC_API(void)
JS_SetLocaleCallbacks(JSRuntime* rt, const JSLocaleCallbacks* callbacks);
/**
* Return the current locale callbacks, which may be nullptr.
*/
extern JS_PUBLIC_API(const JSLocaleCallbacks*)
JS_GetLocaleCallbacks(JSRuntime* rt);
#endif /* js_LocaleSensitive_h */

View File

@ -44,6 +44,7 @@
#include "js/AutoByteString.h"
#include "js/Debug.h"
#include "js/HashTable.h"
#include "js/LocaleSensitive.h"
#include "js/StableStringChars.h"
#include "js/StructuredClone.h"
#include "js/UbiNode.h"

View File

@ -5,6 +5,7 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "js/LocaleSensitive.h"
#include "jsapi-tests/tests.h"
BEGIN_TEST(testDateToLocaleString)

View File

@ -5,6 +5,7 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "js/LocaleSensitive.h"
#include "jsapi-tests/tests.h"
BEGIN_TEST(testIntlAvailableLocales)

View File

@ -61,6 +61,7 @@
#include "js/Date.h"
#include "js/Initialization.h"
#include "js/JSON.h"
#include "js/LocaleSensitive.h"
#include "js/Proxy.h"
#include "js/SliceBudget.h"
#include "js/StableStringChars.h"

View File

@ -223,19 +223,6 @@ typedef void
JS::PromiseRejectionHandlingState state,
void* data);
typedef bool
(* JSLocaleToUpperCase)(JSContext* cx, JS::HandleString src, JS::MutableHandleValue rval);
typedef bool
(* JSLocaleToLowerCase)(JSContext* cx, JS::HandleString src, JS::MutableHandleValue rval);
typedef bool
(* JSLocaleCompare)(JSContext* cx, JS::HandleString src1, JS::HandleString src2,
JS::MutableHandleValue rval);
typedef bool
(* JSLocaleToUnicode)(JSContext* cx, const char* src, JS::MutableHandleValue rval);
/**
* Callback used to ask the embedding for the cross compartment wrapper handler
* that implements the desired prolicy for this kind of object in the
@ -4725,55 +4712,6 @@ PropertySpecNameToPermanentId(JSContext* cx, const char* name, jsid* idp);
/************************************************************************/
/**
* The default locale for the ECMAScript Internationalization API
* (Intl.Collator, Intl.NumberFormat, Intl.DateTimeFormat).
* Note that the Internationalization API encourages clients to
* specify their own locales.
* The locale string remains owned by the caller.
*/
extern JS_PUBLIC_API(bool)
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);
/**
* Reset the default locale to OS defaults.
*/
extern JS_PUBLIC_API(void)
JS_ResetDefaultLocale(JSRuntime* rt);
/**
* Locale specific string conversion and error message callbacks.
*/
struct JSLocaleCallbacks {
JSLocaleToUpperCase localeToUpperCase; // not used #if EXPOSE_INTL_API
JSLocaleToLowerCase localeToLowerCase; // not used #if EXPOSE_INTL_API
JSLocaleCompare localeCompare; // not used #if EXPOSE_INTL_API
JSLocaleToUnicode localeToUnicode;
};
/**
* Establish locale callbacks. The pointer must persist as long as the
* JSContext. Passing nullptr restores the default behaviour.
*/
extern JS_PUBLIC_API(void)
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(JSRuntime* rt);
/************************************************************************/
/*
* Error reporting.
*

View File

@ -68,7 +68,6 @@ struct JSClass;
class JSErrorReport;
struct JSExceptionState;
struct JSFunctionSpec;
struct JSLocaleCallbacks;
struct JSPrincipals;
struct JSPropertySpec;
struct JSSecurityCallbacks;

View File

@ -144,6 +144,7 @@ EXPORTS.js += [
'../public/Id.h',
'../public/Initialization.h',
'../public/JSON.h',
'../public/LocaleSensitive.h',
'../public/MemoryFunctions.h',
'../public/MemoryMetrics.h',
'../public/Principals.h',

View File

@ -69,6 +69,7 @@ class AutoHeapSession;
} // namespace js
struct DtoaState;
struct JSLocaleCallbacks;
#ifdef JS_SIMULATOR_ARM64
namespace vixl {

View File

@ -6,9 +6,8 @@
#include "mozilla/Assertions.h"
#include "jsapi.h"
#include "js/LocaleSensitive.h"
#include "nsJSUtils.h"
#include "nsIObserver.h"
#include "nsComponentManagerUtils.h"
#include "nsServiceManagerUtils.h"
@ -18,7 +17,6 @@
#include "xpcpublic.h"
using namespace JS;
using namespace mozilla;
using mozilla::intl::LocaleService;