1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/js/src/jsatom.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,215 @@ 1.4 +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- 1.5 + * vim: set ts=8 sts=4 et sw=4 tw=99: 1.6 + * This Source Code Form is subject to the terms of the Mozilla Public 1.7 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.8 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.9 + 1.10 +#ifndef jsatom_h 1.11 +#define jsatom_h 1.12 + 1.13 +#include "mozilla/HashFunctions.h" 1.14 + 1.15 +#include "jsalloc.h" 1.16 + 1.17 +#include "gc/Barrier.h" 1.18 +#include "gc/Rooting.h" 1.19 +#include "vm/CommonPropertyNames.h" 1.20 + 1.21 +class JSAtom; 1.22 +class JSAutoByteString; 1.23 + 1.24 +struct JSIdArray { 1.25 + int length; 1.26 + js::HeapId vector[1]; /* actually, length jsid words */ 1.27 +}; 1.28 + 1.29 +namespace js { 1.30 + 1.31 +JS_STATIC_ASSERT(sizeof(HashNumber) == 4); 1.32 + 1.33 +static MOZ_ALWAYS_INLINE js::HashNumber 1.34 +HashId(jsid id) 1.35 +{ 1.36 + return mozilla::HashGeneric(JSID_BITS(id)); 1.37 +} 1.38 + 1.39 +struct JsidHasher 1.40 +{ 1.41 + typedef jsid Lookup; 1.42 + static HashNumber hash(const Lookup &l) { 1.43 + return HashNumber(JSID_BITS(l)); 1.44 + } 1.45 + static bool match(const jsid &id, const Lookup &l) { 1.46 + return id == l; 1.47 + } 1.48 +}; 1.49 + 1.50 +/* 1.51 + * Return a printable, lossless char[] representation of a string-type atom. 1.52 + * The lifetime of the result matches the lifetime of bytes. 1.53 + */ 1.54 +extern const char * 1.55 +AtomToPrintableString(ExclusiveContext *cx, JSAtom *atom, JSAutoByteString *bytes); 1.56 + 1.57 +class AtomStateEntry 1.58 +{ 1.59 + uintptr_t bits; 1.60 + 1.61 + static const uintptr_t NO_TAG_MASK = uintptr_t(-1) - 1; 1.62 + 1.63 + public: 1.64 + AtomStateEntry() : bits(0) {} 1.65 + AtomStateEntry(const AtomStateEntry &other) : bits(other.bits) {} 1.66 + AtomStateEntry(JSAtom *ptr, bool tagged) 1.67 + : bits(uintptr_t(ptr) | uintptr_t(tagged)) 1.68 + { 1.69 + JS_ASSERT((uintptr_t(ptr) & 0x1) == 0); 1.70 + } 1.71 + 1.72 + bool isTagged() const { 1.73 + return bits & 0x1; 1.74 + } 1.75 + 1.76 + /* 1.77 + * Non-branching code sequence. Note that the const_cast is safe because 1.78 + * the hash function doesn't consider the tag to be a portion of the key. 1.79 + */ 1.80 + void setTagged(bool enabled) const { 1.81 + const_cast<AtomStateEntry *>(this)->bits |= uintptr_t(enabled); 1.82 + } 1.83 + 1.84 + JSAtom *asPtr() const; 1.85 +}; 1.86 + 1.87 +struct AtomHasher 1.88 +{ 1.89 + struct Lookup 1.90 + { 1.91 + const jschar *chars; 1.92 + size_t length; 1.93 + const JSAtom *atom; /* Optional. */ 1.94 + 1.95 + HashNumber hash; 1.96 + 1.97 + Lookup(const jschar *chars, size_t length) 1.98 + : chars(chars), length(length), atom(nullptr) 1.99 + { 1.100 + hash = mozilla::HashString(chars, length); 1.101 + } 1.102 + inline Lookup(const JSAtom *atom); 1.103 + }; 1.104 + 1.105 + static HashNumber hash(const Lookup &l) { return l.hash; } 1.106 + static inline bool match(const AtomStateEntry &entry, const Lookup &lookup); 1.107 + static void rekey(AtomStateEntry &k, const AtomStateEntry& newKey) { k = newKey; } 1.108 +}; 1.109 + 1.110 +typedef HashSet<AtomStateEntry, AtomHasher, SystemAllocPolicy> AtomSet; 1.111 + 1.112 +class PropertyName; 1.113 + 1.114 +} /* namespace js */ 1.115 + 1.116 +extern bool 1.117 +AtomIsInterned(JSContext *cx, JSAtom *atom); 1.118 + 1.119 +/* Well-known predefined C strings. */ 1.120 +#define DECLARE_PROTO_STR(name,code,init,clasp) extern const char js_##name##_str[]; 1.121 +JS_FOR_EACH_PROTOTYPE(DECLARE_PROTO_STR) 1.122 +#undef DECLARE_PROTO_STR 1.123 + 1.124 +#define DECLARE_CONST_CHAR_STR(idpart, id, text) extern const char js_##idpart##_str[]; 1.125 +FOR_EACH_COMMON_PROPERTYNAME(DECLARE_CONST_CHAR_STR) 1.126 +#undef DECLARE_CONST_CHAR_STR 1.127 + 1.128 +/* Constant strings that are not atomized. */ 1.129 +extern const char js_break_str[]; 1.130 +extern const char js_case_str[]; 1.131 +extern const char js_catch_str[]; 1.132 +extern const char js_class_str[]; 1.133 +extern const char js_close_str[]; 1.134 +extern const char js_const_str[]; 1.135 +extern const char js_continue_str[]; 1.136 +extern const char js_debugger_str[]; 1.137 +extern const char js_default_str[]; 1.138 +extern const char js_do_str[]; 1.139 +extern const char js_else_str[]; 1.140 +extern const char js_enum_str[]; 1.141 +extern const char js_export_str[]; 1.142 +extern const char js_extends_str[]; 1.143 +extern const char js_finally_str[]; 1.144 +extern const char js_for_str[]; 1.145 +extern const char js_getter_str[]; 1.146 +extern const char js_if_str[]; 1.147 +extern const char js_implements_str[]; 1.148 +extern const char js_import_str[]; 1.149 +extern const char js_in_str[]; 1.150 +extern const char js_instanceof_str[]; 1.151 +extern const char js_interface_str[]; 1.152 +extern const char js_new_str[]; 1.153 +extern const char js_package_str[]; 1.154 +extern const char js_private_str[]; 1.155 +extern const char js_protected_str[]; 1.156 +extern const char js_public_str[]; 1.157 +extern const char js_send_str[]; 1.158 +extern const char js_setter_str[]; 1.159 +extern const char js_static_str[]; 1.160 +extern const char js_super_str[]; 1.161 +extern const char js_switch_str[]; 1.162 +extern const char js_this_str[]; 1.163 +extern const char js_try_str[]; 1.164 +extern const char js_typeof_str[]; 1.165 +extern const char js_void_str[]; 1.166 +extern const char js_while_str[]; 1.167 +extern const char js_with_str[]; 1.168 + 1.169 +namespace js { 1.170 + 1.171 +extern const char * const TypeStrings[]; 1.172 + 1.173 +/* 1.174 + * Atom tracing and garbage collection hooks. 1.175 + */ 1.176 +extern void 1.177 +MarkAtoms(JSTracer *trc); 1.178 + 1.179 +extern void 1.180 +MarkPermanentAtoms(JSTracer *trc); 1.181 + 1.182 +/* N.B. must correspond to boolean tagging behavior. */ 1.183 +enum InternBehavior 1.184 +{ 1.185 + DoNotInternAtom = false, 1.186 + InternAtom = true 1.187 +}; 1.188 + 1.189 +extern JSAtom * 1.190 +Atomize(ExclusiveContext *cx, const char *bytes, size_t length, 1.191 + js::InternBehavior ib = js::DoNotInternAtom); 1.192 + 1.193 +extern JSAtom * 1.194 +AtomizeChars(ExclusiveContext *cx, const jschar *chars, size_t length, 1.195 + js::InternBehavior ib = js::DoNotInternAtom); 1.196 + 1.197 +extern JSAtom * 1.198 +AtomizeString(ExclusiveContext *cx, JSString *str, js::InternBehavior ib = js::DoNotInternAtom); 1.199 + 1.200 +template <AllowGC allowGC> 1.201 +extern JSAtom * 1.202 +ToAtom(ExclusiveContext *cx, typename MaybeRooted<Value, allowGC>::HandleType v); 1.203 + 1.204 +enum XDRMode { 1.205 + XDR_ENCODE, 1.206 + XDR_DECODE 1.207 +}; 1.208 + 1.209 +template <XDRMode mode> 1.210 +class XDRState; 1.211 + 1.212 +template<XDRMode mode> 1.213 +bool 1.214 +XDRAtom(XDRState<mode> *xdr, js::MutableHandleAtom atomp); 1.215 + 1.216 +} /* namespace js */ 1.217 + 1.218 +#endif /* jsatom_h */