js/src/jsatominlines.h

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

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 jsatominlines_h
michael@0 8 #define jsatominlines_h
michael@0 9
michael@0 10 #include "jsatom.h"
michael@0 11
michael@0 12 #include "mozilla/PodOperations.h"
michael@0 13 #include "mozilla/RangedPtr.h"
michael@0 14
michael@0 15 #include "jscntxt.h"
michael@0 16 #include "jsnum.h"
michael@0 17
michael@0 18 #include "vm/String.h"
michael@0 19
michael@0 20 inline JSAtom *
michael@0 21 js::AtomStateEntry::asPtr() const
michael@0 22 {
michael@0 23 JS_ASSERT(bits != 0);
michael@0 24 JSAtom *atom = reinterpret_cast<JSAtom *>(bits & NO_TAG_MASK);
michael@0 25 JSString::readBarrier(atom);
michael@0 26 return atom;
michael@0 27 }
michael@0 28
michael@0 29 namespace js {
michael@0 30
michael@0 31 inline jsid
michael@0 32 AtomToId(JSAtom *atom)
michael@0 33 {
michael@0 34 JS_STATIC_ASSERT(JSID_INT_MIN == 0);
michael@0 35
michael@0 36 uint32_t index;
michael@0 37 if (atom->isIndex(&index) && index <= JSID_INT_MAX)
michael@0 38 return INT_TO_JSID(int32_t(index));
michael@0 39
michael@0 40 return JSID_FROM_BITS(size_t(atom));
michael@0 41 }
michael@0 42
michael@0 43 inline bool
michael@0 44 ValueToIdPure(const Value &v, jsid *id)
michael@0 45 {
michael@0 46 int32_t i;
michael@0 47 if (ValueFitsInInt32(v, &i) && INT_FITS_IN_JSID(i)) {
michael@0 48 *id = INT_TO_JSID(i);
michael@0 49 return true;
michael@0 50 }
michael@0 51
michael@0 52 if (!v.isString() || !v.toString()->isAtom())
michael@0 53 return false;
michael@0 54
michael@0 55 *id = AtomToId(&v.toString()->asAtom());
michael@0 56 return true;
michael@0 57 }
michael@0 58
michael@0 59 template <AllowGC allowGC>
michael@0 60 inline bool
michael@0 61 ValueToId(JSContext* cx, typename MaybeRooted<Value, allowGC>::HandleType v,
michael@0 62 typename MaybeRooted<jsid, allowGC>::MutableHandleType idp)
michael@0 63 {
michael@0 64 int32_t i;
michael@0 65 if (ValueFitsInInt32(v, &i) && INT_FITS_IN_JSID(i)) {
michael@0 66 idp.set(INT_TO_JSID(i));
michael@0 67 return true;
michael@0 68 }
michael@0 69
michael@0 70 JSAtom *atom = ToAtom<allowGC>(cx, v);
michael@0 71 if (!atom)
michael@0 72 return false;
michael@0 73
michael@0 74 idp.set(AtomToId(atom));
michael@0 75 return true;
michael@0 76 }
michael@0 77
michael@0 78 /*
michael@0 79 * Write out character representing |index| to the memory just before |end|.
michael@0 80 * Thus |*end| is not touched, but |end[-1]| and earlier are modified as
michael@0 81 * appropriate. There must be at least js::UINT32_CHAR_BUFFER_LENGTH elements
michael@0 82 * before |end| to avoid buffer underflow. The start of the characters written
michael@0 83 * is returned and is necessarily before |end|.
michael@0 84 */
michael@0 85 template <typename T>
michael@0 86 inline mozilla::RangedPtr<T>
michael@0 87 BackfillIndexInCharBuffer(uint32_t index, mozilla::RangedPtr<T> end)
michael@0 88 {
michael@0 89 #ifdef DEBUG
michael@0 90 /*
michael@0 91 * Assert that the buffer we're filling will hold as many characters as we
michael@0 92 * could write out, by dereferencing the index that would hold the most
michael@0 93 * significant digit.
michael@0 94 */
michael@0 95 (void) *(end - UINT32_CHAR_BUFFER_LENGTH);
michael@0 96 #endif
michael@0 97
michael@0 98 do {
michael@0 99 uint32_t next = index / 10, digit = index % 10;
michael@0 100 *--end = '0' + digit;
michael@0 101 index = next;
michael@0 102 } while (index > 0);
michael@0 103
michael@0 104 return end;
michael@0 105 }
michael@0 106
michael@0 107 bool
michael@0 108 IndexToIdSlow(ExclusiveContext *cx, uint32_t index, MutableHandleId idp);
michael@0 109
michael@0 110 inline bool
michael@0 111 IndexToId(ExclusiveContext *cx, uint32_t index, MutableHandleId idp)
michael@0 112 {
michael@0 113 if (index <= JSID_INT_MAX) {
michael@0 114 idp.set(INT_TO_JSID(index));
michael@0 115 return true;
michael@0 116 }
michael@0 117
michael@0 118 return IndexToIdSlow(cx, index, idp);
michael@0 119 }
michael@0 120
michael@0 121 static MOZ_ALWAYS_INLINE JSFlatString *
michael@0 122 IdToString(JSContext *cx, jsid id)
michael@0 123 {
michael@0 124 if (JSID_IS_STRING(id))
michael@0 125 return JSID_TO_ATOM(id);
michael@0 126
michael@0 127 if (MOZ_LIKELY(JSID_IS_INT(id)))
michael@0 128 return Int32ToString<CanGC>(cx, JSID_TO_INT(id));
michael@0 129
michael@0 130 RootedValue idv(cx, IdToValue(id));
michael@0 131 JSString *str = ToStringSlow<CanGC>(cx, idv);
michael@0 132 if (!str)
michael@0 133 return nullptr;
michael@0 134
michael@0 135 return str->ensureFlat(cx);
michael@0 136 }
michael@0 137
michael@0 138 inline
michael@0 139 AtomHasher::Lookup::Lookup(const JSAtom *atom)
michael@0 140 : chars(atom->chars()), length(atom->length()), atom(atom)
michael@0 141 {
michael@0 142 hash = mozilla::HashString(chars, length);
michael@0 143 }
michael@0 144
michael@0 145 inline bool
michael@0 146 AtomHasher::match(const AtomStateEntry &entry, const Lookup &lookup)
michael@0 147 {
michael@0 148 JSAtom *key = entry.asPtr();
michael@0 149 if (lookup.atom)
michael@0 150 return lookup.atom == key;
michael@0 151 if (key->length() != lookup.length)
michael@0 152 return false;
michael@0 153 return mozilla::PodEqual(key->chars(), lookup.chars, lookup.length);
michael@0 154 }
michael@0 155
michael@0 156 inline Handle<PropertyName*>
michael@0 157 TypeName(JSType type, const JSAtomState &names)
michael@0 158 {
michael@0 159 JS_ASSERT(type < JSTYPE_LIMIT);
michael@0 160 JS_STATIC_ASSERT(offsetof(JSAtomState, undefined) +
michael@0 161 JSTYPE_LIMIT * sizeof(FixedHeapPtr<PropertyName>) <=
michael@0 162 sizeof(JSAtomState));
michael@0 163 JS_STATIC_ASSERT(JSTYPE_VOID == 0);
michael@0 164 return (&names.undefined)[type];
michael@0 165 }
michael@0 166
michael@0 167 inline Handle<PropertyName*>
michael@0 168 ClassName(JSProtoKey key, JSAtomState &atomState)
michael@0 169 {
michael@0 170 JS_ASSERT(key < JSProto_LIMIT);
michael@0 171 JS_STATIC_ASSERT(offsetof(JSAtomState, Null) +
michael@0 172 JSProto_LIMIT * sizeof(FixedHeapPtr<PropertyName>) <=
michael@0 173 sizeof(JSAtomState));
michael@0 174 JS_STATIC_ASSERT(JSProto_Null == 0);
michael@0 175 return (&atomState.Null)[key];
michael@0 176 }
michael@0 177
michael@0 178 inline Handle<PropertyName*>
michael@0 179 ClassName(JSProtoKey key, JSRuntime *rt)
michael@0 180 {
michael@0 181 return ClassName(key, *rt->commonNames);
michael@0 182 }
michael@0 183
michael@0 184 inline Handle<PropertyName*>
michael@0 185 ClassName(JSProtoKey key, ExclusiveContext *cx)
michael@0 186 {
michael@0 187 return ClassName(key, cx->names());
michael@0 188 }
michael@0 189
michael@0 190 } // namespace js
michael@0 191
michael@0 192 #endif /* jsatominlines_h */

mercurial