initial import
[vuplus_webkit] / Source / JavaScriptCore / runtime / Operations.h
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 "JSString.h"
28 #include "JSValueInlineMethods.h"
29
30 namespace JSC {
31
32     NEVER_INLINE JSValue jsAddSlowCase(CallFrame*, JSValue, JSValue);
33     JSValue jsTypeStringForValue(CallFrame*, JSValue);
34     bool jsIsObjectType(JSValue);
35     bool jsIsFunctionType(JSValue);
36
37     ALWAYS_INLINE JSValue jsString(ExecState* exec, JSString* s1, JSString* s2)
38     {
39         unsigned length1 = s1->length();
40         if (!length1)
41             return s2;
42         unsigned length2 = s2->length();
43         if (!length2)
44             return s1;
45         if ((length1 + length2) < length1)
46             return throwOutOfMemoryError(exec);
47
48         unsigned fiberCount = s1->fiberCount() + s2->fiberCount();
49         JSGlobalData* globalData = &exec->globalData();
50
51         if (fiberCount <= JSString::s_maxInternalRopeLength)
52             return JSString::create(*globalData, fiberCount, s1, s2);
53
54         JSString::RopeBuilder ropeBuilder(fiberCount);
55         if (UNLIKELY(ropeBuilder.isOutOfMemory()))
56             return throwOutOfMemoryError(exec);
57         ropeBuilder.append(s1);
58         ropeBuilder.append(s2);
59         return JSString::create(*globalData, ropeBuilder.release());
60     }
61
62     ALWAYS_INLINE JSValue jsString(ExecState* exec, const UString& u1, JSString* s2)
63     {
64         unsigned length1 = u1.length();
65         if (!length1)
66             return s2;
67         unsigned length2 = s2->length();
68         if (!length2)
69             return jsString(exec, u1);
70         if ((length1 + length2) < length1)
71             return throwOutOfMemoryError(exec);
72
73         unsigned fiberCount = 1 + s2->fiberCount();
74         JSGlobalData* globalData = &exec->globalData();
75
76         if (fiberCount <= JSString::s_maxInternalRopeLength)
77             return JSString::create(*globalData, fiberCount, u1, s2);
78
79         JSString::RopeBuilder ropeBuilder(fiberCount);
80         if (UNLIKELY(ropeBuilder.isOutOfMemory()))
81             return throwOutOfMemoryError(exec);
82         ropeBuilder.append(u1);
83         ropeBuilder.append(s2);
84         return JSString::create(*globalData, ropeBuilder.release());
85     }
86
87     ALWAYS_INLINE JSValue jsString(ExecState* exec, JSString* s1, const UString& u2)
88     {
89         unsigned length1 = s1->length();
90         if (!length1)
91             return jsString(exec, u2);
92         unsigned length2 = u2.length();
93         if (!length2)
94             return s1;
95         if ((length1 + length2) < length1)
96             return throwOutOfMemoryError(exec);
97
98         unsigned fiberCount = s1->fiberCount() + 1;
99         JSGlobalData* globalData = &exec->globalData();
100
101         if (fiberCount <= JSString::s_maxInternalRopeLength)
102             return JSString::create(*globalData, fiberCount, s1, u2);
103
104         JSString::RopeBuilder ropeBuilder(fiberCount);
105         if (UNLIKELY(ropeBuilder.isOutOfMemory()))
106             return throwOutOfMemoryError(exec);
107         ropeBuilder.append(s1);
108         ropeBuilder.append(u2);
109         return JSString::create(*globalData, ropeBuilder.release());
110     }
111
112     ALWAYS_INLINE JSValue jsString(ExecState* exec, const UString& u1, const UString& u2)
113     {
114         unsigned length1 = u1.length();
115         if (!length1)
116             return jsString(exec, u2);
117         unsigned length2 = u2.length();
118         if (!length2)
119             return jsString(exec, u1);
120         if ((length1 + length2) < length1)
121             return throwOutOfMemoryError(exec);
122
123         JSGlobalData* globalData = &exec->globalData();
124         return JSString::create(*globalData, u1, u2);
125     }
126
127     ALWAYS_INLINE JSValue jsString(ExecState* exec, const UString& u1, const UString& u2, const UString& u3)
128     {
129         unsigned length1 = u1.length();
130         unsigned length2 = u2.length();
131         unsigned length3 = u3.length();
132         if (!length1)
133             return jsString(exec, u2, u3);
134         if (!length2)
135             return jsString(exec, u1, u3);
136         if (!length3)
137             return jsString(exec, u1, u2);
138
139         if ((length1 + length2) < length1)
140             return throwOutOfMemoryError(exec);
141         if ((length1 + length2 + length3) < length3)
142             return throwOutOfMemoryError(exec);
143
144         JSGlobalData* globalData = &exec->globalData();
145         return JSString::create(*globalData, u1, u2, u3);
146     }
147
148     ALWAYS_INLINE JSValue jsString(ExecState* exec, Register* strings, unsigned count)
149     {
150         ASSERT(count >= 3);
151
152         unsigned fiberCount = 0;
153         for (unsigned i = 0; i < count; ++i) {
154             JSValue v = strings[i].jsValue();
155             if (LIKELY(v.isString()))
156                 fiberCount += asString(v)->fiberCount();
157             else
158                 ++fiberCount;
159         }
160
161         JSGlobalData* globalData = &exec->globalData();
162         if (fiberCount == 3)
163             return JSString::create(exec, strings[0].jsValue(), strings[1].jsValue(), strings[2].jsValue());
164
165         JSString::RopeBuilder ropeBuilder(fiberCount);
166         if (UNLIKELY(ropeBuilder.isOutOfMemory()))
167             return throwOutOfMemoryError(exec);
168
169         unsigned length = 0;
170         bool overflow = false;
171
172         for (unsigned i = 0; i < count; ++i) {
173             JSValue v = strings[i].jsValue();
174             if (LIKELY(v.isString()))
175                 ropeBuilder.append(asString(v));
176             else
177                 ropeBuilder.append(v.toString(exec));
178
179             unsigned newLength = ropeBuilder.length();
180             if (newLength < length)
181                 overflow = true;
182             length = newLength;
183         }
184
185         if (overflow)
186             return throwOutOfMemoryError(exec);
187
188         return JSString::create(*globalData, ropeBuilder.release());
189     }
190
191     ALWAYS_INLINE JSValue jsString(ExecState* exec, JSValue thisValue)
192     {
193         unsigned fiberCount = 0;
194         if (LIKELY(thisValue.isString()))
195             fiberCount += asString(thisValue)->fiberCount();
196         else
197             ++fiberCount;
198         for (unsigned i = 0; i < exec->argumentCount(); ++i) {
199             JSValue v = exec->argument(i);
200             if (LIKELY(v.isString()))
201                 fiberCount += asString(v)->fiberCount();
202             else
203                 ++fiberCount;
204         }
205
206         JSString::RopeBuilder ropeBuilder(fiberCount);
207         if (UNLIKELY(ropeBuilder.isOutOfMemory()))
208             return throwOutOfMemoryError(exec);
209
210         if (LIKELY(thisValue.isString()))
211             ropeBuilder.append(asString(thisValue));
212         else
213             ropeBuilder.append(thisValue.toString(exec));
214
215         unsigned length = 0;
216         bool overflow = false;
217
218         for (unsigned i = 0; i < exec->argumentCount(); ++i) {
219             JSValue v = exec->argument(i);
220             if (LIKELY(v.isString()))
221                 ropeBuilder.append(asString(v));
222             else
223                 ropeBuilder.append(v.toString(exec));
224
225             unsigned newLength = ropeBuilder.length();
226             if (newLength < length)
227                 overflow = true;
228             length = newLength;
229         }
230
231         if (overflow)
232             return throwOutOfMemoryError(exec);
233
234         JSGlobalData* globalData = &exec->globalData();
235         return JSString::create(*globalData, ropeBuilder.release());
236     }
237
238     // ECMA 11.9.3
239     inline bool JSValue::equal(ExecState* exec, JSValue v1, JSValue v2)
240     {
241         if (v1.isInt32() && v2.isInt32())
242             return v1 == v2;
243
244         return equalSlowCase(exec, v1, v2);
245     }
246
247     ALWAYS_INLINE bool JSValue::equalSlowCaseInline(ExecState* exec, JSValue v1, JSValue v2)
248     {
249         do {
250             if (v1.isNumber() && v2.isNumber())
251                 return v1.uncheckedGetNumber() == v2.uncheckedGetNumber();
252
253             bool s1 = v1.isString();
254             bool s2 = v2.isString();
255             if (s1 && s2)
256                 return asString(v1)->value(exec) == asString(v2)->value(exec);
257
258             if (v1.isUndefinedOrNull()) {
259                 if (v2.isUndefinedOrNull())
260                     return true;
261                 if (!v2.isCell())
262                     return false;
263                 return v2.asCell()->structure()->typeInfo().masqueradesAsUndefined();
264             }
265
266             if (v2.isUndefinedOrNull()) {
267                 if (!v1.isCell())
268                     return false;
269                 return v1.asCell()->structure()->typeInfo().masqueradesAsUndefined();
270             }
271
272             if (v1.isObject()) {
273                 if (v2.isObject())
274                     return v1 == v2;
275                 JSValue p1 = v1.toPrimitive(exec);
276                 if (exec->hadException())
277                     return false;
278                 v1 = p1;
279                 if (v1.isInt32() && v2.isInt32())
280                     return v1 == v2;
281                 continue;
282             }
283
284             if (v2.isObject()) {
285                 JSValue p2 = v2.toPrimitive(exec);
286                 if (exec->hadException())
287                     return false;
288                 v2 = p2;
289                 if (v1.isInt32() && v2.isInt32())
290                     return v1 == v2;
291                 continue;
292             }
293
294             if (s1 || s2) {
295                 double d1 = v1.toNumber(exec);
296                 double d2 = v2.toNumber(exec);
297                 return d1 == d2;
298             }
299
300             if (v1.isBoolean()) {
301                 if (v2.isNumber())
302                     return static_cast<double>(v1.getBoolean()) == v2.uncheckedGetNumber();
303             } else if (v2.isBoolean()) {
304                 if (v1.isNumber())
305                     return v1.uncheckedGetNumber() == static_cast<double>(v2.getBoolean());
306             }
307
308             return v1 == v2;
309         } while (true);
310     }
311
312     // ECMA 11.9.3
313     ALWAYS_INLINE bool JSValue::strictEqualSlowCaseInline(ExecState* exec, JSValue v1, JSValue v2)
314     {
315         ASSERT(v1.isCell() && v2.isCell());
316
317         if (v1.asCell()->isString() && v2.asCell()->isString())
318             return asString(v1)->value(exec) == asString(v2)->value(exec);
319
320         return v1 == v2;
321     }
322
323     inline bool JSValue::strictEqual(ExecState* exec, JSValue v1, JSValue v2)
324     {
325         if (v1.isInt32() && v2.isInt32())
326             return v1 == v2;
327
328         if (v1.isNumber() && v2.isNumber())
329             return v1.uncheckedGetNumber() == v2.uncheckedGetNumber();
330
331         if (!v1.isCell() || !v2.isCell())
332             return v1 == v2;
333
334         return strictEqualSlowCaseInline(exec, v1, v2);
335     }
336
337     // See ES5 11.8.1/11.8.2/11.8.5 for definition of leftFirst, this value ensures correct
338     // evaluation ordering for argument conversions for '<' and '>'. For '<' pass the value
339     // true, for leftFirst, for '>' pass the value false (and reverse operand order).
340     template<bool leftFirst>
341     ALWAYS_INLINE bool jsLess(CallFrame* callFrame, JSValue v1, JSValue v2)
342     {
343         if (v1.isInt32() && v2.isInt32())
344             return v1.asInt32() < v2.asInt32();
345
346         double n1;
347         double n2;
348         if (v1.getNumber(n1) && v2.getNumber(n2))
349             return n1 < n2;
350
351         JSGlobalData* globalData = &callFrame->globalData();
352         if (isJSString(globalData, v1) && isJSString(globalData, v2))
353             return asString(v1)->value(callFrame) < asString(v2)->value(callFrame);
354
355         JSValue p1;
356         JSValue p2;
357         bool wasNotString1;
358         bool wasNotString2;
359         if (leftFirst) {
360             wasNotString1 = v1.getPrimitiveNumber(callFrame, n1, p1);
361             wasNotString2 = v2.getPrimitiveNumber(callFrame, n2, p2);
362         } else {
363             wasNotString2 = v2.getPrimitiveNumber(callFrame, n2, p2);
364             wasNotString1 = v1.getPrimitiveNumber(callFrame, n1, p1);
365         }
366
367         if (wasNotString1 | wasNotString2)
368             return n1 < n2;
369         return asString(p1)->value(callFrame) < asString(p2)->value(callFrame);
370     }
371
372     // See ES5 11.8.3/11.8.4/11.8.5 for definition of leftFirst, this value ensures correct
373     // evaluation ordering for argument conversions for '<=' and '=>'. For '<=' pass the
374     // value true, for leftFirst, for '=>' pass the value false (and reverse operand order).
375     template<bool leftFirst>
376     ALWAYS_INLINE bool jsLessEq(CallFrame* callFrame, JSValue v1, JSValue v2)
377     {
378         if (v1.isInt32() && v2.isInt32())
379             return v1.asInt32() <= v2.asInt32();
380
381         double n1;
382         double n2;
383         if (v1.getNumber(n1) && v2.getNumber(n2))
384             return n1 <= n2;
385
386         JSGlobalData* globalData = &callFrame->globalData();
387         if (isJSString(globalData, v1) && isJSString(globalData, v2))
388             return !(asString(v2)->value(callFrame) < asString(v1)->value(callFrame));
389
390         JSValue p1;
391         JSValue p2;
392         bool wasNotString1;
393         bool wasNotString2;
394         if (leftFirst) {
395             wasNotString1 = v1.getPrimitiveNumber(callFrame, n1, p1);
396             wasNotString2 = v2.getPrimitiveNumber(callFrame, n2, p2);
397         } else {
398             wasNotString2 = v2.getPrimitiveNumber(callFrame, n2, p2);
399             wasNotString1 = v1.getPrimitiveNumber(callFrame, n1, p1);
400         }
401
402         if (wasNotString1 | wasNotString2)
403             return n1 <= n2;
404         return !(asString(p2)->value(callFrame) < asString(p1)->value(callFrame));
405     }
406
407     // Fast-path choices here are based on frequency data from SunSpider:
408     //    <times> Add case: <t1> <t2>
409     //    ---------------------------
410     //    5626160 Add case: 3 3 (of these, 3637690 are for immediate values)
411     //    247412  Add case: 5 5
412     //    20900   Add case: 5 6
413     //    13962   Add case: 5 3
414     //    4000    Add case: 3 5
415
416     ALWAYS_INLINE JSValue jsAdd(CallFrame* callFrame, JSValue v1, JSValue v2)
417     {
418         double left = 0.0, right;
419         if (v1.getNumber(left) && v2.getNumber(right))
420             return jsNumber(left + right);
421         
422         if (v1.isString()) {
423             return v2.isString()
424                 ? jsString(callFrame, asString(v1), asString(v2))
425                 : jsString(callFrame, asString(v1), v2.toPrimitiveString(callFrame));
426         }
427
428         // All other cases are pretty uncommon
429         return jsAddSlowCase(callFrame, v1, v2);
430     }
431
432     inline size_t normalizePrototypeChain(CallFrame* callFrame, JSValue base, JSValue slotBase, const Identifier& propertyName, size_t& slotOffset)
433     {
434         JSCell* cell = base.asCell();
435         size_t count = 0;
436
437         while (slotBase != cell) {
438             JSValue v = cell->structure()->prototypeForLookup(callFrame);
439
440             // If we didn't find slotBase in base's prototype chain, then base
441             // must be a proxy for another object.
442
443             if (v.isNull())
444                 return 0;
445
446             cell = v.asCell();
447
448             // Since we're accessing a prototype in a loop, it's a good bet that it
449             // should not be treated as a dictionary.
450             if (cell->structure()->isDictionary()) {
451                 asObject(cell)->flattenDictionaryObject(callFrame->globalData());
452                 if (slotBase == cell)
453                     slotOffset = cell->structure()->get(callFrame->globalData(), propertyName); 
454             }
455
456             ++count;
457         }
458         
459         ASSERT(count);
460         return count;
461     }
462
463     inline size_t normalizePrototypeChain(CallFrame* callFrame, JSCell* base)
464     {
465         size_t count = 0;
466         while (1) {
467             JSValue v = base->structure()->prototypeForLookup(callFrame);
468             if (v.isNull())
469                 return count;
470
471             base = v.asCell();
472
473             // Since we're accessing a prototype in a loop, it's a good bet that it
474             // should not be treated as a dictionary.
475             if (base->structure()->isDictionary())
476                 asObject(base)->flattenDictionaryObject(callFrame->globalData());
477
478             ++count;
479         }
480     }
481
482     ALWAYS_INLINE JSValue resolveBase(CallFrame* callFrame, Identifier& property, ScopeChainNode* scopeChain, bool isStrictPut)
483     {
484         ScopeChainIterator iter = scopeChain->begin();
485         ScopeChainIterator next = iter;
486         ++next;
487         ScopeChainIterator end = scopeChain->end();
488         ASSERT(iter != end);
489
490         PropertySlot slot;
491         JSObject* base;
492         while (true) {
493             base = iter->get();
494             if (next == end) {
495                 if (isStrictPut && !base->getPropertySlot(callFrame, property, slot))
496                     return JSValue();
497                 return base;
498             }
499             if (base->getPropertySlot(callFrame, property, slot))
500                 return base;
501
502             iter = next;
503             ++next;
504         }
505
506         ASSERT_NOT_REACHED();
507         return JSValue();
508     }
509 } // namespace JSC
510
511 #endif // Operations_h