Debian Codegraph @

js/src/builtin/intl/CommonFunctions.h

raw
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 <stddef.h>
11 #include <stdint.h>
12
13 #include "js/RootingAPI.h"
14 #include "js/Utility.h"
15
16 namespace mozilla::intl {
17 enum class ICUError : uint8_t;
18 }
19
20 namespace js {
21
22 class PropertyName;
23
24 namespace intl {
25
26 /**
27 * Initialize a new Intl.* object using the named self-hosted function.
28 */
29 extern bool InitializeObject(JSContext* cx, JS::Handle<JSObject*> obj,
30 JS::Handle<PropertyName*> initializer,
31 JS::Handle<JS::Value> locales,
32 JS::Handle<JS::Value> options);
33
34 enum class DateTimeFormatOptions {
35 Standard,
36 EnableMozExtensions,
37 };
38
39 /**
40 * Initialize an existing object as an Intl.DateTimeFormat object.
41 */
42 extern bool InitializeDateTimeFormatObject(
43 JSContext* cx, JS::Handle<JSObject*> obj, JS::Handle<JS::Value> thisValue,
44 JS::Handle<JS::Value> locales, JS::Handle<JS::Value> options,
45 JS::Handle<JSString*> required, JS::Handle<JSString*> defaults,
46 JS::Handle<JS::Value> toLocaleStringTimeZone,
47 DateTimeFormatOptions dtfOptions, JS::MutableHandle<JS::Value> result);
48
49 /**
50 * Initialize an existing object as an Intl.NumberFormat object.
51 */
52 extern bool InitializeNumberFormatObject(JSContext* cx,
53 JS::Handle<JSObject*> obj,
54 JS::Handle<JS::Value> thisValue,
55 JS::Handle<JS::Value> locales,
56 JS::Handle<JS::Value> options,
57 JS::MutableHandle<JS::Value> result);
58
59 /**
60 * Returns the object holding the internal properties for obj.
61 */
62 extern JSObject* GetInternalsObject(JSContext* cx, JS::Handle<JSObject*> obj);
63
64 /** Report an Intl internal error not directly tied to a spec step. */
65 extern void ReportInternalError(JSContext* cx);
66
67 /** Report an Intl internal error not directly tied to a spec step. */
68 extern void ReportInternalError(JSContext* cx, mozilla::intl::ICUError error);
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 */
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.
89 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 JS::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(JSObject* obj, size_t nbytes);
106
107 void RemoveICUCellMemory(JS::GCContext* gcx, JSObject* obj, size_t nbytes);
108
109 JSLinearString* CanonicalizeTimeZone(JSContext* cx,
110 JS::Handle<JSString*> timeZone);
111 } // namespace intl
112
113 } // namespace js
114
115 #endif /* builtin_intl_CommonFunctions_h */