~ubuntu-branches/ubuntu/raring/qtwebkit-source/raring-proposed

« back to all changes in this revision

Viewing changes to Source/JavaScriptCore/runtime/Operations.h

  • Committer: Package Import Robot
  • Author(s): Jonathan Riddell
  • Date: 2013-02-18 14:24:18 UTC
  • Revision ID: package-import@ubuntu.com-20130218142418-eon0jmjg3nj438uy
Tags: upstream-2.3
ImportĀ upstreamĀ versionĀ 2.3

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 *  Copyright (C) 1999-2000 Harri Porten (porten@kde.org)
 
3
 *  Copyright (C) 2002, 2005, 2006, 2007, 2008, 2009 Apple Inc. All rights reserved.
 
4
 *
 
5
 *  This library is free software; you can redistribute it and/or
 
6
 *  modify it under the terms of the GNU Library General Public
 
7
 *  License as published by the Free Software Foundation; either
 
8
 *  version 2 of the License, or (at your option) any later version.
 
9
 *
 
10
 *  This library is distributed in the hope that it will be useful,
 
11
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
13
 *  Library General Public License for more details.
 
14
 *
 
15
 *  You should have received a copy of the GNU Library General Public License
 
16
 *  along with this library; see the file COPYING.LIB.  If not, write to
 
17
 *  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 
18
 *  Boston, MA 02110-1301, USA.
 
19
 *
 
20
 */
 
21
 
 
22
#ifndef Operations_h
 
23
#define Operations_h
 
24
 
 
25
#include "ExceptionHelpers.h"
 
26
#include "Interpreter.h"
 
27
#include "JSProxy.h"
 
28
#include "JSString.h"
 
29
#include "JSValueInlines.h"
 
30
 
 
31
namespace JSC {
 
32
 
 
33
    NEVER_INLINE JSValue jsAddSlowCase(CallFrame*, JSValue, JSValue);
 
34
    JSValue jsTypeStringForValue(CallFrame*, JSValue);
 
35
    bool jsIsObjectType(CallFrame*, JSValue);
 
36
    bool jsIsFunctionType(JSValue);
 
37
 
 
38
    ALWAYS_INLINE JSValue jsString(ExecState* exec, JSString* s1, JSString* s2)
 
39
    {
 
40
        JSGlobalData& globalData = exec->globalData();
 
41
 
 
42
        unsigned length1 = s1->length();
 
43
        if (!length1)
 
44
            return s2;
 
45
        unsigned length2 = s2->length();
 
46
        if (!length2)
 
47
            return s1;
 
48
        if ((length1 + length2) < length1)
 
49
            return throwOutOfMemoryError(exec);
 
50
 
 
51
        return JSRopeString::create(globalData, s1, s2);
 
52
    }
 
53
 
 
54
    ALWAYS_INLINE JSValue jsString(ExecState* exec, const String& u1, const String& u2, const String& u3)
 
55
    {
 
56
        JSGlobalData* globalData = &exec->globalData();
 
57
 
 
58
        unsigned length1 = u1.length();
 
59
        unsigned length2 = u2.length();
 
60
        unsigned length3 = u3.length();
 
61
        if (!length1)
 
62
            return jsString(exec, jsString(globalData, u2), jsString(globalData, u3));
 
63
        if (!length2)
 
64
            return jsString(exec, jsString(globalData, u1), jsString(globalData, u3));
 
65
        if (!length3)
 
66
            return jsString(exec, jsString(globalData, u1), jsString(globalData, u2));
 
67
 
 
68
        if ((length1 + length2) < length1)
 
69
            return throwOutOfMemoryError(exec);
 
70
        if ((length1 + length2 + length3) < length3)
 
71
            return throwOutOfMemoryError(exec);
 
72
 
 
73
        return JSRopeString::create(exec->globalData(), jsString(globalData, u1), jsString(globalData, u2), jsString(globalData, u3));
 
74
    }
 
75
 
 
76
    ALWAYS_INLINE JSValue jsString(ExecState* exec, Register* strings, unsigned count)
 
77
    {
 
78
        JSGlobalData* globalData = &exec->globalData();
 
79
        JSRopeString::RopeBuilder ropeBuilder(*globalData);
 
80
 
 
81
        unsigned oldLength = 0;
 
82
 
 
83
        for (unsigned i = 0; i < count; ++i) {
 
84
            JSValue v = strings[i].jsValue();
 
85
            ropeBuilder.append(v.toString(exec));
 
86
 
 
87
            if (ropeBuilder.length() < oldLength) // True for overflow
 
88
                return throwOutOfMemoryError(exec);
 
89
            oldLength = ropeBuilder.length();
 
90
        }
 
91
 
 
92
        return ropeBuilder.release();
 
93
    }
 
94
 
 
95
    ALWAYS_INLINE JSValue jsStringFromArguments(ExecState* exec, JSValue thisValue)
 
96
    {
 
97
        JSGlobalData* globalData = &exec->globalData();
 
98
        JSRopeString::RopeBuilder ropeBuilder(*globalData);
 
99
        ropeBuilder.append(thisValue.toString(exec));
 
100
 
 
101
        unsigned oldLength = 0;
 
102
 
 
103
        for (unsigned i = 0; i < exec->argumentCount(); ++i) {
 
104
            JSValue v = exec->argument(i);
 
105
            ropeBuilder.append(v.toString(exec));
 
106
 
 
107
            if (ropeBuilder.length() < oldLength) // True for overflow
 
108
                return throwOutOfMemoryError(exec);
 
109
            oldLength = ropeBuilder.length();
 
110
        }
 
111
 
 
112
        return ropeBuilder.release();
 
113
    }
 
114
 
 
115
    // ECMA 11.9.3
 
116
    inline bool JSValue::equal(ExecState* exec, JSValue v1, JSValue v2)
 
117
    {
 
118
        if (v1.isInt32() && v2.isInt32())
 
119
            return v1 == v2;
 
120
 
 
121
        return equalSlowCase(exec, v1, v2);
 
122
    }
 
123
 
 
124
    ALWAYS_INLINE bool JSValue::equalSlowCaseInline(ExecState* exec, JSValue v1, JSValue v2)
 
125
    {
 
126
        do {
 
127
            if (v1.isNumber() && v2.isNumber())
 
128
                return v1.asNumber() == v2.asNumber();
 
129
 
 
130
            bool s1 = v1.isString();
 
131
            bool s2 = v2.isString();
 
132
            if (s1 && s2)
 
133
                return asString(v1)->value(exec) == asString(v2)->value(exec);
 
134
 
 
135
            if (v1.isUndefinedOrNull()) {
 
136
                if (v2.isUndefinedOrNull())
 
137
                    return true;
 
138
                if (!v2.isCell())
 
139
                    return false;
 
140
                return v2.asCell()->structure()->masqueradesAsUndefined(exec->lexicalGlobalObject());
 
141
            }
 
142
 
 
143
            if (v2.isUndefinedOrNull()) {
 
144
                if (!v1.isCell())
 
145
                    return false;
 
146
                return v1.asCell()->structure()->masqueradesAsUndefined(exec->lexicalGlobalObject());
 
147
            }
 
148
 
 
149
            if (v1.isObject()) {
 
150
                if (v2.isObject())
 
151
                    return v1 == v2;
 
152
                JSValue p1 = v1.toPrimitive(exec);
 
153
                if (exec->hadException())
 
154
                    return false;
 
155
                v1 = p1;
 
156
                if (v1.isInt32() && v2.isInt32())
 
157
                    return v1 == v2;
 
158
                continue;
 
159
            }
 
160
 
 
161
            if (v2.isObject()) {
 
162
                JSValue p2 = v2.toPrimitive(exec);
 
163
                if (exec->hadException())
 
164
                    return false;
 
165
                v2 = p2;
 
166
                if (v1.isInt32() && v2.isInt32())
 
167
                    return v1 == v2;
 
168
                continue;
 
169
            }
 
170
 
 
171
            if (s1 || s2) {
 
172
                double d1 = v1.toNumber(exec);
 
173
                double d2 = v2.toNumber(exec);
 
174
                return d1 == d2;
 
175
            }
 
176
 
 
177
            if (v1.isBoolean()) {
 
178
                if (v2.isNumber())
 
179
                    return static_cast<double>(v1.asBoolean()) == v2.asNumber();
 
180
            } else if (v2.isBoolean()) {
 
181
                if (v1.isNumber())
 
182
                    return v1.asNumber() == static_cast<double>(v2.asBoolean());
 
183
            }
 
184
 
 
185
            return v1 == v2;
 
186
        } while (true);
 
187
    }
 
188
 
 
189
    // ECMA 11.9.3
 
190
    ALWAYS_INLINE bool JSValue::strictEqualSlowCaseInline(ExecState* exec, JSValue v1, JSValue v2)
 
191
    {
 
192
        ASSERT(v1.isCell() && v2.isCell());
 
193
 
 
194
        if (v1.asCell()->isString() && v2.asCell()->isString())
 
195
            return asString(v1)->value(exec) == asString(v2)->value(exec);
 
196
 
 
197
        return v1 == v2;
 
198
    }
 
199
 
 
200
    inline bool JSValue::strictEqual(ExecState* exec, JSValue v1, JSValue v2)
 
201
    {
 
202
        if (v1.isInt32() && v2.isInt32())
 
203
            return v1 == v2;
 
204
 
 
205
        if (v1.isNumber() && v2.isNumber())
 
206
            return v1.asNumber() == v2.asNumber();
 
207
 
 
208
        if (!v1.isCell() || !v2.isCell())
 
209
            return v1 == v2;
 
210
 
 
211
        return strictEqualSlowCaseInline(exec, v1, v2);
 
212
    }
 
213
 
 
214
    // See ES5 11.8.1/11.8.2/11.8.5 for definition of leftFirst, this value ensures correct
 
215
    // evaluation ordering for argument conversions for '<' and '>'. For '<' pass the value
 
216
    // true, for leftFirst, for '>' pass the value false (and reverse operand order).
 
217
    template<bool leftFirst>
 
218
    ALWAYS_INLINE bool jsLess(CallFrame* callFrame, JSValue v1, JSValue v2)
 
219
    {
 
220
        if (v1.isInt32() && v2.isInt32())
 
221
            return v1.asInt32() < v2.asInt32();
 
222
 
 
223
        if (v1.isNumber() && v2.isNumber())
 
224
            return v1.asNumber() < v2.asNumber();
 
225
 
 
226
        if (isJSString(v1) && isJSString(v2))
 
227
            return codePointCompareLessThan(asString(v1)->value(callFrame), asString(v2)->value(callFrame));
 
228
 
 
229
        double n1;
 
230
        double n2;
 
231
        JSValue p1;
 
232
        JSValue p2;
 
233
        bool wasNotString1;
 
234
        bool wasNotString2;
 
235
        if (leftFirst) {
 
236
            wasNotString1 = v1.getPrimitiveNumber(callFrame, n1, p1);
 
237
            wasNotString2 = v2.getPrimitiveNumber(callFrame, n2, p2);
 
238
        } else {
 
239
            wasNotString2 = v2.getPrimitiveNumber(callFrame, n2, p2);
 
240
            wasNotString1 = v1.getPrimitiveNumber(callFrame, n1, p1);
 
241
        }
 
242
 
 
243
        if (wasNotString1 | wasNotString2)
 
244
            return n1 < n2;
 
245
        return codePointCompareLessThan(asString(p1)->value(callFrame), asString(p2)->value(callFrame));
 
246
    }
 
247
 
 
248
    // See ES5 11.8.3/11.8.4/11.8.5 for definition of leftFirst, this value ensures correct
 
249
    // evaluation ordering for argument conversions for '<=' and '=>'. For '<=' pass the
 
250
    // value true, for leftFirst, for '=>' pass the value false (and reverse operand order).
 
251
    template<bool leftFirst>
 
252
    ALWAYS_INLINE bool jsLessEq(CallFrame* callFrame, JSValue v1, JSValue v2)
 
253
    {
 
254
        if (v1.isInt32() && v2.isInt32())
 
255
            return v1.asInt32() <= v2.asInt32();
 
256
 
 
257
        if (v1.isNumber() && v2.isNumber())
 
258
            return v1.asNumber() <= v2.asNumber();
 
259
 
 
260
        if (isJSString(v1) && isJSString(v2))
 
261
            return !codePointCompareLessThan(asString(v2)->value(callFrame), asString(v1)->value(callFrame));
 
262
 
 
263
        double n1;
 
264
        double n2;
 
265
        JSValue p1;
 
266
        JSValue p2;
 
267
        bool wasNotString1;
 
268
        bool wasNotString2;
 
269
        if (leftFirst) {
 
270
            wasNotString1 = v1.getPrimitiveNumber(callFrame, n1, p1);
 
271
            wasNotString2 = v2.getPrimitiveNumber(callFrame, n2, p2);
 
272
        } else {
 
273
            wasNotString2 = v2.getPrimitiveNumber(callFrame, n2, p2);
 
274
            wasNotString1 = v1.getPrimitiveNumber(callFrame, n1, p1);
 
275
        }
 
276
 
 
277
        if (wasNotString1 | wasNotString2)
 
278
            return n1 <= n2;
 
279
        return !codePointCompareLessThan(asString(p2)->value(callFrame), asString(p1)->value(callFrame));
 
280
    }
 
281
 
 
282
    // Fast-path choices here are based on frequency data from SunSpider:
 
283
    //    <times> Add case: <t1> <t2>
 
284
    //    ---------------------------
 
285
    //    5626160 Add case: 3 3 (of these, 3637690 are for immediate values)
 
286
    //    247412  Add case: 5 5
 
287
    //    20900   Add case: 5 6
 
288
    //    13962   Add case: 5 3
 
289
    //    4000    Add case: 3 5
 
290
 
 
291
    ALWAYS_INLINE JSValue jsAdd(CallFrame* callFrame, JSValue v1, JSValue v2)
 
292
    {
 
293
        if (v1.isNumber() && v2.isNumber())
 
294
            return jsNumber(v1.asNumber() + v2.asNumber());
 
295
        
 
296
        if (v1.isString() && !v2.isObject())
 
297
            return jsString(callFrame, asString(v1), v2.toString(callFrame));
 
298
 
 
299
        // All other cases are pretty uncommon
 
300
        return jsAddSlowCase(callFrame, v1, v2);
 
301
    }
 
302
 
 
303
#define InvalidPrototypeChain (std::numeric_limits<size_t>::max())
 
304
 
 
305
    inline size_t normalizePrototypeChain(CallFrame* callFrame, JSValue base, JSValue slotBase, const Identifier& propertyName, PropertyOffset& slotOffset)
 
306
    {
 
307
        JSCell* cell = base.asCell();
 
308
        size_t count = 0;
 
309
 
 
310
        while (slotBase != cell) {
 
311
            if (cell->isProxy())
 
312
                return InvalidPrototypeChain;
 
313
            
 
314
            JSValue v = cell->structure()->prototypeForLookup(callFrame);
 
315
 
 
316
            // If we didn't find slotBase in base's prototype chain, then base
 
317
            // must be a proxy for another object.
 
318
 
 
319
            if (v.isNull())
 
320
                return InvalidPrototypeChain;
 
321
 
 
322
            cell = v.asCell();
 
323
 
 
324
            // Since we're accessing a prototype in a loop, it's a good bet that it
 
325
            // should not be treated as a dictionary.
 
326
            if (cell->structure()->isDictionary()) {
 
327
                asObject(cell)->flattenDictionaryObject(callFrame->globalData());
 
328
                if (slotBase == cell)
 
329
                    slotOffset = cell->structure()->get(callFrame->globalData(), propertyName); 
 
330
            }
 
331
 
 
332
            ++count;
 
333
        }
 
334
        
 
335
        ASSERT(count);
 
336
        return count;
 
337
    }
 
338
 
 
339
    inline size_t normalizePrototypeChain(CallFrame* callFrame, JSCell* base)
 
340
    {
 
341
        size_t count = 0;
 
342
        while (1) {
 
343
            if (base->isProxy())
 
344
                return InvalidPrototypeChain;
 
345
            
 
346
            JSValue v = base->structure()->prototypeForLookup(callFrame);
 
347
            if (v.isNull())
 
348
                return count;
 
349
 
 
350
            base = v.asCell();
 
351
 
 
352
            // Since we're accessing a prototype in a loop, it's a good bet that it
 
353
            // should not be treated as a dictionary.
 
354
            if (base->structure()->isDictionary())
 
355
                asObject(base)->flattenDictionaryObject(callFrame->globalData());
 
356
 
 
357
            ++count;
 
358
        }
 
359
    }
 
360
 
 
361
    inline bool isPrototypeChainNormalized(JSGlobalObject* globalObject, Structure* structure)
 
362
    {
 
363
        for (;;) {
 
364
            if (structure->typeInfo().type() == ProxyType)
 
365
                return false;
 
366
            
 
367
            JSValue v = structure->prototypeForLookup(globalObject);
 
368
            if (v.isNull())
 
369
                return true;
 
370
            
 
371
            structure = v.asCell()->structure();
 
372
            
 
373
            if (structure->isDictionary())
 
374
                return false;
 
375
        }
 
376
    }
 
377
 
 
378
} // namespace JSC
 
379
 
 
380
#endif // Operations_h