Debian Codegraph @

js/src/vm/JSAtomUtils.h

raw
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_JSAtomUtils_h
8 #define vm_JSAtomUtils_h
9
10 #include "mozilla/HashFunctions.h"
11 #include "mozilla/Maybe.h"
12
13 #include "NamespaceImports.h"
14
15 #include "gc/MaybeRooted.h"
16 #include "js/TypeDecls.h"
17 #include "js/Utility.h"
18 #include "vm/StringType.h"
19
20 namespace js {
21
22 class AtomSet;
23
24 /*
25 * Return a printable, lossless char[] representation of a string-type atom.
26 * The returned string is guaranteed to contain only ASCII characters.
27 */
28 extern UniqueChars AtomToPrintableString(JSContext* cx, JSAtom* atom);
29
30 class PropertyName;
31
32 } /* namespace js */
33
34 namespace js {
35
36 /*
37 * Atom tracing and garbage collection hooks.
38 */
39 void TraceAtoms(JSTracer* trc);
40
41 extern JSAtom* Atomize(
42 JSContext* cx, const char* bytes, size_t length,
43 const mozilla::Maybe<uint32_t>& indexValue = mozilla::Nothing());
44
45 extern JSAtom* AtomizeWithoutActiveZone(JSContext* cx, const char* bytes,
46 size_t length);
47
48 template <typename CharT>
49 extern JSAtom* AtomizeChars(JSContext* cx, const CharT* chars, size_t length);
50
51 /*
52 * Optimized entry points for atomization.
53 *
54 * The meaning of suffix:
55 * * "NonStatic": characters don't match StaticStrings
56 * * "ValidLength": length fits JSString::MAX_LENGTH
57 */
58
59 /* Atomize characters when the value of HashString is already known. */
60 template <typename CharT>
61 extern JSAtom* AtomizeCharsNonStaticValidLength(JSContext* cx,
62 mozilla::HashNumber hash,
63 const CharT* chars,
64 size_t length);
65
66 /**
67 * Permanently atomize characters.
68 *
69 * `chars` shouldn't match any of StaticStrings entry.
70 * `length` should be validated by JSString::validateLength.
71 */
72 extern JSAtom* PermanentlyAtomizeCharsNonStaticValidLength(
73 JSContext* cx, AtomSet& atomSet, mozilla::HashNumber hash,
74 const Latin1Char* chars, size_t length);
75
76 /**
77 * Create an atom whose contents are those of the |utf8ByteLength| code units
78 * starting at |utf8Chars|, interpreted as UTF-8.
79 *
80 * Throws if the code units do not contain valid UTF-8.
81 */
82 extern JSAtom* AtomizeUTF8Chars(JSContext* cx, const char* utf8Chars,
83 size_t utf8ByteLength);
84
85 extern JSAtom* AtomizeStringSlow(JSContext* cx, JSString* str);
86
87 MOZ_ALWAYS_INLINE JSAtom* AtomizeString(JSContext* cx, JSString* str) {
88 // Inlined fast path for atoms. This covers ~71% of calls to this function on
89 // Speedometer 3.
90 if (str->isAtom()) {
91 return &str->asAtom();
92 }
93 return AtomizeStringSlow(cx, str);
94 }
95
96 template <AllowGC allowGC>
97 extern JSAtom* ToAtom(JSContext* cx,
98 typename MaybeRooted<JS::Value, allowGC>::HandleType v);
99
100 /*
101 * Pin an atom so that it is never collected. Avoid using this if possible.
102 *
103 * This function does not GC.
104 */
105 extern bool PinAtom(JSContext* cx, JSAtom* atom);
106
107 extern JS::Handle<PropertyName*> ClassName(JSProtoKey key, JSContext* cx);
108
109 #ifdef DEBUG
110
111 bool AtomIsMarked(JS::Zone* zone, JSAtom* atom);
112 bool AtomIsMarked(JS::Zone* zone, jsid id);
113 bool AtomIsMarked(JS::Zone* zone, const JS::Value& value);
114
115 #endif // DEBUG
116
117 } /* namespace js */
118
119 #endif /* vm_JSAtomUtils_h */