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/. */
7 /* Shadow definition of |JSString| innards. Don't use this directly! */
9 #ifndef js_shadow_String_h
10 #define js_shadow_String_h
12 #include <stdint.h> // uint32_t, uintptr_t
14 #include "jstypes.h" // JS_PUBLIC_API, js::Bit, js::BitMask, JS_BITS_PER_WORD
16 #include "js/TypeDecls.h" // JS::Latin1Char
18 class JS_PUBLIC_API JSAtom
;
19 struct JSExternalStringCallbacks
;
21 class JS_PUBLIC_API JSString
;
34 static constexpr uint32_t ATOM_BIT
= js::Bit(3);
35 static constexpr uint32_t LINEAR_BIT
= js::Bit(4);
36 static constexpr uint32_t INLINE_CHARS_BIT
= js::Bit(6);
37 static constexpr uint32_t LATIN1_CHARS_BIT
= js::Bit(9);
38 static constexpr uint32_t EXTERNAL_FLAGS
= LINEAR_BIT
| js::Bit(8);
39 static constexpr uint32_t TYPE_FLAGS_MASK
= js::BitMask(9) - js::BitMask(3);
40 static constexpr uint32_t PERMANENT_ATOM_MASK
= ATOM_BIT
| js::Bit(8);
43 #if JS_BITS_PER_WORD == 32
48 const JS::Latin1Char
* nonInlineCharsLatin1
;
49 const char16_t
* nonInlineCharsTwoByte
;
50 JS::Latin1Char inlineStorageLatin1
[1];
51 char16_t inlineStorageTwoByte
[1];
53 const JSExternalStringCallbacks
* externalCallbacks
;
55 uint32_t flags() const { return static_cast<uint32_t>(flags_
); }
56 uint32_t length() const {
57 #if JS_BITS_PER_WORD == 32
60 return static_cast<uint32_t>(flags_
>> 32);
64 static bool isPermanentAtom(const js::gc::Cell
* cell
) {
65 uint32_t flags
= reinterpret_cast<const String
*>(cell
)->flags();
66 return (flags
& PERMANENT_ATOM_MASK
) == PERMANENT_ATOM_MASK
;
69 bool isLinear() const { return flags() & LINEAR_BIT
; }
70 bool hasLatin1Chars() const { return flags() & LATIN1_CHARS_BIT
; }
72 // For hot code, prefer other type queries.
73 bool isExternal() const {
74 return (flags() & TYPE_FLAGS_MASK
) == EXTERNAL_FLAGS
;
77 const JS::Latin1Char
* latin1LinearChars() const {
78 MOZ_ASSERT(isLinear());
79 MOZ_ASSERT(hasLatin1Chars());
80 return (flags() & String::INLINE_CHARS_BIT
) ? inlineStorageLatin1
81 : nonInlineCharsLatin1
;
84 const char16_t
* twoByteLinearChars() const {
85 MOZ_ASSERT(isLinear());
86 MOZ_ASSERT(!hasLatin1Chars());
87 return (flags() & String::INLINE_CHARS_BIT
) ? inlineStorageTwoByte
88 : nonInlineCharsTwoByte
;
92 inline const String
* AsShadowString(const JSString
* str
) {
93 return reinterpret_cast<const String
*>(str
);
96 inline String
* AsShadowString(JSString
* str
) {
97 return reinterpret_cast<String
*>(str
);
100 inline const String
* AsShadowString(const JSLinearString
* str
) {
101 return reinterpret_cast<const String
*>(str
);
104 inline String
* AsShadowString(JSLinearString
* str
) {
105 return reinterpret_cast<String
*>(str
);
108 inline const String
* AsShadowString(const JSAtom
* str
) {
109 return reinterpret_cast<const String
*>(str
);
112 inline String
* AsShadowString(JSAtom
* str
) {
113 return reinterpret_cast<String
*>(str
);
116 } // namespace shadow
120 #endif // js_shadow_String_h