CommonFunctions.h (4074B)
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/GCVector.h" 14 #include "js/RootingAPI.h" 15 #include "js/Utility.h" 16 17 namespace mozilla::intl { 18 enum class ICUError : uint8_t; 19 } 20 21 namespace js { 22 23 class PropertyName; 24 25 namespace intl { 26 27 /** 28 * Initialize a new Intl.* object using the named self-hosted function. 29 */ 30 extern bool InitializeObject(JSContext* cx, JS::Handle<JSObject*> obj, 31 JS::Handle<PropertyName*> initializer, 32 JS::Handle<JS::Value> locales, 33 JS::Handle<JS::Value> options); 34 35 enum class DateTimeFormatOptions { 36 Standard, 37 EnableMozExtensions, 38 }; 39 40 /** 41 * Initialize an existing object as an Intl.DateTimeFormat object. 42 */ 43 extern bool InitializeDateTimeFormatObject( 44 JSContext* cx, JS::Handle<JSObject*> obj, JS::Handle<JS::Value> thisValue, 45 JS::Handle<JS::Value> locales, JS::Handle<JS::Value> options, 46 JS::Handle<JSString*> required, JS::Handle<JSString*> defaults, 47 JS::Handle<JS::Value> toLocaleStringTimeZone, 48 DateTimeFormatOptions dtfOptions, JS::MutableHandle<JS::Value> result); 49 50 /** 51 * Initialize an existing object as an Intl.NumberFormat object. 52 */ 53 extern bool InitializeNumberFormatObject(JSContext* cx, 54 JS::Handle<JSObject*> obj, 55 JS::Handle<JS::Value> thisValue, 56 JS::Handle<JS::Value> locales, 57 JS::Handle<JS::Value> options, 58 JS::MutableHandle<JS::Value> result); 59 60 /** 61 * Returns the object holding the internal properties for obj. 62 */ 63 extern JSObject* GetInternalsObject(JSContext* cx, JS::Handle<JSObject*> obj); 64 65 /** Report an Intl internal error not directly tied to a spec step. */ 66 extern void ReportInternalError(JSContext* cx); 67 68 /** Report an Intl internal error not directly tied to a spec step. */ 69 extern void ReportInternalError(JSContext* cx, mozilla::intl::ICUError error); 70 71 /** 72 * The last-ditch locale is used if none of the available locales satisfies a 73 * request. "en-GB" is used based on the assumptions that English is the most 74 * common second language, that both en-GB and en-US are normally available in 75 * an implementation, and that en-GB is more representative of the English used 76 * in other locales. 77 */ 78 static inline const char* LastDitchLocale() { return "en-GB"; } 79 80 /** 81 * Certain old, commonly-used language tags that lack a script, are expected to 82 * nonetheless imply one. This object maps these old-style tags to modern 83 * equivalents. 84 */ 85 struct OldStyleLanguageTagMapping { 86 const char* const oldStyle; 87 const char* const modernStyle; 88 89 // Provide a constructor to catch missing initializers in the mappings array. 90 constexpr OldStyleLanguageTagMapping(const char* oldStyle, 91 const char* modernStyle) 92 : oldStyle(oldStyle), modernStyle(modernStyle) {} 93 }; 94 95 extern const OldStyleLanguageTagMapping oldStyleLanguageTagMappings[5]; 96 97 extern JS::UniqueChars EncodeLocale(JSContext* cx, JSString* locale); 98 99 // The inline capacity we use for a Vector<char16_t>. Use this to ensure that 100 // our uses of ICU string functions, below and elsewhere, will try to fill the 101 // buffer's entire inline capacity before growing it and heap-allocating. 102 constexpr size_t INITIAL_CHAR_BUFFER_SIZE = 32; 103 104 void AddICUCellMemory(JSObject* obj, size_t nbytes); 105 106 void RemoveICUCellMemory(JSObject* obj, size_t nbytes); 107 108 void RemoveICUCellMemory(JS::GCContext* gcx, JSObject* obj, size_t nbytes); 109 } // namespace intl 110 111 } // namespace js 112 113 #endif /* builtin_intl_CommonFunctions_h */