tizen beta release
[framework/web/webkit-efl.git] / 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         JSGlobalData& globalData = exec->globalData();
40
41         unsigned length1 = s1->length();
42         if (!length1)
43             return s2;
44         unsigned length2 = s2->length();
45         if (!length2)
46             return s1;
47         if ((length1 + length2) < length1)
48             return throwOutOfMemoryError(exec);
49
50         return fixupVPtr(&globalData, JSString::create(globalData, s1, s2));
51     }
52
53     ALWAYS_INLINE JSValue jsString(ExecState* exec, const UString& u1, const UString& u2, const UString& u3)
54     {
55         JSGlobalData* globalData = &exec->globalData();
56
57         unsigned length1 = u1.length();
58         unsigned length2 = u2.length();
59         unsigned length3 = u3.length();
60         if (!length1)
61             return jsString(exec, jsString(globalData, u2), jsString(globalData, u3));
62         if (!length2)
63             return jsString(exec, jsString(globalData, u1), jsString(globalData, u3));
64         if (!length3)
65             return jsString(exec, jsString(globalData, u1), jsString(globalData, u2));
66
67         if ((length1 + length2) < length1)
68             return throwOutOfMemoryError(exec);
69         if ((length1 + length2 + length3) < length3)
70             return throwOutOfMemoryError(exec);
71
72         return fixupVPtr(globalData, JSString::create(exec->globalData(), jsString(globalData, u1), jsString(globalData, u2), jsString(globalData, u3)));
73     }
74
75     ALWAYS_INLINE JSValue jsString(ExecState* exec, Register* strings, unsigned count)
76     {
77         JSGlobalData* globalData = &exec->globalData();
78         JSString::RopeBuilder ropeBuilder(*globalData);
79
80         unsigned oldLength = 0;
81
82         for (unsigned i = 0; i < count; ++i) {
83             JSValue v = strings[i].jsValue();
84             if (v.isString())
85                 ropeBuilder.append(asString(v));
86             else
87                 ropeBuilder.append(jsString(globalData, v.toString(exec)));
88
89             if (ropeBuilder.length() < oldLength) // True for overflow
90                 return throwOutOfMemoryError(exec);
91         }
92
93         return ropeBuilder.release();
94     }
95
96     ALWAYS_INLINE JSValue jsStringFromArguments(ExecState* exec, JSValue thisValue)
97     {
98         JSGlobalData* globalData = &exec->globalData();
99         JSString::RopeBuilder ropeBuilder(*globalData);
100
101         if (thisValue.isString())
102             ropeBuilder.append(asString(thisValue));
103         else
104             ropeBuilder.append(jsString(globalData, thisValue.toString(exec)));
105
106         unsigned oldLength = 0;
107
108         for (unsigned i = 0; i < exec->argumentCount(); ++i) {
109             JSValue v = exec->argument(i);
110             if (v.isString())
111                 ropeBuilder.append(asString(v));
112             else
113                 ropeBuilder.append(jsString(globalData, v.toString(exec)));
114
115             if (ropeBuilder.length() < oldLength) // True for overflow
116                 return throwOutOfMemoryError(exec);
117         }
118
119         return ropeBuilder.release();
120     }
121
122     // ECMA 11.9.3
123     inline bool JSValue::equal(ExecState* exec, JSValue v1, JSValue v2)
124     {
125         if (v1.isInt32() && v2.isInt32())
126             return v1 == v2;
127
128         return equalSlowCase(exec, v1, v2);
129     }
130
131     ALWAYS_INLINE bool JSValue::equalSlowCaseInline(ExecState* exec, JSValue v1, JSValue v2)
132     {
133         do {
134             if (v1.isNumber() && v2.isNumber())
135                 return v1.asNumber() == v2.asNumber();
136
137             bool s1 = v1.isString();
138             bool s2 = v2.isString();
139             if (s1 && s2)
140                 return asString(v1)->value(exec) == asString(v2)->value(exec);
141
142             if (v1.isUndefinedOrNull()) {
143                 if (v2.isUndefinedOrNull())
144                     return true;
145                 if (!v2.isCell())
146                     return false;
147                 return v2.asCell()->structure()->typeInfo().masqueradesAsUndefined();
148             }
149
150             if (v2.isUndefinedOrNull()) {
151                 if (!v1.isCell())
152                     return false;
153                 return v1.asCell()->structure()->typeInfo().masqueradesAsUndefined();
154             }
155
156             if (v1.isObject()) {
157                 if (v2.isObject())
158                     return v1 == v2;
159                 JSValue p1 = v1.toPrimitive(exec);
160                 if (exec->hadException())
161                     return false;
162                 v1 = p1;
163                 if (v1.isInt32() && v2.isInt32())
164                     return v1 == v2;
165                 continue;
166             }
167
168             if (v2.isObject()) {
169                 JSValue p2 = v2.toPrimitive(exec);
170                 if (exec->hadException())
171                     return false;
172                 v2 = p2;
173                 if (v1.isInt32() && v2.isInt32())
174                     return v1 == v2;
175                 continue;
176             }
177
178             if (s1 || s2) {
179                 double d1 = v1.toNumber(exec);
180                 double d2 = v2.toNumber(exec);
181                 return d1 == d2;
182             }
183
184             if (v1.isBoolean()) {
185                 if (v2.isNumber())
186                     return static_cast<double>(v1.asBoolean()) == v2.asNumber();
187             } else if (v2.isBoolean()) {
188                 if (v1.isNumber())
189                     return v1.asNumber() == static_cast<double>(v2.asBoolean());
190             }
191
192             return v1 == v2;
193         } while (true);
194     }
195
196     // ECMA 11.9.3
197     ALWAYS_INLINE bool JSValue::strictEqualSlowCaseInline(ExecState* exec, JSValue v1, JSValue v2)
198     {
199         ASSERT(v1.isCell() && v2.isCell());
200
201         if (v1.asCell()->isString() && v2.asCell()->isString())
202             return asString(v1)->value(exec) == asString(v2)->value(exec);
203
204         return v1 == v2;
205     }
206
207     inline bool JSValue::strictEqual(ExecState* exec, JSValue v1, JSValue v2)
208     {
209         if (v1.isInt32() && v2.isInt32())
210             return v1 == v2;
211
212         if (v1.isNumber() && v2.isNumber())
213             return v1.asNumber() == v2.asNumber();
214
215         if (!v1.isCell() || !v2.isCell())
216             return v1 == v2;
217
218         return strictEqualSlowCaseInline(exec, v1, v2);
219     }
220
221     // See ES5 11.8.1/11.8.2/11.8.5 for definition of leftFirst, this value ensures correct
222     // evaluation ordering for argument conversions for '<' and '>'. For '<' pass the value
223     // true, for leftFirst, for '>' pass the value false (and reverse operand order).
224     template<bool leftFirst>
225     ALWAYS_INLINE bool jsLess(CallFrame* callFrame, JSValue v1, JSValue v2)
226     {
227         if (v1.isInt32() && v2.isInt32())
228             return v1.asInt32() < v2.asInt32();
229
230         if (v1.isNumber() && v2.isNumber())
231             return v1.asNumber() < v2.asNumber();
232
233         JSGlobalData* globalData = &callFrame->globalData();
234         if (isJSString(globalData, v1) && isJSString(globalData, v2))
235             return asString(v1)->value(callFrame) < asString(v2)->value(callFrame);
236
237         double n1;
238         double n2;
239         JSValue p1;
240         JSValue p2;
241         bool wasNotString1;
242         bool wasNotString2;
243         if (leftFirst) {
244             wasNotString1 = v1.getPrimitiveNumber(callFrame, n1, p1);
245             wasNotString2 = v2.getPrimitiveNumber(callFrame, n2, p2);
246         } else {
247             wasNotString2 = v2.getPrimitiveNumber(callFrame, n2, p2);
248             wasNotString1 = v1.getPrimitiveNumber(callFrame, n1, p1);
249         }
250
251         if (wasNotString1 | wasNotString2)
252             return n1 < n2;
253         return asString(p1)->value(callFrame) < asString(p2)->value(callFrame);
254     }
255
256     // See ES5 11.8.3/11.8.4/11.8.5 for definition of leftFirst, this value ensures correct
257     // evaluation ordering for argument conversions for '<=' and '=>'. For '<=' pass the
258     // value true, for leftFirst, for '=>' pass the value false (and reverse operand order).
259     template<bool leftFirst>
260     ALWAYS_INLINE bool jsLessEq(CallFrame* callFrame, JSValue v1, JSValue v2)
261     {
262         if (v1.isInt32() && v2.isInt32())
263             return v1.asInt32() <= v2.asInt32();
264
265         if (v1.isNumber() && v2.isNumber())
266             return v1.asNumber() <= v2.asNumber();
267
268         JSGlobalData* globalData = &callFrame->globalData();
269         if (isJSString(globalData, v1) && isJSString(globalData, v2))
270             return !(asString(v2)->value(callFrame) < asString(v1)->value(callFrame));
271
272         double n1;
273         double n2;
274         JSValue p1;
275         JSValue p2;
276         bool wasNotString1;
277         bool wasNotString2;
278         if (leftFirst) {
279             wasNotString1 = v1.getPrimitiveNumber(callFrame, n1, p1);
280             wasNotString2 = v2.getPrimitiveNumber(callFrame, n2, p2);
281         } else {
282             wasNotString2 = v2.getPrimitiveNumber(callFrame, n2, p2);
283             wasNotString1 = v1.getPrimitiveNumber(callFrame, n1, p1);
284         }
285
286         if (wasNotString1 | wasNotString2)
287             return n1 <= n2;
288         return !(asString(p2)->value(callFrame) < asString(p1)->value(callFrame));
289     }
290
291     // Fast-path choices here are based on frequency data from SunSpider:
292     //    <times> Add case: <t1> <t2>
293     //    ---------------------------
294     //    5626160 Add case: 3 3 (of these, 3637690 are for immediate values)
295     //    247412  Add case: 5 5
296     //    20900   Add case: 5 6
297     //    13962   Add case: 5 3
298     //    4000    Add case: 3 5
299
300     ALWAYS_INLINE JSValue jsAdd(CallFrame* callFrame, JSValue v1, JSValue v2)
301     {
302         if (v1.isNumber() && v2.isNumber())
303             return jsNumber(v1.asNumber() + v2.asNumber());
304         
305         if (v1.isString()) {
306             return v2.isString()
307                 ? jsString(callFrame, asString(v1), asString(v2))
308                 : jsString(callFrame, asString(v1), v2.toPrimitiveString(callFrame));
309         }
310
311         // All other cases are pretty uncommon
312         return jsAddSlowCase(callFrame, v1, v2);
313     }
314
315     inline size_t normalizePrototypeChain(CallFrame* callFrame, JSValue base, JSValue slotBase, const Identifier& propertyName, size_t& slotOffset)
316     {
317         JSCell* cell = base.asCell();
318         size_t count = 0;
319
320         while (slotBase != cell) {
321             JSValue v = cell->structure()->prototypeForLookup(callFrame);
322
323             // If we didn't find slotBase in base's prototype chain, then base
324             // must be a proxy for another object.
325
326             if (v.isNull())
327                 return 0;
328
329             cell = v.asCell();
330
331             // Since we're accessing a prototype in a loop, it's a good bet that it
332             // should not be treated as a dictionary.
333             if (cell->structure()->isDictionary()) {
334                 asObject(cell)->flattenDictionaryObject(callFrame->globalData());
335                 if (slotBase == cell)
336                     slotOffset = cell->structure()->get(callFrame->globalData(), propertyName); 
337             }
338
339             ++count;
340         }
341         
342         ASSERT(count);
343         return count;
344     }
345
346     inline size_t normalizePrototypeChain(CallFrame* callFrame, JSCell* base)
347     {
348         size_t count = 0;
349         while (1) {
350             JSValue v = base->structure()->prototypeForLookup(callFrame);
351             if (v.isNull())
352                 return count;
353
354             base = v.asCell();
355
356             // Since we're accessing a prototype in a loop, it's a good bet that it
357             // should not be treated as a dictionary.
358             if (base->structure()->isDictionary())
359                 asObject(base)->flattenDictionaryObject(callFrame->globalData());
360
361             ++count;
362         }
363     }
364
365     ALWAYS_INLINE JSValue resolveBase(CallFrame* callFrame, Identifier& property, ScopeChainNode* scopeChain, bool isStrictPut)
366     {
367         ScopeChainIterator iter = scopeChain->begin();
368         ScopeChainIterator next = iter;
369         ++next;
370         ScopeChainIterator end = scopeChain->end();
371         ASSERT(iter != end);
372
373         PropertySlot slot;
374         JSObject* base;
375         while (true) {
376             base = iter->get();
377             if (next == end) {
378                 if (isStrictPut && !base->getPropertySlot(callFrame, property, slot))
379                     return JSValue();
380                 return base;
381             }
382             if (base->getPropertySlot(callFrame, property, slot))
383                 return base;
384
385             iter = next;
386             ++next;
387         }
388
389         ASSERT_NOT_REACHED();
390         return JSValue();
391     }
392 } // namespace JSC
393
394 #endif // Operations_h