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_DisplayNames_h
8 #define builtin_intl_DisplayNames_h
9 
10 #include <stddef.h>
11 #include <stdint.h>
12 
13 #include "jstypes.h"
14 #include "NamespaceImports.h"
15 
16 #include "builtin/SelfHostingDefines.h"
17 #include "js/Class.h"  // JSClass, JSClassOps, js::ClassSpec
18 #include "js/Value.h"
19 #include "vm/JSObject.h"
20 #include "vm/List.h"
21 #include "vm/NativeObject.h"
22 
23 struct JS_PUBLIC_API JSContext;
24 class JS_PUBLIC_API JSFreeOp;
25 
26 namespace mozilla::intl {
27 class DisplayNames;
28 }
29 
30 namespace js {
31 struct ClassSpec;
32 
33 class DisplayNamesObject : public NativeObject {
34  public:
35   static const JSClass class_;
36   static const JSClass& protoClass_;
37 
38   static constexpr uint32_t INTERNALS_SLOT = 0;
39   static constexpr uint32_t LOCALE_DISPLAY_NAMES_SLOT = 1;
40   static constexpr uint32_t SLOT_COUNT = 3;
41 
42   static_assert(INTERNALS_SLOT == INTL_INTERNALS_OBJECT_SLOT,
43                 "INTERNALS_SLOT must match self-hosting define for internals "
44                 "object slot");
45 
46   // Estimated memory use for ULocaleDisplayNames (see IcuMemoryUsage).
47   static constexpr size_t EstimatedMemoryUse = 1238;
48 
getDisplayNames()49   mozilla::intl::DisplayNames* getDisplayNames() const {
50     const auto& slot = getFixedSlot(LOCALE_DISPLAY_NAMES_SLOT);
51     if (slot.isUndefined()) {
52       return nullptr;
53     }
54     return static_cast<mozilla::intl::DisplayNames*>(slot.toPrivate());
55   }
56 
setDisplayNames(mozilla::intl::DisplayNames * displayNames)57   void setDisplayNames(mozilla::intl::DisplayNames* displayNames) {
58     setFixedSlot(LOCALE_DISPLAY_NAMES_SLOT, PrivateValue(displayNames));
59   }
60 
61  private:
62   static const JSClassOps classOps_;
63   static const ClassSpec classSpec_;
64 
65   static void finalize(JSFreeOp* fop, JSObject* obj);
66 };
67 
68 /**
69  * Return the display name for the requested code or undefined if no applicable
70  * display name was found.
71  *
72  * Usage: result = intl_ComputeDisplayName(displayNames, locale, calendar,
73  *                                         style, languageDisplay, fallback,
74  *                                         type, code)
75  */
76 [[nodiscard]] extern bool intl_ComputeDisplayName(JSContext* cx, unsigned argc,
77                                                   Value* vp);
78 
79 }  // namespace js
80 
81 #endif /* builtin_intl_DisplayNames_h */
82