Bumping manifests a=b2g-bump
[gecko.git] / js / src / jsatom.h
blobfbdea7cb96ab96df7625b833fd36f1f7b1538ac7
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2 * vim: set ts=8 sts=4 et sw=4 tw=99:
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 #ifndef jsatom_h
8 #define jsatom_h
10 #include "mozilla/HashFunctions.h"
12 #include "jsalloc.h"
14 #include "gc/Barrier.h"
15 #include "gc/Rooting.h"
16 #include "js/GCAPI.h"
17 #include "vm/CommonPropertyNames.h"
19 class JSAtom;
20 class JSAutoByteString;
22 struct JSIdArray {
23 int length;
24 js::HeapId vector[1]; /* actually, length jsid words */
27 namespace js {
29 JS_STATIC_ASSERT(sizeof(HashNumber) == 4);
31 static MOZ_ALWAYS_INLINE js::HashNumber
32 HashId(jsid id)
34 return mozilla::HashGeneric(JSID_BITS(id));
37 struct JsidHasher
39 typedef jsid Lookup;
40 static HashNumber hash(const Lookup& l) {
41 return HashNumber(JSID_BITS(l));
43 static bool match(const jsid& id, const Lookup& l) {
44 return id == l;
49 * Return a printable, lossless char[] representation of a string-type atom.
50 * The lifetime of the result matches the lifetime of bytes.
52 extern const char*
53 AtomToPrintableString(ExclusiveContext* cx, JSAtom* atom, JSAutoByteString* bytes);
55 class AtomStateEntry
57 uintptr_t bits;
59 static const uintptr_t NO_TAG_MASK = uintptr_t(-1) - 1;
61 public:
62 AtomStateEntry() : bits(0) {}
63 AtomStateEntry(const AtomStateEntry& other) : bits(other.bits) {}
64 AtomStateEntry(JSAtom* ptr, bool tagged)
65 : bits(uintptr_t(ptr) | uintptr_t(tagged))
67 MOZ_ASSERT((uintptr_t(ptr) & 0x1) == 0);
70 bool isTagged() const {
71 return bits & 0x1;
75 * Non-branching code sequence. Note that the const_cast is safe because
76 * the hash function doesn't consider the tag to be a portion of the key.
78 void setTagged(bool enabled) const {
79 const_cast<AtomStateEntry*>(this)->bits |= uintptr_t(enabled);
82 JSAtom* asPtr() const;
85 struct AtomHasher
87 struct Lookup
89 union {
90 const JS::Latin1Char* latin1Chars;
91 const char16_t* twoByteChars;
93 bool isLatin1;
94 size_t length;
95 const JSAtom* atom; /* Optional. */
96 JS::AutoCheckCannotGC nogc;
98 HashNumber hash;
100 Lookup(const char16_t* chars, size_t length)
101 : twoByteChars(chars), isLatin1(false), length(length), atom(nullptr)
103 hash = mozilla::HashString(chars, length);
105 Lookup(const JS::Latin1Char* chars, size_t length)
106 : latin1Chars(chars), isLatin1(true), length(length), atom(nullptr)
108 hash = mozilla::HashString(chars, length);
110 inline explicit Lookup(const JSAtom* atom);
113 static HashNumber hash(const Lookup& l) { return l.hash; }
114 static inline bool match(const AtomStateEntry& entry, const Lookup& lookup);
115 static void rekey(AtomStateEntry& k, const AtomStateEntry& newKey) { k = newKey; }
118 typedef HashSet<AtomStateEntry, AtomHasher, SystemAllocPolicy> AtomSet;
120 class PropertyName;
122 } /* namespace js */
124 extern bool
125 AtomIsInterned(JSContext* cx, JSAtom* atom);
127 /* Well-known predefined C strings. */
128 #define DECLARE_PROTO_STR(name,code,init,clasp) extern const char js_##name##_str[];
129 JS_FOR_EACH_PROTOTYPE(DECLARE_PROTO_STR)
130 #undef DECLARE_PROTO_STR
132 #define DECLARE_CONST_CHAR_STR(idpart, id, text) extern const char js_##idpart##_str[];
133 FOR_EACH_COMMON_PROPERTYNAME(DECLARE_CONST_CHAR_STR)
134 #undef DECLARE_CONST_CHAR_STR
136 /* Constant strings that are not atomized. */
137 extern const char js_break_str[];
138 extern const char js_case_str[];
139 extern const char js_catch_str[];
140 extern const char js_class_str[];
141 extern const char js_close_str[];
142 extern const char js_const_str[];
143 extern const char js_continue_str[];
144 extern const char js_debugger_str[];
145 extern const char js_default_str[];
146 extern const char js_do_str[];
147 extern const char js_else_str[];
148 extern const char js_enum_str[];
149 extern const char js_export_str[];
150 extern const char js_extends_str[];
151 extern const char js_finally_str[];
152 extern const char js_for_str[];
153 extern const char js_getter_str[];
154 extern const char js_if_str[];
155 extern const char js_implements_str[];
156 extern const char js_import_str[];
157 extern const char js_in_str[];
158 extern const char js_instanceof_str[];
159 extern const char js_interface_str[];
160 extern const char js_new_str[];
161 extern const char js_package_str[];
162 extern const char js_private_str[];
163 extern const char js_protected_str[];
164 extern const char js_public_str[];
165 extern const char js_send_str[];
166 extern const char js_setter_str[];
167 extern const char js_static_str[];
168 extern const char js_super_str[];
169 extern const char js_switch_str[];
170 extern const char js_this_str[];
171 extern const char js_try_str[];
172 extern const char js_typeof_str[];
173 extern const char js_void_str[];
174 extern const char js_while_str[];
175 extern const char js_with_str[];
177 namespace js {
180 * Atom tracing and garbage collection hooks.
182 void
183 MarkAtoms(JSTracer* trc);
185 void
186 MarkPermanentAtoms(JSTracer* trc);
188 void
189 MarkWellKnownSymbols(JSTracer* trc);
191 /* N.B. must correspond to boolean tagging behavior. */
192 enum InternBehavior
194 DoNotInternAtom = false,
195 InternAtom = true
198 extern JSAtom*
199 Atomize(ExclusiveContext* cx, const char* bytes, size_t length,
200 js::InternBehavior ib = js::DoNotInternAtom);
202 template <typename CharT>
203 extern JSAtom*
204 AtomizeChars(ExclusiveContext* cx, const CharT* chars, size_t length,
205 js::InternBehavior ib = js::DoNotInternAtom);
207 extern JSAtom*
208 AtomizeString(ExclusiveContext* cx, JSString* str, js::InternBehavior ib = js::DoNotInternAtom);
210 template <AllowGC allowGC>
211 extern JSAtom*
212 ToAtom(ExclusiveContext* cx, typename MaybeRooted<Value, allowGC>::HandleType v);
214 enum XDRMode {
215 XDR_ENCODE,
216 XDR_DECODE
219 template <XDRMode mode>
220 class XDRState;
222 template<XDRMode mode>
223 bool
224 XDRAtom(XDRState<mode>* xdr, js::MutableHandleAtom atomp);
226 } /* namespace js */
228 #endif /* jsatom_h */