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/. */
6 
7 #ifndef vm_JSAtom_h
8 #define vm_JSAtom_h
9 
10 #include "mozilla/Maybe.h"
11 
12 #include "gc/Rooting.h"
13 #include "js/TypeDecls.h"
14 #include "vm/CommonPropertyNames.h"
15 
16 class JSAutoByteString;
17 
18 namespace js {
19 
20 /*
21  * Return a printable, lossless char[] representation of a string-type atom.
22  * The lifetime of the result matches the lifetime of bytes.
23  */
24 extern const char* AtomToPrintableString(JSContext* cx, JSAtom* atom,
25                                          JSAutoByteString* bytes);
26 
27 class PropertyName;
28 
29 } /* namespace js */
30 
31 extern bool AtomIsPinned(JSContext* cx, JSAtom* atom);
32 
33 #ifdef DEBUG
34 
35 // This may be called either with or without the atoms lock held.
36 extern bool AtomIsPinnedInRuntime(JSRuntime* rt, JSAtom* atom);
37 
38 #endif  // DEBUG
39 
40 /* Well-known predefined C strings. */
41 #define DECLARE_PROTO_STR(name, init, clasp) \
42   extern const char js_##name##_str[];
43 JS_FOR_EACH_PROTOTYPE(DECLARE_PROTO_STR)
44 #undef DECLARE_PROTO_STR
45 
46 #define DECLARE_CONST_CHAR_STR(idpart, id, text) \
47   extern const char js_##idpart##_str[];
48 FOR_EACH_COMMON_PROPERTYNAME(DECLARE_CONST_CHAR_STR)
49 #undef DECLARE_CONST_CHAR_STR
50 
51 /* Constant strings that are not atomized. */
52 extern const char js_getter_str[];
53 extern const char js_send_str[];
54 extern const char js_setter_str[];
55 
56 namespace js {
57 
58 class AutoLockForExclusiveAccess;
59 
60 /*
61  * Atom tracing and garbage collection hooks.
62  */
63 void TraceAtoms(JSTracer* trc, AutoLockForExclusiveAccess& lock);
64 
65 void TracePermanentAtoms(JSTracer* trc);
66 
67 void TraceWellKnownSymbols(JSTracer* trc);
68 
69 /* N.B. must correspond to boolean tagging behavior. */
70 enum PinningBehavior { DoNotPinAtom = false, PinAtom = true };
71 
72 extern JSAtom* Atomize(
73     JSContext* cx, const char* bytes, size_t length,
74     js::PinningBehavior pin = js::DoNotPinAtom,
75     const mozilla::Maybe<uint32_t>& indexValue = mozilla::Nothing());
76 
77 template <typename CharT>
78 extern JSAtom* AtomizeChars(JSContext* cx, const CharT* chars, size_t length,
79                             js::PinningBehavior pin = js::DoNotPinAtom);
80 
81 extern JSAtom* AtomizeUTF8Chars(JSContext* cx, const char* utf8Chars,
82                                 size_t utf8ByteLength);
83 
84 extern JSAtom* AtomizeString(JSContext* cx, JSString* str,
85                              js::PinningBehavior pin = js::DoNotPinAtom);
86 
87 template <AllowGC allowGC>
88 extern JSAtom* ToAtom(JSContext* cx,
89                       typename MaybeRooted<JS::Value, allowGC>::HandleType v);
90 
91 enum XDRMode { XDR_ENCODE, XDR_DECODE };
92 
93 template <XDRMode mode>
94 class XDRState;
95 
96 template <XDRMode mode>
97 bool XDRAtom(XDRState<mode>* xdr, js::MutableHandleAtom atomp);
98 
99 extern JS::Handle<PropertyName*> ClassName(JSProtoKey key, JSContext* cx);
100 
101 namespace gc {
102 void MergeAtomsAddedWhileSweeping(JSRuntime* rt);
103 }  // namespace gc
104 
105 #ifdef DEBUG
106 
107 bool AtomIsMarked(JS::Zone* zone, JSAtom* atom);
108 bool AtomIsMarked(JS::Zone* zone, jsid id);
109 bool AtomIsMarked(JS::Zone* zone, const JS::Value& value);
110 
111 #endif  // DEBUG
112 
113 } /* namespace js */
114 
115 #endif /* vm_JSAtom_h */
116