1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2  * vim: set ts=8 sts=2 et sw=2 tw=80:
3  * This Source Code Form is subject to the terms of the Mozilla Public
4  * License, v. 2.0. If a copy of the MPL was not distributed with this
5  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 
7 #ifndef builtin_intl_CommonFunctions_h
8 #define builtin_intl_CommonFunctions_h
9 
10 #include "mozilla/Assertions.h"
11 
12 #include <stddef.h>
13 #include <stdint.h>
14 #include <string.h>
15 #include <type_traits>
16 
17 #include "js/RootingAPI.h"
18 #include "js/Vector.h"
19 #include "vm/StringType.h"
20 
21 namespace mozilla::intl {
22 enum class ICUError : uint8_t;
23 }
24 
25 namespace js {
26 
27 namespace intl {
28 
29 /**
30  * Initialize a new Intl.* object using the named self-hosted function.
31  */
32 extern bool InitializeObject(JSContext* cx, JS::Handle<JSObject*> obj,
33                              JS::Handle<PropertyName*> initializer,
34                              JS::Handle<JS::Value> locales,
35                              JS::Handle<JS::Value> options);
36 
37 enum class DateTimeFormatOptions {
38   Standard,
39   EnableMozExtensions,
40 };
41 
42 /**
43  * Initialize an existing object as an Intl.* object using the named
44  * self-hosted function.  This is only for a few old Intl.* constructors, for
45  * legacy reasons -- new ones should use the function above instead.
46  */
47 extern bool LegacyInitializeObject(JSContext* cx, JS::Handle<JSObject*> obj,
48                                    JS::Handle<PropertyName*> initializer,
49                                    JS::Handle<JS::Value> thisValue,
50                                    JS::Handle<JS::Value> locales,
51                                    JS::Handle<JS::Value> options,
52                                    DateTimeFormatOptions dtfOptions,
53                                    JS::MutableHandle<JS::Value> result);
54 
55 /**
56  * Returns the object holding the internal properties for obj.
57  */
58 extern JSObject* GetInternalsObject(JSContext* cx, JS::Handle<JSObject*> obj);
59 
60 /** Report an Intl internal error not directly tied to a spec step. */
61 extern void ReportInternalError(JSContext* cx);
62 
63 /** Report an Intl internal error not directly tied to a spec step. */
64 extern void ReportInternalError(JSContext* cx, mozilla::intl::ICUError error);
65 
StringsAreEqual(const char * s1,const char * s2)66 static inline bool StringsAreEqual(const char* s1, const char* s2) {
67   return !strcmp(s1, s2);
68 }
69 
70 /**
71  * The last-ditch locale is used if none of the available locales satisfies a
72  * request. "en-GB" is used based on the assumptions that English is the most
73  * common second language, that both en-GB and en-US are normally available in
74  * an implementation, and that en-GB is more representative of the English used
75  * in other locales.
76  */
LastDitchLocale()77 static inline const char* LastDitchLocale() { return "en-GB"; }
78 
79 /**
80  * Certain old, commonly-used language tags that lack a script, are expected to
81  * nonetheless imply one. This object maps these old-style tags to modern
82  * equivalents.
83  */
84 struct OldStyleLanguageTagMapping {
85   const char* const oldStyle;
86   const char* const modernStyle;
87 
88   // Provide a constructor to catch missing initializers in the mappings array.
OldStyleLanguageTagMappingOldStyleLanguageTagMapping89   constexpr OldStyleLanguageTagMapping(const char* oldStyle,
90                                        const char* modernStyle)
91       : oldStyle(oldStyle), modernStyle(modernStyle) {}
92 };
93 
94 extern const OldStyleLanguageTagMapping oldStyleLanguageTagMappings[5];
95 
96 extern UniqueChars EncodeLocale(JSContext* cx, JSString* locale);
97 
98 // The inline capacity we use for a Vector<char16_t>.  Use this to ensure that
99 // our uses of ICU string functions, below and elsewhere, will try to fill the
100 // buffer's entire inline capacity before growing it and heap-allocating.
101 constexpr size_t INITIAL_CHAR_BUFFER_SIZE = 32;
102 
103 void AddICUCellMemory(JSObject* obj, size_t nbytes);
104 
105 void RemoveICUCellMemory(JSFreeOp* fop, JSObject* obj, size_t nbytes);
106 }  // namespace intl
107 
108 }  // namespace js
109 
110 #endif /* builtin_intl_CommonFunctions_h */
111