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 vm_JSAtom_h
8 #define vm_JSAtom_h
9 
10 #include "mozilla/HashFunctions.h"
11 #include "mozilla/Maybe.h"
12 
13 #include "gc/MaybeRooted.h"
14 #include "gc/Rooting.h"
15 #include "js/TypeDecls.h"
16 #include "js/Utility.h"
17 #include "vm/CommonPropertyNames.h"
18 
19 namespace js {
20 
21 /*
22  * Return a printable, lossless char[] representation of a string-type atom.
23  * The returned string is guaranteed to contain only ASCII characters.
24  */
25 extern UniqueChars AtomToPrintableString(JSContext* cx, JSAtom* atom);
26 
27 class PropertyName;
28 
29 } /* namespace js */
30 
31 namespace js {
32 
33 class AutoAccessAtomsZone;
34 
35 /*
36  * Atom tracing and garbage collection hooks.
37  */
38 void TraceAtoms(JSTracer* trc, const AutoAccessAtomsZone& access);
39 
40 void TraceWellKnownSymbols(JSTracer* trc);
41 
42 /* N.B. must correspond to boolean tagging behavior. */
43 enum PinningBehavior { DoNotPinAtom = false, PinAtom = true };
44 
45 extern JSAtom* Atomize(
46     JSContext* cx, const char* bytes, size_t length,
47     js::PinningBehavior pin = js::DoNotPinAtom,
48     const mozilla::Maybe<uint32_t>& indexValue = mozilla::Nothing());
49 
50 extern JSAtom* Atomize(JSContext* cx, HashNumber hash, const char* bytes,
51                        size_t length, PinningBehavior pin);
52 
53 template <typename CharT>
54 extern JSAtom* AtomizeChars(JSContext* cx, const CharT* chars, size_t length,
55                             js::PinningBehavior pin = js::DoNotPinAtom);
56 
57 /* Atomize characters when the value of HashString is already known. */
58 template <typename CharT>
59 extern JSAtom* AtomizeChars(JSContext* cx, mozilla::HashNumber hash,
60                             const CharT* chars, size_t length);
61 
62 /**
63  * Create an atom whose contents are those of the |utf8ByteLength| code units
64  * starting at |utf8Chars|, interpreted as UTF-8.
65  *
66  * Throws if the code units do not contain valid UTF-8.
67  */
68 extern JSAtom* AtomizeUTF8Chars(JSContext* cx, const char* utf8Chars,
69                                 size_t utf8ByteLength);
70 
71 extern JSAtom* AtomizeString(JSContext* cx, JSString* str,
72                              js::PinningBehavior pin = js::DoNotPinAtom);
73 
74 template <AllowGC allowGC>
75 extern JSAtom* ToAtom(JSContext* cx,
76                       typename MaybeRooted<JS::Value, allowGC>::HandleType v);
77 
78 // These functions are declared in vm/Xdr.h
79 //
80 // template<XDRMode mode>
81 // XDRResult
82 // XDRAtom(XDRState<mode>* xdr, js::MutableHandleAtom atomp);
83 
84 // template<XDRMode mode>
85 // XDRResult
86 // XDRAtomOrNull(XDRState<mode>* xdr, js::MutableHandleAtom atomp);
87 
88 extern JS::Handle<PropertyName*> ClassName(JSProtoKey key, JSContext* cx);
89 
90 #ifdef DEBUG
91 
92 bool AtomIsMarked(JS::Zone* zone, JSAtom* atom);
93 bool AtomIsMarked(JS::Zone* zone, jsid id);
94 bool AtomIsMarked(JS::Zone* zone, const JS::Value& value);
95 
96 #endif  // DEBUG
97 
98 } /* namespace js */
99 
100 #endif /* vm_JSAtom_h */
101