summaryrefslogtreecommitdiffstats
path: root/js/src/jsatominlines.h
blob: ab91f974d1dfa7e4a5ca5153fc08f66fe8e4f986 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#ifndef jsatominlines_h
#define jsatominlines_h

#include "jsatom.h"

#include "mozilla/PodOperations.h"
#include "mozilla/RangedPtr.h"

#include "jscntxt.h"
#include "jsnum.h"

#include "vm/String.h"

inline JSAtom*
js::AtomStateEntry::asPtr(js::ExclusiveContext* cx) const
{
    JSAtom* atom = asPtrUnbarriered();
    if (cx->isJSContext())
        JSString::readBarrier(atom);
    return atom;
}

inline JSAtom*
js::AtomStateEntry::asPtrUnbarriered() const
{
    MOZ_ASSERT(bits != 0);
    return reinterpret_cast<JSAtom*>(bits & NO_TAG_MASK);
}

namespace js {

inline jsid
AtomToId(JSAtom* atom)
{
    JS_STATIC_ASSERT(JSID_INT_MIN == 0);

    uint32_t index;
    if (atom->isIndex(&index) && index <= JSID_INT_MAX)
        return INT_TO_JSID(int32_t(index));

    return JSID_FROM_BITS(size_t(atom));
}

inline bool
ValueToIdPure(const Value& v, jsid* id)
{
    int32_t i;
    if (ValueFitsInInt32(v, &i) && INT_FITS_IN_JSID(i)) {
        *id = INT_TO_JSID(i);
        return true;
    }

    if (js::IsSymbolOrSymbolWrapper(v)) {
        *id = SYMBOL_TO_JSID(js::ToSymbolPrimitive(v));
        return true;
    }

    if (!v.isString() || !v.toString()->isAtom())
        return false;

    *id = AtomToId(&v.toString()->asAtom());
    return true;
}

template <AllowGC allowGC>
inline bool
ValueToId(ExclusiveContext* cx, typename MaybeRooted<Value, allowGC>::HandleType v,
          typename MaybeRooted<jsid, allowGC>::MutableHandleType idp)
{
    int32_t i;
    if (ValueFitsInInt32(v, &i) && INT_FITS_IN_JSID(i)) {
        idp.set(INT_TO_JSID(i));
        return true;
    }

    if (js::IsSymbolOrSymbolWrapper(v)) {
        idp.set(SYMBOL_TO_JSID(js::ToSymbolPrimitive(v)));
        return true;
    }

    JSAtom* atom = ToAtom<allowGC>(cx, v);
    if (!atom)
        return false;

    idp.set(AtomToId(atom));
    return true;
}

/*
 * Write out character representing |index| to the memory just before |end|.
 * Thus |*end| is not touched, but |end[-1]| and earlier are modified as
 * appropriate.  There must be at least js::UINT32_CHAR_BUFFER_LENGTH elements
 * before |end| to avoid buffer underflow.  The start of the characters written
 * is returned and is necessarily before |end|.
 */
template <typename T>
inline mozilla::RangedPtr<T>
BackfillIndexInCharBuffer(uint32_t index, mozilla::RangedPtr<T> end)
{
#ifdef DEBUG
    /*
     * Assert that the buffer we're filling will hold as many characters as we
     * could write out, by dereferencing the index that would hold the most
     * significant digit.
     */
    (void) *(end - UINT32_CHAR_BUFFER_LENGTH);
#endif

    do {
        uint32_t next = index / 10, digit = index % 10;
        *--end = '0' + digit;
        index = next;
    } while (index > 0);

    return end;
}

bool
IndexToIdSlow(ExclusiveContext* cx, uint32_t index, MutableHandleId idp);

inline bool
IndexToId(ExclusiveContext* cx, uint32_t index, MutableHandleId idp)
{
    if (index <= JSID_INT_MAX) {
        idp.set(INT_TO_JSID(index));
        return true;
    }

    return IndexToIdSlow(cx, index, idp);
}

static MOZ_ALWAYS_INLINE JSFlatString*
IdToString(JSContext* cx, jsid id)
{
    if (JSID_IS_STRING(id))
        return JSID_TO_ATOM(id);

    if (MOZ_LIKELY(JSID_IS_INT(id)))
        return Int32ToString<CanGC>(cx, JSID_TO_INT(id));

    RootedValue idv(cx, IdToValue(id));
    JSString* str = ToStringSlow<CanGC>(cx, idv);
    if (!str)
        return nullptr;

    return str->ensureFlat(cx);
}

inline
AtomHasher::Lookup::Lookup(const JSAtom* atom)
  : isLatin1(atom->hasLatin1Chars()), length(atom->length()), atom(atom)
{
    hash = atom->hash();
    if (isLatin1) {
        latin1Chars = atom->latin1Chars(nogc);
        MOZ_ASSERT(mozilla::HashString(latin1Chars, length) == hash);
    } else {
        twoByteChars = atom->twoByteChars(nogc);
        MOZ_ASSERT(mozilla::HashString(twoByteChars, length) == hash);
    }
}

inline bool
AtomHasher::match(const AtomStateEntry& entry, const Lookup& lookup)
{
    JSAtom* key = entry.asPtrUnbarriered();
    if (lookup.atom)
        return lookup.atom == key;
    if (key->length() != lookup.length || key->hash() != lookup.hash)
        return false;

    if (key->hasLatin1Chars()) {
        const Latin1Char* keyChars = key->latin1Chars(lookup.nogc);
        if (lookup.isLatin1)
            return mozilla::PodEqual(keyChars, lookup.latin1Chars, lookup.length);
        return EqualChars(keyChars, lookup.twoByteChars, lookup.length);
    }

    const char16_t* keyChars = key->twoByteChars(lookup.nogc);
    if (lookup.isLatin1)
        return EqualChars(lookup.latin1Chars, keyChars, lookup.length);
    return mozilla::PodEqual(keyChars, lookup.twoByteChars, lookup.length);
}

inline Handle<PropertyName*>
TypeName(JSType type, const JSAtomState& names)
{
    MOZ_ASSERT(type < JSTYPE_LIMIT);
    JS_STATIC_ASSERT(offsetof(JSAtomState, undefined) +
                     JSTYPE_LIMIT * sizeof(ImmutablePropertyNamePtr) <=
                     sizeof(JSAtomState));
    JS_STATIC_ASSERT(JSTYPE_VOID == 0);
    return (&names.undefined)[type];
}

inline Handle<PropertyName*>
ClassName(JSProtoKey key, JSAtomState& atomState)
{
    MOZ_ASSERT(key < JSProto_LIMIT);
    JS_STATIC_ASSERT(offsetof(JSAtomState, Null) +
                     JSProto_LIMIT * sizeof(ImmutablePropertyNamePtr) <=
                     sizeof(JSAtomState));
    JS_STATIC_ASSERT(JSProto_Null == 0);
    return (&atomState.Null)[key];
}

inline Handle<PropertyName*>
ClassName(JSProtoKey key, ExclusiveContext* cx)
{
    return ClassName(key, cx->names());
}

} // namespace js

#endif /* jsatominlines_h */