File: JSImmediate.h

package info (click to toggle)
kde4libs 4%3A4.14.2-5%2Bdeb8u2
  • links: PTS, VCS
  • area: main
  • in suites: jessie
  • size: 82,428 kB
  • ctags: 99,415
  • sloc: cpp: 761,864; xml: 12,344; ansic: 6,295; java: 4,060; perl: 2,938; yacc: 2,507; python: 1,207; sh: 1,179; ruby: 337; lex: 278; makefile: 29
file content (311 lines) | stat: -rw-r--r-- 9,253 bytes parent folder | download | duplicates (3)
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
/*
 *  This file is part of the KDE libraries
 *  Copyright (C) 2003, 2004, 2005, 2006, 2007 Apple Inc. All rights reserved.
 *  Copyright (C) 2006 Alexey Proskuryakov (ap@webkit.org)
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Library General Public
 *  License as published by the Free Software Foundation; either
 *  version 2 of the License, or (at your option) any later version.
 *
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Library General Public License for more details.
 *
 *  You should have received a copy of the GNU Library General Public License
 *  along with this library; see the file COPYING.LIB.  If not, write to
 *  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 *  Boston, MA 02110-1301, USA.
 *
 */

#ifndef KJS_JS_IMMEDIATE_H
#define KJS_JS_IMMEDIATE_H

#include <kjs/global.h>
#include "JSType.h"
#include <wtf/Assertions.h>
#include <wtf/AlwaysInline.h>
#include <wtf/MathExtras.h>
#ifdef HAVE_STDINT_H
#include <stdint.h>
#endif
#include <stdlib.h>

#if PLATFORM(SOLARIS_OS)
static inline int signbit(double x)
{
    return (x<0.0) ? 1 : 0;
}
#endif

namespace KJS {

class ExecState;
class JSObject;
class JSValue;
class UString;

KJS_EXPORT extern const double NaN;
KJS_EXPORT extern const double Inf;

/*
 * A JSValue*  is either a pointer to a cell (a heap-allocated object) or an immediate (a type-tagged 
 * signed int masquerading as a pointer). The low two bits in a JSValue* are available 
 * for type tagging because allocator alignment guarantees they will be 00 in cell pointers.
 *
 * For example, on a 32 bit system:
 *
 * JSCell*:       XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX                 00
 *               [ high 30 bits: pointer address ]  [ low 2 bits -- always 0 ]
 *
 * JSImmediate:   XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX                 TT
 *               [ high 30 bits: signed int ]       [ low 2 bits -- type tag ]
 *
 * The bit "payload" (the high 30 bits) is a 30 bit signed int for immediate numbers, a flag to distinguish true/false
 * and undefined/null.
 *
 * Notice that the JSType value of NullType is 4, which requires 3 bits to encode. Since we only have 2 bits 
 * available for type tagging, we tag the null immediate with UndefinedType, and JSImmediate::type() has 
 * to sort them out.
 */

class KJS_EXPORT JSImmediate {
public:
    static ALWAYS_INLINE bool isImmediate(const JSValue* v)
    {
        return getTag(v) != 0;
    }
    
    static ALWAYS_INLINE bool isNumber(const JSValue* v)
    {
        return (getTag(v) == NumberType);
    }
    
    static ALWAYS_INLINE bool isBoolean(const JSValue* v)
    {
        return (getTag(v) == BooleanType);
    }
    
    // Since we have room for only 3 unique tags, null and undefined have to share.
    static ALWAYS_INLINE bool isUndefinedOrNull(const JSValue* v)
    {
        return (getTag(v) == UndefinedType);
    }

    static JSValue* from(char);
    static JSValue* from(signed char);
    static JSValue* from(unsigned char);
    static JSValue* from(short);
    static JSValue* from(unsigned short);
    static JSValue* from(int);
    static JSValue* from(unsigned);
    static JSValue* from(long);
    static JSValue* from(unsigned long);
    static JSValue* from(long long);
    static JSValue* from(unsigned long long);
    static JSValue* from(double);

    static ALWAYS_INLINE bool areBothImmediateNumbers(const JSValue* v1, const JSValue* v2)
    {
        return (reinterpret_cast<uintptr_t>(v1) & reinterpret_cast<uintptr_t>(v2) & TagMask) == NumberType;
    }

    static ALWAYS_INLINE JSValue* andImmediateNumbers(const JSValue* v1, const JSValue* v2)
    {
        ASSERT(areBothImmediateNumbers(v1, v2));
        return reinterpret_cast<JSValue*>(reinterpret_cast<uintptr_t>(v1) & reinterpret_cast<uintptr_t>(v2));
    }

    static double toDouble(const JSValue*);

    // Non-converting getters for Number's
    static double getNumber(const JSValue*);     // This returns NaN if the value is not a Number.
    static bool   getNumber(const JSValue*, double& valOut);
    
    static bool toBoolean(const JSValue*);
    static JSObject* toObject(const JSValue*, ExecState*);
    static UString toString(const JSValue*);
    static JSType type(const JSValue*);

    static bool getUInt32(const JSValue*, uint32_t&);
    static bool getTruncatedInt32(const JSValue*, int32_t&);
    static bool getTruncatedUInt32(const JSValue*, uint32_t&);

    static int32_t getTruncatedInt32(const JSValue*);

    static JSValue* trueImmediate();
    static JSValue* falseImmediate();
    static JSValue* undefinedImmediate();
    static JSValue* nullImmediate();
    
private:
    static const uintptr_t TagMask = 3; // type tags are 2 bits long

    // Immediate values are restricted to a 30 bit signed value.
    static const int minImmediateInt = -(1 << 29);
    static const int maxImmediateInt = (1 << 29) - 1;
    static const unsigned maxImmediateUInt = maxImmediateInt;
    
    static ALWAYS_INLINE JSValue* tag(uintptr_t bits, uintptr_t tag)
    {
        return reinterpret_cast<JSValue*>(bits | tag);
    }
    
    static ALWAYS_INLINE uintptr_t unTag(const JSValue* v)
    {
        return reinterpret_cast<uintptr_t>(v) & ~TagMask;
    }
    
    static ALWAYS_INLINE uintptr_t getTag(const JSValue* v)
    {
        return reinterpret_cast<uintptr_t>(v) & TagMask;
    }
};

ALWAYS_INLINE JSValue* JSImmediate::trueImmediate() { return tag(1 << 2, BooleanType); }
ALWAYS_INLINE JSValue* JSImmediate::falseImmediate() { return tag(0, BooleanType); }
ALWAYS_INLINE JSValue* JSImmediate::undefinedImmediate() { return tag(1 << 2, UndefinedType); }
ALWAYS_INLINE JSValue* JSImmediate::nullImmediate() { return tag(0, UndefinedType); }

ALWAYS_INLINE bool JSImmediate::toBoolean(const JSValue* v)
{
    ASSERT(isImmediate(v));
    uintptr_t bits = unTag(v);
    return (bits != 0) & (JSImmediate::getTag(v) != UndefinedType);
}

ALWAYS_INLINE JSValue* JSImmediate::from(char i)
{
    return tag(i << 2, NumberType);
}

ALWAYS_INLINE JSValue* JSImmediate::from(signed char i)
{
    return tag(i << 2, NumberType);
}

ALWAYS_INLINE JSValue* JSImmediate::from(unsigned char i)
{
    return tag(i << 2, NumberType);
}

ALWAYS_INLINE JSValue* JSImmediate::from(short i)
{
    return tag(i << 2, NumberType);
}

ALWAYS_INLINE JSValue* JSImmediate::from(unsigned short i)
{
    return tag(i << 2, NumberType);
}

ALWAYS_INLINE JSValue* JSImmediate::from(int i)
{
    if ((i < minImmediateInt) || (i > maxImmediateInt))
        return 0;
    return tag(i << 2, NumberType);
}

ALWAYS_INLINE JSValue* JSImmediate::from(unsigned i)
{
    if (i > maxImmediateUInt)
        return 0;
    return tag(i << 2, NumberType);
}

ALWAYS_INLINE JSValue* JSImmediate::from(long i)
{
    if ((i < minImmediateInt) || (i > maxImmediateInt))
        return 0;
    return tag(i << 2, NumberType);
}

ALWAYS_INLINE JSValue* JSImmediate::from(unsigned long i)
{
    if (i > maxImmediateUInt)
        return 0;
    return tag(i << 2, NumberType);
}

ALWAYS_INLINE JSValue* JSImmediate::from(long long i)
{
    if ((i < minImmediateInt) || (i > maxImmediateInt))
        return 0;
    return tag(static_cast<uintptr_t>(i) << 2, NumberType);
}

ALWAYS_INLINE JSValue* JSImmediate::from(unsigned long long i)
{
    if (i > maxImmediateUInt)
        return 0;
    return tag(static_cast<uintptr_t>(i) << 2, NumberType);
}

ALWAYS_INLINE JSValue* JSImmediate::from(double d)
{
    const int intVal = static_cast<int>(d);

    if ((intVal < minImmediateInt) || (intVal > maxImmediateInt))
        return 0;

    // Check for data loss from conversion to int.
    if ((intVal != d) || (!intVal && signbit(d)))
        return 0;

    return tag(intVal << 2, NumberType);
}

ALWAYS_INLINE int32_t JSImmediate::getTruncatedInt32(const JSValue* v)
{
    ASSERT(isNumber(v));
    return static_cast<int32_t>(unTag(v)) >> 2;
}

ALWAYS_INLINE double JSImmediate::toDouble(const JSValue* v)
{
    ASSERT(isImmediate(v));
    const int32_t i = static_cast<int32_t>(unTag(v)) >> 2;
    if (JSImmediate::getTag(v) == UndefinedType && i)
        return NaN;
    return i;
}

ALWAYS_INLINE double JSImmediate::getNumber(const JSValue* v)
{
    ASSERT(isImmediate(v));
    const int32_t i = static_cast<int32_t>(unTag(v)) >> 2;
    if (JSImmediate::getTag(v) != NumberType)
        return NaN;
    return i;
}

ALWAYS_INLINE bool JSImmediate::getNumber(const JSValue* v, double& numberOut)
{
    ASSERT(isImmediate(v));
    numberOut = static_cast<int32_t>(unTag(v)) >> 2;
    return (JSImmediate::getTag(v) == NumberType);
}

ALWAYS_INLINE bool JSImmediate::getUInt32(const JSValue* v, uint32_t& i)
{
    const int32_t si = static_cast<int32_t>(unTag(v)) >> 2;
    i = si;
    return isNumber(v) & (si >= 0);
}

ALWAYS_INLINE bool JSImmediate::getTruncatedInt32(const JSValue* v, int32_t& i)
{
    i = static_cast<int32_t>(unTag(v)) >> 2;
    return isNumber(v);
}

ALWAYS_INLINE bool JSImmediate::getTruncatedUInt32(const JSValue* v, uint32_t& i)
{
    return getUInt32(v, i);
}

} // namespace KJS

#endif