2 * Copyright (C) 1999-2000 Harri Porten (porten@kde.org)
3 * Copyright (C) 2002, 2005, 2006, 2007, 2008, 2009 Apple Inc. All rights reserved.
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.
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.
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.
25 #include "ExceptionHelpers.h"
26 #include "Interpreter.h"
28 #include "JSValueInlineMethods.h"
32 NEVER_INLINE JSValue jsAddSlowCase(CallFrame*, JSValue, JSValue);
33 JSValue jsTypeStringForValue(CallFrame*, JSValue);
34 bool jsIsObjectType(JSValue);
35 bool jsIsFunctionType(JSValue);
37 ALWAYS_INLINE JSValue jsString(ExecState* exec, JSString* s1, JSString* s2)
39 unsigned length1 = s1->length();
42 unsigned length2 = s2->length();
45 if ((length1 + length2) < length1)
46 return throwOutOfMemoryError(exec);
48 unsigned fiberCount = s1->fiberCount() + s2->fiberCount();
49 JSGlobalData* globalData = &exec->globalData();
51 if (fiberCount <= JSString::s_maxInternalRopeLength)
52 return JSString::create(*globalData, fiberCount, s1, s2);
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());
62 ALWAYS_INLINE JSValue jsString(ExecState* exec, const UString& u1, JSString* s2)
64 unsigned length1 = u1.length();
67 unsigned length2 = s2->length();
69 return jsString(exec, u1);
70 if ((length1 + length2) < length1)
71 return throwOutOfMemoryError(exec);
73 unsigned fiberCount = 1 + s2->fiberCount();
74 JSGlobalData* globalData = &exec->globalData();
76 if (fiberCount <= JSString::s_maxInternalRopeLength)
77 return JSString::create(*globalData, fiberCount, u1, s2);
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());
87 ALWAYS_INLINE JSValue jsString(ExecState* exec, JSString* s1, const UString& u2)
89 unsigned length1 = s1->length();
91 return jsString(exec, u2);
92 unsigned length2 = u2.length();
95 if ((length1 + length2) < length1)
96 return throwOutOfMemoryError(exec);
98 unsigned fiberCount = s1->fiberCount() + 1;
99 JSGlobalData* globalData = &exec->globalData();
101 if (fiberCount <= JSString::s_maxInternalRopeLength)
102 return JSString::create(*globalData, fiberCount, s1, u2);
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());
112 ALWAYS_INLINE JSValue jsString(ExecState* exec, const UString& u1, const UString& u2)
114 unsigned length1 = u1.length();
116 return jsString(exec, u2);
117 unsigned length2 = u2.length();
119 return jsString(exec, u1);
120 if ((length1 + length2) < length1)
121 return throwOutOfMemoryError(exec);
123 JSGlobalData* globalData = &exec->globalData();
124 return JSString::create(*globalData, u1, u2);
127 ALWAYS_INLINE JSValue jsString(ExecState* exec, const UString& u1, const UString& u2, const UString& u3)
129 unsigned length1 = u1.length();
130 unsigned length2 = u2.length();
131 unsigned length3 = u3.length();
133 return jsString(exec, u2, u3);
135 return jsString(exec, u1, u3);
137 return jsString(exec, u1, u2);
139 if ((length1 + length2) < length1)
140 return throwOutOfMemoryError(exec);
141 if ((length1 + length2 + length3) < length3)
142 return throwOutOfMemoryError(exec);
144 JSGlobalData* globalData = &exec->globalData();
145 return JSString::create(*globalData, u1, u2, u3);
148 ALWAYS_INLINE JSValue jsString(ExecState* exec, Register* strings, unsigned count)
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();
161 JSGlobalData* globalData = &exec->globalData();
163 return JSString::create(exec, strings[0].jsValue(), strings[1].jsValue(), strings[2].jsValue());
165 JSString::RopeBuilder ropeBuilder(fiberCount);
166 if (UNLIKELY(ropeBuilder.isOutOfMemory()))
167 return throwOutOfMemoryError(exec);
170 bool overflow = false;
172 for (unsigned i = 0; i < count; ++i) {
173 JSValue v = strings[i].jsValue();
174 if (LIKELY(v.isString()))
175 ropeBuilder.append(asString(v));
177 ropeBuilder.append(v.toString(exec));
179 unsigned newLength = ropeBuilder.length();
180 if (newLength < length)
186 return throwOutOfMemoryError(exec);
188 return JSString::create(*globalData, ropeBuilder.release());
191 ALWAYS_INLINE JSValue jsString(ExecState* exec, JSValue thisValue)
193 unsigned fiberCount = 0;
194 if (LIKELY(thisValue.isString()))
195 fiberCount += asString(thisValue)->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();
206 JSString::RopeBuilder ropeBuilder(fiberCount);
207 if (UNLIKELY(ropeBuilder.isOutOfMemory()))
208 return throwOutOfMemoryError(exec);
210 if (LIKELY(thisValue.isString()))
211 ropeBuilder.append(asString(thisValue));
213 ropeBuilder.append(thisValue.toString(exec));
216 bool overflow = false;
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));
223 ropeBuilder.append(v.toString(exec));
225 unsigned newLength = ropeBuilder.length();
226 if (newLength < length)
232 return throwOutOfMemoryError(exec);
234 JSGlobalData* globalData = &exec->globalData();
235 return JSString::create(*globalData, ropeBuilder.release());
239 inline bool JSValue::equal(ExecState* exec, JSValue v1, JSValue v2)
241 if (v1.isInt32() && v2.isInt32())
244 return equalSlowCase(exec, v1, v2);
247 ALWAYS_INLINE bool JSValue::equalSlowCaseInline(ExecState* exec, JSValue v1, JSValue v2)
250 if (v1.isNumber() && v2.isNumber())
251 return v1.uncheckedGetNumber() == v2.uncheckedGetNumber();
253 bool s1 = v1.isString();
254 bool s2 = v2.isString();
256 return asString(v1)->value(exec) == asString(v2)->value(exec);
258 if (v1.isUndefinedOrNull()) {
259 if (v2.isUndefinedOrNull())
263 return v2.asCell()->structure()->typeInfo().masqueradesAsUndefined();
266 if (v2.isUndefinedOrNull()) {
269 return v1.asCell()->structure()->typeInfo().masqueradesAsUndefined();
275 JSValue p1 = v1.toPrimitive(exec);
276 if (exec->hadException())
279 if (v1.isInt32() && v2.isInt32())
285 JSValue p2 = v2.toPrimitive(exec);
286 if (exec->hadException())
289 if (v1.isInt32() && v2.isInt32())
295 double d1 = v1.toNumber(exec);
296 double d2 = v2.toNumber(exec);
300 if (v1.isBoolean()) {
302 return static_cast<double>(v1.getBoolean()) == v2.uncheckedGetNumber();
303 } else if (v2.isBoolean()) {
305 return v1.uncheckedGetNumber() == static_cast<double>(v2.getBoolean());
313 ALWAYS_INLINE bool JSValue::strictEqualSlowCaseInline(ExecState* exec, JSValue v1, JSValue v2)
315 ASSERT(v1.isCell() && v2.isCell());
317 if (v1.asCell()->isString() && v2.asCell()->isString())
318 return asString(v1)->value(exec) == asString(v2)->value(exec);
323 inline bool JSValue::strictEqual(ExecState* exec, JSValue v1, JSValue v2)
325 if (v1.isInt32() && v2.isInt32())
328 if (v1.isNumber() && v2.isNumber())
329 return v1.uncheckedGetNumber() == v2.uncheckedGetNumber();
331 if (!v1.isCell() || !v2.isCell())
334 return strictEqualSlowCaseInline(exec, v1, v2);
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)
343 if (v1.isInt32() && v2.isInt32())
344 return v1.asInt32() < v2.asInt32();
348 if (v1.getNumber(n1) && v2.getNumber(n2))
351 JSGlobalData* globalData = &callFrame->globalData();
352 if (isJSString(globalData, v1) && isJSString(globalData, v2))
353 return asString(v1)->value(callFrame) < asString(v2)->value(callFrame);
360 wasNotString1 = v1.getPrimitiveNumber(callFrame, n1, p1);
361 wasNotString2 = v2.getPrimitiveNumber(callFrame, n2, p2);
363 wasNotString2 = v2.getPrimitiveNumber(callFrame, n2, p2);
364 wasNotString1 = v1.getPrimitiveNumber(callFrame, n1, p1);
367 if (wasNotString1 | wasNotString2)
369 return asString(p1)->value(callFrame) < asString(p2)->value(callFrame);
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)
378 if (v1.isInt32() && v2.isInt32())
379 return v1.asInt32() <= v2.asInt32();
383 if (v1.getNumber(n1) && v2.getNumber(n2))
386 JSGlobalData* globalData = &callFrame->globalData();
387 if (isJSString(globalData, v1) && isJSString(globalData, v2))
388 return !(asString(v2)->value(callFrame) < asString(v1)->value(callFrame));
395 wasNotString1 = v1.getPrimitiveNumber(callFrame, n1, p1);
396 wasNotString2 = v2.getPrimitiveNumber(callFrame, n2, p2);
398 wasNotString2 = v2.getPrimitiveNumber(callFrame, n2, p2);
399 wasNotString1 = v1.getPrimitiveNumber(callFrame, n1, p1);
402 if (wasNotString1 | wasNotString2)
404 return !(asString(p2)->value(callFrame) < asString(p1)->value(callFrame));
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
416 ALWAYS_INLINE JSValue jsAdd(CallFrame* callFrame, JSValue v1, JSValue v2)
418 double left = 0.0, right;
419 if (v1.getNumber(left) && v2.getNumber(right))
420 return jsNumber(left + right);
424 ? jsString(callFrame, asString(v1), asString(v2))
425 : jsString(callFrame, asString(v1), v2.toPrimitiveString(callFrame));
428 // All other cases are pretty uncommon
429 return jsAddSlowCase(callFrame, v1, v2);
432 inline size_t normalizePrototypeChain(CallFrame* callFrame, JSValue base, JSValue slotBase, const Identifier& propertyName, size_t& slotOffset)
434 JSCell* cell = base.asCell();
437 while (slotBase != cell) {
438 JSValue v = cell->structure()->prototypeForLookup(callFrame);
440 // If we didn't find slotBase in base's prototype chain, then base
441 // must be a proxy for another object.
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);
463 inline size_t normalizePrototypeChain(CallFrame* callFrame, JSCell* base)
467 JSValue v = base->structure()->prototypeForLookup(callFrame);
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());
482 ALWAYS_INLINE JSValue resolveBase(CallFrame* callFrame, Identifier& property, ScopeChainNode* scopeChain, bool isStrictPut)
484 ScopeChainIterator iter = scopeChain->begin();
485 ScopeChainIterator next = iter;
487 ScopeChainIterator end = scopeChain->end();
495 if (isStrictPut && !base->getPropertySlot(callFrame, property, slot))
499 if (base->getPropertySlot(callFrame, property, slot))
506 ASSERT_NOT_REACHED();
511 #endif // Operations_h