Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
michael@0 | 1 | /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- |
michael@0 | 2 | * vim: set ts=8 sts=4 et sw=4 tw=99: |
michael@0 | 3 | * This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 4 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 5 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 6 | |
michael@0 | 7 | #ifndef jsatom_h |
michael@0 | 8 | #define jsatom_h |
michael@0 | 9 | |
michael@0 | 10 | #include "mozilla/HashFunctions.h" |
michael@0 | 11 | |
michael@0 | 12 | #include "jsalloc.h" |
michael@0 | 13 | |
michael@0 | 14 | #include "gc/Barrier.h" |
michael@0 | 15 | #include "gc/Rooting.h" |
michael@0 | 16 | #include "vm/CommonPropertyNames.h" |
michael@0 | 17 | |
michael@0 | 18 | class JSAtom; |
michael@0 | 19 | class JSAutoByteString; |
michael@0 | 20 | |
michael@0 | 21 | struct JSIdArray { |
michael@0 | 22 | int length; |
michael@0 | 23 | js::HeapId vector[1]; /* actually, length jsid words */ |
michael@0 | 24 | }; |
michael@0 | 25 | |
michael@0 | 26 | namespace js { |
michael@0 | 27 | |
michael@0 | 28 | JS_STATIC_ASSERT(sizeof(HashNumber) == 4); |
michael@0 | 29 | |
michael@0 | 30 | static MOZ_ALWAYS_INLINE js::HashNumber |
michael@0 | 31 | HashId(jsid id) |
michael@0 | 32 | { |
michael@0 | 33 | return mozilla::HashGeneric(JSID_BITS(id)); |
michael@0 | 34 | } |
michael@0 | 35 | |
michael@0 | 36 | struct JsidHasher |
michael@0 | 37 | { |
michael@0 | 38 | typedef jsid Lookup; |
michael@0 | 39 | static HashNumber hash(const Lookup &l) { |
michael@0 | 40 | return HashNumber(JSID_BITS(l)); |
michael@0 | 41 | } |
michael@0 | 42 | static bool match(const jsid &id, const Lookup &l) { |
michael@0 | 43 | return id == l; |
michael@0 | 44 | } |
michael@0 | 45 | }; |
michael@0 | 46 | |
michael@0 | 47 | /* |
michael@0 | 48 | * Return a printable, lossless char[] representation of a string-type atom. |
michael@0 | 49 | * The lifetime of the result matches the lifetime of bytes. |
michael@0 | 50 | */ |
michael@0 | 51 | extern const char * |
michael@0 | 52 | AtomToPrintableString(ExclusiveContext *cx, JSAtom *atom, JSAutoByteString *bytes); |
michael@0 | 53 | |
michael@0 | 54 | class AtomStateEntry |
michael@0 | 55 | { |
michael@0 | 56 | uintptr_t bits; |
michael@0 | 57 | |
michael@0 | 58 | static const uintptr_t NO_TAG_MASK = uintptr_t(-1) - 1; |
michael@0 | 59 | |
michael@0 | 60 | public: |
michael@0 | 61 | AtomStateEntry() : bits(0) {} |
michael@0 | 62 | AtomStateEntry(const AtomStateEntry &other) : bits(other.bits) {} |
michael@0 | 63 | AtomStateEntry(JSAtom *ptr, bool tagged) |
michael@0 | 64 | : bits(uintptr_t(ptr) | uintptr_t(tagged)) |
michael@0 | 65 | { |
michael@0 | 66 | JS_ASSERT((uintptr_t(ptr) & 0x1) == 0); |
michael@0 | 67 | } |
michael@0 | 68 | |
michael@0 | 69 | bool isTagged() const { |
michael@0 | 70 | return bits & 0x1; |
michael@0 | 71 | } |
michael@0 | 72 | |
michael@0 | 73 | /* |
michael@0 | 74 | * Non-branching code sequence. Note that the const_cast is safe because |
michael@0 | 75 | * the hash function doesn't consider the tag to be a portion of the key. |
michael@0 | 76 | */ |
michael@0 | 77 | void setTagged(bool enabled) const { |
michael@0 | 78 | const_cast<AtomStateEntry *>(this)->bits |= uintptr_t(enabled); |
michael@0 | 79 | } |
michael@0 | 80 | |
michael@0 | 81 | JSAtom *asPtr() const; |
michael@0 | 82 | }; |
michael@0 | 83 | |
michael@0 | 84 | struct AtomHasher |
michael@0 | 85 | { |
michael@0 | 86 | struct Lookup |
michael@0 | 87 | { |
michael@0 | 88 | const jschar *chars; |
michael@0 | 89 | size_t length; |
michael@0 | 90 | const JSAtom *atom; /* Optional. */ |
michael@0 | 91 | |
michael@0 | 92 | HashNumber hash; |
michael@0 | 93 | |
michael@0 | 94 | Lookup(const jschar *chars, size_t length) |
michael@0 | 95 | : chars(chars), length(length), atom(nullptr) |
michael@0 | 96 | { |
michael@0 | 97 | hash = mozilla::HashString(chars, length); |
michael@0 | 98 | } |
michael@0 | 99 | inline Lookup(const JSAtom *atom); |
michael@0 | 100 | }; |
michael@0 | 101 | |
michael@0 | 102 | static HashNumber hash(const Lookup &l) { return l.hash; } |
michael@0 | 103 | static inline bool match(const AtomStateEntry &entry, const Lookup &lookup); |
michael@0 | 104 | static void rekey(AtomStateEntry &k, const AtomStateEntry& newKey) { k = newKey; } |
michael@0 | 105 | }; |
michael@0 | 106 | |
michael@0 | 107 | typedef HashSet<AtomStateEntry, AtomHasher, SystemAllocPolicy> AtomSet; |
michael@0 | 108 | |
michael@0 | 109 | class PropertyName; |
michael@0 | 110 | |
michael@0 | 111 | } /* namespace js */ |
michael@0 | 112 | |
michael@0 | 113 | extern bool |
michael@0 | 114 | AtomIsInterned(JSContext *cx, JSAtom *atom); |
michael@0 | 115 | |
michael@0 | 116 | /* Well-known predefined C strings. */ |
michael@0 | 117 | #define DECLARE_PROTO_STR(name,code,init,clasp) extern const char js_##name##_str[]; |
michael@0 | 118 | JS_FOR_EACH_PROTOTYPE(DECLARE_PROTO_STR) |
michael@0 | 119 | #undef DECLARE_PROTO_STR |
michael@0 | 120 | |
michael@0 | 121 | #define DECLARE_CONST_CHAR_STR(idpart, id, text) extern const char js_##idpart##_str[]; |
michael@0 | 122 | FOR_EACH_COMMON_PROPERTYNAME(DECLARE_CONST_CHAR_STR) |
michael@0 | 123 | #undef DECLARE_CONST_CHAR_STR |
michael@0 | 124 | |
michael@0 | 125 | /* Constant strings that are not atomized. */ |
michael@0 | 126 | extern const char js_break_str[]; |
michael@0 | 127 | extern const char js_case_str[]; |
michael@0 | 128 | extern const char js_catch_str[]; |
michael@0 | 129 | extern const char js_class_str[]; |
michael@0 | 130 | extern const char js_close_str[]; |
michael@0 | 131 | extern const char js_const_str[]; |
michael@0 | 132 | extern const char js_continue_str[]; |
michael@0 | 133 | extern const char js_debugger_str[]; |
michael@0 | 134 | extern const char js_default_str[]; |
michael@0 | 135 | extern const char js_do_str[]; |
michael@0 | 136 | extern const char js_else_str[]; |
michael@0 | 137 | extern const char js_enum_str[]; |
michael@0 | 138 | extern const char js_export_str[]; |
michael@0 | 139 | extern const char js_extends_str[]; |
michael@0 | 140 | extern const char js_finally_str[]; |
michael@0 | 141 | extern const char js_for_str[]; |
michael@0 | 142 | extern const char js_getter_str[]; |
michael@0 | 143 | extern const char js_if_str[]; |
michael@0 | 144 | extern const char js_implements_str[]; |
michael@0 | 145 | extern const char js_import_str[]; |
michael@0 | 146 | extern const char js_in_str[]; |
michael@0 | 147 | extern const char js_instanceof_str[]; |
michael@0 | 148 | extern const char js_interface_str[]; |
michael@0 | 149 | extern const char js_new_str[]; |
michael@0 | 150 | extern const char js_package_str[]; |
michael@0 | 151 | extern const char js_private_str[]; |
michael@0 | 152 | extern const char js_protected_str[]; |
michael@0 | 153 | extern const char js_public_str[]; |
michael@0 | 154 | extern const char js_send_str[]; |
michael@0 | 155 | extern const char js_setter_str[]; |
michael@0 | 156 | extern const char js_static_str[]; |
michael@0 | 157 | extern const char js_super_str[]; |
michael@0 | 158 | extern const char js_switch_str[]; |
michael@0 | 159 | extern const char js_this_str[]; |
michael@0 | 160 | extern const char js_try_str[]; |
michael@0 | 161 | extern const char js_typeof_str[]; |
michael@0 | 162 | extern const char js_void_str[]; |
michael@0 | 163 | extern const char js_while_str[]; |
michael@0 | 164 | extern const char js_with_str[]; |
michael@0 | 165 | |
michael@0 | 166 | namespace js { |
michael@0 | 167 | |
michael@0 | 168 | extern const char * const TypeStrings[]; |
michael@0 | 169 | |
michael@0 | 170 | /* |
michael@0 | 171 | * Atom tracing and garbage collection hooks. |
michael@0 | 172 | */ |
michael@0 | 173 | extern void |
michael@0 | 174 | MarkAtoms(JSTracer *trc); |
michael@0 | 175 | |
michael@0 | 176 | extern void |
michael@0 | 177 | MarkPermanentAtoms(JSTracer *trc); |
michael@0 | 178 | |
michael@0 | 179 | /* N.B. must correspond to boolean tagging behavior. */ |
michael@0 | 180 | enum InternBehavior |
michael@0 | 181 | { |
michael@0 | 182 | DoNotInternAtom = false, |
michael@0 | 183 | InternAtom = true |
michael@0 | 184 | }; |
michael@0 | 185 | |
michael@0 | 186 | extern JSAtom * |
michael@0 | 187 | Atomize(ExclusiveContext *cx, const char *bytes, size_t length, |
michael@0 | 188 | js::InternBehavior ib = js::DoNotInternAtom); |
michael@0 | 189 | |
michael@0 | 190 | extern JSAtom * |
michael@0 | 191 | AtomizeChars(ExclusiveContext *cx, const jschar *chars, size_t length, |
michael@0 | 192 | js::InternBehavior ib = js::DoNotInternAtom); |
michael@0 | 193 | |
michael@0 | 194 | extern JSAtom * |
michael@0 | 195 | AtomizeString(ExclusiveContext *cx, JSString *str, js::InternBehavior ib = js::DoNotInternAtom); |
michael@0 | 196 | |
michael@0 | 197 | template <AllowGC allowGC> |
michael@0 | 198 | extern JSAtom * |
michael@0 | 199 | ToAtom(ExclusiveContext *cx, typename MaybeRooted<Value, allowGC>::HandleType v); |
michael@0 | 200 | |
michael@0 | 201 | enum XDRMode { |
michael@0 | 202 | XDR_ENCODE, |
michael@0 | 203 | XDR_DECODE |
michael@0 | 204 | }; |
michael@0 | 205 | |
michael@0 | 206 | template <XDRMode mode> |
michael@0 | 207 | class XDRState; |
michael@0 | 208 | |
michael@0 | 209 | template<XDRMode mode> |
michael@0 | 210 | bool |
michael@0 | 211 | XDRAtom(XDRState<mode> *xdr, js::MutableHandleAtom atomp); |
michael@0 | 212 | |
michael@0 | 213 | } /* namespace js */ |
michael@0 | 214 | |
michael@0 | 215 | #endif /* jsatom_h */ |