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